1 /*- 2 * SPDX-License-Identifier: BSD-2-Clause 3 * 4 * Copyright (c) 2022 Marshall Kirk McKusick <mckusick@mckusick.com> 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 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 AUTHORS AND CONTRIBUTORS ``AS IS'' AND 16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE 19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 25 * SUCH DAMAGE. 26 */ 27 28 #include <sys/param.h> 29 #include <sys/bio.h> 30 #include <sys/buf.h> 31 #include <sys/ctype.h> 32 #include <sys/kernel.h> 33 #include <sys/lock.h> 34 #include <sys/malloc.h> 35 #include <sys/module.h> 36 #include <sys/reboot.h> 37 #include <sys/rwlock.h> 38 #include <sys/sbuf.h> 39 #include <sys/sysctl.h> 40 41 #include <geom/geom.h> 42 #include <geom/geom_dbg.h> 43 #include <geom/union/g_union.h> 44 45 SYSCTL_DECL(_kern_geom); 46 static SYSCTL_NODE(_kern_geom, OID_AUTO, union, CTLFLAG_RW | CTLFLAG_MPSAFE, 0, 47 "GEOM_UNION stuff"); 48 static u_int g_union_debug = 0; 49 SYSCTL_UINT(_kern_geom_union, OID_AUTO, debug, CTLFLAG_RW, &g_union_debug, 0, 50 "Debug level"); 51 52 static void g_union_config(struct gctl_req *req, struct g_class *mp, 53 const char *verb); 54 static g_access_t g_union_access; 55 static g_start_t g_union_start; 56 static g_dumpconf_t g_union_dumpconf; 57 static g_orphan_t g_union_orphan; 58 static int g_union_destroy_geom(struct gctl_req *req, struct g_class *mp, 59 struct g_geom *gp); 60 static g_provgone_t g_union_providergone; 61 static g_resize_t g_union_resize; 62 63 struct g_class g_union_class = { 64 .name = G_UNION_CLASS_NAME, 65 .version = G_VERSION, 66 .ctlreq = g_union_config, 67 .access = g_union_access, 68 .start = g_union_start, 69 .dumpconf = g_union_dumpconf, 70 .orphan = g_union_orphan, 71 .destroy_geom = g_union_destroy_geom, 72 .providergone = g_union_providergone, 73 .resize = g_union_resize, 74 }; 75 76 static void g_union_ctl_create(struct gctl_req *req, struct g_class *mp, bool); 77 static intmax_t g_union_fetcharg(struct gctl_req *req, const char *name); 78 static bool g_union_verify_nprefix(const char *name); 79 static void g_union_ctl_destroy(struct gctl_req *req, struct g_class *mp, bool); 80 static struct g_geom *g_union_find_geom(struct g_class *mp, const char *name); 81 static void g_union_ctl_reset(struct gctl_req *req, struct g_class *mp, bool); 82 static void g_union_ctl_revert(struct gctl_req *req, struct g_class *mp, bool); 83 static void g_union_revert(struct g_union_softc *sc); 84 static void g_union_doio(struct g_union_wip *wip); 85 static void g_union_ctl_commit(struct gctl_req *req, struct g_class *mp, bool); 86 static void g_union_setmap(struct bio *bp, struct g_union_softc *sc); 87 static bool g_union_getmap(struct bio *bp, struct g_union_softc *sc, 88 off_t *len2read); 89 static void g_union_done(struct bio *bp); 90 static void g_union_kerneldump(struct bio *bp, struct g_union_softc *sc); 91 static int g_union_dumper(void *, void *, off_t, size_t); 92 static int g_union_destroy(struct gctl_req *req, struct g_geom *gp, bool force); 93 94 /* 95 * Operate on union-specific configuration commands. 96 */ 97 static void 98 g_union_config(struct gctl_req *req, struct g_class *mp, const char *verb) 99 { 100 uint32_t *version, *verbose; 101 102 g_topology_assert(); 103 104 version = gctl_get_paraml(req, "version", sizeof(*version)); 105 if (version == NULL) { 106 gctl_error(req, "No '%s' argument.", "version"); 107 return; 108 } 109 if (*version != G_UNION_VERSION) { 110 gctl_error(req, "Userland and kernel parts are out of sync."); 111 return; 112 } 113 verbose = gctl_get_paraml(req, "verbose", sizeof(*verbose)); 114 if (verbose == NULL) { 115 gctl_error(req, "No '%s' argument.", "verbose"); 116 return; 117 } 118 if (strcmp(verb, "create") == 0) { 119 g_union_ctl_create(req, mp, *verbose); 120 return; 121 } else if (strcmp(verb, "destroy") == 0) { 122 g_union_ctl_destroy(req, mp, *verbose); 123 return; 124 } else if (strcmp(verb, "reset") == 0) { 125 g_union_ctl_reset(req, mp, *verbose); 126 return; 127 } else if (strcmp(verb, "revert") == 0) { 128 g_union_ctl_revert(req, mp, *verbose); 129 return; 130 } else if (strcmp(verb, "commit") == 0) { 131 g_union_ctl_commit(req, mp, *verbose); 132 return; 133 } 134 135 gctl_error(req, "Unknown verb."); 136 } 137 138 /* 139 * Create a union device. 140 */ 141 static void 142 g_union_ctl_create(struct gctl_req *req, struct g_class *mp, bool verbose) 143 { 144 struct g_provider *upperpp, *lowerpp, *newpp; 145 struct g_consumer *uppercp, *lowercp; 146 struct g_union_softc *sc; 147 struct g_geom_alias *gap; 148 struct g_geom *gp; 149 intmax_t offset, secsize, size, needed; 150 const char *gunionname; 151 int *nargs, error, i, n; 152 char name[64]; 153 154 g_topology_assert(); 155 156 nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs)); 157 if (nargs == NULL) { 158 gctl_error(req, "No '%s' argument.", "nargs"); 159 return; 160 } 161 if (*nargs < 2) { 162 gctl_error(req, "Missing device(s)."); 163 return; 164 } 165 if (*nargs > 2) { 166 gctl_error(req, "Extra device(s)."); 167 return; 168 } 169 170 offset = g_union_fetcharg(req, "offset"); 171 size = g_union_fetcharg(req, "size"); 172 secsize = g_union_fetcharg(req, "secsize"); 173 gunionname = gctl_get_asciiparam(req, "gunionname"); 174 175 upperpp = gctl_get_provider(req, "arg0"); 176 lowerpp = gctl_get_provider(req, "arg1"); 177 if (upperpp == NULL || lowerpp == NULL) 178 /* error message provided by gctl_get_provider() */ 179 return; 180 /* Create the union */ 181 if (secsize == 0) 182 secsize = lowerpp->sectorsize; 183 else if ((secsize % lowerpp->sectorsize) != 0) { 184 gctl_error(req, "Sector size %jd is not a multiple of lower " 185 "provider %s's %jd sector size.", (intmax_t)secsize, 186 lowerpp->name, (intmax_t)lowerpp->sectorsize); 187 return; 188 } 189 if (secsize > maxphys) { 190 gctl_error(req, "Too big secsize %jd for lower provider %s.", 191 (intmax_t)secsize, lowerpp->name); 192 return; 193 } 194 if (secsize % upperpp->sectorsize != 0) { 195 gctl_error(req, "Sector size %jd is not a multiple of upper " 196 "provider %s's %jd sector size.", (intmax_t)secsize, 197 upperpp->name, (intmax_t)upperpp->sectorsize); 198 return; 199 } 200 if ((offset % secsize) != 0) { 201 gctl_error(req, "Offset %jd is not a multiple of lower " 202 "provider %s's %jd sector size.", (intmax_t)offset, 203 lowerpp->name, (intmax_t)lowerpp->sectorsize); 204 return; 205 } 206 if (size == 0) 207 size = lowerpp->mediasize - offset; 208 else 209 size -= offset; 210 if ((size % secsize) != 0) { 211 gctl_error(req, "Size %jd is not a multiple of sector size " 212 "%jd.", (intmax_t)size, (intmax_t)secsize); 213 return; 214 } 215 if (offset + size < lowerpp->mediasize) { 216 gctl_error(req, "Size %jd is too small for lower provider %s, " 217 "needs %jd.", (intmax_t)(offset + size), lowerpp->name, 218 lowerpp->mediasize); 219 return; 220 } 221 if (size > upperpp->mediasize) { 222 gctl_error(req, "Upper provider %s size (%jd) is too small, " 223 "needs %jd.", upperpp->name, (intmax_t)upperpp->mediasize, 224 (intmax_t)size); 225 return; 226 } 227 if (gunionname != NULL && !g_union_verify_nprefix(gunionname)) { 228 gctl_error(req, "Gunion name %s must be alphanumeric.", 229 gunionname); 230 return; 231 } 232 if (gunionname != NULL) { 233 n = snprintf(name, sizeof(name), "%s%s", gunionname, 234 G_UNION_SUFFIX); 235 } else { 236 n = snprintf(name, sizeof(name), "%s-%s%s", upperpp->name, 237 lowerpp->name, G_UNION_SUFFIX); 238 } 239 if (n <= 0 || n >= sizeof(name)) { 240 gctl_error(req, "Invalid provider name."); 241 return; 242 } 243 LIST_FOREACH(gp, &mp->geom, geom) { 244 if (strcmp(gp->name, name) == 0) { 245 gctl_error(req, "Provider %s already exists.", name); 246 return; 247 } 248 } 249 gp = g_new_geom(mp, name); 250 sc = g_malloc(sizeof(*sc), M_WAITOK | M_ZERO); 251 rw_init(&sc->sc_rwlock, "gunion"); 252 TAILQ_INIT(&sc->sc_wiplist); 253 sc->sc_offset = offset; 254 sc->sc_size = size; 255 sc->sc_sectorsize = secsize; 256 sc->sc_reads = 0; 257 sc->sc_writes = 0; 258 sc->sc_deletes = 0; 259 sc->sc_getattrs = 0; 260 sc->sc_flushes = 0; 261 sc->sc_speedups = 0; 262 sc->sc_cmd0s = 0; 263 sc->sc_cmd1s = 0; 264 sc->sc_cmd2s = 0; 265 sc->sc_readbytes = 0; 266 sc->sc_wrotebytes = 0; 267 sc->sc_writemap_memory = 0; 268 gp->softc = sc; 269 270 newpp = g_new_providerf(gp, "%s", gp->name); 271 newpp->flags |= G_PF_DIRECT_SEND | G_PF_DIRECT_RECEIVE; 272 newpp->mediasize = size; 273 newpp->sectorsize = secsize; 274 LIST_FOREACH(gap, &upperpp->aliases, ga_next) 275 g_provider_add_alias(newpp, "%s%s", gap->ga_alias, 276 G_UNION_SUFFIX); 277 LIST_FOREACH(gap, &lowerpp->aliases, ga_next) 278 g_provider_add_alias(newpp, "%s%s", gap->ga_alias, 279 G_UNION_SUFFIX); 280 lowercp = g_new_consumer(gp); 281 lowercp->flags |= G_CF_DIRECT_SEND | G_CF_DIRECT_RECEIVE; 282 if ((error = g_attach(lowercp, lowerpp)) != 0) { 283 gctl_error(req, "Error %d: cannot attach to provider %s.", 284 error, lowerpp->name); 285 goto fail1; 286 } 287 /* request read and exclusive access for lower */ 288 if ((error = g_access(lowercp, 1, 0, 1)) != 0) { 289 gctl_error(req, "Error %d: cannot obtain exclusive access to " 290 "%s.\n\tMust be unmounted or mounted read-only.", error, 291 lowerpp->name); 292 goto fail2; 293 } 294 uppercp = g_new_consumer(gp); 295 uppercp->flags |= G_CF_DIRECT_SEND | G_CF_DIRECT_RECEIVE; 296 if ((error = g_attach(uppercp, upperpp)) != 0) { 297 gctl_error(req, "Error %d: cannot attach to provider %s.", 298 error, upperpp->name); 299 goto fail3; 300 } 301 /* request read, write, and exclusive access for upper */ 302 if ((error = g_access(uppercp, 1, 1, 1)) != 0) { 303 gctl_error(req, "Error %d: cannot obtain write access to %s.", 304 error, upperpp->name); 305 goto fail4; 306 } 307 sc->sc_uppercp = uppercp; 308 sc->sc_lowercp = lowercp; 309 310 newpp->flags |= (upperpp->flags & G_PF_ACCEPT_UNMAPPED) & 311 (lowerpp->flags & G_PF_ACCEPT_UNMAPPED); 312 g_error_provider(newpp, 0); 313 /* 314 * Allocate the map that tracks the sectors that have been written 315 * to the top layer. We use a 2-level hierarchy as that lets us 316 * map up to 1 petabyte using allocations of less than 33 Mb 317 * when using 4K byte sectors (or 268 Mb with 512 byte sectors). 318 * 319 * We totally populate the leaf nodes rather than allocating them 320 * as they are first used because their usage occurs in the 321 * g_union_start() routine that may be running in the g_down 322 * thread which cannot sleep. 323 */ 324 sc->sc_map_size = roundup(size / secsize, BITS_PER_ENTRY); 325 needed = sc->sc_map_size / BITS_PER_ENTRY; 326 for (sc->sc_root_size = 1; 327 sc->sc_root_size * sc->sc_root_size < needed; 328 sc->sc_root_size++) 329 continue; 330 sc->sc_writemap_root = g_malloc(sc->sc_root_size * sizeof(uint64_t *), 331 M_WAITOK | M_ZERO); 332 sc->sc_leaf_size = sc->sc_root_size; 333 sc->sc_bits_per_leaf = sc->sc_leaf_size * BITS_PER_ENTRY; 334 sc->sc_leafused = g_malloc(roundup(sc->sc_root_size, BITS_PER_ENTRY), 335 M_WAITOK | M_ZERO); 336 for (i = 0; i < sc->sc_root_size; i++) 337 sc->sc_writemap_root[i] = 338 g_malloc(sc->sc_leaf_size * sizeof(uint64_t), 339 M_WAITOK | M_ZERO); 340 sc->sc_writemap_memory = 341 (sc->sc_root_size + sc->sc_root_size * sc->sc_leaf_size) * 342 sizeof(uint64_t) + roundup(sc->sc_root_size, BITS_PER_ENTRY); 343 if (verbose) 344 gctl_msg(req, 0, "Device %s created with memory map size %jd.", 345 gp->name, (intmax_t)sc->sc_writemap_memory); 346 gctl_post_messages(req); 347 G_UNION_DEBUG(1, "Device %s created with memory map size %jd.", 348 gp->name, (intmax_t)sc->sc_writemap_memory); 349 return; 350 351 fail4: 352 g_detach(uppercp); 353 fail3: 354 g_destroy_consumer(uppercp); 355 g_access(lowercp, -1, 0, -1); 356 fail2: 357 g_detach(lowercp); 358 fail1: 359 g_destroy_consumer(lowercp); 360 g_destroy_provider(newpp); 361 rw_destroy(&sc->sc_rwlock); 362 g_free(sc); 363 g_destroy_geom(gp); 364 } 365 366 /* 367 * Fetch named option and verify that it is positive. 368 */ 369 static intmax_t 370 g_union_fetcharg(struct gctl_req *req, const char *name) 371 { 372 intmax_t *val; 373 374 val = gctl_get_paraml_opt(req, name, sizeof(*val)); 375 if (val == NULL) 376 return (0); 377 if (*val >= 0) 378 return (*val); 379 gctl_msg(req, EINVAL, "Invalid '%s' (%jd): negative value, " 380 "using default.", name, *val); 381 return (0); 382 } 383 384 /* 385 * Verify that a name is alphanumeric. 386 */ 387 static bool 388 g_union_verify_nprefix(const char *name) 389 { 390 int i; 391 392 for (i = 0; i < strlen(name); i++) { 393 if (isalpha(name[i]) == 0 && isdigit(name[i]) == 0) { 394 return (false); 395 } 396 } 397 return (true); 398 } 399 400 /* 401 * Destroy a union device. 402 */ 403 static void 404 g_union_ctl_destroy(struct gctl_req *req, struct g_class *mp, bool verbose) 405 { 406 int *nargs, *force, error, i; 407 struct g_geom *gp; 408 const char *name; 409 char param[16]; 410 411 g_topology_assert(); 412 413 nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs)); 414 if (nargs == NULL) { 415 gctl_error(req, "No '%s' argument.", "nargs"); 416 return; 417 } 418 if (*nargs <= 0) { 419 gctl_error(req, "Missing device(s)."); 420 return; 421 } 422 force = gctl_get_paraml(req, "force", sizeof(*force)); 423 if (force == NULL) { 424 gctl_error(req, "No 'force' argument."); 425 return; 426 } 427 428 for (i = 0; i < *nargs; i++) { 429 snprintf(param, sizeof(param), "arg%d", i); 430 name = gctl_get_asciiparam(req, param); 431 if (name == NULL) { 432 gctl_msg(req, EINVAL, "No '%s' argument.", param); 433 continue; 434 } 435 if (strncmp(name, _PATH_DEV, strlen(_PATH_DEV)) == 0) 436 name += strlen(_PATH_DEV); 437 gp = g_union_find_geom(mp, name); 438 if (gp == NULL) { 439 gctl_msg(req, EINVAL, "Device %s is invalid.", name); 440 continue; 441 } 442 error = g_union_destroy(verbose ? req : NULL, gp, *force); 443 if (error != 0) 444 gctl_msg(req, error, "Error %d: " 445 "cannot destroy device %s.", error, gp->name); 446 } 447 gctl_post_messages(req); 448 } 449 450 /* 451 * Find a union geom. 452 */ 453 static struct g_geom * 454 g_union_find_geom(struct g_class *mp, const char *name) 455 { 456 struct g_geom *gp; 457 458 LIST_FOREACH(gp, &mp->geom, geom) { 459 if (strcmp(gp->name, name) == 0) 460 return (gp); 461 } 462 return (NULL); 463 } 464 465 /* 466 * Zero out all the statistics associated with a union device. 467 */ 468 static void 469 g_union_ctl_reset(struct gctl_req *req, struct g_class *mp, bool verbose) 470 { 471 struct g_union_softc *sc; 472 struct g_provider *pp; 473 struct g_geom *gp; 474 char param[16]; 475 int i, *nargs; 476 477 g_topology_assert(); 478 479 nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs)); 480 if (nargs == NULL) { 481 gctl_error(req, "No '%s' argument.", "nargs"); 482 return; 483 } 484 if (*nargs <= 0) { 485 gctl_error(req, "Missing device(s)."); 486 return; 487 } 488 489 for (i = 0; i < *nargs; i++) { 490 snprintf(param, sizeof(param), "arg%d", i); 491 pp = gctl_get_provider(req, param); 492 if (pp == NULL) { 493 gctl_msg(req, EINVAL, "No '%s' argument.", param); 494 continue; 495 } 496 gp = pp->geom; 497 if (gp->class != mp) { 498 gctl_msg(req, EINVAL, "Provider %s is invalid.", 499 pp->name); 500 continue; 501 } 502 sc = gp->softc; 503 sc->sc_reads = 0; 504 sc->sc_writes = 0; 505 sc->sc_deletes = 0; 506 sc->sc_getattrs = 0; 507 sc->sc_flushes = 0; 508 sc->sc_speedups = 0; 509 sc->sc_cmd0s = 0; 510 sc->sc_cmd1s = 0; 511 sc->sc_cmd2s = 0; 512 sc->sc_readbytes = 0; 513 sc->sc_wrotebytes = 0; 514 if (verbose) 515 gctl_msg(req, 0, "Device %s has been reset.", pp->name); 516 G_UNION_DEBUG(1, "Device %s has been reset.", pp->name); 517 } 518 gctl_post_messages(req); 519 } 520 521 /* 522 * Revert all write requests made to the top layer of the union. 523 */ 524 static void 525 g_union_ctl_revert(struct gctl_req *req, struct g_class *mp, bool verbose) 526 { 527 struct g_union_softc *sc; 528 struct g_provider *pp; 529 struct g_geom *gp; 530 char param[16]; 531 int i, *nargs; 532 533 g_topology_assert(); 534 535 nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs)); 536 if (nargs == NULL) { 537 gctl_error(req, "No '%s' argument.", "nargs"); 538 return; 539 } 540 if (*nargs <= 0) { 541 gctl_error(req, "Missing device(s)."); 542 return; 543 } 544 545 for (i = 0; i < *nargs; i++) { 546 snprintf(param, sizeof(param), "arg%d", i); 547 pp = gctl_get_provider(req, param); 548 if (pp == NULL) { 549 gctl_msg(req, EINVAL, "No '%s' argument.", param); 550 continue; 551 } 552 gp = pp->geom; 553 if (gp->class != mp) { 554 gctl_msg(req, EINVAL, "Provider %s is invalid.", 555 pp->name); 556 continue; 557 } 558 sc = gp->softc; 559 if (g_union_get_writelock(sc) != 0) { 560 gctl_msg(req, EINVAL, "Revert already in progress for " 561 "provider %s.", pp->name); 562 continue; 563 } 564 /* 565 * No mount or other use of union is allowed. 566 */ 567 if (pp->acr > 0 || pp->acw > 0 || pp->ace > 0) { 568 gctl_msg(req, EPERM, "Unable to get exclusive access " 569 "for reverting of %s;\n\t%s cannot be mounted or " 570 "otherwise open during a revert.", 571 pp->name, pp->name); 572 g_union_rel_writelock(sc); 573 continue; 574 } 575 g_union_revert(sc); 576 g_union_rel_writelock(sc); 577 if (verbose) 578 gctl_msg(req, 0, "Device %s has been reverted.", 579 pp->name); 580 G_UNION_DEBUG(1, "Device %s has been reverted.", pp->name); 581 } 582 gctl_post_messages(req); 583 } 584 585 /* 586 * Revert union writes by zero'ing out the writemap. 587 */ 588 static void 589 g_union_revert(struct g_union_softc *sc) 590 { 591 int i; 592 593 G_WLOCK(sc); 594 for (i = 0; i < sc->sc_root_size; i++) 595 memset(sc->sc_writemap_root[i], 0, 596 sc->sc_leaf_size * sizeof(uint64_t)); 597 memset(sc->sc_leafused, 0, roundup(sc->sc_root_size, BITS_PER_ENTRY)); 598 G_WUNLOCK(sc); 599 } 600 601 /* 602 * Commit all the writes made in the top layer to the lower layer. 603 */ 604 static void 605 g_union_ctl_commit(struct gctl_req *req, struct g_class *mp, bool verbose) 606 { 607 struct g_union_softc *sc; 608 struct g_provider *pp, *lowerpp; 609 struct g_consumer *lowercp; 610 struct g_geom *gp; 611 struct bio *bp; 612 char param[16]; 613 off_t len2rd, len2wt, savelen; 614 int i, error, error1, *nargs, *force, *reboot; 615 616 g_topology_assert(); 617 618 nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs)); 619 if (nargs == NULL) { 620 gctl_error(req, "No '%s' argument.", "nargs"); 621 return; 622 } 623 if (*nargs <= 0) { 624 gctl_error(req, "Missing device(s)."); 625 return; 626 } 627 force = gctl_get_paraml(req, "force", sizeof(*force)); 628 if (force == NULL) { 629 gctl_error(req, "No 'force' argument."); 630 return; 631 } 632 reboot = gctl_get_paraml(req, "reboot", sizeof(*reboot)); 633 if (reboot == NULL) { 634 gctl_error(req, "No 'reboot' argument."); 635 return; 636 } 637 638 /* Get a bio buffer to do our I/O */ 639 bp = g_alloc_bio(); 640 bp->bio_data = g_malloc(MAXBSIZE, M_WAITOK); 641 bp->bio_done = biodone; 642 for (i = 0; i < *nargs; i++) { 643 snprintf(param, sizeof(param), "arg%d", i); 644 pp = gctl_get_provider(req, param); 645 if (pp == NULL) { 646 gctl_msg(req, EINVAL, "No '%s' argument.", param); 647 continue; 648 } 649 gp = pp->geom; 650 if (gp->class != mp) { 651 gctl_msg(req, EINVAL, "Provider %s is invalid.", 652 pp->name); 653 continue; 654 } 655 sc = gp->softc; 656 if (g_union_get_writelock(sc) != 0) { 657 gctl_msg(req, EINVAL, "Commit already in progress for " 658 "provider %s.", pp->name); 659 continue; 660 } 661 662 /* upgrade to write access for lower */ 663 lowercp = sc->sc_lowercp; 664 lowerpp = lowercp->provider; 665 /* 666 * No mount or other use of union is allowed, unless the 667 * -f flag is given which allows read-only mount or usage. 668 */ 669 if ((*force == false && pp->acr > 0) || pp->acw > 0 || 670 pp->ace > 0) { 671 gctl_msg(req, EPERM, "Unable to get exclusive access " 672 "for writing of %s.\n\tNote that %s cannot be " 673 "mounted or otherwise\n\topen during a commit " 674 "unless the -f flag is used.", pp->name, pp->name); 675 g_union_rel_writelock(sc); 676 continue; 677 } 678 /* 679 * No mount or other use of lower media is allowed, unless the 680 * -f flag is given which allows read-only mount or usage. 681 */ 682 if ((*force == false && lowerpp->acr > lowercp->acr) || 683 lowerpp->acw > lowercp->acw || 684 lowerpp->ace > lowercp->ace) { 685 gctl_msg(req, EPERM, "provider %s is unable to get " 686 "exclusive access to %s\n\tfor writing. Note that " 687 "%s cannot be mounted or otherwise open\n\tduring " 688 "a commit unless the -f flag is used.", pp->name, 689 lowerpp->name, lowerpp->name); 690 g_union_rel_writelock(sc); 691 continue; 692 } 693 if ((error = g_access(lowercp, 0, 1, 0)) != 0) { 694 gctl_msg(req, error, "Error %d: provider %s is unable " 695 "to access %s for writing.", error, pp->name, 696 lowerpp->name); 697 g_union_rel_writelock(sc); 698 continue; 699 } 700 g_topology_unlock(); 701 /* Loop over write map copying across written blocks */ 702 bp->bio_offset = 0; 703 bp->bio_length = sc->sc_map_size * sc->sc_sectorsize; 704 G_RLOCK(sc); 705 error = 0; 706 while (bp->bio_length > 0) { 707 if (!g_union_getmap(bp, sc, &len2rd)) { 708 /* not written, so skip */ 709 bp->bio_offset += len2rd; 710 bp->bio_length -= len2rd; 711 continue; 712 } 713 G_RUNLOCK(sc); 714 /* need to read then write len2rd sectors */ 715 for ( ; len2rd > 0; len2rd -= len2wt) { 716 /* limit ourselves to MAXBSIZE size I/Os */ 717 len2wt = len2rd; 718 if (len2wt > MAXBSIZE) 719 len2wt = MAXBSIZE; 720 savelen = bp->bio_length; 721 bp->bio_length = len2wt; 722 bp->bio_cmd = BIO_READ; 723 g_io_request(bp, sc->sc_uppercp); 724 if ((error = biowait(bp, "rdunion")) != 0) { 725 gctl_msg(req, error, "Commit read " 726 "error %d in provider %s, commit " 727 "aborted.", error, pp->name); 728 goto cleanup; 729 } 730 bp->bio_flags &= ~BIO_DONE; 731 bp->bio_cmd = BIO_WRITE; 732 g_io_request(bp, lowercp); 733 if ((error = biowait(bp, "wtunion")) != 0) { 734 gctl_msg(req, error, "Commit write " 735 "error %d in provider %s, commit " 736 "aborted.", error, pp->name); 737 goto cleanup; 738 } 739 bp->bio_flags &= ~BIO_DONE; 740 bp->bio_offset += len2wt; 741 bp->bio_length = savelen - len2wt; 742 } 743 G_RLOCK(sc); 744 } 745 G_RUNLOCK(sc); 746 /* clear the write map */ 747 g_union_revert(sc); 748 cleanup: 749 g_topology_lock(); 750 /* return lower to previous access */ 751 if ((error1 = g_access(lowercp, 0, -1, 0)) != 0) { 752 G_UNION_DEBUG(2, "Error %d: device %s could not reset " 753 "access to %s (r=0 w=-1 e=0).", error1, pp->name, 754 lowerpp->name); 755 } 756 g_union_rel_writelock(sc); 757 if (error == 0 && verbose) 758 gctl_msg(req, 0, "Device %s has been committed.", 759 pp->name); 760 G_UNION_DEBUG(1, "Device %s has been committed.", pp->name); 761 } 762 gctl_post_messages(req); 763 g_free(bp->bio_data); 764 g_destroy_bio(bp); 765 if (*reboot) 766 kern_reboot(RB_AUTOBOOT); 767 } 768 769 /* 770 * Generally allow access unless a commit is in progress. 771 */ 772 static int 773 g_union_access(struct g_provider *pp, int r, int w, int e) 774 { 775 struct g_union_softc *sc; 776 777 sc = pp->geom->softc; 778 if (sc == NULL) { 779 if (r <= 0 && w <= 0 && e <= 0) 780 return (0); 781 return (ENXIO); 782 } 783 r += pp->acr; 784 w += pp->acw; 785 e += pp->ace; 786 if (g_union_get_writelock(sc) != 0) { 787 if ((pp->acr + pp->acw + pp->ace) > 0 && (r + w + e) == 0) 788 return (0); 789 return (EBUSY); 790 } 791 g_union_rel_writelock(sc); 792 return (0); 793 } 794 795 /* 796 * Initiate an I/O operation on the union device. 797 */ 798 static void 799 g_union_start(struct bio *bp) 800 { 801 struct g_union_softc *sc; 802 struct g_union_wip *wip; 803 struct bio *cbp; 804 805 sc = bp->bio_to->geom->softc; 806 if (bp->bio_cmd == BIO_READ || bp->bio_cmd == BIO_WRITE) { 807 wip = g_malloc(sizeof(*wip), M_NOWAIT); 808 if (wip == NULL) { 809 g_io_deliver(bp, ENOMEM); 810 return; 811 } 812 TAILQ_INIT(&wip->wip_waiting); 813 wip->wip_bp = bp; 814 wip->wip_sc = sc; 815 wip->wip_start = bp->bio_offset + sc->sc_offset; 816 wip->wip_end = wip->wip_start + bp->bio_length - 1; 817 wip->wip_numios = 1; 818 wip->wip_error = 0; 819 g_union_doio(wip); 820 return; 821 } 822 823 /* 824 * All commands other than read and write are passed through to 825 * the upper-level device since it is writable and thus able to 826 * respond to delete, flush, and speedup requests. 827 */ 828 cbp = g_clone_bio(bp); 829 if (cbp == NULL) { 830 g_io_deliver(bp, ENOMEM); 831 return; 832 } 833 cbp->bio_offset = bp->bio_offset + sc->sc_offset; 834 cbp->bio_done = g_std_done; 835 836 switch (cbp->bio_cmd) { 837 case BIO_DELETE: 838 G_UNION_LOGREQ(cbp, "Delete request received."); 839 atomic_add_long(&sc->sc_deletes, 1); 840 break; 841 case BIO_GETATTR: 842 G_UNION_LOGREQ(cbp, "Getattr request received."); 843 atomic_add_long(&sc->sc_getattrs, 1); 844 if (strcmp(cbp->bio_attribute, "GEOM::kerneldump") != 0) 845 /* forward the GETATTR to the lower-level device */ 846 break; 847 g_union_kerneldump(bp, sc); 848 return; 849 case BIO_FLUSH: 850 G_UNION_LOGREQ(cbp, "Flush request received."); 851 atomic_add_long(&sc->sc_flushes, 1); 852 break; 853 case BIO_SPEEDUP: 854 G_UNION_LOGREQ(cbp, "Speedup request received."); 855 atomic_add_long(&sc->sc_speedups, 1); 856 break; 857 case BIO_CMD0: 858 G_UNION_LOGREQ(cbp, "Cmd0 request received."); 859 atomic_add_long(&sc->sc_cmd0s, 1); 860 break; 861 case BIO_CMD1: 862 G_UNION_LOGREQ(cbp, "Cmd1 request received."); 863 atomic_add_long(&sc->sc_cmd1s, 1); 864 break; 865 case BIO_CMD2: 866 G_UNION_LOGREQ(cbp, "Cmd2 request received."); 867 atomic_add_long(&sc->sc_cmd2s, 1); 868 break; 869 default: 870 G_UNION_LOGREQ(cbp, "Unknown (%d) request received.", 871 cbp->bio_cmd); 872 break; 873 } 874 g_io_request(cbp, sc->sc_uppercp); 875 } 876 877 /* 878 * Initiate a read or write operation on the union device. 879 */ 880 static void 881 g_union_doio(struct g_union_wip *wip) 882 { 883 struct g_union_softc *sc; 884 struct g_consumer *cp, *firstcp; 885 struct g_union_wip *activewip; 886 struct bio *cbp, *firstbp; 887 off_t rdlen, len2rd, offset; 888 int iocnt, needstoblock; 889 char *level; 890 891 /* 892 * To maintain consistency, we cannot allow concurrent reads 893 * or writes to the same block. 894 * 895 * A work-in-progress (wip) structure is allocated for each 896 * read or write request. All active requests are kept on the 897 * softc sc_wiplist. As each request arrives, it is checked to 898 * see if it overlaps any of the active entries. If it does not 899 * overlap, then it is added to the active list and initiated. 900 * If it does overlap an active entry, it is added to the 901 * wip_waiting list for the active entry that it overlaps. 902 * When an active entry completes, it restarts all the requests 903 * on its wip_waiting list. 904 */ 905 sc = wip->wip_sc; 906 G_WLOCK(sc); 907 TAILQ_FOREACH(activewip, &sc->sc_wiplist, wip_next) { 908 if (wip->wip_end < activewip->wip_start || 909 wip->wip_start > activewip->wip_end) 910 continue; 911 needstoblock = 1; 912 if (wip->wip_bp->bio_cmd == BIO_WRITE) 913 if (activewip->wip_bp->bio_cmd == BIO_WRITE) 914 sc->sc_writeblockwrite += 1; 915 else 916 sc->sc_readblockwrite += 1; 917 else 918 if (activewip->wip_bp->bio_cmd == BIO_WRITE) 919 sc->sc_writeblockread += 1; 920 else { 921 sc->sc_readcurrentread += 1; 922 needstoblock = 0; 923 } 924 /* Put request on a waiting list if necessary */ 925 if (needstoblock) { 926 TAILQ_INSERT_TAIL(&activewip->wip_waiting, wip, 927 wip_next); 928 G_WUNLOCK(sc); 929 return; 930 } 931 } 932 /* Put request on the active list */ 933 TAILQ_INSERT_TAIL(&sc->sc_wiplist, wip, wip_next); 934 935 /* 936 * Process I/O requests that have been cleared to go. 937 */ 938 cbp = g_clone_bio(wip->wip_bp); 939 if (cbp == NULL) { 940 TAILQ_REMOVE(&sc->sc_wiplist, wip, wip_next); 941 G_WUNLOCK(sc); 942 KASSERT(TAILQ_FIRST(&wip->wip_waiting) == NULL, 943 ("g_union_doio: non-empty work-in-progress waiting queue")); 944 g_io_deliver(wip->wip_bp, ENOMEM); 945 g_free(wip); 946 return; 947 } 948 G_WUNLOCK(sc); 949 cbp->bio_caller1 = wip; 950 cbp->bio_done = g_union_done; 951 cbp->bio_offset = wip->wip_start; 952 953 /* 954 * Writes are always done to the top level. The blocks that 955 * are written are recorded in the bitmap when the I/O completes. 956 */ 957 if (cbp->bio_cmd == BIO_WRITE) { 958 G_UNION_LOGREQ(cbp, "Sending %jd byte write request to upper " 959 "level.", cbp->bio_length); 960 atomic_add_long(&sc->sc_writes, 1); 961 atomic_add_long(&sc->sc_wrotebytes, cbp->bio_length); 962 g_io_request(cbp, sc->sc_uppercp); 963 return; 964 } 965 /* 966 * The usual read case is that we either read the top layer 967 * if the block has been previously written or the bottom layer 968 * if it has not been written. However, it is possible that 969 * only part of the block has been written, For example we may 970 * have written a UFS/FFS file fragment comprising several 971 * sectors out of an 8-sector block. Here, if the entire 972 * 8-sector block is read for example by a snapshot needing 973 * to copy the full block, then we need to read the written 974 * sectors from the upper level and the unwritten sectors from 975 * the lower level. We do this by alternately reading from the 976 * top and bottom layers until we complete the read. We 977 * simplify for the common case to just do the I/O and return. 978 */ 979 atomic_add_long(&sc->sc_reads, 1); 980 atomic_add_long(&sc->sc_readbytes, cbp->bio_length); 981 rdlen = cbp->bio_length; 982 offset = 0; 983 for (iocnt = 0; ; iocnt++) { 984 if (g_union_getmap(cbp, sc, &len2rd)) { 985 /* read top */ 986 cp = sc->sc_uppercp; 987 level = "upper"; 988 } else { 989 /* read bottom */ 990 cp = sc->sc_lowercp; 991 level = "lower"; 992 } 993 /* Check if only a single read is required */ 994 if (iocnt == 0 && rdlen == len2rd) { 995 G_UNION_LOGREQLVL((cp == sc->sc_uppercp) ? 996 3 : 4, cbp, "Sending %jd byte read " 997 "request to %s level.", len2rd, level); 998 g_io_request(cbp, cp); 999 return; 1000 } 1001 cbp->bio_length = len2rd; 1002 if ((cbp->bio_flags & BIO_UNMAPPED) != 0) 1003 cbp->bio_ma_offset += offset; 1004 else 1005 cbp->bio_data += offset; 1006 offset += len2rd; 1007 rdlen -= len2rd; 1008 G_UNION_LOGREQLVL(3, cbp, "Sending %jd byte read " 1009 "request to %s level.", len2rd, level); 1010 /* 1011 * To avoid prematurely notifying our consumer 1012 * that their I/O has completed, we have to delay 1013 * issuing our first I/O request until we have 1014 * issued all the additional I/O requests. 1015 */ 1016 if (iocnt > 0) { 1017 atomic_add_long(&wip->wip_numios, 1); 1018 g_io_request(cbp, cp); 1019 } else { 1020 firstbp = cbp; 1021 firstcp = cp; 1022 } 1023 if (rdlen == 0) 1024 break; 1025 /* set up for next read */ 1026 cbp = g_clone_bio(wip->wip_bp); 1027 if (cbp == NULL) { 1028 wip->wip_error = ENOMEM; 1029 atomic_add_long(&wip->wip_numios, -1); 1030 break; 1031 } 1032 cbp->bio_caller1 = wip; 1033 cbp->bio_done = g_union_done; 1034 cbp->bio_offset += offset; 1035 cbp->bio_length = rdlen; 1036 atomic_add_long(&sc->sc_reads, 1); 1037 } 1038 /* We have issued all our I/O, so start the first one */ 1039 g_io_request(firstbp, firstcp); 1040 return; 1041 } 1042 1043 /* 1044 * Used when completing a union I/O operation. 1045 */ 1046 static void 1047 g_union_done(struct bio *bp) 1048 { 1049 struct g_union_wip *wip, *waitingwip; 1050 struct g_union_softc *sc; 1051 1052 wip = bp->bio_caller1; 1053 if (wip->wip_error != 0 && bp->bio_error == 0) 1054 bp->bio_error = wip->wip_error; 1055 wip->wip_error = 0; 1056 if (atomic_fetchadd_long(&wip->wip_numios, -1) == 1) { 1057 sc = wip->wip_sc; 1058 G_WLOCK(sc); 1059 if (bp->bio_cmd == BIO_WRITE) 1060 g_union_setmap(bp, sc); 1061 TAILQ_REMOVE(&sc->sc_wiplist, wip, wip_next); 1062 G_WUNLOCK(sc); 1063 while ((waitingwip = TAILQ_FIRST(&wip->wip_waiting)) != NULL) { 1064 TAILQ_REMOVE(&wip->wip_waiting, waitingwip, wip_next); 1065 g_union_doio(waitingwip); 1066 } 1067 g_free(wip); 1068 } 1069 g_std_done(bp); 1070 } 1071 1072 /* 1073 * Record blocks that have been written in the map. 1074 */ 1075 static void 1076 g_union_setmap(struct bio *bp, struct g_union_softc *sc) 1077 { 1078 size_t root_idx; 1079 uint64_t **leaf; 1080 uint64_t *wordp; 1081 off_t start, numsec; 1082 1083 G_WLOCKOWNED(sc); 1084 KASSERT(bp->bio_offset % sc->sc_sectorsize == 0, 1085 ("g_union_setmap: offset not on sector boundry")); 1086 KASSERT(bp->bio_length % sc->sc_sectorsize == 0, 1087 ("g_union_setmap: length not a multiple of sectors")); 1088 start = bp->bio_offset / sc->sc_sectorsize; 1089 numsec = bp->bio_length / sc->sc_sectorsize; 1090 KASSERT(start + numsec <= sc->sc_map_size, 1091 ("g_union_setmap: block %jd is out of range", start + numsec)); 1092 for ( ; numsec > 0; numsec--, start++) { 1093 root_idx = start / sc->sc_bits_per_leaf; 1094 leaf = &sc->sc_writemap_root[root_idx]; 1095 wordp = &(*leaf) 1096 [(start % sc->sc_bits_per_leaf) / BITS_PER_ENTRY]; 1097 *wordp |= 1ULL << (start % BITS_PER_ENTRY); 1098 sc->sc_leafused[root_idx / BITS_PER_ENTRY] |= 1099 1ULL << (root_idx % BITS_PER_ENTRY); 1100 } 1101 } 1102 1103 /* 1104 * Check map to determine whether blocks have been written. 1105 * 1106 * Return true if they have been written so should be read from the top 1107 * layer. Return false if they have not been written so should be read 1108 * from the bottom layer. Return in len2read the bytes to be read. See 1109 * the comment above the BIO_READ implementation in g_union_start() for 1110 * an explantion of why len2read may be shorter than the buffer length. 1111 */ 1112 static bool 1113 g_union_getmap(struct bio *bp, struct g_union_softc *sc, off_t *len2read) 1114 { 1115 off_t start, numsec, leafresid, bitloc; 1116 bool first, maptype, retval; 1117 uint64_t *leaf, word; 1118 size_t root_idx; 1119 1120 KASSERT(bp->bio_offset % sc->sc_sectorsize == 0, 1121 ("g_union_getmap: offset not on sector boundry")); 1122 KASSERT(bp->bio_length % sc->sc_sectorsize == 0, 1123 ("g_union_getmap: length not a multiple of sectors")); 1124 start = bp->bio_offset / sc->sc_sectorsize; 1125 numsec = bp->bio_length / sc->sc_sectorsize; 1126 G_UNION_DEBUG(4, "g_union_getmap: check %jd sectors starting at %jd\n", 1127 numsec, start); 1128 KASSERT(start + numsec <= sc->sc_map_size, 1129 ("g_union_getmap: block %jd is out of range", start + numsec)); 1130 root_idx = start / sc->sc_bits_per_leaf; 1131 first = true; 1132 maptype = false; 1133 while (numsec > 0) { 1134 /* Check first if the leaf records any written sectors */ 1135 root_idx = start / sc->sc_bits_per_leaf; 1136 leafresid = sc->sc_bits_per_leaf - 1137 (start % sc->sc_bits_per_leaf); 1138 if (((sc->sc_leafused[root_idx / BITS_PER_ENTRY]) & 1139 (1ULL << (root_idx % BITS_PER_ENTRY))) == 0) { 1140 if (first) { 1141 maptype = false; 1142 first = false; 1143 } 1144 if (maptype) 1145 break; 1146 numsec -= leafresid; 1147 start += leafresid; 1148 continue; 1149 } 1150 /* Check up to a word boundry, then check word by word */ 1151 leaf = sc->sc_writemap_root[root_idx]; 1152 word = leaf[(start % sc->sc_bits_per_leaf) / BITS_PER_ENTRY]; 1153 bitloc = start % BITS_PER_ENTRY; 1154 if (bitloc == 0 && (word == 0 || word == ~0)) { 1155 if (first) { 1156 if (word == 0) 1157 maptype = false; 1158 else 1159 maptype = true; 1160 first = false; 1161 } 1162 if ((word == 0 && maptype) || 1163 (word == ~0 && !maptype)) 1164 break; 1165 numsec -= BITS_PER_ENTRY; 1166 start += BITS_PER_ENTRY; 1167 continue; 1168 } 1169 for ( ; bitloc < BITS_PER_ENTRY; bitloc ++) { 1170 retval = (word & (1ULL << bitloc)) != 0; 1171 if (first) { 1172 maptype = retval; 1173 first = false; 1174 } 1175 if (maptype == retval) { 1176 numsec--; 1177 start++; 1178 continue; 1179 } 1180 goto out; 1181 } 1182 } 1183 out: 1184 if (numsec < 0) { 1185 start += numsec; 1186 numsec = 0; 1187 } 1188 *len2read = bp->bio_length - (numsec * sc->sc_sectorsize); 1189 G_UNION_DEBUG(maptype ? 3 : 4, 1190 "g_union_getmap: return maptype %swritten for %jd " 1191 "sectors ending at %jd\n", maptype ? "" : "NOT ", 1192 *len2read / sc->sc_sectorsize, start - 1); 1193 return (maptype); 1194 } 1195 1196 /* 1197 * Fill in details for a BIO_GETATTR request. 1198 */ 1199 static void 1200 g_union_kerneldump(struct bio *bp, struct g_union_softc *sc) 1201 { 1202 struct g_kerneldump *gkd; 1203 struct g_geom *gp; 1204 struct g_provider *pp; 1205 1206 gkd = (struct g_kerneldump *)bp->bio_data; 1207 gp = bp->bio_to->geom; 1208 g_trace(G_T_TOPOLOGY, "%s(%s, %jd, %jd)", __func__, gp->name, 1209 (intmax_t)gkd->offset, (intmax_t)gkd->length); 1210 1211 pp = LIST_FIRST(&gp->provider); 1212 1213 gkd->di.dumper = g_union_dumper; 1214 gkd->di.priv = sc; 1215 gkd->di.blocksize = pp->sectorsize; 1216 gkd->di.maxiosize = DFLTPHYS; 1217 gkd->di.mediaoffset = sc->sc_offset + gkd->offset; 1218 if (gkd->offset > sc->sc_size) { 1219 g_io_deliver(bp, ENODEV); 1220 return; 1221 } 1222 if (gkd->offset + gkd->length > sc->sc_size) 1223 gkd->length = sc->sc_size - gkd->offset; 1224 gkd->di.mediasize = gkd->length; 1225 g_io_deliver(bp, 0); 1226 } 1227 1228 /* 1229 * Handler for g_union_kerneldump(). 1230 */ 1231 static int 1232 g_union_dumper(void *priv, void *virtual, off_t offset, size_t length) 1233 { 1234 1235 return (0); 1236 } 1237 1238 /* 1239 * List union statistics. 1240 */ 1241 static void 1242 g_union_dumpconf(struct sbuf *sb, const char *indent, struct g_geom *gp, 1243 struct g_consumer *cp, struct g_provider *pp) 1244 { 1245 struct g_union_softc *sc; 1246 1247 if (pp != NULL || cp != NULL || gp->softc == NULL) 1248 return; 1249 sc = gp->softc; 1250 sbuf_printf(sb, "%s<Reads>%ju</Reads>\n", indent, 1251 (uintmax_t)sc->sc_reads); 1252 sbuf_printf(sb, "%s<Writes>%ju</Writes>\n", indent, 1253 (uintmax_t)sc->sc_writes); 1254 sbuf_printf(sb, "%s<Deletes>%ju</Deletes>\n", indent, 1255 (uintmax_t)sc->sc_deletes); 1256 sbuf_printf(sb, "%s<Getattrs>%ju</Getattrs>\n", indent, 1257 (uintmax_t)sc->sc_getattrs); 1258 sbuf_printf(sb, "%s<Flushes>%ju</Flushes>\n", indent, 1259 (uintmax_t)sc->sc_flushes); 1260 sbuf_printf(sb, "%s<Speedups>%ju</Speedups>\n", indent, 1261 (uintmax_t)sc->sc_speedups); 1262 sbuf_printf(sb, "%s<Cmd0s>%ju</Cmd0s>\n", indent, 1263 (uintmax_t)sc->sc_cmd0s); 1264 sbuf_printf(sb, "%s<Cmd1s>%ju</Cmd1s>\n", indent, 1265 (uintmax_t)sc->sc_cmd1s); 1266 sbuf_printf(sb, "%s<Cmd2s>%ju</Cmd2s>\n", indent, 1267 (uintmax_t)sc->sc_cmd2s); 1268 sbuf_printf(sb, "%s<ReadCurrentRead>%ju</ReadCurrentRead>\n", indent, 1269 (uintmax_t)sc->sc_readcurrentread); 1270 sbuf_printf(sb, "%s<ReadBlockWrite>%ju</ReadBlockWrite>\n", indent, 1271 (uintmax_t)sc->sc_readblockwrite); 1272 sbuf_printf(sb, "%s<WriteBlockRead>%ju</WriteBlockRead>\n", indent, 1273 (uintmax_t)sc->sc_writeblockread); 1274 sbuf_printf(sb, "%s<WriteBlockWrite>%ju</WriteBlockWrite>\n", indent, 1275 (uintmax_t)sc->sc_writeblockwrite); 1276 sbuf_printf(sb, "%s<ReadBytes>%ju</ReadBytes>\n", indent, 1277 (uintmax_t)sc->sc_readbytes); 1278 sbuf_printf(sb, "%s<WroteBytes>%ju</WroteBytes>\n", indent, 1279 (uintmax_t)sc->sc_wrotebytes); 1280 sbuf_printf(sb, "%s<Offset>%jd</Offset>\n", indent, 1281 (intmax_t)sc->sc_offset); 1282 } 1283 1284 /* 1285 * Clean up an orphaned geom. 1286 */ 1287 static void 1288 g_union_orphan(struct g_consumer *cp) 1289 { 1290 1291 g_topology_assert(); 1292 g_union_destroy(NULL, cp->geom, true); 1293 } 1294 1295 /* 1296 * Clean up a union geom. 1297 */ 1298 static int 1299 g_union_destroy_geom(struct gctl_req *req, struct g_class *mp, 1300 struct g_geom *gp) 1301 { 1302 1303 return (g_union_destroy(NULL, gp, false)); 1304 } 1305 1306 /* 1307 * Clean up a union device. 1308 */ 1309 static int 1310 g_union_destroy(struct gctl_req *req, struct g_geom *gp, bool force) 1311 { 1312 struct g_union_softc *sc; 1313 struct g_provider *pp; 1314 int error; 1315 1316 g_topology_assert(); 1317 sc = gp->softc; 1318 if (sc == NULL) 1319 return (ENXIO); 1320 pp = LIST_FIRST(&gp->provider); 1321 if ((sc->sc_flags & DOING_COMMIT) != 0 || 1322 (pp != NULL && (pp->acr != 0 || pp->acw != 0 || pp->ace != 0))) { 1323 if (force) { 1324 if (req != NULL) 1325 gctl_msg(req, 0, "Device %s is still in use, " 1326 "so is being forcibly removed.", gp->name); 1327 G_UNION_DEBUG(1, "Device %s is still in use, so " 1328 "is being forcibly removed.", gp->name); 1329 } else { 1330 if (req != NULL) 1331 gctl_msg(req, EBUSY, "Device %s is still open " 1332 "(r=%d w=%d e=%d).", gp->name, pp->acr, 1333 pp->acw, pp->ace); 1334 G_UNION_DEBUG(1, "Device %s is still open " 1335 "(r=%d w=%d e=%d).", gp->name, pp->acr, 1336 pp->acw, pp->ace); 1337 return (EBUSY); 1338 } 1339 } else { 1340 if (req != NULL) 1341 gctl_msg(req, 0, "Device %s removed.", gp->name); 1342 G_UNION_DEBUG(1, "Device %s removed.", gp->name); 1343 } 1344 /* Close consumers */ 1345 if ((error = g_access(sc->sc_lowercp, -1, 0, -1)) != 0) 1346 G_UNION_DEBUG(2, "Error %d: device %s could not reset access " 1347 "to %s.", error, gp->name, sc->sc_lowercp->provider->name); 1348 if ((error = g_access(sc->sc_uppercp, -1, -1, -1)) != 0) 1349 G_UNION_DEBUG(2, "Error %d: device %s could not reset access " 1350 "to %s.", error, gp->name, sc->sc_uppercp->provider->name); 1351 1352 g_wither_geom(gp, ENXIO); 1353 1354 return (0); 1355 } 1356 1357 /* 1358 * Clean up a union provider. 1359 */ 1360 static void 1361 g_union_providergone(struct g_provider *pp) 1362 { 1363 struct g_geom *gp; 1364 struct g_union_softc *sc; 1365 size_t i; 1366 1367 gp = pp->geom; 1368 sc = gp->softc; 1369 gp->softc = NULL; 1370 for (i = 0; i < sc->sc_root_size; i++) 1371 g_free(sc->sc_writemap_root[i]); 1372 g_free(sc->sc_writemap_root); 1373 g_free(sc->sc_leafused); 1374 rw_destroy(&sc->sc_rwlock); 1375 g_free(sc); 1376 } 1377 1378 /* 1379 * Respond to a resized provider. 1380 */ 1381 static void 1382 g_union_resize(struct g_consumer *cp) 1383 { 1384 struct g_union_softc *sc; 1385 struct g_geom *gp; 1386 1387 g_topology_assert(); 1388 1389 gp = cp->geom; 1390 sc = gp->softc; 1391 1392 /* 1393 * If size has gotten bigger, ignore it and just keep using 1394 * the space we already had. Otherwise we are done. 1395 */ 1396 if (sc->sc_size < cp->provider->mediasize - sc->sc_offset) 1397 return; 1398 g_union_destroy(NULL, gp, true); 1399 } 1400 1401 DECLARE_GEOM_CLASS(g_union_class, g_union); 1402 MODULE_VERSION(geom_union, 0); 1403