1/*-
2 * Copyright (c) 2002 Poul-Henning Kamp
3 * Copyright (c) 2002 Networks Associates Technology, Inc.
4 * All rights reserved.
5 *
6 * This software was developed for the FreeBSD Project by Poul-Henning Kamp
7 * and NAI Labs, the Security Research Division of Network Associates, Inc.
8 * under DARPA/SPAWAR contract N66001-01-C-8035 ("CBOSS"), as part of the
9 * DARPA CHATS research program.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. The names of the authors may not be used to endorse or promote
20 * products derived from this software without specific prior written
21 * permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * SUCH DAMAGE.
34 *
35 * $FreeBSD: head/sys/geom/geom_slice.c 110696 2003-02-11 12:36:33Z phk $
36 */
37
38
39#include <sys/param.h>
40#include <sys/stdint.h>
41#ifndef _KERNEL
42#include <stdio.h>
43#include <unistd.h>
44#include <stdlib.h>
45#include <signal.h>
46#include <string.h>
47#include <err.h>
48#else
49#include <sys/systm.h>
50#include <sys/kernel.h>
51#include <sys/malloc.h>
52#include <sys/bio.h>
53#include <sys/sysctl.h>
54#include <sys/proc.h>
55#include <sys/kthread.h>
56#include <sys/lock.h>
57#include <sys/mutex.h>
58#endif
59#include <sys/errno.h>
60#include <sys/sbuf.h>
61#include <geom/geom.h>
62#include <geom/geom_slice.h>
63#include <machine/stdarg.h>
64
65static g_orphan_t g_slice_orphan;
66static g_access_t g_slice_access;
67static g_start_t g_slice_start;
68
69static struct g_slicer *
70g_slice_init(unsigned nslice, unsigned scsize)
71{
72 struct g_slicer *gsp;
73
74 gsp = g_malloc(sizeof *gsp, M_ZERO);
75 gsp->softc = g_malloc(scsize, M_ZERO);
76 gsp->slices = g_malloc(nslice * sizeof(struct g_slice),
77 M_ZERO);
78 gsp->nslice = nslice;
79 return (gsp);
80}
81
82static int
83g_slice_access(struct g_provider *pp, int dr, int dw, int de)
84{
85 int error;
86 u_int u;
87 struct g_geom *gp;
88 struct g_consumer *cp;
89 struct g_provider *pp2;
90 struct g_slicer *gsp;
91 struct g_slice *gsl, *gsl2;
92
93 gp = pp->geom;
94 cp = LIST_FIRST(&gp->consumer);
95 KASSERT (cp != NULL, ("g_slice_access but no consumer"));
96 gsp = gp->softc;
97 gsl = &gsp->slices[pp->index];
98 for (u = 0; u < gsp->nslice; u++) {
99 gsl2 = &gsp->slices[u];
100 if (gsl2->length == 0)
101 continue;
102 if (u == pp->index)
103 continue;
104 if (gsl->offset + gsl->length <= gsl2->offset)
105 continue;
106 if (gsl2->offset + gsl2->length <= gsl->offset)
107 continue;
108 /* overlap */
109 pp2 = gsl2->provider;
110 if ((pp->acw + dw) > 0 && pp2->ace > 0)
111 return (EPERM);
112 if ((pp->ace + de) > 0 && pp2->acw > 0)
113 return (EPERM);
114 }
115 /* On first open, grab an extra "exclusive" bit */
116 if (cp->acr == 0 && cp->acw == 0 && cp->ace == 0)
117 de++;
118 /* ... and let go of it on last close */
119 if ((cp->acr + dr) == 0 && (cp->acw + dw) == 0 && (cp->ace + de) == 1)
120 de--;
121 error = g_access_rel(cp, dr, dw, de);
122 return (error);
123}
124
125void
126g_slice_finish_hot(struct bio *bp)
127{
128 struct bio *bp2;
129 struct g_geom *gp;
130 struct g_consumer *cp;
131 struct g_slicer *gsp;
132 struct g_slice *gsl;
133 int idx;
134
135 KASSERT(bp->bio_to != NULL, ("NULL bio_to in g_slice_finish_hot(%p)", bp));
136 KASSERT(bp->bio_from != NULL, ("NULL bio_from in g_slice_finish_hot(%p)", bp));
137 gp = bp->bio_to->geom;
138 gsp = gp->softc;
139 cp = LIST_FIRST(&gp->consumer);
140 KASSERT(cp != NULL, ("NULL consumer in g_slice_finish_hot(%p)", bp));
141 idx = bp->bio_to->index;
142 gsl = &gsp->slices[idx];
143
144 bp2 = g_clone_bio(bp);
145 if (bp2 == NULL) {
146 g_io_deliver(bp, ENOMEM);
147 return;
148 }
149 if (bp2->bio_offset + bp2->bio_length > gsl->length)
150 bp2->bio_length = gsl->length - bp2->bio_offset;
151 bp2->bio_done = g_std_done;
152 bp2->bio_offset += gsl->offset;
153 g_io_request(bp2, cp);
154 return;
155}
156
157static void
158g_slice_start(struct bio *bp)
159{
160 struct bio *bp2;
161 struct g_provider *pp;
162 struct g_geom *gp;
163 struct g_consumer *cp;
164 struct g_slicer *gsp;
165 struct g_slice *gsl, *gmp;
166 int idx, error;
167 u_int m_index;
168 off_t t;
169
170 pp = bp->bio_to;
171 gp = pp->geom;
172 gsp = gp->softc;
173 cp = LIST_FIRST(&gp->consumer);
174 idx = pp->index;
175 gsl = &gsp->slices[idx];
176 switch(bp->bio_cmd) {
177 case BIO_READ:
178 case BIO_WRITE:
179 case BIO_DELETE:
180 if (bp->bio_offset > gsl->length) {
181 g_io_deliver(bp, EINVAL); /* XXX: EWHAT ? */
182 return;
183 }
184 /*
185 * Check if we collide with any hot spaces, and call the
186 * method once if so.
187 */
188 t = bp->bio_offset + gsl->offset;
189 /* .ctl devices may take us negative */
190 if (t < 0 || (t + bp->bio_length) < 0) {
191 g_io_deliver(bp, EINVAL);
192 return;
193 }
194 for (m_index = 0; m_index < gsp->nhot; m_index++) {
195 gmp = &gsp->hot[m_index];
196 if (t >= gmp->offset + gmp->length)
197 continue;
198 if (t + bp->bio_length <= gmp->offset)
199 continue;
200 error = gsp->start(bp);
201 if (error == EJUSTRETURN)
202 return;
203 else if (error) {
204 g_io_deliver(bp, error);
205 return;
206 }
207 break;
208 }
209 bp2 = g_clone_bio(bp);
210 if (bp2 == NULL) {
211 g_io_deliver(bp, ENOMEM);
212 return;
213 }
214 if (bp2->bio_offset + bp2->bio_length > gsl->length)
215 bp2->bio_length = gsl->length - bp2->bio_offset;
216 bp2->bio_done = g_std_done;
217 bp2->bio_offset += gsl->offset;
218 g_io_request(bp2, cp);
219 return;
220 case BIO_GETATTR:
221 case BIO_SETATTR:
222 /* Give the real method a chance to override */
223 if (gsp->start(bp))
224 return;
225 if (!strcmp("GEOM::frontstuff", bp->bio_attribute)) {
226 t = gsp->cfrontstuff;
227 if (gsp->frontstuff > t)
228 t = gsp->frontstuff;
229 t -= gsl->offset;
230 if (t < 0)
231 t = 0;
232 if (t > gsl->length)
233 t = gsl->length;
234 g_handleattr_off_t(bp, "GEOM::frontstuff", t);
235 return;
236 }
237#ifdef _KERNEL
238 if (!strcmp("GEOM::kerneldump", bp->bio_attribute)) {
239 struct g_kerneldump *gkd;
240
241 gkd = (struct g_kerneldump *)bp->bio_data;
242 gkd->offset += gsp->slices[idx].offset;
243 if (gkd->length > gsp->slices[idx].length)
244 gkd->length = gsp->slices[idx].length;
245 /* now, pass it on downwards... */
246 }
247#endif
248 bp2 = g_clone_bio(bp);
249 if (bp2 == NULL) {
250 g_io_deliver(bp, ENOMEM);
251 return;
252 }
253 bp2->bio_done = g_std_done;
254 g_io_request(bp2, cp);
255 break;
256 default:
257 g_io_deliver(bp, EOPNOTSUPP);
258 return;
259 }
260}
261
262void
263g_slice_dumpconf(struct sbuf *sb, const char *indent, struct g_geom *gp, struct g_consumer *cp, struct g_provider *pp)
264{
265 struct g_slicer *gsp;
266
267 gsp = gp->softc;
268 if (indent == NULL) {
269 sbuf_printf(sb, " i %u", pp->index);
270 sbuf_printf(sb, " o %ju",
271 (uintmax_t)gsp->slices[pp->index].offset);
272 return;
273 }
274 if (gp != NULL && (pp == NULL && cp == NULL)) {
275 sbuf_printf(sb, "%s<frontstuff>%ju</frontstuff>\n",
276 indent, (intmax_t)gsp->frontstuff);
277 }
278 if (pp != NULL) {
279 sbuf_printf(sb, "%s<index>%u</index>\n", indent, pp->index);
280 sbuf_printf(sb, "%s<length>%ju</length>\n",
281 indent, (uintmax_t)gsp->slices[pp->index].length);
282 sbuf_printf(sb, "%s<seclength>%ju</seclength>\n", indent,
283 (uintmax_t)gsp->slices[pp->index].length / 512);
284 sbuf_printf(sb, "%s<offset>%ju</offset>\n", indent,
285 (uintmax_t)gsp->slices[pp->index].offset);
286 sbuf_printf(sb, "%s<secoffset>%ju</secoffset>\n", indent,
287 (uintmax_t)gsp->slices[pp->index].offset / 512);
288 }
289}
290
291int
292g_slice_config(struct g_geom *gp, u_int idx, int how, off_t offset, off_t length, u_int sectorsize, const char *fmt, ...)
293{
294 struct g_provider *pp;
295 struct g_slicer *gsp;
296 struct g_slice *gsl;
297 va_list ap;
298 struct sbuf *sb;
299 int error, acc;
300
301 g_trace(G_T_TOPOLOGY, "g_slice_config(%s, %d, %d)",
302 gp->name, idx, how);
303 g_topology_assert();
304 gsp = gp->softc;
305 error = 0;
306 if (idx >= gsp->nslice)
307 return(EINVAL);
308 gsl = &gsp->slices[idx];
309 pp = gsl->provider;
310 if (pp != NULL)
311 acc = pp->acr + pp->acw + pp->ace;
312 else
313 acc = 0;
314 if (acc != 0 && how != G_SLICE_CONFIG_FORCE) {
315 if (length < gsl->length)
316 return(EBUSY);
317 if (offset != gsl->offset)
318 return(EBUSY);
319 }
320 /* XXX: check offset + length <= MEDIASIZE */
321 if (how == G_SLICE_CONFIG_CHECK)
322 return (0);
323 gsl->length = length;
324 gsl->offset = offset;
325 gsl->sectorsize = sectorsize;
326 if (length == 0) {
327 if (pp == NULL)
328 return (0);
329 if (bootverbose)
330 printf("GEOM: Deconfigure %s\n", pp->name);
331 g_orphan_provider(pp, ENXIO);
332 gsl->provider = NULL;
333 gsp->nprovider--;
334 return (0);
335 }
336 if (pp != NULL) {
337 if (bootverbose)
338 printf("GEOM: Reconfigure %s, start %jd length %jd end %jd\n",
339 pp->name, (intmax_t)offset, (intmax_t)length,
340 (intmax_t)(offset + length - 1));
341 pp->mediasize = gsl->length;
342 return (0);
343 }
344 va_start(ap, fmt);
345 sb = sbuf_new(NULL, NULL, 0, SBUF_AUTOEXTEND);
346 sbuf_vprintf(sb, fmt, ap);
347 sbuf_finish(sb);
348 pp = g_new_providerf(gp, sbuf_data(sb));
349 pp->flags =
350 LIST_FIRST(&gp->consumer)->provider->flags & G_PF_CANDELETE;
351 if (bootverbose)
352 printf("GEOM: Configure %s, start %jd length %jd end %jd\n",
353 pp->name, (intmax_t)offset, (intmax_t)length,
354 (intmax_t)(offset + length - 1));
355 pp->index = idx;
356 pp->mediasize = gsl->length;
357 pp->sectorsize = gsl->sectorsize;
358 gsl->provider = pp;
359 gsp->nprovider++;
360 g_error_provider(pp, 0);
361 sbuf_delete(sb);
362 return(0);
363}
364
365int
366g_slice_conf_hot(struct g_geom *gp, u_int idx, off_t offset, off_t length)
367{
368 struct g_slicer *gsp;
369 struct g_slice *gsl, *gsl2;
370
371 g_trace(G_T_TOPOLOGY, "g_slice_conf_hot()");
372 g_topology_assert();
373 gsp = gp->softc;
374 gsl = gsp->hot;
375 if(idx >= gsp->nhot) {
376 gsl2 = g_malloc((idx + 1) * sizeof *gsl2, M_ZERO);
377 if (gsp->hot != NULL)
378 bcopy(gsp->hot, gsl2, gsp->nhot * sizeof *gsl2);
379 gsp->hot = gsl2;
380 if (gsp->hot != NULL)
381 g_free(gsl);
382 gsl = gsl2;
383 gsp->nhot = idx + 1;
384 }
385 if (bootverbose)
386 printf("GEOM: Add %s hot[%d] start %jd length %jd end %jd\n",
387 gp->name, idx, (intmax_t)offset, (intmax_t)length,
388 (intmax_t)(offset + length - 1));
389 gsl[idx].offset = offset;
390 gsl[idx].length = length;
391 return (0);
392}
393
394struct g_geom *
395g_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)
396{
397 struct g_geom *gp;
398 struct g_slicer *gsp;
399 struct g_consumer *cp;
400 void **vp;
401 int error, i;
402
403 g_topology_assert();
404 vp = (void **)extrap;
405 gp = g_new_geomf(mp, "%s", pp->name);
406 gsp = g_slice_init(slices, extra);
407 gsp->start = start;
408 gp->access = g_slice_access;
409 gp->orphan = g_slice_orphan;
410 gp->softc = gsp;
411 gp->start = g_slice_start;
412 gp->spoiled = g_std_spoiled;
413 gp->dumpconf = g_slice_dumpconf;
414 cp = g_new_consumer(gp);
415 error = g_attach(cp, pp);
416 if (error == 0)
417 error = g_access_rel(cp, 1, 0, 0);
418 if (error) {
419 if (cp->provider != NULL)
420 g_detach(cp);
421 g_destroy_consumer(cp);
422 g_free(gsp->slices);
423 g_free(gp->softc);
424 g_destroy_geom(gp);
425 return (NULL);
426 }
427 /* Find out if there are any magic bytes on the consumer */
428 i = sizeof gsp->cfrontstuff;
429 error = g_io_getattr("GEOM::frontstuff", cp, &i, &gsp->cfrontstuff);
430 if (error)
431 gsp->cfrontstuff = 0;
432 *vp = gsp->softc;
433 *cpp = cp;
434 return (gp);
435}
436
437static void
438g_slice_orphan(struct g_consumer *cp)
439{
440 struct g_geom *gp;
441 struct g_provider *pp;
442 int error;
443
444 g_trace(G_T_TOPOLOGY, "g_slice_orphan(%p/%s)", cp, cp->provider->name);
445 g_topology_assert();
446 KASSERT(cp->provider->error != 0,
447 ("g_slice_orphan with error == 0"));
448
449 gp = cp->geom;
450 /* XXX: Not good enough we leak the softc and its suballocations */
451 gp->flags |= G_GEOM_WITHER;
452 error = cp->provider->error;
453 LIST_FOREACH(pp, &gp->provider, provider)
454 g_orphan_provider(pp, error);
455 return;
456}
2 * Copyright (c) 2002 Poul-Henning Kamp
3 * Copyright (c) 2002 Networks Associates Technology, Inc.
4 * All rights reserved.
5 *
6 * This software was developed for the FreeBSD Project by Poul-Henning Kamp
7 * and NAI Labs, the Security Research Division of Network Associates, Inc.
8 * under DARPA/SPAWAR contract N66001-01-C-8035 ("CBOSS"), as part of the
9 * DARPA CHATS research program.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. The names of the authors may not be used to endorse or promote
20 * products derived from this software without specific prior written
21 * permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * SUCH DAMAGE.
34 *
35 * $FreeBSD: head/sys/geom/geom_slice.c 110696 2003-02-11 12:36:33Z phk $
36 */
37
38
39#include <sys/param.h>
40#include <sys/stdint.h>
41#ifndef _KERNEL
42#include <stdio.h>
43#include <unistd.h>
44#include <stdlib.h>
45#include <signal.h>
46#include <string.h>
47#include <err.h>
48#else
49#include <sys/systm.h>
50#include <sys/kernel.h>
51#include <sys/malloc.h>
52#include <sys/bio.h>
53#include <sys/sysctl.h>
54#include <sys/proc.h>
55#include <sys/kthread.h>
56#include <sys/lock.h>
57#include <sys/mutex.h>
58#endif
59#include <sys/errno.h>
60#include <sys/sbuf.h>
61#include <geom/geom.h>
62#include <geom/geom_slice.h>
63#include <machine/stdarg.h>
64
65static g_orphan_t g_slice_orphan;
66static g_access_t g_slice_access;
67static g_start_t g_slice_start;
68
69static struct g_slicer *
70g_slice_init(unsigned nslice, unsigned scsize)
71{
72 struct g_slicer *gsp;
73
74 gsp = g_malloc(sizeof *gsp, M_ZERO);
75 gsp->softc = g_malloc(scsize, M_ZERO);
76 gsp->slices = g_malloc(nslice * sizeof(struct g_slice),
77 M_ZERO);
78 gsp->nslice = nslice;
79 return (gsp);
80}
81
82static int
83g_slice_access(struct g_provider *pp, int dr, int dw, int de)
84{
85 int error;
86 u_int u;
87 struct g_geom *gp;
88 struct g_consumer *cp;
89 struct g_provider *pp2;
90 struct g_slicer *gsp;
91 struct g_slice *gsl, *gsl2;
92
93 gp = pp->geom;
94 cp = LIST_FIRST(&gp->consumer);
95 KASSERT (cp != NULL, ("g_slice_access but no consumer"));
96 gsp = gp->softc;
97 gsl = &gsp->slices[pp->index];
98 for (u = 0; u < gsp->nslice; u++) {
99 gsl2 = &gsp->slices[u];
100 if (gsl2->length == 0)
101 continue;
102 if (u == pp->index)
103 continue;
104 if (gsl->offset + gsl->length <= gsl2->offset)
105 continue;
106 if (gsl2->offset + gsl2->length <= gsl->offset)
107 continue;
108 /* overlap */
109 pp2 = gsl2->provider;
110 if ((pp->acw + dw) > 0 && pp2->ace > 0)
111 return (EPERM);
112 if ((pp->ace + de) > 0 && pp2->acw > 0)
113 return (EPERM);
114 }
115 /* On first open, grab an extra "exclusive" bit */
116 if (cp->acr == 0 && cp->acw == 0 && cp->ace == 0)
117 de++;
118 /* ... and let go of it on last close */
119 if ((cp->acr + dr) == 0 && (cp->acw + dw) == 0 && (cp->ace + de) == 1)
120 de--;
121 error = g_access_rel(cp, dr, dw, de);
122 return (error);
123}
124
125void
126g_slice_finish_hot(struct bio *bp)
127{
128 struct bio *bp2;
129 struct g_geom *gp;
130 struct g_consumer *cp;
131 struct g_slicer *gsp;
132 struct g_slice *gsl;
133 int idx;
134
135 KASSERT(bp->bio_to != NULL, ("NULL bio_to in g_slice_finish_hot(%p)", bp));
136 KASSERT(bp->bio_from != NULL, ("NULL bio_from in g_slice_finish_hot(%p)", bp));
137 gp = bp->bio_to->geom;
138 gsp = gp->softc;
139 cp = LIST_FIRST(&gp->consumer);
140 KASSERT(cp != NULL, ("NULL consumer in g_slice_finish_hot(%p)", bp));
141 idx = bp->bio_to->index;
142 gsl = &gsp->slices[idx];
143
144 bp2 = g_clone_bio(bp);
145 if (bp2 == NULL) {
146 g_io_deliver(bp, ENOMEM);
147 return;
148 }
149 if (bp2->bio_offset + bp2->bio_length > gsl->length)
150 bp2->bio_length = gsl->length - bp2->bio_offset;
151 bp2->bio_done = g_std_done;
152 bp2->bio_offset += gsl->offset;
153 g_io_request(bp2, cp);
154 return;
155}
156
157static void
158g_slice_start(struct bio *bp)
159{
160 struct bio *bp2;
161 struct g_provider *pp;
162 struct g_geom *gp;
163 struct g_consumer *cp;
164 struct g_slicer *gsp;
165 struct g_slice *gsl, *gmp;
166 int idx, error;
167 u_int m_index;
168 off_t t;
169
170 pp = bp->bio_to;
171 gp = pp->geom;
172 gsp = gp->softc;
173 cp = LIST_FIRST(&gp->consumer);
174 idx = pp->index;
175 gsl = &gsp->slices[idx];
176 switch(bp->bio_cmd) {
177 case BIO_READ:
178 case BIO_WRITE:
179 case BIO_DELETE:
180 if (bp->bio_offset > gsl->length) {
181 g_io_deliver(bp, EINVAL); /* XXX: EWHAT ? */
182 return;
183 }
184 /*
185 * Check if we collide with any hot spaces, and call the
186 * method once if so.
187 */
188 t = bp->bio_offset + gsl->offset;
189 /* .ctl devices may take us negative */
190 if (t < 0 || (t + bp->bio_length) < 0) {
191 g_io_deliver(bp, EINVAL);
192 return;
193 }
194 for (m_index = 0; m_index < gsp->nhot; m_index++) {
195 gmp = &gsp->hot[m_index];
196 if (t >= gmp->offset + gmp->length)
197 continue;
198 if (t + bp->bio_length <= gmp->offset)
199 continue;
200 error = gsp->start(bp);
201 if (error == EJUSTRETURN)
202 return;
203 else if (error) {
204 g_io_deliver(bp, error);
205 return;
206 }
207 break;
208 }
209 bp2 = g_clone_bio(bp);
210 if (bp2 == NULL) {
211 g_io_deliver(bp, ENOMEM);
212 return;
213 }
214 if (bp2->bio_offset + bp2->bio_length > gsl->length)
215 bp2->bio_length = gsl->length - bp2->bio_offset;
216 bp2->bio_done = g_std_done;
217 bp2->bio_offset += gsl->offset;
218 g_io_request(bp2, cp);
219 return;
220 case BIO_GETATTR:
221 case BIO_SETATTR:
222 /* Give the real method a chance to override */
223 if (gsp->start(bp))
224 return;
225 if (!strcmp("GEOM::frontstuff", bp->bio_attribute)) {
226 t = gsp->cfrontstuff;
227 if (gsp->frontstuff > t)
228 t = gsp->frontstuff;
229 t -= gsl->offset;
230 if (t < 0)
231 t = 0;
232 if (t > gsl->length)
233 t = gsl->length;
234 g_handleattr_off_t(bp, "GEOM::frontstuff", t);
235 return;
236 }
237#ifdef _KERNEL
238 if (!strcmp("GEOM::kerneldump", bp->bio_attribute)) {
239 struct g_kerneldump *gkd;
240
241 gkd = (struct g_kerneldump *)bp->bio_data;
242 gkd->offset += gsp->slices[idx].offset;
243 if (gkd->length > gsp->slices[idx].length)
244 gkd->length = gsp->slices[idx].length;
245 /* now, pass it on downwards... */
246 }
247#endif
248 bp2 = g_clone_bio(bp);
249 if (bp2 == NULL) {
250 g_io_deliver(bp, ENOMEM);
251 return;
252 }
253 bp2->bio_done = g_std_done;
254 g_io_request(bp2, cp);
255 break;
256 default:
257 g_io_deliver(bp, EOPNOTSUPP);
258 return;
259 }
260}
261
262void
263g_slice_dumpconf(struct sbuf *sb, const char *indent, struct g_geom *gp, struct g_consumer *cp, struct g_provider *pp)
264{
265 struct g_slicer *gsp;
266
267 gsp = gp->softc;
268 if (indent == NULL) {
269 sbuf_printf(sb, " i %u", pp->index);
270 sbuf_printf(sb, " o %ju",
271 (uintmax_t)gsp->slices[pp->index].offset);
272 return;
273 }
274 if (gp != NULL && (pp == NULL && cp == NULL)) {
275 sbuf_printf(sb, "%s<frontstuff>%ju</frontstuff>\n",
276 indent, (intmax_t)gsp->frontstuff);
277 }
278 if (pp != NULL) {
279 sbuf_printf(sb, "%s<index>%u</index>\n", indent, pp->index);
280 sbuf_printf(sb, "%s<length>%ju</length>\n",
281 indent, (uintmax_t)gsp->slices[pp->index].length);
282 sbuf_printf(sb, "%s<seclength>%ju</seclength>\n", indent,
283 (uintmax_t)gsp->slices[pp->index].length / 512);
284 sbuf_printf(sb, "%s<offset>%ju</offset>\n", indent,
285 (uintmax_t)gsp->slices[pp->index].offset);
286 sbuf_printf(sb, "%s<secoffset>%ju</secoffset>\n", indent,
287 (uintmax_t)gsp->slices[pp->index].offset / 512);
288 }
289}
290
291int
292g_slice_config(struct g_geom *gp, u_int idx, int how, off_t offset, off_t length, u_int sectorsize, const char *fmt, ...)
293{
294 struct g_provider *pp;
295 struct g_slicer *gsp;
296 struct g_slice *gsl;
297 va_list ap;
298 struct sbuf *sb;
299 int error, acc;
300
301 g_trace(G_T_TOPOLOGY, "g_slice_config(%s, %d, %d)",
302 gp->name, idx, how);
303 g_topology_assert();
304 gsp = gp->softc;
305 error = 0;
306 if (idx >= gsp->nslice)
307 return(EINVAL);
308 gsl = &gsp->slices[idx];
309 pp = gsl->provider;
310 if (pp != NULL)
311 acc = pp->acr + pp->acw + pp->ace;
312 else
313 acc = 0;
314 if (acc != 0 && how != G_SLICE_CONFIG_FORCE) {
315 if (length < gsl->length)
316 return(EBUSY);
317 if (offset != gsl->offset)
318 return(EBUSY);
319 }
320 /* XXX: check offset + length <= MEDIASIZE */
321 if (how == G_SLICE_CONFIG_CHECK)
322 return (0);
323 gsl->length = length;
324 gsl->offset = offset;
325 gsl->sectorsize = sectorsize;
326 if (length == 0) {
327 if (pp == NULL)
328 return (0);
329 if (bootverbose)
330 printf("GEOM: Deconfigure %s\n", pp->name);
331 g_orphan_provider(pp, ENXIO);
332 gsl->provider = NULL;
333 gsp->nprovider--;
334 return (0);
335 }
336 if (pp != NULL) {
337 if (bootverbose)
338 printf("GEOM: Reconfigure %s, start %jd length %jd end %jd\n",
339 pp->name, (intmax_t)offset, (intmax_t)length,
340 (intmax_t)(offset + length - 1));
341 pp->mediasize = gsl->length;
342 return (0);
343 }
344 va_start(ap, fmt);
345 sb = sbuf_new(NULL, NULL, 0, SBUF_AUTOEXTEND);
346 sbuf_vprintf(sb, fmt, ap);
347 sbuf_finish(sb);
348 pp = g_new_providerf(gp, sbuf_data(sb));
349 pp->flags =
350 LIST_FIRST(&gp->consumer)->provider->flags & G_PF_CANDELETE;
351 if (bootverbose)
352 printf("GEOM: Configure %s, start %jd length %jd end %jd\n",
353 pp->name, (intmax_t)offset, (intmax_t)length,
354 (intmax_t)(offset + length - 1));
355 pp->index = idx;
356 pp->mediasize = gsl->length;
357 pp->sectorsize = gsl->sectorsize;
358 gsl->provider = pp;
359 gsp->nprovider++;
360 g_error_provider(pp, 0);
361 sbuf_delete(sb);
362 return(0);
363}
364
365int
366g_slice_conf_hot(struct g_geom *gp, u_int idx, off_t offset, off_t length)
367{
368 struct g_slicer *gsp;
369 struct g_slice *gsl, *gsl2;
370
371 g_trace(G_T_TOPOLOGY, "g_slice_conf_hot()");
372 g_topology_assert();
373 gsp = gp->softc;
374 gsl = gsp->hot;
375 if(idx >= gsp->nhot) {
376 gsl2 = g_malloc((idx + 1) * sizeof *gsl2, M_ZERO);
377 if (gsp->hot != NULL)
378 bcopy(gsp->hot, gsl2, gsp->nhot * sizeof *gsl2);
379 gsp->hot = gsl2;
380 if (gsp->hot != NULL)
381 g_free(gsl);
382 gsl = gsl2;
383 gsp->nhot = idx + 1;
384 }
385 if (bootverbose)
386 printf("GEOM: Add %s hot[%d] start %jd length %jd end %jd\n",
387 gp->name, idx, (intmax_t)offset, (intmax_t)length,
388 (intmax_t)(offset + length - 1));
389 gsl[idx].offset = offset;
390 gsl[idx].length = length;
391 return (0);
392}
393
394struct g_geom *
395g_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)
396{
397 struct g_geom *gp;
398 struct g_slicer *gsp;
399 struct g_consumer *cp;
400 void **vp;
401 int error, i;
402
403 g_topology_assert();
404 vp = (void **)extrap;
405 gp = g_new_geomf(mp, "%s", pp->name);
406 gsp = g_slice_init(slices, extra);
407 gsp->start = start;
408 gp->access = g_slice_access;
409 gp->orphan = g_slice_orphan;
410 gp->softc = gsp;
411 gp->start = g_slice_start;
412 gp->spoiled = g_std_spoiled;
413 gp->dumpconf = g_slice_dumpconf;
414 cp = g_new_consumer(gp);
415 error = g_attach(cp, pp);
416 if (error == 0)
417 error = g_access_rel(cp, 1, 0, 0);
418 if (error) {
419 if (cp->provider != NULL)
420 g_detach(cp);
421 g_destroy_consumer(cp);
422 g_free(gsp->slices);
423 g_free(gp->softc);
424 g_destroy_geom(gp);
425 return (NULL);
426 }
427 /* Find out if there are any magic bytes on the consumer */
428 i = sizeof gsp->cfrontstuff;
429 error = g_io_getattr("GEOM::frontstuff", cp, &i, &gsp->cfrontstuff);
430 if (error)
431 gsp->cfrontstuff = 0;
432 *vp = gsp->softc;
433 *cpp = cp;
434 return (gp);
435}
436
437static void
438g_slice_orphan(struct g_consumer *cp)
439{
440 struct g_geom *gp;
441 struct g_provider *pp;
442 int error;
443
444 g_trace(G_T_TOPOLOGY, "g_slice_orphan(%p/%s)", cp, cp->provider->name);
445 g_topology_assert();
446 KASSERT(cp->provider->error != 0,
447 ("g_slice_orphan with error == 0"));
448
449 gp = cp->geom;
450 /* XXX: Not good enough we leak the softc and its suballocations */
451 gp->flags |= G_GEOM_WITHER;
452 error = cp->provider->error;
453 LIST_FOREACH(pp, &gp->provider, provider)
454 g_orphan_provider(pp, error);
455 return;
456}