xref: /freebsd/share/man/man9/g_attach.9 (revision bdcbfde31e8e9b343f113a1956384bdf30d1ed62)
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.Dd October 10, 2020
26.Dt G_ATTACH 9
27.Os
28.Sh NAME
29.Nm g_attach ,
30.Nm g_detach
31.Nd "attach/detach GEOM consumers to/from providers"
32.Sh SYNOPSIS
33.In geom/geom.h
34.Ft int
35.Fn g_attach "struct g_consumer *cp" "struct g_provider *pp"
36.Ft void
37.Fn g_detach "struct g_consumer *cp"
38.Sh DESCRIPTION
39The
40.Fn g_attach
41function attaches given consumer
42.Fa cp
43to given provider
44.Fa pp ,
45thus establishing a communication channel between the consumer and the
46provider that allows to change access counts and perform I/O operations.
47.Pp
48The
49.Fn g_detach
50function detaches given consumer
51.Fa cp
52from its corresponding provider, tearing down the communication channel
53between them.
54.Sh RESTRICTIONS/CONDITIONS
55.Fn g_attach :
56.Bl -item -offset indent
57.It
58The consumer must not be attached to a provider.
59.It
60The operation must not create a topology loop.
61.It
62The topology lock has to be held.
63.El
64.Pp
65.Fn g_detach :
66.Bl -item -offset indent
67.It
68The consumer has to be attached.
69.It
70The access count has to be 0.
71.It
72There must be no active requests.
73.It
74The topology lock has to be held.
75.El
76.Sh RETURN VALUES
77The
78.Fn g_attach
79function returns 0 if successful; otherwise an error code is returned.
80.Sh EXAMPLES
81Create a consumer, attach it to a given provider, gain read access and clean up.
82.Bd -literal -offset indent
83void
84some_function(struct g_geom *mygeom, struct g_provider *pp)
85{
86	struct g_consumer *cp;
87
88	g_topology_assert();
89
90	/* Create new consumer on 'mygeom' geom. */
91	cp = g_new_consumer(mygeom);
92	/* Attach newly created consumer to given provider. */
93	if (g_attach(cp, pp) != 0) {
94		g_destroy_consumer(cp);
95		return;
96	}
97	/* Open provider for reading through our consumer. */
98	if (g_access(cp, 1, 0, 0) != 0) {
99		g_detach(cp);
100		g_destroy_consumer(cp);
101		return;
102	}
103
104	g_topology_unlock();
105	/*
106	 * Read data from provider.
107	 */
108	g_topology_lock();
109
110	/* Disconnect from provider (release access count). */
111	g_access(cp, -1, 0, 0);
112	/* Detach from provider. */
113	g_detach(cp);
114	/* Destroy consumer. */
115	g_destroy_consumer(cp);
116}
117.Ed
118.Sh ERRORS
119Possible errors:
120.Bl -tag -width Er
121.It Bq Er ELOOP
122The operation creates a topology loop.
123.It Bq Er ENXIO
124Provider got orphaned.
125.El
126.Sh SEE ALSO
127.Xr geom 4 ,
128.Xr DECLARE_GEOM_CLASS 9 ,
129.Xr g_access 9 ,
130.Xr g_bio 9 ,
131.Xr g_consumer 9 ,
132.Xr g_data 9 ,
133.Xr g_event 9 ,
134.Xr g_geom 9 ,
135.Xr g_provider 9 ,
136.Xr g_provider_by_name 9 ,
137.Xr g_wither_geom 9
138.Sh AUTHORS
139.An -nosplit
140This manual page was written by
141.An Pawel Jakub Dawidek Aq Mt pjd@FreeBSD.org .
142