xref: /freebsd/share/man/man9/g_attach.9 (revision e6bfd18d21b225af6a0ed67ceeaf1293b7b9eba5)
1.\"
2.\" Copyright (c) 2004 Pawel Jakub Dawidek <pjd@FreeBSD.org>
3.\" All rights reserved.
4.\"
5.\" Redistribution and use in source and binary forms, with or without
6.\" modification, are permitted provided that the following conditions
7.\" are met:
8.\" 1. Redistributions of source code must retain the above copyright
9.\"    notice, this list of conditions and the following disclaimer.
10.\" 2. Redistributions in binary form must reproduce the above copyright
11.\"    notice, this list of conditions and the following disclaimer in the
12.\"    documentation and/or other materials provided with the distribution.
13.\"
14.\" THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY EXPRESS OR
15.\" IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16.\" OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17.\" IN NO EVENT SHALL THE DEVELOPERS BE LIABLE FOR ANY DIRECT, INDIRECT,
18.\" INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19.\" NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20.\" DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21.\" THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22.\" (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23.\" THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24.\"
25.\" $FreeBSD$
26.\"
27.Dd October 10, 2020
28.Dt G_ATTACH 9
29.Os
30.Sh NAME
31.Nm g_attach ,
32.Nm g_detach
33.Nd "attach/detach GEOM consumers to/from providers"
34.Sh SYNOPSIS
35.In geom/geom.h
36.Ft int
37.Fn g_attach "struct g_consumer *cp" "struct g_provider *pp"
38.Ft void
39.Fn g_detach "struct g_consumer *cp"
40.Sh DESCRIPTION
41The
42.Fn g_attach
43function attaches given consumer
44.Fa cp
45to given provider
46.Fa pp ,
47thus establishing a communication channel between the consumer and the
48provider that allows to change access counts and perform I/O operations.
49.Pp
50The
51.Fn g_detach
52function detaches given consumer
53.Fa cp
54from its corresponding provider, tearing down the communication channel
55between them.
56.Sh RESTRICTIONS/CONDITIONS
57.Fn g_attach :
58.Bl -item -offset indent
59.It
60The consumer must not be attached to a provider.
61.It
62The operation must not create a topology loop.
63.It
64The topology lock has to be held.
65.El
66.Pp
67.Fn g_detach :
68.Bl -item -offset indent
69.It
70The consumer has to be attached.
71.It
72The access count has to be 0.
73.It
74There must be no active requests.
75.It
76The topology lock has to be held.
77.El
78.Sh RETURN VALUES
79The
80.Fn g_attach
81function returns 0 if successful; otherwise an error code is returned.
82.Sh EXAMPLES
83Create a consumer, attach it to a given provider, gain read access and clean up.
84.Bd -literal -offset indent
85void
86some_function(struct g_geom *mygeom, struct g_provider *pp)
87{
88	struct g_consumer *cp;
89
90	g_topology_assert();
91
92	/* Create new consumer on 'mygeom' geom. */
93	cp = g_new_consumer(mygeom);
94	/* Attach newly created consumer to given provider. */
95	if (g_attach(cp, pp) != 0) {
96		g_destroy_consumer(cp);
97		return;
98	}
99	/* Open provider for reading through our consumer. */
100	if (g_access(cp, 1, 0, 0) != 0) {
101		g_detach(cp);
102		g_destroy_consumer(cp);
103		return;
104	}
105
106	g_topology_unlock();
107	/*
108	 * Read data from provider.
109	 */
110	g_topology_lock();
111
112	/* Disconnect from provider (release access count). */
113	g_access(cp, -1, 0, 0);
114	/* Detach from provider. */
115	g_detach(cp);
116	/* Destroy consumer. */
117	g_destroy_consumer(cp);
118}
119.Ed
120.Sh ERRORS
121Possible errors:
122.Bl -tag -width Er
123.It Bq Er ELOOP
124The operation creates a topology loop.
125.It Bq Er ENXIO
126Provider got orphaned.
127.El
128.Sh SEE ALSO
129.Xr geom 4 ,
130.Xr DECLARE_GEOM_CLASS 9 ,
131.Xr g_access 9 ,
132.Xr g_bio 9 ,
133.Xr g_consumer 9 ,
134.Xr g_data 9 ,
135.Xr g_event 9 ,
136.Xr g_geom 9 ,
137.Xr g_provider 9 ,
138.Xr g_provider_by_name 9 ,
139.Xr g_wither_geom 9
140.Sh AUTHORS
141.An -nosplit
142This manual page was written by
143.An Pawel Jakub Dawidek Aq Mt pjd@FreeBSD.org .
144