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