Fix cdevsw_remove() warnings related to the removal of mass media (e.g.
[dragonfly.git] / sys / kern / kern_conf.c
CommitLineData
984263bc
MD
1/*-
2 * Parts Copyright (c) 1995 Terrence R. Lambert
3 * Copyright (c) 1995 Julian R. Elischer
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
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 the
13 * documentation and/or other materials provided with the distribution.
14 * 3. All advertising materials mentioning features or use of this software
15 * must display the following acknowledgement:
16 * This product includes software developed by Terrence R. Lambert.
17 * 4. The name Terrence R. Lambert may not be used to endorse or promote
18 * products derived from this software without specific prior written
19 * permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY Julian R. Elischer ``AS IS'' AND ANY
22 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE TERRENCE R. LAMBERT BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 *
33 * $FreeBSD: src/sys/kern/kern_conf.c,v 1.73.2.3 2003/03/10 02:18:25 imp Exp $
dd98570a 34 * $DragonFly: src/sys/kern/kern_conf.c,v 1.9 2004/05/26 01:29:58 dillon Exp $
984263bc
MD
35 */
36
37#include <sys/param.h>
38#include <sys/kernel.h>
39#include <sys/sysctl.h>
40#include <sys/systm.h>
41#include <sys/module.h>
42#include <sys/malloc.h>
43#include <sys/conf.h>
44#include <sys/vnode.h>
45#include <sys/queue.h>
335dda38 46#include <sys/device.h>
984263bc
MD
47#include <machine/stdarg.h>
48
49#define cdevsw_ALLOCSTART (NUMCDEVSW/2)
50
984263bc
MD
51MALLOC_DEFINE(M_DEVT, "dev_t", "dev_t storage");
52
53/*
54 * This is the number of hash-buckets. Experiements with 'real-life'
55 * udev_t's show that a prime halfway between two powers of two works
56 * best.
57 */
58#define DEVT_HASH 83
59
60/* The number of dev_t's we can create before malloc(9) kick in. */
61#define DEVT_STASH 50
62
63static struct specinfo devt_stash[DEVT_STASH];
984263bc 64static LIST_HEAD(, specinfo) dev_hash[DEVT_HASH];
e4c9c0c8 65static LIST_HEAD(, specinfo) dev_free_list;
984263bc
MD
66
67static int free_devt;
68SYSCTL_INT(_debug, OID_AUTO, free_devt, CTLFLAG_RW, &free_devt, 0, "");
e4c9c0c8
MD
69int dev_ref_debug = 0;
70SYSCTL_INT(_debug, OID_AUTO, dev_refs, CTLFLAG_RW, &dev_ref_debug, 0, "");
984263bc 71
984263bc 72/*
e4c9c0c8
MD
73 * dev_t and u_dev_t primitives. Note that the major number is always
74 * extracted from si_udev, not from si_devsw, because si_devsw is replaced
75 * when a device is destroyed.
984263bc 76 */
984263bc
MD
77int
78major(dev_t x)
79{
80 if (x == NODEV)
81 return NOUDEV;
82 return((x->si_udev >> 8) & 0xff);
83}
84
85int
86minor(dev_t x)
87{
88 if (x == NODEV)
89 return NOUDEV;
90 return(x->si_udev & 0xffff00ff);
91}
92
93int
94lminor(dev_t x)
95{
96 int i;
97
98 if (x == NODEV)
99 return NOUDEV;
100 i = minor(x);
101 return ((i & 0xff) | (i >> 8));
102}
103
e4c9c0c8
MD
104/*
105 * This is a bit complex because devices are always created relative to
106 * a particular cdevsw, including 'hidden' cdevsw's (such as the raw device
107 * backing a disk subsystem overlay), so we have to compare both the
108 * devsw and udev fields to locate the correct device.
109 *
110 * The device is created if it does not already exist. If SI_ADHOC is not
111 * set the device will be referenced (once) and SI_ADHOC will be set.
112 * The caller must explicitly add additional references to the device if
113 * the caller wishes to track additional references.
114 */
115static
984263bc 116dev_t
e4c9c0c8 117hashdev(struct cdevsw *devsw, int x, int y)
984263bc
MD
118{
119 struct specinfo *si;
120 udev_t udev;
121 int hash;
122 static int stashed;
123
e4c9c0c8 124 udev = makeudev(x, y);
984263bc
MD
125 hash = udev % DEVT_HASH;
126 LIST_FOREACH(si, &dev_hash[hash], si_hash) {
e4c9c0c8 127 if (si->si_devsw == devsw && si->si_udev == udev)
984263bc
MD
128 return (si);
129 }
130 if (stashed >= DEVT_STASH) {
131 MALLOC(si, struct specinfo *, sizeof(*si), M_DEVT,
e4c9c0c8
MD
132 M_WAITOK|M_USE_RESERVE|M_ZERO);
133 } else if (LIST_FIRST(&dev_free_list)) {
134 si = LIST_FIRST(&dev_free_list);
984263bc
MD
135 LIST_REMOVE(si, si_hash);
136 } else {
137 si = devt_stash + stashed++;
138 si->si_flags |= SI_STASHED;
139 }
e4c9c0c8
MD
140 si->si_devsw = devsw;
141 si->si_flags |= SI_HASHED | SI_ADHOC;
984263bc 142 si->si_udev = udev;
e4c9c0c8 143 si->si_refs = 1;
984263bc 144 LIST_INSERT_HEAD(&dev_hash[hash], si, si_hash);
e4c9c0c8
MD
145 si->si_port = devsw->d_port;
146 devsw->d_clone(si);
147 if (devsw != &dead_cdevsw)
148 ++devsw->d_refs;
149 if (dev_ref_debug) {
150 printf("create dev %p %s(minor=%08x) refs=%d\n",
151 si, devtoname(si), uminor(si->si_udev),
152 si->si_refs);
984263bc 153 }
e4c9c0c8 154 return (si);
984263bc
MD
155}
156
e4c9c0c8
MD
157/*
158 * Convert a device pointer to a device number
159 */
984263bc
MD
160udev_t
161dev2udev(dev_t x)
162{
163 if (x == NODEV)
164 return NOUDEV;
165 return (x->si_udev);
166}
167
e4c9c0c8
MD
168/*
169 * Convert a device number to a device pointer. The device is referenced
170 * ad-hoc, meaning that the caller should call reference_dev() if it wishes
171 * to keep ahold of the returned structure long term.
172 *
173 * The returned device is associated with the currently installed cdevsw
174 * for the requested major number. NODEV is returned if the major number
175 * has not been registered.
176 */
984263bc
MD
177dev_t
178udev2dev(udev_t x, int b)
179{
e4c9c0c8
MD
180 dev_t dev;
181 struct cdevsw *devsw;
984263bc 182
e4c9c0c8
MD
183 if (x == NOUDEV || b != 0)
184 return(NODEV);
185 devsw = cdevsw_get(umajor(x), uminor(x));
186 if (devsw == NULL)
187 return(NODEV);
188 dev = hashdev(devsw, umajor(x), uminor(x));
189 return(dev);
984263bc
MD
190}
191
e4c9c0c8
MD
192int
193dev_is_good(dev_t dev)
194{
195 if (dev != NODEV && dev->si_devsw != &dead_cdevsw)
196 return(1);
197 return(0);
198}
199
200/*
201 * Various user device number extraction and conversion routines
202 */
984263bc
MD
203int
204uminor(udev_t dev)
205{
206 return(dev & 0xffff00ff);
207}
208
209int
210umajor(udev_t dev)
211{
212 return((dev & 0xff00) >> 8);
213}
214
215udev_t
216makeudev(int x, int y)
217{
218 return ((x << 8) | y);
219}
220
e4c9c0c8
MD
221/*
222 * Create an internal or external device.
223 *
224 * Device majors can be overloaded and used directly by the kernel without
225 * conflict, but userland will only see the particular device major that
226 * has been installed with cdevsw_add().
227 *
228 * This routine creates an ad-hoc entry for the device. The caller must
229 * call reference_dev() to track additional references beyond the ad-hoc
230 * entry. If an entry already exists, this function will set (or override)
231 * its cred requirements and name (XXX DEVFS interface).
232 */
984263bc 233dev_t
e4c9c0c8
MD
234make_dev(struct cdevsw *devsw, int minor, uid_t uid, gid_t gid,
235 int perms, const char *fmt, ...)
984263bc
MD
236{
237 dev_t dev;
e2565a42 238 __va_list ap;
984263bc
MD
239 int i;
240
e4c9c0c8
MD
241 /*
242 * compile the cdevsw and install the device
243 */
984263bc 244 compile_devsw(devsw);
e4c9c0c8
MD
245 dev = hashdev(devsw, devsw->d_maj, minor);
246
247 /*
248 * Set additional fields (XXX DEVFS interface goes here)
249 */
e2565a42 250 __va_start(ap, fmt);
984263bc
MD
251 i = kvprintf(fmt, NULL, dev->si_name, 32, ap);
252 dev->si_name[i] = '\0';
e2565a42 253 __va_end(ap);
984263bc
MD
254
255 return (dev);
256}
257
217ff971 258/*
e4c9c0c8
MD
259 * This function is similar to make_dev() but no cred information or name
260 * need be specified.
261 */
262dev_t
263make_adhoc_dev(struct cdevsw *devsw, int minor)
264{
265 dev_t dev;
266
267 dev = hashdev(devsw, devsw->d_maj, minor);
268 return(dev);
269}
270
271/*
272 * This function is similar to make_dev() except the new device is created
273 * using an old device as a template.
274 */
275dev_t
276make_sub_dev(dev_t odev, int minor)
277{
278 dev_t dev;
279
280 dev = hashdev(odev->si_devsw, umajor(odev->si_udev), minor);
281
282 /*
283 * Copy cred requirements and name info XXX DEVFS.
284 */
285 if (dev->si_name[0] == 0 && odev->si_name[0])
286 bcopy(odev->si_name, dev->si_name, sizeof(dev->si_name));
287 return (dev);
288}
289
290/*
291 * destroy_dev() removes the adhoc association for a device and revectors
292 * its devsw to &dead_cdevsw.
293 *
294 * This routine releases the reference count associated with the ADHOC
295 * entry, plus releases the reference count held by the caller. What this
296 * means is that you should not call destroy_dev(make_dev(...)), because
297 * make_dev() does not bump the reference count (beyond what it needs to
298 * create the ad-hoc association). Any procedure that intends to destroy
299 * a device must have its own reference to it first.
217ff971 300 */
984263bc
MD
301void
302destroy_dev(dev_t dev)
303{
e4c9c0c8 304 int hash;
217ff971 305
e4c9c0c8
MD
306 if (dev == NODEV)
307 return;
308 if ((dev->si_flags & SI_ADHOC) == 0) {
309 release_dev(dev);
310 return;
311 }
312 if (dev_ref_debug) {
313 printf("destroy dev %p %s(minor=%08x) refs=%d\n",
314 dev, devtoname(dev), uminor(dev->si_udev),
315 dev->si_refs);
316 }
317 if (dev->si_refs < 2) {
318 printf("destroy_dev(): too few references on device! "
319 "%p %s(minor=%08x) refs=%d\n",
320 dev, devtoname(dev), uminor(dev->si_udev),
321 dev->si_refs);
322 }
323 dev->si_flags &= ~SI_ADHOC;
324 if (dev->si_flags & SI_HASHED) {
325 hash = dev->si_udev % DEVT_HASH;
326 LIST_REMOVE(dev, si_hash);
327 dev->si_flags &= ~SI_HASHED;
328 }
217ff971
MD
329 if (dead_cdevsw.d_port == NULL)
330 compile_devsw(&dead_cdevsw);
e4c9c0c8
MD
331 if (dev->si_devsw && dev->si_devsw != &dead_cdevsw)
332 cdevsw_release(dev->si_devsw);
dd98570a
MD
333 dev->si_drv1 = NULL;
334 dev->si_drv2 = NULL;
217ff971 335 dev->si_devsw = &dead_cdevsw;
e4c9c0c8
MD
336 dev->si_port = dev->si_devsw->d_port;
337 --dev->si_refs; /* release adhoc association reference */
338 release_dev(dev); /* release callers reference */
339}
340
341/*
342 * Destroy all ad-hoc device associations associated with a domain within a
343 * device switch.
344 */
345void
346destroy_all_dev(struct cdevsw *devsw, u_int mask, u_int match)
347{
348 int i;
349 dev_t dev;
350 dev_t ndev;
351
352 for (i = 0; i < DEVT_HASH; ++i) {
353 ndev = LIST_FIRST(&dev_hash[i]);
354 while ((dev = ndev) != NULL) {
355 ndev = LIST_NEXT(dev, si_hash);
356 KKASSERT(dev->si_flags & SI_ADHOC);
357 if (dev->si_devsw == devsw &&
358 (dev->si_udev & mask) == match
359 ) {
360 ++dev->si_refs;
361 destroy_dev(dev);
362 }
363 }
364 }
365}
366
367/*
368 * Add a reference to a device. Callers generally add their own references
369 * when they are going to store a device node in a variable for long periods
370 * of time, to prevent a disassociation from free()ing the node.
371 *
372 * Also note that a caller that intends to call destroy_dev() must first
373 * obtain a reference on the device. The ad-hoc reference you get with
374 * make_dev() and friends is NOT sufficient to be able to call destroy_dev().
375 */
376dev_t
377reference_dev(dev_t dev)
378{
379 if (dev != NODEV) {
380 ++dev->si_refs;
381 if (dev_ref_debug) {
382 printf("reference dev %p %s(minor=%08x) refs=%d\n",
383 dev, devtoname(dev), uminor(dev->si_udev),
384 dev->si_refs);
385 }
386 }
387 return(dev);
388}
389
390/*
391 * release a reference on a device. The device will be freed when the last
392 * reference has been released.
393 *
394 * NOTE: we must use si_udev to figure out the original (major, minor),
395 * because si_devsw could already be pointing at dead_cdevsw.
396 */
397void
398release_dev(dev_t dev)
399{
400 if (dev == NODEV)
401 return;
402 if (free_devt) {
403 KKASSERT(dev->si_refs > 0);
404 } else {
405 if (dev->si_refs <= 0) {
406 printf("Warning: extra release of dev %p(%s)\n",
407 dev, devtoname(dev));
408 free_devt = 0; /* prevent bad things from occuring */
409 }
410 }
411 --dev->si_refs;
412 if (dev_ref_debug) {
413 printf("release dev %p %s(minor=%08x) refs=%d\n",
414 dev, devtoname(dev), uminor(dev->si_udev),
415 dev->si_refs);
416 }
417 if (dev->si_refs == 0) {
418 if (dev->si_flags & SI_ADHOC) {
419 printf("Warning: illegal final release on ADHOC"
420 " device %p(%s), the device was never"
421 " destroyed!\n",
422 dev, devtoname(dev));
423 }
424 if (dev->si_flags & SI_HASHED) {
425 printf("Warning: last release on device, no call"
426 " to destroy_dev() was made! dev %p(%s)\n",
427 dev, devtoname(dev));
428 dev->si_refs = 3;
429 destroy_dev(dev);
430 dev->si_refs = 0;
431 }
432 if (SLIST_FIRST(&dev->si_hlist) != NULL) {
433 printf("Warning: last release on device, vnode"
434 " associations still exist! dev %p(%s)\n",
435 dev, devtoname(dev));
436 free_devt = 0; /* prevent bad things from occuring */
437 }
438 if (dev->si_devsw && dev->si_devsw != &dead_cdevsw) {
439 cdevsw_release(dev->si_devsw);
440 dev->si_devsw = NULL;
441 }
442 if (free_devt) {
443 if (dev->si_flags & SI_STASHED) {
444 bzero(dev, sizeof(*dev));
445 LIST_INSERT_HEAD(&dev_free_list, dev, si_hash);
446 } else {
447 FREE(dev, M_DEVT);
448 }
449 }
450 }
984263bc
MD
451}
452
453const char *
454devtoname(dev_t dev)
455{
984263bc 456 int mynor;
335dda38
MD
457 int len;
458 char *p;
459 const char *dname;
984263bc 460
e4c9c0c8
MD
461 if (dev == NODEV)
462 return("#nodev");
984263bc
MD
463 if (dev->si_name[0] == '#' || dev->si_name[0] == '\0') {
464 p = dev->si_name;
335dda38
MD
465 len = sizeof(dev->si_name);
466 if ((dname = dev_dname(dev)) != NULL)
467 snprintf(p, len, "#%s/", dname);
984263bc 468 else
335dda38
MD
469 snprintf(p, len, "#%d/", major(dev));
470 len -= strlen(p);
984263bc
MD
471 p += strlen(p);
472 mynor = minor(dev);
473 if (mynor < 0 || mynor > 255)
335dda38 474 snprintf(p, len, "%#x", (u_int)mynor);
984263bc 475 else
335dda38 476 snprintf(p, len, "%d", mynor);
984263bc
MD
477 }
478 return (dev->si_name);
479}
e4c9c0c8 480