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 June 30, 2020
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 will invoke the clone handler specified by
80.Fa nhandler
81when it is opened.
82If the
83.Fa bitmap
84argument is specified, it will be initialized using
85.Fn devfs_clone_bitmap_init .
86.Pp
87The clone handler must be defined as follows:
88.Bd -literal -offset indent
89d_clone_t mydev_clone;
90
91int
92mydev_clone(struct dev_clone_args *ap)
93{
94};
95.Ed
96.Pp
97When called, the handler is passed a pointer to a populated
98.Vt dev_clone_args
99structure, which is defined as follows:
100.Bd -literal -offset indent
101struct dev_clone_args {
102        struct dev_generic_args a_head;
103        struct cdev     *a_dev;
104        const char      *a_name;
105        size_t           a_namelen;
106        struct ucred    *a_cred;
107        int              a_mode;
108};
109.Ed
110.Pp
111The
112.Fa a_head.a_dev
113is the
114.Vt cdev_t
115of the accessed autoclone device.
116The
117.Fa a_name
118and
119.Fa a_namelen
120are the registered clonable base name and its length, as specified to
121.Fn make_autoclone_dev .
122The
123.Fa a_mode
124and
125.Fa a_cred
126contain the mode and credential passed to the
127.Fn open
128of the autoclone device.
129.Pp
130The clone handler must set
131.Fa a_dev
132to a new
133.Vt cdev_t
134that is returned by a call to
135.Fn make_only_dev .
136The new
137.Vt cdev_t
138will be automatically made visible and linked into
139.Xr devfs 5
140namespace, as if it was created with
141.Fn make_dev .
142Thus,
143.Fn destroy_dev
144should be used to destroy the cloned
145.Vt cdev_t
146once it is no longer required,
147usually during
148.Fn close .
149.Pp
150.Fn destroy_autoclone_dev
151destroys a
152.Vt cdev_t
153created by
154.Fn make_autoclone_dev
155unregistering its clone handler and (if non-NULL) also uninitializes its
156.Fa bitmap
157using
158.Fn devfs_clone_bitmap_uninit .
159.Pp
160.Fn devfs_clone_bitmap_init
161initializes the given clone
162.Fa bitmap
163so it is ready to use.
164.Pp
165.Fn devfs_clone_bitmap_uninit
166frees the memory associated with the specified clone
167.Fa bitmap
168and leaves it unusable.
169.Pp
170.Fn devfs_clone_bitmap_chk
171checks if
172.Fa unit
173is in use (set) and returns 1 if it is; otherwise 0 is returned.
174.Pp
175.Fn devfs_clone_bitmap_set
176marks
177.Fa unit
178in the
179.Fa bitmap
180as used, so further calls to
181.Fn devfs_clone_bitmap_get
182cannot retrieve it.
183The function returns -1 if the unit is already allocated, otherwise 0.
184If one intends to use a clone handler along with preallocated devices, it
185is recommended to block the unit numbers of the preallocated devices by
186calling
187.Fn devfs_clone_bitmap_set
188on them.
189.Pp
190.Fn devfs_clone_bitmap_get
191will return the first unused unit number and also mark it as used
192in the
193.Fa bitmap .
194If the
195.Fa limit
196argument is greater than 0, the requested unit number cannot be greater
197than the given
198.Fa limit .
199The function returns -1 if no more units are available.
200.Pp
201.Fn devfs_clone_bitmap_put
202marks
203.Fa unit
204in the
205.Fa bitmap
206as unused so further calls to
207.Fn devfs_clone_bitmap_get
208can retrieve it again.
209It is recommended that the associated device be destroyed prior to
210making the
211.Fa unit
212available in the bitmap again, to avoid racing against a new clone.
213.Pp
214The
215.Fn DEVFS_DEFINE_CLONE_BITMAP
216macro defines a clone bitmap with the specified
217.Fa name .
218As long as the name specified is unique, this macro can be used to define
219global variables.
220Similarly,
221.Fn DEVFS_DECLARE_CLONE_BITMAP
222declares a clone bitmap.
223.Pp
224The
225.Fn DEVFS_CLONE_BITMAP
226is a macro which expands the specified
227.Fa name
228to the full name of a clone bitmap.
229It is used in conjunction with
230.Fn DEVFS_DEFINE_CLONE_BITMAP
231and
232.Fn DEVFS_DECLARE_CLONE_BITMAP ,
233as it uses the same name.
234.Sh SEE ALSO
235.Xr devfs 5 ,
236.Xr destroy_dev 9 ,
237.Xr make_dev 9 ,
238.Xr make_only_dev 9
239.Sh HISTORY
240The
241.Xr devfs 5
242clone facilities and the associated functions all appeared in
243.Dx 2.3 .
244.Sh AUTHORS
245.An Alex Hornung
246