]> CyberLeo.Net >> Repos - FreeBSD/releng/7.2.git/blob - sbin/mdconfig/mdconfig.c
Create releng/7.2 from stable/7 in preparation for 7.2-RELEASE.
[FreeBSD/releng/7.2.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 [-v] [-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, vflag;
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         vflag = 0;
84         bzero(mdio.md_file, PATH_MAX);
85         for (;;) {
86                 ch = getopt(argc, argv, "ab:df:lno:s:S:t:u:vx:y:");
87                 if (ch == -1)
88                         break;
89                 switch (ch) {
90                 case 'a':
91                         if (cmdline != 0)
92                                 usage();
93                         action = ATTACH;
94                         cmdline = 1;
95                         break;
96                 case 'd':
97                         if (cmdline != 0)
98                                 usage();
99                         action = DETACH;
100                         mdio.md_options = MD_AUTOUNIT;
101                         cmdline = 3;
102                         break;
103                 case 'l':
104                         if (cmdline != 0)
105                                 usage();
106                         action = LIST;
107                         mdio.md_options = MD_AUTOUNIT;
108                         cmdline = 3;
109                         break;
110                 case 'n':
111                         nflag = 1;
112                         break;
113                 case 't':
114                         if (cmdline != 1)
115                                 usage();
116                         if (!strcmp(optarg, "malloc")) {
117                                 mdio.md_type = MD_MALLOC;
118                                 mdio.md_options = MD_AUTOUNIT | MD_COMPRESS;
119                         } else if (!strcmp(optarg, "preload")) {
120                                 mdio.md_type = MD_PRELOAD;
121                                 mdio.md_options = 0;
122                         } else if (!strcmp(optarg, "vnode")) {
123                                 mdio.md_type = MD_VNODE;
124                                 mdio.md_options = MD_CLUSTER | MD_AUTOUNIT | MD_COMPRESS;
125                         } else if (!strcmp(optarg, "swap")) {
126                                 mdio.md_type = MD_SWAP;
127                                 mdio.md_options = MD_CLUSTER | MD_AUTOUNIT | MD_COMPRESS;
128                         } else {
129                                 usage();
130                         }
131                         cmdline=2;
132                         break;
133                 case 'f':
134                         if (cmdline == 0) {
135                                 action = ATTACH;
136                                 cmdline = 1;
137                         }
138                         if (cmdline == 1) {
139                                 /* Imply ``-t vnode'' */
140                                 mdio.md_type = MD_VNODE;
141                                 mdio.md_options = MD_CLUSTER | MD_AUTOUNIT | MD_COMPRESS;
142                                 cmdline = 2;
143                         }
144                         if (cmdline != 2)
145                                 usage();
146                         if (realpath(optarg, mdio.md_file) == NULL) {
147                                 err(1, "could not find full path for %s",
148                                     optarg);
149                         }
150                         fd = open(mdio.md_file, O_RDONLY);
151                         if (fd < 0)
152                                 err(1, "could not open %s", optarg);
153                         else if (mdio.md_mediasize == 0) {
154                                 struct stat sb;
155
156                                 if (fstat(fd, &sb) == -1)
157                                         err(1, "could not stat %s", optarg);
158                                 mdio.md_mediasize = sb.st_size;
159                         }
160                         close(fd);
161                         break;
162                 case 'o':
163                         if (cmdline != 2)
164                                 usage();
165                         if (!strcmp(optarg, "async"))
166                                 mdio.md_options |= MD_ASYNC;
167                         else if (!strcmp(optarg, "noasync"))
168                                 mdio.md_options &= ~MD_ASYNC;
169                         else if (!strcmp(optarg, "cluster"))
170                                 mdio.md_options |= MD_CLUSTER;
171                         else if (!strcmp(optarg, "nocluster"))
172                                 mdio.md_options &= ~MD_CLUSTER;
173                         else if (!strcmp(optarg, "compress"))
174                                 mdio.md_options |= MD_COMPRESS;
175                         else if (!strcmp(optarg, "nocompress"))
176                                 mdio.md_options &= ~MD_COMPRESS;
177                         else if (!strcmp(optarg, "force"))
178                                 mdio.md_options |= MD_FORCE;
179                         else if (!strcmp(optarg, "noforce"))
180                                 mdio.md_options &= ~MD_FORCE;
181                         else if (!strcmp(optarg, "readonly"))
182                                 mdio.md_options |= MD_READONLY;
183                         else if (!strcmp(optarg, "noreadonly"))
184                                 mdio.md_options &= ~MD_READONLY;
185                         else if (!strcmp(optarg, "reserve"))
186                                 mdio.md_options |= MD_RESERVE;
187                         else if (!strcmp(optarg, "noreserve"))
188                                 mdio.md_options &= ~MD_RESERVE;
189                         else
190                                 errx(1, "Unknown option: %s.", optarg);
191                         break;
192                 case 'S':
193                         if (cmdline != 2)
194                                 usage();
195                         mdio.md_sectorsize = strtoul(optarg, &p, 0);
196                         break;
197                 case 's':
198                         if (cmdline == 0) {
199                                 /* Imply ``-a'' */
200                                 action = ATTACH;
201                                 cmdline = 1;
202                         }
203                         if (cmdline == 1) {
204                                 /* Imply ``-t swap'' */
205                                 mdio.md_type = MD_SWAP;
206                                 mdio.md_options = MD_CLUSTER | MD_AUTOUNIT | MD_COMPRESS;
207                                 cmdline = 2;
208                         }
209                         if (cmdline != 2)
210                                 usage();
211                         mdio.md_mediasize = (off_t)strtoumax(optarg, &p, 0);
212                         if (p == NULL || *p == '\0')
213                                 mdio.md_mediasize *= DEV_BSIZE;
214                         else if (*p == 'b' || *p == 'B')
215                                 ; /* do nothing */
216                         else if (*p == 'k' || *p == 'K')
217                                 mdio.md_mediasize <<= 10;
218                         else if (*p == 'm' || *p == 'M')
219                                 mdio.md_mediasize <<= 20;
220                         else if (*p == 'g' || *p == 'G')
221                                 mdio.md_mediasize <<= 30;
222                         else if (*p == 't' || *p == 'T') {
223                                 mdio.md_mediasize <<= 30;
224                                 mdio.md_mediasize <<= 10;
225                         } else
226                                 errx(1, "Unknown suffix on -s argument");
227                         break;
228                 case 'u':
229                         if (cmdline != 2 && cmdline != 3)
230                                 usage();
231                         if (!strncmp(optarg, "/dev/", 5))
232                                 optarg += 5;
233                         if (!strncmp(optarg, MD_NAME, sizeof(MD_NAME) - 1))
234                                 optarg += sizeof(MD_NAME) - 1;
235                         mdio.md_unit = strtoul(optarg, &p, 0);
236                         if (mdio.md_unit == (unsigned)ULONG_MAX || *p != '\0')
237                                 errx(1, "bad unit: %s", optarg);
238                         mdunit = optarg;
239                         mdio.md_options &= ~MD_AUTOUNIT;
240                         break;
241                 case 'v':
242                         if (cmdline != 3)
243                                 usage();
244                         vflag = OPT_VERBOSE;
245                         break;
246                 case 'x':
247                         if (cmdline != 2)
248                                 usage();
249                         mdio.md_fwsectors = strtoul(optarg, &p, 0);
250                         break;
251                 case 'y':
252                         if (cmdline != 2)
253                                 usage();
254                         mdio.md_fwheads = strtoul(optarg, &p, 0);
255                         break;
256                 default:
257                         usage();
258                 }
259         }
260         mdio.md_version = MDIOVERSION;
261
262         if (!kld_isloaded("g_md") && kld_load("geom_md") == -1)
263                 err(1, "failed to load geom_md module");
264
265         fd = open("/dev/" MDCTL_NAME, O_RDWR, 0);
266         if (fd < 0)
267                 err(1, "open(/dev/%s)", MDCTL_NAME);
268         if (cmdline == 2
269             && (mdio.md_type == MD_MALLOC || mdio.md_type == MD_SWAP))
270                 if (mdio.md_mediasize == 0)
271                         errx(1, "must specify -s for -t malloc or -t swap");
272         if (cmdline == 2 && mdio.md_type == MD_VNODE)
273                 if (mdio.md_file[0] == '\0')
274                         errx(1, "must specify -f for -t vnode");
275         if (mdio.md_type == MD_VNODE &&
276             (mdio.md_options & MD_READONLY) == 0) {
277                 if (access(mdio.md_file, W_OK) < 0 &&
278                     (errno == EACCES || errno == EPERM || errno == EROFS)) {
279                         fprintf(stderr,
280                             "WARNING: opening backing store: %s readonly\n",
281                             mdio.md_file);
282                         mdio.md_options |= MD_READONLY;
283                 }
284         }
285         if (action == LIST) {
286                 if (mdio.md_options & MD_AUTOUNIT) {
287                         /* 
288                          * Listing all devices. This is why we pass NULL
289                          * together with OPT_LIST.
290                          */
291                         md_list(NULL, OPT_LIST | vflag);
292                 } else {
293                         return (md_query(mdunit));
294                 }
295         } else if (action == ATTACH) {
296                 if (cmdline < 2)
297                         usage();
298                 i = ioctl(fd, MDIOCATTACH, &mdio);
299                 if (i < 0)
300                         err(1, "ioctl(/dev/%s)", MDCTL_NAME);
301                 if (mdio.md_options & MD_AUTOUNIT)
302                         printf("%s%d\n", nflag ? "" : MD_NAME, mdio.md_unit);
303         } else if (action == DETACH) {
304                 if (mdio.md_options & MD_AUTOUNIT)
305                         usage();
306                 i = ioctl(fd, MDIOCDETACH, &mdio);
307                 if (i < 0)
308                         err(1, "ioctl(/dev/%s)", MDCTL_NAME);
309         } else
310                 usage();
311         close (fd);
312         return (0);
313 }
314
315 /*
316  * Lists md(4) disks. Is used also as a query routine, since it handles XML
317  * interface. 'units' can be NULL for listing memory disks. It might be
318  * coma-separated string containing md(4) disk names. 'opt' distinguished
319  * between list and query mode.
320  */
321 static int
322 md_list(char *units, int opt)
323 {
324         struct gmesh gm;
325         struct gprovider *pp;
326         struct gconf *gc;
327         struct gident *gid;
328         struct devstat *gsp;
329         struct ggeom *gg;
330         struct gclass *gcl;
331         void *sq;
332         int retcode, found;
333         char *type, *file, *length;
334
335         type = file = length = NULL;
336
337         retcode = geom_gettree(&gm);
338         if (retcode != 0)
339                 return (-1);
340         retcode = geom_stats_open();
341         if (retcode != 0)
342                 return (-1);
343         sq = geom_stats_snapshot_get();
344         if (sq == NULL)
345                 return (-1);
346
347         found = 0;
348         while ((gsp = geom_stats_snapshot_next(sq)) != NULL) {
349                 gid = geom_lookupid(&gm, gsp->id);
350                 if (gid == NULL)
351                         continue;
352                 if (gid->lg_what == ISPROVIDER) {
353                         pp = gid->lg_ptr;
354                         gg = pp->lg_geom;
355                         gcl = gg->lg_class;
356                         if (strcmp(gcl->lg_name, CLASS_NAME_MD) != 0)
357                                 continue;
358                         if ((opt & OPT_UNIT) && (units != NULL)) {
359                                 retcode = md_find(units, pp->lg_name);
360                                 if (retcode != 1)
361                                         continue;
362                                 else
363                                         found = 1;
364                         }
365                         gc = &pp->lg_config;
366                         printf("%s", pp->lg_name);
367                         if (opt & OPT_VERBOSE || opt & OPT_UNIT) {
368                                 type = geom_config_get(gc, "type");
369                                 if (strcmp(type, "vnode") == 0)
370                                         file = geom_config_get(gc, "file");
371                                 length = geom_config_get(gc, "length");
372                                 if (length == NULL)
373                                         length = "<a>";
374                                 printf("\t%s\t", type);
375                                 md_prthumanval(length);
376                                 if (file != NULL) {
377                                         printf("\t%s", file);
378                                         file = NULL;
379                                 }
380                         }
381                         opt |= OPT_DONE;
382                         if ((opt & OPT_LIST) && !(opt & OPT_VERBOSE))
383                                 printf(" ");
384                         else
385                                 printf("\n");
386                 }
387         }
388         if ((opt & OPT_LIST) && (opt & OPT_DONE) && !(opt & OPT_VERBOSE))
389                 printf("\n");
390         /* XXX: Check if it's enough to clean everything. */
391         geom_stats_snapshot_free(sq);
392         if ((opt & OPT_UNIT) && found)
393                 return (0);
394         else
395                 return (-1);
396 }
397
398 /*
399  * Returns value of 'name' from gconfig structure.
400  */
401 static char *
402 geom_config_get(struct gconf *g, char *name)
403 {
404         struct gconfig *gce;
405
406         LIST_FOREACH(gce, g, lg_config) {
407                 if (strcmp(gce->lg_name, name) == 0)
408                         return (gce->lg_val);
409         }
410         return (NULL);
411 }
412
413 /*
414  * List is comma separated list of MD disks. name is a
415  * device name we look for.  Returns 1 if found and 0
416  * otherwise.
417  */
418 static int
419 md_find(char *list, const char *name)
420 {
421         int ret;
422         char num[16];
423         char *ptr, *p, *u;
424
425         ret = 0;
426         ptr = strdup(list);
427         if (ptr == NULL)
428                 return (-1);
429         for (p = ptr; (u = strsep(&p, ",")) != NULL;) {
430                 if (strncmp(u, "/dev/", 5) == 0)
431                         u += 5;
432                 /* Just in case user specified number instead of full name */
433                 snprintf(num, sizeof(num), "md%s", u);
434                 if (strcmp(u, name) == 0 || strcmp(num, name) == 0) {
435                         ret = 1;
436                         break;
437                 }
438         }
439         free(ptr);
440         return (ret);
441 }
442
443 static void
444 md_prthumanval(char *length)
445 {
446         char buf[6];
447         uint64_t bytes;
448         char *endptr;
449
450         bytes = strtoul(length, &endptr, 10);
451         if (bytes == (unsigned)ULONG_MAX || *endptr != '\0')
452                 return;
453         humanize_number(buf, sizeof(buf) - (bytes < 0 ? 0 : 1),
454             bytes, "", HN_AUTOSCALE, HN_B | HN_NOSPACE | HN_DECIMAL);
455         (void)printf("%6s", buf);
456 }
457
458 int
459 md_query(char *name)
460 {
461         return (md_list(name, OPT_UNIT));
462 }