]> CyberLeo.Net >> Repos - FreeBSD/stable/8.git/blob - sbin/geom/class/part/geom_part.c
Copy head to stable/8 as part of 8.0 Release cycle.
[FreeBSD/stable/8.git] / sbin / geom / class / part / geom_part.c
1 /*-
2  * Copyright (c) 2007, 2008 Marcel Moolenaar
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  */
26
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
29
30 #include <sys/stat.h>
31
32 #include <assert.h>
33 #include <err.h>
34 #include <errno.h>
35 #include <fcntl.h>
36 #include <libgeom.h>
37 #include <libutil.h>
38 #include <paths.h>
39 #include <stdint.h>
40 #include <stdio.h>
41 #include <stdlib.h>
42 #include <string.h>
43 #include <strings.h>
44 #include <unistd.h>
45
46 #include "core/geom.h"
47 #include "misc/subr.h"
48
49 #ifdef STATIC_GEOM_CLASSES
50 #define PUBSYM(x)       gpart_##x
51 #else
52 #define PUBSYM(x)       x
53 #endif
54
55 uint32_t PUBSYM(lib_version) = G_LIB_VERSION;
56 uint32_t PUBSYM(version) = 0;
57
58 static char autofill[] = "*";
59 static char optional[] = "";
60 static char flags[] = "C";
61
62 static char bootcode_param[] = "bootcode";
63 static char index_param[] = "index";
64 static char partcode_param[] = "partcode";
65
66 static void gpart_bootcode(struct gctl_req *, unsigned int);
67 static void gpart_issue(struct gctl_req *, unsigned int);
68 static void gpart_show(struct gctl_req *, unsigned int);
69
70 struct g_command PUBSYM(class_commands)[] = {
71         { "add", 0, gpart_issue, {
72                 { 'b', "start", autofill, G_TYPE_ASCLBA },
73                 { 's', "size", autofill, G_TYPE_ASCLBA },
74                 { 't', "type", NULL, G_TYPE_STRING },
75                 { 'i', index_param, optional, G_TYPE_ASCNUM },
76                 { 'l', "label", optional, G_TYPE_STRING },
77                 { 'f', "flags", flags, G_TYPE_STRING },
78                 G_OPT_SENTINEL },
79           "geom", NULL
80         },
81         { "bootcode", 0, gpart_bootcode, {
82                 { 'b', bootcode_param, optional, G_TYPE_STRING },
83                 { 'p', partcode_param, optional, G_TYPE_STRING },
84                 { 'i', index_param, optional, G_TYPE_ASCNUM },
85                 { 'f', "flags", flags, G_TYPE_STRING },
86                 G_OPT_SENTINEL },
87           "geom", NULL
88         },
89         { "commit", 0, gpart_issue, G_NULL_OPTS, "geom", NULL },
90         { "create", 0, gpart_issue, {
91                 { 's', "scheme", NULL, G_TYPE_STRING },
92                 { 'n', "entries", optional, G_TYPE_ASCNUM },
93                 { 'f', "flags", flags, G_TYPE_STRING },
94                 G_OPT_SENTINEL },
95           "provider", NULL
96         },
97         { "delete", 0, gpart_issue, {
98                 { 'i', index_param, NULL, G_TYPE_ASCNUM },
99                 { 'f', "flags", flags, G_TYPE_STRING },
100                 G_OPT_SENTINEL },
101           "geom", NULL
102         },
103         { "destroy", 0, gpart_issue, {
104                 { 'f', "flags", flags, G_TYPE_STRING },
105                 G_OPT_SENTINEL },
106           "geom", NULL },
107         { "modify", 0, gpart_issue, {
108                 { 'i', index_param, NULL, G_TYPE_ASCNUM },
109                 { 'l', "label", optional, G_TYPE_STRING },
110                 { 't', "type", optional, G_TYPE_STRING },
111                 { 'f', "flags", flags, G_TYPE_STRING },
112                 G_OPT_SENTINEL },
113           "geom", NULL
114         },
115         { "set", 0, gpart_issue, {
116                 { 'a', "attrib", NULL, G_TYPE_STRING },
117                 { 'i', index_param, NULL, G_TYPE_ASCNUM },
118                 { 'f', "flags", flags, G_TYPE_STRING },
119                 G_OPT_SENTINEL },
120           "geom", NULL
121         },
122         { "show", 0, gpart_show, {
123                 { 'l', "show_label", NULL, G_TYPE_BOOL },
124                 { 'r', "show_rawtype", NULL, G_TYPE_BOOL },
125                 G_OPT_SENTINEL },
126           NULL, "[-lr] [geom ...]"
127         },
128         { "undo", 0, gpart_issue, G_NULL_OPTS, "geom", NULL },
129         { "unset", 0, gpart_issue, {
130                 { 'a', "attrib", NULL, G_TYPE_STRING },
131                 { 'i', index_param, NULL, G_TYPE_ASCNUM },
132                 { 'f', "flags", flags, G_TYPE_STRING },
133                 G_OPT_SENTINEL },
134           "geom", NULL
135         },
136         G_CMD_SENTINEL
137 };
138
139 static struct gclass *
140 find_class(struct gmesh *mesh, const char *name)
141 {
142         struct gclass *classp;
143
144         LIST_FOREACH(classp, &mesh->lg_class, lg_class) {
145                 if (strcmp(classp->lg_name, name) == 0)
146                         return (classp);
147         }
148         return (NULL);
149 }
150
151 static struct ggeom *
152 find_geom(struct gclass *classp, const char *name)
153 {
154         struct ggeom *gp;
155
156         LIST_FOREACH(gp, &classp->lg_geom, lg_geom) {
157                 if (strcmp(gp->lg_name, name) == 0)
158                         return (gp);
159         }
160         return (NULL);
161 }
162
163 static const char *
164 find_geomcfg(struct ggeom *gp, const char *cfg)
165 {
166         struct gconfig *gc;
167
168         LIST_FOREACH(gc, &gp->lg_config, lg_config) {
169                 if (!strcmp(gc->lg_name, cfg))
170                         return (gc->lg_val);
171         }
172         return (NULL);
173 }
174
175 static const char *
176 find_provcfg(struct gprovider *pp, const char *cfg)
177 {
178         struct gconfig *gc;
179
180         LIST_FOREACH(gc, &pp->lg_config, lg_config) {
181                 if (!strcmp(gc->lg_name, cfg))
182                         return (gc->lg_val);
183         }
184         return (NULL);
185 }
186
187 static struct gprovider *
188 find_provider(struct ggeom *gp, unsigned long long minsector)
189 {
190         struct gprovider *pp, *bestpp;
191         const char *s;
192         unsigned long long sector, bestsector;
193
194         bestpp = NULL;
195         LIST_FOREACH(pp, &gp->lg_provider, lg_provider) {
196                 s = find_provcfg(pp, "start");
197                 if (s == NULL) {
198                         s = find_provcfg(pp, "offset");
199                         sector = atoll(s) / pp->lg_sectorsize;
200                 } else
201                         sector = atoll(s);
202
203                 if (sector < minsector)
204                         continue;
205                 if (bestpp != NULL && sector >= bestsector)
206                         continue;
207
208                 bestpp = pp;
209                 bestsector = sector;
210         }
211         return (bestpp);
212 }
213
214 static const char *
215 fmtsize(int64_t rawsz)
216 {
217         static char buf[5];
218
219         humanize_number(buf, sizeof(buf), rawsz, "", HN_AUTOSCALE,
220             HN_B | HN_NOSPACE | HN_DECIMAL);
221         return (buf);
222 }
223
224 static const char *
225 fmtattrib(struct gprovider *pp)
226 {
227         static char buf[128];
228         struct gconfig *gc;
229         u_int idx;
230
231         buf[0] = '\0';
232         idx = 0;
233         LIST_FOREACH(gc, &pp->lg_config, lg_config) {
234                 if (strcmp(gc->lg_name, "attrib") != 0)
235                         continue;
236                 idx += snprintf(buf + idx, sizeof(buf) - idx, "%s%s",
237                     (idx == 0) ? " [" : ",", gc->lg_val);
238         }
239         if (idx > 0)
240                 snprintf(buf + idx, sizeof(buf) - idx, "] ");
241         return (buf);
242 }
243
244 static int
245 gpart_autofill(struct gctl_req *req)
246 {
247         struct gmesh mesh;
248         struct gclass *cp;
249         struct ggeom *gp;
250         struct gprovider *pp;
251         unsigned long long first, last;
252         unsigned long long size, start;
253         unsigned long long lba, len, grade;
254         const char *s;
255         char *val;
256         int error, has_size, has_start;
257
258         s = gctl_get_ascii(req, "verb");
259         if (strcmp(s, "add") != 0)
260                 return (0);
261
262         s = gctl_get_ascii(req, "size");
263         has_size = (*s == '*') ? 0 : 1;
264         size = (has_size) ? (unsigned long long)atoll(s) : 0ULL;
265
266         s = gctl_get_ascii(req, "start");
267         has_start = (*s == '*') ? 0 : 1;
268         start = (has_start) ? (unsigned long long)atoll(s) : ~0ULL;
269
270         /* No autofill necessary. */
271         if (has_size && has_start)
272                 return (0);
273
274         error = geom_gettree(&mesh);
275         if (error)
276                 return (error);
277         cp = find_class(&mesh, gctl_get_ascii(req, "class"));
278         gp = find_geom(cp, gctl_get_ascii(req, "geom"));
279         first = atoll(find_geomcfg(gp, "first"));
280         last = atoll(find_geomcfg(gp, "last"));
281         grade = ~0ULL;
282         while ((pp = find_provider(gp, first)) != NULL) {
283                 s = find_provcfg(pp, "start");
284                 if (s == NULL) {
285                         s = find_provcfg(pp, "offset");
286                         lba = atoll(s) / pp->lg_sectorsize;
287                 } else
288                         lba = atoll(s);
289
290                 if (first < lba) {
291                         /* Free space [first, lba> */
292                         len = lba - first;
293                         if (has_size) {
294                                 if (len >= size && len - size < grade) {
295                                         start = first;
296                                         grade = len - size;
297                                 }
298                         } else if (has_start) {
299                                 if (start >= first && start < lba) {
300                                         size = lba - start;
301                                         grade = start - first;
302                                 }
303                         } else {
304                                 if (grade == ~0ULL || len > size) {
305                                         start = first;
306                                         size = len;
307                                         grade = 0;
308                                 }
309                         }
310                 }
311
312                 s = find_provcfg(pp, "end");
313                 if (s == NULL) {
314                         s = find_provcfg(pp, "length");
315                         first = lba + atoll(s) / pp->lg_sectorsize;
316                 } else
317                         first = atoll(s) + 1;
318         }
319         if (first <= last) {
320                 /* Free space [first-last] */
321                 len = last - first + 1;
322                 if (has_size) {
323                         if (len >= size && len - size < grade) {
324                                 start = first;
325                                 grade = len - size;
326                         }
327                 } else if (has_start) {
328                         if (start >= first && start <= last) {
329                                 size = last - start + 1;
330                                 grade = start - first;
331                         }
332                 } else {
333                         if (grade == ~0ULL || len > size) {
334                                 start = first;
335                                 size = len;
336                                 grade = 0;
337                         }
338                 }
339         }
340
341         if (grade == ~0ULL)
342                 return (ENOSPC);
343
344         if (!has_size) {
345                 asprintf(&val, "%llu", size);
346                 if (val == NULL)
347                         return (ENOMEM);
348                 gctl_change_param(req, "size", -1, val);
349         }
350         if (!has_start) {
351                 asprintf(&val, "%llu", start);
352                 if (val == NULL)
353                         return (ENOMEM);
354                 gctl_change_param(req, "start", -1, val);
355         }
356         return (0);
357 }
358
359 static void
360 gpart_show_geom(struct ggeom *gp, const char *element)
361 {
362         struct gprovider *pp;
363         const char *s, *scheme;
364         unsigned long long first, last, sector, end;
365         unsigned long long length, secsz;
366         int idx, wblocks, wname;
367
368         scheme = find_geomcfg(gp, "scheme");
369         s = find_geomcfg(gp, "first");
370         first = atoll(s);
371         s = find_geomcfg(gp, "last");
372         last = atoll(s);
373         wblocks = strlen(s);
374         wname = strlen(gp->lg_name);
375         pp = LIST_FIRST(&gp->lg_consumer)->lg_provider;
376         secsz = pp->lg_sectorsize;
377         printf("=>%*llu  %*llu  %*s  %s  (%s)\n",
378             wblocks, first, wblocks, (last - first + 1),
379             wname, gp->lg_name,
380             scheme, fmtsize(pp->lg_mediasize));
381
382         while ((pp = find_provider(gp, first)) != NULL) {
383                 s = find_provcfg(pp, "start");
384                 if (s == NULL) {
385                         s = find_provcfg(pp, "offset");
386                         sector = atoll(s) / secsz;
387                 } else
388                         sector = atoll(s);
389
390                 s = find_provcfg(pp, "end");
391                 if (s == NULL) {
392                         s = find_provcfg(pp, "length");
393                         length = atoll(s) / secsz;
394                         end = sector + length - 1;
395                 } else {
396                         end = atoll(s);
397                         length = end - sector + 1;
398                 }
399                 s = find_provcfg(pp, "index");
400                 idx = atoi(s);
401                 if (first < sector) {
402                         printf("  %*llu  %*llu  %*s  - free -  (%s)\n",
403                             wblocks, first, wblocks, sector - first,
404                             wname, "",
405                             fmtsize((sector - first) * secsz));
406                 }
407                 printf("  %*llu  %*llu  %*d  %s %s (%s)\n",
408                     wblocks, sector, wblocks, length,
409                     wname, idx, find_provcfg(pp, element),
410                     fmtattrib(pp), fmtsize(pp->lg_mediasize));
411                 first = end + 1;
412         }
413         if (first <= last) {
414                 length = last - first + 1;
415                 printf("  %*llu  %*llu  %*s  - free -  (%s)\n",
416                     wblocks, first, wblocks, length,
417                     wname, "",
418                     fmtsize(length * secsz));
419         }
420         printf("\n");
421 }
422
423 static int
424 gpart_show_hasopt(struct gctl_req *req, const char *opt, const char *elt)
425 {
426
427         if (!gctl_get_int(req, opt))
428                 return (0);
429
430         if (elt != NULL)
431                 errx(EXIT_FAILURE, "-l and -r are mutually exclusive");
432
433         return (1);
434 }
435
436 static void
437 gpart_show(struct gctl_req *req, unsigned int fl __unused)
438 {
439         struct gmesh mesh;
440         struct gclass *classp;
441         struct ggeom *gp;
442         const char *element, *name;
443         int error, i, nargs;
444
445         element = NULL;
446         if (gpart_show_hasopt(req, "show_label", element))
447                 element = "label";
448         if (gpart_show_hasopt(req, "show_rawtype", element))
449                 element = "rawtype";
450         if (element == NULL)
451                 element = "type";
452
453         name = gctl_get_ascii(req, "class");
454         if (name == NULL)
455                 abort();
456         error = geom_gettree(&mesh);
457         if (error != 0)
458                 errc(EXIT_FAILURE, error, "Cannot get GEOM tree");
459         classp = find_class(&mesh, name);
460         if (classp == NULL) {
461                 geom_deletetree(&mesh);
462                 errx(EXIT_FAILURE, "Class %s not found.", name);
463         }
464         nargs = gctl_get_int(req, "nargs");
465         if (nargs > 0) {
466                 for (i = 0; i < nargs; i++) {
467                         name = gctl_get_ascii(req, "arg%d", i);
468                         gp = find_geom(classp, name);
469                         if (gp != NULL)
470                                 gpart_show_geom(gp, element);
471                         else
472                                 errx(EXIT_FAILURE, "No such geom: %s.", name);
473                 }
474         } else {
475                 LIST_FOREACH(gp, &classp->lg_geom, lg_geom) {
476                         gpart_show_geom(gp, element);
477                 }
478         }
479         geom_deletetree(&mesh);
480 }
481
482 static void *
483 gpart_bootfile_read(const char *bootfile, ssize_t *size)
484 {
485         struct stat sb;
486         void *code;
487         int fd;
488
489         if (stat(bootfile, &sb) == -1)
490                 err(EXIT_FAILURE, "%s", bootfile);
491         if (!S_ISREG(sb.st_mode))
492                 errx(EXIT_FAILURE, "%s: not a regular file", bootfile);
493         if (sb.st_size == 0)
494                 errx(EXIT_FAILURE, "%s: empty file", bootfile);
495         if (*size > 0 && sb.st_size >= *size)
496                 errx(EXIT_FAILURE, "%s: file too big (%zu limit)", bootfile,
497                     *size);
498
499         *size = sb.st_size;
500
501         fd = open(bootfile, O_RDONLY);
502         if (fd == -1)
503                 err(EXIT_FAILURE, "%s", bootfile);
504         code = malloc(*size);
505         if (code == NULL)
506                 err(EXIT_FAILURE, NULL);
507         if (read(fd, code, *size) != *size)
508                 err(EXIT_FAILURE, "%s", bootfile);
509         close(fd);
510
511         return (code);
512 }
513
514 static void
515 gpart_write_partcode(struct gctl_req *req, int idx, void *code, ssize_t size)
516 {
517         char dsf[128];
518         struct gmesh mesh;
519         struct gclass *classp;
520         struct ggeom *gp;
521         struct gprovider *pp;
522         const char *s;
523         char *buf;
524         off_t bsize;
525         int error, fd;
526
527         s = gctl_get_ascii(req, "class");
528         if (s == NULL)
529                 abort();
530         error = geom_gettree(&mesh);
531         if (error != 0)
532                 errc(EXIT_FAILURE, error, "Cannot get GEOM tree");
533         classp = find_class(&mesh, s);
534         if (classp == NULL) {
535                 geom_deletetree(&mesh);
536                 errx(EXIT_FAILURE, "Class %s not found.", s);
537         }
538         s = gctl_get_ascii(req, "geom");
539         gp = find_geom(classp, s);
540         if (gp == NULL)
541                 errx(EXIT_FAILURE, "No such geom: %s.", s);
542
543         LIST_FOREACH(pp, &gp->lg_provider, lg_provider) {
544                 s = find_provcfg(pp, "index");
545                 if (s == NULL)
546                         continue;
547                 if (atoi(s) == idx)
548                         break;
549         }
550
551         if (pp != NULL) {
552                 snprintf(dsf, sizeof(dsf), "/dev/%s", pp->lg_name);
553                 fd = open(dsf, O_WRONLY);
554                 if (fd == -1)
555                         err(EXIT_FAILURE, "%s", dsf);
556                 if (lseek(fd, size, SEEK_SET) != size)
557                         errx(EXIT_FAILURE, "%s: not enough space", dsf);
558                 if (lseek(fd, 0, SEEK_SET) != 0)
559                         err(EXIT_FAILURE, "%s", dsf);
560
561                 /*
562                  * When writing to a disk device, the write must be
563                  * sector aligned and not write to any partial sectors,
564                  * so round up the buffer size to the next sector and zero it.
565                  */
566                 bsize = (size + pp->lg_sectorsize - 1) /
567                     pp->lg_sectorsize * pp->lg_sectorsize;
568                 buf = calloc(1, bsize);
569                 if (buf == NULL)
570                         err(EXIT_FAILURE, "%s", dsf);
571                 bcopy(code, buf, size);
572                 if (write(fd, buf, bsize) != bsize)
573                         err(EXIT_FAILURE, "%s", dsf);
574                 free(buf);
575                 close(fd);
576         } else
577                 errx(EXIT_FAILURE, "invalid partition index");
578
579         geom_deletetree(&mesh);
580 }
581
582 static void
583 gpart_bootcode(struct gctl_req *req, unsigned int fl)
584 {
585         const char *s;
586         char *sp;
587         void *bootcode, *partcode;
588         size_t bootsize, partsize;
589         int error, idx;
590
591         if (gctl_has_param(req, bootcode_param)) {
592                 s = gctl_get_ascii(req, bootcode_param);
593                 bootsize = 64 * 1024;           /* Arbitrary limit. */
594                 bootcode = gpart_bootfile_read(s, &bootsize);
595                 error = gctl_change_param(req, bootcode_param, bootsize,
596                     bootcode);
597                 if (error)
598                         errc(EXIT_FAILURE, error, "internal error");
599         } else {
600                 bootcode = NULL;
601                 bootsize = 0;
602         }
603
604         if (gctl_has_param(req, partcode_param)) {
605                 s = gctl_get_ascii(req, partcode_param);
606                 partsize = bootsize * 1024;
607                 partcode = gpart_bootfile_read(s, &partsize);
608                 error = gctl_delete_param(req, partcode_param);
609                 if (error)
610                         errc(EXIT_FAILURE, error, "internal error");
611         } else {
612                 partcode = NULL;
613                 partsize = 0;
614         }
615
616         if (gctl_has_param(req, index_param)) {
617                 if (partcode == NULL)
618                         errx(EXIT_FAILURE, "-i is only valid with -p");
619                 s = gctl_get_ascii(req, index_param);
620                 idx = strtol(s, &sp, 10);
621                 if (idx < 1 || *s == '\0' || *sp != '\0')
622                         errx(EXIT_FAILURE, "invalid partition index");
623                 error = gctl_delete_param(req, index_param);
624                 if (error)
625                         errc(EXIT_FAILURE, error, "internal error");
626         } else
627                 idx = 0;
628
629         if (partcode != NULL) {
630                 if (idx == 0)
631                         errx(EXIT_FAILURE, "missing -i option");
632                 gpart_write_partcode(req, idx, partcode, partsize);
633         } else {
634                 if (bootcode == NULL)
635                         errx(EXIT_FAILURE, "no -b nor -p");
636         }
637
638         if (bootcode != NULL)
639                 gpart_issue(req, fl);
640 }
641
642 static void
643 gpart_issue(struct gctl_req *req, unsigned int fl __unused)
644 {
645         char buf[4096];
646         char *errmsg;
647         const char *errstr;
648         int error, status;
649
650         /* autofill parameters (if applicable). */
651         error = gpart_autofill(req);
652         if (error) {
653                 warnc(error, "autofill");
654                 status = EXIT_FAILURE;
655                 goto done;
656         }
657
658         bzero(buf, sizeof(buf));
659         gctl_rw_param(req, "output", sizeof(buf), buf);
660         errstr = gctl_issue(req);
661         if (errstr == NULL || errstr[0] == '\0') {
662                 if (buf[0] != '\0')
663                         printf("%s", buf);
664                 status = EXIT_SUCCESS;
665                 goto done;
666         }
667
668         error = strtol(errstr, &errmsg, 0);
669         if (errmsg != errstr) {
670                 while (errmsg[0] == ' ')
671                         errmsg++;
672                 if (errmsg[0] != '\0')
673                         warnc(error, "%s", errmsg);
674                 else
675                         warnc(error, NULL);
676         } else
677                 warnx("%s", errmsg);
678
679         status = EXIT_FAILURE;
680
681  done:
682         gctl_free(req);
683         exit(status);
684 }