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