G_ATTACH(9)
π¦ G_ATTACH(9) β the kernel dev manual, bestie edition π¦
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 the two little functions that decide whether a GEOM consumer and a provider are talking to each other or officially giving each other the silent treatment π
β¨ 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 g_attach() is the matchmaker function π it attaches the given consumer cp to the given provider pp, and by doing that it literally establishes a communication channel between the consumer and the provider. like, they're linked now. that channel is what lets them change access counts and perform I/O operations. it's giving "you two are connected now, go be iconic together" β¨
and g_detach() is the breakup π it detaches the given consumer cp from its corresponding provider, tearing down the communication channel between them. no more channel, no more talking. periodt.
π RESTRICTIONS/CONDITIONS π
g_attach() π§
- the consumer must not already be attached to a provider
- the operation must not create a topology loop
- the topology lock has to be held π
g_detach() π§
- the consumer has to be attached
- the access count has to be
0 - there must be no active requests
- the topology lock has to be held π
bestie these are not suggestions, these are the rules β vibe check your state before you call either of these fr fr π
β RETURN VALUES β
g_attach() returns 0 if it slayed (i.e. it was successful); otherwise an error code comes back at you. yeah, that's the whole deal.
(also a tiny heads-up: g_detach() is void, so it doesn't hand you a return value β the original page only documents a return value for g_attach() π)
π EXAMPLES π
okay so here's the whole little story: create a consumer, attach it to a given provider, gain read access, and clean up. the full character arc π
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);
}
notice how every failure path cleans up after itself? that's main character energy, no leaks, no mess π«Άβ¨
π ERRORS π
possible errors, aka the ways this can go wrong:
[ELOOP]β the operation creates a topology loop π (yes, the thing the restrictions said not to do)[ENXIO]β the provider got orphaned π
π₯ 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)
go read the whole friend group, iykyk π
π AUTHORS π
this manual page was written by Pawel Jakub Dawidek <pjd@FreeBSD.org>. iconic of him honestly π
and that's the page!! this beauty comes from the Debian build, dated October 10, 2020 β so yeah, go forth and attach responsibly, bestie πΈπ β¨
π¦ where this page comes from
- freebsd 14.5: base 14.5
- freebsd 15.1: base 15.1
- Copyright (c) 2004 Pawel Jakub Dawidek <pjd@FreeBSD.org>
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms