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