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