ptyfs_subr.c revision 1.2.2.2 1 1.2.2.2 skrll /* $NetBSD: ptyfs_subr.c,v 1.2.2.2 2004/11/14 08:15:57 skrll Exp $ */
2 1.2.2.2 skrll
3 1.2.2.2 skrll /*
4 1.2.2.2 skrll * Copyright (c) 1993
5 1.2.2.2 skrll * The Regents of the University of California. All rights reserved.
6 1.2.2.2 skrll *
7 1.2.2.2 skrll * This code is derived from software contributed to Berkeley by
8 1.2.2.2 skrll * Jan-Simon Pendry.
9 1.2.2.2 skrll *
10 1.2.2.2 skrll * Redistribution and use in source and binary forms, with or without
11 1.2.2.2 skrll * modification, are permitted provided that the following conditions
12 1.2.2.2 skrll * are met:
13 1.2.2.2 skrll * 1. Redistributions of source code must retain the above copyright
14 1.2.2.2 skrll * notice, this list of conditions and the following disclaimer.
15 1.2.2.2 skrll * 2. Redistributions in binary form must reproduce the above copyright
16 1.2.2.2 skrll * notice, this list of conditions and the following disclaimer in the
17 1.2.2.2 skrll * documentation and/or other materials provided with the distribution.
18 1.2.2.2 skrll * 3. Neither the name of the University nor the names of its contributors
19 1.2.2.2 skrll * may be used to endorse or promote products derived from this software
20 1.2.2.2 skrll * without specific prior written permission.
21 1.2.2.2 skrll *
22 1.2.2.2 skrll * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 1.2.2.2 skrll * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 1.2.2.2 skrll * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 1.2.2.2 skrll * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 1.2.2.2 skrll * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 1.2.2.2 skrll * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 1.2.2.2 skrll * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 1.2.2.2 skrll * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 1.2.2.2 skrll * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 1.2.2.2 skrll * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 1.2.2.2 skrll * SUCH DAMAGE.
33 1.2.2.2 skrll *
34 1.2.2.2 skrll * @(#)ptyfs_subr.c 8.6 (Berkeley) 5/14/95
35 1.2.2.2 skrll */
36 1.2.2.2 skrll
37 1.2.2.2 skrll /*
38 1.2.2.2 skrll * Copyright (c) 1994 Christopher G. Demetriou. All rights reserved.
39 1.2.2.2 skrll * Copyright (c) 1993 Jan-Simon Pendry
40 1.2.2.2 skrll *
41 1.2.2.2 skrll * This code is derived from software contributed to Berkeley by
42 1.2.2.2 skrll * Jan-Simon Pendry.
43 1.2.2.2 skrll *
44 1.2.2.2 skrll * Redistribution and use in source and binary forms, with or without
45 1.2.2.2 skrll * modification, are permitted provided that the following conditions
46 1.2.2.2 skrll * are met:
47 1.2.2.2 skrll * 1. Redistributions of source code must retain the above copyright
48 1.2.2.2 skrll * notice, this list of conditions and the following disclaimer.
49 1.2.2.2 skrll * 2. Redistributions in binary form must reproduce the above copyright
50 1.2.2.2 skrll * notice, this list of conditions and the following disclaimer in the
51 1.2.2.2 skrll * documentation and/or other materials provided with the distribution.
52 1.2.2.2 skrll * 3. All advertising materials mentioning features or use of this software
53 1.2.2.2 skrll * must display the following acknowledgement:
54 1.2.2.2 skrll * This product includes software developed by the University of
55 1.2.2.2 skrll * California, Berkeley and its contributors.
56 1.2.2.2 skrll * 4. Neither the name of the University nor the names of its contributors
57 1.2.2.2 skrll * may be used to endorse or promote products derived from this software
58 1.2.2.2 skrll * without specific prior written permission.
59 1.2.2.2 skrll *
60 1.2.2.2 skrll * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
61 1.2.2.2 skrll * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
62 1.2.2.2 skrll * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
63 1.2.2.2 skrll * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
64 1.2.2.2 skrll * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
65 1.2.2.2 skrll * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
66 1.2.2.2 skrll * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
67 1.2.2.2 skrll * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
68 1.2.2.2 skrll * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
69 1.2.2.2 skrll * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
70 1.2.2.2 skrll * SUCH DAMAGE.
71 1.2.2.2 skrll *
72 1.2.2.2 skrll * @(#)procfs_subr.c 8.6 (Berkeley) 5/14/95
73 1.2.2.2 skrll */
74 1.2.2.2 skrll
75 1.2.2.2 skrll #include <sys/cdefs.h>
76 1.2.2.2 skrll __KERNEL_RCSID(0, "$NetBSD: ptyfs_subr.c,v 1.2.2.2 2004/11/14 08:15:57 skrll Exp $");
77 1.2.2.2 skrll
78 1.2.2.2 skrll #include <sys/param.h>
79 1.2.2.2 skrll #include <sys/systm.h>
80 1.2.2.2 skrll #include <sys/time.h>
81 1.2.2.2 skrll #include <sys/kernel.h>
82 1.2.2.2 skrll #include <sys/vnode.h>
83 1.2.2.2 skrll #include <sys/malloc.h>
84 1.2.2.2 skrll #include <sys/stat.h>
85 1.2.2.2 skrll #include <sys/file.h>
86 1.2.2.2 skrll #include <sys/namei.h>
87 1.2.2.2 skrll #include <sys/filedesc.h>
88 1.2.2.2 skrll #include <sys/select.h>
89 1.2.2.2 skrll #include <sys/tty.h>
90 1.2.2.2 skrll #include <sys/pty.h>
91 1.2.2.2 skrll
92 1.2.2.2 skrll #include <fs/ptyfs/ptyfs.h>
93 1.2.2.2 skrll #include <miscfs/specfs/specdev.h>
94 1.2.2.2 skrll
95 1.2.2.2 skrll static struct lock ptyfs_hashlock;
96 1.2.2.2 skrll
97 1.2.2.2 skrll static LIST_HEAD(ptyfs_hashhead, ptyfsnode) *ptyfs_used_tbl, *ptyfs_free_tbl;
98 1.2.2.2 skrll static u_long ptyfs_used_mask, ptyfs_free_mask; /* size of hash table - 1 */
99 1.2.2.2 skrll static struct simplelock ptyfs_used_slock, ptyfs_free_slock;
100 1.2.2.2 skrll
101 1.2.2.2 skrll static void ptyfs_getinfo(struct ptyfsnode *, struct lwp *);
102 1.2.2.2 skrll
103 1.2.2.2 skrll static void ptyfs_hashins(struct ptyfsnode *);
104 1.2.2.2 skrll static void ptyfs_hashrem(struct ptyfsnode *);
105 1.2.2.2 skrll
106 1.2.2.2 skrll static struct vnode *ptyfs_used_get(ptyfstype, int, struct mount *);
107 1.2.2.2 skrll static struct ptyfsnode *ptyfs_free_get(ptyfstype, int, struct lwp *);
108 1.2.2.2 skrll
109 1.2.2.2 skrll static void ptyfs_rehash(struct simplelock *, struct ptyfs_hashhead **,
110 1.2.2.2 skrll u_long *);
111 1.2.2.2 skrll
112 1.2.2.2 skrll #define PTYHASH(type, pty, mask) (PTYFS_FILENO(type, pty) % (mask + 1))
113 1.2.2.2 skrll
114 1.2.2.2 skrll
115 1.2.2.2 skrll static void
116 1.2.2.2 skrll ptyfs_getinfo(struct ptyfsnode *ptyfs, struct lwp *l)
117 1.2.2.2 skrll {
118 1.2.2.2 skrll extern struct ptm_pty *ptyfs_save_ptm, ptm_ptyfspty;
119 1.2.2.2 skrll
120 1.2.2.2 skrll if (ptyfs->ptyfs_type == PTYFSroot) {
121 1.2.2.2 skrll ptyfs->ptyfs_mode = S_IRUSR|S_IXUSR|S_IRGRP|S_IXGRP|
122 1.2.2.2 skrll S_IROTH|S_IXOTH;
123 1.2.2.2 skrll goto out;
124 1.2.2.2 skrll } else
125 1.2.2.2 skrll ptyfs->ptyfs_mode = S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|
126 1.2.2.2 skrll S_IROTH|S_IWOTH;
127 1.2.2.2 skrll
128 1.2.2.2 skrll if (ptyfs_save_ptm != NULL && ptyfs_save_ptm != &ptm_ptyfspty) {
129 1.2.2.2 skrll int error;
130 1.2.2.2 skrll struct nameidata nd;
131 1.2.2.2 skrll char ttyname[64];
132 1.2.2.2 skrll struct ucred *cred;
133 1.2.2.2 skrll struct vattr va;
134 1.2.2.2 skrll /*
135 1.2.2.2 skrll * We support traditional ptys, so we copy the info
136 1.2.2.2 skrll * from the inode
137 1.2.2.2 skrll */
138 1.2.2.2 skrll if ((error = (*ptyfs_save_ptm->makename)(
139 1.2.2.2 skrll ptyfs_save_ptm, ttyname, sizeof(ttyname),
140 1.2.2.2 skrll ptyfs->ptyfs_pty, ptyfs->ptyfs_type == PTYFSpts ? 't'
141 1.2.2.2 skrll : 'p')) != 0)
142 1.2.2.2 skrll goto out;
143 1.2.2.2 skrll NDINIT(&nd, LOOKUP, NOFOLLOW|LOCKLEAF, UIO_SYSSPACE, ttyname,
144 1.2.2.2 skrll l);
145 1.2.2.2 skrll if ((error = namei(&nd)) != 0)
146 1.2.2.2 skrll goto out;
147 1.2.2.2 skrll cred = crget();
148 1.2.2.2 skrll error = VOP_GETATTR(nd.ni_vp, &va, cred, l);
149 1.2.2.2 skrll crfree(cred);
150 1.2.2.2 skrll VOP_UNLOCK(nd.ni_vp, 0);
151 1.2.2.2 skrll vrele(nd.ni_vp);
152 1.2.2.2 skrll if (error)
153 1.2.2.2 skrll goto out;
154 1.2.2.2 skrll ptyfs->ptyfs_uid = va.va_uid;
155 1.2.2.2 skrll ptyfs->ptyfs_gid = va.va_gid;
156 1.2.2.2 skrll ptyfs->ptyfs_mode = va.va_mode;
157 1.2.2.2 skrll ptyfs->ptyfs_flags = va.va_flags;
158 1.2.2.2 skrll ptyfs->ptyfs_birthtime = va.va_birthtime;
159 1.2.2.2 skrll ptyfs->ptyfs_ctime = va.va_ctime;
160 1.2.2.2 skrll ptyfs->ptyfs_mtime = va.va_mtime;
161 1.2.2.2 skrll ptyfs->ptyfs_atime = va.va_atime;
162 1.2.2.2 skrll return;
163 1.2.2.2 skrll }
164 1.2.2.2 skrll out:
165 1.2.2.2 skrll ptyfs->ptyfs_uid = ptyfs->ptyfs_gid = 0;
166 1.2.2.2 skrll TIMEVAL_TO_TIMESPEC(&time, &ptyfs->ptyfs_ctime);
167 1.2.2.2 skrll ptyfs->ptyfs_birthtime = ptyfs->ptyfs_mtime =
168 1.2.2.2 skrll ptyfs->ptyfs_atime = ptyfs->ptyfs_ctime;
169 1.2.2.2 skrll ptyfs->ptyfs_flags = 0;
170 1.2.2.2 skrll }
171 1.2.2.2 skrll
172 1.2.2.2 skrll
173 1.2.2.2 skrll /*
174 1.2.2.2 skrll * allocate a ptyfsnode/vnode pair. the vnode is
175 1.2.2.2 skrll * referenced, and locked.
176 1.2.2.2 skrll *
177 1.2.2.2 skrll * the pid, ptyfs_type, and mount point uniquely
178 1.2.2.2 skrll * identify a ptyfsnode. the mount point is needed
179 1.2.2.2 skrll * because someone might mount this filesystem
180 1.2.2.2 skrll * twice.
181 1.2.2.2 skrll *
182 1.2.2.2 skrll * all ptyfsnodes are maintained on a singly-linked
183 1.2.2.2 skrll * list. new nodes are only allocated when they cannot
184 1.2.2.2 skrll * be found on this list. entries on the list are
185 1.2.2.2 skrll * removed when the vfs reclaim entry is called.
186 1.2.2.2 skrll *
187 1.2.2.2 skrll * a single lock is kept for the entire list. this is
188 1.2.2.2 skrll * needed because the getnewvnode() function can block
189 1.2.2.2 skrll * waiting for a vnode to become free, in which case there
190 1.2.2.2 skrll * may be more than one ptyess trying to get the same
191 1.2.2.2 skrll * vnode. this lock is only taken if we are going to
192 1.2.2.2 skrll * call getnewvnode, since the kernel itself is single-threaded.
193 1.2.2.2 skrll *
194 1.2.2.2 skrll * if an entry is found on the list, then call vget() to
195 1.2.2.2 skrll * take a reference. this is done because there may be
196 1.2.2.2 skrll * zero references to it and so it needs to removed from
197 1.2.2.2 skrll * the vnode free list.
198 1.2.2.2 skrll */
199 1.2.2.2 skrll int
200 1.2.2.2 skrll ptyfs_allocvp(struct mount *mp, struct vnode **vpp, ptyfstype type, int pty,
201 1.2.2.2 skrll struct lwp *l)
202 1.2.2.2 skrll {
203 1.2.2.2 skrll struct ptyfsnode *ptyfs;
204 1.2.2.2 skrll struct vnode *vp, *nvp;
205 1.2.2.2 skrll int error;
206 1.2.2.2 skrll
207 1.2.2.2 skrll do {
208 1.2.2.2 skrll if ((*vpp = ptyfs_used_get(type, pty, mp)) != NULL)
209 1.2.2.2 skrll return 0;
210 1.2.2.2 skrll } while (lockmgr(&ptyfs_hashlock, LK_EXCLUSIVE|LK_SLEEPFAIL, 0));
211 1.2.2.2 skrll
212 1.2.2.2 skrll if ((error = getnewvnode(VT_PTYFS, mp, ptyfs_vnodeop_p, &vp)) != 0) {
213 1.2.2.2 skrll *vpp = NULL;
214 1.2.2.2 skrll lockmgr(&ptyfs_hashlock, LK_RELEASE, NULL);
215 1.2.2.2 skrll return error;
216 1.2.2.2 skrll }
217 1.2.2.2 skrll
218 1.2.2.2 skrll vp->v_data = ptyfs = ptyfs_free_get(type, pty, l);
219 1.2.2.2 skrll ptyfs->ptyfs_vnode = vp;
220 1.2.2.2 skrll
221 1.2.2.2 skrll switch (type) {
222 1.2.2.2 skrll case PTYFSroot: /* /pts = dr-xr-xr-x */
223 1.2.2.2 skrll vp->v_type = VDIR;
224 1.2.2.2 skrll vp->v_flag = VROOT;
225 1.2.2.2 skrll break;
226 1.2.2.2 skrll
227 1.2.2.2 skrll case PTYFSpts: /* /pts/N = cxxxxxxxxx */
228 1.2.2.2 skrll case PTYFSptc: /* controlling side = cxxxxxxxxx */
229 1.2.2.2 skrll vp->v_type = VCHR;
230 1.2.2.2 skrll if ((nvp = checkalias(vp, PTYFS_MAKEDEV(ptyfs), mp)) != NULL) {
231 1.2.2.2 skrll /*
232 1.2.2.2 skrll * Discard unneeded vnode, but save its inode.
233 1.2.2.2 skrll */
234 1.2.2.2 skrll nvp->v_data = vp->v_data;
235 1.2.2.2 skrll vp->v_data = NULL;
236 1.2.2.2 skrll /* XXX spec_vnodeops has no locking, do it explicitly */
237 1.2.2.2 skrll VOP_UNLOCK(vp, 0);
238 1.2.2.2 skrll vp->v_op = spec_vnodeop_p;
239 1.2.2.2 skrll vp->v_flag &= ~VLOCKSWORK;
240 1.2.2.2 skrll vrele(vp);
241 1.2.2.2 skrll vgone(vp);
242 1.2.2.2 skrll lockmgr(&nvp->v_lock, LK_EXCLUSIVE, &nvp->v_interlock);
243 1.2.2.2 skrll /*
244 1.2.2.2 skrll * Reinitialize aliased inode.
245 1.2.2.2 skrll */
246 1.2.2.2 skrll vp = nvp;
247 1.2.2.2 skrll ptyfs->ptyfs_vnode = vp;
248 1.2.2.2 skrll }
249 1.2.2.2 skrll break;
250 1.2.2.2 skrll default:
251 1.2.2.2 skrll panic("ptyfs_allocvp");
252 1.2.2.2 skrll }
253 1.2.2.2 skrll
254 1.2.2.2 skrll ptyfs_hashins(ptyfs);
255 1.2.2.2 skrll uvm_vnp_setsize(vp, 0);
256 1.2.2.2 skrll lockmgr(&ptyfs_hashlock, LK_RELEASE, NULL);
257 1.2.2.2 skrll
258 1.2.2.2 skrll *vpp = vp;
259 1.2.2.2 skrll return 0;
260 1.2.2.2 skrll }
261 1.2.2.2 skrll
262 1.2.2.2 skrll int
263 1.2.2.2 skrll ptyfs_freevp(struct vnode *vp)
264 1.2.2.2 skrll {
265 1.2.2.2 skrll struct ptyfsnode *ptyfs = VTOPTYFS(vp);
266 1.2.2.2 skrll
267 1.2.2.2 skrll ptyfs_hashrem(ptyfs);
268 1.2.2.2 skrll vp->v_data = NULL;
269 1.2.2.2 skrll return 0;
270 1.2.2.2 skrll }
271 1.2.2.2 skrll
272 1.2.2.2 skrll /*
273 1.2.2.2 skrll * Initialize ptyfsnode hash table.
274 1.2.2.2 skrll */
275 1.2.2.2 skrll void
276 1.2.2.2 skrll ptyfs_hashinit(void)
277 1.2.2.2 skrll {
278 1.2.2.2 skrll lockinit(&ptyfs_hashlock, PINOD, "ptyfs_hashlock", 0, 0);
279 1.2.2.2 skrll ptyfs_used_tbl = hashinit(desiredvnodes / 4, HASH_LIST, M_UFSMNT,
280 1.2.2.2 skrll M_WAITOK, &ptyfs_used_mask);
281 1.2.2.2 skrll ptyfs_free_tbl = hashinit(desiredvnodes / 4, HASH_LIST, M_UFSMNT,
282 1.2.2.2 skrll M_WAITOK, &ptyfs_free_mask);
283 1.2.2.2 skrll simple_lock_init(&ptyfs_used_slock);
284 1.2.2.2 skrll simple_lock_init(&ptyfs_free_slock);
285 1.2.2.2 skrll }
286 1.2.2.2 skrll
287 1.2.2.2 skrll void
288 1.2.2.2 skrll ptyfs_hashreinit(void)
289 1.2.2.2 skrll {
290 1.2.2.2 skrll ptyfs_rehash(&ptyfs_used_slock, &ptyfs_used_tbl, &ptyfs_used_mask);
291 1.2.2.2 skrll ptyfs_rehash(&ptyfs_free_slock, &ptyfs_free_tbl, &ptyfs_free_mask);
292 1.2.2.2 skrll }
293 1.2.2.2 skrll
294 1.2.2.2 skrll static void
295 1.2.2.2 skrll ptyfs_rehash(struct simplelock *hlock, struct ptyfs_hashhead **hhead,
296 1.2.2.2 skrll u_long *hmask)
297 1.2.2.2 skrll {
298 1.2.2.2 skrll struct ptyfsnode *pp;
299 1.2.2.2 skrll struct ptyfs_hashhead *oldhash, *hash;
300 1.2.2.2 skrll u_long i, oldmask, mask, val;
301 1.2.2.2 skrll
302 1.2.2.2 skrll hash = hashinit(desiredvnodes / 4, HASH_LIST, M_UFSMNT, M_WAITOK,
303 1.2.2.2 skrll &mask);
304 1.2.2.2 skrll
305 1.2.2.2 skrll simple_lock(hlock);
306 1.2.2.2 skrll oldhash = *hhead;
307 1.2.2.2 skrll oldmask = *hmask;
308 1.2.2.2 skrll *hhead = hash;
309 1.2.2.2 skrll *hmask = mask;
310 1.2.2.2 skrll for (i = 0; i <= oldmask; i++) {
311 1.2.2.2 skrll while ((pp = LIST_FIRST(&oldhash[i])) != NULL) {
312 1.2.2.2 skrll LIST_REMOVE(pp, ptyfs_hash);
313 1.2.2.2 skrll val = PTYHASH(pp->ptyfs_type, pp->ptyfs_pty,
314 1.2.2.2 skrll ptyfs_used_mask);
315 1.2.2.2 skrll LIST_INSERT_HEAD(&hash[val], pp, ptyfs_hash);
316 1.2.2.2 skrll }
317 1.2.2.2 skrll }
318 1.2.2.2 skrll simple_unlock(hlock);
319 1.2.2.2 skrll hashdone(oldhash, M_UFSMNT);
320 1.2.2.2 skrll }
321 1.2.2.2 skrll
322 1.2.2.2 skrll /*
323 1.2.2.2 skrll * Free ptyfsnode hash table.
324 1.2.2.2 skrll */
325 1.2.2.2 skrll void
326 1.2.2.2 skrll ptyfs_hashdone(void)
327 1.2.2.2 skrll {
328 1.2.2.2 skrll hashdone(ptyfs_used_tbl, M_UFSMNT);
329 1.2.2.2 skrll hashdone(ptyfs_free_tbl, M_UFSMNT);
330 1.2.2.2 skrll }
331 1.2.2.2 skrll
332 1.2.2.2 skrll /*
333 1.2.2.2 skrll * Get a ptyfsnode from the free table, or allocate one.
334 1.2.2.2 skrll * Removes the node from the free table.
335 1.2.2.2 skrll */
336 1.2.2.2 skrll struct ptyfsnode *
337 1.2.2.2 skrll ptyfs_free_get(ptyfstype type, int pty, struct lwp *l)
338 1.2.2.2 skrll {
339 1.2.2.2 skrll struct ptyfs_hashhead *ppp;
340 1.2.2.2 skrll struct ptyfsnode *pp;
341 1.2.2.2 skrll
342 1.2.2.2 skrll simple_lock(&ptyfs_free_slock);
343 1.2.2.2 skrll ppp = &ptyfs_free_tbl[PTYHASH(type, pty, ptyfs_free_mask)];
344 1.2.2.2 skrll LIST_FOREACH(pp, ppp, ptyfs_hash) {
345 1.2.2.2 skrll if (pty == pp->ptyfs_pty && pp->ptyfs_type == type) {
346 1.2.2.2 skrll LIST_REMOVE(pp, ptyfs_hash);
347 1.2.2.2 skrll simple_unlock(&ptyfs_free_slock);
348 1.2.2.2 skrll return pp;
349 1.2.2.2 skrll }
350 1.2.2.2 skrll }
351 1.2.2.2 skrll simple_unlock(&ptyfs_free_slock);
352 1.2.2.2 skrll
353 1.2.2.2 skrll MALLOC(pp, void *, sizeof(struct ptyfsnode), M_TEMP, M_WAITOK);
354 1.2.2.2 skrll pp->ptyfs_pty = pty;
355 1.2.2.2 skrll pp->ptyfs_type = type;
356 1.2.2.2 skrll pp->ptyfs_fileno = PTYFS_FILENO(pty, type);
357 1.2.2.2 skrll ptyfs_getinfo(pp, l);
358 1.2.2.2 skrll return pp;
359 1.2.2.2 skrll }
360 1.2.2.2 skrll
361 1.2.2.2 skrll struct vnode *
362 1.2.2.2 skrll ptyfs_used_get(ptyfstype type, int pty, struct mount *mp)
363 1.2.2.2 skrll {
364 1.2.2.2 skrll struct ptyfs_hashhead *ppp;
365 1.2.2.2 skrll struct ptyfsnode *pp;
366 1.2.2.2 skrll struct vnode *vp;
367 1.2.2.2 skrll
368 1.2.2.2 skrll loop:
369 1.2.2.2 skrll simple_lock(&ptyfs_used_slock);
370 1.2.2.2 skrll ppp = &ptyfs_used_tbl[PTYHASH(type, pty, ptyfs_used_mask)];
371 1.2.2.2 skrll LIST_FOREACH(pp, ppp, ptyfs_hash) {
372 1.2.2.2 skrll vp = PTYFSTOV(pp);
373 1.2.2.2 skrll if (pty == pp->ptyfs_pty && pp->ptyfs_type == type &&
374 1.2.2.2 skrll vp->v_mount == mp) {
375 1.2.2.2 skrll simple_lock(&vp->v_interlock);
376 1.2.2.2 skrll simple_unlock(&ptyfs_used_slock);
377 1.2.2.2 skrll if (vget(vp, LK_EXCLUSIVE | LK_INTERLOCK))
378 1.2.2.2 skrll goto loop;
379 1.2.2.2 skrll return vp;
380 1.2.2.2 skrll }
381 1.2.2.2 skrll }
382 1.2.2.2 skrll simple_unlock(&ptyfs_used_slock);
383 1.2.2.2 skrll return NULL;
384 1.2.2.2 skrll }
385 1.2.2.2 skrll
386 1.2.2.2 skrll /*
387 1.2.2.2 skrll * Insert the ptyfsnode into the used table and lock it.
388 1.2.2.2 skrll */
389 1.2.2.2 skrll static void
390 1.2.2.2 skrll ptyfs_hashins(struct ptyfsnode *pp)
391 1.2.2.2 skrll {
392 1.2.2.2 skrll struct ptyfs_hashhead *ppp;
393 1.2.2.2 skrll
394 1.2.2.2 skrll /* lock the ptyfsnode, then put it on the appropriate hash list */
395 1.2.2.2 skrll lockmgr(&pp->ptyfs_vnode->v_lock, LK_EXCLUSIVE, NULL);
396 1.2.2.2 skrll
397 1.2.2.2 skrll simple_lock(&ptyfs_used_slock);
398 1.2.2.2 skrll ppp = &ptyfs_used_tbl[PTYHASH(pp->ptyfs_type, pp->ptyfs_pty,
399 1.2.2.2 skrll ptyfs_used_mask)];
400 1.2.2.2 skrll LIST_INSERT_HEAD(ppp, pp, ptyfs_hash);
401 1.2.2.2 skrll simple_unlock(&ptyfs_used_slock);
402 1.2.2.2 skrll }
403 1.2.2.2 skrll
404 1.2.2.2 skrll /*
405 1.2.2.2 skrll * Remove the ptyfsnode from the used table, and add it to the free table
406 1.2.2.2 skrll */
407 1.2.2.2 skrll static void
408 1.2.2.2 skrll ptyfs_hashrem(struct ptyfsnode *pp)
409 1.2.2.2 skrll {
410 1.2.2.2 skrll struct ptyfs_hashhead *ppp;
411 1.2.2.2 skrll
412 1.2.2.2 skrll simple_lock(&ptyfs_used_slock);
413 1.2.2.2 skrll LIST_REMOVE(pp, ptyfs_hash);
414 1.2.2.2 skrll simple_unlock(&ptyfs_used_slock);
415 1.2.2.2 skrll
416 1.2.2.2 skrll simple_lock(&ptyfs_free_slock);
417 1.2.2.2 skrll ppp = &ptyfs_free_tbl[PTYHASH(pp->ptyfs_type, pp->ptyfs_pty,
418 1.2.2.2 skrll ptyfs_free_mask)];
419 1.2.2.2 skrll LIST_INSERT_HEAD(ppp, pp, ptyfs_hash);
420 1.2.2.2 skrll simple_unlock(&ptyfs_free_slock);
421 1.2.2.2 skrll }
422