1 /*-
2 * SPDX-License-Identifier: BSD-2-Clause
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
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, RESIZE, 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
usage(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] [-L label]\n"
83 " [-x sectors/track] [-y heads/cylinder]\n"
84 " mdconfig -d -u unit [-o [no]force]\n"
85 " mdconfig -r -u unit -s size [-o [no]force]\n"
86 " mdconfig -l [-v] [-n] [-f file] [-u unit]\n"
87 " mdconfig file\n");
88 fprintf(stderr, "\t\ttype = {malloc, vnode, swap}\n");
89 fprintf(stderr, "\t\toption = {cache, cluster, compress, force,\n");
90 fprintf(stderr, "\t\t readonly, reserve, ro, verify}\n");
91 fprintf(stderr, "\t\tsize = %%d (512 byte blocks), %%db (B),\n");
92 fprintf(stderr, "\t\t %%dk (kB), %%dm (MB), %%dg (GB), \n");
93 fprintf(stderr, "\t\t %%dt (TB), or %%dp (PB)\n");
94 exit(1);
95 }
96
97 int
main(int argc,char ** argv)98 main(int argc, char **argv)
99 {
100 int ch, fd, i, vflag;
101 char *p;
102 char *fflag = NULL, *sflag = NULL, *tflag = NULL, *uflag = NULL;
103
104 bzero(&mdio, sizeof(mdio));
105 mdio.md_file = malloc(PATH_MAX);
106 mdio.md_label = malloc(PATH_MAX);
107 if (mdio.md_file == NULL || mdio.md_label == NULL)
108 err(1, "could not allocate memory");
109 vflag = 0;
110 bzero(mdio.md_file, PATH_MAX);
111 bzero(mdio.md_label, PATH_MAX);
112
113 if (argc == 1)
114 usage();
115
116 while ((ch = getopt(argc, argv, "ab:df:lno:rs:S:t:u:vx:y:L:")) != -1) {
117 switch (ch) {
118 case 'a':
119 if (action != UNSET && action != ATTACH)
120 errx(1, "-a is mutually exclusive "
121 "with -d, -r, and -l");
122 action = ATTACH;
123 break;
124 case 'd':
125 if (action != UNSET && action != DETACH)
126 errx(1, "-d is mutually exclusive "
127 "with -a, -r, and -l");
128 action = DETACH;
129 mdio.md_options |= MD_AUTOUNIT;
130 break;
131 case 'r':
132 if (action != UNSET && action != RESIZE)
133 errx(1, "-r is mutually exclusive "
134 "with -a, -d, and -l");
135 action = RESIZE;
136 mdio.md_options |= MD_AUTOUNIT;
137 break;
138 case 'l':
139 if (action != UNSET && action != LIST)
140 errx(1, "-l is mutually exclusive "
141 "with -a, -r, and -d");
142 action = LIST;
143 mdio.md_options |= MD_AUTOUNIT;
144 break;
145 case 'n':
146 nflag = 1;
147 break;
148 case 't':
149 if (tflag != NULL)
150 errx(1, "-t can be passed only once");
151 tflag = optarg;
152 if (!strcmp(optarg, "malloc")) {
153 mdio.md_type = MD_MALLOC;
154 mdio.md_options |= MD_AUTOUNIT | MD_COMPRESS;
155 } else if (!strcmp(optarg, "vnode")) {
156 mdio.md_type = MD_VNODE;
157 mdio.md_options |= MD_CLUSTER | MD_AUTOUNIT | MD_COMPRESS;
158 } else if (!strcmp(optarg, "swap")) {
159 mdio.md_type = MD_SWAP;
160 mdio.md_options |= MD_CLUSTER | MD_AUTOUNIT | MD_COMPRESS;
161 } else if (!strcmp(optarg, "null")) {
162 mdio.md_type = MD_NULL;
163 mdio.md_options |= MD_CLUSTER | MD_AUTOUNIT | MD_COMPRESS;
164 } else
165 errx(1, "unknown type: %s", optarg);
166 break;
167 case 'f':
168 if (fflag != NULL)
169 errx(1, "-f can be passed only once");
170 fflag = realpath(optarg, NULL);
171 if (fflag == NULL)
172 err(1, "realpath");
173 break;
174 case 'o':
175 if (!strcmp(optarg, "async"))
176 mdio.md_options |= MD_ASYNC;
177 else if (!strcmp(optarg, "noasync"))
178 mdio.md_options &= ~MD_ASYNC;
179 else if (!strcmp(optarg, "cache"))
180 mdio.md_options |= MD_CACHE;
181 else if (!strcmp(optarg, "nocache"))
182 mdio.md_options &= ~MD_CACHE;
183 else if (!strcmp(optarg, "cluster"))
184 mdio.md_options |= MD_CLUSTER;
185 else if (!strcmp(optarg, "nocluster"))
186 mdio.md_options &= ~MD_CLUSTER;
187 else if (!strcmp(optarg, "compress"))
188 mdio.md_options |= MD_COMPRESS;
189 else if (!strcmp(optarg, "nocompress"))
190 mdio.md_options &= ~MD_COMPRESS;
191 else if (!strcmp(optarg, "force"))
192 mdio.md_options |= MD_FORCE;
193 else if (!strcmp(optarg, "noforce"))
194 mdio.md_options &= ~MD_FORCE;
195 else if (!strcmp(optarg, "readonly"))
196 mdio.md_options |= MD_READONLY;
197 else if (!strcmp(optarg, "noreadonly"))
198 mdio.md_options &= ~MD_READONLY;
199 else if (!strcmp(optarg, "ro"))
200 mdio.md_options |= MD_READONLY;
201 else if (!strcmp(optarg, "noro"))
202 mdio.md_options &= ~MD_READONLY;
203 else if (!strcmp(optarg, "reserve"))
204 mdio.md_options |= MD_RESERVE;
205 else if (!strcmp(optarg, "noreserve"))
206 mdio.md_options &= ~MD_RESERVE;
207 else if (!strcmp(optarg, "verify"))
208 mdio.md_options |= MD_VERIFY;
209 else if (!strcmp(optarg, "noverify"))
210 mdio.md_options &= ~MD_VERIFY;
211 else
212 errx(1, "unknown option: %s", optarg);
213 break;
214 case 'S':
215 mdio.md_sectorsize = strtoul(optarg, &p, 0);
216 break;
217 case 's':
218 if (sflag != NULL)
219 errx(1, "-s can be passed only once");
220 sflag = optarg;
221 mdio.md_mediasize = (off_t)strtoumax(optarg, &p, 0);
222 if (p == NULL || *p == '\0')
223 mdio.md_mediasize *= DEV_BSIZE;
224 else if (*p == 'b' || *p == 'B')
225 ; /* do nothing */
226 else if (*p == 'k' || *p == 'K')
227 mdio.md_mediasize <<= 10;
228 else if (*p == 'm' || *p == 'M')
229 mdio.md_mediasize <<= 20;
230 else if (*p == 'g' || *p == 'G')
231 mdio.md_mediasize <<= 30;
232 else if (*p == 't' || *p == 'T') {
233 mdio.md_mediasize <<= 30;
234 mdio.md_mediasize <<= 10;
235 } else if (*p == 'p' || *p == 'P') {
236 mdio.md_mediasize <<= 30;
237 mdio.md_mediasize <<= 20;
238 } else
239 errx(1, "unknown suffix on -s argument");
240 break;
241 case 'u':
242 if (!strncmp(optarg, _PATH_DEV, sizeof(_PATH_DEV) - 1))
243 optarg += sizeof(_PATH_DEV) - 1;
244 if (!strncmp(optarg, MD_NAME, sizeof(MD_NAME) - 1))
245 optarg += sizeof(MD_NAME) - 1;
246 uflag = optarg;
247 break;
248 case 'v':
249 vflag = OPT_VERBOSE;
250 break;
251 case 'x':
252 mdio.md_fwsectors = strtoul(optarg, &p, 0);
253 break;
254 case 'y':
255 mdio.md_fwheads = strtoul(optarg, &p, 0);
256 break;
257 case 'L':
258 strlcpy(mdio.md_label, optarg, PATH_MAX);
259 break;
260 default:
261 usage();
262 }
263 }
264
265 argc -= optind;
266 argv += optind;
267
268 if (action == UNSET)
269 action = ATTACH;
270
271 if (action == ATTACH) {
272 if (tflag == NULL) {
273 /*
274 * Try to infer the type based on other arguments.
275 */
276 if (fflag != NULL || argc > 0) {
277 /* Imply ``-t vnode'' */
278 mdio.md_type = MD_VNODE;
279 mdio.md_options |= MD_CLUSTER | MD_AUTOUNIT |
280 MD_COMPRESS;
281 } else if (sflag != NULL) {
282 /* Imply ``-t swap'' */
283 mdio.md_type = MD_SWAP;
284 mdio.md_options |= MD_CLUSTER | MD_AUTOUNIT |
285 MD_COMPRESS;
286 } else
287 errx(1, "unable to determine type");
288 }
289
290 if ((fflag != NULL || argc > 0) && mdio.md_type != MD_VNODE)
291 errx(1, "only -t vnode can be used with file name");
292
293 if (mdio.md_type == MD_VNODE) {
294 if (fflag != NULL) {
295 if (argc != 0)
296 usage();
297 md_set_file(fflag);
298 } else {
299 if (argc != 1)
300 usage();
301 md_set_file(*argv);
302 }
303
304 if ((mdio.md_options & MD_READONLY) == 0 &&
305 access(mdio.md_file, W_OK) < 0 &&
306 (errno == EACCES || errno == EPERM ||
307 errno == EROFS)) {
308 warnx("WARNING: opening backing store: %s "
309 "readonly", mdio.md_file);
310 mdio.md_options |= MD_READONLY;
311 }
312 }
313
314 if ((mdio.md_type == MD_MALLOC || mdio.md_type == MD_SWAP ||
315 mdio.md_type == MD_NULL) && sflag == NULL)
316 errx(1, "must specify -s for -t malloc, -t swap, "
317 "or -t null");
318 if (mdio.md_type == MD_VNODE && mdio.md_file[0] == '\0')
319 errx(1, "must specify -f for -t vnode");
320 } else {
321 if (mdio.md_sectorsize != 0)
322 errx(1, "-S can only be used with -a");
323 if (action != RESIZE && sflag != NULL)
324 errx(1, "-s can only be used with -a and -r");
325 if (mdio.md_fwsectors != 0)
326 errx(1, "-x can only be used with -a");
327 if (mdio.md_fwheads != 0)
328 errx(1, "-y can only be used with -a");
329 if (fflag != NULL && action != LIST)
330 errx(1, "-f can only be used with -a and -l");
331 if (tflag != NULL)
332 errx(1, "-t can only be used with -a");
333 if (argc > 0)
334 errx(1, "file can only be used with -a");
335 if ((action != DETACH && action != RESIZE) &&
336 (mdio.md_options & ~MD_AUTOUNIT) != 0)
337 errx(1, "-o can only be used with -a, -d, and -r");
338 if (action == DETACH &&
339 (mdio.md_options & ~(MD_FORCE | MD_AUTOUNIT)) != 0)
340 errx(1, "only -o [no]force can be used with -d");
341 if (action == RESIZE &&
342 (mdio.md_options & ~(MD_FORCE | MD_RESERVE | MD_AUTOUNIT)) != 0)
343 errx(1, "only -o [no]force and -o [no]reserve can be used with -r");
344 }
345
346 if (action == RESIZE && sflag == NULL)
347 errx(1, "must specify -s for -r");
348
349 if (action != LIST && vflag == OPT_VERBOSE)
350 errx(1, "-v can only be used with -l");
351
352 if (uflag != NULL) {
353 mdio.md_unit = strtoul(uflag, &p, 0);
354 if (mdio.md_unit == (unsigned)ULONG_MAX || *p != '\0')
355 errx(1, "bad unit: %s", uflag);
356 mdio.md_options &= ~MD_AUTOUNIT;
357 }
358
359 mdio.md_version = MDIOVERSION;
360
361 if (!kld_isloaded("g_md") && kld_load("geom_md") == -1)
362 err(1, "failed to load geom_md module");
363
364 fd = open(_PATH_DEV MDCTL_NAME, O_RDWR, 0);
365 if (fd < 0)
366 err(1, "open(%s%s)", _PATH_DEV, MDCTL_NAME);
367
368 if (action == ATTACH) {
369 i = ioctl(fd, MDIOCATTACH, &mdio);
370 if (i < 0)
371 err(1, "ioctl(%s%s)", _PATH_DEV, MDCTL_NAME);
372 if (mdio.md_options & MD_AUTOUNIT)
373 printf("%s%d\n", nflag ? "" : MD_NAME, mdio.md_unit);
374 } else if (action == DETACH) {
375 if (mdio.md_options & MD_AUTOUNIT)
376 errx(1, "-d requires -u");
377 i = ioctl(fd, MDIOCDETACH, &mdio);
378 if (i < 0)
379 err(1, "ioctl(%s%s)", _PATH_DEV, MDCTL_NAME);
380 } else if (action == RESIZE) {
381 if (mdio.md_options & MD_AUTOUNIT)
382 errx(1, "-r requires -u");
383 i = ioctl(fd, MDIOCRESIZE, &mdio);
384 if (i < 0)
385 err(1, "ioctl(%s%s)", _PATH_DEV, MDCTL_NAME);
386 } else if (action == LIST) {
387 if (mdio.md_options & MD_AUTOUNIT) {
388 /*
389 * Listing all devices. This is why we pass NULL
390 * together with OPT_LIST.
391 */
392 return (md_list(NULL, OPT_LIST | vflag, fflag));
393 } else
394 return (md_query(uflag, vflag, fflag));
395 } else
396 usage();
397 close(fd);
398 return (0);
399 }
400
401 static void
md_set_file(const char * fn)402 md_set_file(const char *fn)
403 {
404 struct stat sb;
405 int fd;
406
407 if (realpath(fn, mdio.md_file) == NULL)
408 err(1, "could not find full path for %s", fn);
409 fd = open(mdio.md_file, O_RDONLY);
410 if (fd < 0)
411 err(1, "could not open %s", fn);
412 if (fstat(fd, &sb) == -1)
413 err(1, "could not stat %s", fn);
414 if (!S_ISREG(sb.st_mode))
415 errx(1, "%s is not a regular file", fn);
416 if (mdio.md_mediasize == 0)
417 mdio.md_mediasize = sb.st_size;
418 close(fd);
419 }
420
421 /*
422 * Lists md(4) disks. Is used also as a query routine, since it handles XML
423 * interface. 'units' can be NULL for listing memory disks. It might be
424 * comma-separated string containing md(4) disk names. 'opt' distinguished
425 * between list and query mode.
426 */
427 static int
md_list(const char * units,int opt,const char * fflag)428 md_list(const char *units, int opt, const char *fflag)
429 {
430 struct gmesh gm;
431 struct gprovider *pp;
432 struct gconf *gc;
433 struct gident *gid;
434 struct devstat *gsp;
435 struct ggeom *gg;
436 struct gclass *gcl;
437 void *sq;
438 int retcode, ffound, ufound;
439 char *length;
440 const char *type, *file, *label;
441
442 type = file = length = NULL;
443
444 retcode = geom_gettree(&gm);
445 if (retcode != 0)
446 return (-1);
447 retcode = geom_stats_open();
448 if (retcode != 0)
449 return (-1);
450 sq = geom_stats_snapshot_get();
451 if (sq == NULL)
452 return (-1);
453
454 ffound = ufound = 0;
455 while ((gsp = geom_stats_snapshot_next(sq)) != NULL) {
456 gid = geom_lookupid(&gm, gsp->id);
457 if (gid == NULL)
458 continue;
459 if (gid->lg_what == ISPROVIDER) {
460 pp = gid->lg_ptr;
461 gg = pp->lg_geom;
462 gcl = gg->lg_class;
463 if (strcmp(gcl->lg_name, CLASS_NAME_MD) != 0)
464 continue;
465 if ((opt & OPT_UNIT) && (units != NULL)) {
466 retcode = md_find(units, pp->lg_name);
467 if (retcode != 1)
468 continue;
469 else
470 ufound = 1;
471 }
472 gc = &pp->lg_config;
473 type = geom_config_get(gc, "type");
474 if (type != NULL && (strcmp(type, "vnode") == 0 ||
475 strcmp(type, "preload") == 0)) {
476 file = geom_config_get(gc, "file");
477 if (fflag != NULL &&
478 strcmp(fflag, file) != 0)
479 continue;
480 else
481 ffound = 1;
482 } else if (fflag != NULL)
483 continue;
484 if (nflag && strncmp(pp->lg_name, MD_NAME, 2) == 0)
485 printf("%s", pp->lg_name + 2);
486 else
487 printf("%s", pp->lg_name);
488
489 if (opt & OPT_VERBOSE ||
490 ((opt & OPT_UNIT) && fflag == NULL)) {
491 length = geom_config_get(gc, "length");
492 printf("\t%s\t", type);
493 if (length != NULL)
494 md_prthumanval(length);
495 if (file == NULL)
496 file = "-";
497 printf("\t%s", file);
498 file = NULL;
499 label = geom_config_get(gc, "label");
500 if (label == NULL)
501 label = "";
502 printf("\t%s", label);
503 }
504 opt |= OPT_DONE;
505 if ((opt & OPT_LIST) && !(opt & OPT_VERBOSE))
506 printf(" ");
507 else
508 printf("\n");
509 }
510 }
511 if ((opt & OPT_LIST) && (opt & OPT_DONE) && !(opt & OPT_VERBOSE))
512 printf("\n");
513 /* XXX: Check if it's enough to clean everything. */
514 geom_stats_snapshot_free(sq);
515 if (opt & OPT_UNIT) {
516 if (((fflag == NULL) && ufound) ||
517 ((fflag == NULL) && (units != NULL) && ufound) ||
518 ((fflag != NULL) && ffound) ||
519 ((fflag != NULL) && (units != NULL) && ufound && ffound))
520 return (0);
521 } else if (opt & OPT_LIST) {
522 if ((fflag == NULL) ||
523 ((fflag != NULL) && ffound))
524 return (0);
525 }
526 return (-1);
527 }
528
529 /*
530 * Returns value of 'name' from gconfig structure.
531 */
532 static char *
geom_config_get(struct gconf * g,const char * name)533 geom_config_get(struct gconf *g, const char *name)
534 {
535 struct gconfig *gce;
536
537 LIST_FOREACH(gce, g, lg_config) {
538 if (strcmp(gce->lg_name, name) == 0)
539 return (gce->lg_val);
540 }
541 return (NULL);
542 }
543
544 /*
545 * List is comma separated list of MD disks. name is a
546 * device name we look for. Returns 1 if found and 0
547 * otherwise.
548 */
549 static int
md_find(const char * list,const char * name)550 md_find(const char *list, const char *name)
551 {
552 int ret;
553 char num[PATH_MAX];
554 char *ptr, *p, *u;
555
556 ret = 0;
557 ptr = strdup(list);
558 if (ptr == NULL)
559 return (-1);
560 for (p = ptr; (u = strsep(&p, ",")) != NULL;) {
561 if (strncmp(u, _PATH_DEV, sizeof(_PATH_DEV) - 1) == 0)
562 u += sizeof(_PATH_DEV) - 1;
563 /* Just in case user specified number instead of full name */
564 snprintf(num, sizeof(num), "%s%s", MD_NAME, u);
565 if (strcmp(u, name) == 0 || strcmp(num, name) == 0) {
566 ret = 1;
567 break;
568 }
569 }
570 free(ptr);
571 return (ret);
572 }
573
574 static void
md_prthumanval(char * length)575 md_prthumanval(char *length)
576 {
577 char buf[6];
578 uintmax_t bytes;
579 char *endptr;
580
581 errno = 0;
582 bytes = strtoumax(length, &endptr, 10);
583 if (errno != 0 || *endptr != '\0' || bytes > INT64_MAX)
584 return;
585 humanize_number(buf, sizeof(buf), (int64_t)bytes, "",
586 HN_AUTOSCALE, HN_B | HN_NOSPACE | HN_DECIMAL);
587 (void)printf("%6s", buf);
588 }
589
590 static int
md_query(const char * name,const int opt,const char * fflag)591 md_query(const char *name, const int opt, const char *fflag)
592 {
593
594 return (md_list(name, opt | OPT_UNIT, fflag));
595 }
596