ptyfs_vnops.c revision 1.9 1 /* $NetBSD: ptyfs_vnops.c,v 1.9 2005/09/12 16:42:09 christos Exp $ */
2
3 /*
4 * Copyright (c) 1993, 1995
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 * @(#)procfs_vnops.c 8.18 (Berkeley) 5/21/95
35 */
36
37 /*
38 * Copyright (c) 1993 Jan-Simon Pendry
39 *
40 * This code is derived from software contributed to Berkeley by
41 * Jan-Simon Pendry.
42 *
43 * Redistribution and use in source and binary forms, with or without
44 * modification, are permitted provided that the following conditions
45 * are met:
46 * 1. Redistributions of source code must retain the above copyright
47 * notice, this list of conditions and the following disclaimer.
48 * 2. Redistributions in binary form must reproduce the above copyright
49 * notice, this list of conditions and the following disclaimer in the
50 * documentation and/or other materials provided with the distribution.
51 * 3. All advertising materials mentioning features or use of this software
52 * must display the following acknowledgement:
53 * This product includes software developed by the University of
54 * California, Berkeley and its contributors.
55 * 4. Neither the name of the University nor the names of its contributors
56 * may be used to endorse or promote products derived from this software
57 * without specific prior written permission.
58 *
59 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
60 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
61 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
62 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
63 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
64 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
65 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
66 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
67 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
68 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
69 * SUCH DAMAGE.
70 *
71 * @(#)procfs_vnops.c 8.18 (Berkeley) 5/21/95
72 */
73
74 /*
75 * ptyfs vnode interface
76 */
77
78 #include <sys/cdefs.h>
79 __KERNEL_RCSID(0, "$NetBSD: ptyfs_vnops.c,v 1.9 2005/09/12 16:42:09 christos Exp $");
80
81 #include <sys/param.h>
82 #include <sys/systm.h>
83 #include <sys/time.h>
84 #include <sys/kernel.h>
85 #include <sys/file.h>
86 #include <sys/filedesc.h>
87 #include <sys/proc.h>
88 #include <sys/vnode.h>
89 #include <sys/namei.h>
90 #include <sys/malloc.h>
91 #include <sys/mount.h>
92 #include <sys/select.h>
93 #include <sys/dirent.h>
94 #include <sys/resourcevar.h>
95 #include <sys/stat.h>
96 #include <sys/conf.h>
97 #include <sys/tty.h>
98 #include <sys/pty.h>
99
100 #include <uvm/uvm_extern.h> /* for PAGE_SIZE */
101
102 #include <machine/reg.h>
103
104 #include <fs/ptyfs/ptyfs.h>
105 #include <miscfs/genfs/genfs.h>
106 #include <miscfs/specfs/specdev.h>
107
108 /*
109 * Vnode Operations.
110 *
111 */
112
113 int ptyfs_lookup (void *);
114 #define ptyfs_create genfs_eopnotsupp
115 #define ptyfs_mknod genfs_eopnotsupp
116 int ptyfs_open (void *);
117 int ptyfs_close (void *);
118 int ptyfs_access (void *);
119 int ptyfs_getattr (void *);
120 int ptyfs_setattr (void *);
121 int ptyfs_read (void *);
122 int ptyfs_write (void *);
123 #define ptyfs_fcntl genfs_fcntl
124 int ptyfs_ioctl (void *);
125 int ptyfs_poll (void *);
126 int ptyfs_kqfilter (void *);
127 #define ptyfs_revoke genfs_revoke
128 #define ptyfs_mmap genfs_eopnotsupp
129 #define ptyfs_fsync genfs_nullop
130 #define ptyfs_seek genfs_nullop
131 #define ptyfs_remove genfs_eopnotsupp
132 #define ptyfs_link genfs_abortop
133 #define ptyfs_rename genfs_eopnotsupp
134 #define ptyfs_mkdir genfs_eopnotsupp
135 #define ptyfs_rmdir genfs_eopnotsupp
136 #define ptyfs_symlink genfs_abortop
137 int ptyfs_readdir (void *);
138 #define ptyfs_readlink genfs_eopnotsupp
139 #define ptyfs_abortop genfs_abortop
140 int ptyfs_reclaim (void *);
141 #define ptyfs_lock genfs_lock
142 #define ptyfs_unlock genfs_unlock
143 #define ptyfs_bmap genfs_badop
144 #define ptyfs_strategy genfs_badop
145 int ptyfs_print (void *);
146 int ptyfs_pathconf (void *);
147 #define ptyfs_islocked genfs_islocked
148 #define ptyfs_advlock genfs_einval
149 #define ptyfs_blkatoff genfs_eopnotsupp
150 #define ptyfs_valloc genfs_eopnotsupp
151 #define ptyfs_vfree genfs_nullop
152 #define ptyfs_truncate genfs_eopnotsupp
153 int ptyfs_update (void *);
154 #define ptyfs_bwrite genfs_eopnotsupp
155 #define ptyfs_putpages genfs_null_putpages
156
157 static int ptyfs_chown(struct vnode *, uid_t, gid_t, struct ucred *,
158 struct proc *);
159 static int ptyfs_chmod(struct vnode *, mode_t, struct ucred *, struct proc *);
160 static int atoi(const char *, size_t);
161
162 extern const struct cdevsw pts_cdevsw, ptc_cdevsw;
163
164 /*
165 * ptyfs vnode operations.
166 */
167 int (**ptyfs_vnodeop_p)(void *);
168 const struct vnodeopv_entry_desc ptyfs_vnodeop_entries[] = {
169 { &vop_default_desc, vn_default_error },
170 { &vop_lookup_desc, ptyfs_lookup }, /* lookup */
171 { &vop_create_desc, ptyfs_create }, /* create */
172 { &vop_mknod_desc, ptyfs_mknod }, /* mknod */
173 { &vop_open_desc, ptyfs_open }, /* open */
174 { &vop_close_desc, ptyfs_close }, /* close */
175 { &vop_access_desc, ptyfs_access }, /* access */
176 { &vop_getattr_desc, ptyfs_getattr }, /* getattr */
177 { &vop_setattr_desc, ptyfs_setattr }, /* setattr */
178 { &vop_read_desc, ptyfs_read }, /* read */
179 { &vop_write_desc, ptyfs_write }, /* write */
180 { &vop_ioctl_desc, ptyfs_ioctl }, /* ioctl */
181 { &vop_fcntl_desc, ptyfs_fcntl }, /* fcntl */
182 { &vop_poll_desc, ptyfs_poll }, /* poll */
183 { &vop_kqfilter_desc, ptyfs_kqfilter }, /* kqfilter */
184 { &vop_revoke_desc, ptyfs_revoke }, /* revoke */
185 { &vop_mmap_desc, ptyfs_mmap }, /* mmap */
186 { &vop_fsync_desc, ptyfs_fsync }, /* fsync */
187 { &vop_seek_desc, ptyfs_seek }, /* seek */
188 { &vop_remove_desc, ptyfs_remove }, /* remove */
189 { &vop_link_desc, ptyfs_link }, /* link */
190 { &vop_rename_desc, ptyfs_rename }, /* rename */
191 { &vop_mkdir_desc, ptyfs_mkdir }, /* mkdir */
192 { &vop_rmdir_desc, ptyfs_rmdir }, /* rmdir */
193 { &vop_symlink_desc, ptyfs_symlink }, /* symlink */
194 { &vop_readdir_desc, ptyfs_readdir }, /* readdir */
195 { &vop_readlink_desc, ptyfs_readlink }, /* readlink */
196 { &vop_abortop_desc, ptyfs_abortop }, /* abortop */
197 { &vop_inactive_desc, spec_inactive }, /* inactive */
198 { &vop_reclaim_desc, ptyfs_reclaim }, /* reclaim */
199 { &vop_lock_desc, ptyfs_lock }, /* lock */
200 { &vop_unlock_desc, ptyfs_unlock }, /* unlock */
201 { &vop_bmap_desc, ptyfs_bmap }, /* bmap */
202 { &vop_strategy_desc, ptyfs_strategy }, /* strategy */
203 { &vop_print_desc, ptyfs_print }, /* print */
204 { &vop_islocked_desc, ptyfs_islocked }, /* islocked */
205 { &vop_pathconf_desc, ptyfs_pathconf }, /* pathconf */
206 { &vop_advlock_desc, ptyfs_advlock }, /* advlock */
207 { &vop_blkatoff_desc, ptyfs_blkatoff }, /* blkatoff */
208 { &vop_valloc_desc, ptyfs_valloc }, /* valloc */
209 { &vop_vfree_desc, ptyfs_vfree }, /* vfree */
210 { &vop_truncate_desc, ptyfs_truncate }, /* truncate */
211 { &vop_update_desc, ptyfs_update }, /* update */
212 { &vop_bwrite_desc, ptyfs_bwrite }, /* bwrite */
213 { &vop_putpages_desc, ptyfs_putpages }, /* putpages */
214 { NULL, NULL }
215 };
216 const struct vnodeopv_desc ptyfs_vnodeop_opv_desc =
217 { &ptyfs_vnodeop_p, ptyfs_vnodeop_entries };
218
219 /*
220 * _reclaim is called when getnewvnode()
221 * wants to make use of an entry on the vnode
222 * free list. at this time the filesystem needs
223 * to free any private data and remove the node
224 * from any private lists.
225 */
226 int
227 ptyfs_reclaim(void *v)
228 {
229 struct vop_reclaim_args /* {
230 struct vnode *a_vp;
231 } */ *ap = v;
232 return ptyfs_freevp(ap->a_vp);
233 }
234
235 /*
236 * Return POSIX pathconf information applicable to special devices.
237 */
238 int
239 ptyfs_pathconf(void *v)
240 {
241 struct vop_pathconf_args /* {
242 struct vnode *a_vp;
243 int a_name;
244 register_t *a_retval;
245 } */ *ap = v;
246
247 switch (ap->a_name) {
248 case _PC_LINK_MAX:
249 *ap->a_retval = LINK_MAX;
250 return 0;
251 case _PC_MAX_CANON:
252 *ap->a_retval = MAX_CANON;
253 return 0;
254 case _PC_MAX_INPUT:
255 *ap->a_retval = MAX_INPUT;
256 return 0;
257 case _PC_PIPE_BUF:
258 *ap->a_retval = PIPE_BUF;
259 return 0;
260 case _PC_CHOWN_RESTRICTED:
261 *ap->a_retval = 1;
262 return 0;
263 case _PC_VDISABLE:
264 *ap->a_retval = _POSIX_VDISABLE;
265 return 0;
266 case _PC_SYNC_IO:
267 *ap->a_retval = 1;
268 return 0;
269 default:
270 return EINVAL;
271 }
272 }
273
274 /*
275 * _print is used for debugging.
276 * just print a readable description
277 * of (vp).
278 */
279 int
280 ptyfs_print(void *v)
281 {
282 struct vop_print_args /* {
283 struct vnode *a_vp;
284 } */ *ap = v;
285 struct ptyfsnode *ptyfs = VTOPTYFS(ap->a_vp);
286
287 printf("tag VT_PTYFS, type %d, pty %d\n",
288 ptyfs->ptyfs_type, ptyfs->ptyfs_pty);
289 return 0;
290 }
291
292 /*
293 * Invent attributes for ptyfsnode (vp) and store
294 * them in (vap).
295 * Directories lengths are returned as zero since
296 * any real length would require the genuine size
297 * to be computed, and nothing cares anyway.
298 *
299 * this is relatively minimal for ptyfs.
300 */
301 int
302 ptyfs_getattr(void *v)
303 {
304 struct vop_getattr_args /* {
305 struct vnode *a_vp;
306 struct vattr *a_vap;
307 struct ucred *a_cred;
308 struct proc *a_p;
309 } */ *ap = v;
310 struct ptyfsnode *ptyfs = VTOPTYFS(ap->a_vp);
311 struct vattr *vap = ap->a_vap;
312
313 PTYFS_ITIMES(ptyfs, NULL, NULL, NULL);
314
315 /* start by zeroing out the attributes */
316 VATTR_NULL(vap);
317
318 /* next do all the common fields */
319 vap->va_type = ap->a_vp->v_type;
320 vap->va_fsid = ap->a_vp->v_mount->mnt_stat.f_fsidx.__fsid_val[0];
321 vap->va_fileid = ptyfs->ptyfs_fileno;
322 vap->va_gen = 0;
323 vap->va_flags = 0;
324 vap->va_nlink = 1;
325 vap->va_blocksize = PAGE_SIZE;
326
327 vap->va_atime = ptyfs->ptyfs_atime;
328 vap->va_mtime = ptyfs->ptyfs_mtime;
329 vap->va_ctime = ptyfs->ptyfs_ctime;
330 vap->va_birthtime = ptyfs->ptyfs_birthtime;
331 vap->va_mode = ptyfs->ptyfs_mode;
332 vap->va_flags = ptyfs->ptyfs_flags;
333 vap->va_uid = ptyfs->ptyfs_uid;
334 vap->va_gid = ptyfs->ptyfs_gid;
335
336 switch (ptyfs->ptyfs_type) {
337 case PTYFSpts:
338 case PTYFSptc:
339 if (pty_isfree(ptyfs->ptyfs_pty, 1))
340 return ENOENT;
341 vap->va_bytes = vap->va_size = 0;
342 vap->va_rdev = ap->a_vp->v_rdev;
343 break;
344 case PTYFSroot:
345 vap->va_rdev = 0;
346 vap->va_bytes = vap->va_size = DEV_BSIZE;
347 break;
348
349 default:
350 return EOPNOTSUPP;
351 }
352
353 return 0;
354 }
355
356 /*ARGSUSED*/
357 int
358 ptyfs_setattr(void *v)
359 {
360 struct vop_setattr_args /* {
361 struct vnodeop_desc *a_desc;
362 struct vnode *a_vp;
363 struct vattr *a_vap;
364 struct ucred *a_cred;
365 struct proc *a_p;
366 } */ *ap = v;
367 struct vnode *vp = ap->a_vp;
368 struct ptyfsnode *ptyfs = VTOPTYFS(vp);
369 struct vattr *vap = ap->a_vap;
370 struct ucred *cred = ap->a_cred;
371 struct proc *p = ap->a_p;
372 int error;
373
374 if (vap->va_size != VNOVAL) {
375 switch (ptyfs->ptyfs_type) {
376 case PTYFSroot:
377 return EISDIR;
378 case PTYFSpts:
379 case PTYFSptc:
380 break;
381 default:
382 return EINVAL;
383 }
384 }
385
386 if (vap->va_flags != VNOVAL) {
387 if (vp->v_mount->mnt_flag & MNT_RDONLY)
388 return EROFS;
389 if (cred->cr_uid != ptyfs->ptyfs_uid &&
390 (error = suser(cred, &p->p_acflag)) != 0)
391 return error;
392 if (cred->cr_uid == 0) {
393 if ((ptyfs->ptyfs_flags & (SF_IMMUTABLE | SF_APPEND)) &&
394 securelevel > 0)
395 return EPERM;
396 /* Snapshot flag cannot be set or cleared */
397 if ((vap->va_flags & SF_SNAPSHOT) !=
398 (ptyfs->ptyfs_flags & SF_SNAPSHOT))
399 return EPERM;
400 ptyfs->ptyfs_flags = vap->va_flags;
401 } else {
402 if ((ptyfs->ptyfs_flags & (SF_IMMUTABLE | SF_APPEND)) ||
403 (vap->va_flags & UF_SETTABLE) != vap->va_flags)
404 return EPERM;
405 if ((ptyfs->ptyfs_flags & SF_SETTABLE) !=
406 (vap->va_flags & SF_SETTABLE))
407 return EPERM;
408 ptyfs->ptyfs_flags &= SF_SETTABLE;
409 ptyfs->ptyfs_flags |= (vap->va_flags & UF_SETTABLE);
410 }
411 ptyfs->ptyfs_flag |= PTYFS_CHANGE;
412 if (vap->va_flags & (IMMUTABLE | APPEND))
413 return 0;
414 }
415 if (ptyfs->ptyfs_flags & (IMMUTABLE | APPEND))
416 return EPERM;
417 /*
418 * Go through the fields and update iff not VNOVAL.
419 */
420 if (vap->va_uid != (uid_t)VNOVAL || vap->va_gid != (gid_t)VNOVAL) {
421 if (vp->v_mount->mnt_flag & MNT_RDONLY)
422 return EROFS;
423 if (ptyfs->ptyfs_type == PTYFSroot)
424 return EPERM;
425 error = ptyfs_chown(vp, vap->va_uid, vap->va_gid, cred, p);
426 if (error)
427 return error;
428 }
429
430 if (vap->va_atime.tv_sec != VNOVAL || vap->va_mtime.tv_sec != VNOVAL ||
431 vap->va_birthtime.tv_sec != VNOVAL) {
432 if (vp->v_mount->mnt_flag & MNT_RDONLY)
433 return EROFS;
434 if ((ptyfs->ptyfs_flags & SF_SNAPSHOT) != 0)
435 return EPERM;
436 if (cred->cr_uid != ptyfs->ptyfs_uid &&
437 (error = suser(cred, &p->p_acflag)) &&
438 ((vap->va_vaflags & VA_UTIMES_NULL) == 0 ||
439 (error = VOP_ACCESS(vp, VWRITE, cred, p)) != 0))
440 return (error);
441 if (vap->va_atime.tv_sec != VNOVAL)
442 if (!(vp->v_mount->mnt_flag & MNT_NOATIME))
443 ptyfs->ptyfs_flag |= PTYFS_ACCESS;
444 if (vap->va_mtime.tv_sec != VNOVAL)
445 ptyfs->ptyfs_flag |= PTYFS_CHANGE | PTYFS_MODIFY;
446 if (vap->va_birthtime.tv_sec != VNOVAL)
447 ptyfs->ptyfs_birthtime = vap->va_birthtime;
448 ptyfs->ptyfs_flag |= PTYFS_CHANGE;
449 error = VOP_UPDATE(vp, &vap->va_atime, &vap->va_mtime, 0);
450 if (error)
451 return error;
452 }
453 if (vap->va_mode != (mode_t)VNOVAL) {
454 if (vp->v_mount->mnt_flag & MNT_RDONLY)
455 return EROFS;
456 if (ptyfs->ptyfs_type == PTYFSroot)
457 return EPERM;
458 if ((ptyfs->ptyfs_flags & SF_SNAPSHOT) != 0 &&
459 (vap->va_mode &
460 (S_IXUSR|S_IWUSR|S_IXGRP|S_IWGRP|S_IXOTH|S_IWOTH)))
461 return EPERM;
462 error = ptyfs_chmod(vp, vap->va_mode, cred, p);
463 if (error)
464 return error;
465 }
466 VN_KNOTE(vp, NOTE_ATTRIB);
467 return 0;
468 }
469
470 /*
471 * Change the mode on a file.
472 * Inode must be locked before calling.
473 */
474 static int
475 ptyfs_chmod(struct vnode *vp, mode_t mode, struct ucred *cred, struct proc *p)
476 {
477 struct ptyfsnode *ptyfs = VTOPTYFS(vp);
478 int error;
479
480 if (cred->cr_uid != ptyfs->ptyfs_uid &&
481 (error = suser(cred, &p->p_acflag)) != 0)
482 return error;
483 ptyfs->ptyfs_mode &= ~ALLPERMS;
484 ptyfs->ptyfs_mode |= (mode & ALLPERMS);
485 return 0;
486 }
487
488 /*
489 * Perform chown operation on inode ip;
490 * inode must be locked prior to call.
491 */
492 static int
493 ptyfs_chown(struct vnode *vp, uid_t uid, gid_t gid, struct ucred *cred,
494 struct proc *p)
495 {
496 struct ptyfsnode *ptyfs = VTOPTYFS(vp);
497 int error;
498
499 if (uid == (uid_t)VNOVAL)
500 uid = ptyfs->ptyfs_uid;
501 if (gid == (gid_t)VNOVAL)
502 gid = ptyfs->ptyfs_gid;
503 /*
504 * If we don't own the file, are trying to change the owner
505 * of the file, or are not a member of the target group,
506 * the caller's credentials must imply super-user privilege
507 * or the call fails.
508 */
509 if ((cred->cr_uid != ptyfs->ptyfs_uid || uid != ptyfs->ptyfs_uid ||
510 (gid != ptyfs->ptyfs_gid &&
511 !(cred->cr_gid == gid || groupmember((gid_t)gid, cred)))) &&
512 ((error = suser(cred, &p->p_acflag)) != 0))
513 return error;
514
515 ptyfs->ptyfs_gid = gid;
516 ptyfs->ptyfs_uid = uid;
517 return 0;
518 }
519
520 /*
521 * implement access checking.
522 *
523 * actually, the check for super-user is slightly
524 * broken since it will allow read access to write-only
525 * objects. this doesn't cause any particular trouble
526 * but does mean that the i/o entry points need to check
527 * that the operation really does make sense.
528 */
529 int
530 ptyfs_access(void *v)
531 {
532 struct vop_access_args /* {
533 struct vnode *a_vp;
534 int a_mode;
535 struct ucred *a_cred;
536 struct proc *a_p;
537 } */ *ap = v;
538 struct vattr va;
539 int error;
540
541 if ((error = VOP_GETATTR(ap->a_vp, &va, ap->a_cred, ap->a_p)) != 0)
542 return error;
543
544 return vaccess(va.va_type, va.va_mode,
545 va.va_uid, va.va_gid, ap->a_mode, ap->a_cred);
546 }
547
548 /*
549 * lookup. this is incredibly complicated in the
550 * general case, however for most pseudo-filesystems
551 * very little needs to be done.
552 *
553 * Locking isn't hard here, just poorly documented.
554 *
555 * If we're looking up ".", just vref the parent & return it.
556 *
557 * If we're looking up "..", unlock the parent, and lock "..". If everything
558 * went ok, and we're on the last component and the caller requested the
559 * parent locked, try to re-lock the parent. We do this to prevent lock
560 * races.
561 *
562 * For anything else, get the needed node. Then unlock the parent if not
563 * the last component or not LOCKPARENT (i.e. if we wouldn't re-lock the
564 * parent in the .. case).
565 *
566 * We try to exit with the parent locked in error cases.
567 */
568 int
569 ptyfs_lookup(void *v)
570 {
571 struct vop_lookup_args /* {
572 struct vnode * a_dvp;
573 struct vnode ** a_vpp;
574 struct componentname * a_cnp;
575 } */ *ap = v;
576 struct componentname *cnp = ap->a_cnp;
577 struct vnode **vpp = ap->a_vpp;
578 struct vnode *dvp = ap->a_dvp;
579 const char *pname = cnp->cn_nameptr;
580 struct ptyfsnode *ptyfs;
581 int pty, error, wantpunlock;
582
583 *vpp = NULL;
584 cnp->cn_flags &= ~PDIRUNLOCK;
585
586 if (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME)
587 return EROFS;
588
589 if (cnp->cn_namelen == 1 && *pname == '.') {
590 *vpp = dvp;
591 VREF(dvp);
592 return 0;
593 }
594
595 wantpunlock = ~cnp->cn_flags & (LOCKPARENT | ISLASTCN);
596 ptyfs = VTOPTYFS(dvp);
597 switch (ptyfs->ptyfs_type) {
598 case PTYFSroot:
599 /*
600 * Shouldn't get here with .. in the root node.
601 */
602 if (cnp->cn_flags & ISDOTDOT)
603 return EIO;
604
605 pty = atoi(pname, cnp->cn_namelen);
606
607 if (pty < 0 || pty >= npty || pty_isfree(pty, 1))
608 break;
609
610 error = ptyfs_allocvp(dvp->v_mount, vpp, PTYFSpts, pty,
611 curproc);
612 if (error == 0 && wantpunlock) {
613 VOP_UNLOCK(dvp, 0);
614 cnp->cn_flags |= PDIRUNLOCK;
615 }
616 return error;
617
618 default:
619 return ENOTDIR;
620 }
621
622 return cnp->cn_nameiop == LOOKUP ? ENOENT : EROFS;
623 }
624
625 /*
626 * readdir returns directory entries from ptyfsnode (vp).
627 *
628 * the strategy here with ptyfs is to generate a single
629 * directory entry at a time (struct dirent) and then
630 * copy that out to userland using uiomove. a more efficent
631 * though more complex implementation, would try to minimize
632 * the number of calls to uiomove(). for ptyfs, this is
633 * hardly worth the added code complexity.
634 *
635 * this should just be done through read()
636 */
637 int
638 ptyfs_readdir(void *v)
639 {
640 struct vop_readdir_args /* {
641 struct vnode *a_vp;
642 struct uio *a_uio;
643 struct ucred *a_cred;
644 int *a_eofflag;
645 off_t **a_cookies;
646 int *a_ncookies;
647 } */ *ap = v;
648 struct uio *uio = ap->a_uio;
649 struct dirent d;
650 struct ptyfsnode *ptyfs;
651 off_t i;
652 int error;
653 off_t *cookies = NULL;
654 int ncookies;
655 struct vnode *vp;
656 int nc = 0;
657
658 vp = ap->a_vp;
659 ptyfs = VTOPTYFS(vp);
660
661 if (uio->uio_resid < UIO_MX)
662 return EINVAL;
663 if (uio->uio_offset < 0)
664 return EINVAL;
665
666 error = 0;
667 i = uio->uio_offset;
668 (void)memset(&d, 0, sizeof(d));
669 d.d_reclen = UIO_MX;
670 ncookies = uio->uio_resid / UIO_MX;
671
672 switch (ptyfs->ptyfs_type) {
673 case PTYFSroot: /* root */
674
675 if (i >= npty)
676 return 0;
677
678 if (ap->a_ncookies) {
679 ncookies = min(ncookies, (npty + 2 - i));
680 cookies = malloc(ncookies * sizeof (off_t),
681 M_TEMP, M_WAITOK);
682 *ap->a_cookies = cookies;
683 }
684
685 for (; i < 2; i++) {
686 switch (i) {
687 case 0: /* `.' */
688 case 1: /* `..' */
689 d.d_fileno = PTYFS_FILENO(0, PTYFSroot);
690 d.d_namlen = i + 1;
691 (void)memcpy(d.d_name, "..", d.d_namlen);
692 d.d_name[i + 1] = '\0';
693 d.d_type = DT_DIR;
694 break;
695 }
696 if ((error = uiomove(&d, UIO_MX, uio)) != 0)
697 break;
698 if (cookies)
699 *cookies++ = i + 1;
700 nc++;
701 }
702 if (error) {
703 ncookies = nc;
704 break;
705 }
706 for (; uio->uio_resid >= UIO_MX && i < npty; i++) {
707 /* check for used ptys */
708 if (pty_isfree(i - 2, 1))
709 continue;
710
711 d.d_fileno = PTYFS_FILENO(i - 2, PTYFSpts);
712 d.d_namlen = snprintf(d.d_name, sizeof(d.d_name),
713 "%lld", (long long)(i - 2));
714 d.d_type = DT_CHR;
715 if ((error = uiomove(&d, UIO_MX, uio)) != 0)
716 break;
717 if (cookies)
718 *cookies++ = i + 1;
719 nc++;
720 }
721 ncookies = nc;
722 break;
723
724 default:
725 error = ENOTDIR;
726 break;
727 }
728
729 if (ap->a_ncookies) {
730 if (error) {
731 if (cookies)
732 free(*ap->a_cookies, M_TEMP);
733 *ap->a_ncookies = 0;
734 *ap->a_cookies = NULL;
735 } else
736 *ap->a_ncookies = ncookies;
737 }
738 uio->uio_offset = i;
739 return error;
740 }
741
742 int
743 ptyfs_open(void *v)
744 {
745 struct vop_open_args /* {
746 struct vnode *a_vp;
747 int a_mode;
748 struct ucred *a_cred;
749 struct proc *a_p;
750 } */ *ap = v;
751 struct vnode *vp = ap->a_vp;
752 struct ptyfsnode *ptyfs = VTOPTYFS(vp);
753
754 ptyfs->ptyfs_flag |= PTYFS_CHANGE|PTYFS_ACCESS;
755 switch (ptyfs->ptyfs_type) {
756 case PTYFSpts:
757 case PTYFSptc:
758 return spec_open(v);
759 case PTYFSroot:
760 return 0;
761 default:
762 return EINVAL;
763 }
764 }
765
766 int
767 ptyfs_close(void *v)
768 {
769 struct vop_close_args /* {
770 struct vnode *a_vp;
771 int a_fflag;
772 struct ucred *a_cred;
773 struct proc *a_p;
774 } */ *ap = v;
775 struct vnode *vp = ap->a_vp;
776 struct ptyfsnode *ptyfs = VTOPTYFS(vp);
777
778 simple_lock(&vp->v_interlock);
779 if (vp->v_usecount > 1)
780 PTYFS_ITIMES(ptyfs, NULL, NULL, NULL);
781 simple_unlock(&vp->v_interlock);
782
783 switch (ptyfs->ptyfs_type) {
784 case PTYFSpts:
785 case PTYFSptc:
786 return spec_close(v);
787 case PTYFSroot:
788 return 0;
789 default:
790 return EINVAL;
791 }
792 }
793
794 int
795 ptyfs_read(void *v)
796 {
797 struct vop_read_args /* {
798 struct vnode *a_vp;
799 struct uio *a_uio;
800 int a_ioflag;
801 struct ucred *a_cred;
802 } */ *ap = v;
803 struct vnode *vp = ap->a_vp;
804 struct ptyfsnode *ptyfs = VTOPTYFS(vp);
805 int error;
806
807 ptyfs->ptyfs_flag |= PTYFS_ACCESS;
808 switch (ptyfs->ptyfs_type) {
809 case PTYFSpts:
810 VOP_UNLOCK(vp, 0);
811 error = (*pts_cdevsw.d_read)(vp->v_rdev, ap->a_uio,
812 ap->a_ioflag);
813 vn_lock(vp, LK_RETRY|LK_EXCLUSIVE);
814 return error;
815 case PTYFSptc:
816 VOP_UNLOCK(vp, 0);
817 error = (*ptc_cdevsw.d_read)(vp->v_rdev, ap->a_uio,
818 ap->a_ioflag);
819 vn_lock(vp, LK_RETRY|LK_EXCLUSIVE);
820 return error;
821 default:
822 return EOPNOTSUPP;
823 }
824 }
825
826 int
827 ptyfs_write(void *v)
828 {
829 struct vop_write_args /* {
830 struct vnode *a_vp;
831 struct uio *a_uio;
832 int a_ioflag;
833 struct ucred *a_cred;
834 } */ *ap = v;
835 int error;
836 struct vnode *vp = ap->a_vp;
837 struct ptyfsnode *ptyfs = VTOPTYFS(vp);
838
839 ptyfs->ptyfs_flag |= PTYFS_MODIFY;
840 switch (ptyfs->ptyfs_type) {
841 case PTYFSpts:
842 VOP_UNLOCK(vp, 0);
843 error = (*pts_cdevsw.d_write)(vp->v_rdev, ap->a_uio,
844 ap->a_ioflag);
845 vn_lock(vp, LK_RETRY|LK_EXCLUSIVE);
846 return error;
847 case PTYFSptc:
848 VOP_UNLOCK(vp, 0);
849 error = (*ptc_cdevsw.d_write)(vp->v_rdev, ap->a_uio,
850 ap->a_ioflag);
851 vn_lock(vp, LK_RETRY|LK_EXCLUSIVE);
852 return error;
853 default:
854 return EOPNOTSUPP;
855 }
856 }
857
858 int
859 ptyfs_ioctl(void *v)
860 {
861 struct vop_ioctl_args /* {
862 struct vnode *a_vp;
863 u_long a_command;
864 void *a_data;
865 int a_fflag;
866 struct ucred *a_cred;
867 struct proc *a_p;
868 } */ *ap = v;
869 struct vnode *vp = ap->a_vp;
870 struct ptyfsnode *ptyfs = VTOPTYFS(vp);
871
872 switch (ptyfs->ptyfs_type) {
873 case PTYFSpts:
874 return (*pts_cdevsw.d_ioctl)(vp->v_rdev, ap->a_command,
875 ap->a_data, ap->a_fflag, ap->a_p);
876 case PTYFSptc:
877 return (*ptc_cdevsw.d_ioctl)(vp->v_rdev, ap->a_command,
878 ap->a_data, ap->a_fflag, ap->a_p);
879 default:
880 return EOPNOTSUPP;
881 }
882 }
883
884 int
885 ptyfs_poll(void *v)
886 {
887 struct vop_poll_args /* {
888 struct vnode *a_vp;
889 int a_events;
890 struct proc *a_p;
891 } */ *ap = v;
892 struct vnode *vp = ap->a_vp;
893 struct ptyfsnode *ptyfs = VTOPTYFS(vp);
894
895 switch (ptyfs->ptyfs_type) {
896 case PTYFSpts:
897 return (*pts_cdevsw.d_poll)(vp->v_rdev, ap->a_events, ap->a_p);
898 case PTYFSptc:
899 return (*ptc_cdevsw.d_poll)(vp->v_rdev, ap->a_events, ap->a_p);
900 default:
901 return genfs_poll(v);
902 }
903 }
904
905 int
906 ptyfs_kqfilter(void *v)
907 {
908 struct vop_kqfilter_args /* {
909 struct vnode *a_vp;
910 struct knote *a_kn;
911 } */ *ap = v;
912 struct vnode *vp = ap->a_vp;
913 struct ptyfsnode *ptyfs = VTOPTYFS(vp);
914
915 switch (ptyfs->ptyfs_type) {
916 case PTYFSpts:
917 return (*pts_cdevsw.d_kqfilter)(vp->v_rdev, ap->a_kn);
918 case PTYFSptc:
919 return (*ptc_cdevsw.d_kqfilter)(vp->v_rdev, ap->a_kn);
920 default:
921 return genfs_kqfilter(v);
922 }
923 }
924
925 int
926 ptyfs_update(v)
927 void *v;
928 {
929 struct vop_update_args /* {
930 struct vnode *a_vp;
931 struct timespec *a_access;
932 struct timespec *a_modify;
933 int a_flags;
934 } */ *ap = v;
935 struct ptyfsnode *ptyfs = VTOPTYFS(ap->a_vp);
936
937 if (ap->a_vp->v_mount->mnt_flag & MNT_RDONLY)
938 return 0;
939
940 PTYFS_ITIMES(ptyfs, ap->a_access, ap->a_modify, NULL);
941 return 0;
942 }
943
944 void
945 ptyfs_itimes(struct ptyfsnode *ptyfs, const struct timespec *acc,
946 const struct timespec *mod, const struct timespec *cre)
947 {
948 struct timespec *ts = NULL, tsb;
949
950 KASSERT(ptyfs->ptyfs_flag & (PTYFS_ACCESS|PTYFS_CHANGE|PTYFS_MODIFY));
951 if (ptyfs->ptyfs_flag & (PTYFS_ACCESS|PTYFS_MODIFY)) {
952 if (acc == NULL)
953 acc = ts == NULL ? (ts = nanotime(&tsb)) : ts;
954 ptyfs->ptyfs_atime = *acc;
955 }
956 if (ptyfs->ptyfs_flag & PTYFS_MODIFY) {
957 if (mod == NULL)
958 mod = ts == NULL ? (ts = nanotime(&tsb)) : ts;
959 ptyfs->ptyfs_mtime = *mod;
960 }
961 if (ptyfs->ptyfs_flag & PTYFS_CHANGE) {
962 if (cre == NULL)
963 cre = ts == NULL ? (ts = nanotime(&tsb)) : ts;
964 ptyfs->ptyfs_ctime = *cre;
965 }
966 ptyfs->ptyfs_flag &= ~(PTYFS_ACCESS|PTYFS_CHANGE|PTYFS_MODIFY);
967 }
968
969 /*
970 * convert decimal ascii to int
971 */
972 static int
973 atoi(const char *b, size_t len)
974 {
975 int p = 0;
976
977 while (len--) {
978 char c = *b++;
979 if (c < '0' || c > '9')
980 return -1;
981 p = 10 * p + (c - '0');
982 }
983
984 return p;
985 }
986