]> CyberLeo.Net >> Repos - FreeBSD/releng/9.2.git/blob - sbin/mdconfig/mdconfig.c
- Copy stable/9 to releng/9.2 as part of the 9.2-RELEASE cycle.
[FreeBSD/releng/9.2.git] / sbin / mdconfig / mdconfig.c
1 /*-
2  * Copyright (c) 2000-2004 Poul-Henning Kamp <phk@FreeBSD.org>
3  * Copyright (c) 2012 The FreeBSD Foundation
4  * All rights reserved.
5  *
6  * Portions of this software were developed by Edward Tomasz Napierala
7  * under sponsorship from the FreeBSD Foundation.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28  * SUCH DAMAGE.
29  *
30  * $FreeBSD$
31  */
32
33 #include <sys/param.h>
34 #include <sys/devicestat.h>
35 #include <sys/ioctl.h>
36 #include <sys/linker.h>
37 #include <sys/mdioctl.h>
38 #include <sys/module.h>
39 #include <sys/resource.h>
40 #include <sys/stat.h>
41
42 #include <assert.h>
43 #include <devstat.h>
44 #include <err.h>
45 #include <errno.h>
46 #include <fcntl.h>
47 #include <inttypes.h>
48 #include <libgeom.h>
49 #include <libutil.h>
50 #include <paths.h>
51 #include <stdarg.h>
52 #include <stdio.h>
53 #include <stdlib.h>
54 #include <string.h>
55 #include <unistd.h>
56
57 static struct md_ioctl mdio;
58 static enum {UNSET, ATTACH, DETACH, LIST} action = UNSET;
59 static int nflag;
60
61 static void usage(void);
62 static void md_set_file(const char *);
63 static int md_find(const char *, const char *);
64 static int md_query(const char *, const int, const char *);
65 static int md_list(const char *, int, const char *);
66 static char *geom_config_get(struct gconf *g, const char *name);
67 static void md_prthumanval(char *length);
68
69 #define OPT_VERBOSE     0x01
70 #define OPT_UNIT        0x02
71 #define OPT_DONE        0x04
72 #define OPT_LIST        0x10
73
74 #define CLASS_NAME_MD   "MD"
75
76 static void
77 usage(void)
78 {
79
80         fprintf(stderr,
81 "usage: mdconfig -a -t type [-n] [-o [no]option] ... [-f file]\n"
82 "                [-s size] [-S sectorsize] [-u unit]\n"
83 "                [-x sectors/track] [-y heads/cylinder]\n"
84 "       mdconfig -d -u unit [-o [no]force]\n"
85 "       mdconfig -l [-v] [-n] [-f file] [-u unit]\n"
86 "       mdconfig file\n");
87         fprintf(stderr, "\t\ttype = {malloc, vnode, swap}\n");
88         fprintf(stderr, "\t\toption = {cluster, compress, reserve}\n");
89         fprintf(stderr, "\t\tsize = %%d (512 byte blocks), %%db (B),\n");
90         fprintf(stderr, "\t\t       %%dk (kB), %%dm (MB), %%dg (GB) or\n");
91         fprintf(stderr, "\t\t       %%dt (TB)\n");
92         exit(1);
93 }
94
95 int
96 main(int argc, char **argv)
97 {
98         int ch, fd, i, vflag;
99         char *p;
100         char *fflag = NULL, *tflag = NULL, *uflag = NULL;
101
102         bzero(&mdio, sizeof(mdio));
103         mdio.md_file = malloc(PATH_MAX);
104         if (mdio.md_file == NULL)
105                 err(1, "could not allocate memory");
106         vflag = 0;
107         bzero(mdio.md_file, PATH_MAX);
108
109         if (argc == 1)
110                 usage();
111
112         while ((ch = getopt(argc, argv, "ab:df:lno:s:S:t:u:vx:y:")) != -1) {
113                 switch (ch) {
114                 case 'a':
115                         if (action != UNSET && action != ATTACH)
116                                 errx(1,
117                                     "-a is mutually exclusive with -d and -l");
118                         action = ATTACH;
119                         break;
120                 case 'd':
121                         if (action != UNSET && action != DETACH)
122                                 errx(1,
123                                     "-d is mutually exclusive with -a and -l");
124                         action = DETACH;
125                         mdio.md_options |= MD_AUTOUNIT;
126                         break;
127                 case 'l':
128                         if (action != UNSET && action != LIST)
129                                 errx(1,
130                                     "-l is mutually exclusive with -a and -d");
131                         action = LIST;
132                         mdio.md_options |= MD_AUTOUNIT;
133                         break;
134                 case 'n':
135                         nflag = 1;
136                         break;
137                 case 't':
138                         if (tflag != NULL)
139                                 errx(1, "-t can be passed only once");
140                         tflag = optarg;
141                         if (!strcmp(optarg, "malloc")) {
142                                 mdio.md_type = MD_MALLOC;
143                                 mdio.md_options |= MD_AUTOUNIT | MD_COMPRESS;
144                         } else if (!strcmp(optarg, "preload")) {
145                                 mdio.md_type = MD_PRELOAD;
146                         } else if (!strcmp(optarg, "vnode")) {
147                                 mdio.md_type = MD_VNODE;
148                                 mdio.md_options |= MD_CLUSTER | MD_AUTOUNIT | MD_COMPRESS;
149                         } else if (!strcmp(optarg, "swap")) {
150                                 mdio.md_type = MD_SWAP;
151                                 mdio.md_options |= MD_CLUSTER | MD_AUTOUNIT | MD_COMPRESS;
152                         } else
153                                 errx(1, "unknown type: %s", optarg);
154                         break;
155                 case 'f':
156                         if (fflag != NULL)
157                                 errx(1, "-f can be passed only once");
158                         fflag = optarg;
159                         break;
160                 case 'o':
161                         if (!strcmp(optarg, "async"))
162                                 mdio.md_options |= MD_ASYNC;
163                         else if (!strcmp(optarg, "noasync"))
164                                 mdio.md_options &= ~MD_ASYNC;
165                         else if (!strcmp(optarg, "cluster"))
166                                 mdio.md_options |= MD_CLUSTER;
167                         else if (!strcmp(optarg, "nocluster"))
168                                 mdio.md_options &= ~MD_CLUSTER;
169                         else if (!strcmp(optarg, "compress"))
170                                 mdio.md_options |= MD_COMPRESS;
171                         else if (!strcmp(optarg, "nocompress"))
172                                 mdio.md_options &= ~MD_COMPRESS;
173                         else if (!strcmp(optarg, "force"))
174                                 mdio.md_options |= MD_FORCE;
175                         else if (!strcmp(optarg, "noforce"))
176                                 mdio.md_options &= ~MD_FORCE;
177                         else if (!strcmp(optarg, "readonly"))
178                                 mdio.md_options |= MD_READONLY;
179                         else if (!strcmp(optarg, "noreadonly"))
180                                 mdio.md_options &= ~MD_READONLY;
181                         else if (!strcmp(optarg, "reserve"))
182                                 mdio.md_options |= MD_RESERVE;
183                         else if (!strcmp(optarg, "noreserve"))
184                                 mdio.md_options &= ~MD_RESERVE;
185                         else
186                                 errx(1, "unknown option: %s", optarg);
187                         break;
188                 case 'S':
189                         mdio.md_sectorsize = strtoul(optarg, &p, 0);
190                         break;
191                 case 's':
192                         mdio.md_mediasize = (off_t)strtoumax(optarg, &p, 0);
193                         if (p == NULL || *p == '\0')
194                                 mdio.md_mediasize *= DEV_BSIZE;
195                         else if (*p == 'b' || *p == 'B')
196                                 ; /* do nothing */
197                         else if (*p == 'k' || *p == 'K')
198                                 mdio.md_mediasize <<= 10;
199                         else if (*p == 'm' || *p == 'M')
200                                 mdio.md_mediasize <<= 20;
201                         else if (*p == 'g' || *p == 'G')
202                                 mdio.md_mediasize <<= 30;
203                         else if (*p == 't' || *p == 'T') {
204                                 mdio.md_mediasize <<= 30;
205                                 mdio.md_mediasize <<= 10;
206                         } else
207                                 errx(1, "unknown suffix on -s argument");
208                         break;
209                 case 'u':
210                         if (!strncmp(optarg, _PATH_DEV, sizeof(_PATH_DEV) - 1))
211                                 optarg += sizeof(_PATH_DEV) - 1;
212                         if (!strncmp(optarg, MD_NAME, sizeof(MD_NAME) - 1))
213                                 optarg += sizeof(MD_NAME) - 1;
214                         uflag = optarg;
215                         break;
216                 case 'v':
217                         vflag = OPT_VERBOSE;
218                         break;
219                 case 'x':
220                         mdio.md_fwsectors = strtoul(optarg, &p, 0);
221                         break;
222                 case 'y':
223                         mdio.md_fwheads = strtoul(optarg, &p, 0);
224                         break;
225                 default:
226                         usage();
227                 }
228         }
229
230         argc -= optind;
231         argv += optind;
232
233         if (action == UNSET)
234                 action = ATTACH;
235
236         if (action == ATTACH) {
237                 if (tflag == NULL) {
238                         /*
239                          * Try to infer the type based on other arguments.
240                          */
241                         if (fflag != NULL || argc > 0) {
242                                 /* Imply ``-t vnode'' */
243                                 mdio.md_type = MD_VNODE;
244                                 mdio.md_options |= MD_CLUSTER | MD_AUTOUNIT |
245                                     MD_COMPRESS;
246                         } else if (mdio.md_mediasize != 0) {
247                                 /* Imply ``-t swap'' */
248                                 mdio.md_type = MD_SWAP;
249                                 mdio.md_options |= MD_CLUSTER | MD_AUTOUNIT |
250                                     MD_COMPRESS;
251                         } else
252                                 errx(1, "unable to determine type");
253                 }
254
255                 if ((fflag != NULL || argc > 0) && mdio.md_type != MD_VNODE)
256                         errx(1, "only -t vnode can be used with file name");
257
258                 if (mdio.md_type == MD_VNODE) {
259                         if (fflag != NULL) {
260                                 if (argc != 0)
261                                         usage();
262                                 md_set_file(fflag);
263                         } else {
264                                 if (argc != 1)
265                                         usage();
266                                 md_set_file(*argv);
267                         }
268
269                         if ((mdio.md_options & MD_READONLY) == 0 &&
270                             access(mdio.md_file, W_OK) < 0 &&
271                             (errno == EACCES || errno == EPERM ||
272                              errno == EROFS)) {
273                                 warnx("WARNING: opening backing store: %s "
274                                     "readonly", mdio.md_file);
275                                 mdio.md_options |= MD_READONLY;
276                         }
277                 }
278
279                 if ((mdio.md_type == MD_MALLOC || mdio.md_type == MD_SWAP) &&
280                     mdio.md_mediasize == 0)
281                         errx(1, "must specify -s for -t malloc or -t swap");
282                 if (mdio.md_type == MD_VNODE && mdio.md_file[0] == '\0')
283                         errx(1, "must specify -f for -t vnode");
284         } else {
285                 if (mdio.md_sectorsize != 0)
286                         errx(1, "-S can only be used with -a");
287                 if (mdio.md_mediasize != 0)
288                         errx(1, "-s can only be used with -a");
289                 if (mdio.md_fwsectors != 0)
290                         errx(1, "-x can only be used with -a");
291                 if (mdio.md_fwheads != 0)
292                         errx(1, "-y can only be used with -a");
293                 if (fflag != NULL && action != LIST)
294                         errx(1, "-f can only be used with -a and -l");
295                 if (tflag != NULL)
296                         errx(1, "-t can only be used with -a");
297                 if (argc > 0)
298                         errx(1, "file can only be used with -a");
299                 if (action != DETACH && (mdio.md_options & ~MD_AUTOUNIT) != 0)
300                         errx(1, "-o can only be used with -a and -d");
301                 if (action == DETACH &&
302                     (mdio.md_options & ~(MD_FORCE | MD_AUTOUNIT)) != 0)
303                         errx(1, "only -o [no]force can be used with -d");
304         }
305
306         if (action != LIST && vflag == OPT_VERBOSE)
307                 errx(1, "-v can only be used with -l");
308
309         if (uflag != NULL) {
310                 mdio.md_unit = strtoul(uflag, &p, 0);
311                 if (mdio.md_unit == (unsigned)ULONG_MAX || *p != '\0')
312                         errx(1, "bad unit: %s", uflag);
313                 mdio.md_options &= ~MD_AUTOUNIT;
314         }
315
316         mdio.md_version = MDIOVERSION;
317
318         if (!kld_isloaded("g_md") && kld_load("geom_md") == -1)
319                 err(1, "failed to load geom_md module");
320
321         fd = open(_PATH_DEV MDCTL_NAME, O_RDWR, 0);
322         if (fd < 0)
323                 err(1, "open(%s%s)", _PATH_DEV, MDCTL_NAME);
324
325         if (action == ATTACH) {
326                 i = ioctl(fd, MDIOCATTACH, &mdio);
327                 if (i < 0)
328                         err(1, "ioctl(%s%s)", _PATH_DEV, MDCTL_NAME);
329                 if (mdio.md_options & MD_AUTOUNIT)
330                         printf("%s%d\n", nflag ? "" : MD_NAME, mdio.md_unit);
331         } else if (action == DETACH) {
332                 if (mdio.md_options & MD_AUTOUNIT)
333                         errx(1, "-d requires -u");
334                 i = ioctl(fd, MDIOCDETACH, &mdio);
335                 if (i < 0)
336                         err(1, "ioctl(%s%s)", _PATH_DEV, MDCTL_NAME);
337         } else if (action == LIST) {
338                 if (mdio.md_options & MD_AUTOUNIT) {
339                         /*
340                          * Listing all devices. This is why we pass NULL
341                          * together with OPT_LIST.
342                          */
343                         return (md_list(NULL, OPT_LIST | vflag, fflag));
344                 } else
345                         return (md_query(uflag, vflag, fflag));
346
347         } else
348                 usage();
349         close(fd);
350         return (0);
351 }
352
353 static void
354 md_set_file(const char *fn)
355 {
356         struct stat sb;
357         int fd;
358
359         if (realpath(fn, mdio.md_file) == NULL)
360                 err(1, "could not find full path for %s", fn);
361         fd = open(mdio.md_file, O_RDONLY);
362         if (fd < 0)
363                 err(1, "could not open %s", fn);
364         if (fstat(fd, &sb) == -1)
365                 err(1, "could not stat %s", fn);
366         if (!S_ISREG(sb.st_mode))
367                 errx(1, "%s is not a regular file", fn);
368         if (mdio.md_mediasize == 0)
369                 mdio.md_mediasize = sb.st_size;
370         close(fd);
371 }
372
373 /*
374  * Lists md(4) disks. Is used also as a query routine, since it handles XML
375  * interface. 'units' can be NULL for listing memory disks. It might be
376  * coma-separated string containing md(4) disk names. 'opt' distinguished
377  * between list and query mode.
378  */
379 static int
380 md_list(const char *units, int opt, const char *fflag)
381 {
382         struct gmesh gm;
383         struct gprovider *pp;
384         struct gconf *gc;
385         struct gident *gid;
386         struct devstat *gsp;
387         struct ggeom *gg;
388         struct gclass *gcl;
389         void *sq;
390         int retcode, ffound, ufound;
391         char *type, *file, *length;
392
393         type = file = length = NULL;
394
395         retcode = geom_gettree(&gm);
396         if (retcode != 0)
397                 return (-1);
398         retcode = geom_stats_open();
399         if (retcode != 0)
400                 return (-1);
401         sq = geom_stats_snapshot_get();
402         if (sq == NULL)
403                 return (-1);
404
405         ffound = ufound = 0;
406         while ((gsp = geom_stats_snapshot_next(sq)) != NULL) {
407                 gid = geom_lookupid(&gm, gsp->id);
408                 if (gid == NULL)
409                         continue;
410                 if (gid->lg_what == ISPROVIDER) {
411                         pp = gid->lg_ptr;
412                         gg = pp->lg_geom;
413                         gcl = gg->lg_class;
414                         if (strcmp(gcl->lg_name, CLASS_NAME_MD) != 0)
415                                 continue;
416                         if ((opt & OPT_UNIT) && (units != NULL)) {
417                                 retcode = md_find(units, pp->lg_name);
418                                 if (retcode != 1)
419                                         continue;
420                                 else
421                                         ufound = 1;
422                         }
423                         gc = &pp->lg_config;
424                         type = geom_config_get(gc, "type");
425                         if (strcmp(type, "vnode") == 0) {
426                                 file = geom_config_get(gc, "file");
427                                 if (fflag != NULL &&
428                                     strcmp(fflag, file) != 0)
429                                         continue;
430                                 else
431                                         ffound = 1;
432                         } else if (fflag != NULL)
433                                         continue;
434                         if (nflag && strncmp(pp->lg_name, MD_NAME, 2) == 0)
435                                 printf("%s", pp->lg_name + 2);
436                         else
437                                 printf("%s", pp->lg_name);
438
439                         if (opt & OPT_VERBOSE ||
440                             ((opt & OPT_UNIT) && fflag == NULL)) {
441                                 length = geom_config_get(gc, "length");
442                                 printf("\t%s\t", type);
443                                 if (length != NULL)
444                                         md_prthumanval(length);
445                                 if (file != NULL) {
446                                         printf("\t%s", file);
447                                         file = NULL;
448                                 }
449                         }
450                         opt |= OPT_DONE;
451                         if ((opt & OPT_LIST) && !(opt & OPT_VERBOSE))
452                                 printf(" ");
453                         else
454                                 printf("\n");
455                 }
456         }
457         if ((opt & OPT_LIST) && (opt & OPT_DONE) && !(opt & OPT_VERBOSE))
458                 printf("\n");
459         /* XXX: Check if it's enough to clean everything. */
460         geom_stats_snapshot_free(sq);
461         if (((opt & OPT_UNIT) && (fflag == NULL) && ufound) ||
462             ((opt & OPT_UNIT) == 0 && (fflag != NULL) && ffound) ||
463             ((opt & OPT_UNIT) && (fflag != NULL) && ufound && ffound))
464                 return (0);
465         else
466                 return (-1);
467 }
468
469 /*
470  * Returns value of 'name' from gconfig structure.
471  */
472 static char *
473 geom_config_get(struct gconf *g, const char *name)
474 {
475         struct gconfig *gce;
476
477         LIST_FOREACH(gce, g, lg_config) {
478                 if (strcmp(gce->lg_name, name) == 0)
479                         return (gce->lg_val);
480         }
481         return (NULL);
482 }
483
484 /*
485  * List is comma separated list of MD disks. name is a
486  * device name we look for.  Returns 1 if found and 0
487  * otherwise.
488  */
489 static int
490 md_find(const char *list, const char *name)
491 {
492         int ret;
493         char num[PATH_MAX];
494         char *ptr, *p, *u;
495
496         ret = 0;
497         ptr = strdup(list);
498         if (ptr == NULL)
499                 return (-1);
500         for (p = ptr; (u = strsep(&p, ",")) != NULL;) {
501                 if (strncmp(u, _PATH_DEV, sizeof(_PATH_DEV) - 1) == 0)
502                         u += sizeof(_PATH_DEV) - 1;
503                 /* Just in case user specified number instead of full name */
504                 snprintf(num, sizeof(num), "%s%s", MD_NAME, u);
505                 if (strcmp(u, name) == 0 || strcmp(num, name) == 0) {
506                         ret = 1;
507                         break;
508                 }
509         }
510         free(ptr);
511         return (ret);
512 }
513
514 static void
515 md_prthumanval(char *length)
516 {
517         char buf[6];
518         uintmax_t bytes;
519         char *endptr;
520
521         errno = 0;
522         bytes = strtoumax(length, &endptr, 10);
523         if (errno != 0 || *endptr != '\0' || bytes > INT64_MAX)
524                 return;
525         humanize_number(buf, sizeof(buf), (int64_t)bytes, "",
526             HN_AUTOSCALE, HN_B | HN_NOSPACE | HN_DECIMAL);
527         (void)printf("%6s", buf);
528 }
529
530 static int
531 md_query(const char *name, const int opt, const char *fflag)
532 {
533
534         return (md_list(name, opt | OPT_UNIT, fflag));
535 }