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