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