xref: /freebsd/lib/libgeom/libgeom.3 (revision 2e3507c25e42292b45a5482e116d278f5515d04d)
1.\" Copyright (c) 2003 Poul-Henning Kamp
2.\" Copyright (c) 2007 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.\" 3. The names of the authors may not be used to endorse or promote
14.\"    products derived from this software without specific prior written
15.\"    permission.
16.\"
17.\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27.\" SUCH DAMAGE.
28.\"
29.Dd October 16, 2017
30.Dt LIBGEOM 3
31.Os
32.Sh NAME
33.Nm geom_stats_open ,
34.Nm geom_stats_close ,
35.Nm geom_stats_resync ,
36.Nm geom_stats_snapshot_get ,
37.Nm geom_stats_snapshot_free ,
38.Nm geom_stats_snapshot_timestamp ,
39.Nm geom_stats_snapshot_reset ,
40.Nm geom_stats_snapshot_next ,
41.Nm gctl_get_handle ,
42.Nm gctl_ro_param ,
43.Nm gctl_rw_param ,
44.Nm gctl_issue ,
45.Nm gctl_free ,
46.Nm gctl_dump ,
47.Nm geom_getxml ,
48.Nm geom_xml2tree ,
49.Nm geom_gettree ,
50.Nm geom_deletetree ,
51.Nm g_open ,
52.Nm g_close ,
53.Nm g_mediasize ,
54.Nm g_sectorsize ,
55.Nm g_stripeoffset ,
56.Nm g_stripesize ,
57.Nm g_flush ,
58.Nm g_delete ,
59.Nm g_device_path ,
60.Nm g_get_ident ,
61.Nm g_get_name ,
62.Nm g_open_by_ident ,
63.Nm g_providername
64.Nd userland API library for kernel GEOM subsystem
65.Sh LIBRARY
66.Lb libgeom
67.Sh SYNOPSIS
68.Bd -literal
69/* stdio.h is only required for `gctl_dump` */
70.Ed
71.In stdio.h
72.In libgeom.h
73.Ss "Statistics Functions"
74.Ft void
75.Fn geom_stats_close void
76.Ft int
77.Fn geom_stats_open void
78.Ft void
79.Fn geom_stats_resync void
80.Ft "void *"
81.Fn geom_stats_snapshot_get void
82.Ft void
83.Fn geom_stats_snapshot_free "void *arg"
84.Ft void
85.Fn geom_stats_snapshot_timestamp "void *arg" "struct timespec *tp"
86.Ft void
87.Fn geom_stats_snapshot_reset "void *arg"
88.Ft "struct devstat *"
89.Fn geom_stats_snapshot_next "void *arg"
90.Ss "Control Functions"
91.Ft "struct gctl_req *"
92.Fn gctl_get_handle "void"
93.Ft void
94.Fn gctl_ro_param "struct gctl_req *req" "const char *name" "int len" "const void *value"
95.Ft void
96.Fn gctl_rw_param "struct gctl_req *req" "const char *name" "int len" "void *value"
97.Ft "const char *"
98.Fn gctl_issue "struct gctl_req *req"
99.Ft void
100.Fn gctl_free "struct gctl_req *req"
101.Ft void
102.Fn gctl_dump "struct gctl_req *req" "FILE *f"
103.Ss "Utility Functions"
104.Ft "char *"
105.Fn geom_getxml void
106.Ft int
107.Fn geom_xml2tree "struct gmesh *gmp" "char *p"
108.Ft int
109.Fn geom_gettree "struct gmesh *gmp"
110.Ft void
111.Fn geom_deletetree "struct gmesh *gmp"
112.Ft int
113.Fn g_open "const char *name" "int dowrite"
114.Ft int
115.Fn g_close "int fd"
116.Ft off_t
117.Fn g_mediasize "int fd"
118.Ft ssize_t
119.Fn g_sectorsize "int fd"
120.Ft ssize_t
121.Fn g_stripeoffset "int fd"
122.Ft ssize_t
123.Fn g_stripesize "int fd"
124.Ft int
125.Fn g_flush "int fd"
126.Ft int
127.Fn g_delete "int fd" "off_t offset" "off_t length"
128.Ft "char *"
129.Fn g_device_path "const char *devpath"
130.Ft int
131.Fn g_get_ident "int fd" "char *ident" "size_t size"
132.Ft int
133.Fn g_get_name "const char *ident" "char *name" "size_t size"
134.Ft int
135.Fn g_open_by_ident "const char *ident" "int dowrite" "char *name" "size_t size"
136.Ft "char *"
137.Fn g_providername "int fd"
138.Sh DESCRIPTION
139The
140.Nm geom
141library contains the official and publicized API for
142interacting with the GEOM subsystem in the kernel.
143.Ss "Statistics Functions"
144GEOM collects statistics data for all consumers and providers, but does
145not perform any normalization or presentation on the raw data, this is
146left as an exercise for user-land presentation utilities.
147.Pp
148The
149.Fn geom_stats_open
150and
151.Fn geom_stats_close
152functions open and close the necessary pathways to access the raw
153statistics information in the kernel.
154These functions are likely to
155open one or more files and cache the file descriptors locally.
156The
157.Fn geom_stats_open
158function returns zero on success, and sets
159.Va errno
160if not.
161.Pp
162The
163.Fn geom_stats_resync
164function will check if more statistics collection points have been
165added in the kernel since
166.Fn geom_stats_open
167or the previous call to
168.Fn geom_stats_resync .
169.Pp
170The
171.Fn geom_stats_snapshot_get
172function
173will acquire a snapshot of the raw data from the kernel, and while a
174reasonable effort is made to make this snapshot as atomic and consistent
175as possible, no guarantee is given that it will actually be so.
176The snapshot must be freed again using the
177.Fn geom_stats_snapshot_free
178function.
179The
180.Fn geom_stats_snapshot_get
181function returns
182.Dv NULL
183on failure.
184.Pp
185The
186.Fn geom_stats_snapshot_timestamp
187function
188provides access to the timestamp acquired in the snapshot.
189.Pp
190The
191.Fn geom_stats_snapshot_reset
192and
193.Fn geom_stats_snapshot_next
194functions
195provide an iterator over the statistics slots in the snapshot.
196The
197.Fn geom_stats_snapshot_reset
198function
199forces the internal pointer in the snapshot back to before the first item.
200The
201.Fn geom_stats_snapshot_next
202function
203returns the next item, and
204.Dv NULL
205if there are no more items in the snapshot.
206.Ss "Control Functions"
207The
208.Fn gctl_*
209functions are used to send requests to GEOM classes.
210In order for a GEOM
211class to actually be able to receive these requests, it must have defined a
212"ctlreq" method.
213.Pp
214A
215.Vt "struct gctl_req *" ,
216obtained with
217.Fn gctl_get_handle ,
218can hold any number of parameters, which must be added to it with
219.Fn gctl_ro_param
220(for read-only parameters) or
221.Fn gctl_rw_param
222(for read/write parameters).
223.Pp
224Both
225.Fn gctl_ro_param
226and
227.Fn gctl_rw_param
228take a string
229.Fa name ,
230which is used to identify the parameter, and a
231.Fa value ,
232which contains, in the read-only case, the data to be passed to the
233GEOM class, or, in the read/write case, a pointer to preallocated memory
234that the GEOM class should fill with the desired data.
235If
236.Fa len
237is negative, it is assumed that
238.Fa value
239is an
240.Tn ASCII
241string and the actual length is taken from the string length of
242.Fa value ;
243otherwise it must hold the size of
244.Fa value .
245.Pp
246A parameter with a
247.Fa name
248containing the string
249.Qq Li class
250is mandatory for each request, and the
251corresponding
252.Fa value
253must hold the name of the GEOM class where the request should be sent to.
254.Pp
255Also mandatory for each request is a parameter with a
256.Fa name
257called
258.Qq Li verb ,
259and the corresponding
260.Fa value
261needs to hold the command string that the GEOM class should react upon.
262.Pp
263Once all desired parameters are filled in, the request must be sent to
264the GEOM subsystem with
265.Fn gctl_issue ,
266which returns
267.Dv NULL
268on success, or a string containing the error message
269on failure.
270.Pp
271After the request is finished, the allocated memory should be released with
272.Fn gctl_free .
273.Pp
274The
275.Fn gctl_dump
276function
277can be used to format the contents of
278.Fa req
279to the open file handle pointed to by
280.Fa f ,
281for debugging purposes.
282.Pp
283Error handling for the control functions is postponed until the call
284to
285.Fn gctl_issue ,
286which returns
287.Dv NULL
288on success, or an error message corresponding to the
289first error which happened.
290.Ss "Utility Functions"
291The
292.Fn geom_getxml
293function is a wrapper around
294.Xr sysctl 3
295that fetches the
296.Ar kern.geom.confxml
297OID, and returns it's value.
298The allocated memory should be released with
299.Xr free 3
300after use.
301.Pp
302The
303.Fn geom_xml2tree
304function parses the XML representation of a GEOM topology passed as
305.Ar p ,
306allocates the needed data structures to access this information and fills in
307the passed
308.Ar gmp
309data structure.
310Memory allocated during this transformation should be released
311using
312.Fn geom_deletetree
313after use.
314.Pp
315The
316.Fn geom_gettree
317function is a wrapper around the
318.Fn geom_getxml
319and
320.Fn geom_xml2tree
321functions.
322Memory allocated during this operation should be released using
323.Fn geom_deletetree
324after use.
325.Pp
326The
327.Fn geom_deletetree
328function releases memory allocated for storing the data-structures referenced by
329.Ar gmp .
330.Pp
331The
332.Fn g_*
333functions are used to communicate with GEOM providers.
334.Pp
335The
336.Fn g_open
337function opens the given provider and returns file descriptor number, which can
338be used with other functions.
339The
340.Fa dowrite
341argument indicates if operations that modify the provider (like
342.Fn g_flush
343or
344.Fn g_delete )
345are going to be called.
346.Pp
347The
348.Fn g_close
349function closes the provider.
350.Pp
351The
352.Fn g_mediasize
353function returns size of the given provider.
354.Pp
355The
356.Fn g_sectorsize
357function returns sector size of the given provider.
358.Pp
359The
360.Fn g_stripeoffset
361function returns stripe offset of the given provider.
362.Pp
363The
364.Fn g_stripesize
365function returns stripe size of the given provider.
366.Pp
367The
368.Fn g_flush
369function sends
370.Dv BIO_FLUSH
371request to flush write cache of the provider.
372.Pp
373The
374.Fn g_delete
375function tells the provider that the given data range is no longer used.
376.Pp
377The
378.Fn g_device_path
379function returns the full path to a provider given a partial or full path to the
380device node.
381.Dv NULL
382is returned if the device cannot be found or is not a valid geom provider.
383.Pp
384The
385.Fn g_get_ident
386function returns provider's fixed and unique identifier.
387The
388.Fa ident
389argument should be at least
390.Dv DISK_IDENT_SIZE
391big.
392.Pp
393The
394.Fn g_get_name
395function returns name of the provider, which identifier is equal to the
396.Fa ident
397string.
398.Pp
399The
400.Fn g_open_by_ident
401function opens provider using its identification, unlike
402.Fn g_open
403which uses the provider's name.
404The function will store the provider's name in the
405.Fa name
406parameter if it is not
407.Dv NULL .
408.Pp
409The
410.Fn g_providername
411function returns the provider name of an open file descriptor.
412.Dv NULL
413is returned the file descriptor does not point to a valid geom provider.
414.Pp
415All functions except
416.Fn g_providername
417and
418.Fn g_device_path
419return a value greater than or equal to
420.Va 0
421on success or
422.Va -1
423on failure.
424.Sh EXAMPLES
425Create a request that is to be sent to the CCD class, and tell
426it to destroy a specific geom:
427.Bd -literal -offset indent
428H = gctl_get_handle();
429gctl_ro_param(H, "verb", -1, "destroy geom");
430gctl_ro_param(H, "class", -1, "CCD");
431sprintf(buf, "ccd%d", ccd);
432gctl_ro_param(H, "geom", -1, buf);
433errstr = gctl_issue(H);
434if (errstr != NULL)
435    err(1, "could not destroy ccd: %s", errstr);
436gctl_free(H);
437.Ed
438.Sh HISTORY
439The
440.Nm geom
441library appeared in
442.Fx 5.1 .
443.Sh AUTHORS
444.An Poul-Henning Kamp Aq Mt phk@FreeBSD.org
445.An Lukas Ertl Aq Mt le@FreeBSD.org
446.An Pawel Jakub Dawidek Aq Mt pjd@FreeBSD.org
447