192108Sphk/*- 292108Sphk * Copyright (c) 2002 Poul-Henning Kamp 392108Sphk * Copyright (c) 2002 Networks Associates Technology, Inc. 492108Sphk * All rights reserved. 592108Sphk * 692108Sphk * This software was developed for the FreeBSD Project by Poul-Henning Kamp 792108Sphk * and NAI Labs, the Security Research Division of Network Associates, Inc. 892108Sphk * under DARPA/SPAWAR contract N66001-01-C-8035 ("CBOSS"), as part of the 992108Sphk * DARPA CHATS research program. 1092108Sphk * 1192108Sphk * Redistribution and use in source and binary forms, with or without 1292108Sphk * modification, are permitted provided that the following conditions 1392108Sphk * are met: 1492108Sphk * 1. Redistributions of source code must retain the above copyright 1592108Sphk * notice, this list of conditions and the following disclaimer. 1692108Sphk * 2. Redistributions in binary form must reproduce the above copyright 1792108Sphk * notice, this list of conditions and the following disclaimer in the 1892108Sphk * documentation and/or other materials provided with the distribution. 1992108Sphk * 3. The names of the authors may not be used to endorse or promote 2092108Sphk * products derived from this software without specific prior written 2192108Sphk * permission. 2292108Sphk * 2392108Sphk * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 2492108Sphk * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 2592108Sphk * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 2692108Sphk * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 2792108Sphk * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 2892108Sphk * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 2992108Sphk * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 3092108Sphk * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 3192108Sphk * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 3292108Sphk * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 3392108Sphk * SUCH DAMAGE. 3492108Sphk */ 3592108Sphk 36116196Sobrien#include <sys/cdefs.h> 37116196Sobrien__FBSDID("$FreeBSD: stable/10/sys/geom/geom_slice.c 325908 2017-11-16 22:37:03Z avg $"); 3892108Sphk 3992108Sphk#include <sys/param.h> 4092108Sphk#include <sys/systm.h> 4192108Sphk#include <sys/kernel.h> 4292108Sphk#include <sys/malloc.h> 4392108Sphk#include <sys/bio.h> 4492108Sphk#include <sys/sysctl.h> 4592108Sphk#include <sys/proc.h> 4692108Sphk#include <sys/lock.h> 4792108Sphk#include <sys/mutex.h> 4892108Sphk#include <sys/errno.h> 4992108Sphk#include <sys/sbuf.h> 5092108Sphk#include <geom/geom.h> 5192108Sphk#include <geom/geom_slice.h> 5292108Sphk#include <machine/stdarg.h> 5392108Sphk 5493776Sphkstatic g_access_t g_slice_access; 5593776Sphkstatic g_start_t g_slice_start; 5693776Sphk 5795321Sphkstatic struct g_slicer * 58114493Sphkg_slice_alloc(unsigned nslice, unsigned scsize) 5992108Sphk{ 6092108Sphk struct g_slicer *gsp; 6192108Sphk 62111119Simp gsp = g_malloc(sizeof *gsp, M_WAITOK | M_ZERO); 63131046Spjd if (scsize > 0) 64131046Spjd gsp->softc = g_malloc(scsize, M_WAITOK | M_ZERO); 65131046Spjd else 66131046Spjd gsp->softc = NULL; 67104057Sphk gsp->slices = g_malloc(nslice * sizeof(struct g_slice), 68111119Simp M_WAITOK | M_ZERO); 6992108Sphk gsp->nslice = nslice; 7092108Sphk return (gsp); 7192108Sphk} 7292108Sphk 73114493Sphkstatic void 74325908Savgg_slice_free(struct g_geom *gp) 75114493Sphk{ 76325908Savg struct g_slicer *gsp; 77114493Sphk 78325908Savg gsp = gp->softc; 79325908Savg gp->softc = NULL; 80325908Savg 81325908Savg /* 82325908Savg * We can get multiple spoiled events before wither-washer 83325908Savg * detaches our consumer, so this can get called multiple 84325908Savg * times. 85325908Savg */ 86325908Savg if (gsp == NULL) 87132631Sle return; 88114493Sphk g_free(gsp->slices); 89114493Sphk if (gsp->hotspot != NULL) 90114493Sphk g_free(gsp->hotspot); 91131408Spjd if (gsp->softc != NULL) 92131408Spjd g_free(gsp->softc); 93114493Sphk g_free(gsp); 94114493Sphk} 95114493Sphk 9693776Sphkstatic int 9792108Sphkg_slice_access(struct g_provider *pp, int dr, int dw, int de) 9892108Sphk{ 99107953Sphk int error; 100107953Sphk u_int u; 10192108Sphk struct g_geom *gp; 10292108Sphk struct g_consumer *cp; 10392108Sphk struct g_provider *pp2; 10492108Sphk struct g_slicer *gsp; 10592108Sphk struct g_slice *gsl, *gsl2; 10692108Sphk 10792108Sphk gp = pp->geom; 10892108Sphk cp = LIST_FIRST(&gp->consumer); 10992108Sphk KASSERT (cp != NULL, ("g_slice_access but no consumer")); 11092108Sphk gsp = gp->softc; 111131568Sphk if (dr > 0 || dw > 0 || de > 0) { 112131568Sphk gsl = &gsp->slices[pp->index]; 113131568Sphk for (u = 0; u < gsp->nslice; u++) { 114131568Sphk gsl2 = &gsp->slices[u]; 115131568Sphk if (gsl2->length == 0) 116131568Sphk continue; 117131568Sphk if (u == pp->index) 118131568Sphk continue; 119131568Sphk if (gsl->offset + gsl->length <= gsl2->offset) 120131568Sphk continue; 121131568Sphk if (gsl2->offset + gsl2->length <= gsl->offset) 122131568Sphk continue; 123131568Sphk /* overlap */ 124131568Sphk pp2 = gsl2->provider; 125131568Sphk if ((pp->acw + dw) > 0 && pp2->ace > 0) 126131568Sphk return (EPERM); 127131568Sphk if ((pp->ace + de) > 0 && pp2->acw > 0) 128131568Sphk return (EPERM); 129131568Sphk } 13092108Sphk } 13192108Sphk /* On first open, grab an extra "exclusive" bit */ 13292108Sphk if (cp->acr == 0 && cp->acw == 0 && cp->ace == 0) 13392108Sphk de++; 13492108Sphk /* ... and let go of it on last close */ 13592108Sphk if ((cp->acr + dr) == 0 && (cp->acw + dw) == 0 && (cp->ace + de) == 1) 13692108Sphk de--; 137125755Sphk error = g_access(cp, dr, dw, de); 138325908Savg 139325908Savg /* 140325908Savg * Free the softc if all providers have been closed and this geom 141325908Savg * is being removed. 142325908Savg */ 143325908Savg if (error == 0 && (gp->flags & G_GEOM_WITHER) != 0 && 144325908Savg (cp->acr + cp->acw + cp->ace) == 0) 145325908Savg g_slice_free(gp); 146325908Savg 14792108Sphk return (error); 14892108Sphk} 14992108Sphk 150113713Sphk/* 151113713Sphk * XXX: It should be possible to specify here if we should finish all of the 152113713Sphk * XXX: bio, or only the non-hot bits. This would get messy if there were 153113713Sphk * XXX: two hot spots in the same bio, so for now we simply finish off the 154113713Sphk * XXX: entire bio. Modifying hot data on the way to disk is frowned on 155113713Sphk * XXX: so making that considerably harder is not a bad idea anyway. 156113713Sphk */ 157107522Sphkvoid 158107522Sphkg_slice_finish_hot(struct bio *bp) 159107522Sphk{ 160107522Sphk struct bio *bp2; 161107522Sphk struct g_geom *gp; 162107522Sphk struct g_consumer *cp; 163107522Sphk struct g_slicer *gsp; 164107522Sphk struct g_slice *gsl; 165107953Sphk int idx; 166107522Sphk 167113713Sphk KASSERT(bp->bio_to != NULL, 168113713Sphk ("NULL bio_to in g_slice_finish_hot(%p)", bp)); 169113713Sphk KASSERT(bp->bio_from != NULL, 170113713Sphk ("NULL bio_from in g_slice_finish_hot(%p)", bp)); 171107522Sphk gp = bp->bio_to->geom; 172107522Sphk gsp = gp->softc; 173107522Sphk cp = LIST_FIRST(&gp->consumer); 174107522Sphk KASSERT(cp != NULL, ("NULL consumer in g_slice_finish_hot(%p)", bp)); 175107953Sphk idx = bp->bio_to->index; 176107953Sphk gsl = &gsp->slices[idx]; 177107522Sphk 178107522Sphk bp2 = g_clone_bio(bp); 179107522Sphk if (bp2 == NULL) { 180107522Sphk g_io_deliver(bp, ENOMEM); 181107522Sphk return; 182107522Sphk } 183107522Sphk if (bp2->bio_offset + bp2->bio_length > gsl->length) 184107522Sphk bp2->bio_length = gsl->length - bp2->bio_offset; 185107522Sphk bp2->bio_done = g_std_done; 186107522Sphk bp2->bio_offset += gsl->offset; 187107522Sphk g_io_request(bp2, cp); 188107522Sphk return; 189107522Sphk} 190107522Sphk 19193776Sphkstatic void 192169288Spjdg_slice_done(struct bio *bp) 193169288Spjd{ 194169288Spjd 195169288Spjd KASSERT(bp->bio_cmd == BIO_GETATTR && 196169288Spjd strcmp(bp->bio_attribute, "GEOM::ident") == 0, 197169288Spjd ("bio_cmd=0x%x bio_attribute=%s", bp->bio_cmd, bp->bio_attribute)); 198169288Spjd 199169288Spjd if (bp->bio_error == 0 && bp->bio_data[0] != '\0') { 200169288Spjd char idx[8]; 201169288Spjd 202169288Spjd /* Add index to the ident received. */ 203169288Spjd snprintf(idx, sizeof(idx), "s%d", 204169288Spjd bp->bio_parent->bio_to->index); 205169288Spjd if (strlcat(bp->bio_data, idx, bp->bio_length) >= 206169288Spjd bp->bio_length) { 207169288Spjd bp->bio_error = EFAULT; 208169288Spjd } 209169288Spjd } 210169288Spjd g_std_done(bp); 211169288Spjd} 212169288Spjd 213169288Spjdstatic void 21492108Sphkg_slice_start(struct bio *bp) 21592108Sphk{ 21692108Sphk struct bio *bp2; 21792108Sphk struct g_provider *pp; 21892108Sphk struct g_geom *gp; 21992108Sphk struct g_consumer *cp; 22092108Sphk struct g_slicer *gsp; 221113712Sphk struct g_slice *gsl; 222113713Sphk struct g_slice_hot *ghp; 223107953Sphk int idx, error; 224107522Sphk u_int m_index; 22594287Sphk off_t t; 22692108Sphk 22792108Sphk pp = bp->bio_to; 22892108Sphk gp = pp->geom; 22992108Sphk gsp = gp->softc; 23092108Sphk cp = LIST_FIRST(&gp->consumer); 231107953Sphk idx = pp->index; 232107953Sphk gsl = &gsp->slices[idx]; 23392108Sphk switch(bp->bio_cmd) { 23492108Sphk case BIO_READ: 23592108Sphk case BIO_WRITE: 23692108Sphk case BIO_DELETE: 23792108Sphk if (bp->bio_offset > gsl->length) { 238104195Sphk g_io_deliver(bp, EINVAL); /* XXX: EWHAT ? */ 23992108Sphk return; 24092108Sphk } 241107522Sphk /* 242107522Sphk * Check if we collide with any hot spaces, and call the 243107522Sphk * method once if so. 244107522Sphk */ 245107832Sphk t = bp->bio_offset + gsl->offset; 246113712Sphk for (m_index = 0; m_index < gsp->nhotspot; m_index++) { 247113713Sphk ghp = &gsp->hotspot[m_index]; 248113713Sphk if (t >= ghp->offset + ghp->length) 249107522Sphk continue; 250113713Sphk if (t + bp->bio_length <= ghp->offset) 251107522Sphk continue; 252113713Sphk switch(bp->bio_cmd) { 253113713Sphk case BIO_READ: idx = ghp->ract; break; 254113713Sphk case BIO_WRITE: idx = ghp->wact; break; 255113713Sphk case BIO_DELETE: idx = ghp->dact; break; 256113713Sphk } 257113713Sphk switch(idx) { 258113713Sphk case G_SLICE_HOT_ALLOW: 259113713Sphk /* Fall out and continue normal processing */ 260113713Sphk continue; 261113713Sphk case G_SLICE_HOT_DENY: 262113713Sphk g_io_deliver(bp, EROFS); 263107522Sphk return; 264113713Sphk case G_SLICE_HOT_START: 265113713Sphk error = gsp->start(bp); 266113713Sphk if (error && error != EJUSTRETURN) 267113713Sphk g_io_deliver(bp, error); 268107522Sphk return; 269113713Sphk case G_SLICE_HOT_CALL: 270113937Sphk error = g_post_event(gsp->hot, bp, M_NOWAIT, 271113937Sphk gp, NULL); 272113713Sphk if (error) 273113713Sphk g_io_deliver(bp, error); 274113713Sphk return; 275107522Sphk } 276107522Sphk break; 277107522Sphk } 27892108Sphk bp2 = g_clone_bio(bp); 279104057Sphk if (bp2 == NULL) { 280104195Sphk g_io_deliver(bp, ENOMEM); 281104057Sphk return; 282104057Sphk } 28392108Sphk if (bp2->bio_offset + bp2->bio_length > gsl->length) 28492108Sphk bp2->bio_length = gsl->length - bp2->bio_offset; 28592108Sphk bp2->bio_done = g_std_done; 28692108Sphk bp2->bio_offset += gsl->offset; 28792108Sphk g_io_request(bp2, cp); 28892108Sphk return; 28992108Sphk case BIO_GETATTR: 29094287Sphk /* Give the real method a chance to override */ 291113878Sphk if (gsp->start != NULL && gsp->start(bp)) 29294287Sphk return; 293169288Spjd if (!strcmp("GEOM::ident", bp->bio_attribute)) { 294169288Spjd bp2 = g_clone_bio(bp); 295169288Spjd if (bp2 == NULL) { 296169288Spjd g_io_deliver(bp, ENOMEM); 297169288Spjd return; 298169288Spjd } 299169288Spjd bp2->bio_done = g_slice_done; 300169288Spjd g_io_request(bp2, cp); 301169288Spjd return; 302169288Spjd } 30395038Sphk if (!strcmp("GEOM::kerneldump", bp->bio_attribute)) { 30495038Sphk struct g_kerneldump *gkd; 30595038Sphk 30695038Sphk gkd = (struct g_kerneldump *)bp->bio_data; 307107953Sphk gkd->offset += gsp->slices[idx].offset; 308107953Sphk if (gkd->length > gsp->slices[idx].length) 309107953Sphk gkd->length = gsp->slices[idx].length; 31095038Sphk /* now, pass it on downwards... */ 31195038Sphk } 312163836Spjd /* FALLTHROUGH */ 313163836Spjd case BIO_FLUSH: 31492108Sphk bp2 = g_clone_bio(bp); 315104081Sphk if (bp2 == NULL) { 316104195Sphk g_io_deliver(bp, ENOMEM); 317104081Sphk return; 318104081Sphk } 31992108Sphk bp2->bio_done = g_std_done; 32092108Sphk g_io_request(bp2, cp); 32192108Sphk break; 32292108Sphk default: 323104195Sphk g_io_deliver(bp, EOPNOTSUPP); 32492108Sphk return; 32592108Sphk } 32692108Sphk} 32792108Sphk 32892108Sphkvoid 329107953Sphkg_slice_dumpconf(struct sbuf *sb, const char *indent, struct g_geom *gp, struct g_consumer *cp, struct g_provider *pp) 33092108Sphk{ 33192108Sphk struct g_slicer *gsp; 33292108Sphk 33392108Sphk gsp = gp->softc; 334106101Sphk if (indent == NULL) { 335106101Sphk sbuf_printf(sb, " i %u", pp->index); 336106101Sphk sbuf_printf(sb, " o %ju", 337106101Sphk (uintmax_t)gsp->slices[pp->index].offset); 338106101Sphk return; 339106101Sphk } 34092108Sphk if (pp != NULL) { 34192108Sphk sbuf_printf(sb, "%s<index>%u</index>\n", indent, pp->index); 342105540Sphk sbuf_printf(sb, "%s<length>%ju</length>\n", 343105540Sphk indent, (uintmax_t)gsp->slices[pp->index].length); 344105540Sphk sbuf_printf(sb, "%s<seclength>%ju</seclength>\n", indent, 345105540Sphk (uintmax_t)gsp->slices[pp->index].length / 512); 346105540Sphk sbuf_printf(sb, "%s<offset>%ju</offset>\n", indent, 347105540Sphk (uintmax_t)gsp->slices[pp->index].offset); 348105540Sphk sbuf_printf(sb, "%s<secoffset>%ju</secoffset>\n", indent, 349105540Sphk (uintmax_t)gsp->slices[pp->index].offset / 512); 35092108Sphk } 35192108Sphk} 35292108Sphk 353104064Sphkint 354107953Sphkg_slice_config(struct g_geom *gp, u_int idx, int how, off_t offset, off_t length, u_int sectorsize, const char *fmt, ...) 355104064Sphk{ 356110710Sphk struct g_provider *pp, *pp2; 357104064Sphk struct g_slicer *gsp; 358104064Sphk struct g_slice *gsl; 359104064Sphk va_list ap; 360104064Sphk struct sbuf *sb; 361115506Sphk int acc; 362104064Sphk 363104195Sphk g_trace(G_T_TOPOLOGY, "g_slice_config(%s, %d, %d)", 364107953Sphk gp->name, idx, how); 365104064Sphk g_topology_assert(); 366104064Sphk gsp = gp->softc; 367107953Sphk if (idx >= gsp->nslice) 368104064Sphk return(EINVAL); 369107953Sphk gsl = &gsp->slices[idx]; 370104064Sphk pp = gsl->provider; 371104064Sphk if (pp != NULL) 372104064Sphk acc = pp->acr + pp->acw + pp->ace; 373104064Sphk else 374104064Sphk acc = 0; 375104064Sphk if (acc != 0 && how != G_SLICE_CONFIG_FORCE) { 376104064Sphk if (length < gsl->length) 377104064Sphk return(EBUSY); 378104064Sphk if (offset != gsl->offset) 379104064Sphk return(EBUSY); 380104064Sphk } 381104064Sphk /* XXX: check offset + length <= MEDIASIZE */ 382104064Sphk if (how == G_SLICE_CONFIG_CHECK) 383104064Sphk return (0); 384104064Sphk gsl->length = length; 385104064Sphk gsl->offset = offset; 386105542Sphk gsl->sectorsize = sectorsize; 387105957Sphk if (length == 0) { 388105957Sphk if (pp == NULL) 389105957Sphk return (0); 390105957Sphk if (bootverbose) 391105957Sphk printf("GEOM: Deconfigure %s\n", pp->name); 392157619Smarcel g_wither_provider(pp, ENXIO); 393104064Sphk gsl->provider = NULL; 394104064Sphk gsp->nprovider--; 395104064Sphk return (0); 396104064Sphk } 397105957Sphk if (pp != NULL) { 398105957Sphk if (bootverbose) 399105957Sphk printf("GEOM: Reconfigure %s, start %jd length %jd end %jd\n", 400105957Sphk pp->name, (intmax_t)offset, (intmax_t)length, 401105957Sphk (intmax_t)(offset + length - 1)); 402259328Strasz g_resize_provider(pp, gsl->length); 403105957Sphk return (0); 404105957Sphk } 405181463Sdes sb = sbuf_new_auto(); 406104064Sphk va_start(ap, fmt); 407104064Sphk sbuf_vprintf(sb, fmt, ap); 408115949Sphk va_end(ap); 409104064Sphk sbuf_finish(sb); 410243333Sjh pp = g_new_providerf(gp, "%s", sbuf_data(sb)); 411110710Sphk pp2 = LIST_FIRST(&gp->consumer)->provider; 412201645Smav pp->stripesize = pp2->stripesize; 413201645Smav pp->stripeoffset = pp2->stripeoffset + offset; 414201645Smav if (pp->stripesize > 0) 415201645Smav pp->stripeoffset %= pp->stripesize; 416260385Sscottl if (gsp->nhotspot == 0) { 417248722Smav pp->flags |= pp2->flags & G_PF_ACCEPT_UNMAPPED; 418260385Sscottl pp->flags |= G_PF_DIRECT_SEND | G_PF_DIRECT_RECEIVE; 419260385Sscottl } 420137149Sphk if (0 && bootverbose) 421105957Sphk printf("GEOM: Configure %s, start %jd length %jd end %jd\n", 422105957Sphk pp->name, (intmax_t)offset, (intmax_t)length, 423105957Sphk (intmax_t)(offset + length - 1)); 424107953Sphk pp->index = idx; 425105542Sphk pp->mediasize = gsl->length; 426105542Sphk pp->sectorsize = gsl->sectorsize; 427104064Sphk gsl->provider = pp; 428104064Sphk gsp->nprovider++; 429104064Sphk g_error_provider(pp, 0); 430104064Sphk sbuf_delete(sb); 431104064Sphk return(0); 432104064Sphk} 433104064Sphk 434113713Sphk/* 435113713Sphk * Configure "hotspots". A hotspot is a piece of the parent device which 436113713Sphk * this particular slicer cares about for some reason. Typically because 437113713Sphk * it contains meta-data used to configure the slicer. 438113713Sphk * A hotspot is identified by its index number. The offset and length are 439113713Sphk * relative to the parent device, and the three "?act" fields specify 440113713Sphk * what action to take on BIO_READ, BIO_DELETE and BIO_WRITE. 441113713Sphk * 442113713Sphk * XXX: There may be a race relative to g_slice_start() here, if an existing 443113713Sphk * XXX: hotspot is changed wile I/O is happening. Should this become a problem 444113713Sphk * XXX: we can protect the hotspot stuff with a mutex. 445113713Sphk */ 446113713Sphk 447107522Sphkint 448113713Sphkg_slice_conf_hot(struct g_geom *gp, u_int idx, off_t offset, off_t length, int ract, int dact, int wact) 449107522Sphk{ 450107522Sphk struct g_slicer *gsp; 451113712Sphk struct g_slice_hot *gsl, *gsl2; 452260385Sscottl struct g_consumer *cp; 453248722Smav struct g_provider *pp; 454107522Sphk 455113713Sphk g_trace(G_T_TOPOLOGY, "g_slice_conf_hot(%s, idx: %d, off: %jd, len: %jd)", 456113713Sphk gp->name, idx, (intmax_t)offset, (intmax_t)length); 457107522Sphk g_topology_assert(); 458107522Sphk gsp = gp->softc; 459260385Sscottl /* Deny unmapped I/O and direct dispatch if hotspots are used. */ 460248722Smav if (gsp->nhotspot == 0) { 461248722Smav LIST_FOREACH(pp, &gp->provider, provider) 462260385Sscottl pp->flags &= ~(G_PF_ACCEPT_UNMAPPED | 463260385Sscottl G_PF_DIRECT_SEND | G_PF_DIRECT_RECEIVE); 464260385Sscottl LIST_FOREACH(cp, &gp->consumer, consumer) 465260385Sscottl cp->flags &= ~(G_CF_DIRECT_SEND | G_CF_DIRECT_RECEIVE); 466248722Smav } 467113712Sphk gsl = gsp->hotspot; 468113712Sphk if(idx >= gsp->nhotspot) { 469111119Simp gsl2 = g_malloc((idx + 1) * sizeof *gsl2, M_WAITOK | M_ZERO); 470113712Sphk if (gsp->hotspot != NULL) 471113712Sphk bcopy(gsp->hotspot, gsl2, gsp->nhotspot * sizeof *gsl2); 472113712Sphk gsp->hotspot = gsl2; 473113712Sphk if (gsp->hotspot != NULL) 474107522Sphk g_free(gsl); 475107522Sphk gsl = gsl2; 476113712Sphk gsp->nhotspot = idx + 1; 477107522Sphk } 478107953Sphk gsl[idx].offset = offset; 479107953Sphk gsl[idx].length = length; 480113878Sphk KASSERT(!((ract | dact | wact) & G_SLICE_HOT_START) 481113878Sphk || gsp->start != NULL, ("G_SLICE_HOT_START but no slice->start")); 482113878Sphk /* XXX: check that we _have_ a start function if HOT_START specified */ 483113713Sphk gsl[idx].ract = ract; 484113713Sphk gsl[idx].dact = dact; 485113713Sphk gsl[idx].wact = wact; 486107522Sphk return (0); 487107522Sphk} 488107522Sphk 489114504Sphkvoid 490325908Savgg_slice_orphan(struct g_consumer *cp) 491114504Sphk{ 492114504Sphk struct g_geom *gp; 493114504Sphk 494114504Sphk g_topology_assert(); 495114504Sphk gp = cp->geom; 496325908Savg g_trace(G_T_TOPOLOGY, "%s(%p/%s)", __func__, cp, gp->name); 497114504Sphk g_wither_geom(gp, ENXIO); 498325908Savg 499325908Savg /* 500325908Savg * We can safely free the softc now if there are no accesses, 501325908Savg * otherwise g_slice_access() will do that after the last close. 502325908Savg */ 503325908Savg if ((cp->acr + cp->acw + cp->ace) == 0) 504325908Savg g_slice_free(gp); 505114504Sphk} 506114504Sphk 507325908Savgvoid 508325908Savgg_slice_spoiled(struct g_consumer *cp) 509325908Savg{ 510325908Savg 511325908Savg g_trace(G_T_TOPOLOGY, "%s(%p/%s)", __func__, cp, cp->geom->name); 512325908Savg cp->flags |= G_CF_ORPHAN; 513325908Savg g_slice_orphan(cp); 514325908Savg} 515325908Savg 516115506Sphkint 517115506Sphkg_slice_destroy_geom(struct gctl_req *req, struct g_class *mp, struct g_geom *gp) 518115506Sphk{ 519115506Sphk 520115506Sphk g_slice_spoiled(LIST_FIRST(&gp->consumer)); 521115506Sphk return (0); 522115506Sphk} 523115506Sphk 52492108Sphkstruct g_geom * 525107522Sphkg_slice_new(struct g_class *mp, u_int slices, struct g_provider *pp, struct g_consumer **cpp, void *extrap, int extra, g_slice_start_t *start) 52692108Sphk{ 52792108Sphk struct g_geom *gp; 52892108Sphk struct g_slicer *gsp; 52992108Sphk struct g_consumer *cp; 53092108Sphk void **vp; 531113390Sphk int error; 53292108Sphk 53392108Sphk g_topology_assert(); 53492108Sphk vp = (void **)extrap; 53592108Sphk gp = g_new_geomf(mp, "%s", pp->name); 536114493Sphk gsp = g_slice_alloc(slices, extra); 53792108Sphk gsp->start = start; 538325908Savg gp->softc = gsp; 539325908Savg gp->start = g_slice_start; 54093776Sphk gp->access = g_slice_access; 54193776Sphk gp->orphan = g_slice_orphan; 542114504Sphk gp->spoiled = g_slice_spoiled; 543133986Sphk if (gp->dumpconf == NULL) 544133986Sphk gp->dumpconf = g_slice_dumpconf; 545115506Sphk if (gp->class->destroy_geom == NULL) 546115506Sphk gp->class->destroy_geom = g_slice_destroy_geom; 54792108Sphk cp = g_new_consumer(gp); 548260385Sscottl cp->flags |= G_CF_DIRECT_SEND | G_CF_DIRECT_RECEIVE; 549105551Sphk error = g_attach(cp, pp); 550105551Sphk if (error == 0) 551125755Sphk error = g_access(cp, 1, 0, 0); 55292108Sphk if (error) { 553114504Sphk g_wither_geom(gp, ENXIO); 55492108Sphk return (NULL); 55592108Sphk } 556131046Spjd if (extrap != NULL) 557131046Spjd *vp = gsp->softc; 55892108Sphk *cpp = cp; 55992108Sphk return (gp); 56092108Sphk} 561