1 /*-
2 * SPDX-License-Identifier: BSD-3-Clause
3 *
4 * Copyright (c) 2002 Poul-Henning Kamp
5 * Copyright (c) 2002 Networks Associates Technology, Inc.
6 * All rights reserved.
7 *
8 * This software was developed for the FreeBSD Project by Poul-Henning Kamp
9 * and NAI Labs, the Security Research Division of Network Associates, Inc.
10 * under DARPA/SPAWAR contract N66001-01-C-8035 ("CBOSS"), as part of the
11 * DARPA CHATS research program.
12 *
13 * Redistribution and use in source and binary forms, with or without
14 * modification, are permitted provided that the following conditions
15 * are met:
16 * 1. Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution.
21 * 3. The names of the authors may not be used to endorse or promote
22 * products derived from this software without specific prior written
23 * permission.
24 *
25 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
26 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 * SUCH DAMAGE.
36 */
37
38 #include <sys/cdefs.h>
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/lock.h>
47 #include <sys/mutex.h>
48 #include <sys/errno.h>
49 #include <sys/sbuf.h>
50 #include <geom/geom.h>
51 #include <geom/geom_slice.h>
52 #include <machine/stdarg.h>
53
54 static g_access_t g_slice_access;
55 static g_start_t g_slice_start;
56
57 static struct g_slicer *
g_slice_alloc(unsigned nslice,unsigned scsize)58 g_slice_alloc(unsigned nslice, unsigned scsize)
59 {
60 struct g_slicer *gsp;
61
62 gsp = g_malloc(sizeof *gsp, M_WAITOK | M_ZERO);
63 if (scsize > 0)
64 gsp->softc = g_malloc(scsize, M_WAITOK | M_ZERO);
65 else
66 gsp->softc = NULL;
67 gsp->slices = g_malloc(nslice * sizeof(struct g_slice),
68 M_WAITOK | M_ZERO);
69 gsp->nslice = nslice;
70 return (gsp);
71 }
72
73 static void
g_slice_free(struct g_geom * gp)74 g_slice_free(struct g_geom *gp)
75 {
76 struct g_slicer *gsp;
77
78 gsp = gp->softc;
79 gp->softc = NULL;
80
81 /*
82 * We can get multiple spoiled events before wither-washer
83 * detaches our consumer, so this can get called multiple
84 * times.
85 */
86 if (gsp == NULL)
87 return;
88 g_free(gsp->slices);
89 g_free(gsp->hotspot);
90 g_free(gsp->softc);
91 g_free(gsp);
92 }
93
94 static int
g_slice_access(struct g_provider * pp,int dr,int dw,int de)95 g_slice_access(struct g_provider *pp, int dr, int dw, int de)
96 {
97 int error;
98 u_int u;
99 struct g_geom *gp;
100 struct g_consumer *cp;
101 struct g_provider *pp2;
102 struct g_slicer *gsp;
103 struct g_slice *gsl, *gsl2;
104
105 gp = pp->geom;
106 cp = LIST_FIRST(&gp->consumer);
107 KASSERT (cp != NULL, ("g_slice_access but no consumer"));
108 gsp = gp->softc;
109 if (dr > 0 || dw > 0 || de > 0) {
110 gsl = &gsp->slices[pp->index];
111 for (u = 0; u < gsp->nslice; u++) {
112 gsl2 = &gsp->slices[u];
113 if (gsl2->length == 0)
114 continue;
115 if (u == pp->index)
116 continue;
117 if (gsl->offset + gsl->length <= gsl2->offset)
118 continue;
119 if (gsl2->offset + gsl2->length <= gsl->offset)
120 continue;
121 /* overlap */
122 pp2 = gsl2->provider;
123 if ((pp->acw + dw) > 0 && pp2->ace > 0)
124 return (EPERM);
125 if ((pp->ace + de) > 0 && pp2->acw > 0)
126 return (EPERM);
127 }
128 }
129 /* On first open, grab an extra "exclusive" bit */
130 if (cp->acr == 0 && cp->acw == 0 && cp->ace == 0)
131 de++;
132 /* ... and let go of it on last close */
133 if ((cp->acr + dr) == 0 && (cp->acw + dw) == 0 && (cp->ace + de) == 1)
134 de--;
135 error = g_access(cp, dr, dw, de);
136
137 /*
138 * Free the softc if all providers have been closed and this geom
139 * is being removed.
140 */
141 if (error == 0 && (gp->flags & G_GEOM_WITHER) != 0 &&
142 (cp->acr + cp->acw + cp->ace) == 0)
143 g_slice_free(gp);
144
145 return (error);
146 }
147
148 /*
149 * XXX: It should be possible to specify here if we should finish all of the
150 * XXX: bio, or only the non-hot bits. This would get messy if there were
151 * XXX: two hot spots in the same bio, so for now we simply finish off the
152 * XXX: entire bio. Modifying hot data on the way to disk is frowned on
153 * XXX: so making that considerably harder is not a bad idea anyway.
154 */
155 void
g_slice_finish_hot(struct bio * bp)156 g_slice_finish_hot(struct bio *bp)
157 {
158 struct bio *bp2;
159 struct g_geom *gp;
160 struct g_consumer *cp;
161 struct g_slicer *gsp;
162 struct g_slice *gsl;
163 int idx;
164
165 KASSERT(bp->bio_to != NULL,
166 ("NULL bio_to in g_slice_finish_hot(%p)", bp));
167 KASSERT(bp->bio_from != NULL,
168 ("NULL bio_from in g_slice_finish_hot(%p)", bp));
169 gp = bp->bio_to->geom;
170 gsp = gp->softc;
171 cp = LIST_FIRST(&gp->consumer);
172 KASSERT(cp != NULL, ("NULL consumer in g_slice_finish_hot(%p)", bp));
173 idx = bp->bio_to->index;
174 gsl = &gsp->slices[idx];
175
176 bp2 = g_clone_bio(bp);
177 if (bp2 == NULL) {
178 g_io_deliver(bp, ENOMEM);
179 return;
180 }
181 if (bp2->bio_offset + bp2->bio_length > gsl->length)
182 bp2->bio_length = gsl->length - bp2->bio_offset;
183 bp2->bio_done = g_std_done;
184 bp2->bio_offset += gsl->offset;
185 g_io_request(bp2, cp);
186 return;
187 }
188
189 static void
g_slice_done(struct bio * bp)190 g_slice_done(struct bio *bp)
191 {
192
193 KASSERT(bp->bio_cmd == BIO_GETATTR &&
194 strcmp(bp->bio_attribute, "GEOM::ident") == 0,
195 ("bio_cmd=0x%x bio_attribute=%s", bp->bio_cmd, bp->bio_attribute));
196
197 if (bp->bio_error == 0 && bp->bio_data[0] != '\0') {
198 char idx[8];
199
200 /* Add index to the ident received. */
201 snprintf(idx, sizeof(idx), "s%d",
202 bp->bio_parent->bio_to->index);
203 if (strlcat(bp->bio_data, idx, bp->bio_length) >=
204 bp->bio_length) {
205 bp->bio_error = EFAULT;
206 }
207 }
208 g_std_done(bp);
209 }
210
211 static void
g_slice_start(struct bio * bp)212 g_slice_start(struct bio *bp)
213 {
214 struct bio *bp2;
215 struct g_provider *pp;
216 struct g_geom *gp;
217 struct g_consumer *cp;
218 struct g_slicer *gsp;
219 struct g_slice *gsl;
220 struct g_slice_hot *ghp;
221 int idx, error;
222 u_int m_index;
223 off_t t;
224
225 pp = bp->bio_to;
226 gp = pp->geom;
227 gsp = gp->softc;
228 cp = LIST_FIRST(&gp->consumer);
229 idx = pp->index;
230 gsl = &gsp->slices[idx];
231 switch(bp->bio_cmd) {
232 case BIO_READ:
233 case BIO_WRITE:
234 case BIO_DELETE:
235 if (bp->bio_offset > gsl->length) {
236 g_io_deliver(bp, EINVAL); /* XXX: EWHAT ? */
237 return;
238 }
239 /*
240 * Check if we collide with any hot spaces, and call the
241 * method once if so.
242 */
243 t = bp->bio_offset + gsl->offset;
244 for (m_index = 0; m_index < gsp->nhotspot; m_index++) {
245 ghp = &gsp->hotspot[m_index];
246 if (t >= ghp->offset + ghp->length)
247 continue;
248 if (t + bp->bio_length <= ghp->offset)
249 continue;
250 switch(bp->bio_cmd) {
251 case BIO_READ: idx = ghp->ract; break;
252 case BIO_WRITE: idx = ghp->wact; break;
253 case BIO_DELETE: idx = ghp->dact; break;
254 }
255 switch(idx) {
256 case G_SLICE_HOT_ALLOW:
257 /* Fall out and continue normal processing */
258 continue;
259 case G_SLICE_HOT_DENY:
260 g_io_deliver(bp, EROFS);
261 return;
262 case G_SLICE_HOT_START:
263 error = gsp->start(bp);
264 if (error && error != EJUSTRETURN)
265 g_io_deliver(bp, error);
266 return;
267 case G_SLICE_HOT_CALL:
268 error = g_post_event(gsp->hot, bp, M_NOWAIT,
269 gp, NULL);
270 if (error)
271 g_io_deliver(bp, error);
272 return;
273 }
274 break;
275 }
276 bp2 = g_clone_bio(bp);
277 if (bp2 == NULL) {
278 g_io_deliver(bp, ENOMEM);
279 return;
280 }
281 if (bp2->bio_offset + bp2->bio_length > gsl->length)
282 bp2->bio_length = gsl->length - bp2->bio_offset;
283 bp2->bio_done = g_std_done;
284 bp2->bio_offset += gsl->offset;
285 g_io_request(bp2, cp);
286 return;
287 case BIO_GETATTR:
288 /* Give the real method a chance to override */
289 if (gsp->start != NULL && gsp->start(bp))
290 return;
291 if (!strcmp("GEOM::ident", bp->bio_attribute)) {
292 bp2 = g_clone_bio(bp);
293 if (bp2 == NULL) {
294 g_io_deliver(bp, ENOMEM);
295 return;
296 }
297 bp2->bio_done = g_slice_done;
298 g_io_request(bp2, cp);
299 return;
300 }
301 if (!strcmp("GEOM::kerneldump", bp->bio_attribute)) {
302 struct g_kerneldump *gkd;
303
304 gkd = (struct g_kerneldump *)bp->bio_data;
305 gkd->offset += gsp->slices[idx].offset;
306 if (gkd->length > gsp->slices[idx].length)
307 gkd->length = gsp->slices[idx].length;
308 /* now, pass it on downwards... */
309 }
310 /* FALLTHROUGH */
311 case BIO_SPEEDUP:
312 case BIO_FLUSH:
313 bp2 = g_clone_bio(bp);
314 if (bp2 == NULL) {
315 g_io_deliver(bp, ENOMEM);
316 return;
317 }
318 bp2->bio_done = g_std_done;
319 g_io_request(bp2, cp);
320 break;
321 default:
322 g_io_deliver(bp, EOPNOTSUPP);
323 return;
324 }
325 }
326
327 void
g_slice_dumpconf(struct sbuf * sb,const char * indent,struct g_geom * gp,struct g_consumer * cp,struct g_provider * pp)328 g_slice_dumpconf(struct sbuf *sb, const char *indent, struct g_geom *gp, struct g_consumer *cp, struct g_provider *pp)
329 {
330 struct g_slicer *gsp;
331
332 gsp = gp->softc;
333 if (indent == NULL) {
334 sbuf_printf(sb, " i %u", pp->index);
335 sbuf_printf(sb, " o %ju",
336 (uintmax_t)gsp->slices[pp->index].offset);
337 return;
338 }
339 if (pp != NULL) {
340 sbuf_printf(sb, "%s<index>%u</index>\n", indent, pp->index);
341 sbuf_printf(sb, "%s<length>%ju</length>\n",
342 indent, (uintmax_t)gsp->slices[pp->index].length);
343 sbuf_printf(sb, "%s<seclength>%ju</seclength>\n", indent,
344 (uintmax_t)gsp->slices[pp->index].length / 512);
345 sbuf_printf(sb, "%s<offset>%ju</offset>\n", indent,
346 (uintmax_t)gsp->slices[pp->index].offset);
347 sbuf_printf(sb, "%s<secoffset>%ju</secoffset>\n", indent,
348 (uintmax_t)gsp->slices[pp->index].offset / 512);
349 }
350 }
351
352 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,...)353 g_slice_config(struct g_geom *gp, u_int idx, int how, off_t offset, off_t length, u_int sectorsize, const char *fmt, ...)
354 {
355 struct g_provider *pp, *pp2;
356 struct g_slicer *gsp;
357 struct g_slice *gsl;
358 va_list ap;
359 struct sbuf *sb;
360 int acc;
361
362 g_trace(G_T_TOPOLOGY, "g_slice_config(%s, %d, %d)",
363 gp->name, idx, how);
364 g_topology_assert();
365 gsp = gp->softc;
366 if (idx >= gsp->nslice)
367 return(EINVAL);
368 gsl = &gsp->slices[idx];
369 pp = gsl->provider;
370 if (pp != NULL)
371 acc = pp->acr + pp->acw + pp->ace;
372 else
373 acc = 0;
374 if (acc != 0 && how != G_SLICE_CONFIG_FORCE) {
375 if (length < gsl->length)
376 return(EBUSY);
377 if (offset != gsl->offset)
378 return(EBUSY);
379 }
380 /* XXX: check offset + length <= MEDIASIZE */
381 if (how == G_SLICE_CONFIG_CHECK)
382 return (0);
383 gsl->length = length;
384 gsl->offset = offset;
385 gsl->sectorsize = sectorsize;
386 if (length == 0) {
387 if (pp == NULL)
388 return (0);
389 if (bootverbose)
390 printf("GEOM: Deconfigure %s\n", pp->name);
391 g_wither_provider(pp, ENXIO);
392 gsl->provider = NULL;
393 gsp->nprovider--;
394 return (0);
395 }
396 if (pp != NULL) {
397 if (bootverbose)
398 printf("GEOM: Reconfigure %s, start %jd length %jd end %jd\n",
399 pp->name, (intmax_t)offset, (intmax_t)length,
400 (intmax_t)(offset + length - 1));
401 g_resize_provider(pp, gsl->length);
402 return (0);
403 }
404 sb = sbuf_new_auto();
405 va_start(ap, fmt);
406 sbuf_vprintf(sb, fmt, ap);
407 va_end(ap);
408 sbuf_finish(sb);
409 pp = g_new_providerf(gp, "%s", sbuf_data(sb));
410 pp2 = LIST_FIRST(&gp->consumer)->provider;
411 pp->stripesize = pp2->stripesize;
412 pp->stripeoffset = pp2->stripeoffset + offset;
413 if (pp->stripesize > 0)
414 pp->stripeoffset %= pp->stripesize;
415 if (gsp->nhotspot == 0) {
416 pp->flags |= pp2->flags & G_PF_ACCEPT_UNMAPPED;
417 pp->flags |= G_PF_DIRECT_SEND | G_PF_DIRECT_RECEIVE;
418 }
419 if (0 && bootverbose)
420 printf("GEOM: Configure %s, start %jd length %jd end %jd\n",
421 pp->name, (intmax_t)offset, (intmax_t)length,
422 (intmax_t)(offset + length - 1));
423 pp->index = idx;
424 pp->mediasize = gsl->length;
425 pp->sectorsize = gsl->sectorsize;
426 gsl->provider = pp;
427 gsp->nprovider++;
428 g_error_provider(pp, 0);
429 sbuf_delete(sb);
430 return(0);
431 }
432
433 /*
434 * Configure "hotspots". A hotspot is a piece of the parent device which
435 * this particular slicer cares about for some reason. Typically because
436 * it contains meta-data used to configure the slicer.
437 * A hotspot is identified by its index number. The offset and length are
438 * relative to the parent device, and the three "?act" fields specify
439 * what action to take on BIO_READ, BIO_DELETE and BIO_WRITE.
440 *
441 * XXX: There may be a race relative to g_slice_start() here, if an existing
442 * XXX: hotspot is changed wile I/O is happening. Should this become a problem
443 * XXX: we can protect the hotspot stuff with a mutex.
444 */
445
446 int
g_slice_conf_hot(struct g_geom * gp,u_int idx,off_t offset,off_t length,int ract,int dact,int wact)447 g_slice_conf_hot(struct g_geom *gp, u_int idx, off_t offset, off_t length, int ract, int dact, int wact)
448 {
449 struct g_slicer *gsp;
450 struct g_slice_hot *gsl, *gsl2;
451 struct g_consumer *cp;
452 struct g_provider *pp;
453
454 g_trace(G_T_TOPOLOGY, "g_slice_conf_hot(%s, idx: %d, off: %jd, len: %jd)",
455 gp->name, idx, (intmax_t)offset, (intmax_t)length);
456 g_topology_assert();
457 gsp = gp->softc;
458 /* Deny unmapped I/O and direct dispatch if hotspots are used. */
459 if (gsp->nhotspot == 0) {
460 LIST_FOREACH(pp, &gp->provider, provider)
461 pp->flags &= ~(G_PF_ACCEPT_UNMAPPED |
462 G_PF_DIRECT_SEND | G_PF_DIRECT_RECEIVE);
463 LIST_FOREACH(cp, &gp->consumer, consumer)
464 cp->flags &= ~(G_CF_DIRECT_SEND | G_CF_DIRECT_RECEIVE);
465 }
466 gsl = gsp->hotspot;
467 if(idx >= gsp->nhotspot) {
468 gsl2 = g_malloc((idx + 1) * sizeof *gsl2, M_WAITOK | M_ZERO);
469 if (gsp->hotspot != NULL)
470 bcopy(gsp->hotspot, gsl2, gsp->nhotspot * sizeof *gsl2);
471 gsp->hotspot = gsl2;
472 if (gsp->hotspot != NULL)
473 g_free(gsl);
474 gsl = gsl2;
475 gsp->nhotspot = idx + 1;
476 }
477 gsl[idx].offset = offset;
478 gsl[idx].length = length;
479 KASSERT(!((ract | dact | wact) & G_SLICE_HOT_START)
480 || gsp->start != NULL, ("G_SLICE_HOT_START but no slice->start"));
481 /* XXX: check that we _have_ a start function if HOT_START specified */
482 gsl[idx].ract = ract;
483 gsl[idx].dact = dact;
484 gsl[idx].wact = wact;
485 return (0);
486 }
487
488 void
g_slice_orphan(struct g_consumer * cp)489 g_slice_orphan(struct g_consumer *cp)
490 {
491 struct g_geom *gp;
492
493 g_topology_assert();
494 gp = cp->geom;
495 g_trace(G_T_TOPOLOGY, "%s(%p/%s)", __func__, cp, gp->name);
496 g_wither_geom(gp, ENXIO);
497
498 /*
499 * We can safely free the softc now if there are no accesses,
500 * otherwise g_slice_access() will do that after the last close.
501 */
502 if ((cp->acr + cp->acw + cp->ace) == 0)
503 g_slice_free(gp);
504 }
505
506 void
g_slice_spoiled(struct g_consumer * cp)507 g_slice_spoiled(struct g_consumer *cp)
508 {
509
510 g_trace(G_T_TOPOLOGY, "%s(%p/%s)", __func__, cp, cp->geom->name);
511 cp->flags |= G_CF_ORPHAN;
512 g_slice_orphan(cp);
513 }
514
515 int
g_slice_destroy_geom(struct gctl_req * req,struct g_class * mp,struct g_geom * gp)516 g_slice_destroy_geom(struct gctl_req *req, struct g_class *mp, struct g_geom *gp)
517 {
518
519 g_slice_spoiled(LIST_FIRST(&gp->consumer));
520 return (0);
521 }
522
523 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)524 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)
525 {
526 struct g_geom *gp;
527 struct g_slicer *gsp;
528 struct g_consumer *cp;
529 void **vp;
530 int error;
531
532 g_topology_assert();
533 vp = (void **)extrap;
534 gp = g_new_geomf(mp, "%s", pp->name);
535 gsp = g_slice_alloc(slices, extra);
536 gsp->start = start;
537 gp->softc = gsp;
538 gp->start = g_slice_start;
539 gp->access = g_slice_access;
540 gp->orphan = g_slice_orphan;
541 gp->spoiled = g_slice_spoiled;
542 if (gp->dumpconf == NULL)
543 gp->dumpconf = g_slice_dumpconf;
544 if (gp->class->destroy_geom == NULL)
545 gp->class->destroy_geom = g_slice_destroy_geom;
546 cp = g_new_consumer(gp);
547 cp->flags |= G_CF_DIRECT_SEND | G_CF_DIRECT_RECEIVE;
548 error = g_attach(cp, pp);
549 if (error == 0)
550 error = g_access(cp, 1, 0, 0);
551 if (error) {
552 g_wither_geom(gp, ENXIO);
553 return (NULL);
554 }
555 if (extrap != NULL)
556 *vp = gsp->softc;
557 *cpp = cp;
558 return (gp);
559 }
560