]> CyberLeo.Net >> Repos - FreeBSD/releng/8.1.git/blob - sbin/mdconfig/mdconfig.c
Copy stable/8 to releng/8.1 in preparation for 8.1-RC1.
[FreeBSD/releng/8.1.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 [-o [no]force]\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 (action == DETACH) {
164                                 if (!strcmp(optarg, "force"))
165                                         mdio.md_options |= MD_FORCE;
166                                 else if (!strcmp(optarg, "noforce"))
167                                         mdio.md_options &= ~MD_FORCE;
168                                 else
169                                         errx(1, "Unknown option: %s.", optarg);
170                                 break;
171                         }
172
173                         if (cmdline != 2)
174                                 usage();
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, "cluster"))
180                                 mdio.md_options |= MD_CLUSTER;
181                         else if (!strcmp(optarg, "nocluster"))
182                                 mdio.md_options &= ~MD_CLUSTER;
183                         else if (!strcmp(optarg, "compress"))
184                                 mdio.md_options |= MD_COMPRESS;
185                         else if (!strcmp(optarg, "nocompress"))
186                                 mdio.md_options &= ~MD_COMPRESS;
187                         else if (!strcmp(optarg, "force"))
188                                 mdio.md_options |= MD_FORCE;
189                         else if (!strcmp(optarg, "noforce"))
190                                 mdio.md_options &= ~MD_FORCE;
191                         else if (!strcmp(optarg, "readonly"))
192                                 mdio.md_options |= MD_READONLY;
193                         else if (!strcmp(optarg, "noreadonly"))
194                                 mdio.md_options &= ~MD_READONLY;
195                         else if (!strcmp(optarg, "reserve"))
196                                 mdio.md_options |= MD_RESERVE;
197                         else if (!strcmp(optarg, "noreserve"))
198                                 mdio.md_options &= ~MD_RESERVE;
199                         else
200                                 errx(1, "Unknown option: %s.", optarg);
201                         break;
202                 case 'S':
203                         if (cmdline != 2)
204                                 usage();
205                         mdio.md_sectorsize = strtoul(optarg, &p, 0);
206                         break;
207                 case 's':
208                         if (cmdline == 0) {
209                                 /* Imply ``-a'' */
210                                 action = ATTACH;
211                                 cmdline = 1;
212                         }
213                         if (cmdline == 1) {
214                                 /* Imply ``-t swap'' */
215                                 mdio.md_type = MD_SWAP;
216                                 mdio.md_options = MD_CLUSTER | MD_AUTOUNIT | MD_COMPRESS;
217                                 cmdline = 2;
218                         }
219                         if (cmdline != 2)
220                                 usage();
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
236                                 errx(1, "Unknown suffix on -s argument");
237                         break;
238                 case 'u':
239                         if (cmdline != 2 && cmdline != 3)
240                                 usage();
241                         if (!strncmp(optarg, "/dev/", 5))
242                                 optarg += 5;
243                         if (!strncmp(optarg, MD_NAME, sizeof(MD_NAME) - 1))
244                                 optarg += sizeof(MD_NAME) - 1;
245                         mdio.md_unit = strtoul(optarg, &p, 0);
246                         if (mdio.md_unit == (unsigned)ULONG_MAX || *p != '\0')
247                                 errx(1, "bad unit: %s", optarg);
248                         mdunit = optarg;
249                         mdio.md_options &= ~MD_AUTOUNIT;
250                         break;
251                 case 'v':
252                         if (cmdline != 3)
253                                 usage();
254                         vflag = OPT_VERBOSE;
255                         break;
256                 case 'x':
257                         if (cmdline != 2)
258                                 usage();
259                         mdio.md_fwsectors = strtoul(optarg, &p, 0);
260                         break;
261                 case 'y':
262                         if (cmdline != 2)
263                                 usage();
264                         mdio.md_fwheads = strtoul(optarg, &p, 0);
265                         break;
266                 default:
267                         usage();
268                 }
269         }
270         mdio.md_version = MDIOVERSION;
271
272         if (!kld_isloaded("g_md") && kld_load("geom_md") == -1)
273                 err(1, "failed to load geom_md module");
274
275         fd = open("/dev/" MDCTL_NAME, O_RDWR, 0);
276         if (fd < 0)
277                 err(1, "open(/dev/%s)", MDCTL_NAME);
278         if (cmdline == 2
279             && (mdio.md_type == MD_MALLOC || mdio.md_type == MD_SWAP))
280                 if (mdio.md_mediasize == 0)
281                         errx(1, "must specify -s for -t malloc or -t swap");
282         if (cmdline == 2 && mdio.md_type == MD_VNODE)
283                 if (mdio.md_file[0] == '\0')
284                         errx(1, "must specify -f for -t vnode");
285         if (mdio.md_type == MD_VNODE &&
286             (mdio.md_options & MD_READONLY) == 0) {
287                 if (access(mdio.md_file, W_OK) < 0 &&
288                     (errno == EACCES || errno == EPERM || errno == EROFS)) {
289                         fprintf(stderr,
290                             "WARNING: opening backing store: %s readonly\n",
291                             mdio.md_file);
292                         mdio.md_options |= MD_READONLY;
293                 }
294         }
295         if (action == LIST) {
296                 if (mdio.md_options & MD_AUTOUNIT) {
297                         /* 
298                          * Listing all devices. This is why we pass NULL
299                          * together with OPT_LIST.
300                          */
301                         md_list(NULL, OPT_LIST | vflag);
302                 } else {
303                         return (md_query(mdunit));
304                 }
305         } else if (action == ATTACH) {
306                 if (cmdline < 2)
307                         usage();
308                 i = ioctl(fd, MDIOCATTACH, &mdio);
309                 if (i < 0)
310                         err(1, "ioctl(/dev/%s)", MDCTL_NAME);
311                 if (mdio.md_options & MD_AUTOUNIT)
312                         printf("%s%d\n", nflag ? "" : MD_NAME, mdio.md_unit);
313         } else if (action == DETACH) {
314                 if (mdio.md_options & MD_AUTOUNIT)
315                         usage();
316                 i = ioctl(fd, MDIOCDETACH, &mdio);
317                 if (i < 0)
318                         err(1, "ioctl(/dev/%s)", MDCTL_NAME);
319         } else
320                 usage();
321         close (fd);
322         return (0);
323 }
324
325 /*
326  * Lists md(4) disks. Is used also as a query routine, since it handles XML
327  * interface. 'units' can be NULL for listing memory disks. It might be
328  * coma-separated string containing md(4) disk names. 'opt' distinguished
329  * between list and query mode.
330  */
331 static int
332 md_list(char *units, int opt)
333 {
334         struct gmesh gm;
335         struct gprovider *pp;
336         struct gconf *gc;
337         struct gident *gid;
338         struct devstat *gsp;
339         struct ggeom *gg;
340         struct gclass *gcl;
341         void *sq;
342         int retcode, found;
343         char *type, *file, *length;
344
345         type = file = length = NULL;
346
347         retcode = geom_gettree(&gm);
348         if (retcode != 0)
349                 return (-1);
350         retcode = geom_stats_open();
351         if (retcode != 0)
352                 return (-1);
353         sq = geom_stats_snapshot_get();
354         if (sq == NULL)
355                 return (-1);
356
357         found = 0;
358         while ((gsp = geom_stats_snapshot_next(sq)) != NULL) {
359                 gid = geom_lookupid(&gm, gsp->id);
360                 if (gid == NULL)
361                         continue;
362                 if (gid->lg_what == ISPROVIDER) {
363                         pp = gid->lg_ptr;
364                         gg = pp->lg_geom;
365                         gcl = gg->lg_class;
366                         if (strcmp(gcl->lg_name, CLASS_NAME_MD) != 0)
367                                 continue;
368                         if ((opt & OPT_UNIT) && (units != NULL)) {
369                                 retcode = md_find(units, pp->lg_name);
370                                 if (retcode != 1)
371                                         continue;
372                                 else
373                                         found = 1;
374                         }
375                         gc = &pp->lg_config;
376                         printf("%s", pp->lg_name);
377                         if (opt & OPT_VERBOSE || opt & OPT_UNIT) {
378                                 type = geom_config_get(gc, "type");
379                                 if (strcmp(type, "vnode") == 0)
380                                         file = geom_config_get(gc, "file");
381                                 length = geom_config_get(gc, "length");
382                                 if (length == NULL)
383                                         length = "<a>";
384                                 printf("\t%s\t", type);
385                                 md_prthumanval(length);
386                                 if (file != NULL) {
387                                         printf("\t%s", file);
388                                         file = NULL;
389                                 }
390                         }
391                         opt |= OPT_DONE;
392                         if ((opt & OPT_LIST) && !(opt & OPT_VERBOSE))
393                                 printf(" ");
394                         else
395                                 printf("\n");
396                 }
397         }
398         if ((opt & OPT_LIST) && (opt & OPT_DONE) && !(opt & OPT_VERBOSE))
399                 printf("\n");
400         /* XXX: Check if it's enough to clean everything. */
401         geom_stats_snapshot_free(sq);
402         if ((opt & OPT_UNIT) && found)
403                 return (0);
404         else
405                 return (-1);
406 }
407
408 /*
409  * Returns value of 'name' from gconfig structure.
410  */
411 static char *
412 geom_config_get(struct gconf *g, char *name)
413 {
414         struct gconfig *gce;
415
416         LIST_FOREACH(gce, g, lg_config) {
417                 if (strcmp(gce->lg_name, name) == 0)
418                         return (gce->lg_val);
419         }
420         return (NULL);
421 }
422
423 /*
424  * List is comma separated list of MD disks. name is a
425  * device name we look for.  Returns 1 if found and 0
426  * otherwise.
427  */
428 static int
429 md_find(char *list, const char *name)
430 {
431         int ret;
432         char num[16];
433         char *ptr, *p, *u;
434
435         ret = 0;
436         ptr = strdup(list);
437         if (ptr == NULL)
438                 return (-1);
439         for (p = ptr; (u = strsep(&p, ",")) != NULL;) {
440                 if (strncmp(u, "/dev/", 5) == 0)
441                         u += 5;
442                 /* Just in case user specified number instead of full name */
443                 snprintf(num, sizeof(num), "md%s", u);
444                 if (strcmp(u, name) == 0 || strcmp(num, name) == 0) {
445                         ret = 1;
446                         break;
447                 }
448         }
449         free(ptr);
450         return (ret);
451 }
452
453 static void
454 md_prthumanval(char *length)
455 {
456         char buf[6];
457         uintmax_t bytes;
458         char *endptr;
459
460         errno = 0;
461         bytes = strtoumax(length, &endptr, 10);
462         if (errno != 0 || *endptr != '\0' || bytes > INT64_MAX)
463                 return;
464         humanize_number(buf, sizeof(buf), (int64_t)bytes, "",
465             HN_AUTOSCALE, HN_B | HN_NOSPACE | HN_DECIMAL);
466         (void)printf("%6s", buf);
467 }
468
469 int
470 md_query(char *name)
471 {
472         return (md_list(name, OPT_UNIT));
473 }