xref: /freebsd/sys/geom/geom_subr.c (revision f388c311d3d97f5aff47015228033c99b4e6e1eb)
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 "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/proc.h>
48 #include <sys/kthread.h>
49 #include <sys/lock.h>
50 #include <sys/mutex.h>
51 #include <sys/errno.h>
52 #include <sys/sbuf.h>
53 #include <sys/sdt.h>
54 #include <sys/stdarg.h>
55 #include <geom/geom.h>
56 #include <geom/geom_dbg.h>
57 #include <geom/geom_int.h>
58 
59 #ifdef DDB
60 #include <ddb/ddb.h>
61 #endif
62 
63 #ifdef KDB
64 #include <sys/kdb.h>
65 #endif
66 
67 SDT_PROVIDER_DEFINE(geom);
68 
69 struct class_list_head g_classes = LIST_HEAD_INITIALIZER(g_classes);
70 static struct g_tailq_head geoms = TAILQ_HEAD_INITIALIZER(geoms);
71 char *g_wait_event, *g_wait_up, *g_wait_down;
72 
73 struct g_hh00 {
74 	struct g_class		*mp;
75 	struct g_provider	*pp;
76 	off_t			size;
77 	int			error;
78 	int			post;
79 };
80 
81 void
g_dbg_printf(const char * classname,int lvl,struct bio * bp,const char * format,...)82 g_dbg_printf(const char *classname, int lvl, struct bio *bp,
83     const char *format,
84     ...)
85 {
86 #ifndef PRINTF_BUFR_SIZE
87 #define PRINTF_BUFR_SIZE 64
88 #endif
89 	char bufr[PRINTF_BUFR_SIZE];
90 	struct sbuf sb, *sbp __unused;
91 	va_list ap;
92 
93 	sbp = sbuf_new(&sb, bufr, sizeof(bufr), SBUF_FIXEDLEN);
94 	KASSERT(sbp != NULL, ("sbuf_new misused?"));
95 
96 	sbuf_set_drain(&sb, sbuf_printf_drain, NULL);
97 
98 	sbuf_cat(&sb, classname);
99 	if (lvl >= 0)
100 		sbuf_printf(&sb, "[%d]", lvl);
101 
102 	va_start(ap, format);
103 	sbuf_vprintf(&sb, format, ap);
104 	va_end(ap);
105 
106 	if (bp != NULL) {
107 		sbuf_putc(&sb, ' ');
108 		g_format_bio(&sb, bp);
109 	}
110 
111 	/* Terminate the debug line with a single '\n'. */
112 	sbuf_nl_terminate(&sb);
113 
114 	/* Flush line to printf. */
115 	sbuf_finish(&sb);
116 	sbuf_delete(&sb);
117 }
118 
119 /*
120  * This event offers a new class a chance to taste all preexisting providers.
121  */
122 static void
g_load_class(void * arg,int flag)123 g_load_class(void *arg, int flag)
124 {
125 	struct g_hh00 *hh;
126 	struct g_class *mp2, *mp;
127 	struct g_geom *gp;
128 	struct g_provider *pp;
129 
130 	g_topology_assert();
131 	if (flag == EV_CANCEL)	/* XXX: can't happen ? */
132 		return;
133 	if (g_shutdown)
134 		return;
135 
136 	hh = arg;
137 	mp = hh->mp;
138 	hh->error = 0;
139 	if (hh->post) {
140 		g_free(hh);
141 		hh = NULL;
142 	}
143 	g_trace(G_T_TOPOLOGY, "g_load_class(%s)", mp->name);
144 	KASSERT(mp->name != NULL && *mp->name != '\0',
145 	    ("GEOM class has no name"));
146 	LIST_FOREACH(mp2, &g_classes, class) {
147 		if (mp2 == mp) {
148 			printf("The GEOM class %s is already loaded.\n",
149 			    mp2->name);
150 			if (hh != NULL)
151 				hh->error = EEXIST;
152 			return;
153 		} else if (strcmp(mp2->name, mp->name) == 0) {
154 			printf("A GEOM class %s is already loaded.\n",
155 			    mp2->name);
156 			if (hh != NULL)
157 				hh->error = EEXIST;
158 			return;
159 		}
160 	}
161 
162 	LIST_INIT(&mp->geom);
163 	LIST_INSERT_HEAD(&g_classes, mp, class);
164 	if (mp->init != NULL)
165 		mp->init(mp);
166 	if (mp->taste == NULL)
167 		return;
168 	LIST_FOREACH(mp2, &g_classes, class) {
169 		if (mp == mp2)
170 			continue;
171 		LIST_FOREACH(gp, &mp2->geom, geom) {
172 			LIST_FOREACH(pp, &gp->provider, provider) {
173 				mp->taste(mp, pp, 0);
174 				g_topology_assert();
175 			}
176 		}
177 	}
178 }
179 
180 static int
g_unload_class(struct g_class * mp)181 g_unload_class(struct g_class *mp)
182 {
183 	struct g_geom *gp;
184 	struct g_provider *pp;
185 	struct g_consumer *cp;
186 	int error;
187 
188 	g_topology_lock();
189 	g_trace(G_T_TOPOLOGY, "g_unload_class(%s)", mp->name);
190 retry:
191 	G_VALID_CLASS(mp);
192 	LIST_FOREACH(gp, &mp->geom, geom) {
193 		/* We refuse to unload if anything is open */
194 		LIST_FOREACH(pp, &gp->provider, provider)
195 			if (pp->acr || pp->acw || pp->ace) {
196 				g_topology_unlock();
197 				return (EBUSY);
198 			}
199 		LIST_FOREACH(cp, &gp->consumer, consumer)
200 			if (cp->acr || cp->acw || cp->ace) {
201 				g_topology_unlock();
202 				return (EBUSY);
203 			}
204 		/* If the geom is withering, wait for it to finish. */
205 		if (gp->flags & G_GEOM_WITHER) {
206 			g_topology_sleep(mp, 1);
207 			goto retry;
208 		}
209 	}
210 
211 	/*
212 	 * We allow unloading if we have no geoms, or a class
213 	 * method we can use to get rid of them.
214 	 */
215 	if (!LIST_EMPTY(&mp->geom) && mp->destroy_geom == NULL) {
216 		g_topology_unlock();
217 		return (EOPNOTSUPP);
218 	}
219 
220 	/* Bar new entries */
221 	mp->taste = NULL;
222 
223 	LIST_FOREACH(gp, &mp->geom, geom) {
224 		error = mp->destroy_geom(NULL, mp, gp);
225 		if (error != 0) {
226 			g_topology_unlock();
227 			return (error);
228 		}
229 	}
230 	/* Wait for withering to finish. */
231 	for (;;) {
232 		gp = LIST_FIRST(&mp->geom);
233 		if (gp == NULL)
234 			break;
235 		KASSERT(gp->flags & G_GEOM_WITHER,
236 		   ("Non-withering geom in class %s", mp->name));
237 		g_topology_sleep(mp, 1);
238 	}
239 	G_VALID_CLASS(mp);
240 	if (mp->fini != NULL)
241 		mp->fini(mp);
242 	LIST_REMOVE(mp, class);
243 	g_topology_unlock();
244 
245 	return (0);
246 }
247 
248 int
g_modevent(module_t mod,int type,void * data)249 g_modevent(module_t mod, int type, void *data)
250 {
251 	struct g_hh00 *hh;
252 	int error;
253 	static int g_ignition;
254 	struct g_class *mp;
255 
256 	mp = data;
257 	if (mp->version != G_VERSION) {
258 		printf("GEOM class %s has Wrong version %x\n",
259 		    mp->name, mp->version);
260 		return (EINVAL);
261 	}
262 	if (!g_ignition) {
263 		g_ignition++;
264 		g_init();
265 	}
266 	error = EOPNOTSUPP;
267 	switch (type) {
268 	case MOD_LOAD:
269 		g_trace(G_T_TOPOLOGY, "g_modevent(%s, LOAD)", mp->name);
270 		hh = g_malloc(sizeof *hh, M_WAITOK | M_ZERO);
271 		hh->mp = mp;
272 		/*
273 		 * Once the system is not cold, MOD_LOAD calls will be
274 		 * from the userland and the g_event thread will be able
275 		 * to acknowledge their completion.
276 		 */
277 		if (cold) {
278 			hh->post = 1;
279 			error = g_post_event(g_load_class, hh, M_WAITOK, NULL);
280 		} else {
281 			error = g_waitfor_event(g_load_class, hh, M_WAITOK,
282 			    NULL);
283 			if (error == 0)
284 				error = hh->error;
285 			g_free(hh);
286 		}
287 		break;
288 	case MOD_UNLOAD:
289 		g_trace(G_T_TOPOLOGY, "g_modevent(%s, UNLOAD)", mp->name);
290 		error = g_unload_class(mp);
291 		if (error == 0) {
292 			KASSERT(LIST_EMPTY(&mp->geom),
293 			    ("Unloaded class (%s) still has geom", mp->name));
294 		}
295 		break;
296 	}
297 	return (error);
298 }
299 
300 static void
g_retaste_event(void * arg,int flag)301 g_retaste_event(void *arg, int flag)
302 {
303 	struct g_class *mp, *mp2;
304 	struct g_geom *gp;
305 	struct g_hh00 *hh;
306 	struct g_provider *pp;
307 	struct g_consumer *cp;
308 
309 	g_topology_assert();
310 	if (flag == EV_CANCEL)  /* XXX: can't happen ? */
311 		return;
312 	if (g_shutdown || g_notaste)
313 		return;
314 
315 	hh = arg;
316 	mp = hh->mp;
317 	hh->error = 0;
318 	if (hh->post) {
319 		g_free(hh);
320 		hh = NULL;
321 	}
322 	g_trace(G_T_TOPOLOGY, "g_retaste(%s)", mp->name);
323 
324 	LIST_FOREACH(mp2, &g_classes, class) {
325 		LIST_FOREACH(gp, &mp2->geom, geom) {
326 			LIST_FOREACH(pp, &gp->provider, provider) {
327 				if (pp->acr || pp->acw || pp->ace)
328 					continue;
329 				LIST_FOREACH(cp, &pp->consumers, consumers) {
330 					if (cp->geom->class == mp &&
331 					    (cp->flags & G_CF_ORPHAN) == 0)
332 						break;
333 				}
334 				if (cp != NULL) {
335 					cp->flags |= G_CF_ORPHAN;
336 					g_wither_geom(cp->geom, ENXIO);
337 				}
338 				mp->taste(mp, pp, 0);
339 				g_topology_assert();
340 			}
341 		}
342 	}
343 }
344 
345 int
g_retaste(struct g_class * mp)346 g_retaste(struct g_class *mp)
347 {
348 	struct g_hh00 *hh;
349 	int error;
350 
351 	if (mp->taste == NULL)
352 		return (EINVAL);
353 
354 	hh = g_malloc(sizeof *hh, M_WAITOK | M_ZERO);
355 	hh->mp = mp;
356 
357 	if (cold) {
358 		hh->post = 1;
359 		error = g_post_event(g_retaste_event, hh, M_WAITOK, NULL);
360 	} else {
361 		error = g_waitfor_event(g_retaste_event, hh, M_WAITOK, NULL);
362 		if (error == 0)
363 			error = hh->error;
364 		g_free(hh);
365 	}
366 
367 	return (error);
368 }
369 
370 struct g_geom *
g_new_geomf(struct g_class * mp,const char * fmt,...)371 g_new_geomf(struct g_class *mp, const char *fmt, ...)
372 {
373 	struct g_geom *gp;
374 	va_list ap;
375 	struct sbuf *sb;
376 
377 	g_topology_assert();
378 	G_VALID_CLASS(mp);
379 	sb = sbuf_new_auto();
380 	va_start(ap, fmt);
381 	sbuf_vprintf(sb, fmt, ap);
382 	va_end(ap);
383 	sbuf_finish(sb);
384 	gp = g_malloc(sizeof *gp + sbuf_len(sb) + 1, M_WAITOK | M_ZERO);
385 	gp->name = (char *)(gp + 1);
386 	gp->class = mp;
387 	gp->rank = 1;
388 	LIST_INIT(&gp->consumer);
389 	LIST_INIT(&gp->provider);
390 	LIST_INSERT_HEAD(&mp->geom, gp, geom);
391 	TAILQ_INSERT_HEAD(&geoms, gp, geoms);
392 	strcpy(gp->name, sbuf_data(sb));
393 	sbuf_delete(sb);
394 	/* Fill in defaults from class */
395 	gp->start = mp->start;
396 	gp->spoiled = mp->spoiled;
397 	gp->attrchanged = mp->attrchanged;
398 	gp->providergone = mp->providergone;
399 	gp->dumpconf = mp->dumpconf;
400 	gp->access = mp->access;
401 	gp->orphan = mp->orphan;
402 	gp->ioctl = mp->ioctl;
403 	gp->resize = mp->resize;
404 	return (gp);
405 }
406 
407 void
g_destroy_geom(struct g_geom * gp)408 g_destroy_geom(struct g_geom *gp)
409 {
410 
411 	g_topology_assert();
412 	G_VALID_GEOM(gp);
413 	g_trace(G_T_TOPOLOGY, "g_destroy_geom(%p(%s))", gp, gp->name);
414 	KASSERT(LIST_EMPTY(&gp->consumer),
415 	    ("g_destroy_geom(%s) with consumer(s) [%p]",
416 	    gp->name, LIST_FIRST(&gp->consumer)));
417 	KASSERT(LIST_EMPTY(&gp->provider),
418 	    ("g_destroy_geom(%s) with provider(s) [%p]",
419 	    gp->name, LIST_FIRST(&gp->provider)));
420 	g_cancel_event(gp);
421 	LIST_REMOVE(gp, geom);
422 	TAILQ_REMOVE(&geoms, gp, geoms);
423 	g_free(gp);
424 }
425 
426 /*
427  * This function is called (repeatedly) until the geom has withered away.
428  */
429 void
g_wither_geom(struct g_geom * gp,int error)430 g_wither_geom(struct g_geom *gp, int error)
431 {
432 	struct g_provider *pp;
433 
434 	g_topology_assert();
435 	G_VALID_GEOM(gp);
436 	g_trace(G_T_TOPOLOGY, "g_wither_geom(%p(%s))", gp, gp->name);
437 	if (!(gp->flags & G_GEOM_WITHER)) {
438 		gp->flags |= G_GEOM_WITHER;
439 		LIST_FOREACH(pp, &gp->provider, provider)
440 			if (!(pp->flags & G_PF_ORPHAN))
441 				g_orphan_provider(pp, error);
442 	}
443 	g_do_wither();
444 }
445 
446 /*
447  * Convenience function to destroy a particular provider.
448  */
449 void
g_wither_provider(struct g_provider * pp,int error)450 g_wither_provider(struct g_provider *pp, int error)
451 {
452 
453 	pp->flags |= G_PF_WITHER;
454 	if (!(pp->flags & G_PF_ORPHAN))
455 		g_orphan_provider(pp, error);
456 }
457 
458 /*
459  * This function is called (repeatedly) until the has withered away.
460  */
461 void
g_wither_geom_close(struct g_geom * gp,int error)462 g_wither_geom_close(struct g_geom *gp, int error)
463 {
464 	struct g_consumer *cp;
465 
466 	g_topology_assert();
467 	G_VALID_GEOM(gp);
468 	g_trace(G_T_TOPOLOGY, "g_wither_geom_close(%p(%s))", gp, gp->name);
469 	LIST_FOREACH(cp, &gp->consumer, consumer)
470 		if (cp->acr || cp->acw || cp->ace)
471 			g_access(cp, -cp->acr, -cp->acw, -cp->ace);
472 	g_wither_geom(gp, error);
473 }
474 
475 /*
476  * This function is called (repeatedly) until we can't wash away more
477  * withered bits at present.
478  */
479 void
g_wither_washer(void)480 g_wither_washer(void)
481 {
482 	struct g_class *mp;
483 	struct g_geom *gp, *gp2;
484 	struct g_provider *pp, *pp2;
485 	struct g_consumer *cp, *cp2;
486 
487 	g_topology_assert();
488 	LIST_FOREACH(mp, &g_classes, class) {
489 		LIST_FOREACH_SAFE(gp, &mp->geom, geom, gp2) {
490 			LIST_FOREACH_SAFE(pp, &gp->provider, provider, pp2) {
491 				if (!(pp->flags & G_PF_WITHER))
492 					continue;
493 				if (LIST_EMPTY(&pp->consumers))
494 					g_destroy_provider(pp);
495 			}
496 			if (!(gp->flags & G_GEOM_WITHER))
497 				continue;
498 			LIST_FOREACH_SAFE(pp, &gp->provider, provider, pp2) {
499 				if (LIST_EMPTY(&pp->consumers))
500 					g_destroy_provider(pp);
501 			}
502 			LIST_FOREACH_SAFE(cp, &gp->consumer, consumer, cp2) {
503 				if (cp->acr || cp->acw || cp->ace)
504 					continue;
505 				if (cp->provider != NULL)
506 					g_detach(cp);
507 				g_destroy_consumer(cp);
508 			}
509 			if (LIST_EMPTY(&gp->provider) &&
510 			    LIST_EMPTY(&gp->consumer))
511 				g_destroy_geom(gp);
512 		}
513 	}
514 }
515 
516 struct g_consumer *
g_new_consumer(struct g_geom * gp)517 g_new_consumer(struct g_geom *gp)
518 {
519 	struct g_consumer *cp;
520 
521 	g_topology_assert();
522 	G_VALID_GEOM(gp);
523 	KASSERT(!(gp->flags & G_GEOM_WITHER),
524 	    ("g_new_consumer on WITHERing geom(%s) (class %s)",
525 	    gp->name, gp->class->name));
526 	KASSERT(gp->orphan != NULL,
527 	    ("g_new_consumer on geom(%s) (class %s) without orphan",
528 	    gp->name, gp->class->name));
529 
530 	cp = g_malloc(sizeof *cp, M_WAITOK | M_ZERO);
531 	cp->geom = gp;
532 	cp->stat = devstat_new_entry(cp, -1, 0, DEVSTAT_ALL_SUPPORTED,
533 	    DEVSTAT_TYPE_DIRECT, DEVSTAT_PRIORITY_MAX);
534 	LIST_INSERT_HEAD(&gp->consumer, cp, consumer);
535 	return(cp);
536 }
537 
538 void
g_destroy_consumer(struct g_consumer * cp)539 g_destroy_consumer(struct g_consumer *cp)
540 {
541 	struct g_geom *gp;
542 
543 	g_topology_assert();
544 	G_VALID_CONSUMER(cp);
545 	g_trace(G_T_TOPOLOGY, "g_destroy_consumer(%p)", cp);
546 	KASSERT (cp->provider == NULL, ("g_destroy_consumer but attached"));
547 	KASSERT (cp->acr == 0, ("g_destroy_consumer with acr"));
548 	KASSERT (cp->acw == 0, ("g_destroy_consumer with acw"));
549 	KASSERT (cp->ace == 0, ("g_destroy_consumer with ace"));
550 	g_cancel_event(cp);
551 	gp = cp->geom;
552 	LIST_REMOVE(cp, consumer);
553 	devstat_remove_entry(cp->stat);
554 	g_free(cp);
555 	if (gp->flags & G_GEOM_WITHER)
556 		g_do_wither();
557 }
558 
559 static void
g_new_provider_event(void * arg,int flag)560 g_new_provider_event(void *arg, int flag)
561 {
562 	struct g_class *mp;
563 	struct g_provider *pp;
564 	struct g_consumer *cp, *next_cp;
565 
566 	g_topology_assert();
567 	if (flag == EV_CANCEL)
568 		return;
569 	if (g_shutdown)
570 		return;
571 	pp = arg;
572 	G_VALID_PROVIDER(pp);
573 	if ((pp->flags & G_PF_WITHER) != 0)
574 		return;
575 	LIST_FOREACH_SAFE(cp, &pp->consumers, consumers, next_cp) {
576 		if ((cp->flags & G_CF_ORPHAN) == 0 &&
577 		    cp->geom->attrchanged != NULL)
578 			cp->geom->attrchanged(cp, "GEOM::media");
579 	}
580 	if (g_notaste)
581 		return;
582 	LIST_FOREACH(mp, &g_classes, class) {
583 		if (mp->taste == NULL)
584 			continue;
585 		LIST_FOREACH(cp, &pp->consumers, consumers)
586 			if (cp->geom->class == mp &&
587 			    (cp->flags & G_CF_ORPHAN) == 0)
588 				break;
589 		if (cp != NULL)
590 			continue;
591 		mp->taste(mp, pp, 0);
592 		g_topology_assert();
593 	}
594 }
595 
596 struct g_provider *
g_new_providerf(struct g_geom * gp,const char * fmt,...)597 g_new_providerf(struct g_geom *gp, const char *fmt, ...)
598 {
599 	struct g_provider *pp;
600 	struct sbuf *sb;
601 	va_list ap;
602 
603 	g_topology_assert();
604 	G_VALID_GEOM(gp);
605 	KASSERT(gp->access != NULL,
606 	    ("new provider on geom(%s) without ->access (class %s)",
607 	    gp->name, gp->class->name));
608 	KASSERT(gp->start != NULL,
609 	    ("new provider on geom(%s) without ->start (class %s)",
610 	    gp->name, gp->class->name));
611 	KASSERT(!(gp->flags & G_GEOM_WITHER),
612 	    ("new provider on WITHERing geom(%s) (class %s)",
613 	    gp->name, gp->class->name));
614 	sb = sbuf_new_auto();
615 	va_start(ap, fmt);
616 	sbuf_vprintf(sb, fmt, ap);
617 	va_end(ap);
618 	sbuf_finish(sb);
619 	pp = g_malloc(sizeof *pp + sbuf_len(sb) + 1, M_WAITOK | M_ZERO);
620 	pp->name = (char *)(pp + 1);
621 	strcpy(pp->name, sbuf_data(sb));
622 	sbuf_delete(sb);
623 	LIST_INIT(&pp->consumers);
624 	LIST_INIT(&pp->aliases);
625 	pp->error = ENXIO;
626 	pp->geom = gp;
627 	pp->stat = devstat_new_entry(pp, -1, 0, DEVSTAT_ALL_SUPPORTED,
628 	    DEVSTAT_TYPE_DIRECT, DEVSTAT_PRIORITY_MAX);
629 	LIST_INSERT_HEAD(&gp->provider, pp, provider);
630 	g_post_event(g_new_provider_event, pp, M_WAITOK, pp, gp, NULL);
631 	return (pp);
632 }
633 
634 void
g_provider_add_alias(struct g_provider * pp,const char * fmt,...)635 g_provider_add_alias(struct g_provider *pp, const char *fmt, ...)
636 {
637 	struct sbuf *sb;
638 	struct g_geom_alias *gap;
639 	va_list ap;
640 
641 	/*
642 	 * Generate the alias string and save it in the list.
643 	 */
644 	sb = sbuf_new_auto();
645 	va_start(ap, fmt);
646 	sbuf_vprintf(sb, fmt, ap);
647 	va_end(ap);
648 	sbuf_finish(sb);
649 
650 	LIST_FOREACH(gap, &pp->aliases, ga_next) {
651 		if (strcmp(gap->ga_alias, sbuf_data(sb)) != 0)
652 			continue;
653 		/* Don't re-add the same alias. */
654 		sbuf_delete(sb);
655 		return;
656 	}
657 
658 	gap = g_malloc(sizeof(*gap) + sbuf_len(sb) + 1, M_WAITOK | M_ZERO);
659 	memcpy((char *)(gap + 1), sbuf_data(sb), sbuf_len(sb));
660 	sbuf_delete(sb);
661 	gap->ga_alias = (const char *)(gap + 1);
662 	LIST_INSERT_HEAD(&pp->aliases, gap, ga_next);
663 }
664 
665 void
g_error_provider(struct g_provider * pp,int error)666 g_error_provider(struct g_provider *pp, int error)
667 {
668 
669 	/* G_VALID_PROVIDER(pp);  We may not have g_topology */
670 	pp->error = error;
671 }
672 
673 static void
g_resize_provider_event(void * arg,int flag)674 g_resize_provider_event(void *arg, int flag)
675 {
676 	struct g_hh00 *hh;
677 	struct g_class *mp;
678 	struct g_geom *gp;
679 	struct g_provider *pp;
680 	struct g_consumer *cp, *cp2;
681 	off_t size;
682 
683 	g_topology_assert();
684 	if (g_shutdown)
685 		return;
686 
687 	hh = arg;
688 	pp = hh->pp;
689 	size = hh->size;
690 	g_free(hh);
691 
692 	G_VALID_PROVIDER(pp);
693 	KASSERT(!(pp->flags & G_PF_WITHER),
694 	    ("g_resize_provider_event but withered"));
695 	g_trace(G_T_TOPOLOGY, "g_resize_provider_event(%p)", pp);
696 
697 	LIST_FOREACH_SAFE(cp, &pp->consumers, consumers, cp2) {
698 		gp = cp->geom;
699 		if (gp->resize == NULL && size < pp->mediasize) {
700 			/*
701 			 * XXX: g_dev_orphan method does deferred destroying
702 			 * and it is possible, that other event could already
703 			 * call the orphan method. Check consumer's flags to
704 			 * do not schedule it twice.
705 			 */
706 			if (cp->flags & G_CF_ORPHAN)
707 				continue;
708 			cp->flags |= G_CF_ORPHAN;
709 			cp->geom->orphan(cp);
710 		}
711 	}
712 
713 	pp->mediasize = size;
714 
715 	LIST_FOREACH_SAFE(cp, &pp->consumers, consumers, cp2) {
716 		gp = cp->geom;
717 		if ((gp->flags & G_GEOM_WITHER) == 0 && gp->resize != NULL)
718 			gp->resize(cp);
719 	}
720 
721 	/*
722 	 * After resizing, the previously invalid GEOM class metadata
723 	 * might become valid.  This means we should retaste.
724 	 */
725 	LIST_FOREACH(mp, &g_classes, class) {
726 		if (mp->taste == NULL)
727 			continue;
728 		LIST_FOREACH(cp, &pp->consumers, consumers)
729 			if (cp->geom->class == mp &&
730 			    (cp->flags & G_CF_ORPHAN) == 0)
731 				break;
732 		if (cp != NULL)
733 			continue;
734 		mp->taste(mp, pp, 0);
735 		g_topology_assert();
736 	}
737 }
738 
739 void
g_resize_provider(struct g_provider * pp,off_t size)740 g_resize_provider(struct g_provider *pp, off_t size)
741 {
742 	struct g_hh00 *hh;
743 
744 	G_VALID_PROVIDER(pp);
745 	if (pp->flags & G_PF_WITHER)
746 		return;
747 
748 	if (size == pp->mediasize)
749 		return;
750 
751 	hh = g_malloc(sizeof *hh, M_WAITOK | M_ZERO);
752 	hh->pp = pp;
753 	hh->size = size;
754 	g_post_event(g_resize_provider_event, hh, M_WAITOK, NULL);
755 }
756 
757 struct g_provider *
g_provider_by_name(char const * arg)758 g_provider_by_name(char const *arg)
759 {
760 	struct g_class *cp;
761 	struct g_geom *gp;
762 	struct g_provider *pp, *wpp;
763 
764 	if (strncmp(arg, _PATH_DEV, sizeof(_PATH_DEV) - 1) == 0)
765 		arg += sizeof(_PATH_DEV) - 1;
766 
767 	wpp = NULL;
768 	LIST_FOREACH(cp, &g_classes, class) {
769 		LIST_FOREACH(gp, &cp->geom, geom) {
770 			LIST_FOREACH(pp, &gp->provider, provider) {
771 				if (strcmp(arg, pp->name) != 0)
772 					continue;
773 				if ((gp->flags & G_GEOM_WITHER) == 0 &&
774 				    (pp->flags & G_PF_WITHER) == 0)
775 					return (pp);
776 				else
777 					wpp = pp;
778 			}
779 		}
780 	}
781 
782 	return (wpp);
783 }
784 
785 void
g_destroy_provider(struct g_provider * pp)786 g_destroy_provider(struct g_provider *pp)
787 {
788 	struct g_geom *gp;
789 	struct g_geom_alias *gap, *gaptmp;
790 
791 	g_topology_assert();
792 	G_VALID_PROVIDER(pp);
793 	KASSERT(LIST_EMPTY(&pp->consumers),
794 	    ("g_destroy_provider but attached"));
795 	KASSERT (pp->acr == 0, ("g_destroy_provider with acr"));
796 	KASSERT (pp->acw == 0, ("g_destroy_provider with acw"));
797 	KASSERT (pp->ace == 0, ("g_destroy_provider with ace"));
798 	g_cancel_event(pp);
799 	LIST_REMOVE(pp, provider);
800 	gp = pp->geom;
801 	devstat_remove_entry(pp->stat);
802 	/*
803 	 * If a callback was provided, send notification that the provider
804 	 * is now gone.
805 	 */
806 	if (gp->providergone != NULL)
807 		gp->providergone(pp);
808 	LIST_FOREACH_SAFE(gap, &pp->aliases, ga_next, gaptmp)
809 		g_free(gap);
810 	g_free(pp);
811 	if ((gp->flags & G_GEOM_WITHER))
812 		g_do_wither();
813 }
814 
815 /*
816  * We keep the "geoms" list sorted by topological order (== increasing
817  * numerical rank) at all times.
818  * When an attach is done, the attaching geoms rank is invalidated
819  * and it is moved to the tail of the list.
820  * All geoms later in the sequence has their ranks reevaluated in
821  * sequence.  If we cannot assign rank to a geom because it's
822  * prerequisites do not have rank, we move that element to the tail
823  * of the sequence with invalid rank as well.
824  * At some point we encounter our original geom and if we stil fail
825  * to assign it a rank, there must be a loop and we fail back to
826  * g_attach() which detach again and calls redo_rank again
827  * to fix up the damage.
828  * It would be much simpler code wise to do it recursively, but we
829  * can't risk that on the kernel stack.
830  */
831 
832 static int
redo_rank(struct g_geom * gp)833 redo_rank(struct g_geom *gp)
834 {
835 	struct g_consumer *cp;
836 	struct g_geom *gp1, *gp2;
837 	int n, m;
838 
839 	g_topology_assert();
840 	G_VALID_GEOM(gp);
841 
842 	/* Invalidate this geoms rank and move it to the tail */
843 	gp1 = TAILQ_NEXT(gp, geoms);
844 	if (gp1 != NULL) {
845 		gp->rank = 0;
846 		TAILQ_REMOVE(&geoms, gp, geoms);
847 		TAILQ_INSERT_TAIL(&geoms, gp, geoms);
848 	} else {
849 		gp1 = gp;
850 	}
851 
852 	/* re-rank the rest of the sequence */
853 	for (; gp1 != NULL; gp1 = gp2) {
854 		gp1->rank = 0;
855 		m = 1;
856 		LIST_FOREACH(cp, &gp1->consumer, consumer) {
857 			if (cp->provider == NULL)
858 				continue;
859 			n = cp->provider->geom->rank;
860 			if (n == 0) {
861 				m = 0;
862 				break;
863 			} else if (n >= m)
864 				m = n + 1;
865 		}
866 		gp1->rank = m;
867 		gp2 = TAILQ_NEXT(gp1, geoms);
868 
869 		/* got a rank, moving on */
870 		if (m != 0)
871 			continue;
872 
873 		/* no rank to original geom means loop */
874 		if (gp == gp1)
875 			return (ELOOP);
876 
877 		/* no rank, put it at the end move on */
878 		TAILQ_REMOVE(&geoms, gp1, geoms);
879 		TAILQ_INSERT_TAIL(&geoms, gp1, geoms);
880 	}
881 	return (0);
882 }
883 
884 int
g_attach(struct g_consumer * cp,struct g_provider * pp)885 g_attach(struct g_consumer *cp, struct g_provider *pp)
886 {
887 	int error;
888 
889 	g_topology_assert();
890 	G_VALID_CONSUMER(cp);
891 	G_VALID_PROVIDER(pp);
892 	g_trace(G_T_TOPOLOGY, "g_attach(%p, %p)", cp, pp);
893 	KASSERT(cp->provider == NULL, ("attach but attached"));
894 	if ((pp->flags & (G_PF_ORPHAN | G_PF_WITHER)) != 0)
895 		return (ENXIO);
896 	cp->provider = pp;
897 	cp->flags &= ~G_CF_ORPHAN;
898 	LIST_INSERT_HEAD(&pp->consumers, cp, consumers);
899 	error = redo_rank(cp->geom);
900 	if (error) {
901 		LIST_REMOVE(cp, consumers);
902 		cp->provider = NULL;
903 		redo_rank(cp->geom);
904 	}
905 	return (error);
906 }
907 
908 void
g_detach(struct g_consumer * cp)909 g_detach(struct g_consumer *cp)
910 {
911 	struct g_provider *pp;
912 
913 	g_topology_assert();
914 	G_VALID_CONSUMER(cp);
915 	g_trace(G_T_TOPOLOGY, "g_detach(%p)", cp);
916 	KASSERT(cp->provider != NULL, ("detach but not attached"));
917 	KASSERT(cp->acr == 0, ("detach but nonzero acr"));
918 	KASSERT(cp->acw == 0, ("detach but nonzero acw"));
919 	KASSERT(cp->ace == 0, ("detach but nonzero ace"));
920 	KASSERT(cp->nstart == cp->nend,
921 	    ("detach with active requests"));
922 	pp = cp->provider;
923 	LIST_REMOVE(cp, consumers);
924 	cp->provider = NULL;
925 	if ((cp->geom->flags & G_GEOM_WITHER) ||
926 	    (pp->geom->flags & G_GEOM_WITHER) ||
927 	    (pp->flags & G_PF_WITHER))
928 		g_do_wither();
929 	redo_rank(cp->geom);
930 }
931 
932 /*
933  * g_access()
934  *
935  * Access-check with delta values.  The question asked is "can provider
936  * "cp" change the access counters by the relative amounts dc[rwe] ?"
937  */
938 
939 int
g_access(struct g_consumer * cp,int dcr,int dcw,int dce)940 g_access(struct g_consumer *cp, int dcr, int dcw, int dce)
941 {
942 	struct g_provider *pp;
943 	struct g_geom *gp;
944 	int pw, pe;
945 #ifdef INVARIANTS
946 	int sr, sw, se;
947 #endif
948 	int error;
949 
950 	g_topology_assert();
951 	G_VALID_CONSUMER(cp);
952 	pp = cp->provider;
953 	KASSERT(pp != NULL, ("access but not attached"));
954 	G_VALID_PROVIDER(pp);
955 	gp = pp->geom;
956 
957 	g_trace(G_T_ACCESS, "g_access(%p(%s), %d, %d, %d)",
958 	    cp, pp->name, dcr, dcw, dce);
959 
960 	KASSERT(cp->acr + dcr >= 0, ("access resulting in negative acr"));
961 	KASSERT(cp->acw + dcw >= 0, ("access resulting in negative acw"));
962 	KASSERT(cp->ace + dce >= 0, ("access resulting in negative ace"));
963 	KASSERT(dcr != 0 || dcw != 0 || dce != 0, ("NOP access request"));
964 	KASSERT(cp->acr + dcr != 0 || cp->acw + dcw != 0 ||
965 	    cp->ace + dce != 0 || cp->nstart == cp->nend,
966 	    ("Last close with active requests"));
967 	KASSERT(gp->access != NULL, ("NULL geom->access"));
968 
969 	/*
970 	 * If our class cares about being spoiled, and we have been, we
971 	 * are probably just ahead of the event telling us that.  Fail
972 	 * now rather than having to unravel this later.
973 	 */
974 	if (cp->geom->spoiled != NULL && (cp->flags & G_CF_SPOILED) &&
975 	    (dcr > 0 || dcw > 0 || dce > 0))
976 		return (ENXIO);
977 
978 	/*
979 	 * A number of GEOM classes either need to perform an I/O on the first
980 	 * open or to acquire a different subsystem's lock.  To do that they
981 	 * may have to drop the topology lock.
982 	 * Other GEOM classes perform special actions when opening a lower rank
983 	 * geom for the first time.  As a result, more than one thread may
984 	 * end up performing the special actions.
985 	 * So, we prevent concurrent "first" opens by marking the consumer with
986 	 * special flag.
987 	 *
988 	 * Note that if the geom's access method never drops the topology lock,
989 	 * then we will never see G_GEOM_IN_ACCESS here.
990 	 */
991 	while ((gp->flags & G_GEOM_IN_ACCESS) != 0) {
992 		g_trace(G_T_ACCESS,
993 		    "%s: race on geom %s via provider %s and consumer of %s",
994 		    __func__, gp->name, pp->name, cp->geom->name);
995 		gp->flags |= G_GEOM_ACCESS_WAIT;
996 		g_topology_sleep(gp, 0);
997 	}
998 
999 	/*
1000 	 * Figure out what counts the provider would have had, if this
1001 	 * consumer had (r0w0e0) at this time.
1002 	 */
1003 	pw = pp->acw - cp->acw;
1004 	pe = pp->ace - cp->ace;
1005 
1006 	g_trace(G_T_ACCESS,
1007     "open delta:[r%dw%de%d] old:[r%dw%de%d] provider:[r%dw%de%d] %p(%s)",
1008 	    dcr, dcw, dce,
1009 	    cp->acr, cp->acw, cp->ace,
1010 	    pp->acr, pp->acw, pp->ace,
1011 	    pp, pp->name);
1012 
1013 	/* If foot-shooting is enabled, any open on rank#1 is OK */
1014 	if ((g_debugflags & G_F_FOOTSHOOTING) && gp->rank == 1)
1015 		;
1016 	/* If we try exclusive but already write: fail */
1017 	else if (dce > 0 && pw > 0)
1018 		return (EPERM);
1019 	/* If we try write but already exclusive: fail */
1020 	else if (dcw > 0 && pe > 0)
1021 		return (EPERM);
1022 	/* If we try to open more but provider is error'ed: fail */
1023 	else if ((dcr > 0 || dcw > 0 || dce > 0) && pp->error != 0) {
1024 		printf("%s(%d): provider %s has error %d set\n",
1025 		    __func__, __LINE__, pp->name, pp->error);
1026 		return (pp->error);
1027 	}
1028 
1029 	/* Ok then... */
1030 
1031 #ifdef INVARIANTS
1032 	sr = cp->acr;
1033 	sw = cp->acw;
1034 	se = cp->ace;
1035 #endif
1036 	gp->flags |= G_GEOM_IN_ACCESS;
1037 	error = gp->access(pp, dcr, dcw, dce);
1038 	KASSERT(dcr > 0 || dcw > 0 || dce > 0 || error == 0,
1039 	    ("Geom provider %s::%s dcr=%d dcw=%d dce=%d error=%d failed "
1040 	    "closing ->access()", gp->class->name, pp->name, dcr, dcw,
1041 	    dce, error));
1042 
1043 	g_topology_assert();
1044 	gp->flags &= ~G_GEOM_IN_ACCESS;
1045 	KASSERT(cp->acr == sr && cp->acw == sw && cp->ace == se,
1046 	    ("Access counts changed during geom->access"));
1047 	if ((gp->flags & G_GEOM_ACCESS_WAIT) != 0) {
1048 		gp->flags &= ~G_GEOM_ACCESS_WAIT;
1049 		wakeup(gp);
1050 	}
1051 
1052 	if (!error) {
1053 		/*
1054 		 * If we open first write, spoil any partner consumers.
1055 		 * If we close last write and provider is not errored,
1056 		 * trigger re-taste.
1057 		 */
1058 		if (pp->acw == 0 && dcw != 0)
1059 			g_spoil(pp, cp);
1060 		else if (pp->acw != 0 && pp->acw == -dcw && pp->error == 0 &&
1061 		    !(gp->flags & G_GEOM_WITHER))
1062 			g_post_event(g_new_provider_event, pp, M_WAITOK,
1063 			    pp, NULL);
1064 
1065 		pp->acr += dcr;
1066 		pp->acw += dcw;
1067 		pp->ace += dce;
1068 		cp->acr += dcr;
1069 		cp->acw += dcw;
1070 		cp->ace += dce;
1071 		if (pp->acr != 0 || pp->acw != 0 || pp->ace != 0)
1072 			KASSERT(pp->sectorsize > 0,
1073 			    ("Provider %s lacks sectorsize", pp->name));
1074 		if ((cp->geom->flags & G_GEOM_WITHER) &&
1075 		    cp->acr == 0 && cp->acw == 0 && cp->ace == 0)
1076 			g_do_wither();
1077 	}
1078 	return (error);
1079 }
1080 
1081 int
g_handleattr_int(struct bio * bp,const char * attribute,int val)1082 g_handleattr_int(struct bio *bp, const char *attribute, int val)
1083 {
1084 
1085 	return (g_handleattr(bp, attribute, &val, sizeof val));
1086 }
1087 
1088 int
g_handleattr_uint16_t(struct bio * bp,const char * attribute,uint16_t val)1089 g_handleattr_uint16_t(struct bio *bp, const char *attribute, uint16_t val)
1090 {
1091 
1092 	return (g_handleattr(bp, attribute, &val, sizeof val));
1093 }
1094 
1095 int
g_handleattr_off_t(struct bio * bp,const char * attribute,off_t val)1096 g_handleattr_off_t(struct bio *bp, const char *attribute, off_t val)
1097 {
1098 
1099 	return (g_handleattr(bp, attribute, &val, sizeof val));
1100 }
1101 
1102 int
g_handleattr_str(struct bio * bp,const char * attribute,const char * str)1103 g_handleattr_str(struct bio *bp, const char *attribute, const char *str)
1104 {
1105 
1106 	return (g_handleattr(bp, attribute, str, 0));
1107 }
1108 
1109 int
g_handleattr(struct bio * bp,const char * attribute,const void * val,int len)1110 g_handleattr(struct bio *bp, const char *attribute, const void *val, int len)
1111 {
1112 	int error = 0;
1113 
1114 	if (strcmp(bp->bio_attribute, attribute))
1115 		return (0);
1116 	if (len == 0) {
1117 		bzero(bp->bio_data, bp->bio_length);
1118 		if (strlcpy(bp->bio_data, val, bp->bio_length) >=
1119 		    bp->bio_length) {
1120 			printf("%s: %s %s bio_length %jd strlen %zu -> EFAULT\n",
1121 			    __func__, bp->bio_to->name, attribute,
1122 			    (intmax_t)bp->bio_length, strlen(val));
1123 			error = EFAULT;
1124 		}
1125 	} else if (bp->bio_length == len) {
1126 		bcopy(val, bp->bio_data, len);
1127 	} else {
1128 		printf("%s: %s %s bio_length %jd len %d -> EFAULT\n", __func__,
1129 		    bp->bio_to->name, attribute, (intmax_t)bp->bio_length, len);
1130 		error = EFAULT;
1131 	}
1132 	if (error == 0)
1133 		bp->bio_completed = bp->bio_length;
1134 	g_io_deliver(bp, error);
1135 	return (1);
1136 }
1137 
1138 int
g_std_access(struct g_provider * pp,int dr __unused,int dw __unused,int de __unused)1139 g_std_access(struct g_provider *pp,
1140 	int dr __unused, int dw __unused, int de __unused)
1141 {
1142 
1143 	g_topology_assert();
1144 	G_VALID_PROVIDER(pp);
1145         return (0);
1146 }
1147 
1148 void
g_std_done(struct bio * bp)1149 g_std_done(struct bio *bp)
1150 {
1151 	struct bio *bp2;
1152 
1153 	bp2 = bp->bio_parent;
1154 	if (bp2->bio_error == 0)
1155 		bp2->bio_error = bp->bio_error;
1156 	bp2->bio_completed += bp->bio_completed;
1157 	g_destroy_bio(bp);
1158 	bp2->bio_inbed++;
1159 	if (bp2->bio_children == bp2->bio_inbed) {
1160 		if (bp2->bio_cmd == BIO_SPEEDUP)
1161 			bp2->bio_completed = bp2->bio_length;
1162 		g_io_deliver(bp2, bp2->bio_error);
1163 	}
1164 }
1165 
1166 /* XXX: maybe this is only g_slice_spoiled */
1167 
1168 void
g_std_spoiled(struct g_consumer * cp)1169 g_std_spoiled(struct g_consumer *cp)
1170 {
1171 	struct g_geom *gp;
1172 	struct g_provider *pp;
1173 
1174 	g_topology_assert();
1175 	G_VALID_CONSUMER(cp);
1176 	g_trace(G_T_TOPOLOGY, "g_std_spoiled(%p)", cp);
1177 	cp->flags |= G_CF_ORPHAN;
1178 	g_detach(cp);
1179 	gp = cp->geom;
1180 	LIST_FOREACH(pp, &gp->provider, provider)
1181 		g_orphan_provider(pp, ENXIO);
1182 	g_destroy_consumer(cp);
1183 	if (LIST_EMPTY(&gp->provider) && LIST_EMPTY(&gp->consumer))
1184 		g_destroy_geom(gp);
1185 	else
1186 		gp->flags |= G_GEOM_WITHER;
1187 }
1188 
1189 /*
1190  * Spoiling happens when a provider is opened for writing, but consumers
1191  * which are configured by in-band data are attached (slicers for instance).
1192  * Since the write might potentially change the in-band data, such consumers
1193  * need to re-evaluate their existence after the writing session closes.
1194  * We do this by (offering to) tear them down when the open for write happens
1195  * in return for a re-taste when it closes again.
1196  * Together with the fact that such consumers grab an 'e' bit whenever they
1197  * are open, regardless of mode, this ends up DTRT.
1198  */
1199 
1200 static void
g_spoil_event(void * arg,int flag)1201 g_spoil_event(void *arg, int flag)
1202 {
1203 	struct g_provider *pp;
1204 	struct g_consumer *cp, *cp2;
1205 
1206 	g_topology_assert();
1207 	if (flag == EV_CANCEL)
1208 		return;
1209 	pp = arg;
1210 	G_VALID_PROVIDER(pp);
1211 	g_trace(G_T_TOPOLOGY, "%s %p(%s:%s:%s)", __func__, pp,
1212 	    pp->geom->class->name, pp->geom->name, pp->name);
1213 	for (cp = LIST_FIRST(&pp->consumers); cp != NULL; cp = cp2) {
1214 		cp2 = LIST_NEXT(cp, consumers);
1215 		if ((cp->flags & G_CF_SPOILED) == 0)
1216 			continue;
1217 		cp->flags &= ~G_CF_SPOILED;
1218 		if (cp->geom->spoiled == NULL)
1219 			continue;
1220 		cp->geom->spoiled(cp);
1221 		g_topology_assert();
1222 	}
1223 }
1224 
1225 void
g_spoil(struct g_provider * pp,struct g_consumer * cp)1226 g_spoil(struct g_provider *pp, struct g_consumer *cp)
1227 {
1228 	struct g_consumer *cp2;
1229 
1230 	g_topology_assert();
1231 	G_VALID_PROVIDER(pp);
1232 	G_VALID_CONSUMER(cp);
1233 
1234 	LIST_FOREACH(cp2, &pp->consumers, consumers) {
1235 		if (cp2 == cp)
1236 			continue;
1237 /*
1238 		KASSERT(cp2->acr == 0, ("spoiling cp->acr = %d", cp2->acr));
1239 		KASSERT(cp2->acw == 0, ("spoiling cp->acw = %d", cp2->acw));
1240 */
1241 		KASSERT(cp2->ace == 0, ("spoiling cp->ace = %d", cp2->ace));
1242 		cp2->flags |= G_CF_SPOILED;
1243 	}
1244 	g_post_event(g_spoil_event, pp, M_WAITOK, pp, NULL);
1245 }
1246 
1247 static void
g_media_changed_event(void * arg,int flag)1248 g_media_changed_event(void *arg, int flag)
1249 {
1250 	struct g_provider *pp;
1251 	int retaste;
1252 
1253 	g_topology_assert();
1254 	if (flag == EV_CANCEL)
1255 		return;
1256 	pp = arg;
1257 	G_VALID_PROVIDER(pp);
1258 
1259 	/*
1260 	 * If provider was not open for writing, queue retaste after spoiling.
1261 	 * If it was, retaste will happen automatically on close.
1262 	 */
1263 	retaste = (pp->acw == 0 && pp->error == 0 &&
1264 	    !(pp->geom->flags & G_GEOM_WITHER));
1265 	g_spoil_event(arg, flag);
1266 	if (retaste)
1267 		g_post_event(g_new_provider_event, pp, M_WAITOK, pp, NULL);
1268 }
1269 
1270 int
g_media_changed(struct g_provider * pp,int flag)1271 g_media_changed(struct g_provider *pp, int flag)
1272 {
1273 	struct g_consumer *cp;
1274 
1275 	LIST_FOREACH(cp, &pp->consumers, consumers)
1276 		cp->flags |= G_CF_SPOILED;
1277 	return (g_post_event(g_media_changed_event, pp, flag, pp, NULL));
1278 }
1279 
1280 int
g_media_gone(struct g_provider * pp,int flag)1281 g_media_gone(struct g_provider *pp, int flag)
1282 {
1283 	struct g_consumer *cp;
1284 
1285 	LIST_FOREACH(cp, &pp->consumers, consumers)
1286 		cp->flags |= G_CF_SPOILED;
1287 	return (g_post_event(g_spoil_event, pp, flag, pp, NULL));
1288 }
1289 
1290 int
g_getattr__(const char * attr,struct g_consumer * cp,void * var,int len)1291 g_getattr__(const char *attr, struct g_consumer *cp, void *var, int len)
1292 {
1293 	int error, i;
1294 
1295 	i = len;
1296 	error = g_io_getattr(attr, cp, &i, var);
1297 	if (error)
1298 		return (error);
1299 	if (i != len)
1300 		return (EINVAL);
1301 	return (0);
1302 }
1303 
1304 static int
g_get_device_prefix_len(const char * name)1305 g_get_device_prefix_len(const char *name)
1306 {
1307 	int len;
1308 
1309 	if (strncmp(name, "ada", 3) == 0)
1310 		len = 3;
1311 	else if (strncmp(name, "ad", 2) == 0)
1312 		len = 2;
1313 	else
1314 		return (0);
1315 	if (name[len] < '0' || name[len] > '9')
1316 		return (0);
1317 	do {
1318 		len++;
1319 	} while (name[len] >= '0' && name[len] <= '9');
1320 	return (len);
1321 }
1322 
1323 int
g_compare_names(const char * namea,const char * nameb)1324 g_compare_names(const char *namea, const char *nameb)
1325 {
1326 	int deva, devb;
1327 
1328 	if (strcmp(namea, nameb) == 0)
1329 		return (1);
1330 	deva = g_get_device_prefix_len(namea);
1331 	if (deva == 0)
1332 		return (0);
1333 	devb = g_get_device_prefix_len(nameb);
1334 	if (devb == 0)
1335 		return (0);
1336 	if (strcmp(namea + deva, nameb + devb) == 0)
1337 		return (1);
1338 	return (0);
1339 }
1340 
1341 #if defined(DIAGNOSTIC) || defined(DDB)
1342 /*
1343  * This function walks the mesh and returns a non-zero integer if it
1344  * finds the argument pointer is an object. The return value indicates
1345  * which type of object it is believed to be. If topology is not locked,
1346  * this function is potentially dangerous, but we don't assert that the
1347  * topology lock is held when called from debugger.
1348  */
1349 int
g_valid_obj(void const * ptr)1350 g_valid_obj(void const *ptr)
1351 {
1352 	struct g_class *mp;
1353 	struct g_geom *gp;
1354 	struct g_consumer *cp;
1355 	struct g_provider *pp;
1356 
1357 #ifdef KDB
1358 	if (kdb_active == 0)
1359 #endif
1360 		g_topology_assert();
1361 
1362 	LIST_FOREACH(mp, &g_classes, class) {
1363 		if (ptr == mp)
1364 			return (1);
1365 		LIST_FOREACH(gp, &mp->geom, geom) {
1366 			if (ptr == gp)
1367 				return (2);
1368 			LIST_FOREACH(cp, &gp->consumer, consumer)
1369 				if (ptr == cp)
1370 					return (3);
1371 			LIST_FOREACH(pp, &gp->provider, provider)
1372 				if (ptr == pp)
1373 					return (4);
1374 		}
1375 	}
1376 	return(0);
1377 }
1378 #endif
1379 
1380 #ifdef DDB
1381 
1382 #define	gprintf(...)	do {						\
1383 	db_printf("%*s", indent, "");					\
1384 	db_printf(__VA_ARGS__);						\
1385 } while (0)
1386 #define	gprintln(...)	do {						\
1387 	gprintf(__VA_ARGS__);						\
1388 	db_printf("\n");						\
1389 } while (0)
1390 
1391 #define	ADDFLAG(obj, flag, sflag)	do {				\
1392 	if ((obj)->flags & (flag)) {					\
1393 		if (comma)						\
1394 			strlcat(str, ",", size);			\
1395 		strlcat(str, (sflag), size);				\
1396 		comma = 1;						\
1397 	}								\
1398 } while (0)
1399 
1400 static char *
provider_flags_to_string(struct g_provider * pp,char * str,size_t size)1401 provider_flags_to_string(struct g_provider *pp, char *str, size_t size)
1402 {
1403 	int comma = 0;
1404 
1405 	bzero(str, size);
1406 	if (pp->flags == 0) {
1407 		strlcpy(str, "NONE", size);
1408 		return (str);
1409 	}
1410 	ADDFLAG(pp, G_PF_WITHER, "G_PF_WITHER");
1411 	ADDFLAG(pp, G_PF_ORPHAN, "G_PF_ORPHAN");
1412 	return (str);
1413 }
1414 
1415 static char *
geom_flags_to_string(struct g_geom * gp,char * str,size_t size)1416 geom_flags_to_string(struct g_geom *gp, char *str, size_t size)
1417 {
1418 	int comma = 0;
1419 
1420 	bzero(str, size);
1421 	if (gp->flags == 0) {
1422 		strlcpy(str, "NONE", size);
1423 		return (str);
1424 	}
1425 	ADDFLAG(gp, G_GEOM_WITHER, "G_GEOM_WITHER");
1426 	return (str);
1427 }
1428 static void
db_show_geom_consumer(int indent,struct g_consumer * cp)1429 db_show_geom_consumer(int indent, struct g_consumer *cp)
1430 {
1431 
1432 	if (indent == 0) {
1433 		gprintln("consumer: %p", cp);
1434 		gprintln("  class:    %s (%p)", cp->geom->class->name,
1435 		    cp->geom->class);
1436 		gprintln("  geom:     %s (%p)", cp->geom->name, cp->geom);
1437 		if (cp->provider == NULL)
1438 			gprintln("  provider: none");
1439 		else {
1440 			gprintln("  provider: %s (%p)", cp->provider->name,
1441 			    cp->provider);
1442 		}
1443 		gprintln("  access:   r%dw%de%d", cp->acr, cp->acw, cp->ace);
1444 		gprintln("  flags:    0x%04x", cp->flags);
1445 #ifdef INVARIANTS
1446 		gprintln("  nstart:   %u", cp->nstart);
1447 		gprintln("  nend:     %u", cp->nend);
1448 #endif
1449 	} else {
1450 		gprintf("consumer: %p (%s), access=r%dw%de%d", cp,
1451 		    cp->provider != NULL ? cp->provider->name : "none",
1452 		    cp->acr, cp->acw, cp->ace);
1453 		if (cp->flags)
1454 			db_printf(", flags=0x%04x", cp->flags);
1455 		db_printf("\n");
1456 	}
1457 }
1458 
1459 static void
db_show_geom_provider(int indent,struct g_provider * pp)1460 db_show_geom_provider(int indent, struct g_provider *pp)
1461 {
1462 	struct g_consumer *cp;
1463 	char flags[64];
1464 
1465 	if (indent == 0) {
1466 		gprintln("provider: %s (%p)", pp->name, pp);
1467 		gprintln("  class:        %s (%p)", pp->geom->class->name,
1468 		    pp->geom->class);
1469 		gprintln("  geom:         %s (%p)", pp->geom->name, pp->geom);
1470 		gprintln("  mediasize:    %jd", (intmax_t)pp->mediasize);
1471 		gprintln("  sectorsize:   %u", pp->sectorsize);
1472 		gprintln("  stripesize:   %ju", (uintmax_t)pp->stripesize);
1473 		gprintln("  stripeoffset: %ju", (uintmax_t)pp->stripeoffset);
1474 		gprintln("  access:       r%dw%de%d", pp->acr, pp->acw,
1475 		    pp->ace);
1476 		gprintln("  flags:        %s (0x%04x)",
1477 		    provider_flags_to_string(pp, flags, sizeof(flags)),
1478 		    pp->flags);
1479 		gprintln("  error:        %d", pp->error);
1480 		if (LIST_EMPTY(&pp->consumers))
1481 			gprintln("  consumers:    none");
1482 	} else {
1483 		gprintf("provider: %s (%p), access=r%dw%de%d",
1484 		    pp->name, pp, pp->acr, pp->acw, pp->ace);
1485 		if (pp->flags != 0) {
1486 			db_printf(", flags=%s (0x%04x)",
1487 			    provider_flags_to_string(pp, flags, sizeof(flags)),
1488 			    pp->flags);
1489 		}
1490 		db_printf("\n");
1491 	}
1492 	if (!LIST_EMPTY(&pp->consumers)) {
1493 		LIST_FOREACH(cp, &pp->consumers, consumers) {
1494 			db_show_geom_consumer(indent + 2, cp);
1495 			if (db_pager_quit)
1496 				break;
1497 		}
1498 	}
1499 }
1500 
1501 static void
db_show_geom_geom(int indent,struct g_geom * gp)1502 db_show_geom_geom(int indent, struct g_geom *gp)
1503 {
1504 	struct g_provider *pp;
1505 	struct g_consumer *cp;
1506 	char flags[64];
1507 
1508 	if (indent == 0) {
1509 		gprintln("geom: %s (%p)", gp->name, gp);
1510 		gprintln("  class:     %s (%p)", gp->class->name, gp->class);
1511 		gprintln("  flags:     %s (0x%04x)",
1512 		    geom_flags_to_string(gp, flags, sizeof(flags)), gp->flags);
1513 		gprintln("  rank:      %d", gp->rank);
1514 		if (LIST_EMPTY(&gp->provider))
1515 			gprintln("  providers: none");
1516 		if (LIST_EMPTY(&gp->consumer))
1517 			gprintln("  consumers: none");
1518 	} else {
1519 		gprintf("geom: %s (%p), rank=%d", gp->name, gp, gp->rank);
1520 		if (gp->flags != 0) {
1521 			db_printf(", flags=%s (0x%04x)",
1522 			    geom_flags_to_string(gp, flags, sizeof(flags)),
1523 			    gp->flags);
1524 		}
1525 		db_printf("\n");
1526 	}
1527 	if (!LIST_EMPTY(&gp->provider)) {
1528 		LIST_FOREACH(pp, &gp->provider, provider) {
1529 			db_show_geom_provider(indent + 2, pp);
1530 			if (db_pager_quit)
1531 				break;
1532 		}
1533 	}
1534 	if (!LIST_EMPTY(&gp->consumer)) {
1535 		LIST_FOREACH(cp, &gp->consumer, consumer) {
1536 			db_show_geom_consumer(indent + 2, cp);
1537 			if (db_pager_quit)
1538 				break;
1539 		}
1540 	}
1541 }
1542 
1543 static void
db_show_geom_class(struct g_class * mp)1544 db_show_geom_class(struct g_class *mp)
1545 {
1546 	struct g_geom *gp;
1547 
1548 	db_printf("class: %s (%p)\n", mp->name, mp);
1549 	LIST_FOREACH(gp, &mp->geom, geom) {
1550 		db_show_geom_geom(2, gp);
1551 		if (db_pager_quit)
1552 			break;
1553 	}
1554 }
1555 
1556 /*
1557  * Print the GEOM topology or the given object.
1558  */
DB_SHOW_COMMAND(geom,db_show_geom)1559 DB_SHOW_COMMAND(geom, db_show_geom)
1560 {
1561 	struct g_class *mp;
1562 
1563 	if (!have_addr) {
1564 		/* No address given, print the entire topology. */
1565 		LIST_FOREACH(mp, &g_classes, class) {
1566 			db_show_geom_class(mp);
1567 			db_printf("\n");
1568 			if (db_pager_quit)
1569 				break;
1570 		}
1571 	} else {
1572 		switch (g_valid_obj((void *)addr)) {
1573 		case 1:
1574 			db_show_geom_class((struct g_class *)addr);
1575 			break;
1576 		case 2:
1577 			db_show_geom_geom(0, (struct g_geom *)addr);
1578 			break;
1579 		case 3:
1580 			db_show_geom_consumer(0, (struct g_consumer *)addr);
1581 			break;
1582 		case 4:
1583 			db_show_geom_provider(0, (struct g_provider *)addr);
1584 			break;
1585 		default:
1586 			db_printf("Not a GEOM object.\n");
1587 			break;
1588 		}
1589 	}
1590 }
1591 
1592 static void
db_print_bio_cmd(struct bio * bp)1593 db_print_bio_cmd(struct bio *bp)
1594 {
1595 	db_printf("  cmd: ");
1596 	switch (bp->bio_cmd) {
1597 	case BIO_READ: db_printf("BIO_READ"); break;
1598 	case BIO_WRITE: db_printf("BIO_WRITE"); break;
1599 	case BIO_DELETE: db_printf("BIO_DELETE"); break;
1600 	case BIO_GETATTR: db_printf("BIO_GETATTR"); break;
1601 	case BIO_FLUSH: db_printf("BIO_FLUSH"); break;
1602 	case BIO_CMD0: db_printf("BIO_CMD0"); break;
1603 	case BIO_CMD1: db_printf("BIO_CMD1"); break;
1604 	case BIO_CMD2: db_printf("BIO_CMD2"); break;
1605 	case BIO_ZONE: db_printf("BIO_ZONE"); break;
1606 	default: db_printf("UNKNOWN"); break;
1607 	}
1608 	db_printf("\n");
1609 }
1610 
1611 static void
db_print_bio_flags(struct bio * bp)1612 db_print_bio_flags(struct bio *bp)
1613 {
1614 	int comma;
1615 
1616 	comma = 0;
1617 	db_printf("  flags: ");
1618 	if (bp->bio_flags & BIO_ERROR) {
1619 		db_printf("BIO_ERROR");
1620 		comma = 1;
1621 	}
1622 	if (bp->bio_flags & BIO_DONE) {
1623 		db_printf("%sBIO_DONE", (comma ? ", " : ""));
1624 		comma = 1;
1625 	}
1626 	if (bp->bio_flags & BIO_ONQUEUE)
1627 		db_printf("%sBIO_ONQUEUE", (comma ? ", " : ""));
1628 	db_printf("\n");
1629 }
1630 
1631 /*
1632  * Print useful information in a BIO
1633  */
DB_SHOW_COMMAND(bio,db_show_bio)1634 DB_SHOW_COMMAND(bio, db_show_bio)
1635 {
1636 	struct bio *bp;
1637 
1638 	if (have_addr) {
1639 		bp = (struct bio *)addr;
1640 		db_printf("BIO %p\n", bp);
1641 		db_print_bio_cmd(bp);
1642 		db_print_bio_flags(bp);
1643 		db_printf("  cflags: 0x%hx\n", bp->bio_cflags);
1644 		db_printf("  pflags: 0x%hx\n", bp->bio_pflags);
1645 		db_printf("  offset: %jd\n", (intmax_t)bp->bio_offset);
1646 		db_printf("  length: %jd\n", (intmax_t)bp->bio_length);
1647 		db_printf("  bcount: %ld\n", bp->bio_bcount);
1648 		db_printf("  resid: %ld\n", bp->bio_resid);
1649 		db_printf("  completed: %jd\n", (intmax_t)bp->bio_completed);
1650 		db_printf("  children: %u\n", bp->bio_children);
1651 		db_printf("  inbed: %u\n", bp->bio_inbed);
1652 		db_printf("  error: %d\n", bp->bio_error);
1653 		db_printf("  parent: %p\n", bp->bio_parent);
1654 		db_printf("  driver1: %p\n", bp->bio_driver1);
1655 		db_printf("  driver2: %p\n", bp->bio_driver2);
1656 		db_printf("  caller1: %p\n", bp->bio_caller1);
1657 		db_printf("  caller2: %p\n", bp->bio_caller2);
1658 		db_printf("  bio_from: %p\n", bp->bio_from);
1659 		db_printf("  bio_to: %p\n", bp->bio_to);
1660 
1661 #if defined(BUF_TRACKING) || defined(FULL_BUF_TRACKING)
1662 		db_printf("  bio_track_bp: %p\n", bp->bio_track_bp);
1663 #endif
1664 	}
1665 }
1666 
1667 #undef	gprintf
1668 #undef	gprintln
1669 #undef	ADDFLAG
1670 
1671 #endif	/* DDB */
1672