kernel - Fix tapN creation >= 32 units, fix pty issues >= 32 ptys
[dragonfly.git] / sys / vfs / devfs / devfs_helper.c
1 /*
2  * Copyright (c) 2009 The DragonFly Project.  All rights reserved.
3  *
4  * This code is derived from software contributed to The DragonFly Project
5  * by Alex Hornung <ahornung@gmail.com>
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in
15  *    the documentation and/or other materials provided with the
16  *    distribution.
17  * 3. Neither the name of The DragonFly Project nor the names of its
18  *    contributors may be used to endorse or promote products derived
19  *    from this software without specific, prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE
25  * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26  * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
27  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
29  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
31  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32  * SUCH DAMAGE.
33  */
34 #include <sys/param.h>
35 #include <sys/systm.h>
36 #include <sys/kernel.h>
37 #include <sys/types.h>
38 #include <machine/limits.h>
39 #include <sys/devfs.h>
40
41 MALLOC_DECLARE(M_DEVFS);
42
43 /*
44  * DEVFS clone bitmap functions
45  */
46 void
47 devfs_clone_bitmap_init(struct devfs_bitmap *bitmap)
48 {
49         bitmap->chunks = DEVFS_BITMAP_INITIAL_SIZE;
50         bitmap->bitmap = kmalloc(bitmap->chunks * sizeof(u_long),
51                                  M_DEVFS, M_WAITOK);
52         memset(bitmap->bitmap, -1, bitmap->chunks * sizeof(u_long));
53 }
54
55
56 void
57 devfs_clone_bitmap_uninit(struct devfs_bitmap *bitmap)
58 {
59         kfree(bitmap->bitmap, M_DEVFS);
60 }
61
62
63 void
64 devfs_clone_bitmap_resize(struct devfs_bitmap *bitmap, int newchunks)
65 {
66         int oldchunks = bitmap->chunks;
67
68         bitmap->chunks = newchunks + 2;
69         bitmap->bitmap = krealloc(bitmap->bitmap,
70                                   sizeof(u_long) * bitmap->chunks,
71                                   M_DEVFS, M_WAITOK);
72         memset(bitmap->bitmap + oldchunks, -1,
73                sizeof(u_long) * (bitmap->chunks - oldchunks));
74 }
75
76
77 int
78 devfs_clone_bitmap_fff(struct devfs_bitmap *bitmap)
79 {
80         u_long curbitmap;
81         int bit, i;
82         int chunks;
83
84         chunks = bitmap->chunks;
85
86         for (i = 0; i < chunks + 1; i++) {
87                 if (i == chunks)
88                         devfs_clone_bitmap_resize(bitmap, i);
89                 curbitmap = bitmap->bitmap[i];
90
91                 if (curbitmap) {
92                         curbitmap &= (~curbitmap)+1;
93                         for (bit = 1; curbitmap != 1; bit++)
94                                 curbitmap >>= 1;
95
96                         return (bit - 1 + (i << 3) * sizeof(curbitmap));
97                 }
98         }
99
100         /*
101          * NOT REACHED
102          */
103         panic("devfs_clone_bitmap_fff");
104         return -1;
105 }
106
107 /*
108  * Caller wants to know if the specified unit has been allocated
109  * or not.  Return 0, indicating that it has not been allocated.
110  *
111  * (the bitmap implements 0=allocated, 1=not-allocated but the
112  * return value is inverted).
113  */
114 int
115 devfs_clone_bitmap_chk(struct devfs_bitmap *bitmap, int unit)
116 {
117         int chunk;
118
119         chunk = unit / (sizeof(u_long) * 8);
120         unit -= chunk * (sizeof(u_long) * 8);
121
122         if (chunk >= bitmap->chunks)
123                 return 0;               /* device does not exist */
124
125         return !((bitmap->bitmap[chunk]) & (1L << unit));
126 }
127
128
129 void
130 devfs_clone_bitmap_set(struct devfs_bitmap *bitmap, int unit)
131 {
132         int chunk;
133
134         chunk = unit / (sizeof(u_long) * 8);
135         unit -= chunk * (sizeof(u_long) * 8);
136
137         if (chunk >= bitmap->chunks)
138                 devfs_clone_bitmap_resize(bitmap, chunk);
139         bitmap->bitmap[chunk] &= ~(1<<unit);
140 }
141
142 /*
143  * Deallocate a unit number.  We must synchronize any destroy_dev()'s
144  * the device called before we clear the bitmap bit to avoid races
145  * against a new clone.
146  */
147 void
148 devfs_clone_bitmap_put(struct devfs_bitmap *bitmap, int unit)
149 {
150         int chunk;
151
152         devfs_config();
153
154         chunk = unit / (sizeof(u_long) * 8);
155         unit -= chunk * (sizeof(u_long) * 8);
156
157         if (chunk >= bitmap->chunks)
158                 return;
159         bitmap->bitmap[chunk] |= (1L << unit);
160 }
161
162 /*
163  * Allocate a unit number for a device
164  */
165 int
166 devfs_clone_bitmap_get(struct devfs_bitmap *bitmap, int limit)
167 {
168         int unit;
169
170         unit = devfs_clone_bitmap_fff(bitmap);
171         if ((limit > 0) && (unit > limit))
172                 return -1;
173         devfs_clone_bitmap_set(bitmap, unit);
174
175         return unit;
176 }
177