g_attach(9)

g_attach9

Attach/detach geom consumers to/from providers

Usage

#include <geom/geom.h>

int

g_attach(struct g_consumer *cp, struct g_provider *pp);

void

g_detach(struct g_consumer *cp);

Overview

The g_attach() function attaches given consumer cp to given provider pp, thus establishing a communication channel between the consumer and the provider that allows to change access counts and perform I/O operations.

The g_detach() function detaches given consumer cp from its corresponding provider, tearing down the communication channel between them.

Restrictions/conditions

g_attach():

The consumer must not 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.

Return values

The g_attach() function returns 0 if successful; otherwise an error code is returned.

Examples

Create a consumer, attach it to a given provider, gain read access and clean up.

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);
}

Errors

Possible errors:

[ELOOP]

The operation creates a topology loop.

[ENXIO]

Provider got orphaned.

Releases

52 releases since FreeBSD 5.3-RELEASE; the text changed 33 times.

  1. 5.3–5.5
  2. 6.0–6.3
  3. 6.4
  4. 7.0
  5. 7.1
  6. 7.2
  7. 7.3
  8. 7.4
  9. 8.0
  10. 8.1
  11. 8.2
  12. 8.3
  13. 8.4
  14. 9.0
  15. 9.1
  16. 9.2
  17. 9.3
  18. 10.0
  19. 10.1
  20. 10.2
  21. 10.3
  22. 10.4
  23. 11.0
  24. 11.1
  25. 11.2
  26. 11.3
  27. 11.4
  28. 12.0
  29. 12.1
  30. 12.2
  31. 12.3
  32. 12.4
  33. 13.0–13.2
  34. 13.3–16.0

Authors

This manual page was written by Pawel Jakub Dawidek ⟨[email protected]⟩.

From base 16.0-CURRENT-20261027-61c70dc16a79 in FreeBSD 16.0-CURRENT (2026-10-27). Parsed by carta from the page's mdoc source.