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