xref: /freebsd/share/man/man9/g_access.9 (revision 4b9d6057)
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 January 16, 2004
26.Dt G_ACCESS 9
27.Os
28.Sh NAME
29.Nm g_access
30.Nd "control access to GEOM consumers and their providers"
31.Sh SYNOPSIS
32.In geom/geom.h
33.Ft int
34.Fn g_access "struct g_consumer *cp" "int dcr" "int dcw" "int dce"
35.Sh DESCRIPTION
36The
37.Fn g_access
38function allows to open, close, and generally change access to the provider
39which is attached to the given consumer
40.Fa cp .
41The arguments
42.Fa dcr ,
43.Fa dcw ,
44and
45.Fa dce
46represent relative read, write, and exclusive access count changes.
47Read and write access counts are self explanatory, and
48exclusive access counts deny write access to other interested parties.
49A provider's access count is the sum of the access counts of all
50attached consumers.
51.Pp
52After attaching a consumer to a provider with
53.Xr g_attach 9 ,
54the
55.Fn g_access
56function has to be called on the consumer before starting I/O requests.
57.Sh RESTRICTIONS/CONDITIONS
58The consumer has to be attached to a provider.
59.Pp
60The intended change must not result in a negative access count.
61.Pp
62No-operation is not permitted
63.Fa ( dcr
64=
65.Fa dcw
66=
67.Fa dce
68=
69.Li 0 ) .
70.Pp
71The provider's geom must have an access method defined (e.g.,
72.Va gp->access ) .
73.Pp
74The topology lock has to be held.
75.Sh RETURN VALUES
76The
77.Fn g_access
78function returns 0 if successful; otherwise an error code is returned.
79Note that
80.Fn g_access
81cannot fail when the arguments
82.Fa dcr ,
83.Fa dcw ,
84and
85.Fa dce
86are less than or equal to 0.
87.Sh EXAMPLES
88Create a consumer, attach it to a given provider, gain read access and
89read first sector.
90.Bd -literal -offset indent
91void
92some_function(struct g_geom *mygeom, struct g_provider *pp)
93{
94	struct g_consumer *cp;
95	void *ptr;
96	int error;
97
98	g_topology_assert();
99
100	/* Create new consumer on 'mygeom' geom. */
101	cp = g_new_consumer(mygeom);
102	/* Attach newly created consumer to given provider. */
103	if (g_attach(cp, pp) != 0) {
104		g_destroy_consumer(cp);
105		return;
106	}
107	/* Open provider for reading through our consumer. */
108	error = g_access(cp, 1, 0, 0);
109	if (error != 0) {
110		printf("Cannot access provider: %s\\n", error);
111		g_detach(cp);
112		g_destroy_consumer(cp);
113		return;
114	}
115
116	/*
117	 * Don't hold topology lock while reading.
118	 */
119	g_topology_unlock();
120	ptr = g_read_data(cp, 0, pp->sectorsize, &error);
121	if (ptr == NULL)
122		printf("Error while reading: %d\\n", error);
123	/*
124	 * Do something useful with data.
125	 */
126	g_topology_lock();
127
128	/* Disconnect from provider (release access count). */
129	g_access(cp, -1, 0, 0);
130	/* Detach from provider. */
131	g_detach(cp);
132	/* Destroy consumer. */
133	g_destroy_consumer(cp);
134}
135.Ed
136.Sh ERRORS
137Possible errors:
138.Bl -tag -width Er
139.It Bq Er EPERM
140The function is trying to open a provider with an exclusive access count, but
141it is already open for writing.
142.It Bq Er EPERM
143The function is trying to open a provider for writing, but it is already
144exclusively open.
145.El
146.Pp
147Any other error that can be returned by the provider's access method.
148.Sh SEE ALSO
149.Xr geom 4 ,
150.Xr DECLARE_GEOM_CLASS 9 ,
151.Xr g_attach 9 ,
152.Xr g_bio 9 ,
153.Xr g_consumer 9 ,
154.Xr g_data 9 ,
155.Xr g_event 9 ,
156.Xr g_geom 9 ,
157.Xr g_provider 9 ,
158.Xr g_provider_by_name 9 ,
159.Xr g_wither_geom 9
160.Sh AUTHORS
161.An -nosplit
162This manual page was written by
163.An Pawel Jakub Dawidek Aq Mt pjd@FreeBSD.org .
164