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.\" $FreeBSD$ 30.\" 31.Dd April 22, 2017 32.Dt LIBGEOM 3 33.Os 34.Sh NAME 35.Nm geom_stats_open , 36.Nm geom_stats_close , 37.Nm geom_stats_resync , 38.Nm geom_stats_snapshot_get , 39.Nm geom_stats_snapshot_free , 40.Nm geom_stats_snapshot_timestamp , 41.Nm geom_stats_snapshot_reset , 42.Nm geom_stats_snapshot_next , 43.Nm gctl_get_handle , 44.Nm gctl_ro_param , 45.Nm gctl_rw_param , 46.Nm gctl_issue , 47.Nm gctl_free , 48.Nm gctl_dump , 49.Nm g_open , 50.Nm g_close , 51.Nm g_mediasize , 52.Nm g_sectorsize , 53.Nm g_stripeoffset , 54.Nm g_stripesize , 55.Nm g_flush , 56.Nm g_delete , 57.Nm g_device_path , 58.Nm g_get_ident , 59.Nm g_get_name , 60.Nm g_open_by_ident , 61.Nm g_providername 62.Nd userland API library for kernel GEOM subsystem 63.Sh LIBRARY 64.Lb libgeom 65.Sh SYNOPSIS 66.Bd -literal 67/* stdio.h is only required for `gctl_dump` */ 68.Ed 69.In stdio.h 70.In libgeom.h 71.Ss "Statistics Functions" 72.Ft void 73.Fn geom_stats_close void 74.Ft int 75.Fn geom_stats_open void 76.Ft void 77.Fn geom_stats_resync void 78.Ft "void *" 79.Fn geom_stats_snapshot_get void 80.Ft void 81.Fn geom_stats_snapshot_free "void *arg" 82.Ft void 83.Fn geom_stats_snapshot_timestamp "void *arg" "struct timespec *tp" 84.Ft void 85.Fn geom_stats_snapshot_reset "void *arg" 86.Ft "struct devstat *" 87.Fn geom_stats_snapshot_next "void *arg" 88.Ss "Control Functions" 89.Ft "struct gctl_req *" 90.Fn gctl_get_handle "void" 91.Ft void 92.Fn gctl_ro_param "struct gctl_req *req" "const char *name" "int len" "const void *value" 93.Ft void 94.Fn gctl_rw_param "struct gctl_req *req" "const char *name" "int len" "void *value" 95.Ft "const char *" 96.Fn gctl_issue "struct gctl_req *req" 97.Ft void 98.Fn gctl_free "struct gctl_req *req" 99.Ft void 100.Fn gctl_dump "struct gctl_req *req" "FILE *f" 101.Ss "Utility Functions" 102.Ft int 103.Fn g_open "const char *name" "int dowrite" 104.Ft int 105.Fn g_close "int fd" 106.Ft off_t 107.Fn g_mediasize "int fd" 108.Ft ssize_t 109.Fn g_sectorsize "int fd" 110.Ft ssize_t 111.Fn g_stripeoffset "int fd" 112.Ft ssize_t 113.Fn g_stripesize "int fd" 114.Ft int 115.Fn g_flush "int fd" 116.Ft int 117.Fn g_delete "int fd" "off_t offset" "off_t length" 118.Ft "char *" 119.Fn g_device_path "const char *devpath" 120.Ft int 121.Fn g_get_ident "int fd" "char *ident" "size_t size" 122.Ft int 123.Fn g_get_name "const char *ident" "char *name" "size_t size" 124.Ft int 125.Fn g_open_by_ident "const char *ident" "int dowrite" "char *name" "size_t size" 126.Ft "char *" 127.Fn g_providername "int fd" 128.Sh DESCRIPTION 129The 130.Nm geom 131library contains the official and publicized API for 132interacting with the GEOM subsystem in the kernel. 133.Ss "Statistics Functions" 134GEOM collects statistics data for all consumers and providers, but does 135not perform any normalization or presentation on the raw data, this is 136left as an exercise for user-land presentation utilities. 137.Pp 138The 139.Fn geom_stats_open 140and 141.Fn geom_stats_close 142functions open and close the necessary pathways to access the raw 143statistics information in the kernel. 144These functions are likely to 145open one or more files and cache the file descriptors locally. 146The 147.Fn geom_stats_open 148function returns zero on success, and sets 149.Va errno 150if not. 151.Pp 152The 153.Fn geom_stats_resync 154function will check if more statistics collection points have been 155added in the kernel since 156.Fn geom_stats_open 157or the previous call to 158.Fn geom_stats_resync . 159.Pp 160The 161.Fn geom_stats_snapshot_get 162function 163will acquire a snapshot of the raw data from the kernel, and while a 164reasonable effort is made to make this snapshot as atomic and consistent 165as possible, no guarantee is given that it will actually be so. 166The snapshot must be freed again using the 167.Fn geom_stats_snapshot_free 168function. 169The 170.Fn geom_stats_snapshot_get 171function returns 172.Dv NULL 173on failure. 174.Pp 175The 176.Fn geom_stats_snapshot_timestamp 177function 178provides access to the timestamp acquired in the snapshot. 179.Pp 180The 181.Fn geom_stats_snapshot_reset 182and 183.Fn geom_stats_snapshot_next 184functions 185provide an iterator over the statistics slots in the snapshot. 186The 187.Fn geom_stats_snapshot_reset 188function 189forces the internal pointer in the snapshot back to before the first item. 190The 191.Fn geom_stats_snapshot_next 192function 193returns the next item, and 194.Dv NULL 195if there are no more items in the snapshot. 196.Ss "Control Functions" 197The 198.Fn gctl_* 199functions are used to send requests to GEOM classes. 200In order for a GEOM 201class to actually be able to receive these requests, it must have defined a 202"ctlreq" method. 203.Pp 204A 205.Vt "struct gctl_req *" , 206obtained with 207.Fn gctl_get_handle , 208can hold any number of parameters, which must be added to it with 209.Fn gctl_ro_param 210(for read-only parameters) or 211.Fn gctl_rw_param 212(for read/write parameters). 213.Pp 214Both 215.Fn gctl_ro_param 216and 217.Fn gctl_rw_param 218take a string 219.Fa name , 220which is used to identify the parameter, and a 221.Fa value , 222which contains, in the read-only case, the data to be passed to the 223GEOM class, or, in the read/write case, a pointer to preallocated memory 224that the GEOM class should fill with the desired data. 225If 226.Fa len 227is negative, it is assumed that 228.Fa value 229is an 230.Tn ASCII 231string and the actual length is taken from the string length of 232.Fa value ; 233otherwise it must hold the size of 234.Fa value . 235.Pp 236A parameter with a 237.Fa name 238containing the string 239.Qq Li class 240is mandatory for each request, and the 241corresponding 242.Fa value 243must hold the name of the GEOM class where the request should be sent to. 244.Pp 245Also mandatory for each request is a parameter with a 246.Fa name 247called 248.Qq Li verb , 249and the corresponding 250.Fa value 251needs to hold the command string that the GEOM class should react upon. 252.Pp 253Once all desired parameters are filled in, the request must be sent to 254the GEOM subsystem with 255.Fn gctl_issue , 256which returns 257.Dv NULL 258on success, or a string containing the error message 259on failure. 260.Pp 261After the request is finished, the allocated memory should be released with 262.Fn gctl_free . 263.Pp 264The 265.Fn gctl_dump 266function 267can be used to format the contents of 268.Fa req 269to the open file handle pointed to by 270.Fa f , 271for debugging purposes. 272.Pp 273Error handling for the control functions is postponed until the call 274to 275.Fn gctl_issue , 276which returns 277.Dv NULL 278on success, or an error message corresponding to the 279first error which happened. 280.Ss "Utility Functions" 281The 282.Fn g_* 283functions are used to communicate with GEOM providers. 284.Pp 285The 286.Fn g_open 287function opens the given provider and returns file descriptor number, which can 288be used with other functions. 289The 290.Fa dowrite 291argument indicates if operations that modify the provider (like 292.Fn g_flush 293or 294.Fn g_delete ) 295are going to be called. 296.Pp 297The 298.Fn g_close 299function closes the provider. 300.Pp 301The 302.Fn g_mediasize 303function returns size of the given provider. 304.Pp 305The 306.Fn g_sectorsize 307function returns sector size of the given provider. 308.Pp 309The 310.Fn g_stripeoffset 311function returns stripe offset of the given provider. 312.Pp 313The 314.Fn g_stripesize 315function returns stripe size of the given provider. 316.Pp 317The 318.Fn g_flush 319function sends 320.Dv BIO_FLUSH 321request to flush write cache of the provider. 322.Pp 323The 324.Fn g_delete 325function tells the provider that the given data range is no longer used. 326.Pp 327The 328.Fn g_device_path 329function returns the full path to a provider given a partial or full path to the 330device node. 331.Dv NULL 332is returned if the device cannot be found or is not a valid geom provider. 333.Pp 334The 335.Fn g_get_ident 336function returns provider's fixed and unique identifier. 337The 338.Fa ident 339argument should be at least 340.Dv DISK_IDENT_SIZE 341big. 342.Pp 343The 344.Fn g_get_name 345function returns name of the provider, which identifier is equal to the 346.Fa ident 347string. 348.Pp 349The 350.Fn g_open_by_ident 351function opens provider using its identification, unlike 352.Fn g_open 353which uses the provider's name. 354The function will store the provider's name in the 355.Fa name 356parameter if it is not 357.Dv NULL . 358.Pp 359The 360.Fn g_providername 361function returns the provider name of an open file descriptor. 362.Dv NULL 363is returned the file descriptor does not point to a valid geom provider. 364.Pp 365All functions except 366.Fn g_providername 367and 368.Fn g_device_path 369return a value greater than or equal to 370.Va 0 371on success or 372.Va -1 373on failure. 374.Sh EXAMPLES 375Create a request that is to be sent to the CCD class, and tell 376it to destroy a specific geom: 377.Bd -literal -offset indent 378H = gctl_get_handle(); 379gctl_ro_param(H, "verb", -1, "destroy geom"); 380gctl_ro_param(H, "class", -1, "CCD"); 381sprintf(buf, "ccd%d", ccd); 382gctl_ro_param(H, "geom", -1, buf); 383errstr = gctl_issue(H); 384if (errstr != NULL) 385 err(1, "could not destroy ccd: %s", errstr); 386gctl_free(H); 387.Ed 388.Sh HISTORY 389The 390.Nm geom 391library appeared in 392.Fx 5.1 . 393.Sh AUTHORS 394.An Poul-Henning Kamp Aq Mt phk@FreeBSD.org 395.An Lukas Ertl Aq Mt le@FreeBSD.org 396.An Pawel Jakub Dawidek Aq Mt pjd@FreeBSD.org 397