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