xref: /freebsd/sys/geom/geom_subr.c (revision b52f49a9a0f22207ad5130ad8faba08de3ed23d8)
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$");
38 
39 #include <sys/param.h>
40 #include <sys/systm.h>
41 #include <sys/devicestat.h>
42 #include <sys/kernel.h>
43 #include <sys/malloc.h>
44 #include <sys/bio.h>
45 #include <sys/sysctl.h>
46 #include <sys/proc.h>
47 #include <sys/kthread.h>
48 #include <sys/lock.h>
49 #include <sys/mutex.h>
50 #include <sys/errno.h>
51 #include <sys/sbuf.h>
52 #include <geom/geom.h>
53 #include <geom/geom_int.h>
54 #include <machine/stdarg.h>
55 
56 struct class_list_head g_classes = LIST_HEAD_INITIALIZER(g_classes);
57 static struct g_tailq_head geoms = TAILQ_HEAD_INITIALIZER(geoms);
58 char *g_wait_event, *g_wait_up, *g_wait_down, *g_wait_sim;
59 
60 
61 struct g_hh00 {
62 	struct g_class	*mp;
63 	int		error;
64 };
65 
66 /*
67  * This event offers a new class a chance to taste all preexisting providers.
68  */
69 static void
70 g_load_class(void *arg, int flag)
71 {
72 	struct g_hh00 *hh;
73 	struct g_class *mp2, *mp;
74 	struct g_geom *gp;
75 	struct g_provider *pp;
76 
77 	g_topology_assert();
78 	if (flag == EV_CANCEL)	/* XXX: can't happen ? */
79 		return;
80 	if (g_shutdown)
81 		return;
82 
83 	hh = arg;
84 	mp = hh->mp;
85 	g_free(hh);
86 	g_trace(G_T_TOPOLOGY, "g_load_class(%s)", mp->name);
87 	LIST_FOREACH(mp2, &g_classes, class) {
88 		KASSERT(mp2 != mp,
89 		    ("The GEOM class %s already loaded", mp2->name));
90 		KASSERT(strcmp(mp2->name, mp->name) != 0,
91 		    ("A GEOM class named %s is already loaded", mp2->name));
92 	}
93 
94 	if (mp->init != NULL)
95 		mp->init(mp);
96 	LIST_INIT(&mp->geom);
97 	LIST_INSERT_HEAD(&g_classes, mp, class);
98 	if (mp->taste == NULL)
99 		return;
100 	LIST_FOREACH(mp2, &g_classes, class) {
101 		if (mp == mp2)
102 			continue;
103 		LIST_FOREACH(gp, &mp2->geom, geom) {
104 			LIST_FOREACH(pp, &gp->provider, provider) {
105 				mp->taste(mp, pp, 0);
106 				g_topology_assert();
107 			}
108 		}
109 	}
110 }
111 
112 static void
113 g_unload_class(void *arg, int flag)
114 {
115 	struct g_hh00 *hh;
116 	struct g_class *mp;
117 	struct g_geom *gp;
118 	struct g_provider *pp;
119 	struct g_consumer *cp;
120 	int error;
121 
122 	g_topology_assert();
123 	hh = arg;
124 	mp = hh->mp;
125 	g_trace(G_T_TOPOLOGY, "g_unload_class(%s)", mp->name);
126 	if (mp->destroy_geom == NULL) {
127 		hh->error = EOPNOTSUPP;
128 		return;
129 	}
130 
131 	/* We refuse to unload if anything is open */
132 	LIST_FOREACH(gp, &mp->geom, geom) {
133 		LIST_FOREACH(pp, &gp->provider, provider)
134 			if (pp->acr || pp->acw || pp->ace) {
135 				hh->error = EBUSY;
136 				return;
137 			}
138 		LIST_FOREACH(cp, &gp->consumer, consumer)
139 			if (cp->acr || cp->acw || cp->ace) {
140 				hh->error = EBUSY;
141 				return;
142 			}
143 	}
144 
145 	/* Bar new entries */
146 	mp->taste = NULL;
147 	mp->config = NULL;
148 
149 	error = 0;
150 	LIST_FOREACH(gp, &mp->geom, geom) {
151 		error = mp->destroy_geom(NULL, mp, gp);
152 		if (error != 0)
153 			break;
154 	}
155 	if (error == 0) {
156 		LIST_REMOVE(mp, class);
157 		if (mp->fini != NULL)
158 			mp->fini(mp);
159 	}
160 	hh->error = error;
161 	return;
162 }
163 
164 int
165 g_modevent(module_t mod, int type, void *data)
166 {
167 	struct g_hh00 *hh;
168 	int error;
169 	static int g_ignition;
170 
171 	if (!g_ignition) {
172 		g_ignition++;
173 		g_init();
174 	}
175 	hh = g_malloc(sizeof *hh, M_WAITOK | M_ZERO);
176 	hh->mp = data;
177 	error = EOPNOTSUPP;
178 	switch (type) {
179 	case MOD_LOAD:
180 		g_trace(G_T_TOPOLOGY, "g_modevent(%s, LOAD)", hh->mp->name);
181 		g_post_event(g_load_class, hh, M_WAITOK, NULL);
182 		error = 0;
183 		break;
184 	case MOD_UNLOAD:
185 		g_trace(G_T_TOPOLOGY, "g_modevent(%s, UNLOAD)", hh->mp->name);
186 		error = g_waitfor_event(g_unload_class, hh, M_WAITOK, NULL);
187 		if (error == 0)
188 			error = hh->error;
189 		g_waitidle();
190 		KASSERT(LIST_EMPTY(&hh->mp->geom),
191 		    ("Unloaded class (%s) still has geom", hh->mp->name));
192 		g_free(hh);
193 		break;
194 	}
195 	return (error);
196 }
197 
198 struct g_geom *
199 g_new_geomf(struct g_class *mp, const char *fmt, ...)
200 {
201 	struct g_geom *gp;
202 	va_list ap;
203 	struct sbuf *sb;
204 
205 	g_topology_assert();
206 	sb = sbuf_new(NULL, NULL, 0, SBUF_AUTOEXTEND);
207 	va_start(ap, fmt);
208 	sbuf_vprintf(sb, fmt, ap);
209 	va_end(ap);
210 	sbuf_finish(sb);
211 	gp = g_malloc(sizeof *gp, M_WAITOK | M_ZERO);
212 	gp->name = g_malloc(sbuf_len(sb) + 1, M_WAITOK | M_ZERO);
213 	gp->class = mp;
214 	gp->rank = 1;
215 	LIST_INIT(&gp->consumer);
216 	LIST_INIT(&gp->provider);
217 	LIST_INSERT_HEAD(&mp->geom, gp, geom);
218 	TAILQ_INSERT_HEAD(&geoms, gp, geoms);
219 	strcpy(gp->name, sbuf_data(sb));
220 	sbuf_delete(sb);
221 	return (gp);
222 }
223 
224 void
225 g_destroy_geom(struct g_geom *gp)
226 {
227 
228 	g_trace(G_T_TOPOLOGY, "g_destroy_geom(%p(%s))", gp, gp->name);
229 	g_topology_assert();
230 	KASSERT(LIST_EMPTY(&gp->consumer),
231 	    ("g_destroy_geom(%s) with consumer(s) [%p]",
232 	    gp->name, LIST_FIRST(&gp->consumer)));
233 	KASSERT(LIST_EMPTY(&gp->provider),
234 	    ("g_destroy_geom(%s) with provider(s) [%p]",
235 	    gp->name, LIST_FIRST(&gp->consumer)));
236 	g_cancel_event(gp);
237 	LIST_REMOVE(gp, geom);
238 	TAILQ_REMOVE(&geoms, gp, geoms);
239 	g_free(gp->name);
240 	g_free(gp);
241 }
242 
243 /*
244  * This function is called (repeatedly) until has withered away.
245  */
246 void
247 g_wither_geom(struct g_geom *gp, int error)
248 {
249 	struct g_provider *pp, *pp2;
250 	struct g_consumer *cp, *cp2;
251 	static int once_is_enough;
252 
253 	if (once_is_enough)
254 		return;
255 	once_is_enough = 1;
256 	g_trace(G_T_TOPOLOGY, "g_wither_geom(%p(%s))", gp, gp->name);
257 	g_topology_assert();
258 	if (!(gp->flags & G_GEOM_WITHER)) {
259 		gp->flags |= G_GEOM_WITHER;
260 		LIST_FOREACH(pp, &gp->provider, provider)
261 			g_orphan_provider(pp, error);
262 	}
263 	for (pp = LIST_FIRST(&gp->provider); pp != NULL; pp = pp2) {
264 		pp2 = LIST_NEXT(pp, provider);
265 		if (!LIST_EMPTY(&pp->consumers))
266 			continue;
267 		g_destroy_provider(pp);
268 	}
269 	for (cp = LIST_FIRST(&gp->consumer); cp != NULL; cp = cp2) {
270 		cp2 = LIST_NEXT(cp, consumer);
271 		if (cp->acr || cp->acw || cp->ace)
272 			continue;
273 		g_detach(cp);
274 		g_destroy_consumer(cp);
275 	}
276 	if (LIST_EMPTY(&gp->provider) && LIST_EMPTY(&gp->consumer))
277 		g_destroy_geom(gp);
278 	once_is_enough = 0;
279 }
280 
281 struct g_consumer *
282 g_new_consumer(struct g_geom *gp)
283 {
284 	struct g_consumer *cp;
285 
286 	g_topology_assert();
287 	KASSERT(gp->orphan != NULL,
288 	    ("g_new_consumer on geom(%s) (class %s) without orphan",
289 	    gp->name, gp->class->name));
290 
291 	cp = g_malloc(sizeof *cp, M_WAITOK | M_ZERO);
292 	cp->geom = gp;
293 	cp->stat = devstat_new_entry(cp, -1, 0, DEVSTAT_ALL_SUPPORTED,
294 	    DEVSTAT_TYPE_DIRECT, DEVSTAT_PRIORITY_MAX);
295 	LIST_INSERT_HEAD(&gp->consumer, cp, consumer);
296 	return(cp);
297 }
298 
299 void
300 g_destroy_consumer(struct g_consumer *cp)
301 {
302 	struct g_geom *gp;
303 
304 	g_trace(G_T_TOPOLOGY, "g_destroy_consumer(%p)", cp);
305 	g_topology_assert();
306 	KASSERT (cp->provider == NULL, ("g_destroy_consumer but attached"));
307 	KASSERT (cp->acr == 0, ("g_destroy_consumer with acr"));
308 	KASSERT (cp->acw == 0, ("g_destroy_consumer with acw"));
309 	KASSERT (cp->ace == 0, ("g_destroy_consumer with ace"));
310 	g_cancel_event(cp);
311 	gp = cp->geom;
312 	LIST_REMOVE(cp, consumer);
313 	devstat_remove_entry(cp->stat);
314 	g_free(cp);
315 	if (gp->flags & G_GEOM_WITHER)
316 		g_wither_geom(gp, 0);
317 }
318 
319 static void
320 g_new_provider_event(void *arg, int flag)
321 {
322 	struct g_class *mp;
323 	struct g_provider *pp;
324 	struct g_consumer *cp;
325 	int i;
326 
327 	g_topology_assert();
328 	if (flag == EV_CANCEL)
329 		return;
330 	if (g_shutdown)
331 		return;
332 	pp = arg;
333 	LIST_FOREACH(mp, &g_classes, class) {
334 		if (mp->taste == NULL)
335 			continue;
336 		i = 1;
337 		LIST_FOREACH(cp, &pp->consumers, consumers)
338 			if (cp->geom->class == mp)
339 				i = 0;
340 		if (!i)
341 			continue;
342 		mp->taste(mp, pp, 0);
343 		g_topology_assert();
344 	}
345 }
346 
347 
348 struct g_provider *
349 g_new_providerf(struct g_geom *gp, const char *fmt, ...)
350 {
351 	struct g_provider *pp;
352 	struct sbuf *sb;
353 	va_list ap;
354 
355 	g_topology_assert();
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_malloc(sizeof *pp + sbuf_len(sb) + 1, M_WAITOK | M_ZERO);
362 	pp->name = (char *)(pp + 1);
363 	strcpy(pp->name, sbuf_data(sb));
364 	sbuf_delete(sb);
365 	LIST_INIT(&pp->consumers);
366 	pp->error = ENXIO;
367 	pp->geom = gp;
368 	pp->stat = devstat_new_entry(pp, -1, 0, DEVSTAT_ALL_SUPPORTED,
369 	    DEVSTAT_TYPE_DIRECT, DEVSTAT_PRIORITY_MAX);
370 	LIST_INSERT_HEAD(&gp->provider, pp, provider);
371 	g_post_event(g_new_provider_event, pp, M_WAITOK, pp, NULL);
372 	return (pp);
373 }
374 
375 void
376 g_error_provider(struct g_provider *pp, int error)
377 {
378 
379 	pp->error = error;
380 }
381 
382 struct g_provider *
383 g_provider_by_name(char const *arg)
384 {
385 	struct g_class *cp;
386 	struct g_geom *gp;
387 	struct g_provider *pp;
388 
389 	LIST_FOREACH(cp, &g_classes, class) {
390 		LIST_FOREACH(gp, &cp->geom, geom) {
391 			LIST_FOREACH(pp, &gp->provider, provider) {
392 				if (!strcmp(arg, pp->name))
393 					return (pp);
394 			}
395 		}
396 	}
397 	return (NULL);
398 }
399 
400 void
401 g_destroy_provider(struct g_provider *pp)
402 {
403 	struct g_geom *gp;
404 
405 	g_topology_assert();
406 	KASSERT(LIST_EMPTY(&pp->consumers),
407 	    ("g_destroy_provider but attached"));
408 	KASSERT (pp->acr == 0, ("g_destroy_provider with acr"));
409 	KASSERT (pp->acw == 0, ("g_destroy_provider with acw"));
410 	KASSERT (pp->acw == 0, ("g_destroy_provider with ace"));
411 	g_cancel_event(pp);
412 	LIST_REMOVE(pp, provider);
413 	gp = pp->geom;
414 	devstat_remove_entry(pp->stat);
415 	g_free(pp);
416 	if ((gp->flags & G_GEOM_WITHER))
417 		g_wither_geom(gp, 0);
418 }
419 
420 /*
421  * We keep the "geoms" list sorted by topological order (== increasing
422  * numerical rank) at all times.
423  * When an attach is done, the attaching geoms rank is invalidated
424  * and it is moved to the tail of the list.
425  * All geoms later in the sequence has their ranks reevaluated in
426  * sequence.  If we cannot assign rank to a geom because it's
427  * prerequisites do not have rank, we move that element to the tail
428  * of the sequence with invalid rank as well.
429  * At some point we encounter our original geom and if we stil fail
430  * to assign it a rank, there must be a loop and we fail back to
431  * g_attach() which detach again and calls redo_rank again
432  * to fix up the damage.
433  * It would be much simpler code wise to do it recursively, but we
434  * can't risk that on the kernel stack.
435  */
436 
437 static int
438 redo_rank(struct g_geom *gp)
439 {
440 	struct g_consumer *cp;
441 	struct g_geom *gp1, *gp2;
442 	int n, m;
443 
444 	g_topology_assert();
445 
446 	/* Invalidate this geoms rank and move it to the tail */
447 	gp1 = TAILQ_NEXT(gp, geoms);
448 	if (gp1 != NULL) {
449 		gp->rank = 0;
450 		TAILQ_REMOVE(&geoms, gp, geoms);
451 		TAILQ_INSERT_TAIL(&geoms, gp, geoms);
452 	} else {
453 		gp1 = gp;
454 	}
455 
456 	/* re-rank the rest of the sequence */
457 	for (; gp1 != NULL; gp1 = gp2) {
458 		gp1->rank = 0;
459 		m = 1;
460 		LIST_FOREACH(cp, &gp1->consumer, consumer) {
461 			if (cp->provider == NULL)
462 				continue;
463 			n = cp->provider->geom->rank;
464 			if (n == 0) {
465 				m = 0;
466 				break;
467 			} else if (n >= m)
468 				m = n + 1;
469 		}
470 		gp1->rank = m;
471 		gp2 = TAILQ_NEXT(gp1, geoms);
472 
473 		/* got a rank, moving on */
474 		if (m != 0)
475 			continue;
476 
477 		/* no rank to original geom means loop */
478 		if (gp == gp1)
479 			return (ELOOP);
480 
481 		/* no rank, put it at the end move on */
482 		TAILQ_REMOVE(&geoms, gp1, geoms);
483 		TAILQ_INSERT_TAIL(&geoms, gp1, geoms);
484 	}
485 	return (0);
486 }
487 
488 int
489 g_attach(struct g_consumer *cp, struct g_provider *pp)
490 {
491 	int error;
492 
493 	g_topology_assert();
494 	KASSERT(cp->provider == NULL, ("attach but attached"));
495 	cp->provider = pp;
496 	LIST_INSERT_HEAD(&pp->consumers, cp, consumers);
497 	error = redo_rank(cp->geom);
498 	if (error) {
499 		LIST_REMOVE(cp, consumers);
500 		cp->provider = NULL;
501 		redo_rank(cp->geom);
502 	}
503 	return (error);
504 }
505 
506 void
507 g_detach(struct g_consumer *cp)
508 {
509 	struct g_provider *pp;
510 
511 	g_trace(G_T_TOPOLOGY, "g_detach(%p)", cp);
512 	KASSERT(cp != (void*)0xd0d0d0d0, ("ARGH!"));
513 	g_topology_assert();
514 	KASSERT(cp->provider != NULL, ("detach but not attached"));
515 	KASSERT(cp->acr == 0, ("detach but nonzero acr"));
516 	KASSERT(cp->acw == 0, ("detach but nonzero acw"));
517 	KASSERT(cp->ace == 0, ("detach but nonzero ace"));
518 	KASSERT(cp->nstart == cp->nend,
519 	    ("detach with active requests"));
520 	pp = cp->provider;
521 	LIST_REMOVE(cp, consumers);
522 	cp->provider = NULL;
523 	if (pp->geom->flags & G_GEOM_WITHER)
524 		g_wither_geom(pp->geom, 0);
525 	redo_rank(cp->geom);
526 }
527 
528 
529 /*
530  * g_access_abs()
531  *
532  * Access-check with absolute new values:  Just fall through
533  * and use the relative version.
534  */
535 int
536 g_access_abs(struct g_consumer *cp, int acr, int acw, int ace)
537 {
538 
539 	g_topology_assert();
540 	return(g_access_rel(cp,
541 		acr - cp->acr,
542 		acw - cp->acw,
543 		ace - cp->ace));
544 }
545 
546 /*
547  * g_access_rel()
548  *
549  * Access-check with delta values.  The question asked is "can provider
550  * "cp" change the access counters by the relative amounts dc[rwe] ?"
551  */
552 
553 int
554 g_access_rel(struct g_consumer *cp, int dcr, int dcw, int dce)
555 {
556 	struct g_provider *pp;
557 	int pr,pw,pe;
558 	int error;
559 
560 	pp = cp->provider;
561 
562 	g_trace(G_T_ACCESS, "g_access_rel(%p(%s), %d, %d, %d)",
563 	    cp, pp->name, dcr, dcw, dce);
564 
565 	g_topology_assert();
566 	KASSERT(cp->provider != NULL, ("access but not attached"));
567 	KASSERT(cp->acr + dcr >= 0, ("access resulting in negative acr"));
568 	KASSERT(cp->acw + dcw >= 0, ("access resulting in negative acw"));
569 	KASSERT(cp->ace + dce >= 0, ("access resulting in negative ace"));
570 	KASSERT(pp->geom->access != NULL, ("NULL geom->access"));
571 
572 	/*
573 	 * If our class cares about being spoiled, and we have been, we
574 	 * are probably just ahead of the event telling us that.  Fail
575 	 * now rather than having to unravel this later.
576 	 */
577 	if (cp->geom->spoiled != NULL && cp->spoiled) {
578 		KASSERT(dcr <= 0, ("spoiled but dcr = %d", dcr));
579 		KASSERT(dcw <= 0, ("spoiled but dce = %d", dcw));
580 		KASSERT(dce <= 0, ("spoiled but dcw = %d", dce));
581 	}
582 
583 	/*
584 	 * Figure out what counts the provider would have had, if this
585 	 * consumer had (r0w0e0) at this time.
586 	 */
587 	pr = pp->acr - cp->acr;
588 	pw = pp->acw - cp->acw;
589 	pe = pp->ace - cp->ace;
590 
591 	g_trace(G_T_ACCESS,
592     "open delta:[r%dw%de%d] old:[r%dw%de%d] provider:[r%dw%de%d] %p(%s)",
593 	    dcr, dcw, dce,
594 	    cp->acr, cp->acw, cp->ace,
595 	    pp->acr, pp->acw, pp->ace,
596 	    pp, pp->name);
597 
598 	/* If foot-shooting is enabled, any open on rank#1 is OK */
599 	if ((g_debugflags & 16) && pp->geom->rank == 1)
600 		;
601 	/* If we try exclusive but already write: fail */
602 	else if (dce > 0 && pw > 0)
603 		return (EPERM);
604 	/* If we try write but already exclusive: fail */
605 	else if (dcw > 0 && pe > 0)
606 		return (EPERM);
607 	/* If we try to open more but provider is error'ed: fail */
608 	else if ((dcr > 0 || dcw > 0 || dce > 0) && pp->error != 0)
609 		return (pp->error);
610 
611 	/* Ok then... */
612 
613 	error = pp->geom->access(pp, dcr, dcw, dce);
614 	if (!error) {
615 		/*
616 		 * If we open first write, spoil any partner consumers.
617 		 * If we close last write, trigger re-taste.
618 		 */
619 		if (pp->acw == 0 && dcw != 0)
620 			g_spoil(pp, cp);
621 		else if (pp->acw != 0 && pp->acw == -dcw &&
622 		    !(pp->geom->flags & G_GEOM_WITHER))
623 			g_post_event(g_new_provider_event, pp, M_WAITOK,
624 			    pp, NULL);
625 
626 		pp->acr += dcr;
627 		pp->acw += dcw;
628 		pp->ace += dce;
629 		cp->acr += dcr;
630 		cp->acw += dcw;
631 		cp->ace += dce;
632 	}
633 	return (error);
634 }
635 
636 int
637 g_handleattr_int(struct bio *bp, const char *attribute, int val)
638 {
639 
640 	return (g_handleattr(bp, attribute, &val, sizeof val));
641 }
642 
643 int
644 g_handleattr_off_t(struct bio *bp, const char *attribute, off_t val)
645 {
646 
647 	return (g_handleattr(bp, attribute, &val, sizeof val));
648 }
649 
650 int
651 g_handleattr(struct bio *bp, const char *attribute, void *val, int len)
652 {
653 	int error;
654 
655 	if (strcmp(bp->bio_attribute, attribute))
656 		return (0);
657 	if (bp->bio_length != len) {
658 		printf("bio_length %jd len %d -> EFAULT\n",
659 		    (intmax_t)bp->bio_length, len);
660 		error = EFAULT;
661 	} else {
662 		error = 0;
663 		bcopy(val, bp->bio_data, len);
664 		bp->bio_completed = len;
665 	}
666 	g_io_deliver(bp, error);
667 	return (1);
668 }
669 
670 int
671 g_std_access(struct g_provider *pp __unused,
672 	int dr __unused, int dw __unused, int de __unused)
673 {
674 
675         return (0);
676 }
677 
678 void
679 g_std_done(struct bio *bp)
680 {
681 	struct bio *bp2;
682 
683 	bp2 = bp->bio_parent;
684 	if (bp2->bio_error == 0)
685 		bp2->bio_error = bp->bio_error;
686 	bp2->bio_completed += bp->bio_completed;
687 	g_destroy_bio(bp);
688 	bp2->bio_inbed++;
689 	if (bp2->bio_children == bp2->bio_inbed)
690 		g_io_deliver(bp2, bp2->bio_error);
691 }
692 
693 /* XXX: maybe this is only g_slice_spoiled */
694 
695 void
696 g_std_spoiled(struct g_consumer *cp)
697 {
698 	struct g_geom *gp;
699 	struct g_provider *pp;
700 
701 	g_trace(G_T_TOPOLOGY, "g_std_spoiled(%p)", cp);
702 	g_topology_assert();
703 	g_detach(cp);
704 	gp = cp->geom;
705 	LIST_FOREACH(pp, &gp->provider, provider)
706 		g_orphan_provider(pp, ENXIO);
707 	g_destroy_consumer(cp);
708 	if (LIST_EMPTY(&gp->provider) && LIST_EMPTY(&gp->consumer))
709 		g_destroy_geom(gp);
710 	else
711 		gp->flags |= G_GEOM_WITHER;
712 }
713 
714 /*
715  * Spoiling happens when a provider is opened for writing, but consumers
716  * which are configured by in-band data are attached (slicers for instance).
717  * Since the write might potentially change the in-band data, such consumers
718  * need to re-evaluate their existence after the writing session closes.
719  * We do this by (offering to) tear them down when the open for write happens
720  * in return for a re-taste when it closes again.
721  * Together with the fact that such consumers grab an 'e' bit whenever they
722  * are open, regardless of mode, this ends up DTRT.
723  */
724 
725 static void
726 g_spoil_event(void *arg, int flag)
727 {
728 	struct g_provider *pp;
729 	struct g_consumer *cp, *cp2;
730 
731 	g_topology_assert();
732 	if (flag == EV_CANCEL)
733 		return;
734 	pp = arg;
735 	for (cp = LIST_FIRST(&pp->consumers); cp != NULL; cp = cp2) {
736 		cp2 = LIST_NEXT(cp, consumers);
737 		if (!cp->spoiled)
738 			continue;
739 		cp->spoiled = 0;
740 		if (cp->geom->spoiled == NULL)
741 			continue;
742 		cp->geom->spoiled(cp);
743 		g_topology_assert();
744 	}
745 }
746 
747 void
748 g_spoil(struct g_provider *pp, struct g_consumer *cp)
749 {
750 	struct g_consumer *cp2;
751 
752 	g_topology_assert();
753 
754 	LIST_FOREACH(cp2, &pp->consumers, consumers) {
755 		if (cp2 == cp)
756 			continue;
757 /*
758 		KASSERT(cp2->acr == 0, ("spoiling cp->acr = %d", cp2->acr));
759 		KASSERT(cp2->acw == 0, ("spoiling cp->acw = %d", cp2->acw));
760 */
761 		KASSERT(cp2->ace == 0, ("spoiling cp->ace = %d", cp2->ace));
762 		cp2->spoiled++;
763 	}
764 	g_post_event(g_spoil_event, pp, M_WAITOK, pp, NULL);
765 }
766 
767 int
768 g_getattr__(const char *attr, struct g_consumer *cp, void *var, int len)
769 {
770 	int error, i;
771 
772 	i = len;
773 	error = g_io_getattr(attr, cp, &i, var);
774 	if (error)
775 		return (error);
776 	if (i != len)
777 		return (EINVAL);
778 	return (0);
779 }
780 
781 /*
782  * Check if the given pointer is a live object
783  */
784 
785 void
786 g_sanity(void const *ptr)
787 {
788 	struct g_class *mp;
789 	struct g_geom *gp;
790 	struct g_consumer *cp;
791 	struct g_provider *pp;
792 
793 	if (!(g_debugflags & 0x8))
794 		return;
795 	LIST_FOREACH(mp, &g_classes, class) {
796 		KASSERT(mp != ptr, ("Ptr is live class"));
797 		LIST_FOREACH(gp, &mp->geom, geom) {
798 			KASSERT(gp != ptr, ("Ptr is live geom"));
799 			KASSERT(gp->name != ptr, ("Ptr is live geom's name"));
800 			LIST_FOREACH(cp, &gp->consumer, consumer) {
801 				KASSERT(cp != ptr, ("Ptr is live consumer"));
802 			}
803 			LIST_FOREACH(pp, &gp->provider, provider) {
804 				KASSERT(pp != ptr, ("Ptr is live provider"));
805 			}
806 		}
807 	}
808 }
809 
810