1dd84a43cSPoul-Henning Kamp /*-
23728855aSPedro F. Giffuni * SPDX-License-Identifier: BSD-3-Clause
33728855aSPedro F. Giffuni *
4dd84a43cSPoul-Henning Kamp * Copyright (c) 2002 Poul-Henning Kamp
5dd84a43cSPoul-Henning Kamp * Copyright (c) 2002 Networks Associates Technology, Inc.
6dd84a43cSPoul-Henning Kamp * All rights reserved.
7dd84a43cSPoul-Henning Kamp *
8dd84a43cSPoul-Henning Kamp * This software was developed for the FreeBSD Project by Poul-Henning Kamp
9dd84a43cSPoul-Henning Kamp * and NAI Labs, the Security Research Division of Network Associates, Inc.
10dd84a43cSPoul-Henning Kamp * under DARPA/SPAWAR contract N66001-01-C-8035 ("CBOSS"), as part of the
11dd84a43cSPoul-Henning Kamp * DARPA CHATS research program.
12dd84a43cSPoul-Henning Kamp *
13dd84a43cSPoul-Henning Kamp * Redistribution and use in source and binary forms, with or without
14dd84a43cSPoul-Henning Kamp * modification, are permitted provided that the following conditions
15dd84a43cSPoul-Henning Kamp * are met:
16dd84a43cSPoul-Henning Kamp * 1. Redistributions of source code must retain the above copyright
17dd84a43cSPoul-Henning Kamp * notice, this list of conditions and the following disclaimer.
18dd84a43cSPoul-Henning Kamp * 2. Redistributions in binary form must reproduce the above copyright
19dd84a43cSPoul-Henning Kamp * notice, this list of conditions and the following disclaimer in the
20dd84a43cSPoul-Henning Kamp * documentation and/or other materials provided with the distribution.
21dd84a43cSPoul-Henning Kamp * 3. The names of the authors may not be used to endorse or promote
22dd84a43cSPoul-Henning Kamp * products derived from this software without specific prior written
23dd84a43cSPoul-Henning Kamp * permission.
24dd84a43cSPoul-Henning Kamp *
25dd84a43cSPoul-Henning Kamp * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
26dd84a43cSPoul-Henning Kamp * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27dd84a43cSPoul-Henning Kamp * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28dd84a43cSPoul-Henning Kamp * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
29dd84a43cSPoul-Henning Kamp * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30dd84a43cSPoul-Henning Kamp * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31dd84a43cSPoul-Henning Kamp * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32dd84a43cSPoul-Henning Kamp * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33dd84a43cSPoul-Henning Kamp * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34dd84a43cSPoul-Henning Kamp * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35dd84a43cSPoul-Henning Kamp * SUCH DAMAGE.
36dd84a43cSPoul-Henning Kamp */
37dd84a43cSPoul-Henning Kamp
38dd84a43cSPoul-Henning Kamp #include <sys/param.h>
39dd84a43cSPoul-Henning Kamp #include <sys/systm.h>
40dd84a43cSPoul-Henning Kamp #include <sys/kernel.h>
41dd84a43cSPoul-Henning Kamp #include <sys/malloc.h>
42dd84a43cSPoul-Henning Kamp #include <sys/bio.h>
43dd84a43cSPoul-Henning Kamp #include <sys/proc.h>
44dd84a43cSPoul-Henning Kamp #include <sys/lock.h>
45dd84a43cSPoul-Henning Kamp #include <sys/mutex.h>
46dd84a43cSPoul-Henning Kamp #include <sys/errno.h>
47dd84a43cSPoul-Henning Kamp #include <sys/sbuf.h>
48dd84a43cSPoul-Henning Kamp #include <geom/geom.h>
49dd84a43cSPoul-Henning Kamp #include <geom/geom_slice.h>
50dd84a43cSPoul-Henning Kamp #include <machine/stdarg.h>
51dd84a43cSPoul-Henning Kamp
5207d77fc6SPoul-Henning Kamp static g_access_t g_slice_access;
5307d77fc6SPoul-Henning Kamp static g_start_t g_slice_start;
5407d77fc6SPoul-Henning Kamp
552285b094SPoul-Henning Kamp static struct g_slicer *
g_slice_alloc(unsigned nslice,unsigned scsize)5682b53b8dSPoul-Henning Kamp g_slice_alloc(unsigned nslice, unsigned scsize)
57dd84a43cSPoul-Henning Kamp {
58dd84a43cSPoul-Henning Kamp struct g_slicer *gsp;
59dd84a43cSPoul-Henning Kamp
60a163d034SWarner Losh gsp = g_malloc(sizeof *gsp, M_WAITOK | M_ZERO);
6140f798daSPawel Jakub Dawidek if (scsize > 0)
62a163d034SWarner Losh gsp->softc = g_malloc(scsize, M_WAITOK | M_ZERO);
6340f798daSPawel Jakub Dawidek else
6440f798daSPawel Jakub Dawidek gsp->softc = NULL;
65beece77aSPoul-Henning Kamp gsp->slices = g_malloc(nslice * sizeof(struct g_slice),
66a163d034SWarner Losh M_WAITOK | M_ZERO);
67dd84a43cSPoul-Henning Kamp gsp->nslice = nslice;
68dd84a43cSPoul-Henning Kamp return (gsp);
69dd84a43cSPoul-Henning Kamp }
70dd84a43cSPoul-Henning Kamp
7182b53b8dSPoul-Henning Kamp static void
g_slice_free(struct g_geom * gp)72f0fa2af6SAndriy Gapon g_slice_free(struct g_geom *gp)
7382b53b8dSPoul-Henning Kamp {
74f0fa2af6SAndriy Gapon struct g_slicer *gsp;
7582b53b8dSPoul-Henning Kamp
76f0fa2af6SAndriy Gapon gsp = gp->softc;
77f0fa2af6SAndriy Gapon gp->softc = NULL;
78f0fa2af6SAndriy Gapon
79f0fa2af6SAndriy Gapon /*
80f0fa2af6SAndriy Gapon * We can get multiple spoiled events before wither-washer
81f0fa2af6SAndriy Gapon * detaches our consumer, so this can get called multiple
82f0fa2af6SAndriy Gapon * times.
83f0fa2af6SAndriy Gapon */
84f0fa2af6SAndriy Gapon if (gsp == NULL)
855289667cSLukas Ertl return;
8682b53b8dSPoul-Henning Kamp g_free(gsp->slices);
8782b53b8dSPoul-Henning Kamp g_free(gsp->hotspot);
8882b53b8dSPoul-Henning Kamp g_free(gsp->softc);
8982b53b8dSPoul-Henning Kamp g_free(gsp);
9082b53b8dSPoul-Henning Kamp }
9182b53b8dSPoul-Henning Kamp
9207d77fc6SPoul-Henning Kamp static int
g_slice_access(struct g_provider * pp,int dr,int dw,int de)93dd84a43cSPoul-Henning Kamp g_slice_access(struct g_provider *pp, int dr, int dw, int de)
94dd84a43cSPoul-Henning Kamp {
950f9d3dbaSPoul-Henning Kamp int error;
960f9d3dbaSPoul-Henning Kamp u_int u;
97dd84a43cSPoul-Henning Kamp struct g_geom *gp;
98dd84a43cSPoul-Henning Kamp struct g_consumer *cp;
99dd84a43cSPoul-Henning Kamp struct g_provider *pp2;
100dd84a43cSPoul-Henning Kamp struct g_slicer *gsp;
101dd84a43cSPoul-Henning Kamp struct g_slice *gsl, *gsl2;
102dd84a43cSPoul-Henning Kamp
103dd84a43cSPoul-Henning Kamp gp = pp->geom;
104dd84a43cSPoul-Henning Kamp cp = LIST_FIRST(&gp->consumer);
105dd84a43cSPoul-Henning Kamp KASSERT (cp != NULL, ("g_slice_access but no consumer"));
106dd84a43cSPoul-Henning Kamp gsp = gp->softc;
1078529ce7aSPoul-Henning Kamp if (dr > 0 || dw > 0 || de > 0) {
108dd84a43cSPoul-Henning Kamp gsl = &gsp->slices[pp->index];
1090f9d3dbaSPoul-Henning Kamp for (u = 0; u < gsp->nslice; u++) {
1100f9d3dbaSPoul-Henning Kamp gsl2 = &gsp->slices[u];
111dd84a43cSPoul-Henning Kamp if (gsl2->length == 0)
112dd84a43cSPoul-Henning Kamp continue;
1130f9d3dbaSPoul-Henning Kamp if (u == pp->index)
114dd84a43cSPoul-Henning Kamp continue;
115dd84a43cSPoul-Henning Kamp if (gsl->offset + gsl->length <= gsl2->offset)
116dd84a43cSPoul-Henning Kamp continue;
117dd84a43cSPoul-Henning Kamp if (gsl2->offset + gsl2->length <= gsl->offset)
118dd84a43cSPoul-Henning Kamp continue;
119dd84a43cSPoul-Henning Kamp /* overlap */
120dd84a43cSPoul-Henning Kamp pp2 = gsl2->provider;
121dd84a43cSPoul-Henning Kamp if ((pp->acw + dw) > 0 && pp2->ace > 0)
122dd84a43cSPoul-Henning Kamp return (EPERM);
123dd84a43cSPoul-Henning Kamp if ((pp->ace + de) > 0 && pp2->acw > 0)
124dd84a43cSPoul-Henning Kamp return (EPERM);
125dd84a43cSPoul-Henning Kamp }
1268529ce7aSPoul-Henning Kamp }
127dd84a43cSPoul-Henning Kamp /* On first open, grab an extra "exclusive" bit */
128dd84a43cSPoul-Henning Kamp if (cp->acr == 0 && cp->acw == 0 && cp->ace == 0)
129dd84a43cSPoul-Henning Kamp de++;
130dd84a43cSPoul-Henning Kamp /* ... and let go of it on last close */
131dd84a43cSPoul-Henning Kamp if ((cp->acr + dr) == 0 && (cp->acw + dw) == 0 && (cp->ace + de) == 1)
132dd84a43cSPoul-Henning Kamp de--;
133d2bae332SPoul-Henning Kamp error = g_access(cp, dr, dw, de);
1349662d80aSAndriy Gapon
1359662d80aSAndriy Gapon /*
1369662d80aSAndriy Gapon * Free the softc if all providers have been closed and this geom
1379662d80aSAndriy Gapon * is being removed.
1389662d80aSAndriy Gapon */
1399662d80aSAndriy Gapon if (error == 0 && (gp->flags & G_GEOM_WITHER) != 0 &&
1409662d80aSAndriy Gapon (cp->acr + cp->acw + cp->ace) == 0)
141f0fa2af6SAndriy Gapon g_slice_free(gp);
1429662d80aSAndriy Gapon
143dd84a43cSPoul-Henning Kamp return (error);
144dd84a43cSPoul-Henning Kamp }
145dd84a43cSPoul-Henning Kamp
1467220a9e7SPoul-Henning Kamp /*
1477220a9e7SPoul-Henning Kamp * XXX: It should be possible to specify here if we should finish all of the
1487220a9e7SPoul-Henning Kamp * XXX: bio, or only the non-hot bits. This would get messy if there were
1497220a9e7SPoul-Henning Kamp * XXX: two hot spots in the same bio, so for now we simply finish off the
1507220a9e7SPoul-Henning Kamp * XXX: entire bio. Modifying hot data on the way to disk is frowned on
1517220a9e7SPoul-Henning Kamp * XXX: so making that considerably harder is not a bad idea anyway.
1527220a9e7SPoul-Henning Kamp */
153188321b7SPoul-Henning Kamp void
g_slice_finish_hot(struct bio * bp)154188321b7SPoul-Henning Kamp g_slice_finish_hot(struct bio *bp)
155188321b7SPoul-Henning Kamp {
156188321b7SPoul-Henning Kamp struct bio *bp2;
157188321b7SPoul-Henning Kamp struct g_geom *gp;
158188321b7SPoul-Henning Kamp struct g_consumer *cp;
159188321b7SPoul-Henning Kamp struct g_slicer *gsp;
160188321b7SPoul-Henning Kamp struct g_slice *gsl;
1610f9d3dbaSPoul-Henning Kamp int idx;
162188321b7SPoul-Henning Kamp
1637220a9e7SPoul-Henning Kamp KASSERT(bp->bio_to != NULL,
1647220a9e7SPoul-Henning Kamp ("NULL bio_to in g_slice_finish_hot(%p)", bp));
1657220a9e7SPoul-Henning Kamp KASSERT(bp->bio_from != NULL,
1667220a9e7SPoul-Henning Kamp ("NULL bio_from in g_slice_finish_hot(%p)", bp));
167188321b7SPoul-Henning Kamp gp = bp->bio_to->geom;
168188321b7SPoul-Henning Kamp gsp = gp->softc;
169188321b7SPoul-Henning Kamp cp = LIST_FIRST(&gp->consumer);
170188321b7SPoul-Henning Kamp KASSERT(cp != NULL, ("NULL consumer in g_slice_finish_hot(%p)", bp));
1710f9d3dbaSPoul-Henning Kamp idx = bp->bio_to->index;
1720f9d3dbaSPoul-Henning Kamp gsl = &gsp->slices[idx];
173188321b7SPoul-Henning Kamp
174188321b7SPoul-Henning Kamp bp2 = g_clone_bio(bp);
175188321b7SPoul-Henning Kamp if (bp2 == NULL) {
176188321b7SPoul-Henning Kamp g_io_deliver(bp, ENOMEM);
177188321b7SPoul-Henning Kamp return;
178188321b7SPoul-Henning Kamp }
179188321b7SPoul-Henning Kamp if (bp2->bio_offset + bp2->bio_length > gsl->length)
180188321b7SPoul-Henning Kamp bp2->bio_length = gsl->length - bp2->bio_offset;
181188321b7SPoul-Henning Kamp bp2->bio_done = g_std_done;
182188321b7SPoul-Henning Kamp bp2->bio_offset += gsl->offset;
183188321b7SPoul-Henning Kamp g_io_request(bp2, cp);
184188321b7SPoul-Henning Kamp return;
185188321b7SPoul-Henning Kamp }
186188321b7SPoul-Henning Kamp
18707d77fc6SPoul-Henning Kamp static void
g_slice_done(struct bio * bp)188a04c28bdSPawel Jakub Dawidek g_slice_done(struct bio *bp)
189a04c28bdSPawel Jakub Dawidek {
190a04c28bdSPawel Jakub Dawidek
191a04c28bdSPawel Jakub Dawidek KASSERT(bp->bio_cmd == BIO_GETATTR &&
192a04c28bdSPawel Jakub Dawidek strcmp(bp->bio_attribute, "GEOM::ident") == 0,
193a04c28bdSPawel Jakub Dawidek ("bio_cmd=0x%x bio_attribute=%s", bp->bio_cmd, bp->bio_attribute));
194a04c28bdSPawel Jakub Dawidek
195a04c28bdSPawel Jakub Dawidek if (bp->bio_error == 0 && bp->bio_data[0] != '\0') {
196a04c28bdSPawel Jakub Dawidek char idx[8];
197a04c28bdSPawel Jakub Dawidek
198a04c28bdSPawel Jakub Dawidek /* Add index to the ident received. */
199a04c28bdSPawel Jakub Dawidek snprintf(idx, sizeof(idx), "s%d",
200a04c28bdSPawel Jakub Dawidek bp->bio_parent->bio_to->index);
201a04c28bdSPawel Jakub Dawidek if (strlcat(bp->bio_data, idx, bp->bio_length) >=
202a04c28bdSPawel Jakub Dawidek bp->bio_length) {
203a04c28bdSPawel Jakub Dawidek bp->bio_error = EFAULT;
204a04c28bdSPawel Jakub Dawidek }
205a04c28bdSPawel Jakub Dawidek }
206a04c28bdSPawel Jakub Dawidek g_std_done(bp);
207a04c28bdSPawel Jakub Dawidek }
208a04c28bdSPawel Jakub Dawidek
209a04c28bdSPawel Jakub Dawidek static void
g_slice_start(struct bio * bp)210dd84a43cSPoul-Henning Kamp g_slice_start(struct bio *bp)
211dd84a43cSPoul-Henning Kamp {
212dd84a43cSPoul-Henning Kamp struct bio *bp2;
213dd84a43cSPoul-Henning Kamp struct g_provider *pp;
214dd84a43cSPoul-Henning Kamp struct g_geom *gp;
215dd84a43cSPoul-Henning Kamp struct g_consumer *cp;
216dd84a43cSPoul-Henning Kamp struct g_slicer *gsp;
217183a45f6SPoul-Henning Kamp struct g_slice *gsl;
2187220a9e7SPoul-Henning Kamp struct g_slice_hot *ghp;
2190f9d3dbaSPoul-Henning Kamp int idx, error;
220188321b7SPoul-Henning Kamp u_int m_index;
2211bdb20a6SPoul-Henning Kamp off_t t;
222dd84a43cSPoul-Henning Kamp
223dd84a43cSPoul-Henning Kamp pp = bp->bio_to;
224dd84a43cSPoul-Henning Kamp gp = pp->geom;
225dd84a43cSPoul-Henning Kamp gsp = gp->softc;
226dd84a43cSPoul-Henning Kamp cp = LIST_FIRST(&gp->consumer);
2270f9d3dbaSPoul-Henning Kamp idx = pp->index;
2280f9d3dbaSPoul-Henning Kamp gsl = &gsp->slices[idx];
229dd84a43cSPoul-Henning Kamp switch(bp->bio_cmd) {
230dd84a43cSPoul-Henning Kamp case BIO_READ:
231dd84a43cSPoul-Henning Kamp case BIO_WRITE:
232dd84a43cSPoul-Henning Kamp case BIO_DELETE:
233dd84a43cSPoul-Henning Kamp if (bp->bio_offset > gsl->length) {
23472840432SPoul-Henning Kamp g_io_deliver(bp, EINVAL); /* XXX: EWHAT ? */
235dd84a43cSPoul-Henning Kamp return;
236dd84a43cSPoul-Henning Kamp }
237188321b7SPoul-Henning Kamp /*
238188321b7SPoul-Henning Kamp * Check if we collide with any hot spaces, and call the
239188321b7SPoul-Henning Kamp * method once if so.
240188321b7SPoul-Henning Kamp */
241821a4d01SPoul-Henning Kamp t = bp->bio_offset + gsl->offset;
242183a45f6SPoul-Henning Kamp for (m_index = 0; m_index < gsp->nhotspot; m_index++) {
2437220a9e7SPoul-Henning Kamp ghp = &gsp->hotspot[m_index];
2447220a9e7SPoul-Henning Kamp if (t >= ghp->offset + ghp->length)
245188321b7SPoul-Henning Kamp continue;
2467220a9e7SPoul-Henning Kamp if (t + bp->bio_length <= ghp->offset)
247188321b7SPoul-Henning Kamp continue;
2487220a9e7SPoul-Henning Kamp switch(bp->bio_cmd) {
2497220a9e7SPoul-Henning Kamp case BIO_READ: idx = ghp->ract; break;
2507220a9e7SPoul-Henning Kamp case BIO_WRITE: idx = ghp->wact; break;
2517220a9e7SPoul-Henning Kamp case BIO_DELETE: idx = ghp->dact; break;
2527220a9e7SPoul-Henning Kamp }
2537220a9e7SPoul-Henning Kamp switch(idx) {
2547220a9e7SPoul-Henning Kamp case G_SLICE_HOT_ALLOW:
2557220a9e7SPoul-Henning Kamp /* Fall out and continue normal processing */
2567220a9e7SPoul-Henning Kamp continue;
2577220a9e7SPoul-Henning Kamp case G_SLICE_HOT_DENY:
2587220a9e7SPoul-Henning Kamp g_io_deliver(bp, EROFS);
259188321b7SPoul-Henning Kamp return;
2607220a9e7SPoul-Henning Kamp case G_SLICE_HOT_START:
2617220a9e7SPoul-Henning Kamp error = gsp->start(bp);
2627220a9e7SPoul-Henning Kamp if (error && error != EJUSTRETURN)
2637220a9e7SPoul-Henning Kamp g_io_deliver(bp, error);
2647220a9e7SPoul-Henning Kamp return;
2657220a9e7SPoul-Henning Kamp case G_SLICE_HOT_CALL:
2668cd1535aSPoul-Henning Kamp error = g_post_event(gsp->hot, bp, M_NOWAIT,
2678cd1535aSPoul-Henning Kamp gp, NULL);
2687220a9e7SPoul-Henning Kamp if (error)
269188321b7SPoul-Henning Kamp g_io_deliver(bp, error);
270188321b7SPoul-Henning Kamp return;
271188321b7SPoul-Henning Kamp }
272188321b7SPoul-Henning Kamp break;
273188321b7SPoul-Henning Kamp }
274dd84a43cSPoul-Henning Kamp bp2 = g_clone_bio(bp);
275beece77aSPoul-Henning Kamp if (bp2 == NULL) {
27672840432SPoul-Henning Kamp g_io_deliver(bp, ENOMEM);
277beece77aSPoul-Henning Kamp return;
278beece77aSPoul-Henning Kamp }
279dd84a43cSPoul-Henning Kamp if (bp2->bio_offset + bp2->bio_length > gsl->length)
280dd84a43cSPoul-Henning Kamp bp2->bio_length = gsl->length - bp2->bio_offset;
281dd84a43cSPoul-Henning Kamp bp2->bio_done = g_std_done;
282dd84a43cSPoul-Henning Kamp bp2->bio_offset += gsl->offset;
283dd84a43cSPoul-Henning Kamp g_io_request(bp2, cp);
284dd84a43cSPoul-Henning Kamp return;
285dd84a43cSPoul-Henning Kamp case BIO_GETATTR:
2861bdb20a6SPoul-Henning Kamp /* Give the real method a chance to override */
287d3a1a137SPoul-Henning Kamp if (gsp->start != NULL && gsp->start(bp))
2881bdb20a6SPoul-Henning Kamp return;
289a04c28bdSPawel Jakub Dawidek if (!strcmp("GEOM::ident", bp->bio_attribute)) {
290a04c28bdSPawel Jakub Dawidek bp2 = g_clone_bio(bp);
291a04c28bdSPawel Jakub Dawidek if (bp2 == NULL) {
292a04c28bdSPawel Jakub Dawidek g_io_deliver(bp, ENOMEM);
293a04c28bdSPawel Jakub Dawidek return;
294a04c28bdSPawel Jakub Dawidek }
295a04c28bdSPawel Jakub Dawidek bp2->bio_done = g_slice_done;
296a04c28bdSPawel Jakub Dawidek g_io_request(bp2, cp);
297a04c28bdSPawel Jakub Dawidek return;
298a04c28bdSPawel Jakub Dawidek }
29995c24b31SPoul-Henning Kamp if (!strcmp("GEOM::kerneldump", bp->bio_attribute)) {
30095c24b31SPoul-Henning Kamp struct g_kerneldump *gkd;
30195c24b31SPoul-Henning Kamp
30295c24b31SPoul-Henning Kamp gkd = (struct g_kerneldump *)bp->bio_data;
3030f9d3dbaSPoul-Henning Kamp gkd->offset += gsp->slices[idx].offset;
3040f9d3dbaSPoul-Henning Kamp if (gkd->length > gsp->slices[idx].length)
3050f9d3dbaSPoul-Henning Kamp gkd->length = gsp->slices[idx].length;
30695c24b31SPoul-Henning Kamp /* now, pass it on downwards... */
30795c24b31SPoul-Henning Kamp }
30842461fbaSPawel Jakub Dawidek /* FALLTHROUGH */
309*8b522bdaSWarner Losh case BIO_SPEEDUP:
31042461fbaSPawel Jakub Dawidek case BIO_FLUSH:
311dd84a43cSPoul-Henning Kamp bp2 = g_clone_bio(bp);
312852f3924SPoul-Henning Kamp if (bp2 == NULL) {
31372840432SPoul-Henning Kamp g_io_deliver(bp, ENOMEM);
314852f3924SPoul-Henning Kamp return;
315852f3924SPoul-Henning Kamp }
316dd84a43cSPoul-Henning Kamp bp2->bio_done = g_std_done;
317dd84a43cSPoul-Henning Kamp g_io_request(bp2, cp);
318dd84a43cSPoul-Henning Kamp break;
319dd84a43cSPoul-Henning Kamp default:
32072840432SPoul-Henning Kamp g_io_deliver(bp, EOPNOTSUPP);
321dd84a43cSPoul-Henning Kamp return;
322dd84a43cSPoul-Henning Kamp }
323dd84a43cSPoul-Henning Kamp }
324dd84a43cSPoul-Henning Kamp
325dd84a43cSPoul-Henning Kamp void
g_slice_dumpconf(struct sbuf * sb,const char * indent,struct g_geom * gp,struct g_consumer * cp,struct g_provider * pp)3260f9d3dbaSPoul-Henning Kamp g_slice_dumpconf(struct sbuf *sb, const char *indent, struct g_geom *gp, struct g_consumer *cp, struct g_provider *pp)
327dd84a43cSPoul-Henning Kamp {
328dd84a43cSPoul-Henning Kamp struct g_slicer *gsp;
329dd84a43cSPoul-Henning Kamp
330dd84a43cSPoul-Henning Kamp gsp = gp->softc;
331d518e539SPoul-Henning Kamp if (indent == NULL) {
332d518e539SPoul-Henning Kamp sbuf_printf(sb, " i %u", pp->index);
333d518e539SPoul-Henning Kamp sbuf_printf(sb, " o %ju",
334d518e539SPoul-Henning Kamp (uintmax_t)gsp->slices[pp->index].offset);
335d518e539SPoul-Henning Kamp return;
336d518e539SPoul-Henning Kamp }
337dd84a43cSPoul-Henning Kamp if (pp != NULL) {
338dd84a43cSPoul-Henning Kamp sbuf_printf(sb, "%s<index>%u</index>\n", indent, pp->index);
33914ac6812SPoul-Henning Kamp sbuf_printf(sb, "%s<length>%ju</length>\n",
34014ac6812SPoul-Henning Kamp indent, (uintmax_t)gsp->slices[pp->index].length);
34114ac6812SPoul-Henning Kamp sbuf_printf(sb, "%s<seclength>%ju</seclength>\n", indent,
34214ac6812SPoul-Henning Kamp (uintmax_t)gsp->slices[pp->index].length / 512);
34314ac6812SPoul-Henning Kamp sbuf_printf(sb, "%s<offset>%ju</offset>\n", indent,
34414ac6812SPoul-Henning Kamp (uintmax_t)gsp->slices[pp->index].offset);
34514ac6812SPoul-Henning Kamp sbuf_printf(sb, "%s<secoffset>%ju</secoffset>\n", indent,
34614ac6812SPoul-Henning Kamp (uintmax_t)gsp->slices[pp->index].offset / 512);
347dd84a43cSPoul-Henning Kamp }
348dd84a43cSPoul-Henning Kamp }
349dd84a43cSPoul-Henning Kamp
3505a6baf54SPoul-Henning Kamp int
g_slice_config(struct g_geom * gp,u_int idx,int how,off_t offset,off_t length,u_int sectorsize,const char * fmt,...)3510f9d3dbaSPoul-Henning Kamp g_slice_config(struct g_geom *gp, u_int idx, int how, off_t offset, off_t length, u_int sectorsize, const char *fmt, ...)
3525a6baf54SPoul-Henning Kamp {
3538a63edc3SPoul-Henning Kamp struct g_provider *pp, *pp2;
3545a6baf54SPoul-Henning Kamp struct g_slicer *gsp;
3555a6baf54SPoul-Henning Kamp struct g_slice *gsl;
3565a6baf54SPoul-Henning Kamp va_list ap;
3575a6baf54SPoul-Henning Kamp struct sbuf *sb;
358ce67c955SPoul-Henning Kamp int acc;
3595a6baf54SPoul-Henning Kamp
36072840432SPoul-Henning Kamp g_trace(G_T_TOPOLOGY, "g_slice_config(%s, %d, %d)",
3610f9d3dbaSPoul-Henning Kamp gp->name, idx, how);
3625a6baf54SPoul-Henning Kamp g_topology_assert();
3635a6baf54SPoul-Henning Kamp gsp = gp->softc;
3640f9d3dbaSPoul-Henning Kamp if (idx >= gsp->nslice)
3655a6baf54SPoul-Henning Kamp return(EINVAL);
3660f9d3dbaSPoul-Henning Kamp gsl = &gsp->slices[idx];
3675a6baf54SPoul-Henning Kamp pp = gsl->provider;
3685a6baf54SPoul-Henning Kamp if (pp != NULL)
3695a6baf54SPoul-Henning Kamp acc = pp->acr + pp->acw + pp->ace;
3705a6baf54SPoul-Henning Kamp else
3715a6baf54SPoul-Henning Kamp acc = 0;
3725a6baf54SPoul-Henning Kamp if (acc != 0 && how != G_SLICE_CONFIG_FORCE) {
3735a6baf54SPoul-Henning Kamp if (length < gsl->length)
3745a6baf54SPoul-Henning Kamp return(EBUSY);
3755a6baf54SPoul-Henning Kamp if (offset != gsl->offset)
3765a6baf54SPoul-Henning Kamp return(EBUSY);
3775a6baf54SPoul-Henning Kamp }
3785a6baf54SPoul-Henning Kamp /* XXX: check offset + length <= MEDIASIZE */
3795a6baf54SPoul-Henning Kamp if (how == G_SLICE_CONFIG_CHECK)
3805a6baf54SPoul-Henning Kamp return (0);
3815a6baf54SPoul-Henning Kamp gsl->length = length;
3825a6baf54SPoul-Henning Kamp gsl->offset = offset;
38348444d62SPoul-Henning Kamp gsl->sectorsize = sectorsize;
3843d5500fcSPoul-Henning Kamp if (length == 0) {
3853d5500fcSPoul-Henning Kamp if (pp == NULL)
3865a6baf54SPoul-Henning Kamp return (0);
3873d5500fcSPoul-Henning Kamp if (bootverbose)
3883d5500fcSPoul-Henning Kamp printf("GEOM: Deconfigure %s\n", pp->name);
389d99c1559SMarcel Moolenaar g_wither_provider(pp, ENXIO);
3905a6baf54SPoul-Henning Kamp gsl->provider = NULL;
3915a6baf54SPoul-Henning Kamp gsp->nprovider--;
3925a6baf54SPoul-Henning Kamp return (0);
3935a6baf54SPoul-Henning Kamp }
3943d5500fcSPoul-Henning Kamp if (pp != NULL) {
3953d5500fcSPoul-Henning Kamp if (bootverbose)
3963d5500fcSPoul-Henning Kamp printf("GEOM: Reconfigure %s, start %jd length %jd end %jd\n",
3973d5500fcSPoul-Henning Kamp pp->name, (intmax_t)offset, (intmax_t)length,
3983d5500fcSPoul-Henning Kamp (intmax_t)(offset + length - 1));
39919e5b2d5SEdward Tomasz Napierala g_resize_provider(pp, gsl->length);
4003d5500fcSPoul-Henning Kamp return (0);
4013d5500fcSPoul-Henning Kamp }
4022616144eSDag-Erling Smørgrav sb = sbuf_new_auto();
403a1a9b445SPoul-Henning Kamp va_start(ap, fmt);
4045a6baf54SPoul-Henning Kamp sbuf_vprintf(sb, fmt, ap);
405a1a9b445SPoul-Henning Kamp va_end(ap);
4065a6baf54SPoul-Henning Kamp sbuf_finish(sb);
40702c62349SJaakko Heinonen pp = g_new_providerf(gp, "%s", sbuf_data(sb));
4088a63edc3SPoul-Henning Kamp pp2 = LIST_FIRST(&gp->consumer)->provider;
4098a63edc3SPoul-Henning Kamp pp->stripesize = pp2->stripesize;
4100c8fd0c8SAlexander Motin pp->stripeoffset = pp2->stripeoffset + offset;
4110c8fd0c8SAlexander Motin if (pp->stripesize > 0)
4120c8fd0c8SAlexander Motin pp->stripeoffset %= pp->stripesize;
41340ea77a0SAlexander Motin if (gsp->nhotspot == 0) {
4147868ec50SAlexander Motin pp->flags |= pp2->flags & G_PF_ACCEPT_UNMAPPED;
41540ea77a0SAlexander Motin pp->flags |= G_PF_DIRECT_SEND | G_PF_DIRECT_RECEIVE;
41640ea77a0SAlexander Motin }
4172859a695SPoul-Henning Kamp if (0 && bootverbose)
4183d5500fcSPoul-Henning Kamp printf("GEOM: Configure %s, start %jd length %jd end %jd\n",
4193d5500fcSPoul-Henning Kamp pp->name, (intmax_t)offset, (intmax_t)length,
4203d5500fcSPoul-Henning Kamp (intmax_t)(offset + length - 1));
4210f9d3dbaSPoul-Henning Kamp pp->index = idx;
42248444d62SPoul-Henning Kamp pp->mediasize = gsl->length;
42348444d62SPoul-Henning Kamp pp->sectorsize = gsl->sectorsize;
4245a6baf54SPoul-Henning Kamp gsl->provider = pp;
4255a6baf54SPoul-Henning Kamp gsp->nprovider++;
4265a6baf54SPoul-Henning Kamp g_error_provider(pp, 0);
4275a6baf54SPoul-Henning Kamp sbuf_delete(sb);
4285a6baf54SPoul-Henning Kamp return(0);
4295a6baf54SPoul-Henning Kamp }
4305a6baf54SPoul-Henning Kamp
4317220a9e7SPoul-Henning Kamp /*
4327220a9e7SPoul-Henning Kamp * Configure "hotspots". A hotspot is a piece of the parent device which
4337220a9e7SPoul-Henning Kamp * this particular slicer cares about for some reason. Typically because
4347220a9e7SPoul-Henning Kamp * it contains meta-data used to configure the slicer.
4357220a9e7SPoul-Henning Kamp * A hotspot is identified by its index number. The offset and length are
4367220a9e7SPoul-Henning Kamp * relative to the parent device, and the three "?act" fields specify
4377220a9e7SPoul-Henning Kamp * what action to take on BIO_READ, BIO_DELETE and BIO_WRITE.
4387220a9e7SPoul-Henning Kamp *
4397220a9e7SPoul-Henning Kamp * XXX: There may be a race relative to g_slice_start() here, if an existing
4407220a9e7SPoul-Henning Kamp * XXX: hotspot is changed wile I/O is happening. Should this become a problem
4417220a9e7SPoul-Henning Kamp * XXX: we can protect the hotspot stuff with a mutex.
4427220a9e7SPoul-Henning Kamp */
4437220a9e7SPoul-Henning Kamp
444188321b7SPoul-Henning Kamp int
g_slice_conf_hot(struct g_geom * gp,u_int idx,off_t offset,off_t length,int ract,int dact,int wact)4457220a9e7SPoul-Henning Kamp g_slice_conf_hot(struct g_geom *gp, u_int idx, off_t offset, off_t length, int ract, int dact, int wact)
446188321b7SPoul-Henning Kamp {
447188321b7SPoul-Henning Kamp struct g_slicer *gsp;
448183a45f6SPoul-Henning Kamp struct g_slice_hot *gsl, *gsl2;
44940ea77a0SAlexander Motin struct g_consumer *cp;
4507868ec50SAlexander Motin struct g_provider *pp;
451188321b7SPoul-Henning Kamp
4527220a9e7SPoul-Henning Kamp g_trace(G_T_TOPOLOGY, "g_slice_conf_hot(%s, idx: %d, off: %jd, len: %jd)",
4537220a9e7SPoul-Henning Kamp gp->name, idx, (intmax_t)offset, (intmax_t)length);
454188321b7SPoul-Henning Kamp g_topology_assert();
455188321b7SPoul-Henning Kamp gsp = gp->softc;
45640ea77a0SAlexander Motin /* Deny unmapped I/O and direct dispatch if hotspots are used. */
4577868ec50SAlexander Motin if (gsp->nhotspot == 0) {
4587868ec50SAlexander Motin LIST_FOREACH(pp, &gp->provider, provider)
45940ea77a0SAlexander Motin pp->flags &= ~(G_PF_ACCEPT_UNMAPPED |
46040ea77a0SAlexander Motin G_PF_DIRECT_SEND | G_PF_DIRECT_RECEIVE);
46140ea77a0SAlexander Motin LIST_FOREACH(cp, &gp->consumer, consumer)
46240ea77a0SAlexander Motin cp->flags &= ~(G_CF_DIRECT_SEND | G_CF_DIRECT_RECEIVE);
4637868ec50SAlexander Motin }
464183a45f6SPoul-Henning Kamp gsl = gsp->hotspot;
465183a45f6SPoul-Henning Kamp if(idx >= gsp->nhotspot) {
466a163d034SWarner Losh gsl2 = g_malloc((idx + 1) * sizeof *gsl2, M_WAITOK | M_ZERO);
467183a45f6SPoul-Henning Kamp if (gsp->hotspot != NULL)
468183a45f6SPoul-Henning Kamp bcopy(gsp->hotspot, gsl2, gsp->nhotspot * sizeof *gsl2);
469183a45f6SPoul-Henning Kamp gsp->hotspot = gsl2;
470183a45f6SPoul-Henning Kamp if (gsp->hotspot != NULL)
471188321b7SPoul-Henning Kamp g_free(gsl);
472188321b7SPoul-Henning Kamp gsl = gsl2;
473183a45f6SPoul-Henning Kamp gsp->nhotspot = idx + 1;
474188321b7SPoul-Henning Kamp }
4750f9d3dbaSPoul-Henning Kamp gsl[idx].offset = offset;
4760f9d3dbaSPoul-Henning Kamp gsl[idx].length = length;
477d3a1a137SPoul-Henning Kamp KASSERT(!((ract | dact | wact) & G_SLICE_HOT_START)
478d3a1a137SPoul-Henning Kamp || gsp->start != NULL, ("G_SLICE_HOT_START but no slice->start"));
479d3a1a137SPoul-Henning Kamp /* XXX: check that we _have_ a start function if HOT_START specified */
4807220a9e7SPoul-Henning Kamp gsl[idx].ract = ract;
4817220a9e7SPoul-Henning Kamp gsl[idx].dact = dact;
4827220a9e7SPoul-Henning Kamp gsl[idx].wact = wact;
483188321b7SPoul-Henning Kamp return (0);
484188321b7SPoul-Henning Kamp }
485188321b7SPoul-Henning Kamp
48615649213SPoul-Henning Kamp void
g_slice_orphan(struct g_consumer * cp)4879662d80aSAndriy Gapon g_slice_orphan(struct g_consumer *cp)
48815649213SPoul-Henning Kamp {
48915649213SPoul-Henning Kamp struct g_geom *gp;
49015649213SPoul-Henning Kamp
49115649213SPoul-Henning Kamp g_topology_assert();
49215649213SPoul-Henning Kamp gp = cp->geom;
4939662d80aSAndriy Gapon g_trace(G_T_TOPOLOGY, "%s(%p/%s)", __func__, cp, gp->name);
49415649213SPoul-Henning Kamp g_wither_geom(gp, ENXIO);
4959662d80aSAndriy Gapon
4969662d80aSAndriy Gapon /*
4979662d80aSAndriy Gapon * We can safely free the softc now if there are no accesses,
4989662d80aSAndriy Gapon * otherwise g_slice_access() will do that after the last close.
4999662d80aSAndriy Gapon */
5009662d80aSAndriy Gapon if ((cp->acr + cp->acw + cp->ace) == 0)
501f0fa2af6SAndriy Gapon g_slice_free(gp);
5029662d80aSAndriy Gapon }
5039662d80aSAndriy Gapon
5049662d80aSAndriy Gapon void
g_slice_spoiled(struct g_consumer * cp)5059662d80aSAndriy Gapon g_slice_spoiled(struct g_consumer *cp)
5069662d80aSAndriy Gapon {
5079662d80aSAndriy Gapon
5089662d80aSAndriy Gapon g_trace(G_T_TOPOLOGY, "%s(%p/%s)", __func__, cp, cp->geom->name);
5099662d80aSAndriy Gapon cp->flags |= G_CF_ORPHAN;
5109662d80aSAndriy Gapon g_slice_orphan(cp);
51115649213SPoul-Henning Kamp }
51215649213SPoul-Henning Kamp
513ce67c955SPoul-Henning Kamp int
g_slice_destroy_geom(struct gctl_req * req,struct g_class * mp,struct g_geom * gp)514ce67c955SPoul-Henning Kamp g_slice_destroy_geom(struct gctl_req *req, struct g_class *mp, struct g_geom *gp)
515ce67c955SPoul-Henning Kamp {
516ce67c955SPoul-Henning Kamp
517ce67c955SPoul-Henning Kamp g_slice_spoiled(LIST_FIRST(&gp->consumer));
518ce67c955SPoul-Henning Kamp return (0);
519ce67c955SPoul-Henning Kamp }
520ce67c955SPoul-Henning Kamp
521dd84a43cSPoul-Henning Kamp struct g_geom *
g_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)522188321b7SPoul-Henning Kamp g_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)
523dd84a43cSPoul-Henning Kamp {
524dd84a43cSPoul-Henning Kamp struct g_geom *gp;
525dd84a43cSPoul-Henning Kamp struct g_slicer *gsp;
526dd84a43cSPoul-Henning Kamp struct g_consumer *cp;
527dd84a43cSPoul-Henning Kamp void **vp;
5280d3e96e3SPoul-Henning Kamp int error;
529dd84a43cSPoul-Henning Kamp
530dd84a43cSPoul-Henning Kamp g_topology_assert();
531dd84a43cSPoul-Henning Kamp vp = (void **)extrap;
532dd84a43cSPoul-Henning Kamp gp = g_new_geomf(mp, "%s", pp->name);
53382b53b8dSPoul-Henning Kamp gsp = g_slice_alloc(slices, extra);
534dd84a43cSPoul-Henning Kamp gsp->start = start;
535dd84a43cSPoul-Henning Kamp gp->softc = gsp;
536dd84a43cSPoul-Henning Kamp gp->start = g_slice_start;
5379662d80aSAndriy Gapon gp->access = g_slice_access;
5389662d80aSAndriy Gapon gp->orphan = g_slice_orphan;
53915649213SPoul-Henning Kamp gp->spoiled = g_slice_spoiled;
540a9654c8cSPoul-Henning Kamp if (gp->dumpconf == NULL)
541dd84a43cSPoul-Henning Kamp gp->dumpconf = g_slice_dumpconf;
542ce67c955SPoul-Henning Kamp if (gp->class->destroy_geom == NULL)
543ce67c955SPoul-Henning Kamp gp->class->destroy_geom = g_slice_destroy_geom;
544dd84a43cSPoul-Henning Kamp cp = g_new_consumer(gp);
54540ea77a0SAlexander Motin cp->flags |= G_CF_DIRECT_SEND | G_CF_DIRECT_RECEIVE;
5463f12caa1SPoul-Henning Kamp error = g_attach(cp, pp);
5473f12caa1SPoul-Henning Kamp if (error == 0)
548d2bae332SPoul-Henning Kamp error = g_access(cp, 1, 0, 0);
549dd84a43cSPoul-Henning Kamp if (error) {
55015649213SPoul-Henning Kamp g_wither_geom(gp, ENXIO);
551dd84a43cSPoul-Henning Kamp return (NULL);
552dd84a43cSPoul-Henning Kamp }
55340f798daSPawel Jakub Dawidek if (extrap != NULL)
554dd84a43cSPoul-Henning Kamp *vp = gsp->softc;
555dd84a43cSPoul-Henning Kamp *cpp = cp;
556dd84a43cSPoul-Henning Kamp return (gp);
557dd84a43cSPoul-Henning Kamp }
558