G_ATTACH(9)
π G_ATTACH β bestie's guide to gluing GEOM consumers to providers π
okay so this is the G_ATTACH(9) page from the Kernel Developer's Manual, and honestly? it's a whole vibe. it's about g_attach and g_detach, aka the two functions that decide whether a GEOM consumer and a GEOM provider are talking or not talking. situationship management, basically. π
β¨ NAME β¨
g_attach, g_detach β attach/detach GEOM consumers to/from providers
πΈ SYNOPSIS πΈ
#include <geom/geom.h>
int
g_attach(struct g_consumer *cp, struct g_provider *pp);
void
g_detach(struct g_consumer *cp);
π DESCRIPTION π
so the g_attach() function takes the consumer you hand it, cp, and attaches it to the provider you hand it, pp. that's it, that's the matchmaking. and what it does is establish a communication channel between the consumer and the provider β a little private line between them π«Ά β which is what lets you change access counts and do I/O operations. no channel, no I/O, simple as.
then g_detach() is the breakup arc π. it detaches the given consumer cp from whatever provider it's currently corresponding with, and it tears down that communication channel between them. gone. done. no more talking.
π RESTRICTIONS/CONDITIONS π
bestie these are not suggestions, these are the rules, and the kernel will be mad at you if you break them. β
for g_attach():
- the consumer must not already be attached to a provider (no double-dipping, that's not the vibe)
- the operation must not create a topology loop (do NOT make the geometry eat itself ππ)
- the topology lock has to be held
for g_detach():
- the consumer has to be attached (you can't detach from nothing, bestie)
- the access count has to be
0 - there must be no active requests
- the topology lock has to be held
β RETURN VALUES β
g_attach() is a queen that reports back: it returns 0 when it's successful. otherwise? it hands you an error code instead. so like, always check it. always. π
π EXAMPLES π
here's the whole glow-up routine: create a consumer, attach it to a given provider, gain read access, and then clean up after yourself like a responsible icon. π§Ήβ¨
void
some_function(struct g_geom *mygeom, struct g_provider *pp)
{
struct g_consumer *cp;
g_topology_assert();
/* Create new consumer on 'mygeom' geom. */
cp = g_new_consumer(mygeom);
/* Attach newly created consumer to given provider. */
if (g_attach(cp, pp) != 0) {
g_destroy_consumer(cp);
return;
}
/* Open provider for reading through our consumer. */
if (g_access(cp, 1, 0, 0) != 0) {
g_detach(cp);
g_destroy_consumer(cp);
return;
}
g_topology_unlock();
/*
* Read data from provider.
*/
g_topology_lock();
/* Disconnect from provider (release access count). */
g_access(cp, -1, 0, 0);
/* Detach from provider. */
g_detach(cp);
/* Destroy consumer. */
g_destroy_consumer(cp);
}
like, notice how every failure path cleans up after itself? that's the energy. π₯
π ERRORS π
possible errors, and yes there's only one on this list so it's lowkey the main character:
[ELOOP]β the operation creates a topology loop. (you made the geometry ouroboros itself, bestie, and it did not slay π)
π¦ SEE ALSO π¦
geom(4), DECLARE_GEOM_CLASS(9), g_access(9), g_bio(9), g_consumer(9), g_data(9), g_event(9), g_geom(9), g_provider(9), g_provider_by_name(9), g_wither_geom(9)
π AUTHORS π
this manual page was written by Pawel Jakub Dawidek <pjd@FreeBSD.org>. iconic behavior, honestly. π«Ά
okay and the lil sign-off at the bottom of the page says this came from Debian, dated January 16, 2004, and it lives in section G_ATTACH(9) β so go forth, attach responsibly, detach dramatically, and hold that topology lock. no cap. πβ¨π
π¦ where this page comes from
- debian bookworm: freebsd-manpages 12.2-1 Β· BSD-2-Clause-FreeBSD AND BSD-4-Clause
- debian trixie: freebsd-manpages 12.2-2 Β· BSD-2-Clause-FreeBSD AND BSD-4-Clause
- ubuntu jammy: freebsd-manpages 12.2-1 Β· BSD-2-Clause-FreeBSD AND BSD-4-Clause
- ubuntu noble: freebsd-manpages 12.2-1 Β· BSD-2-Clause-FreeBSD AND BSD-4-Clause
- ubuntu questing: freebsd-manpages 12.2-2
- ubuntu resolute: freebsd-manpages 12.2-2build1 Β· BSD-2-Clause-FreeBSD AND BSD-4-Clause
- ubuntu stonking: freebsd-manpages 12.2-2build1 Β· BSD-2-Clause-FreeBSD AND BSD-4-Clause
- Copyright (c) 2004 Pawel Jakub Dawidek <pjd@FreeBSD.org>
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms