xref: /freebsd/sys/geom/geom_subr.c (revision 10f0bcab61ef441cb5af32fb706688d8cbd55dc0)
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 "opt_ddb.h"
40 
41 #include <sys/param.h>
42 #include <sys/systm.h>
43 #include <sys/devicestat.h>
44 #include <sys/kernel.h>
45 #include <sys/malloc.h>
46 #include <sys/bio.h>
47 #include <sys/sysctl.h>
48 #include <sys/proc.h>
49 #include <sys/kthread.h>
50 #include <sys/lock.h>
51 #include <sys/mutex.h>
52 #include <sys/errno.h>
53 #include <sys/sbuf.h>
54 #include <geom/geom.h>
55 #include <geom/geom_int.h>
56 #include <machine/stdarg.h>
57 
58 #ifdef DDB
59 #include <ddb/ddb.h>
60 #endif
61 
62 struct class_list_head g_classes = LIST_HEAD_INITIALIZER(g_classes);
63 static struct g_tailq_head geoms = TAILQ_HEAD_INITIALIZER(geoms);
64 char *g_wait_event, *g_wait_up, *g_wait_down, *g_wait_sim;
65 
66 struct g_hh00 {
67 	struct g_class	*mp;
68 	int		error;
69 	int		post;
70 };
71 
72 /*
73  * This event offers a new class a chance to taste all preexisting providers.
74  */
75 static void
76 g_load_class(void *arg, int flag)
77 {
78 	struct g_hh00 *hh;
79 	struct g_class *mp2, *mp;
80 	struct g_geom *gp;
81 	struct g_provider *pp;
82 
83 	g_topology_assert();
84 	if (flag == EV_CANCEL)	/* XXX: can't happen ? */
85 		return;
86 	if (g_shutdown)
87 		return;
88 
89 	hh = arg;
90 	mp = hh->mp;
91 	hh->error = 0;
92 	if (hh->post) {
93 		g_free(hh);
94 		hh = NULL;
95 	}
96 	g_trace(G_T_TOPOLOGY, "g_load_class(%s)", mp->name);
97 	KASSERT(mp->name != NULL && *mp->name != '\0',
98 	    ("GEOM class has no name"));
99 	LIST_FOREACH(mp2, &g_classes, class) {
100 		if (mp2 == mp) {
101 			printf("The GEOM class %s is already loaded.\n",
102 			    mp2->name);
103 			if (hh != NULL)
104 				hh->error = EEXIST;
105 			return;
106 		} else if (strcmp(mp2->name, mp->name) == 0) {
107 			printf("A GEOM class %s is already loaded.\n",
108 			    mp2->name);
109 			if (hh != NULL)
110 				hh->error = EEXIST;
111 			return;
112 		}
113 	}
114 
115 	LIST_INIT(&mp->geom);
116 	LIST_INSERT_HEAD(&g_classes, mp, class);
117 	if (mp->init != NULL)
118 		mp->init(mp);
119 	if (mp->taste == NULL)
120 		return;
121 	LIST_FOREACH(mp2, &g_classes, class) {
122 		if (mp == mp2)
123 			continue;
124 		LIST_FOREACH(gp, &mp2->geom, geom) {
125 			LIST_FOREACH(pp, &gp->provider, provider) {
126 				mp->taste(mp, pp, 0);
127 				g_topology_assert();
128 			}
129 		}
130 	}
131 }
132 
133 static void
134 g_unload_class(void *arg, int flag)
135 {
136 	struct g_hh00 *hh;
137 	struct g_class *mp;
138 	struct g_geom *gp;
139 	struct g_provider *pp;
140 	struct g_consumer *cp;
141 	int error;
142 
143 	g_topology_assert();
144 	hh = arg;
145 	mp = hh->mp;
146 	G_VALID_CLASS(mp);
147 	g_trace(G_T_TOPOLOGY, "g_unload_class(%s)", mp->name);
148 
149 	/*
150 	 * We allow unloading if we have no geoms, or a class
151 	 * method we can use to get rid of them.
152 	 */
153 	if (!LIST_EMPTY(&mp->geom) && mp->destroy_geom == NULL) {
154 		hh->error = EOPNOTSUPP;
155 		return;
156 	}
157 
158 	/* We refuse to unload if anything is open */
159 	LIST_FOREACH(gp, &mp->geom, geom) {
160 		LIST_FOREACH(pp, &gp->provider, provider)
161 			if (pp->acr || pp->acw || pp->ace) {
162 				hh->error = EBUSY;
163 				return;
164 			}
165 		LIST_FOREACH(cp, &gp->consumer, consumer)
166 			if (cp->acr || cp->acw || cp->ace) {
167 				hh->error = EBUSY;
168 				return;
169 			}
170 	}
171 
172 	/* Bar new entries */
173 	mp->taste = NULL;
174 	mp->config = NULL;
175 
176 	error = 0;
177 	for (;;) {
178 		gp = LIST_FIRST(&mp->geom);
179 		if (gp == NULL)
180 			break;
181 		error = mp->destroy_geom(NULL, mp, gp);
182 		if (error != 0)
183 			break;
184 	}
185 	if (error == 0) {
186 		if (mp->fini != NULL)
187 			mp->fini(mp);
188 		LIST_REMOVE(mp, class);
189 	}
190 	hh->error = error;
191 	return;
192 }
193 
194 int
195 g_modevent(module_t mod, int type, void *data)
196 {
197 	struct g_hh00 *hh;
198 	int error;
199 	static int g_ignition;
200 	struct g_class *mp;
201 
202 	mp = data;
203 	if (mp->version != G_VERSION) {
204 		printf("GEOM class %s has Wrong version %x\n",
205 		    mp->name, mp->version);
206 		return (EINVAL);
207 	}
208 	if (!g_ignition) {
209 		g_ignition++;
210 		g_init();
211 	}
212 	hh = g_malloc(sizeof *hh, M_WAITOK | M_ZERO);
213 	hh->mp = data;
214 	error = EOPNOTSUPP;
215 	switch (type) {
216 	case MOD_LOAD:
217 		g_trace(G_T_TOPOLOGY, "g_modevent(%s, LOAD)", hh->mp->name);
218 		/*
219 		 * Once the system is not cold, MOD_LOAD calls will be
220 		 * from the userland and the g_event thread will be able
221 		 * to acknowledge their completion.
222 		 */
223 		if (cold) {
224 			hh->post = 1;
225 			error = g_post_event(g_load_class, hh, M_WAITOK, NULL);
226 		} else {
227 			error = g_waitfor_event(g_load_class, hh, M_WAITOK,
228 			    NULL);
229 			if (error == 0)
230 				error = hh->error;
231 			g_free(hh);
232 		}
233 		break;
234 	case MOD_UNLOAD:
235 		g_trace(G_T_TOPOLOGY, "g_modevent(%s, UNLOAD)", hh->mp->name);
236 		error = g_waitfor_event(g_unload_class, hh, M_WAITOK, NULL);
237 		if (error == 0)
238 			error = hh->error;
239 		if (error == 0) {
240 			KASSERT(LIST_EMPTY(&hh->mp->geom),
241 			    ("Unloaded class (%s) still has geom", hh->mp->name));
242 		}
243 		g_free(hh);
244 		break;
245 	default:
246 		g_free(hh);
247 		break;
248 	}
249 	return (error);
250 }
251 
252 static void
253 g_retaste_event(void *arg, int flag)
254 {
255 	struct g_class *cp, *mp;
256 	struct g_geom *gp;
257 	struct g_hh00 *hh;
258 	struct g_provider *pp;
259 
260 	g_topology_assert();
261 	if (flag == EV_CANCEL)  /* XXX: can't happen ? */
262 		return;
263 	if (g_shutdown)
264 		return;
265 
266 	hh = arg;
267 	mp = hh->mp;
268 	hh->error = 0;
269 	if (hh->post) {
270 		g_free(hh);
271 		hh = NULL;
272 	}
273 	g_trace(G_T_TOPOLOGY, "g_retaste(%s)", mp->name);
274 
275 	LIST_FOREACH(cp, &g_classes, class) {
276 		LIST_FOREACH(gp, &cp->geom, geom) {
277 			LIST_FOREACH(pp, &gp->provider, provider) {
278 				if (pp->acr || pp->acw || pp->ace)
279 					continue;
280 				mp->taste(mp, pp, 0);
281 				g_topology_assert();
282 			}
283 		}
284 	}
285 }
286 
287 int
288 g_retaste(struct g_class *mp)
289 {
290 	struct g_hh00 *hh;
291 	int error;
292 
293 	if (mp->taste == NULL)
294 		return (EINVAL);
295 
296 	hh = g_malloc(sizeof *hh, M_WAITOK | M_ZERO);
297 	hh->mp = mp;
298 
299 	if (cold) {
300 		hh->post = 1;
301 		error = g_post_event(g_retaste_event, hh, M_WAITOK, NULL);
302 	} else {
303 		error = g_waitfor_event(g_retaste_event, hh, M_WAITOK, NULL);
304 		if (error == 0)
305 			error = hh->error;
306 		g_free(hh);
307 	}
308 
309 	return (error);
310 }
311 
312 struct g_geom *
313 g_new_geomf(struct g_class *mp, const char *fmt, ...)
314 {
315 	struct g_geom *gp;
316 	va_list ap;
317 	struct sbuf *sb;
318 
319 	g_topology_assert();
320 	G_VALID_CLASS(mp);
321 	sb = sbuf_new(NULL, NULL, 0, SBUF_AUTOEXTEND);
322 	va_start(ap, fmt);
323 	sbuf_vprintf(sb, fmt, ap);
324 	va_end(ap);
325 	sbuf_finish(sb);
326 	gp = g_malloc(sizeof *gp, M_WAITOK | M_ZERO);
327 	gp->name = g_malloc(sbuf_len(sb) + 1, M_WAITOK | M_ZERO);
328 	gp->class = mp;
329 	gp->rank = 1;
330 	LIST_INIT(&gp->consumer);
331 	LIST_INIT(&gp->provider);
332 	LIST_INSERT_HEAD(&mp->geom, gp, geom);
333 	TAILQ_INSERT_HEAD(&geoms, gp, geoms);
334 	strcpy(gp->name, sbuf_data(sb));
335 	sbuf_delete(sb);
336 	/* Fill in defaults from class */
337 	gp->start = mp->start;
338 	gp->spoiled = mp->spoiled;
339 	gp->dumpconf = mp->dumpconf;
340 	gp->access = mp->access;
341 	gp->orphan = mp->orphan;
342 	gp->ioctl = mp->ioctl;
343 	return (gp);
344 }
345 
346 void
347 g_destroy_geom(struct g_geom *gp)
348 {
349 
350 	g_topology_assert();
351 	G_VALID_GEOM(gp);
352 	g_trace(G_T_TOPOLOGY, "g_destroy_geom(%p(%s))", gp, gp->name);
353 	KASSERT(LIST_EMPTY(&gp->consumer),
354 	    ("g_destroy_geom(%s) with consumer(s) [%p]",
355 	    gp->name, LIST_FIRST(&gp->consumer)));
356 	KASSERT(LIST_EMPTY(&gp->provider),
357 	    ("g_destroy_geom(%s) with provider(s) [%p]",
358 	    gp->name, LIST_FIRST(&gp->provider)));
359 	g_cancel_event(gp);
360 	LIST_REMOVE(gp, geom);
361 	TAILQ_REMOVE(&geoms, gp, geoms);
362 	g_free(gp->name);
363 	g_free(gp);
364 }
365 
366 /*
367  * This function is called (repeatedly) until the has withered away.
368  */
369 void
370 g_wither_geom(struct g_geom *gp, int error)
371 {
372 	struct g_provider *pp;
373 
374 	g_topology_assert();
375 	G_VALID_GEOM(gp);
376 	g_trace(G_T_TOPOLOGY, "g_wither_geom(%p(%s))", gp, gp->name);
377 	if (!(gp->flags & G_GEOM_WITHER)) {
378 		gp->flags |= G_GEOM_WITHER;
379 		LIST_FOREACH(pp, &gp->provider, provider)
380 			if (!(pp->flags & G_PF_ORPHAN))
381 				g_orphan_provider(pp, error);
382 	}
383 	g_do_wither();
384 }
385 
386 /*
387  * Convenience function to destroy a particular provider.
388  */
389 void
390 g_wither_provider(struct g_provider *pp, int error)
391 {
392 
393 	pp->flags |= G_PF_WITHER;
394 	if (!(pp->flags & G_PF_ORPHAN))
395 		g_orphan_provider(pp, error);
396 }
397 
398 /*
399  * This function is called (repeatedly) until the has withered away.
400  */
401 void
402 g_wither_geom_close(struct g_geom *gp, int error)
403 {
404 	struct g_consumer *cp;
405 
406 	g_topology_assert();
407 	G_VALID_GEOM(gp);
408 	g_trace(G_T_TOPOLOGY, "g_wither_geom_close(%p(%s))", gp, gp->name);
409 	LIST_FOREACH(cp, &gp->consumer, consumer)
410 		if (cp->acr || cp->acw || cp->ace)
411 			g_access(cp, -cp->acr, -cp->acw, -cp->ace);
412 	g_wither_geom(gp, error);
413 }
414 
415 /*
416  * This function is called (repeatedly) until we cant wash away more
417  * withered bits at present.  Return value contains two bits.  Bit 0
418  * set means "withering stuff we can't wash now", bit 1 means "call
419  * me again, there may be stuff I didn't get the first time around.
420  */
421 int
422 g_wither_washer()
423 {
424 	struct g_class *mp;
425 	struct g_geom *gp, *gp2;
426 	struct g_provider *pp, *pp2;
427 	struct g_consumer *cp, *cp2;
428 	int result;
429 
430 	result = 0;
431 	g_topology_assert();
432 	LIST_FOREACH(mp, &g_classes, class) {
433 		LIST_FOREACH_SAFE(gp, &mp->geom, geom, gp2) {
434 			LIST_FOREACH_SAFE(pp, &gp->provider, provider, pp2) {
435 				if (!(pp->flags & G_PF_WITHER))
436 					continue;
437 				if (LIST_EMPTY(&pp->consumers))
438 					g_destroy_provider(pp);
439 				else
440 					result |= 1;
441 			}
442 			if (!(gp->flags & G_GEOM_WITHER))
443 				continue;
444 			LIST_FOREACH_SAFE(pp, &gp->provider, provider, pp2) {
445 				if (LIST_EMPTY(&pp->consumers))
446 					g_destroy_provider(pp);
447 				else
448 					result |= 1;
449 			}
450 			LIST_FOREACH_SAFE(cp, &gp->consumer, consumer, cp2) {
451 				if (cp->acr || cp->acw || cp->ace) {
452 					result |= 1;
453 					continue;
454 				}
455 				if (cp->provider != NULL)
456 					g_detach(cp);
457 				g_destroy_consumer(cp);
458 				result |= 2;
459 			}
460 			if (LIST_EMPTY(&gp->provider) &&
461 			    LIST_EMPTY(&gp->consumer))
462 				g_destroy_geom(gp);
463 			else
464 				result |= 1;
465 		}
466 	}
467 	return (result);
468 }
469 
470 struct g_consumer *
471 g_new_consumer(struct g_geom *gp)
472 {
473 	struct g_consumer *cp;
474 
475 	g_topology_assert();
476 	G_VALID_GEOM(gp);
477 	KASSERT(!(gp->flags & G_GEOM_WITHER),
478 	    ("g_new_consumer on WITHERing geom(%s) (class %s)",
479 	    gp->name, gp->class->name));
480 	KASSERT(gp->orphan != NULL,
481 	    ("g_new_consumer on geom(%s) (class %s) without orphan",
482 	    gp->name, gp->class->name));
483 
484 	cp = g_malloc(sizeof *cp, M_WAITOK | M_ZERO);
485 	cp->geom = gp;
486 	cp->stat = devstat_new_entry(cp, -1, 0, DEVSTAT_ALL_SUPPORTED,
487 	    DEVSTAT_TYPE_DIRECT, DEVSTAT_PRIORITY_MAX);
488 	LIST_INSERT_HEAD(&gp->consumer, cp, consumer);
489 	return(cp);
490 }
491 
492 void
493 g_destroy_consumer(struct g_consumer *cp)
494 {
495 	struct g_geom *gp;
496 
497 	g_topology_assert();
498 	G_VALID_CONSUMER(cp);
499 	g_trace(G_T_TOPOLOGY, "g_destroy_consumer(%p)", cp);
500 	KASSERT (cp->provider == NULL, ("g_destroy_consumer but attached"));
501 	KASSERT (cp->acr == 0, ("g_destroy_consumer with acr"));
502 	KASSERT (cp->acw == 0, ("g_destroy_consumer with acw"));
503 	KASSERT (cp->ace == 0, ("g_destroy_consumer with ace"));
504 	g_cancel_event(cp);
505 	gp = cp->geom;
506 	LIST_REMOVE(cp, consumer);
507 	devstat_remove_entry(cp->stat);
508 	g_free(cp);
509 	if (gp->flags & G_GEOM_WITHER)
510 		g_do_wither();
511 }
512 
513 static void
514 g_new_provider_event(void *arg, int flag)
515 {
516 	struct g_class *mp;
517 	struct g_provider *pp;
518 	struct g_consumer *cp;
519 	int i;
520 
521 	g_topology_assert();
522 	if (flag == EV_CANCEL)
523 		return;
524 	if (g_shutdown)
525 		return;
526 	pp = arg;
527 	G_VALID_PROVIDER(pp);
528 	LIST_FOREACH(mp, &g_classes, class) {
529 		if (mp->taste == NULL)
530 			continue;
531 		i = 1;
532 		LIST_FOREACH(cp, &pp->consumers, consumers)
533 			if (cp->geom->class == mp)
534 				i = 0;
535 		if (!i)
536 			continue;
537 		mp->taste(mp, pp, 0);
538 		g_topology_assert();
539 	}
540 }
541 
542 
543 struct g_provider *
544 g_new_providerf(struct g_geom *gp, const char *fmt, ...)
545 {
546 	struct g_provider *pp;
547 	struct sbuf *sb;
548 	va_list ap;
549 
550 	g_topology_assert();
551 	G_VALID_GEOM(gp);
552 	KASSERT(gp->access != NULL,
553 	    ("new provider on geom(%s) without ->access (class %s)",
554 	    gp->name, gp->class->name));
555 	KASSERT(gp->start != NULL,
556 	    ("new provider on geom(%s) without ->start (class %s)",
557 	    gp->name, gp->class->name));
558 	KASSERT(!(gp->flags & G_GEOM_WITHER),
559 	    ("new provider on WITHERing geom(%s) (class %s)",
560 	    gp->name, gp->class->name));
561 	sb = sbuf_new(NULL, NULL, 0, SBUF_AUTOEXTEND);
562 	va_start(ap, fmt);
563 	sbuf_vprintf(sb, fmt, ap);
564 	va_end(ap);
565 	sbuf_finish(sb);
566 	pp = g_malloc(sizeof *pp + sbuf_len(sb) + 1, M_WAITOK | M_ZERO);
567 	pp->name = (char *)(pp + 1);
568 	strcpy(pp->name, sbuf_data(sb));
569 	sbuf_delete(sb);
570 	LIST_INIT(&pp->consumers);
571 	pp->error = ENXIO;
572 	pp->geom = gp;
573 	pp->stat = devstat_new_entry(pp, -1, 0, DEVSTAT_ALL_SUPPORTED,
574 	    DEVSTAT_TYPE_DIRECT, DEVSTAT_PRIORITY_MAX);
575 	LIST_INSERT_HEAD(&gp->provider, pp, provider);
576 	g_post_event(g_new_provider_event, pp, M_WAITOK, pp, gp, NULL);
577 	return (pp);
578 }
579 
580 void
581 g_error_provider(struct g_provider *pp, int error)
582 {
583 
584 	/* G_VALID_PROVIDER(pp);  We may not have g_topology */
585 	pp->error = error;
586 }
587 
588 struct g_provider *
589 g_provider_by_name(char const *arg)
590 {
591 	struct g_class *cp;
592 	struct g_geom *gp;
593 	struct g_provider *pp;
594 
595 	LIST_FOREACH(cp, &g_classes, class) {
596 		LIST_FOREACH(gp, &cp->geom, geom) {
597 			LIST_FOREACH(pp, &gp->provider, provider) {
598 				if (!strcmp(arg, pp->name))
599 					return (pp);
600 			}
601 		}
602 	}
603 	return (NULL);
604 }
605 
606 void
607 g_destroy_provider(struct g_provider *pp)
608 {
609 	struct g_geom *gp;
610 
611 	g_topology_assert();
612 	G_VALID_PROVIDER(pp);
613 	KASSERT(LIST_EMPTY(&pp->consumers),
614 	    ("g_destroy_provider but attached"));
615 	KASSERT (pp->acr == 0, ("g_destroy_provider with acr"));
616 	KASSERT (pp->acw == 0, ("g_destroy_provider with acw"));
617 	KASSERT (pp->acw == 0, ("g_destroy_provider with ace"));
618 	g_cancel_event(pp);
619 	LIST_REMOVE(pp, provider);
620 	gp = pp->geom;
621 	devstat_remove_entry(pp->stat);
622 	g_free(pp);
623 	if ((gp->flags & G_GEOM_WITHER))
624 		g_do_wither();
625 }
626 
627 /*
628  * We keep the "geoms" list sorted by topological order (== increasing
629  * numerical rank) at all times.
630  * When an attach is done, the attaching geoms rank is invalidated
631  * and it is moved to the tail of the list.
632  * All geoms later in the sequence has their ranks reevaluated in
633  * sequence.  If we cannot assign rank to a geom because it's
634  * prerequisites do not have rank, we move that element to the tail
635  * of the sequence with invalid rank as well.
636  * At some point we encounter our original geom and if we stil fail
637  * to assign it a rank, there must be a loop and we fail back to
638  * g_attach() which detach again and calls redo_rank again
639  * to fix up the damage.
640  * It would be much simpler code wise to do it recursively, but we
641  * can't risk that on the kernel stack.
642  */
643 
644 static int
645 redo_rank(struct g_geom *gp)
646 {
647 	struct g_consumer *cp;
648 	struct g_geom *gp1, *gp2;
649 	int n, m;
650 
651 	g_topology_assert();
652 	G_VALID_GEOM(gp);
653 
654 	/* Invalidate this geoms rank and move it to the tail */
655 	gp1 = TAILQ_NEXT(gp, geoms);
656 	if (gp1 != NULL) {
657 		gp->rank = 0;
658 		TAILQ_REMOVE(&geoms, gp, geoms);
659 		TAILQ_INSERT_TAIL(&geoms, gp, geoms);
660 	} else {
661 		gp1 = gp;
662 	}
663 
664 	/* re-rank the rest of the sequence */
665 	for (; gp1 != NULL; gp1 = gp2) {
666 		gp1->rank = 0;
667 		m = 1;
668 		LIST_FOREACH(cp, &gp1->consumer, consumer) {
669 			if (cp->provider == NULL)
670 				continue;
671 			n = cp->provider->geom->rank;
672 			if (n == 0) {
673 				m = 0;
674 				break;
675 			} else if (n >= m)
676 				m = n + 1;
677 		}
678 		gp1->rank = m;
679 		gp2 = TAILQ_NEXT(gp1, geoms);
680 
681 		/* got a rank, moving on */
682 		if (m != 0)
683 			continue;
684 
685 		/* no rank to original geom means loop */
686 		if (gp == gp1)
687 			return (ELOOP);
688 
689 		/* no rank, put it at the end move on */
690 		TAILQ_REMOVE(&geoms, gp1, geoms);
691 		TAILQ_INSERT_TAIL(&geoms, gp1, geoms);
692 	}
693 	return (0);
694 }
695 
696 int
697 g_attach(struct g_consumer *cp, struct g_provider *pp)
698 {
699 	int error;
700 
701 	g_topology_assert();
702 	G_VALID_CONSUMER(cp);
703 	G_VALID_PROVIDER(pp);
704 	KASSERT(cp->provider == NULL, ("attach but attached"));
705 	cp->provider = pp;
706 	LIST_INSERT_HEAD(&pp->consumers, cp, consumers);
707 	error = redo_rank(cp->geom);
708 	if (error) {
709 		LIST_REMOVE(cp, consumers);
710 		cp->provider = NULL;
711 		redo_rank(cp->geom);
712 	}
713 	return (error);
714 }
715 
716 void
717 g_detach(struct g_consumer *cp)
718 {
719 	struct g_provider *pp;
720 
721 	g_topology_assert();
722 	G_VALID_CONSUMER(cp);
723 	g_trace(G_T_TOPOLOGY, "g_detach(%p)", cp);
724 	KASSERT(cp->provider != NULL, ("detach but not attached"));
725 	KASSERT(cp->acr == 0, ("detach but nonzero acr"));
726 	KASSERT(cp->acw == 0, ("detach but nonzero acw"));
727 	KASSERT(cp->ace == 0, ("detach but nonzero ace"));
728 	KASSERT(cp->nstart == cp->nend,
729 	    ("detach with active requests"));
730 	pp = cp->provider;
731 	LIST_REMOVE(cp, consumers);
732 	cp->provider = NULL;
733 	if (pp->geom->flags & G_GEOM_WITHER)
734 		g_do_wither();
735 	else if (pp->flags & G_PF_WITHER)
736 		g_do_wither();
737 	redo_rank(cp->geom);
738 }
739 
740 /*
741  * g_access()
742  *
743  * Access-check with delta values.  The question asked is "can provider
744  * "cp" change the access counters by the relative amounts dc[rwe] ?"
745  */
746 
747 int
748 g_access(struct g_consumer *cp, int dcr, int dcw, int dce)
749 {
750 	struct g_provider *pp;
751 	int pr,pw,pe;
752 	int error;
753 
754 	g_topology_assert();
755 	G_VALID_CONSUMER(cp);
756 	pp = cp->provider;
757 	KASSERT(pp != NULL, ("access but not attached"));
758 	G_VALID_PROVIDER(pp);
759 
760 	g_trace(G_T_ACCESS, "g_access(%p(%s), %d, %d, %d)",
761 	    cp, pp->name, dcr, dcw, dce);
762 
763 	KASSERT(cp->acr + dcr >= 0, ("access resulting in negative acr"));
764 	KASSERT(cp->acw + dcw >= 0, ("access resulting in negative acw"));
765 	KASSERT(cp->ace + dce >= 0, ("access resulting in negative ace"));
766 	KASSERT(dcr != 0 || dcw != 0 || dce != 0, ("NOP access request"));
767 	KASSERT(pp->geom->access != NULL, ("NULL geom->access"));
768 
769 	/*
770 	 * If our class cares about being spoiled, and we have been, we
771 	 * are probably just ahead of the event telling us that.  Fail
772 	 * now rather than having to unravel this later.
773 	 */
774 	if (cp->geom->spoiled != NULL && cp->spoiled &&
775 	    (dcr > 0 || dcw > 0 || dce > 0))
776 		return (ENXIO);
777 
778 	/*
779 	 * Figure out what counts the provider would have had, if this
780 	 * consumer had (r0w0e0) at this time.
781 	 */
782 	pr = pp->acr - cp->acr;
783 	pw = pp->acw - cp->acw;
784 	pe = pp->ace - cp->ace;
785 
786 	g_trace(G_T_ACCESS,
787     "open delta:[r%dw%de%d] old:[r%dw%de%d] provider:[r%dw%de%d] %p(%s)",
788 	    dcr, dcw, dce,
789 	    cp->acr, cp->acw, cp->ace,
790 	    pp->acr, pp->acw, pp->ace,
791 	    pp, pp->name);
792 
793 	/* If foot-shooting is enabled, any open on rank#1 is OK */
794 	if ((g_debugflags & 16) && pp->geom->rank == 1)
795 		;
796 	/* If we try exclusive but already write: fail */
797 	else if (dce > 0 && pw > 0)
798 		return (EPERM);
799 	/* If we try write but already exclusive: fail */
800 	else if (dcw > 0 && pe > 0)
801 		return (EPERM);
802 	/* If we try to open more but provider is error'ed: fail */
803 	else if ((dcr > 0 || dcw > 0 || dce > 0) && pp->error != 0)
804 		return (pp->error);
805 
806 	/* Ok then... */
807 
808 	error = pp->geom->access(pp, dcr, dcw, dce);
809 	KASSERT(dcr > 0 || dcw > 0 || dce > 0 || error == 0,
810 	    ("Geom provider %s::%s failed closing ->access()",
811 	    pp->geom->class->name, pp->name));
812 	if (!error) {
813 		/*
814 		 * If we open first write, spoil any partner consumers.
815 		 * If we close last write and provider is not errored,
816 		 * trigger re-taste.
817 		 */
818 		if (pp->acw == 0 && dcw != 0)
819 			g_spoil(pp, cp);
820 		else if (pp->acw != 0 && pp->acw == -dcw && pp->error == 0 &&
821 		    !(pp->geom->flags & G_GEOM_WITHER))
822 			g_post_event(g_new_provider_event, pp, M_WAITOK,
823 			    pp, NULL);
824 
825 		pp->acr += dcr;
826 		pp->acw += dcw;
827 		pp->ace += dce;
828 		cp->acr += dcr;
829 		cp->acw += dcw;
830 		cp->ace += dce;
831 		if (pp->acr != 0 || pp->acw != 0 || pp->ace != 0)
832 			KASSERT(pp->sectorsize > 0,
833 			    ("Provider %s lacks sectorsize", pp->name));
834 	}
835 	return (error);
836 }
837 
838 int
839 g_handleattr_int(struct bio *bp, const char *attribute, int val)
840 {
841 
842 	return (g_handleattr(bp, attribute, &val, sizeof val));
843 }
844 
845 int
846 g_handleattr_off_t(struct bio *bp, const char *attribute, off_t val)
847 {
848 
849 	return (g_handleattr(bp, attribute, &val, sizeof val));
850 }
851 
852 int
853 g_handleattr_str(struct bio *bp, const char *attribute, char *str)
854 {
855 
856 	return (g_handleattr(bp, attribute, str, 0));
857 }
858 
859 int
860 g_handleattr(struct bio *bp, const char *attribute, void *val, int len)
861 {
862 	int error = 0;
863 
864 	if (strcmp(bp->bio_attribute, attribute))
865 		return (0);
866 	if (len == 0) {
867 		bzero(bp->bio_data, bp->bio_length);
868 		if (strlcpy(bp->bio_data, val, bp->bio_length) >=
869 		    bp->bio_length) {
870 			printf("%s: %s bio_length %jd len %zu -> EFAULT\n",
871 			    __func__, bp->bio_to->name,
872 			    (intmax_t)bp->bio_length, strlen(val));
873 			error = EFAULT;
874 		}
875 	} else if (bp->bio_length == len) {
876 		bcopy(val, bp->bio_data, len);
877 		bp->bio_completed = len;
878 	} else {
879 		printf("%s: %s bio_length %jd len %d -> EFAULT\n", __func__,
880 		    bp->bio_to->name, (intmax_t)bp->bio_length, len);
881 		error = EFAULT;
882 	}
883 	g_io_deliver(bp, error);
884 	return (1);
885 }
886 
887 int
888 g_std_access(struct g_provider *pp,
889 	int dr __unused, int dw __unused, int de __unused)
890 {
891 
892 	g_topology_assert();
893 	G_VALID_PROVIDER(pp);
894         return (0);
895 }
896 
897 void
898 g_std_done(struct bio *bp)
899 {
900 	struct bio *bp2;
901 
902 	bp2 = bp->bio_parent;
903 	if (bp2->bio_error == 0)
904 		bp2->bio_error = bp->bio_error;
905 	bp2->bio_completed += bp->bio_completed;
906 	g_destroy_bio(bp);
907 	bp2->bio_inbed++;
908 	if (bp2->bio_children == bp2->bio_inbed)
909 		g_io_deliver(bp2, bp2->bio_error);
910 }
911 
912 /* XXX: maybe this is only g_slice_spoiled */
913 
914 void
915 g_std_spoiled(struct g_consumer *cp)
916 {
917 	struct g_geom *gp;
918 	struct g_provider *pp;
919 
920 	g_topology_assert();
921 	G_VALID_CONSUMER(cp);
922 	g_trace(G_T_TOPOLOGY, "g_std_spoiled(%p)", cp);
923 	g_detach(cp);
924 	gp = cp->geom;
925 	LIST_FOREACH(pp, &gp->provider, provider)
926 		g_orphan_provider(pp, ENXIO);
927 	g_destroy_consumer(cp);
928 	if (LIST_EMPTY(&gp->provider) && LIST_EMPTY(&gp->consumer))
929 		g_destroy_geom(gp);
930 	else
931 		gp->flags |= G_GEOM_WITHER;
932 }
933 
934 /*
935  * Spoiling happens when a provider is opened for writing, but consumers
936  * which are configured by in-band data are attached (slicers for instance).
937  * Since the write might potentially change the in-band data, such consumers
938  * need to re-evaluate their existence after the writing session closes.
939  * We do this by (offering to) tear them down when the open for write happens
940  * in return for a re-taste when it closes again.
941  * Together with the fact that such consumers grab an 'e' bit whenever they
942  * are open, regardless of mode, this ends up DTRT.
943  */
944 
945 static void
946 g_spoil_event(void *arg, int flag)
947 {
948 	struct g_provider *pp;
949 	struct g_consumer *cp, *cp2;
950 
951 	g_topology_assert();
952 	if (flag == EV_CANCEL)
953 		return;
954 	pp = arg;
955 	G_VALID_PROVIDER(pp);
956 	for (cp = LIST_FIRST(&pp->consumers); cp != NULL; cp = cp2) {
957 		cp2 = LIST_NEXT(cp, consumers);
958 		if (!cp->spoiled)
959 			continue;
960 		cp->spoiled = 0;
961 		if (cp->geom->spoiled == NULL)
962 			continue;
963 		cp->geom->spoiled(cp);
964 		g_topology_assert();
965 	}
966 }
967 
968 void
969 g_spoil(struct g_provider *pp, struct g_consumer *cp)
970 {
971 	struct g_consumer *cp2;
972 
973 	g_topology_assert();
974 	G_VALID_PROVIDER(pp);
975 	G_VALID_CONSUMER(cp);
976 
977 	LIST_FOREACH(cp2, &pp->consumers, consumers) {
978 		if (cp2 == cp)
979 			continue;
980 /*
981 		KASSERT(cp2->acr == 0, ("spoiling cp->acr = %d", cp2->acr));
982 		KASSERT(cp2->acw == 0, ("spoiling cp->acw = %d", cp2->acw));
983 */
984 		KASSERT(cp2->ace == 0, ("spoiling cp->ace = %d", cp2->ace));
985 		cp2->spoiled++;
986 	}
987 	g_post_event(g_spoil_event, pp, M_WAITOK, pp, NULL);
988 }
989 
990 int
991 g_getattr__(const char *attr, struct g_consumer *cp, void *var, int len)
992 {
993 	int error, i;
994 
995 	i = len;
996 	error = g_io_getattr(attr, cp, &i, var);
997 	if (error)
998 		return (error);
999 	if (i != len)
1000 		return (EINVAL);
1001 	return (0);
1002 }
1003 
1004 #if defined(DIAGNOSTIC) || defined(DDB)
1005 /*
1006  * This function walks (topologically unsafely) the mesh and return a
1007  * non-zero integer if it finds the argument pointer is an object.
1008  * The return value indicates which type of object it is belived to be.
1009  * If topology is not locked, this function is potentially dangerous,
1010  * but since it is for debugging purposes and can be useful for instance
1011  * from DDB, we do not assert topology lock is held.
1012  */
1013 int
1014 g_valid_obj(void const *ptr)
1015 {
1016 	struct g_class *mp;
1017 	struct g_geom *gp;
1018 	struct g_consumer *cp;
1019 	struct g_provider *pp;
1020 
1021 	LIST_FOREACH(mp, &g_classes, class) {
1022 		if (ptr == mp)
1023 			return (1);
1024 		LIST_FOREACH(gp, &mp->geom, geom) {
1025 			if (ptr == gp)
1026 				return (2);
1027 			LIST_FOREACH(cp, &gp->consumer, consumer)
1028 				if (ptr == cp)
1029 					return (3);
1030 			LIST_FOREACH(pp, &gp->provider, provider)
1031 				if (ptr == pp)
1032 					return (4);
1033 		}
1034 	}
1035 	return(0);
1036 }
1037 #endif
1038 
1039 #ifdef DDB
1040 
1041 #define	gprintf(...)	do {						\
1042 	printf("%*s", indent, "");					\
1043 	printf(__VA_ARGS__);						\
1044 } while (0)
1045 #define	gprintln(...)	do {						\
1046 	gprintf(__VA_ARGS__);						\
1047 	printf("\n");							\
1048 } while (0)
1049 
1050 #define	ADDFLAG(obj, flag, sflag)	do {				\
1051 	if ((obj)->flags & (flag)) {					\
1052 		if (comma)						\
1053 			strlcat(str, ",", size);			\
1054 		strlcat(str, (sflag), size);				\
1055 		comma = 1;						\
1056 	}								\
1057 } while (0)
1058 
1059 static char *
1060 provider_flags_to_string(struct g_provider *pp, char *str, size_t size)
1061 {
1062 	int comma = 0;
1063 
1064 	bzero(str, size);
1065 	if (pp->flags == 0) {
1066 		strlcpy(str, "NONE", size);
1067 		return (str);
1068 	}
1069 	ADDFLAG(pp, G_PF_CANDELETE, "G_PF_CANDELETE");
1070 	ADDFLAG(pp, G_PF_WITHER, "G_PF_WITHER");
1071 	ADDFLAG(pp, G_PF_ORPHAN, "G_PF_ORPHAN");
1072 	return (str);
1073 }
1074 
1075 static char *
1076 geom_flags_to_string(struct g_geom *gp, char *str, size_t size)
1077 {
1078 	int comma = 0;
1079 
1080 	bzero(str, size);
1081 	if (gp->flags == 0) {
1082 		strlcpy(str, "NONE", size);
1083 		return (str);
1084 	}
1085 	ADDFLAG(gp, G_GEOM_WITHER, "G_GEOM_WITHER");
1086 	return (str);
1087 }
1088 static void
1089 db_show_geom_consumer(int indent, struct g_consumer *cp)
1090 {
1091 
1092 	if (indent == 0) {
1093 		gprintln("consumer: %p", cp);
1094 		gprintln("  class:    %s (%p)", cp->geom->class->name,
1095 		    cp->geom->class);
1096 		gprintln("  geom:     %s (%p)", cp->geom->name, cp->geom);
1097 		if (cp->provider == NULL)
1098 			gprintln("  provider: none");
1099 		else {
1100 			gprintln("  provider: %s (%p)", cp->provider->name,
1101 			    cp->provider);
1102 		}
1103 		gprintln("  access:   r%dw%de%d", cp->acr, cp->acw, cp->ace);
1104 		gprintln("  spoiled:  %d", cp->spoiled);
1105 		gprintln("  nstart:   %u", cp->nstart);
1106 		gprintln("  nend:     %u", cp->nend);
1107 	} else {
1108 		gprintf("consumer: %p (%s), access=r%dw%de%d", cp,
1109 		    cp->provider != NULL ? cp->provider->name : "none",
1110 		    cp->acr, cp->acw, cp->ace);
1111 		if (cp->spoiled)
1112 			printf(", spoiled=%d", cp->spoiled);
1113 		printf("\n");
1114 	}
1115 }
1116 
1117 static void
1118 db_show_geom_provider(int indent, struct g_provider *pp)
1119 {
1120 	struct g_consumer *cp;
1121 	char flags[64];
1122 
1123 	if (indent == 0) {
1124 		gprintln("provider: %s (%p)", pp->name, pp);
1125 		gprintln("  class:        %s (%p)", pp->geom->class->name,
1126 		    pp->geom->class);
1127 		gprintln("  geom:         %s (%p)", pp->geom->name, pp->geom);
1128 		gprintln("  mediasize:    %jd", (intmax_t)pp->mediasize);
1129 		gprintln("  sectorsize:   %u", pp->sectorsize);
1130 		gprintln("  stripesize:   %u", pp->stripesize);
1131 		gprintln("  stripeoffset: %u", pp->stripeoffset);
1132 		gprintln("  access:       r%dw%de%d", pp->acr, pp->acw,
1133 		    pp->ace);
1134 		gprintln("  flags:        %s (0x%04x)",
1135 		    provider_flags_to_string(pp, flags, sizeof(flags)),
1136 		    pp->flags);
1137 		gprintln("  error:        %d", pp->error);
1138 		gprintln("  nstart:       %u", pp->nstart);
1139 		gprintln("  nend:         %u", pp->nend);
1140 		if (LIST_EMPTY(&pp->consumers))
1141 			gprintln("  consumers:    none");
1142 	} else {
1143 		gprintf("provider: %s (%p), access=r%dw%de%d",
1144 		    pp->name, pp, pp->acr, pp->acw, pp->ace);
1145 		if (pp->flags != 0) {
1146 			printf(", flags=%s (0x%04x)",
1147 			    provider_flags_to_string(pp, flags, sizeof(flags)),
1148 			    pp->flags);
1149 		}
1150 		printf("\n");
1151 	}
1152 	if (!LIST_EMPTY(&pp->consumers)) {
1153 		LIST_FOREACH(cp, &pp->consumers, consumers)
1154 			db_show_geom_consumer(indent + 2, cp);
1155 	}
1156 }
1157 
1158 static void
1159 db_show_geom_geom(int indent, struct g_geom *gp)
1160 {
1161 	struct g_provider *pp;
1162 	struct g_consumer *cp;
1163 	char flags[64];
1164 
1165 	if (indent == 0) {
1166 		gprintln("geom: %s (%p)", gp->name, gp);
1167 		gprintln("  class:     %s (%p)", gp->class->name, gp->class);
1168 		gprintln("  flags:     %s (0x%04x)",
1169 		    geom_flags_to_string(gp, flags, sizeof(flags)), gp->flags);
1170 		gprintln("  rank:      %d", gp->rank);
1171 		if (LIST_EMPTY(&gp->provider))
1172 			gprintln("  providers: none");
1173 		if (LIST_EMPTY(&gp->consumer))
1174 			gprintln("  consumers: none");
1175 	} else {
1176 		gprintf("geom: %s (%p), rank=%d", gp->name, gp, gp->rank);
1177 		if (gp->flags != 0) {
1178 			printf(", flags=%s (0x%04x)",
1179 			    geom_flags_to_string(gp, flags, sizeof(flags)),
1180 			    gp->flags);
1181 		}
1182 		printf("\n");
1183 	}
1184 	if (!LIST_EMPTY(&gp->provider)) {
1185 		LIST_FOREACH(pp, &gp->provider, provider)
1186 			db_show_geom_provider(indent + 2, pp);
1187 	}
1188 	if (!LIST_EMPTY(&gp->consumer)) {
1189 		LIST_FOREACH(cp, &gp->consumer, consumer)
1190 			db_show_geom_consumer(indent + 2, cp);
1191 	}
1192 }
1193 
1194 static void
1195 db_show_geom_class(struct g_class *mp)
1196 {
1197 	struct g_geom *gp;
1198 
1199 	printf("class: %s (%p)\n", mp->name, mp);
1200 	LIST_FOREACH(gp, &mp->geom, geom)
1201 		db_show_geom_geom(2, gp);
1202 }
1203 
1204 /*
1205  * Print the GEOM topology or the given object.
1206  */
1207 DB_SHOW_COMMAND(geom, db_show_geom)
1208 {
1209 	struct g_class *mp;
1210 
1211 	if (!have_addr) {
1212 		/* No address given, print the entire topology. */
1213 		LIST_FOREACH(mp, &g_classes, class) {
1214 			db_show_geom_class(mp);
1215 			printf("\n");
1216 		}
1217 	} else {
1218 		switch (g_valid_obj((void *)addr)) {
1219 		case 1:
1220 			db_show_geom_class((struct g_class *)addr);
1221 			break;
1222 		case 2:
1223 			db_show_geom_geom(0, (struct g_geom *)addr);
1224 			break;
1225 		case 3:
1226 			db_show_geom_consumer(0, (struct g_consumer *)addr);
1227 			break;
1228 		case 4:
1229 			db_show_geom_provider(0, (struct g_provider *)addr);
1230 			break;
1231 		default:
1232 			printf("Not a GEOM object.\n");
1233 			break;
1234 		}
1235 	}
1236 }
1237 
1238 #undef	gprintf
1239 #undef	gprintln
1240 #undef	ADDFLAG
1241 
1242 #endif	/* DDB */
1243