g_part.c revision 236023
1/*- 2 * Copyright (c) 2002, 2005-2009 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 * 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25 */ 26 27#include <sys/cdefs.h> 28__FBSDID("$FreeBSD: head/sys/geom/part/g_part.c 236023 2012-05-25 20:33:34Z marcel $"); 29 30#include <sys/param.h> 31#include <sys/bio.h> 32#include <sys/diskmbr.h> 33#include <sys/endian.h> 34#include <sys/kernel.h> 35#include <sys/kobj.h> 36#include <sys/limits.h> 37#include <sys/lock.h> 38#include <sys/malloc.h> 39#include <sys/mutex.h> 40#include <sys/queue.h> 41#include <sys/sbuf.h> 42#include <sys/sysctl.h> 43#include <sys/systm.h> 44#include <sys/uuid.h> 45#include <geom/geom.h> 46#include <geom/geom_ctl.h> 47#include <geom/geom_int.h> 48#include <geom/part/g_part.h> 49 50#include "g_part_if.h" 51 52#ifndef _PATH_DEV 53#define _PATH_DEV "/dev/" 54#endif 55 56static kobj_method_t g_part_null_methods[] = { 57 { 0, 0 } 58}; 59 60static struct g_part_scheme g_part_null_scheme = { 61 "(none)", 62 g_part_null_methods, 63 sizeof(struct g_part_table), 64}; 65 66TAILQ_HEAD(, g_part_scheme) g_part_schemes = 67 TAILQ_HEAD_INITIALIZER(g_part_schemes); 68 69struct g_part_alias_list { 70 const char *lexeme; 71 enum g_part_alias alias; 72} g_part_alias_list[G_PART_ALIAS_COUNT] = { 73 { "apple-boot", G_PART_ALIAS_APPLE_BOOT }, 74 { "apple-hfs", G_PART_ALIAS_APPLE_HFS }, 75 { "apple-label", G_PART_ALIAS_APPLE_LABEL }, 76 { "apple-raid", G_PART_ALIAS_APPLE_RAID }, 77 { "apple-raid-offline", G_PART_ALIAS_APPLE_RAID_OFFLINE }, 78 { "apple-tv-recovery", G_PART_ALIAS_APPLE_TV_RECOVERY }, 79 { "apple-ufs", G_PART_ALIAS_APPLE_UFS }, 80 { "bios-boot", G_PART_ALIAS_BIOS_BOOT }, 81 { "ebr", G_PART_ALIAS_EBR }, 82 { "efi", G_PART_ALIAS_EFI }, 83 { "fat32", G_PART_ALIAS_MS_FAT32 }, 84 { "freebsd", G_PART_ALIAS_FREEBSD }, 85 { "freebsd-boot", G_PART_ALIAS_FREEBSD_BOOT }, 86 { "freebsd-nandfs", G_PART_ALIAS_FREEBSD_NANDFS }, 87 { "freebsd-swap", G_PART_ALIAS_FREEBSD_SWAP }, 88 { "freebsd-ufs", G_PART_ALIAS_FREEBSD_UFS }, 89 { "freebsd-vinum", G_PART_ALIAS_FREEBSD_VINUM }, 90 { "freebsd-zfs", G_PART_ALIAS_FREEBSD_ZFS }, 91 { "linux-data", G_PART_ALIAS_LINUX_DATA }, 92 { "linux-lvm", G_PART_ALIAS_LINUX_LVM }, 93 { "linux-raid", G_PART_ALIAS_LINUX_RAID }, 94 { "linux-swap", G_PART_ALIAS_LINUX_SWAP }, 95 { "mbr", G_PART_ALIAS_MBR }, 96 { "ms-basic-data", G_PART_ALIAS_MS_BASIC_DATA }, 97 { "ms-ldm-data", G_PART_ALIAS_MS_LDM_DATA }, 98 { "ms-ldm-metadata", G_PART_ALIAS_MS_LDM_METADATA }, 99 { "ms-reserved", G_PART_ALIAS_MS_RESERVED }, 100 { "ntfs", G_PART_ALIAS_MS_NTFS }, 101 { "netbsd-ccd", G_PART_ALIAS_NETBSD_CCD }, 102 { "netbsd-cgd", G_PART_ALIAS_NETBSD_CGD }, 103 { "netbsd-ffs", G_PART_ALIAS_NETBSD_FFS }, 104 { "netbsd-lfs", G_PART_ALIAS_NETBSD_LFS }, 105 { "netbsd-raid", G_PART_ALIAS_NETBSD_RAID }, 106 { "netbsd-swap", G_PART_ALIAS_NETBSD_SWAP }, 107 { "vmware-vmfs", G_PART_ALIAS_VMFS }, 108 { "vmware-vmkdiag", G_PART_ALIAS_VMKDIAG }, 109 { "vmware-reserved", G_PART_ALIAS_VMRESERVED }, 110}; 111 112SYSCTL_DECL(_kern_geom); 113SYSCTL_NODE(_kern_geom, OID_AUTO, part, CTLFLAG_RW, 0, 114 "GEOM_PART stuff"); 115static u_int check_integrity = 1; 116TUNABLE_INT("kern.geom.part.check_integrity", &check_integrity); 117SYSCTL_UINT(_kern_geom_part, OID_AUTO, check_integrity, 118 CTLFLAG_RW | CTLFLAG_TUN, &check_integrity, 1, 119 "Enable integrity checking"); 120 121/* 122 * The GEOM partitioning class. 123 */ 124static g_ctl_req_t g_part_ctlreq; 125static g_ctl_destroy_geom_t g_part_destroy_geom; 126static g_fini_t g_part_fini; 127static g_init_t g_part_init; 128static g_taste_t g_part_taste; 129 130static g_access_t g_part_access; 131static g_dumpconf_t g_part_dumpconf; 132static g_orphan_t g_part_orphan; 133static g_spoiled_t g_part_spoiled; 134static g_start_t g_part_start; 135 136static struct g_class g_part_class = { 137 .name = "PART", 138 .version = G_VERSION, 139 /* Class methods. */ 140 .ctlreq = g_part_ctlreq, 141 .destroy_geom = g_part_destroy_geom, 142 .fini = g_part_fini, 143 .init = g_part_init, 144 .taste = g_part_taste, 145 /* Geom methods. */ 146 .access = g_part_access, 147 .dumpconf = g_part_dumpconf, 148 .orphan = g_part_orphan, 149 .spoiled = g_part_spoiled, 150 .start = g_part_start, 151}; 152 153DECLARE_GEOM_CLASS(g_part_class, g_part); 154MODULE_VERSION(g_part, 0); 155 156/* 157 * Support functions. 158 */ 159 160static void g_part_wither(struct g_geom *, int); 161 162const char * 163g_part_alias_name(enum g_part_alias alias) 164{ 165 int i; 166 167 for (i = 0; i < G_PART_ALIAS_COUNT; i++) { 168 if (g_part_alias_list[i].alias != alias) 169 continue; 170 return (g_part_alias_list[i].lexeme); 171 } 172 173 return (NULL); 174} 175 176void 177g_part_geometry_heads(off_t blocks, u_int sectors, off_t *bestchs, 178 u_int *bestheads) 179{ 180 static u_int candidate_heads[] = { 1, 2, 16, 32, 64, 128, 255, 0 }; 181 off_t chs, cylinders; 182 u_int heads; 183 int idx; 184 185 *bestchs = 0; 186 *bestheads = 0; 187 for (idx = 0; candidate_heads[idx] != 0; idx++) { 188 heads = candidate_heads[idx]; 189 cylinders = blocks / heads / sectors; 190 if (cylinders < heads || cylinders < sectors) 191 break; 192 if (cylinders > 1023) 193 continue; 194 chs = cylinders * heads * sectors; 195 if (chs > *bestchs || (chs == *bestchs && *bestheads == 1)) { 196 *bestchs = chs; 197 *bestheads = heads; 198 } 199 } 200} 201 202static void 203g_part_geometry(struct g_part_table *table, struct g_consumer *cp, 204 off_t blocks) 205{ 206 static u_int candidate_sectors[] = { 1, 9, 17, 33, 63, 0 }; 207 off_t chs, bestchs; 208 u_int heads, sectors; 209 int idx; 210 211 if (g_getattr("GEOM::fwsectors", cp, §ors) != 0 || sectors == 0 || 212 g_getattr("GEOM::fwheads", cp, &heads) != 0 || heads == 0) { 213 table->gpt_fixgeom = 0; 214 table->gpt_heads = 0; 215 table->gpt_sectors = 0; 216 bestchs = 0; 217 for (idx = 0; candidate_sectors[idx] != 0; idx++) { 218 sectors = candidate_sectors[idx]; 219 g_part_geometry_heads(blocks, sectors, &chs, &heads); 220 if (chs == 0) 221 continue; 222 /* 223 * Prefer a geometry with sectors > 1, but only if 224 * it doesn't bump down the number of heads to 1. 225 */ 226 if (chs > bestchs || (chs == bestchs && heads > 1 && 227 table->gpt_sectors == 1)) { 228 bestchs = chs; 229 table->gpt_heads = heads; 230 table->gpt_sectors = sectors; 231 } 232 } 233 /* 234 * If we didn't find a geometry at all, then the disk is 235 * too big. This means we can use the maximum number of 236 * heads and sectors. 237 */ 238 if (bestchs == 0) { 239 table->gpt_heads = 255; 240 table->gpt_sectors = 63; 241 } 242 } else { 243 table->gpt_fixgeom = 1; 244 table->gpt_heads = heads; 245 table->gpt_sectors = sectors; 246 } 247} 248 249#define DPRINTF(...) if (bootverbose) { \ 250 printf("GEOM_PART: " __VA_ARGS__); \ 251} 252 253static int 254g_part_check_integrity(struct g_part_table *table, struct g_consumer *cp) 255{ 256 struct g_part_entry *e1, *e2; 257 struct g_provider *pp; 258 off_t offset; 259 int failed; 260 261 failed = 0; 262 pp = cp->provider; 263 if (table->gpt_last < table->gpt_first) { 264 DPRINTF("last LBA is below first LBA: %jd < %jd\n", 265 (intmax_t)table->gpt_last, (intmax_t)table->gpt_first); 266 failed++; 267 } 268 if (table->gpt_last > pp->mediasize / pp->sectorsize - 1) { 269 DPRINTF("last LBA extends beyond mediasize: " 270 "%jd > %jd\n", (intmax_t)table->gpt_last, 271 (intmax_t)pp->mediasize / pp->sectorsize - 1); 272 failed++; 273 } 274 LIST_FOREACH(e1, &table->gpt_entry, gpe_entry) { 275 if (e1->gpe_deleted || e1->gpe_internal) 276 continue; 277 if (e1->gpe_start < table->gpt_first) { 278 DPRINTF("partition %d has start offset below first " 279 "LBA: %jd < %jd\n", e1->gpe_index, 280 (intmax_t)e1->gpe_start, 281 (intmax_t)table->gpt_first); 282 failed++; 283 } 284 if (e1->gpe_start > table->gpt_last) { 285 DPRINTF("partition %d has start offset beyond last " 286 "LBA: %jd > %jd\n", e1->gpe_index, 287 (intmax_t)e1->gpe_start, 288 (intmax_t)table->gpt_last); 289 failed++; 290 } 291 if (e1->gpe_end < e1->gpe_start) { 292 DPRINTF("partition %d has end offset below start " 293 "offset: %jd < %jd\n", e1->gpe_index, 294 (intmax_t)e1->gpe_end, 295 (intmax_t)e1->gpe_start); 296 failed++; 297 } 298 if (e1->gpe_end > table->gpt_last) { 299 DPRINTF("partition %d has end offset beyond last " 300 "LBA: %jd > %jd\n", e1->gpe_index, 301 (intmax_t)e1->gpe_end, 302 (intmax_t)table->gpt_last); 303 failed++; 304 } 305 if (pp->stripesize > 0) { 306 offset = e1->gpe_start * pp->sectorsize; 307 if (e1->gpe_offset > offset) 308 offset = e1->gpe_offset; 309 if ((offset + pp->stripeoffset) % pp->stripesize) { 310 DPRINTF("partition %d is not aligned on %u " 311 "bytes\n", e1->gpe_index, pp->stripesize); 312 /* Don't treat this as a critical failure */ 313 } 314 } 315 e2 = e1; 316 while ((e2 = LIST_NEXT(e2, gpe_entry)) != NULL) { 317 if (e2->gpe_deleted || e2->gpe_internal) 318 continue; 319 if (e1->gpe_start >= e2->gpe_start && 320 e1->gpe_start <= e2->gpe_end) { 321 DPRINTF("partition %d has start offset inside " 322 "partition %d: start[%d] %jd >= start[%d] " 323 "%jd <= end[%d] %jd\n", 324 e1->gpe_index, e2->gpe_index, 325 e2->gpe_index, (intmax_t)e2->gpe_start, 326 e1->gpe_index, (intmax_t)e1->gpe_start, 327 e2->gpe_index, (intmax_t)e2->gpe_end); 328 failed++; 329 } 330 if (e1->gpe_end >= e2->gpe_start && 331 e1->gpe_end <= e2->gpe_end) { 332 DPRINTF("partition %d has end offset inside " 333 "partition %d: start[%d] %jd >= end[%d] " 334 "%jd <= end[%d] %jd\n", 335 e1->gpe_index, e2->gpe_index, 336 e2->gpe_index, (intmax_t)e2->gpe_start, 337 e1->gpe_index, (intmax_t)e1->gpe_end, 338 e2->gpe_index, (intmax_t)e2->gpe_end); 339 failed++; 340 } 341 if (e1->gpe_start < e2->gpe_start && 342 e1->gpe_end > e2->gpe_end) { 343 DPRINTF("partition %d contains partition %d: " 344 "start[%d] %jd > start[%d] %jd, end[%d] " 345 "%jd < end[%d] %jd\n", 346 e1->gpe_index, e2->gpe_index, 347 e1->gpe_index, (intmax_t)e1->gpe_start, 348 e2->gpe_index, (intmax_t)e2->gpe_start, 349 e2->gpe_index, (intmax_t)e2->gpe_end, 350 e1->gpe_index, (intmax_t)e1->gpe_end); 351 failed++; 352 } 353 } 354 } 355 if (failed != 0) { 356 printf("GEOM_PART: integrity check failed (%s, %s)\n", 357 pp->name, table->gpt_scheme->name); 358 if (check_integrity != 0) 359 return (EINVAL); 360 table->gpt_corrupt = 1; 361 } 362 return (0); 363} 364#undef DPRINTF 365 366struct g_part_entry * 367g_part_new_entry(struct g_part_table *table, int index, quad_t start, 368 quad_t end) 369{ 370 struct g_part_entry *entry, *last; 371 372 last = NULL; 373 LIST_FOREACH(entry, &table->gpt_entry, gpe_entry) { 374 if (entry->gpe_index == index) 375 break; 376 if (entry->gpe_index > index) { 377 entry = NULL; 378 break; 379 } 380 last = entry; 381 } 382 if (entry == NULL) { 383 entry = g_malloc(table->gpt_scheme->gps_entrysz, 384 M_WAITOK | M_ZERO); 385 entry->gpe_index = index; 386 if (last == NULL) 387 LIST_INSERT_HEAD(&table->gpt_entry, entry, gpe_entry); 388 else 389 LIST_INSERT_AFTER(last, entry, gpe_entry); 390 } else 391 entry->gpe_offset = 0; 392 entry->gpe_start = start; 393 entry->gpe_end = end; 394 return (entry); 395} 396 397static void 398g_part_new_provider(struct g_geom *gp, struct g_part_table *table, 399 struct g_part_entry *entry) 400{ 401 struct g_consumer *cp; 402 struct g_provider *pp; 403 struct sbuf *sb; 404 off_t offset; 405 406 cp = LIST_FIRST(&gp->consumer); 407 pp = cp->provider; 408 409 offset = entry->gpe_start * pp->sectorsize; 410 if (entry->gpe_offset < offset) 411 entry->gpe_offset = offset; 412 413 if (entry->gpe_pp == NULL) { 414 sb = sbuf_new_auto(); 415 G_PART_FULLNAME(table, entry, sb, gp->name); 416 sbuf_finish(sb); 417 entry->gpe_pp = g_new_providerf(gp, "%s", sbuf_data(sb)); 418 sbuf_delete(sb); 419 entry->gpe_pp->private = entry; /* Close the circle. */ 420 } 421 entry->gpe_pp->index = entry->gpe_index - 1; /* index is 1-based. */ 422 entry->gpe_pp->mediasize = (entry->gpe_end - entry->gpe_start + 1) * 423 pp->sectorsize; 424 entry->gpe_pp->mediasize -= entry->gpe_offset - offset; 425 entry->gpe_pp->sectorsize = pp->sectorsize; 426 entry->gpe_pp->flags = pp->flags & G_PF_CANDELETE; 427 entry->gpe_pp->stripesize = pp->stripesize; 428 entry->gpe_pp->stripeoffset = pp->stripeoffset + entry->gpe_offset; 429 if (pp->stripesize > 0) 430 entry->gpe_pp->stripeoffset %= pp->stripesize; 431 g_error_provider(entry->gpe_pp, 0); 432} 433 434static struct g_geom* 435g_part_find_geom(const char *name) 436{ 437 struct g_geom *gp; 438 LIST_FOREACH(gp, &g_part_class.geom, geom) { 439 if (!strcmp(name, gp->name)) 440 break; 441 } 442 return (gp); 443} 444 445static int 446g_part_parm_geom(struct gctl_req *req, const char *name, struct g_geom **v) 447{ 448 struct g_geom *gp; 449 const char *gname; 450 451 gname = gctl_get_asciiparam(req, name); 452 if (gname == NULL) 453 return (ENOATTR); 454 if (strncmp(gname, _PATH_DEV, sizeof(_PATH_DEV) - 1) == 0) 455 gname += sizeof(_PATH_DEV) - 1; 456 gp = g_part_find_geom(gname); 457 if (gp == NULL) { 458 gctl_error(req, "%d %s '%s'", EINVAL, name, gname); 459 return (EINVAL); 460 } 461 if ((gp->flags & G_GEOM_WITHER) != 0) { 462 gctl_error(req, "%d %s", ENXIO, gname); 463 return (ENXIO); 464 } 465 *v = gp; 466 return (0); 467} 468 469static int 470g_part_parm_provider(struct gctl_req *req, const char *name, 471 struct g_provider **v) 472{ 473 struct g_provider *pp; 474 const char *pname; 475 476 pname = gctl_get_asciiparam(req, name); 477 if (pname == NULL) 478 return (ENOATTR); 479 if (strncmp(pname, _PATH_DEV, sizeof(_PATH_DEV) - 1) == 0) 480 pname += sizeof(_PATH_DEV) - 1; 481 pp = g_provider_by_name(pname); 482 if (pp == NULL) { 483 gctl_error(req, "%d %s '%s'", EINVAL, name, pname); 484 return (EINVAL); 485 } 486 *v = pp; 487 return (0); 488} 489 490static int 491g_part_parm_quad(struct gctl_req *req, const char *name, quad_t *v) 492{ 493 const char *p; 494 char *x; 495 quad_t q; 496 497 p = gctl_get_asciiparam(req, name); 498 if (p == NULL) 499 return (ENOATTR); 500 q = strtoq(p, &x, 0); 501 if (*x != '\0' || q < 0) { 502 gctl_error(req, "%d %s '%s'", EINVAL, name, p); 503 return (EINVAL); 504 } 505 *v = q; 506 return (0); 507} 508 509static int 510g_part_parm_scheme(struct gctl_req *req, const char *name, 511 struct g_part_scheme **v) 512{ 513 struct g_part_scheme *s; 514 const char *p; 515 516 p = gctl_get_asciiparam(req, name); 517 if (p == NULL) 518 return (ENOATTR); 519 TAILQ_FOREACH(s, &g_part_schemes, scheme_list) { 520 if (s == &g_part_null_scheme) 521 continue; 522 if (!strcasecmp(s->name, p)) 523 break; 524 } 525 if (s == NULL) { 526 gctl_error(req, "%d %s '%s'", EINVAL, name, p); 527 return (EINVAL); 528 } 529 *v = s; 530 return (0); 531} 532 533static int 534g_part_parm_str(struct gctl_req *req, const char *name, const char **v) 535{ 536 const char *p; 537 538 p = gctl_get_asciiparam(req, name); 539 if (p == NULL) 540 return (ENOATTR); 541 /* An empty label is always valid. */ 542 if (strcmp(name, "label") != 0 && p[0] == '\0') { 543 gctl_error(req, "%d %s '%s'", EINVAL, name, p); 544 return (EINVAL); 545 } 546 *v = p; 547 return (0); 548} 549 550static int 551g_part_parm_intmax(struct gctl_req *req, const char *name, u_int *v) 552{ 553 const intmax_t *p; 554 int size; 555 556 p = gctl_get_param(req, name, &size); 557 if (p == NULL) 558 return (ENOATTR); 559 if (size != sizeof(*p) || *p < 0 || *p > INT_MAX) { 560 gctl_error(req, "%d %s '%jd'", EINVAL, name, *p); 561 return (EINVAL); 562 } 563 *v = (u_int)*p; 564 return (0); 565} 566 567static int 568g_part_parm_uint32(struct gctl_req *req, const char *name, u_int *v) 569{ 570 const uint32_t *p; 571 int size; 572 573 p = gctl_get_param(req, name, &size); 574 if (p == NULL) 575 return (ENOATTR); 576 if (size != sizeof(*p) || *p > INT_MAX) { 577 gctl_error(req, "%d %s '%u'", EINVAL, name, (unsigned int)*p); 578 return (EINVAL); 579 } 580 *v = (u_int)*p; 581 return (0); 582} 583 584static int 585g_part_parm_bootcode(struct gctl_req *req, const char *name, const void **v, 586 unsigned int *s) 587{ 588 const void *p; 589 int size; 590 591 p = gctl_get_param(req, name, &size); 592 if (p == NULL) 593 return (ENOATTR); 594 *v = p; 595 *s = size; 596 return (0); 597} 598 599static int 600g_part_probe(struct g_geom *gp, struct g_consumer *cp, int depth) 601{ 602 struct g_part_scheme *iter, *scheme; 603 struct g_part_table *table; 604 int pri, probe; 605 606 table = gp->softc; 607 scheme = (table != NULL) ? table->gpt_scheme : NULL; 608 pri = (scheme != NULL) ? G_PART_PROBE(table, cp) : INT_MIN; 609 if (pri == 0) 610 goto done; 611 if (pri > 0) { /* error */ 612 scheme = NULL; 613 pri = INT_MIN; 614 } 615 616 TAILQ_FOREACH(iter, &g_part_schemes, scheme_list) { 617 if (iter == &g_part_null_scheme) 618 continue; 619 table = (void *)kobj_create((kobj_class_t)iter, M_GEOM, 620 M_WAITOK); 621 table->gpt_gp = gp; 622 table->gpt_scheme = iter; 623 table->gpt_depth = depth; 624 probe = G_PART_PROBE(table, cp); 625 if (probe <= 0 && probe > pri) { 626 pri = probe; 627 scheme = iter; 628 if (gp->softc != NULL) 629 kobj_delete((kobj_t)gp->softc, M_GEOM); 630 gp->softc = table; 631 if (pri == 0) 632 goto done; 633 } else 634 kobj_delete((kobj_t)table, M_GEOM); 635 } 636 637done: 638 return ((scheme == NULL) ? ENXIO : 0); 639} 640 641/* 642 * Control request functions. 643 */ 644 645static int 646g_part_ctl_add(struct gctl_req *req, struct g_part_parms *gpp) 647{ 648 struct g_geom *gp; 649 struct g_provider *pp; 650 struct g_part_entry *delent, *last, *entry; 651 struct g_part_table *table; 652 struct sbuf *sb; 653 quad_t end; 654 unsigned int index; 655 int error; 656 657 gp = gpp->gpp_geom; 658 G_PART_TRACE((G_T_TOPOLOGY, "%s(%s)", __func__, gp->name)); 659 g_topology_assert(); 660 661 pp = LIST_FIRST(&gp->consumer)->provider; 662 table = gp->softc; 663 end = gpp->gpp_start + gpp->gpp_size - 1; 664 665 if (gpp->gpp_start < table->gpt_first || 666 gpp->gpp_start > table->gpt_last) { 667 gctl_error(req, "%d start '%jd'", EINVAL, 668 (intmax_t)gpp->gpp_start); 669 return (EINVAL); 670 } 671 if (end < gpp->gpp_start || end > table->gpt_last) { 672 gctl_error(req, "%d size '%jd'", EINVAL, 673 (intmax_t)gpp->gpp_size); 674 return (EINVAL); 675 } 676 if (gpp->gpp_index > table->gpt_entries) { 677 gctl_error(req, "%d index '%d'", EINVAL, gpp->gpp_index); 678 return (EINVAL); 679 } 680 681 delent = last = NULL; 682 index = (gpp->gpp_index > 0) ? gpp->gpp_index : 1; 683 LIST_FOREACH(entry, &table->gpt_entry, gpe_entry) { 684 if (entry->gpe_deleted) { 685 if (entry->gpe_index == index) 686 delent = entry; 687 continue; 688 } 689 if (entry->gpe_index == index) 690 index = entry->gpe_index + 1; 691 if (entry->gpe_index < index) 692 last = entry; 693 if (entry->gpe_internal) 694 continue; 695 if (gpp->gpp_start >= entry->gpe_start && 696 gpp->gpp_start <= entry->gpe_end) { 697 gctl_error(req, "%d start '%jd'", ENOSPC, 698 (intmax_t)gpp->gpp_start); 699 return (ENOSPC); 700 } 701 if (end >= entry->gpe_start && end <= entry->gpe_end) { 702 gctl_error(req, "%d end '%jd'", ENOSPC, (intmax_t)end); 703 return (ENOSPC); 704 } 705 if (gpp->gpp_start < entry->gpe_start && end > entry->gpe_end) { 706 gctl_error(req, "%d size '%jd'", ENOSPC, 707 (intmax_t)gpp->gpp_size); 708 return (ENOSPC); 709 } 710 } 711 if (gpp->gpp_index > 0 && index != gpp->gpp_index) { 712 gctl_error(req, "%d index '%d'", EEXIST, gpp->gpp_index); 713 return (EEXIST); 714 } 715 if (index > table->gpt_entries) { 716 gctl_error(req, "%d index '%d'", ENOSPC, index); 717 return (ENOSPC); 718 } 719 720 entry = (delent == NULL) ? g_malloc(table->gpt_scheme->gps_entrysz, 721 M_WAITOK | M_ZERO) : delent; 722 entry->gpe_index = index; 723 entry->gpe_start = gpp->gpp_start; 724 entry->gpe_end = end; 725 error = G_PART_ADD(table, entry, gpp); 726 if (error) { 727 gctl_error(req, "%d", error); 728 if (delent == NULL) 729 g_free(entry); 730 return (error); 731 } 732 if (delent == NULL) { 733 if (last == NULL) 734 LIST_INSERT_HEAD(&table->gpt_entry, entry, gpe_entry); 735 else 736 LIST_INSERT_AFTER(last, entry, gpe_entry); 737 entry->gpe_created = 1; 738 } else { 739 entry->gpe_deleted = 0; 740 entry->gpe_modified = 1; 741 } 742 g_part_new_provider(gp, table, entry); 743 744 /* Provide feedback if so requested. */ 745 if (gpp->gpp_parms & G_PART_PARM_OUTPUT) { 746 sb = sbuf_new_auto(); 747 G_PART_FULLNAME(table, entry, sb, gp->name); 748 if (pp->stripesize > 0 && entry->gpe_pp->stripeoffset != 0) 749 sbuf_printf(sb, " added, but partition is not " 750 "aligned on %u bytes\n", pp->stripesize); 751 else 752 sbuf_cat(sb, " added\n"); 753 sbuf_finish(sb); 754 gctl_set_param(req, "output", sbuf_data(sb), sbuf_len(sb) + 1); 755 sbuf_delete(sb); 756 } 757 return (0); 758} 759 760static int 761g_part_ctl_bootcode(struct gctl_req *req, struct g_part_parms *gpp) 762{ 763 struct g_geom *gp; 764 struct g_part_table *table; 765 struct sbuf *sb; 766 int error, sz; 767 768 gp = gpp->gpp_geom; 769 G_PART_TRACE((G_T_TOPOLOGY, "%s(%s)", __func__, gp->name)); 770 g_topology_assert(); 771 772 table = gp->softc; 773 sz = table->gpt_scheme->gps_bootcodesz; 774 if (sz == 0) { 775 error = ENODEV; 776 goto fail; 777 } 778 if (gpp->gpp_codesize > sz) { 779 error = EFBIG; 780 goto fail; 781 } 782 783 error = G_PART_BOOTCODE(table, gpp); 784 if (error) 785 goto fail; 786 787 /* Provide feedback if so requested. */ 788 if (gpp->gpp_parms & G_PART_PARM_OUTPUT) { 789 sb = sbuf_new_auto(); 790 sbuf_printf(sb, "bootcode written to %s\n", gp->name); 791 sbuf_finish(sb); 792 gctl_set_param(req, "output", sbuf_data(sb), sbuf_len(sb) + 1); 793 sbuf_delete(sb); 794 } 795 return (0); 796 797 fail: 798 gctl_error(req, "%d", error); 799 return (error); 800} 801 802static int 803g_part_ctl_commit(struct gctl_req *req, struct g_part_parms *gpp) 804{ 805 struct g_consumer *cp; 806 struct g_geom *gp; 807 struct g_provider *pp; 808 struct g_part_entry *entry, *tmp; 809 struct g_part_table *table; 810 char *buf; 811 int error, i; 812 813 gp = gpp->gpp_geom; 814 G_PART_TRACE((G_T_TOPOLOGY, "%s(%s)", __func__, gp->name)); 815 g_topology_assert(); 816 817 table = gp->softc; 818 if (!table->gpt_opened) { 819 gctl_error(req, "%d", EPERM); 820 return (EPERM); 821 } 822 823 g_topology_unlock(); 824 825 cp = LIST_FIRST(&gp->consumer); 826 if ((table->gpt_smhead | table->gpt_smtail) != 0) { 827 pp = cp->provider; 828 buf = g_malloc(pp->sectorsize, M_WAITOK | M_ZERO); 829 while (table->gpt_smhead != 0) { 830 i = ffs(table->gpt_smhead) - 1; 831 error = g_write_data(cp, i * pp->sectorsize, buf, 832 pp->sectorsize); 833 if (error) { 834 g_free(buf); 835 goto fail; 836 } 837 table->gpt_smhead &= ~(1 << i); 838 } 839 while (table->gpt_smtail != 0) { 840 i = ffs(table->gpt_smtail) - 1; 841 error = g_write_data(cp, pp->mediasize - (i + 1) * 842 pp->sectorsize, buf, pp->sectorsize); 843 if (error) { 844 g_free(buf); 845 goto fail; 846 } 847 table->gpt_smtail &= ~(1 << i); 848 } 849 g_free(buf); 850 } 851 852 if (table->gpt_scheme == &g_part_null_scheme) { 853 g_topology_lock(); 854 g_access(cp, -1, -1, -1); 855 g_part_wither(gp, ENXIO); 856 return (0); 857 } 858 859 error = G_PART_WRITE(table, cp); 860 if (error) 861 goto fail; 862 863 LIST_FOREACH_SAFE(entry, &table->gpt_entry, gpe_entry, tmp) { 864 if (!entry->gpe_deleted) { 865 entry->gpe_created = 0; 866 entry->gpe_modified = 0; 867 continue; 868 } 869 LIST_REMOVE(entry, gpe_entry); 870 g_free(entry); 871 } 872 table->gpt_created = 0; 873 table->gpt_opened = 0; 874 875 g_topology_lock(); 876 g_access(cp, -1, -1, -1); 877 return (0); 878 879fail: 880 g_topology_lock(); 881 gctl_error(req, "%d", error); 882 return (error); 883} 884 885static int 886g_part_ctl_create(struct gctl_req *req, struct g_part_parms *gpp) 887{ 888 struct g_consumer *cp; 889 struct g_geom *gp; 890 struct g_provider *pp; 891 struct g_part_scheme *scheme; 892 struct g_part_table *null, *table; 893 struct sbuf *sb; 894 int attr, error; 895 896 pp = gpp->gpp_provider; 897 scheme = gpp->gpp_scheme; 898 G_PART_TRACE((G_T_TOPOLOGY, "%s(%s)", __func__, pp->name)); 899 g_topology_assert(); 900 901 /* Check that there isn't already a g_part geom on the provider. */ 902 gp = g_part_find_geom(pp->name); 903 if (gp != NULL) { 904 null = gp->softc; 905 if (null->gpt_scheme != &g_part_null_scheme) { 906 gctl_error(req, "%d geom '%s'", EEXIST, pp->name); 907 return (EEXIST); 908 } 909 } else 910 null = NULL; 911 912 if ((gpp->gpp_parms & G_PART_PARM_ENTRIES) && 913 (gpp->gpp_entries < scheme->gps_minent || 914 gpp->gpp_entries > scheme->gps_maxent)) { 915 gctl_error(req, "%d entries '%d'", EINVAL, gpp->gpp_entries); 916 return (EINVAL); 917 } 918 919 if (null == NULL) 920 gp = g_new_geomf(&g_part_class, "%s", pp->name); 921 gp->softc = kobj_create((kobj_class_t)gpp->gpp_scheme, M_GEOM, 922 M_WAITOK); 923 table = gp->softc; 924 table->gpt_gp = gp; 925 table->gpt_scheme = gpp->gpp_scheme; 926 table->gpt_entries = (gpp->gpp_parms & G_PART_PARM_ENTRIES) ? 927 gpp->gpp_entries : scheme->gps_minent; 928 LIST_INIT(&table->gpt_entry); 929 if (null == NULL) { 930 cp = g_new_consumer(gp); 931 error = g_attach(cp, pp); 932 if (error == 0) 933 error = g_access(cp, 1, 1, 1); 934 if (error != 0) { 935 g_part_wither(gp, error); 936 gctl_error(req, "%d geom '%s'", error, pp->name); 937 return (error); 938 } 939 table->gpt_opened = 1; 940 } else { 941 cp = LIST_FIRST(&gp->consumer); 942 table->gpt_opened = null->gpt_opened; 943 table->gpt_smhead = null->gpt_smhead; 944 table->gpt_smtail = null->gpt_smtail; 945 } 946 947 g_topology_unlock(); 948 949 /* Make sure the provider has media. */ 950 if (pp->mediasize == 0 || pp->sectorsize == 0) { 951 error = ENODEV; 952 goto fail; 953 } 954 955 /* Make sure we can nest and if so, determine our depth. */ 956 error = g_getattr("PART::isleaf", cp, &attr); 957 if (!error && attr) { 958 error = ENODEV; 959 goto fail; 960 } 961 error = g_getattr("PART::depth", cp, &attr); 962 table->gpt_depth = (!error) ? attr + 1 : 0; 963 964 /* 965 * Synthesize a disk geometry. Some partitioning schemes 966 * depend on it and since some file systems need it even 967 * when the partitition scheme doesn't, we do it here in 968 * scheme-independent code. 969 */ 970 g_part_geometry(table, cp, pp->mediasize / pp->sectorsize); 971 972 error = G_PART_CREATE(table, gpp); 973 if (error) 974 goto fail; 975 976 g_topology_lock(); 977 978 table->gpt_created = 1; 979 if (null != NULL) 980 kobj_delete((kobj_t)null, M_GEOM); 981 982 /* 983 * Support automatic commit by filling in the gpp_geom 984 * parameter. 985 */ 986 gpp->gpp_parms |= G_PART_PARM_GEOM; 987 gpp->gpp_geom = gp; 988 989 /* Provide feedback if so requested. */ 990 if (gpp->gpp_parms & G_PART_PARM_OUTPUT) { 991 sb = sbuf_new_auto(); 992 sbuf_printf(sb, "%s created\n", gp->name); 993 sbuf_finish(sb); 994 gctl_set_param(req, "output", sbuf_data(sb), sbuf_len(sb) + 1); 995 sbuf_delete(sb); 996 } 997 return (0); 998 999fail: 1000 g_topology_lock(); 1001 if (null == NULL) { 1002 g_access(cp, -1, -1, -1); 1003 g_part_wither(gp, error); 1004 } else { 1005 kobj_delete((kobj_t)gp->softc, M_GEOM); 1006 gp->softc = null; 1007 } 1008 gctl_error(req, "%d provider", error); 1009 return (error); 1010} 1011 1012static int 1013g_part_ctl_delete(struct gctl_req *req, struct g_part_parms *gpp) 1014{ 1015 struct g_geom *gp; 1016 struct g_provider *pp; 1017 struct g_part_entry *entry; 1018 struct g_part_table *table; 1019 struct sbuf *sb; 1020 1021 gp = gpp->gpp_geom; 1022 G_PART_TRACE((G_T_TOPOLOGY, "%s(%s)", __func__, gp->name)); 1023 g_topology_assert(); 1024 1025 table = gp->softc; 1026 1027 LIST_FOREACH(entry, &table->gpt_entry, gpe_entry) { 1028 if (entry->gpe_deleted || entry->gpe_internal) 1029 continue; 1030 if (entry->gpe_index == gpp->gpp_index) 1031 break; 1032 } 1033 if (entry == NULL) { 1034 gctl_error(req, "%d index '%d'", ENOENT, gpp->gpp_index); 1035 return (ENOENT); 1036 } 1037 1038 pp = entry->gpe_pp; 1039 if (pp != NULL) { 1040 if (pp->acr > 0 || pp->acw > 0 || pp->ace > 0) { 1041 gctl_error(req, "%d", EBUSY); 1042 return (EBUSY); 1043 } 1044 1045 pp->private = NULL; 1046 entry->gpe_pp = NULL; 1047 } 1048 1049 if (pp != NULL) 1050 g_wither_provider(pp, ENXIO); 1051 1052 /* Provide feedback if so requested. */ 1053 if (gpp->gpp_parms & G_PART_PARM_OUTPUT) { 1054 sb = sbuf_new_auto(); 1055 G_PART_FULLNAME(table, entry, sb, gp->name); 1056 sbuf_cat(sb, " deleted\n"); 1057 sbuf_finish(sb); 1058 gctl_set_param(req, "output", sbuf_data(sb), sbuf_len(sb) + 1); 1059 sbuf_delete(sb); 1060 } 1061 1062 if (entry->gpe_created) { 1063 LIST_REMOVE(entry, gpe_entry); 1064 g_free(entry); 1065 } else { 1066 entry->gpe_modified = 0; 1067 entry->gpe_deleted = 1; 1068 } 1069 return (0); 1070} 1071 1072static int 1073g_part_ctl_destroy(struct gctl_req *req, struct g_part_parms *gpp) 1074{ 1075 struct g_consumer *cp; 1076 struct g_geom *gp; 1077 struct g_provider *pp; 1078 struct g_part_entry *entry, *tmp; 1079 struct g_part_table *null, *table; 1080 struct sbuf *sb; 1081 int error; 1082 1083 gp = gpp->gpp_geom; 1084 G_PART_TRACE((G_T_TOPOLOGY, "%s(%s)", __func__, gp->name)); 1085 g_topology_assert(); 1086 1087 table = gp->softc; 1088 /* Check for busy providers. */ 1089 LIST_FOREACH(entry, &table->gpt_entry, gpe_entry) { 1090 if (entry->gpe_deleted || entry->gpe_internal) 1091 continue; 1092 if (gpp->gpp_force) { 1093 pp = entry->gpe_pp; 1094 if (pp == NULL) 1095 continue; 1096 if (pp->acr == 0 && pp->acw == 0 && pp->ace == 0) 1097 continue; 1098 } 1099 gctl_error(req, "%d", EBUSY); 1100 return (EBUSY); 1101 } 1102 1103 if (gpp->gpp_force) { 1104 /* Destroy all providers. */ 1105 LIST_FOREACH_SAFE(entry, &table->gpt_entry, gpe_entry, tmp) { 1106 pp = entry->gpe_pp; 1107 if (pp != NULL) { 1108 pp->private = NULL; 1109 g_wither_provider(pp, ENXIO); 1110 } 1111 LIST_REMOVE(entry, gpe_entry); 1112 g_free(entry); 1113 } 1114 } 1115 1116 error = G_PART_DESTROY(table, gpp); 1117 if (error) { 1118 gctl_error(req, "%d", error); 1119 return (error); 1120 } 1121 1122 gp->softc = kobj_create((kobj_class_t)&g_part_null_scheme, M_GEOM, 1123 M_WAITOK); 1124 null = gp->softc; 1125 null->gpt_gp = gp; 1126 null->gpt_scheme = &g_part_null_scheme; 1127 LIST_INIT(&null->gpt_entry); 1128 1129 cp = LIST_FIRST(&gp->consumer); 1130 pp = cp->provider; 1131 null->gpt_last = pp->mediasize / pp->sectorsize - 1; 1132 1133 null->gpt_depth = table->gpt_depth; 1134 null->gpt_opened = table->gpt_opened; 1135 null->gpt_smhead = table->gpt_smhead; 1136 null->gpt_smtail = table->gpt_smtail; 1137 1138 while ((entry = LIST_FIRST(&table->gpt_entry)) != NULL) { 1139 LIST_REMOVE(entry, gpe_entry); 1140 g_free(entry); 1141 } 1142 kobj_delete((kobj_t)table, M_GEOM); 1143 1144 /* Provide feedback if so requested. */ 1145 if (gpp->gpp_parms & G_PART_PARM_OUTPUT) { 1146 sb = sbuf_new_auto(); 1147 sbuf_printf(sb, "%s destroyed\n", gp->name); 1148 sbuf_finish(sb); 1149 gctl_set_param(req, "output", sbuf_data(sb), sbuf_len(sb) + 1); 1150 sbuf_delete(sb); 1151 } 1152 return (0); 1153} 1154 1155static int 1156g_part_ctl_modify(struct gctl_req *req, struct g_part_parms *gpp) 1157{ 1158 struct g_geom *gp; 1159 struct g_part_entry *entry; 1160 struct g_part_table *table; 1161 struct sbuf *sb; 1162 int error; 1163 1164 gp = gpp->gpp_geom; 1165 G_PART_TRACE((G_T_TOPOLOGY, "%s(%s)", __func__, gp->name)); 1166 g_topology_assert(); 1167 1168 table = gp->softc; 1169 1170 LIST_FOREACH(entry, &table->gpt_entry, gpe_entry) { 1171 if (entry->gpe_deleted || entry->gpe_internal) 1172 continue; 1173 if (entry->gpe_index == gpp->gpp_index) 1174 break; 1175 } 1176 if (entry == NULL) { 1177 gctl_error(req, "%d index '%d'", ENOENT, gpp->gpp_index); 1178 return (ENOENT); 1179 } 1180 1181 error = G_PART_MODIFY(table, entry, gpp); 1182 if (error) { 1183 gctl_error(req, "%d", error); 1184 return (error); 1185 } 1186 1187 if (!entry->gpe_created) 1188 entry->gpe_modified = 1; 1189 1190 /* Provide feedback if so requested. */ 1191 if (gpp->gpp_parms & G_PART_PARM_OUTPUT) { 1192 sb = sbuf_new_auto(); 1193 G_PART_FULLNAME(table, entry, sb, gp->name); 1194 sbuf_cat(sb, " modified\n"); 1195 sbuf_finish(sb); 1196 gctl_set_param(req, "output", sbuf_data(sb), sbuf_len(sb) + 1); 1197 sbuf_delete(sb); 1198 } 1199 return (0); 1200} 1201 1202static int 1203g_part_ctl_move(struct gctl_req *req, struct g_part_parms *gpp) 1204{ 1205 gctl_error(req, "%d verb 'move'", ENOSYS); 1206 return (ENOSYS); 1207} 1208 1209static int 1210g_part_ctl_recover(struct gctl_req *req, struct g_part_parms *gpp) 1211{ 1212 struct g_part_table *table; 1213 struct g_geom *gp; 1214 struct sbuf *sb; 1215 int error, recovered; 1216 1217 gp = gpp->gpp_geom; 1218 G_PART_TRACE((G_T_TOPOLOGY, "%s(%s)", __func__, gp->name)); 1219 g_topology_assert(); 1220 table = gp->softc; 1221 error = recovered = 0; 1222 1223 if (table->gpt_corrupt) { 1224 error = G_PART_RECOVER(table); 1225 if (error == 0) 1226 error = g_part_check_integrity(table, 1227 LIST_FIRST(&gp->consumer)); 1228 if (error) { 1229 gctl_error(req, "%d recovering '%s' failed", 1230 error, gp->name); 1231 return (error); 1232 } 1233 recovered = 1; 1234 } 1235 /* Provide feedback if so requested. */ 1236 if (gpp->gpp_parms & G_PART_PARM_OUTPUT) { 1237 sb = sbuf_new_auto(); 1238 if (recovered) 1239 sbuf_printf(sb, "%s recovered\n", gp->name); 1240 else 1241 sbuf_printf(sb, "%s recovering is not needed\n", 1242 gp->name); 1243 sbuf_finish(sb); 1244 gctl_set_param(req, "output", sbuf_data(sb), sbuf_len(sb) + 1); 1245 sbuf_delete(sb); 1246 } 1247 return (0); 1248} 1249 1250static int 1251g_part_ctl_resize(struct gctl_req *req, struct g_part_parms *gpp) 1252{ 1253 struct g_geom *gp; 1254 struct g_provider *pp; 1255 struct g_part_entry *pe, *entry; 1256 struct g_part_table *table; 1257 struct sbuf *sb; 1258 quad_t end; 1259 int error; 1260 1261 gp = gpp->gpp_geom; 1262 G_PART_TRACE((G_T_TOPOLOGY, "%s(%s)", __func__, gp->name)); 1263 g_topology_assert(); 1264 table = gp->softc; 1265 1266 /* check gpp_index */ 1267 LIST_FOREACH(entry, &table->gpt_entry, gpe_entry) { 1268 if (entry->gpe_deleted || entry->gpe_internal) 1269 continue; 1270 if (entry->gpe_index == gpp->gpp_index) 1271 break; 1272 } 1273 if (entry == NULL) { 1274 gctl_error(req, "%d index '%d'", ENOENT, gpp->gpp_index); 1275 return (ENOENT); 1276 } 1277 1278 /* check gpp_size */ 1279 end = entry->gpe_start + gpp->gpp_size - 1; 1280 if (gpp->gpp_size < 1 || end > table->gpt_last) { 1281 gctl_error(req, "%d size '%jd'", EINVAL, 1282 (intmax_t)gpp->gpp_size); 1283 return (EINVAL); 1284 } 1285 1286 LIST_FOREACH(pe, &table->gpt_entry, gpe_entry) { 1287 if (pe->gpe_deleted || pe->gpe_internal || pe == entry) 1288 continue; 1289 if (end >= pe->gpe_start && end <= pe->gpe_end) { 1290 gctl_error(req, "%d end '%jd'", ENOSPC, 1291 (intmax_t)end); 1292 return (ENOSPC); 1293 } 1294 if (entry->gpe_start < pe->gpe_start && end > pe->gpe_end) { 1295 gctl_error(req, "%d size '%jd'", ENOSPC, 1296 (intmax_t)gpp->gpp_size); 1297 return (ENOSPC); 1298 } 1299 } 1300 1301 pp = entry->gpe_pp; 1302 if ((g_debugflags & 16) == 0 && 1303 (pp->acr > 0 || pp->acw > 0 || pp->ace > 0)) { 1304 gctl_error(req, "%d", EBUSY); 1305 return (EBUSY); 1306 } 1307 1308 error = G_PART_RESIZE(table, entry, gpp); 1309 if (error) { 1310 gctl_error(req, "%d", error); 1311 return (error); 1312 } 1313 1314 if (!entry->gpe_created) 1315 entry->gpe_modified = 1; 1316 1317 /* update mediasize of changed provider */ 1318 pp->mediasize = (entry->gpe_end - entry->gpe_start + 1) * 1319 pp->sectorsize; 1320 1321 /* Provide feedback if so requested. */ 1322 if (gpp->gpp_parms & G_PART_PARM_OUTPUT) { 1323 sb = sbuf_new_auto(); 1324 G_PART_FULLNAME(table, entry, sb, gp->name); 1325 sbuf_cat(sb, " resized\n"); 1326 sbuf_finish(sb); 1327 gctl_set_param(req, "output", sbuf_data(sb), sbuf_len(sb) + 1); 1328 sbuf_delete(sb); 1329 } 1330 return (0); 1331} 1332 1333static int 1334g_part_ctl_setunset(struct gctl_req *req, struct g_part_parms *gpp, 1335 unsigned int set) 1336{ 1337 struct g_geom *gp; 1338 struct g_part_entry *entry; 1339 struct g_part_table *table; 1340 struct sbuf *sb; 1341 int error; 1342 1343 gp = gpp->gpp_geom; 1344 G_PART_TRACE((G_T_TOPOLOGY, "%s(%s)", __func__, gp->name)); 1345 g_topology_assert(); 1346 1347 table = gp->softc; 1348 1349 LIST_FOREACH(entry, &table->gpt_entry, gpe_entry) { 1350 if (entry->gpe_deleted || entry->gpe_internal) 1351 continue; 1352 if (entry->gpe_index == gpp->gpp_index) 1353 break; 1354 } 1355 if (entry == NULL) { 1356 gctl_error(req, "%d index '%d'", ENOENT, gpp->gpp_index); 1357 return (ENOENT); 1358 } 1359 1360 error = G_PART_SETUNSET(table, entry, gpp->gpp_attrib, set); 1361 if (error) { 1362 gctl_error(req, "%d attrib '%s'", error, gpp->gpp_attrib); 1363 return (error); 1364 } 1365 1366 /* Provide feedback if so requested. */ 1367 if (gpp->gpp_parms & G_PART_PARM_OUTPUT) { 1368 sb = sbuf_new_auto(); 1369 sbuf_printf(sb, "%s %sset on ", gpp->gpp_attrib, 1370 (set) ? "" : "un"); 1371 G_PART_FULLNAME(table, entry, sb, gp->name); 1372 sbuf_printf(sb, "\n"); 1373 sbuf_finish(sb); 1374 gctl_set_param(req, "output", sbuf_data(sb), sbuf_len(sb) + 1); 1375 sbuf_delete(sb); 1376 } 1377 return (0); 1378} 1379 1380static int 1381g_part_ctl_undo(struct gctl_req *req, struct g_part_parms *gpp) 1382{ 1383 struct g_consumer *cp; 1384 struct g_provider *pp; 1385 struct g_geom *gp; 1386 struct g_part_entry *entry, *tmp; 1387 struct g_part_table *table; 1388 int error, reprobe; 1389 1390 gp = gpp->gpp_geom; 1391 G_PART_TRACE((G_T_TOPOLOGY, "%s(%s)", __func__, gp->name)); 1392 g_topology_assert(); 1393 1394 table = gp->softc; 1395 if (!table->gpt_opened) { 1396 gctl_error(req, "%d", EPERM); 1397 return (EPERM); 1398 } 1399 1400 cp = LIST_FIRST(&gp->consumer); 1401 LIST_FOREACH_SAFE(entry, &table->gpt_entry, gpe_entry, tmp) { 1402 entry->gpe_modified = 0; 1403 if (entry->gpe_created) { 1404 pp = entry->gpe_pp; 1405 if (pp != NULL) { 1406 pp->private = NULL; 1407 entry->gpe_pp = NULL; 1408 g_wither_provider(pp, ENXIO); 1409 } 1410 entry->gpe_deleted = 1; 1411 } 1412 if (entry->gpe_deleted) { 1413 LIST_REMOVE(entry, gpe_entry); 1414 g_free(entry); 1415 } 1416 } 1417 1418 g_topology_unlock(); 1419 1420 reprobe = (table->gpt_scheme == &g_part_null_scheme || 1421 table->gpt_created) ? 1 : 0; 1422 1423 if (reprobe) { 1424 LIST_FOREACH(entry, &table->gpt_entry, gpe_entry) { 1425 if (entry->gpe_internal) 1426 continue; 1427 error = EBUSY; 1428 goto fail; 1429 } 1430 while ((entry = LIST_FIRST(&table->gpt_entry)) != NULL) { 1431 LIST_REMOVE(entry, gpe_entry); 1432 g_free(entry); 1433 } 1434 error = g_part_probe(gp, cp, table->gpt_depth); 1435 if (error) { 1436 g_topology_lock(); 1437 g_access(cp, -1, -1, -1); 1438 g_part_wither(gp, error); 1439 return (0); 1440 } 1441 table = gp->softc; 1442 1443 /* 1444 * Synthesize a disk geometry. Some partitioning schemes 1445 * depend on it and since some file systems need it even 1446 * when the partitition scheme doesn't, we do it here in 1447 * scheme-independent code. 1448 */ 1449 pp = cp->provider; 1450 g_part_geometry(table, cp, pp->mediasize / pp->sectorsize); 1451 } 1452 1453 error = G_PART_READ(table, cp); 1454 if (error) 1455 goto fail; 1456 error = g_part_check_integrity(table, cp); 1457 if (error) 1458 goto fail; 1459 1460 g_topology_lock(); 1461 LIST_FOREACH(entry, &table->gpt_entry, gpe_entry) { 1462 if (!entry->gpe_internal) 1463 g_part_new_provider(gp, table, entry); 1464 } 1465 1466 table->gpt_opened = 0; 1467 g_access(cp, -1, -1, -1); 1468 return (0); 1469 1470fail: 1471 g_topology_lock(); 1472 gctl_error(req, "%d", error); 1473 return (error); 1474} 1475 1476static void 1477g_part_wither(struct g_geom *gp, int error) 1478{ 1479 struct g_part_entry *entry; 1480 struct g_part_table *table; 1481 1482 table = gp->softc; 1483 if (table != NULL) { 1484 G_PART_DESTROY(table, NULL); 1485 while ((entry = LIST_FIRST(&table->gpt_entry)) != NULL) { 1486 LIST_REMOVE(entry, gpe_entry); 1487 g_free(entry); 1488 } 1489 if (gp->softc != NULL) { 1490 kobj_delete((kobj_t)gp->softc, M_GEOM); 1491 gp->softc = NULL; 1492 } 1493 } 1494 g_wither_geom(gp, error); 1495} 1496 1497/* 1498 * Class methods. 1499 */ 1500 1501static void 1502g_part_ctlreq(struct gctl_req *req, struct g_class *mp, const char *verb) 1503{ 1504 struct g_part_parms gpp; 1505 struct g_part_table *table; 1506 struct gctl_req_arg *ap; 1507 enum g_part_ctl ctlreq; 1508 unsigned int i, mparms, oparms, parm; 1509 int auto_commit, close_on_error; 1510 int error, modifies; 1511 1512 G_PART_TRACE((G_T_TOPOLOGY, "%s(%s,%s)", __func__, mp->name, verb)); 1513 g_topology_assert(); 1514 1515 ctlreq = G_PART_CTL_NONE; 1516 modifies = 1; 1517 mparms = 0; 1518 oparms = G_PART_PARM_FLAGS | G_PART_PARM_OUTPUT | G_PART_PARM_VERSION; 1519 switch (*verb) { 1520 case 'a': 1521 if (!strcmp(verb, "add")) { 1522 ctlreq = G_PART_CTL_ADD; 1523 mparms |= G_PART_PARM_GEOM | G_PART_PARM_SIZE | 1524 G_PART_PARM_START | G_PART_PARM_TYPE; 1525 oparms |= G_PART_PARM_INDEX | G_PART_PARM_LABEL; 1526 } 1527 break; 1528 case 'b': 1529 if (!strcmp(verb, "bootcode")) { 1530 ctlreq = G_PART_CTL_BOOTCODE; 1531 mparms |= G_PART_PARM_GEOM | G_PART_PARM_BOOTCODE; 1532 } 1533 break; 1534 case 'c': 1535 if (!strcmp(verb, "commit")) { 1536 ctlreq = G_PART_CTL_COMMIT; 1537 mparms |= G_PART_PARM_GEOM; 1538 modifies = 0; 1539 } else if (!strcmp(verb, "create")) { 1540 ctlreq = G_PART_CTL_CREATE; 1541 mparms |= G_PART_PARM_PROVIDER | G_PART_PARM_SCHEME; 1542 oparms |= G_PART_PARM_ENTRIES; 1543 } 1544 break; 1545 case 'd': 1546 if (!strcmp(verb, "delete")) { 1547 ctlreq = G_PART_CTL_DELETE; 1548 mparms |= G_PART_PARM_GEOM | G_PART_PARM_INDEX; 1549 } else if (!strcmp(verb, "destroy")) { 1550 ctlreq = G_PART_CTL_DESTROY; 1551 mparms |= G_PART_PARM_GEOM; 1552 oparms |= G_PART_PARM_FORCE; 1553 } 1554 break; 1555 case 'm': 1556 if (!strcmp(verb, "modify")) { 1557 ctlreq = G_PART_CTL_MODIFY; 1558 mparms |= G_PART_PARM_GEOM | G_PART_PARM_INDEX; 1559 oparms |= G_PART_PARM_LABEL | G_PART_PARM_TYPE; 1560 } else if (!strcmp(verb, "move")) { 1561 ctlreq = G_PART_CTL_MOVE; 1562 mparms |= G_PART_PARM_GEOM | G_PART_PARM_INDEX; 1563 } 1564 break; 1565 case 'r': 1566 if (!strcmp(verb, "recover")) { 1567 ctlreq = G_PART_CTL_RECOVER; 1568 mparms |= G_PART_PARM_GEOM; 1569 } else if (!strcmp(verb, "resize")) { 1570 ctlreq = G_PART_CTL_RESIZE; 1571 mparms |= G_PART_PARM_GEOM | G_PART_PARM_INDEX | 1572 G_PART_PARM_SIZE; 1573 } 1574 break; 1575 case 's': 1576 if (!strcmp(verb, "set")) { 1577 ctlreq = G_PART_CTL_SET; 1578 mparms |= G_PART_PARM_ATTRIB | G_PART_PARM_GEOM | 1579 G_PART_PARM_INDEX; 1580 } 1581 break; 1582 case 'u': 1583 if (!strcmp(verb, "undo")) { 1584 ctlreq = G_PART_CTL_UNDO; 1585 mparms |= G_PART_PARM_GEOM; 1586 modifies = 0; 1587 } else if (!strcmp(verb, "unset")) { 1588 ctlreq = G_PART_CTL_UNSET; 1589 mparms |= G_PART_PARM_ATTRIB | G_PART_PARM_GEOM | 1590 G_PART_PARM_INDEX; 1591 } 1592 break; 1593 } 1594 if (ctlreq == G_PART_CTL_NONE) { 1595 gctl_error(req, "%d verb '%s'", EINVAL, verb); 1596 return; 1597 } 1598 1599 bzero(&gpp, sizeof(gpp)); 1600 for (i = 0; i < req->narg; i++) { 1601 ap = &req->arg[i]; 1602 parm = 0; 1603 switch (ap->name[0]) { 1604 case 'a': 1605 if (!strcmp(ap->name, "arg0")) { 1606 parm = mparms & 1607 (G_PART_PARM_GEOM | G_PART_PARM_PROVIDER); 1608 } 1609 if (!strcmp(ap->name, "attrib")) 1610 parm = G_PART_PARM_ATTRIB; 1611 break; 1612 case 'b': 1613 if (!strcmp(ap->name, "bootcode")) 1614 parm = G_PART_PARM_BOOTCODE; 1615 break; 1616 case 'c': 1617 if (!strcmp(ap->name, "class")) 1618 continue; 1619 break; 1620 case 'e': 1621 if (!strcmp(ap->name, "entries")) 1622 parm = G_PART_PARM_ENTRIES; 1623 break; 1624 case 'f': 1625 if (!strcmp(ap->name, "flags")) 1626 parm = G_PART_PARM_FLAGS; 1627 else if (!strcmp(ap->name, "force")) 1628 parm = G_PART_PARM_FORCE; 1629 break; 1630 case 'i': 1631 if (!strcmp(ap->name, "index")) 1632 parm = G_PART_PARM_INDEX; 1633 break; 1634 case 'l': 1635 if (!strcmp(ap->name, "label")) 1636 parm = G_PART_PARM_LABEL; 1637 break; 1638 case 'o': 1639 if (!strcmp(ap->name, "output")) 1640 parm = G_PART_PARM_OUTPUT; 1641 break; 1642 case 's': 1643 if (!strcmp(ap->name, "scheme")) 1644 parm = G_PART_PARM_SCHEME; 1645 else if (!strcmp(ap->name, "size")) 1646 parm = G_PART_PARM_SIZE; 1647 else if (!strcmp(ap->name, "start")) 1648 parm = G_PART_PARM_START; 1649 break; 1650 case 't': 1651 if (!strcmp(ap->name, "type")) 1652 parm = G_PART_PARM_TYPE; 1653 break; 1654 case 'v': 1655 if (!strcmp(ap->name, "verb")) 1656 continue; 1657 else if (!strcmp(ap->name, "version")) 1658 parm = G_PART_PARM_VERSION; 1659 break; 1660 } 1661 if ((parm & (mparms | oparms)) == 0) { 1662 gctl_error(req, "%d param '%s'", EINVAL, ap->name); 1663 return; 1664 } 1665 switch (parm) { 1666 case G_PART_PARM_ATTRIB: 1667 error = g_part_parm_str(req, ap->name, 1668 &gpp.gpp_attrib); 1669 break; 1670 case G_PART_PARM_BOOTCODE: 1671 error = g_part_parm_bootcode(req, ap->name, 1672 &gpp.gpp_codeptr, &gpp.gpp_codesize); 1673 break; 1674 case G_PART_PARM_ENTRIES: 1675 error = g_part_parm_intmax(req, ap->name, 1676 &gpp.gpp_entries); 1677 break; 1678 case G_PART_PARM_FLAGS: 1679 error = g_part_parm_str(req, ap->name, &gpp.gpp_flags); 1680 break; 1681 case G_PART_PARM_FORCE: 1682 error = g_part_parm_uint32(req, ap->name, 1683 &gpp.gpp_force); 1684 break; 1685 case G_PART_PARM_GEOM: 1686 error = g_part_parm_geom(req, ap->name, &gpp.gpp_geom); 1687 break; 1688 case G_PART_PARM_INDEX: 1689 error = g_part_parm_intmax(req, ap->name, 1690 &gpp.gpp_index); 1691 break; 1692 case G_PART_PARM_LABEL: 1693 error = g_part_parm_str(req, ap->name, &gpp.gpp_label); 1694 break; 1695 case G_PART_PARM_OUTPUT: 1696 error = 0; /* Write-only parameter */ 1697 break; 1698 case G_PART_PARM_PROVIDER: 1699 error = g_part_parm_provider(req, ap->name, 1700 &gpp.gpp_provider); 1701 break; 1702 case G_PART_PARM_SCHEME: 1703 error = g_part_parm_scheme(req, ap->name, 1704 &gpp.gpp_scheme); 1705 break; 1706 case G_PART_PARM_SIZE: 1707 error = g_part_parm_quad(req, ap->name, &gpp.gpp_size); 1708 break; 1709 case G_PART_PARM_START: 1710 error = g_part_parm_quad(req, ap->name, 1711 &gpp.gpp_start); 1712 break; 1713 case G_PART_PARM_TYPE: 1714 error = g_part_parm_str(req, ap->name, &gpp.gpp_type); 1715 break; 1716 case G_PART_PARM_VERSION: 1717 error = g_part_parm_uint32(req, ap->name, 1718 &gpp.gpp_version); 1719 break; 1720 default: 1721 error = EDOOFUS; 1722 gctl_error(req, "%d %s", error, ap->name); 1723 break; 1724 } 1725 if (error != 0) { 1726 if (error == ENOATTR) { 1727 gctl_error(req, "%d param '%s'", error, 1728 ap->name); 1729 } 1730 return; 1731 } 1732 gpp.gpp_parms |= parm; 1733 } 1734 if ((gpp.gpp_parms & mparms) != mparms) { 1735 parm = mparms - (gpp.gpp_parms & mparms); 1736 gctl_error(req, "%d param '%x'", ENOATTR, parm); 1737 return; 1738 } 1739 1740 /* Obtain permissions if possible/necessary. */ 1741 close_on_error = 0; 1742 table = NULL; 1743 if (modifies && (gpp.gpp_parms & G_PART_PARM_GEOM)) { 1744 table = gpp.gpp_geom->softc; 1745 if (table != NULL && table->gpt_corrupt && 1746 ctlreq != G_PART_CTL_DESTROY && 1747 ctlreq != G_PART_CTL_RECOVER) { 1748 gctl_error(req, "%d table '%s' is corrupt", 1749 EPERM, gpp.gpp_geom->name); 1750 return; 1751 } 1752 if (table != NULL && !table->gpt_opened) { 1753 error = g_access(LIST_FIRST(&gpp.gpp_geom->consumer), 1754 1, 1, 1); 1755 if (error) { 1756 gctl_error(req, "%d geom '%s'", error, 1757 gpp.gpp_geom->name); 1758 return; 1759 } 1760 table->gpt_opened = 1; 1761 close_on_error = 1; 1762 } 1763 } 1764 1765 /* Allow the scheme to check or modify the parameters. */ 1766 if (table != NULL) { 1767 error = G_PART_PRECHECK(table, ctlreq, &gpp); 1768 if (error) { 1769 gctl_error(req, "%d pre-check failed", error); 1770 goto out; 1771 } 1772 } else 1773 error = EDOOFUS; /* Prevent bogus uninit. warning. */ 1774 1775 switch (ctlreq) { 1776 case G_PART_CTL_NONE: 1777 panic("%s", __func__); 1778 case G_PART_CTL_ADD: 1779 error = g_part_ctl_add(req, &gpp); 1780 break; 1781 case G_PART_CTL_BOOTCODE: 1782 error = g_part_ctl_bootcode(req, &gpp); 1783 break; 1784 case G_PART_CTL_COMMIT: 1785 error = g_part_ctl_commit(req, &gpp); 1786 break; 1787 case G_PART_CTL_CREATE: 1788 error = g_part_ctl_create(req, &gpp); 1789 break; 1790 case G_PART_CTL_DELETE: 1791 error = g_part_ctl_delete(req, &gpp); 1792 break; 1793 case G_PART_CTL_DESTROY: 1794 error = g_part_ctl_destroy(req, &gpp); 1795 break; 1796 case G_PART_CTL_MODIFY: 1797 error = g_part_ctl_modify(req, &gpp); 1798 break; 1799 case G_PART_CTL_MOVE: 1800 error = g_part_ctl_move(req, &gpp); 1801 break; 1802 case G_PART_CTL_RECOVER: 1803 error = g_part_ctl_recover(req, &gpp); 1804 break; 1805 case G_PART_CTL_RESIZE: 1806 error = g_part_ctl_resize(req, &gpp); 1807 break; 1808 case G_PART_CTL_SET: 1809 error = g_part_ctl_setunset(req, &gpp, 1); 1810 break; 1811 case G_PART_CTL_UNDO: 1812 error = g_part_ctl_undo(req, &gpp); 1813 break; 1814 case G_PART_CTL_UNSET: 1815 error = g_part_ctl_setunset(req, &gpp, 0); 1816 break; 1817 } 1818 1819 /* Implement automatic commit. */ 1820 if (!error) { 1821 auto_commit = (modifies && 1822 (gpp.gpp_parms & G_PART_PARM_FLAGS) && 1823 strchr(gpp.gpp_flags, 'C') != NULL) ? 1 : 0; 1824 if (auto_commit) { 1825 KASSERT(gpp.gpp_parms & G_PART_PARM_GEOM, ("%s", 1826 __func__)); 1827 error = g_part_ctl_commit(req, &gpp); 1828 } 1829 } 1830 1831 out: 1832 if (error && close_on_error) { 1833 g_access(LIST_FIRST(&gpp.gpp_geom->consumer), -1, -1, -1); 1834 table->gpt_opened = 0; 1835 } 1836} 1837 1838static int 1839g_part_destroy_geom(struct gctl_req *req, struct g_class *mp, 1840 struct g_geom *gp) 1841{ 1842 1843 G_PART_TRACE((G_T_TOPOLOGY, "%s(%s,%s)", __func__, mp->name, gp->name)); 1844 g_topology_assert(); 1845 1846 g_part_wither(gp, EINVAL); 1847 return (0); 1848} 1849 1850static struct g_geom * 1851g_part_taste(struct g_class *mp, struct g_provider *pp, int flags __unused) 1852{ 1853 struct g_consumer *cp; 1854 struct g_geom *gp; 1855 struct g_part_entry *entry; 1856 struct g_part_table *table; 1857 struct root_hold_token *rht; 1858 int attr, depth; 1859 int error; 1860 1861 G_PART_TRACE((G_T_TOPOLOGY, "%s(%s,%s)", __func__, mp->name, pp->name)); 1862 g_topology_assert(); 1863 1864 /* Skip providers that are already open for writing. */ 1865 if (pp->acw > 0) 1866 return (NULL); 1867 1868 /* 1869 * Create a GEOM with consumer and hook it up to the provider. 1870 * With that we become part of the topology. Optain read access 1871 * to the provider. 1872 */ 1873 gp = g_new_geomf(mp, "%s", pp->name); 1874 cp = g_new_consumer(gp); 1875 error = g_attach(cp, pp); 1876 if (error == 0) 1877 error = g_access(cp, 1, 0, 0); 1878 if (error != 0) { 1879 g_part_wither(gp, error); 1880 return (NULL); 1881 } 1882 1883 rht = root_mount_hold(mp->name); 1884 g_topology_unlock(); 1885 1886 /* 1887 * Short-circuit the whole probing galore when there's no 1888 * media present. 1889 */ 1890 if (pp->mediasize == 0 || pp->sectorsize == 0) { 1891 error = ENODEV; 1892 goto fail; 1893 } 1894 1895 /* Make sure we can nest and if so, determine our depth. */ 1896 error = g_getattr("PART::isleaf", cp, &attr); 1897 if (!error && attr) { 1898 error = ENODEV; 1899 goto fail; 1900 } 1901 error = g_getattr("PART::depth", cp, &attr); 1902 depth = (!error) ? attr + 1 : 0; 1903 1904 error = g_part_probe(gp, cp, depth); 1905 if (error) 1906 goto fail; 1907 1908 table = gp->softc; 1909 1910 /* 1911 * Synthesize a disk geometry. Some partitioning schemes 1912 * depend on it and since some file systems need it even 1913 * when the partitition scheme doesn't, we do it here in 1914 * scheme-independent code. 1915 */ 1916 g_part_geometry(table, cp, pp->mediasize / pp->sectorsize); 1917 1918 error = G_PART_READ(table, cp); 1919 if (error) 1920 goto fail; 1921 error = g_part_check_integrity(table, cp); 1922 if (error) 1923 goto fail; 1924 1925 g_topology_lock(); 1926 LIST_FOREACH(entry, &table->gpt_entry, gpe_entry) { 1927 if (!entry->gpe_internal) 1928 g_part_new_provider(gp, table, entry); 1929 } 1930 1931 root_mount_rel(rht); 1932 g_access(cp, -1, 0, 0); 1933 return (gp); 1934 1935 fail: 1936 g_topology_lock(); 1937 root_mount_rel(rht); 1938 g_access(cp, -1, 0, 0); 1939 g_part_wither(gp, error); 1940 return (NULL); 1941} 1942 1943/* 1944 * Geom methods. 1945 */ 1946 1947static int 1948g_part_access(struct g_provider *pp, int dr, int dw, int de) 1949{ 1950 struct g_consumer *cp; 1951 1952 G_PART_TRACE((G_T_ACCESS, "%s(%s,%d,%d,%d)", __func__, pp->name, dr, 1953 dw, de)); 1954 1955 cp = LIST_FIRST(&pp->geom->consumer); 1956 1957 /* We always gain write-exclusive access. */ 1958 return (g_access(cp, dr, dw, dw + de)); 1959} 1960 1961static void 1962g_part_dumpconf(struct sbuf *sb, const char *indent, struct g_geom *gp, 1963 struct g_consumer *cp, struct g_provider *pp) 1964{ 1965 char buf[64]; 1966 struct g_part_entry *entry; 1967 struct g_part_table *table; 1968 1969 KASSERT(sb != NULL && gp != NULL, ("%s", __func__)); 1970 table = gp->softc; 1971 1972 if (indent == NULL) { 1973 KASSERT(cp == NULL && pp != NULL, ("%s", __func__)); 1974 entry = pp->private; 1975 if (entry == NULL) 1976 return; 1977 sbuf_printf(sb, " i %u o %ju ty %s", entry->gpe_index, 1978 (uintmax_t)entry->gpe_offset, 1979 G_PART_TYPE(table, entry, buf, sizeof(buf))); 1980 /* 1981 * libdisk compatibility quirk - the scheme dumps the 1982 * slicer name and partition type in a way that is 1983 * compatible with libdisk. When libdisk is not used 1984 * anymore, this should go away. 1985 */ 1986 G_PART_DUMPCONF(table, entry, sb, indent); 1987 } else if (cp != NULL) { /* Consumer configuration. */ 1988 KASSERT(pp == NULL, ("%s", __func__)); 1989 /* none */ 1990 } else if (pp != NULL) { /* Provider configuration. */ 1991 entry = pp->private; 1992 if (entry == NULL) 1993 return; 1994 sbuf_printf(sb, "%s<start>%ju</start>\n", indent, 1995 (uintmax_t)entry->gpe_start); 1996 sbuf_printf(sb, "%s<end>%ju</end>\n", indent, 1997 (uintmax_t)entry->gpe_end); 1998 sbuf_printf(sb, "%s<index>%u</index>\n", indent, 1999 entry->gpe_index); 2000 sbuf_printf(sb, "%s<type>%s</type>\n", indent, 2001 G_PART_TYPE(table, entry, buf, sizeof(buf))); 2002 sbuf_printf(sb, "%s<offset>%ju</offset>\n", indent, 2003 (uintmax_t)entry->gpe_offset); 2004 sbuf_printf(sb, "%s<length>%ju</length>\n", indent, 2005 (uintmax_t)pp->mediasize); 2006 G_PART_DUMPCONF(table, entry, sb, indent); 2007 } else { /* Geom configuration. */ 2008 sbuf_printf(sb, "%s<scheme>%s</scheme>\n", indent, 2009 table->gpt_scheme->name); 2010 sbuf_printf(sb, "%s<entries>%u</entries>\n", indent, 2011 table->gpt_entries); 2012 sbuf_printf(sb, "%s<first>%ju</first>\n", indent, 2013 (uintmax_t)table->gpt_first); 2014 sbuf_printf(sb, "%s<last>%ju</last>\n", indent, 2015 (uintmax_t)table->gpt_last); 2016 sbuf_printf(sb, "%s<fwsectors>%u</fwsectors>\n", indent, 2017 table->gpt_sectors); 2018 sbuf_printf(sb, "%s<fwheads>%u</fwheads>\n", indent, 2019 table->gpt_heads); 2020 sbuf_printf(sb, "%s<state>%s</state>\n", indent, 2021 table->gpt_corrupt ? "CORRUPT": "OK"); 2022 sbuf_printf(sb, "%s<modified>%s</modified>\n", indent, 2023 table->gpt_opened ? "true": "false"); 2024 G_PART_DUMPCONF(table, NULL, sb, indent); 2025 } 2026} 2027 2028static void 2029g_part_orphan(struct g_consumer *cp) 2030{ 2031 struct g_provider *pp; 2032 struct g_part_table *table; 2033 2034 pp = cp->provider; 2035 KASSERT(pp != NULL, ("%s", __func__)); 2036 G_PART_TRACE((G_T_TOPOLOGY, "%s(%s)", __func__, pp->name)); 2037 g_topology_assert(); 2038 2039 KASSERT(pp->error != 0, ("%s", __func__)); 2040 table = cp->geom->softc; 2041 if (table != NULL && table->gpt_opened) 2042 g_access(cp, -1, -1, -1); 2043 g_part_wither(cp->geom, pp->error); 2044} 2045 2046static void 2047g_part_spoiled(struct g_consumer *cp) 2048{ 2049 2050 G_PART_TRACE((G_T_TOPOLOGY, "%s(%s)", __func__, cp->provider->name)); 2051 g_topology_assert(); 2052 2053 g_part_wither(cp->geom, ENXIO); 2054} 2055 2056static void 2057g_part_start(struct bio *bp) 2058{ 2059 struct bio *bp2; 2060 struct g_consumer *cp; 2061 struct g_geom *gp; 2062 struct g_part_entry *entry; 2063 struct g_part_table *table; 2064 struct g_kerneldump *gkd; 2065 struct g_provider *pp; 2066 char buf[64]; 2067 2068 pp = bp->bio_to; 2069 gp = pp->geom; 2070 table = gp->softc; 2071 cp = LIST_FIRST(&gp->consumer); 2072 2073 G_PART_TRACE((G_T_BIO, "%s: cmd=%d, provider=%s", __func__, bp->bio_cmd, 2074 pp->name)); 2075 2076 entry = pp->private; 2077 if (entry == NULL) { 2078 g_io_deliver(bp, ENXIO); 2079 return; 2080 } 2081 2082 switch(bp->bio_cmd) { 2083 case BIO_DELETE: 2084 case BIO_READ: 2085 case BIO_WRITE: 2086 if (bp->bio_offset >= pp->mediasize) { 2087 g_io_deliver(bp, EIO); 2088 return; 2089 } 2090 bp2 = g_clone_bio(bp); 2091 if (bp2 == NULL) { 2092 g_io_deliver(bp, ENOMEM); 2093 return; 2094 } 2095 if (bp2->bio_offset + bp2->bio_length > pp->mediasize) 2096 bp2->bio_length = pp->mediasize - bp2->bio_offset; 2097 bp2->bio_done = g_std_done; 2098 bp2->bio_offset += entry->gpe_offset; 2099 g_io_request(bp2, cp); 2100 return; 2101 case BIO_FLUSH: 2102 break; 2103 case BIO_GETATTR: 2104 if (g_handleattr_int(bp, "GEOM::fwheads", table->gpt_heads)) 2105 return; 2106 if (g_handleattr_int(bp, "GEOM::fwsectors", table->gpt_sectors)) 2107 return; 2108 if (g_handleattr_int(bp, "PART::isleaf", table->gpt_isleaf)) 2109 return; 2110 if (g_handleattr_int(bp, "PART::depth", table->gpt_depth)) 2111 return; 2112 if (g_handleattr_str(bp, "PART::scheme", 2113 table->gpt_scheme->name)) 2114 return; 2115 if (g_handleattr_str(bp, "PART::type", 2116 G_PART_TYPE(table, entry, buf, sizeof(buf)))) 2117 return; 2118 if (!strcmp("GEOM::kerneldump", bp->bio_attribute)) { 2119 /* 2120 * Check that the partition is suitable for kernel 2121 * dumps. Typically only swap partitions should be 2122 * used. If the request comes from the nested scheme 2123 * we allow dumping there as well. 2124 */ 2125 if ((bp->bio_from == NULL || 2126 bp->bio_from->geom->class != &g_part_class) && 2127 G_PART_DUMPTO(table, entry) == 0) { 2128 g_io_deliver(bp, ENODEV); 2129 printf("GEOM_PART: Partition '%s' not suitable" 2130 " for kernel dumps (wrong type?)\n", 2131 pp->name); 2132 return; 2133 } 2134 gkd = (struct g_kerneldump *)bp->bio_data; 2135 if (gkd->offset >= pp->mediasize) { 2136 g_io_deliver(bp, EIO); 2137 return; 2138 } 2139 if (gkd->offset + gkd->length > pp->mediasize) 2140 gkd->length = pp->mediasize - gkd->offset; 2141 gkd->offset += entry->gpe_offset; 2142 } 2143 break; 2144 default: 2145 g_io_deliver(bp, EOPNOTSUPP); 2146 return; 2147 } 2148 2149 bp2 = g_clone_bio(bp); 2150 if (bp2 == NULL) { 2151 g_io_deliver(bp, ENOMEM); 2152 return; 2153 } 2154 bp2->bio_done = g_std_done; 2155 g_io_request(bp2, cp); 2156} 2157 2158static void 2159g_part_init(struct g_class *mp) 2160{ 2161 2162 TAILQ_INSERT_HEAD(&g_part_schemes, &g_part_null_scheme, scheme_list); 2163} 2164 2165static void 2166g_part_fini(struct g_class *mp) 2167{ 2168 2169 TAILQ_REMOVE(&g_part_schemes, &g_part_null_scheme, scheme_list); 2170} 2171 2172static void 2173g_part_unload_event(void *arg, int flag) 2174{ 2175 struct g_consumer *cp; 2176 struct g_geom *gp; 2177 struct g_provider *pp; 2178 struct g_part_scheme *scheme; 2179 struct g_part_table *table; 2180 uintptr_t *xchg; 2181 int acc, error; 2182 2183 if (flag == EV_CANCEL) 2184 return; 2185 2186 xchg = arg; 2187 error = 0; 2188 scheme = (void *)(*xchg); 2189 2190 g_topology_assert(); 2191 2192 LIST_FOREACH(gp, &g_part_class.geom, geom) { 2193 table = gp->softc; 2194 if (table->gpt_scheme != scheme) 2195 continue; 2196 2197 acc = 0; 2198 LIST_FOREACH(pp, &gp->provider, provider) 2199 acc += pp->acr + pp->acw + pp->ace; 2200 LIST_FOREACH(cp, &gp->consumer, consumer) 2201 acc += cp->acr + cp->acw + cp->ace; 2202 2203 if (!acc) 2204 g_part_wither(gp, ENOSYS); 2205 else 2206 error = EBUSY; 2207 } 2208 2209 if (!error) 2210 TAILQ_REMOVE(&g_part_schemes, scheme, scheme_list); 2211 2212 *xchg = error; 2213} 2214 2215int 2216g_part_modevent(module_t mod, int type, struct g_part_scheme *scheme) 2217{ 2218 struct g_part_scheme *iter; 2219 uintptr_t arg; 2220 int error; 2221 2222 error = 0; 2223 switch (type) { 2224 case MOD_LOAD: 2225 TAILQ_FOREACH(iter, &g_part_schemes, scheme_list) { 2226 if (scheme == iter) { 2227 printf("GEOM_PART: scheme %s is already " 2228 "registered!\n", scheme->name); 2229 break; 2230 } 2231 } 2232 if (iter == NULL) { 2233 TAILQ_INSERT_TAIL(&g_part_schemes, scheme, 2234 scheme_list); 2235 g_retaste(&g_part_class); 2236 } 2237 break; 2238 case MOD_UNLOAD: 2239 arg = (uintptr_t)scheme; 2240 error = g_waitfor_event(g_part_unload_event, &arg, M_WAITOK, 2241 NULL); 2242 if (error == 0) 2243 error = arg; 2244 break; 2245 default: 2246 error = EOPNOTSUPP; 2247 break; 2248 } 2249 2250 return (error); 2251} 2252