FreeBSD/Linux Kernel Cross Reference
sys/geom/geom_slice.c
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
36 #include <sys/cdefs.h>
37 __FBSDID("$FreeBSD: releng/6.0/sys/geom/geom_slice.c 151107 2005-10-08 22:11:38Z rodrigc $");
38
39 #include <sys/param.h>
40 #include <sys/systm.h>
41 #include <sys/kernel.h>
42 #include <sys/malloc.h>
43 #include <sys/bio.h>
44 #include <sys/sysctl.h>
45 #include <sys/proc.h>
46 #include <sys/kthread.h>
47 #include <sys/lock.h>
48 #include <sys/mutex.h>
49 #include <sys/errno.h>
50 #include <sys/sbuf.h>
51 #include <geom/geom.h>
52 #include <geom/geom_slice.h>
53 #include <machine/stdarg.h>
54
55 static g_orphan_t g_slice_orphan;
56 static g_access_t g_slice_access;
57 static g_start_t g_slice_start;
58
59 static struct g_slicer *
60 g_slice_alloc(unsigned nslice, unsigned scsize)
61 {
62 struct g_slicer *gsp;
63
64 gsp = g_malloc(sizeof *gsp, M_WAITOK | M_ZERO);
65 if (scsize > 0)
66 gsp->softc = g_malloc(scsize, M_WAITOK | M_ZERO);
67 else
68 gsp->softc = NULL;
69 gsp->slices = g_malloc(nslice * sizeof(struct g_slice),
70 M_WAITOK | M_ZERO);
71 gsp->nslice = nslice;
72 return (gsp);
73 }
74
75 static void
76 g_slice_free(struct g_slicer *gsp)
77 {
78
79 if (gsp == NULL) /* XXX: phk thinks about this */
80 return;
81 g_free(gsp->slices);
82 if (gsp->hotspot != NULL)
83 g_free(gsp->hotspot);
84 if (gsp->softc != NULL)
85 g_free(gsp->softc);
86 g_free(gsp);
87 }
88
89 static int
90 g_slice_access(struct g_provider *pp, int dr, int dw, int de)
91 {
92 int error;
93 u_int u;
94 struct g_geom *gp;
95 struct g_consumer *cp;
96 struct g_provider *pp2;
97 struct g_slicer *gsp;
98 struct g_slice *gsl, *gsl2;
99
100 gp = pp->geom;
101 cp = LIST_FIRST(&gp->consumer);
102 KASSERT (cp != NULL, ("g_slice_access but no consumer"));
103 gsp = gp->softc;
104 if (dr > 0 || dw > 0 || de > 0) {
105 gsl = &gsp->slices[pp->index];
106 for (u = 0; u < gsp->nslice; u++) {
107 gsl2 = &gsp->slices[u];
108 if (gsl2->length == 0)
109 continue;
110 if (u == pp->index)
111 continue;
112 if (gsl->offset + gsl->length <= gsl2->offset)
113 continue;
114 if (gsl2->offset + gsl2->length <= gsl->offset)
115 continue;
116 /* overlap */
117 pp2 = gsl2->provider;
118 if ((pp->acw + dw) > 0 && pp2->ace > 0)
119 return (EPERM);
120 if ((pp->ace + de) > 0 && pp2->acw > 0)
121 return (EPERM);
122 }
123 }
124 /* On first open, grab an extra "exclusive" bit */
125 if (cp->acr == 0 && cp->acw == 0 && cp->ace == 0)
126 de++;
127 /* ... and let go of it on last close */
128 if ((cp->acr + dr) == 0 && (cp->acw + dw) == 0 && (cp->ace + de) == 1)
129 de--;
130 error = g_access(cp, dr, dw, de);
131 return (error);
132 }
133
134 /*
135 * XXX: It should be possible to specify here if we should finish all of the
136 * XXX: bio, or only the non-hot bits. This would get messy if there were
137 * XXX: two hot spots in the same bio, so for now we simply finish off the
138 * XXX: entire bio. Modifying hot data on the way to disk is frowned on
139 * XXX: so making that considerably harder is not a bad idea anyway.
140 */
141 void
142 g_slice_finish_hot(struct bio *bp)
143 {
144 struct bio *bp2;
145 struct g_geom *gp;
146 struct g_consumer *cp;
147 struct g_slicer *gsp;
148 struct g_slice *gsl;
149 int idx;
150
151 KASSERT(bp->bio_to != NULL,
152 ("NULL bio_to in g_slice_finish_hot(%p)", bp));
153 KASSERT(bp->bio_from != NULL,
154 ("NULL bio_from in g_slice_finish_hot(%p)", bp));
155 gp = bp->bio_to->geom;
156 gsp = gp->softc;
157 cp = LIST_FIRST(&gp->consumer);
158 KASSERT(cp != NULL, ("NULL consumer in g_slice_finish_hot(%p)", bp));
159 idx = bp->bio_to->index;
160 gsl = &gsp->slices[idx];
161
162 bp2 = g_clone_bio(bp);
163 if (bp2 == NULL) {
164 g_io_deliver(bp, ENOMEM);
165 return;
166 }
167 if (bp2->bio_offset + bp2->bio_length > gsl->length)
168 bp2->bio_length = gsl->length - bp2->bio_offset;
169 bp2->bio_done = g_std_done;
170 bp2->bio_offset += gsl->offset;
171 g_io_request(bp2, cp);
172 return;
173 }
174
175 static void
176 g_slice_start(struct bio *bp)
177 {
178 struct bio *bp2;
179 struct g_provider *pp;
180 struct g_geom *gp;
181 struct g_consumer *cp;
182 struct g_slicer *gsp;
183 struct g_slice *gsl;
184 struct g_slice_hot *ghp;
185 int idx, error;
186 u_int m_index;
187 off_t t;
188
189 pp = bp->bio_to;
190 gp = pp->geom;
191 gsp = gp->softc;
192 cp = LIST_FIRST(&gp->consumer);
193 idx = pp->index;
194 gsl = &gsp->slices[idx];
195 switch(bp->bio_cmd) {
196 case BIO_READ:
197 case BIO_WRITE:
198 case BIO_DELETE:
199 if (bp->bio_offset > gsl->length) {
200 g_io_deliver(bp, EINVAL); /* XXX: EWHAT ? */
201 return;
202 }
203 /*
204 * Check if we collide with any hot spaces, and call the
205 * method once if so.
206 */
207 t = bp->bio_offset + gsl->offset;
208 for (m_index = 0; m_index < gsp->nhotspot; m_index++) {
209 ghp = &gsp->hotspot[m_index];
210 if (t >= ghp->offset + ghp->length)
211 continue;
212 if (t + bp->bio_length <= ghp->offset)
213 continue;
214 switch(bp->bio_cmd) {
215 case BIO_READ: idx = ghp->ract; break;
216 case BIO_WRITE: idx = ghp->wact; break;
217 case BIO_DELETE: idx = ghp->dact; break;
218 }
219 switch(idx) {
220 case G_SLICE_HOT_ALLOW:
221 /* Fall out and continue normal processing */
222 continue;
223 case G_SLICE_HOT_DENY:
224 g_io_deliver(bp, EROFS);
225 return;
226 case G_SLICE_HOT_START:
227 error = gsp->start(bp);
228 if (error && error != EJUSTRETURN)
229 g_io_deliver(bp, error);
230 return;
231 case G_SLICE_HOT_CALL:
232 error = g_post_event(gsp->hot, bp, M_NOWAIT,
233 gp, NULL);
234 if (error)
235 g_io_deliver(bp, error);
236 return;
237 }
238 break;
239 }
240 bp2 = g_clone_bio(bp);
241 if (bp2 == NULL) {
242 g_io_deliver(bp, ENOMEM);
243 return;
244 }
245 if (bp2->bio_offset + bp2->bio_length > gsl->length)
246 bp2->bio_length = gsl->length - bp2->bio_offset;
247 bp2->bio_done = g_std_done;
248 bp2->bio_offset += gsl->offset;
249 g_io_request(bp2, cp);
250 return;
251 case BIO_GETATTR:
252 /* Give the real method a chance to override */
253 if (gsp->start != NULL && gsp->start(bp))
254 return;
255 if (!strcmp("GEOM::kerneldump", bp->bio_attribute)) {
256 struct g_kerneldump *gkd;
257
258 gkd = (struct g_kerneldump *)bp->bio_data;
259 gkd->offset += gsp->slices[idx].offset;
260 if (gkd->length > gsp->slices[idx].length)
261 gkd->length = gsp->slices[idx].length;
262 /* now, pass it on downwards... */
263 }
264 bp2 = g_clone_bio(bp);
265 if (bp2 == NULL) {
266 g_io_deliver(bp, ENOMEM);
267 return;
268 }
269 bp2->bio_done = g_std_done;
270 g_io_request(bp2, cp);
271 break;
272 default:
273 g_io_deliver(bp, EOPNOTSUPP);
274 return;
275 }
276 }
277
278 void
279 g_slice_dumpconf(struct sbuf *sb, const char *indent, struct g_geom *gp, struct g_consumer *cp, struct g_provider *pp)
280 {
281 struct g_slicer *gsp;
282
283 gsp = gp->softc;
284 if (indent == NULL) {
285 sbuf_printf(sb, " i %u", pp->index);
286 sbuf_printf(sb, " o %ju",
287 (uintmax_t)gsp->slices[pp->index].offset);
288 return;
289 }
290 if (pp != NULL) {
291 sbuf_printf(sb, "%s<index>%u</index>\n", indent, pp->index);
292 sbuf_printf(sb, "%s<length>%ju</length>\n",
293 indent, (uintmax_t)gsp->slices[pp->index].length);
294 sbuf_printf(sb, "%s<seclength>%ju</seclength>\n", indent,
295 (uintmax_t)gsp->slices[pp->index].length / 512);
296 sbuf_printf(sb, "%s<offset>%ju</offset>\n", indent,
297 (uintmax_t)gsp->slices[pp->index].offset);
298 sbuf_printf(sb, "%s<secoffset>%ju</secoffset>\n", indent,
299 (uintmax_t)gsp->slices[pp->index].offset / 512);
300 }
301 }
302
303 int
304 g_slice_config(struct g_geom *gp, u_int idx, int how, off_t offset, off_t length, u_int sectorsize, const char *fmt, ...)
305 {
306 struct g_provider *pp, *pp2;
307 struct g_slicer *gsp;
308 struct g_slice *gsl;
309 va_list ap;
310 struct sbuf *sb;
311 int acc;
312
313 g_trace(G_T_TOPOLOGY, "g_slice_config(%s, %d, %d)",
314 gp->name, idx, how);
315 g_topology_assert();
316 gsp = gp->softc;
317 if (idx >= gsp->nslice)
318 return(EINVAL);
319 gsl = &gsp->slices[idx];
320 pp = gsl->provider;
321 if (pp != NULL)
322 acc = pp->acr + pp->acw + pp->ace;
323 else
324 acc = 0;
325 if (acc != 0 && how != G_SLICE_CONFIG_FORCE) {
326 if (length < gsl->length)
327 return(EBUSY);
328 if (offset != gsl->offset)
329 return(EBUSY);
330 }
331 /* XXX: check offset + length <= MEDIASIZE */
332 if (how == G_SLICE_CONFIG_CHECK)
333 return (0);
334 gsl->length = length;
335 gsl->offset = offset;
336 gsl->sectorsize = sectorsize;
337 if (length == 0) {
338 if (pp == NULL)
339 return (0);
340 if (bootverbose)
341 printf("GEOM: Deconfigure %s\n", pp->name);
342 pp->flags |= G_PF_WITHER;
343 g_orphan_provider(pp, ENXIO);
344 gsl->provider = NULL;
345 gsp->nprovider--;
346 return (0);
347 }
348 if (pp != NULL) {
349 if (bootverbose)
350 printf("GEOM: Reconfigure %s, start %jd length %jd end %jd\n",
351 pp->name, (intmax_t)offset, (intmax_t)length,
352 (intmax_t)(offset + length - 1));
353 pp->mediasize = gsl->length;
354 return (0);
355 }
356 sb = sbuf_new(NULL, NULL, 0, SBUF_AUTOEXTEND);
357 va_start(ap, fmt);
358 sbuf_vprintf(sb, fmt, ap);
359 va_end(ap);
360 sbuf_finish(sb);
361 pp = g_new_providerf(gp, sbuf_data(sb));
362 pp2 = LIST_FIRST(&gp->consumer)->provider;
363 pp->flags = pp2->flags & G_PF_CANDELETE;
364 if (pp2->stripesize > 0) {
365 pp->stripesize = pp2->stripesize;
366 pp->stripeoffset = (pp2->stripeoffset + offset) % pp->stripesize;
367 }
368 if (0 && bootverbose)
369 printf("GEOM: Configure %s, start %jd length %jd end %jd\n",
370 pp->name, (intmax_t)offset, (intmax_t)length,
371 (intmax_t)(offset + length - 1));
372 pp->index = idx;
373 pp->mediasize = gsl->length;
374 pp->sectorsize = gsl->sectorsize;
375 gsl->provider = pp;
376 gsp->nprovider++;
377 g_error_provider(pp, 0);
378 sbuf_delete(sb);
379 return(0);
380 }
381
382 /*
383 * Configure "hotspots". A hotspot is a piece of the parent device which
384 * this particular slicer cares about for some reason. Typically because
385 * it contains meta-data used to configure the slicer.
386 * A hotspot is identified by its index number. The offset and length are
387 * relative to the parent device, and the three "?act" fields specify
388 * what action to take on BIO_READ, BIO_DELETE and BIO_WRITE.
389 *
390 * XXX: There may be a race relative to g_slice_start() here, if an existing
391 * XXX: hotspot is changed wile I/O is happening. Should this become a problem
392 * XXX: we can protect the hotspot stuff with a mutex.
393 */
394
395 int
396 g_slice_conf_hot(struct g_geom *gp, u_int idx, off_t offset, off_t length, int ract, int dact, int wact)
397 {
398 struct g_slicer *gsp;
399 struct g_slice_hot *gsl, *gsl2;
400
401 g_trace(G_T_TOPOLOGY, "g_slice_conf_hot(%s, idx: %d, off: %jd, len: %jd)",
402 gp->name, idx, (intmax_t)offset, (intmax_t)length);
403 g_topology_assert();
404 gsp = gp->softc;
405 gsl = gsp->hotspot;
406 if(idx >= gsp->nhotspot) {
407 gsl2 = g_malloc((idx + 1) * sizeof *gsl2, M_WAITOK | M_ZERO);
408 if (gsp->hotspot != NULL)
409 bcopy(gsp->hotspot, gsl2, gsp->nhotspot * sizeof *gsl2);
410 gsp->hotspot = gsl2;
411 if (gsp->hotspot != NULL)
412 g_free(gsl);
413 gsl = gsl2;
414 gsp->nhotspot = idx + 1;
415 }
416 gsl[idx].offset = offset;
417 gsl[idx].length = length;
418 KASSERT(!((ract | dact | wact) & G_SLICE_HOT_START)
419 || gsp->start != NULL, ("G_SLICE_HOT_START but no slice->start"));
420 /* XXX: check that we _have_ a start function if HOT_START specified */
421 gsl[idx].ract = ract;
422 gsl[idx].dact = dact;
423 gsl[idx].wact = wact;
424 return (0);
425 }
426
427 void
428 g_slice_spoiled(struct g_consumer *cp)
429 {
430 struct g_geom *gp;
431 struct g_slicer *gsp;
432
433 g_topology_assert();
434 gp = cp->geom;
435 g_trace(G_T_TOPOLOGY, "g_slice_spoiled(%p/%s)", cp, gp->name);
436 gsp = gp->softc;
437 gp->softc = NULL;
438 g_slice_free(gsp);
439 g_wither_geom(gp, ENXIO);
440 }
441
442 int
443 g_slice_destroy_geom(struct gctl_req *req, struct g_class *mp, struct g_geom *gp)
444 {
445
446 g_slice_spoiled(LIST_FIRST(&gp->consumer));
447 return (0);
448 }
449
450 struct g_geom *
451 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)
452 {
453 struct g_geom *gp;
454 struct g_slicer *gsp;
455 struct g_consumer *cp;
456 void **vp;
457 int error;
458
459 g_topology_assert();
460 vp = (void **)extrap;
461 gp = g_new_geomf(mp, "%s", pp->name);
462 gsp = g_slice_alloc(slices, extra);
463 gsp->start = start;
464 gp->access = g_slice_access;
465 gp->orphan = g_slice_orphan;
466 gp->softc = gsp;
467 gp->start = g_slice_start;
468 gp->spoiled = g_slice_spoiled;
469 if (gp->dumpconf == NULL)
470 gp->dumpconf = g_slice_dumpconf;
471 if (gp->class->destroy_geom == NULL)
472 gp->class->destroy_geom = g_slice_destroy_geom;
473 cp = g_new_consumer(gp);
474 error = g_attach(cp, pp);
475 if (error == 0)
476 error = g_access(cp, 1, 0, 0);
477 if (error) {
478 g_wither_geom(gp, ENXIO);
479 return (NULL);
480 }
481 if (extrap != NULL)
482 *vp = gsp->softc;
483 *cpp = cp;
484 return (gp);
485 }
486
487 static void
488 g_slice_orphan(struct g_consumer *cp)
489 {
490
491 g_trace(G_T_TOPOLOGY, "g_slice_orphan(%p/%s)", cp, cp->provider->name);
492 g_topology_assert();
493 KASSERT(cp->provider->error != 0,
494 ("g_slice_orphan with error == 0"));
495
496 /* XXX: Not good enough we leak the softc and its suballocations */
497 g_slice_free(cp->geom->softc);
498 g_wither_geom(cp->geom, cp->provider->error);
499 }
Cache object: 7c6e32dfe9e0d0a28e68aeeec7187aeb
|