2 * Copyright (c) 2007, 2008 Marcel Moolenaar
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
46 #include "core/geom.h"
47 #include "misc/subr.h"
49 #ifdef STATIC_GEOM_CLASSES
50 #define PUBSYM(x) gpart_##x
55 uint32_t PUBSYM(lib_version) = G_LIB_VERSION;
56 uint32_t PUBSYM(version) = 0;
58 static char autofill[] = "*";
59 static char optional[] = "";
60 static char flags[] = "C";
62 static char bootcode_param[] = "bootcode";
63 static char index_param[] = "index";
64 static char partcode_param[] = "partcode";
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);
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 },
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 },
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 },
97 { "delete", 0, gpart_issue, {
98 { 'i', index_param, NULL, G_TYPE_ASCNUM },
99 { 'f', "flags", flags, G_TYPE_STRING },
103 { "destroy", 0, gpart_issue, {
104 { 'f', "flags", flags, G_TYPE_STRING },
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 },
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 },
122 { "show", 0, gpart_show, {
123 { 'l', "show_label", NULL, G_TYPE_BOOL },
124 { 'r', "show_rawtype", NULL, G_TYPE_BOOL },
126 NULL, "[-lr] [geom ...]"
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 },
139 static struct gclass *
140 find_class(struct gmesh *mesh, const char *name)
142 struct gclass *classp;
144 LIST_FOREACH(classp, &mesh->lg_class, lg_class) {
145 if (strcmp(classp->lg_name, name) == 0)
151 static struct ggeom *
152 find_geom(struct gclass *classp, const char *name)
156 LIST_FOREACH(gp, &classp->lg_geom, lg_geom) {
157 if (strcmp(gp->lg_name, name) == 0)
164 find_geomcfg(struct ggeom *gp, const char *cfg)
168 LIST_FOREACH(gc, &gp->lg_config, lg_config) {
169 if (!strcmp(gc->lg_name, cfg))
176 find_provcfg(struct gprovider *pp, const char *cfg)
180 LIST_FOREACH(gc, &pp->lg_config, lg_config) {
181 if (!strcmp(gc->lg_name, cfg))
187 static struct gprovider *
188 find_provider(struct ggeom *gp, unsigned long long minsector)
190 struct gprovider *pp, *bestpp;
192 unsigned long long sector, bestsector;
195 LIST_FOREACH(pp, &gp->lg_provider, lg_provider) {
196 s = find_provcfg(pp, "start");
198 s = find_provcfg(pp, "offset");
199 sector = atoll(s) / pp->lg_sectorsize;
203 if (sector < minsector)
205 if (bestpp != NULL && sector >= bestsector)
215 fmtsize(int64_t rawsz)
219 humanize_number(buf, sizeof(buf), rawsz, "", HN_AUTOSCALE,
220 HN_B | HN_NOSPACE | HN_DECIMAL);
225 fmtattrib(struct gprovider *pp)
227 static char buf[128];
233 LIST_FOREACH(gc, &pp->lg_config, lg_config) {
234 if (strcmp(gc->lg_name, "attrib") != 0)
236 idx += snprintf(buf + idx, sizeof(buf) - idx, "%s%s",
237 (idx == 0) ? " [" : ",", gc->lg_val);
240 snprintf(buf + idx, sizeof(buf) - idx, "] ");
245 gpart_autofill(struct gctl_req *req)
250 struct gprovider *pp;
251 unsigned long long first, last;
252 unsigned long long size, start;
253 unsigned long long lba, len, grade;
256 int error, has_size, has_start;
258 s = gctl_get_ascii(req, "verb");
259 if (strcmp(s, "add") != 0)
262 s = gctl_get_ascii(req, "size");
263 has_size = (*s == '*') ? 0 : 1;
264 size = (has_size) ? (unsigned long long)atoll(s) : 0ULL;
266 s = gctl_get_ascii(req, "start");
267 has_start = (*s == '*') ? 0 : 1;
268 start = (has_start) ? (unsigned long long)atoll(s) : ~0ULL;
270 /* No autofill necessary. */
271 if (has_size && has_start)
274 error = geom_gettree(&mesh);
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"));
282 while ((pp = find_provider(gp, first)) != NULL) {
283 s = find_provcfg(pp, "start");
285 s = find_provcfg(pp, "offset");
286 lba = atoll(s) / pp->lg_sectorsize;
291 /* Free space [first, lba> */
294 if (len >= size && len - size < grade) {
298 } else if (has_start) {
299 if (start >= first && start < lba) {
301 grade = start - first;
304 if (grade == ~0ULL || len > size) {
312 s = find_provcfg(pp, "end");
314 s = find_provcfg(pp, "length");
315 first = lba + atoll(s) / pp->lg_sectorsize;
317 first = atoll(s) + 1;
320 /* Free space [first-last] */
321 len = last - first + 1;
323 if (len >= size && len - size < grade) {
327 } else if (has_start) {
328 if (start >= first && start <= last) {
329 size = last - start + 1;
330 grade = start - first;
333 if (grade == ~0ULL || len > size) {
345 asprintf(&val, "%llu", size);
348 gctl_change_param(req, "size", -1, val);
351 asprintf(&val, "%llu", start);
354 gctl_change_param(req, "start", -1, val);
360 gpart_show_geom(struct ggeom *gp, const char *element)
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;
368 scheme = find_geomcfg(gp, "scheme");
369 s = find_geomcfg(gp, "first");
371 s = find_geomcfg(gp, "last");
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),
380 scheme, fmtsize(pp->lg_mediasize));
382 while ((pp = find_provider(gp, first)) != NULL) {
383 s = find_provcfg(pp, "start");
385 s = find_provcfg(pp, "offset");
386 sector = atoll(s) / secsz;
390 s = find_provcfg(pp, "end");
392 s = find_provcfg(pp, "length");
393 length = atoll(s) / secsz;
394 end = sector + length - 1;
397 length = end - sector + 1;
399 s = find_provcfg(pp, "index");
401 if (first < sector) {
402 printf(" %*llu %*llu %*s - free - (%s)\n",
403 wblocks, first, wblocks, sector - first,
405 fmtsize((sector - first) * secsz));
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));
414 length = last - first + 1;
415 printf(" %*llu %*llu %*s - free - (%s)\n",
416 wblocks, first, wblocks, length,
418 fmtsize(length * secsz));
424 gpart_show_hasopt(struct gctl_req *req, const char *opt, const char *elt)
427 if (!gctl_get_int(req, opt))
431 errx(EXIT_FAILURE, "-l and -r are mutually exclusive");
437 gpart_show(struct gctl_req *req, unsigned int fl __unused)
440 struct gclass *classp;
442 const char *element, *name;
446 if (gpart_show_hasopt(req, "show_label", element))
448 if (gpart_show_hasopt(req, "show_rawtype", element))
453 name = gctl_get_ascii(req, "class");
456 error = geom_gettree(&mesh);
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);
464 nargs = gctl_get_int(req, "nargs");
466 for (i = 0; i < nargs; i++) {
467 name = gctl_get_ascii(req, "arg%d", i);
468 gp = find_geom(classp, name);
470 gpart_show_geom(gp, element);
472 errx(EXIT_FAILURE, "No such geom: %s.", name);
475 LIST_FOREACH(gp, &classp->lg_geom, lg_geom) {
476 gpart_show_geom(gp, element);
479 geom_deletetree(&mesh);
483 gpart_bootfile_read(const char *bootfile, ssize_t *size)
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);
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,
501 fd = open(bootfile, O_RDONLY);
503 err(EXIT_FAILURE, "%s", bootfile);
504 code = malloc(*size);
506 err(EXIT_FAILURE, NULL);
507 if (read(fd, code, *size) != *size)
508 err(EXIT_FAILURE, "%s", bootfile);
515 gpart_write_partcode(struct gctl_req *req, int idx, void *code, ssize_t size)
519 struct gclass *classp;
521 struct gprovider *pp;
527 s = gctl_get_ascii(req, "class");
530 error = geom_gettree(&mesh);
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);
538 s = gctl_get_ascii(req, "geom");
539 gp = find_geom(classp, s);
541 errx(EXIT_FAILURE, "No such geom: %s.", s);
543 LIST_FOREACH(pp, &gp->lg_provider, lg_provider) {
544 s = find_provcfg(pp, "index");
552 snprintf(dsf, sizeof(dsf), "/dev/%s", pp->lg_name);
553 fd = open(dsf, O_WRONLY);
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);
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.
566 bsize = (size + pp->lg_sectorsize - 1) /
567 pp->lg_sectorsize * pp->lg_sectorsize;
568 buf = calloc(1, bsize);
570 err(EXIT_FAILURE, "%s", dsf);
571 bcopy(code, buf, size);
572 if (write(fd, buf, bsize) != bsize)
573 err(EXIT_FAILURE, "%s", dsf);
577 errx(EXIT_FAILURE, "invalid partition index");
579 geom_deletetree(&mesh);
583 gpart_bootcode(struct gctl_req *req, unsigned int fl)
587 void *bootcode, *partcode;
588 size_t bootsize, partsize;
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,
598 errc(EXIT_FAILURE, error, "internal error");
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);
610 errc(EXIT_FAILURE, error, "internal error");
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);
625 errc(EXIT_FAILURE, error, "internal error");
629 if (partcode != NULL) {
631 errx(EXIT_FAILURE, "missing -i option");
632 gpart_write_partcode(req, idx, partcode, partsize);
634 if (bootcode == NULL)
635 errx(EXIT_FAILURE, "no -b nor -p");
638 if (bootcode != NULL)
639 gpart_issue(req, fl);
643 gpart_issue(struct gctl_req *req, unsigned int fl __unused)
650 /* autofill parameters (if applicable). */
651 error = gpart_autofill(req);
653 warnc(error, "autofill");
654 status = EXIT_FAILURE;
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') {
664 status = EXIT_SUCCESS;
668 error = strtol(errstr, &errmsg, 0);
669 if (errmsg != errstr) {
670 while (errmsg[0] == ' ')
672 if (errmsg[0] != '\0')
673 warnc(error, "%s", errmsg);
679 status = EXIT_FAILURE;