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