1.\"
2.\" Copyright (c) 2009
3.\"	The DragonFly Project.  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.\"
9.\" 1. Redistributions of source code must retain the above copyright
10.\"    notice, this list of conditions and the following disclaimer.
11.\" 2. Redistributions in binary form must reproduce the above copyright
12.\"    notice, this list of conditions and the following disclaimer in
13.\"    the documentation and/or other materials provided with the
14.\"    distribution.
15.\" 3. Neither the name of The DragonFly Project nor the names of its
16.\"    contributors may be used to endorse or promote products derived
17.\"    from this software without specific, prior written permission.
18.\"
19.\" THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20.\" ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21.\" LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
22.\" FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE
23.\" COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
24.\" INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
25.\" BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
26.\" LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
27.\" AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
28.\" OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
29.\" OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30.\" SUCH DAMAGE.
31.\"
32.Dd August 22, 2019
33.Dt MAKE_AUTOCLONE_DEV 9
34.Os
35.Sh NAME
36.Nm make_autoclone_dev ,
37.Nm destroy_autoclone_dev ,
38.Nm devfs_clone_bitmap_init ,
39.Nm devfs_clone_bitmap_uninit ,
40.Nm devfs_clone_bitmap_chk ,
41.Nm devfs_clone_bitmap_set ,
42.Nm devfs_clone_bitmap_get ,
43.Nm devfs_clone_bitmap_put ,
44.Nm DEVFS_DECLARE_CLONE_BITMAP ,
45.Nm DEVFS_DEFINE_CLONE_BITMAP ,
46.Nm DEVFS_CLONE_BITMAP
47.Nd device clone functions
48.Sh SYNOPSIS
49.In sys/types.h
50.In sys/conf.h
51.In sys/devfs.h
52.Ft cdev_t
53.Fn make_autoclone_dev "struct dev_ops *ops" "struct devfs_bitmap *bitmap" "d_clone_t *nhandler" "uid_t uid" "gid_t gid" "int perms" "const char *fmt" "..."
54.Ft void
55.Fn destroy_autoclone_dev "cdev_t dev" "struct devfs_bitmap *bitmap"
56.Ft void
57.Fn devfs_clone_bitmap_init "struct devfs_bitmap *bitmap"
58.Ft void
59.Fn devfs_clone_bitmap_uninit "struct devfs_bitmap *bitmap"
60.Ft int
61.Fn devfs_clone_bitmap_chk "struct devfs_bitmap *bitmap" "int unit"
62.Ft int
63.Fn devfs_clone_bitmap_set "struct devfs_bitmap *bitmap" "int unit"
64.Ft int
65.Fn devfs_clone_bitmap_get "struct devfs_bitmap *bitmap" "int limit"
66.Ft void
67.Fn devfs_clone_bitmap_put "struct devfs_bitmap *bitmap" "int unit"
68.Fn DEVFS_DECLARE_CLONE_BITMAP "name"
69.Fn DEVFS_DEFINE_CLONE_BITMAP "name"
70.Fn DEVFS_CLONE_BITMAP "name"
71.Sh DESCRIPTION
72.Fn make_autoclone_dev
73creates a
74.Vt cdev_t
75with the default
76.Fa ops ,
77visible in the
78.Xr devfs 5
79namespace, that (when opened) will invoke the clone handler specified by
80.Fa nhandler .
81If a
82.Vt devfs_bitmap *
83is specified, the given
84.Fa bitmap
85is initialized using
86.Fn devfs_clone_bitmap_init .
87.Pp
88The clone handler must be defined as follows:
89.Bd -literal
90d_clone_t mydev_clone;
91
92int
93mydev_clone(struct dev_clone_args *ap)
94{
95};
96.Ed
97.Pp
98When called, the handler is passed a pointer to a populated
99.Vt dev_clone_args
100structure, which is defined as follows:
101.Bd -literal
102struct dev_clone_args {
103        struct dev_generic_args a_head;
104        struct cdev     *a_dev;
105        const char      *a_name;
106        size_t           a_namelen;
107        struct ucred    *a_cred;
108        int              a_mode;
109};
110.Ed
111.Pp
112.Fa a_head.a_dev
113is the
114.Vt cdev_t
115of the accessed autoclone device.
116.Fa a_name
117and
118.Fa a_namelen
119are the registered clonable base name and its length, as specified to
120.Fn make_autoclone_dev .
121.Fa a_mode
122and
123.Fa a_cred
124contain the mode and cred passed to the autoclone device's
125.Fn open .
126.Pp
127The clone handler must set
128.Fa a_dev
129to a new
130.Vt cdev_t ,
131returned by a call to
132.Fn make_only_dev .
133.Fa a_dev
134may also be set to
135.Dv NULL ,
136in which case the
137.Fn open
138will ultimately fail with
139.Er ENXIO .
140If
141.Fa a_dev
142is
143.Pf non- Ns Dv NULL ,
144it is automatically made visible and linked into
145.Xr devfs 5
146as if it was created with
147.Fn make_dev .
148Thus,
149.Fn destroy_dev
150should be used to destroy the cloned
151.Vt cdev_t
152once it is no longer required,
153usually during
154.Fn close .
155.Pp
156.Fn destroy_autoclone_dev
157destroys a
158.Vt cdev_t
159created by
160.Fn make_autoclone_dev
161unregistering its clone handler and (if non-NULL) also uninitializes its
162.Fa bitmap
163using
164.Fn devfs_clone_bitmap_uninit .
165.Pp
166.Fn devfs_clone_bitmap_init
167initializes the given clone
168.Fa bitmap
169so it is ready to use.
170.Pp
171.Fn devfs_clone_bitmap_uninit
172frees the memory associated with the specified clone
173.Fa bitmap
174and leaves it unusable.
175.Pp
176.Fn devfs_clone_bitmap_chk
177checks if
178.Fa unit
179is in use (set) and returns 1 if it is; otherwise 0 is returned.
180.Pp
181.Fn devfs_clone_bitmap_set
182marks
183.Fa unit
184in
185.Fa bitmap
186as used, so further calls to
187.Fn devfs_clone_bitmap_get
188cannot retrieve it.
189If one intends to use a clone handler along with preallocated devices, it
190is recommended to block the unit numbers of the preallocated devices by
191calling
192.Fn devfs_clone_bitmap_set
193on them.
194The function returns -1 if the unit is already allocated, otherwise 0.
195.Pp
196.Fn devfs_clone_bitmap_put
197marks
198.Fa unit
199in the
200.Fa bitmap
201as unused so further calls to
202.Fn devfs_clone_bitmap_fff
203or
204.Fn devfs_clone_bitmap_get
205can retrieve it again.
206It is recommended that the associated device be destroyed prior to
207making the
208.Fa unit
209available in the bitmap again, to avoid racing against a new clone.
210.Pp
211.Fn devfs_clone_bitmap_get
212will return the first unused unit number and also mark it as used
213in the
214.Fa bitmap .
215.Pp
216The
217.Fn DEVFS_DEFINE_CLONE_BITMAP
218macro defines a clone bitmap with the specified
219.Fa name .
220As long as the name specified is unique, this macro can be used to define
221global variables.
222Similarly,
223.Fn DEVFS_DECLARE_CLONE_BITMAP
224declares a clone bitmap.
225.Pp
226The
227.Fn DEVFS_CLONE_BITMAP
228is a macro which expands the specified
229.Fa name
230to the full name of a clone bitmap.
231It is used in conjunction with
232.Fn DEVFS_DEFINE_CLONE_BITMAP
233and
234.Fn DEVFS_DECLARE_CLONE_BITMAP ,
235as it uses the same name.
236.Sh HISTORY
237The
238.Xr devfs 5
239clone facilities and the associated functions all appeared in
240.Dx 2.3 .
241.Sh AUTHORS
242.An Alex Hornung
243