fdesc_vnops.c revision 1.51 1 /* $NetBSD: fdesc_vnops.c,v 1.51 1999/08/14 02:17:17 christos Exp $ */
2
3 /*
4 * Copyright (c) 1992, 1993
5 * The Regents of the University of California. All rights reserved.
6 *
7 * This code is derived from software donated 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. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the University of
21 * California, Berkeley and its contributors.
22 * 4. Neither the name of the University nor the names of its contributors
23 * may be used to endorse or promote products derived from this software
24 * without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 * SUCH DAMAGE.
37 *
38 * @(#)fdesc_vnops.c 8.17 (Berkeley) 5/22/95
39 *
40 * #Id: fdesc_vnops.c,v 1.12 1993/04/06 16:17:17 jsp Exp #
41 */
42
43 /*
44 * /dev/fd Filesystem
45 */
46
47 #include <sys/param.h>
48 #include <sys/systm.h>
49 #include <sys/types.h>
50 #include <sys/time.h>
51 #include <sys/proc.h>
52 #include <sys/kernel.h> /* boottime */
53 #include <sys/resourcevar.h>
54 #include <sys/socketvar.h>
55 #include <sys/filedesc.h>
56 #include <sys/vnode.h>
57 #include <sys/malloc.h>
58 #include <sys/conf.h>
59 #include <sys/file.h>
60 #include <sys/stat.h>
61 #include <sys/mount.h>
62 #include <sys/namei.h>
63 #include <sys/buf.h>
64 #include <sys/dirent.h>
65 #include <sys/tty.h>
66
67 #include <miscfs/fdesc/fdesc.h>
68 #include <miscfs/genfs/genfs.h>
69
70 #define cttyvp(p) ((p)->p_flag & P_CONTROLT ? (p)->p_session->s_ttyvp : NULL)
71
72 #define FDL_WANT 0x01
73 #define FDL_LOCKED 0x02
74 static int fdcache_lock;
75
76 dev_t devctty;
77
78 #if (FD_STDIN != FD_STDOUT-1) || (FD_STDOUT != FD_STDERR-1)
79 FD_STDIN, FD_STDOUT, FD_STDERR must be a sequence n, n+1, n+2
80 #endif
81
82 #define NFDCACHE 4
83
84 #define FD_NHASH(ix) \
85 (&fdhashtbl[(ix) & fdhash])
86 LIST_HEAD(fdhashhead, fdescnode) *fdhashtbl;
87 u_long fdhash;
88
89 int fdesc_lookup __P((void *));
90 #define fdesc_create genfs_eopnotsupp_rele
91 #define fdesc_mknod genfs_eopnotsupp_rele
92 int fdesc_open __P((void *));
93 #define fdesc_close genfs_nullop
94 #define fdesc_access genfs_nullop
95 int fdesc_getattr __P((void *));
96 int fdesc_setattr __P((void *));
97 int fdesc_read __P((void *));
98 int fdesc_write __P((void *));
99 int fdesc_ioctl __P((void *));
100 int fdesc_poll __P((void *));
101 #define fdesc_mmap genfs_eopnotsupp
102 #define fdesc_fcntl genfs_fcntl
103 #define fdesc_fsync genfs_nullop
104 #define fdesc_seek genfs_seek
105 #define fdesc_remove genfs_eopnotsupp_rele
106 int fdesc_link __P((void *));
107 #define fdesc_rename genfs_eopnotsupp_rele
108 #define fdesc_mkdir genfs_eopnotsupp_rele
109 #define fdesc_rmdir genfs_eopnotsupp_rele
110 int fdesc_symlink __P((void *));
111 int fdesc_readdir __P((void *));
112 int fdesc_readlink __P((void *));
113 #define fdesc_abortop genfs_abortop
114 int fdesc_inactive __P((void *));
115 int fdesc_reclaim __P((void *));
116 #define fdesc_lock genfs_lock
117 #define fdesc_unlock genfs_unlock
118 #define fdesc_bmap genfs_badop
119 #define fdesc_strategy genfs_badop
120 int fdesc_print __P((void *));
121 int fdesc_pathconf __P((void *));
122 #define fdesc_islocked genfs_islocked
123 #define fdesc_advlock genfs_einval
124 #define fdesc_blkatoff genfs_eopnotsupp
125 #define fdesc_valloc genfs_eopnotsupp
126 #define fdesc_vfree genfs_nullop
127 #define fdesc_truncate genfs_eopnotsupp
128 #define fdesc_update genfs_nullop
129 #define fdesc_bwrite genfs_eopnotsupp
130 #define fdesc_revoke genfs_revoke
131
132 static int fdesc_attr __P((int, struct vattr *, struct ucred *, struct proc *));
133
134 int (**fdesc_vnodeop_p) __P((void *));
135 struct vnodeopv_entry_desc fdesc_vnodeop_entries[] = {
136 { &vop_default_desc, vn_default_error },
137 { &vop_lookup_desc, fdesc_lookup }, /* lookup */
138 { &vop_create_desc, fdesc_create }, /* create */
139 { &vop_mknod_desc, fdesc_mknod }, /* mknod */
140 { &vop_open_desc, fdesc_open }, /* open */
141 { &vop_close_desc, fdesc_close }, /* close */
142 { &vop_access_desc, fdesc_access }, /* access */
143 { &vop_getattr_desc, fdesc_getattr }, /* getattr */
144 { &vop_setattr_desc, fdesc_setattr }, /* setattr */
145 { &vop_read_desc, fdesc_read }, /* read */
146 { &vop_write_desc, fdesc_write }, /* write */
147 { &vop_ioctl_desc, fdesc_ioctl }, /* ioctl */
148 { &vop_fcntl_desc, fdesc_fcntl }, /* fcntl */
149 { &vop_poll_desc, fdesc_poll }, /* poll */
150 { &vop_revoke_desc, fdesc_revoke }, /* revoke */
151 { &vop_mmap_desc, fdesc_mmap }, /* mmap */
152 { &vop_fsync_desc, fdesc_fsync }, /* fsync */
153 { &vop_seek_desc, fdesc_seek }, /* seek */
154 { &vop_remove_desc, fdesc_remove }, /* remove */
155 { &vop_link_desc, fdesc_link }, /* link */
156 { &vop_rename_desc, fdesc_rename }, /* rename */
157 { &vop_mkdir_desc, fdesc_mkdir }, /* mkdir */
158 { &vop_rmdir_desc, fdesc_rmdir }, /* rmdir */
159 { &vop_symlink_desc, fdesc_symlink }, /* symlink */
160 { &vop_readdir_desc, fdesc_readdir }, /* readdir */
161 { &vop_readlink_desc, fdesc_readlink }, /* readlink */
162 { &vop_abortop_desc, fdesc_abortop }, /* abortop */
163 { &vop_inactive_desc, fdesc_inactive }, /* inactive */
164 { &vop_reclaim_desc, fdesc_reclaim }, /* reclaim */
165 { &vop_lock_desc, fdesc_lock }, /* lock */
166 { &vop_unlock_desc, fdesc_unlock }, /* unlock */
167 { &vop_bmap_desc, fdesc_bmap }, /* bmap */
168 { &vop_strategy_desc, fdesc_strategy }, /* strategy */
169 { &vop_print_desc, fdesc_print }, /* print */
170 { &vop_islocked_desc, fdesc_islocked }, /* islocked */
171 { &vop_pathconf_desc, fdesc_pathconf }, /* pathconf */
172 { &vop_advlock_desc, fdesc_advlock }, /* advlock */
173 { &vop_blkatoff_desc, fdesc_blkatoff }, /* blkatoff */
174 { &vop_valloc_desc, fdesc_valloc }, /* valloc */
175 { &vop_vfree_desc, fdesc_vfree }, /* vfree */
176 { &vop_truncate_desc, fdesc_truncate }, /* truncate */
177 { &vop_update_desc, fdesc_update }, /* update */
178 { &vop_bwrite_desc, fdesc_bwrite }, /* bwrite */
179 { (struct vnodeop_desc*)NULL, (int(*) __P((void *)))NULL }
180 };
181
182 struct vnodeopv_desc fdesc_vnodeop_opv_desc =
183 { &fdesc_vnodeop_p, fdesc_vnodeop_entries };
184
185 /*
186 * Initialise cache headers
187 */
188 void
189 fdesc_init()
190 {
191 int cttymajor;
192
193 /* locate the major number */
194 for (cttymajor = 0; cttymajor < nchrdev; cttymajor++)
195 if (cdevsw[cttymajor].d_open == cttyopen)
196 break;
197 devctty = makedev(cttymajor, 0);
198 fdhashtbl = hashinit(NFDCACHE, M_CACHE, M_NOWAIT, &fdhash);
199 }
200
201 /*
202 * Return a locked vnode of the correct type.
203 */
204 int
205 fdesc_allocvp(ftype, ix, mp, vpp)
206 fdntype ftype;
207 int ix;
208 struct mount *mp;
209 struct vnode **vpp;
210 {
211 struct fdhashhead *fc;
212 struct fdescnode *fd;
213 int error = 0;
214
215 fc = FD_NHASH(ix);
216 loop:
217 for (fd = fc->lh_first; fd != 0; fd = fd->fd_hash.le_next) {
218 if (fd->fd_ix == ix && fd->fd_vnode->v_mount == mp) {
219 if (vget(fd->fd_vnode, LK_EXCLUSIVE))
220 goto loop;
221 *vpp = fd->fd_vnode;
222 return (error);
223 }
224 }
225
226 /*
227 * otherwise lock the array while we call getnewvnode
228 * since that can block.
229 */
230 if (fdcache_lock & FDL_LOCKED) {
231 fdcache_lock |= FDL_WANT;
232 sleep((caddr_t) &fdcache_lock, PINOD);
233 goto loop;
234 }
235 fdcache_lock |= FDL_LOCKED;
236
237 error = getnewvnode(VT_FDESC, mp, fdesc_vnodeop_p, vpp);
238 if (error)
239 goto out;
240 MALLOC(fd, void *, sizeof(struct fdescnode), M_TEMP, M_WAITOK);
241 (*vpp)->v_data = fd;
242 fd->fd_vnode = *vpp;
243 fd->fd_type = ftype;
244 fd->fd_fd = -1;
245 fd->fd_link = 0;
246 fd->fd_ix = ix;
247 VOP_LOCK(*vpp, LK_EXCLUSIVE);
248 LIST_INSERT_HEAD(fc, fd, fd_hash);
249
250 out:;
251 fdcache_lock &= ~FDL_LOCKED;
252
253 if (fdcache_lock & FDL_WANT) {
254 fdcache_lock &= ~FDL_WANT;
255 wakeup((caddr_t) &fdcache_lock);
256 }
257
258 return (error);
259 }
260
261 /*
262 * vp is the current namei directory
263 * ndp is the name to locate in that directory...
264 */
265 int
266 fdesc_lookup(v)
267 void *v;
268 {
269 struct vop_lookup_args /* {
270 struct vnode * a_dvp;
271 struct vnode ** a_vpp;
272 struct componentname * a_cnp;
273 } */ *ap = v;
274 struct vnode **vpp = ap->a_vpp;
275 struct vnode *dvp = ap->a_dvp;
276 struct componentname *cnp = ap->a_cnp;
277 struct proc *p = cnp->cn_proc;
278 const char *pname = cnp->cn_nameptr;
279 int nfiles = p->p_fd->fd_nfiles;
280 unsigned fd = 0;
281 int error;
282 struct vnode *fvp;
283 char *ln;
284
285 if (cnp->cn_namelen == 1 && *pname == '.') {
286 *vpp = dvp;
287 VREF(dvp);
288 return (0);
289 }
290
291 switch (VTOFDESC(dvp)->fd_type) {
292 default:
293 case Flink:
294 case Fdesc:
295 case Fctty:
296 error = ENOTDIR;
297 goto bad;
298
299 case Froot:
300 if (cnp->cn_namelen == 2 && memcmp(pname, "fd", 2) == 0) {
301 error = fdesc_allocvp(Fdevfd, FD_DEVFD, dvp->v_mount, &fvp);
302 if (error)
303 goto bad;
304 *vpp = fvp;
305 fvp->v_type = VDIR;
306 goto good;
307 }
308
309 if (cnp->cn_namelen == 3 && memcmp(pname, "tty", 3) == 0) {
310 struct vnode *ttyvp = cttyvp(p);
311 if (ttyvp == NULL) {
312 error = ENXIO;
313 goto bad;
314 }
315 error = fdesc_allocvp(Fctty, FD_CTTY, dvp->v_mount, &fvp);
316 if (error)
317 goto bad;
318 *vpp = fvp;
319 fvp->v_type = VCHR;
320 goto good;
321 }
322
323 ln = 0;
324 switch (cnp->cn_namelen) {
325 case 5:
326 if (memcmp(pname, "stdin", 5) == 0) {
327 ln = "fd/0";
328 fd = FD_STDIN;
329 }
330 break;
331 case 6:
332 if (memcmp(pname, "stdout", 6) == 0) {
333 ln = "fd/1";
334 fd = FD_STDOUT;
335 } else
336 if (memcmp(pname, "stderr", 6) == 0) {
337 ln = "fd/2";
338 fd = FD_STDERR;
339 }
340 break;
341 }
342
343 if (ln) {
344 error = fdesc_allocvp(Flink, fd, dvp->v_mount, &fvp);
345 if (error)
346 goto bad;
347 VTOFDESC(fvp)->fd_link = ln;
348 *vpp = fvp;
349 fvp->v_type = VLNK;
350 goto good;
351 } else {
352 error = ENOENT;
353 goto bad;
354 }
355
356 /* FALL THROUGH */
357
358 case Fdevfd:
359 if (cnp->cn_namelen == 2 && memcmp(pname, "..", 2) == 0) {
360 VOP_UNLOCK(dvp, 0);
361 cnp->cn_flags |= PDIRUNLOCK;
362 error = fdesc_root(dvp->v_mount, vpp);
363 if (error)
364 goto bad;
365 /*
366 * If we're at the last component and need the
367 * parent locked, undo the unlock above.
368 */
369 if (((~cnp->cn_flags & (ISLASTCN | LOCKPARENT)) == 0) &&
370 ((error = vn_lock(dvp, LK_EXCLUSIVE)) == 0))
371 cnp->cn_flags &= ~PDIRUNLOCK;
372 return (error);
373 }
374
375 fd = 0;
376 while (*pname >= '0' && *pname <= '9') {
377 fd = 10 * fd + *pname++ - '0';
378 if (fd >= nfiles)
379 break;
380 }
381
382 if (*pname != '\0') {
383 error = ENOENT;
384 goto bad;
385 }
386
387 if (fd >= nfiles || p->p_fd->fd_ofiles[fd] == NULL) {
388 error = EBADF;
389 goto bad;
390 }
391
392 error = fdesc_allocvp(Fdesc, FD_DESC+fd, dvp->v_mount, &fvp);
393 if (error)
394 goto bad;
395 VTOFDESC(fvp)->fd_fd = fd;
396 *vpp = fvp;
397 goto good;
398 }
399
400 bad:;
401 *vpp = NULL;
402 return (error);
403
404 good:;
405 /*
406 * As "." was special cased above, we now unlock the parent if we're
407 * suppoed to. We're only supposed to not unlock if this is the
408 * last component, and the caller requested LOCKPARENT. So if either
409 * condition is false, unlock.
410 */
411 if (((~cnp->cn_flags) & (ISLASTCN | LOCKPARENT)) != 0) {
412 VOP_UNLOCK(dvp, 0);
413 cnp->cn_flags |= PDIRUNLOCK;
414 }
415 return (0);
416 }
417
418 int
419 fdesc_open(v)
420 void *v;
421 {
422 struct vop_open_args /* {
423 struct vnode *a_vp;
424 int a_mode;
425 struct ucred *a_cred;
426 struct proc *a_p;
427 } */ *ap = v;
428 struct vnode *vp = ap->a_vp;
429
430 switch (VTOFDESC(vp)->fd_type) {
431 case Fdesc:
432 /*
433 * XXX Kludge: set p->p_dupfd to contain the value of the
434 * the file descriptor being sought for duplication. The error
435 * return ensures that the vnode for this device will be
436 * released by vn_open. Open will detect this special error and
437 * take the actions in dupfdopen. Other callers of vn_open or
438 * VOP_OPEN will simply report the error.
439 */
440 ap->a_p->p_dupfd = VTOFDESC(vp)->fd_fd; /* XXX */
441 return (ENODEV);
442
443 case Fctty:
444 return (cttyopen(devctty, ap->a_mode, 0, ap->a_p));
445 case Froot:
446 case Fdevfd:
447 case Flink:
448 break;
449 }
450
451 return (0);
452 }
453
454 static int
455 fdesc_attr(fd, vap, cred, p)
456 int fd;
457 struct vattr *vap;
458 struct ucred *cred;
459 struct proc *p;
460 {
461 struct filedesc *fdp = p->p_fd;
462 struct file *fp;
463 struct stat stb;
464 int error;
465
466 if (fd >= fdp->fd_nfiles || (fp = fdp->fd_ofiles[fd]) == NULL)
467 return (EBADF);
468
469 switch (fp->f_type) {
470 case DTYPE_VNODE:
471 error = VOP_GETATTR((struct vnode *) fp->f_data, vap, cred, p);
472 if (error == 0 && vap->va_type == VDIR) {
473 /*
474 * directories can cause loops in the namespace,
475 * so turn off the 'x' bits to avoid trouble.
476 */
477 vap->va_mode &= ~(S_IXUSR|S_IXGRP|S_IXOTH);
478 }
479 break;
480
481 case DTYPE_SOCKET:
482 error = soo_stat((struct socket *)fp->f_data, &stb);
483 if (error == 0) {
484 vattr_null(vap);
485 vap->va_type = VSOCK;
486 vap->va_mode = stb.st_mode;
487 vap->va_nlink = stb.st_nlink;
488 vap->va_uid = stb.st_uid;
489 vap->va_gid = stb.st_gid;
490 vap->va_fsid = stb.st_dev;
491 vap->va_fileid = stb.st_ino;
492 vap->va_size = stb.st_size;
493 vap->va_blocksize = stb.st_blksize;
494 vap->va_atime = stb.st_atimespec;
495 vap->va_mtime = stb.st_mtimespec;
496 vap->va_ctime = stb.st_ctimespec;
497 vap->va_gen = stb.st_gen;
498 vap->va_flags = stb.st_flags;
499 vap->va_rdev = stb.st_rdev;
500 vap->va_bytes = stb.st_blocks * stb.st_blksize;
501 }
502 break;
503
504 default:
505 panic("fdesc attr");
506 break;
507 }
508
509 return (error);
510 }
511
512 int
513 fdesc_getattr(v)
514 void *v;
515 {
516 struct vop_getattr_args /* {
517 struct vnode *a_vp;
518 struct vattr *a_vap;
519 struct ucred *a_cred;
520 struct proc *a_p;
521 } */ *ap = v;
522 struct vnode *vp = ap->a_vp;
523 struct vattr *vap = ap->a_vap;
524 unsigned fd;
525 int error = 0;
526
527 switch (VTOFDESC(vp)->fd_type) {
528 case Froot:
529 case Fdevfd:
530 case Flink:
531 case Fctty:
532 VATTR_NULL(vap);
533 vap->va_fileid = VTOFDESC(vp)->fd_ix;
534
535 #define R_ALL (S_IRUSR|S_IRGRP|S_IROTH)
536 #define W_ALL (S_IWUSR|S_IWGRP|S_IWOTH)
537 #define X_ALL (S_IXUSR|S_IXGRP|S_IXOTH)
538
539 switch (VTOFDESC(vp)->fd_type) {
540 case Flink:
541 vap->va_mode = R_ALL|X_ALL;
542 vap->va_type = VLNK;
543 vap->va_rdev = 0;
544 vap->va_nlink = 1;
545 vap->va_size = strlen(VTOFDESC(vp)->fd_link);
546 break;
547
548 case Fctty:
549 vap->va_mode = R_ALL|W_ALL;
550 vap->va_type = VCHR;
551 vap->va_rdev = devctty;
552 vap->va_nlink = 1;
553 vap->va_size = 0;
554 break;
555
556 default:
557 vap->va_mode = R_ALL|X_ALL;
558 vap->va_type = VDIR;
559 vap->va_rdev = 0;
560 vap->va_nlink = 2;
561 vap->va_size = DEV_BSIZE;
562 break;
563 }
564 vap->va_uid = 0;
565 vap->va_gid = 0;
566 vap->va_fsid = vp->v_mount->mnt_stat.f_fsid.val[0];
567 vap->va_blocksize = DEV_BSIZE;
568 vap->va_atime.tv_sec = boottime.tv_sec;
569 vap->va_atime.tv_nsec = 0;
570 vap->va_mtime = vap->va_atime;
571 vap->va_ctime = vap->va_mtime;
572 vap->va_gen = 0;
573 vap->va_flags = 0;
574 vap->va_bytes = 0;
575 break;
576
577 case Fdesc:
578 fd = VTOFDESC(vp)->fd_fd;
579 error = fdesc_attr(fd, vap, ap->a_cred, ap->a_p);
580 break;
581
582 default:
583 panic("fdesc_getattr");
584 break;
585 }
586
587 if (error == 0)
588 vp->v_type = vap->va_type;
589
590 return (error);
591 }
592
593 int
594 fdesc_setattr(v)
595 void *v;
596 {
597 struct vop_setattr_args /* {
598 struct vnode *a_vp;
599 struct vattr *a_vap;
600 struct ucred *a_cred;
601 struct proc *a_p;
602 } */ *ap = v;
603 struct filedesc *fdp = ap->a_p->p_fd;
604 struct file *fp;
605 unsigned fd;
606 int error;
607
608 /*
609 * Can't mess with the root vnode
610 */
611 switch (VTOFDESC(ap->a_vp)->fd_type) {
612 case Fdesc:
613 break;
614
615 case Fctty:
616 return (0);
617
618 default:
619 return (EACCES);
620 }
621
622 fd = VTOFDESC(ap->a_vp)->fd_fd;
623 if (fd >= fdp->fd_nfiles || (fp = fdp->fd_ofiles[fd]) == NULL) {
624 return (EBADF);
625 }
626
627 /*
628 * Can setattr the underlying vnode, but not sockets!
629 */
630 switch (fp->f_type) {
631 case DTYPE_VNODE:
632 error = VOP_SETATTR((struct vnode *) fp->f_data, ap->a_vap, ap->a_cred, ap->a_p);
633 break;
634
635 case DTYPE_SOCKET:
636 error = 0;
637 break;
638
639 default:
640 panic("fdesc setattr");
641 break;
642 }
643
644 return (error);
645 }
646
647 #define UIO_MX 32
648
649 struct fdesc_target {
650 ino_t ft_fileno;
651 u_char ft_type;
652 u_char ft_namlen;
653 char *ft_name;
654 } fdesc_targets[] = {
655 /* NOTE: The name must be less than UIO_MX-16 chars in length */
656 #define N(s) sizeof(s)-1, s
657 { FD_DEVFD, DT_DIR, N("fd") },
658 { FD_STDIN, DT_LNK, N("stdin") },
659 { FD_STDOUT, DT_LNK, N("stdout") },
660 { FD_STDERR, DT_LNK, N("stderr") },
661 { FD_CTTY, DT_UNKNOWN, N("tty") },
662 #undef N
663 };
664 static int nfdesc_targets = sizeof(fdesc_targets) / sizeof(fdesc_targets[0]);
665
666 int
667 fdesc_readdir(v)
668 void *v;
669 {
670 struct vop_readdir_args /* {
671 struct vnode *a_vp;
672 struct uio *a_uio;
673 struct ucred *a_cred;
674 int *a_eofflag;
675 off_t **a_cookies;
676 int *a_ncookies;
677 } */ *ap = v;
678 struct uio *uio = ap->a_uio;
679 struct dirent d;
680 struct filedesc *fdp;
681 int i;
682 int error;
683 off_t *cookies = NULL;
684 int ncookies = 0;
685
686 switch (VTOFDESC(ap->a_vp)->fd_type) {
687 case Fctty:
688 return (0);
689
690 case Fdesc:
691 return (ENOTDIR);
692
693 default:
694 break;
695 }
696
697 fdp = uio->uio_procp->p_fd;
698
699 if (uio->uio_resid < UIO_MX)
700 return (EINVAL);
701 if (uio->uio_offset < 0)
702 return (EINVAL);
703
704 error = 0;
705 i = uio->uio_offset;
706 memset((caddr_t)&d, 0, UIO_MX);
707 d.d_reclen = UIO_MX;
708 if (ap->a_ncookies)
709 ncookies = (uio->uio_resid / UIO_MX);
710
711 if (VTOFDESC(ap->a_vp)->fd_type == Froot) {
712 struct fdesc_target *ft;
713
714 if (i >= nfdesc_targets)
715 return EINVAL;
716
717 if (ap->a_ncookies) {
718 ncookies = min(ncookies, (nfdesc_targets - i));
719 MALLOC(cookies, off_t *, ncookies * sizeof(off_t),
720 M_TEMP, M_WAITOK);
721 *ap->a_cookies = cookies;
722 *ap->a_ncookies = ncookies;
723 }
724
725 for (ft = &fdesc_targets[i];
726 uio->uio_resid >= UIO_MX && i < nfdesc_targets; ft++, i++) {
727 switch (ft->ft_fileno) {
728 case FD_CTTY:
729 if (cttyvp(uio->uio_procp) == NULL)
730 continue;
731 break;
732
733 case FD_STDIN:
734 case FD_STDOUT:
735 case FD_STDERR:
736 if ((ft->ft_fileno - FD_STDIN) >= fdp->fd_nfiles)
737 continue;
738 if (fdp->fd_ofiles[ft->ft_fileno - FD_STDIN] == NULL)
739 continue;
740 break;
741 }
742
743 d.d_fileno = ft->ft_fileno;
744 d.d_namlen = ft->ft_namlen;
745 memcpy(d.d_name, ft->ft_name, ft->ft_namlen + 1);
746 d.d_type = ft->ft_type;
747
748 if ((error = uiomove((caddr_t)&d, UIO_MX, uio)) != 0)
749 break;
750 if (cookies)
751 *cookies++ = i + 1;
752 }
753 } else {
754 if (ap->a_ncookies) {
755 ncookies = min(ncookies, (fdp->fd_nfiles + 2));
756 MALLOC(cookies, off_t *, ncookies * sizeof(off_t), M_TEMP, M_WAITOK);
757 *ap->a_cookies = cookies;
758 *ap->a_ncookies = ncookies;
759 }
760 for (; i - 2 < fdp->fd_nfiles && uio->uio_resid >= UIO_MX;
761 i++) {
762 switch (i) {
763 case 0:
764 case 1:
765 d.d_fileno = FD_ROOT; /* XXX */
766 d.d_namlen = i + 1;
767 memcpy(d.d_name, "..", d.d_namlen);
768 d.d_name[i + 1] = '\0';
769 d.d_type = DT_DIR;
770 break;
771
772 default:
773 if (fdp->fd_ofiles[i - 2] == NULL)
774 continue;
775 d.d_fileno = i - 2 + FD_STDIN;
776 d.d_namlen = sprintf(d.d_name, "%d", i - 2);
777 d.d_type = DT_UNKNOWN;
778 break;
779 }
780
781 if ((error = uiomove((caddr_t)&d, UIO_MX, uio)) != 0)
782 break;
783 if (cookies)
784 *cookies++ = i + 1;
785 }
786 }
787
788 if (ap->a_ncookies && error) {
789 FREE(*ap->a_cookies, M_TEMP);
790 *ap->a_ncookies = 0;
791 *ap->a_cookies = NULL;
792 }
793
794 uio->uio_offset = i;
795 return (error);
796 }
797
798 int
799 fdesc_readlink(v)
800 void *v;
801 {
802 struct vop_readlink_args /* {
803 struct vnode *a_vp;
804 struct uio *a_uio;
805 struct ucred *a_cred;
806 } */ *ap = v;
807 struct vnode *vp = ap->a_vp;
808 int error;
809
810 if (vp->v_type != VLNK)
811 return (EPERM);
812
813 if (VTOFDESC(vp)->fd_type == Flink) {
814 char *ln = VTOFDESC(vp)->fd_link;
815 error = uiomove(ln, strlen(ln), ap->a_uio);
816 } else {
817 error = EOPNOTSUPP;
818 }
819
820 return (error);
821 }
822
823 int
824 fdesc_read(v)
825 void *v;
826 {
827 struct vop_read_args /* {
828 struct vnode *a_vp;
829 struct uio *a_uio;
830 int a_ioflag;
831 struct ucred *a_cred;
832 } */ *ap = v;
833 int error = EOPNOTSUPP;
834 struct vnode *vp = ap->a_vp;
835
836 switch (VTOFDESC(vp)->fd_type) {
837 case Fctty:
838 VOP_UNLOCK(vp, 0);
839 error = cttyread(devctty, ap->a_uio, ap->a_ioflag);
840 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
841 break;
842
843 default:
844 error = EOPNOTSUPP;
845 break;
846 }
847
848 return (error);
849 }
850
851 int
852 fdesc_write(v)
853 void *v;
854 {
855 struct vop_write_args /* {
856 struct vnode *a_vp;
857 struct uio *a_uio;
858 int a_ioflag;
859 struct ucred *a_cred;
860 } */ *ap = v;
861 int error = EOPNOTSUPP;
862 struct vnode *vp = ap->a_vp;
863
864 switch (VTOFDESC(vp)->fd_type) {
865 case Fctty:
866 VOP_UNLOCK(vp, 0);
867 error = cttywrite(devctty, ap->a_uio, ap->a_ioflag);
868 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
869 break;
870
871 default:
872 error = EOPNOTSUPP;
873 break;
874 }
875
876 return (error);
877 }
878
879 int
880 fdesc_ioctl(v)
881 void *v;
882 {
883 struct vop_ioctl_args /* {
884 struct vnode *a_vp;
885 u_long a_command;
886 caddr_t a_data;
887 int a_fflag;
888 struct ucred *a_cred;
889 struct proc *a_p;
890 } */ *ap = v;
891 int error = EOPNOTSUPP;
892
893 switch (VTOFDESC(ap->a_vp)->fd_type) {
894 case Fctty:
895 error = cttyioctl(devctty, ap->a_command, ap->a_data,
896 ap->a_fflag, ap->a_p);
897 break;
898
899 default:
900 error = EOPNOTSUPP;
901 break;
902 }
903
904 return (error);
905 }
906
907 int
908 fdesc_poll(v)
909 void *v;
910 {
911 struct vop_poll_args /* {
912 struct vnode *a_vp;
913 int a_events;
914 struct proc *a_p;
915 } */ *ap = v;
916 int revents;
917
918 switch (VTOFDESC(ap->a_vp)->fd_type) {
919 case Fctty:
920 revents = cttypoll(devctty, ap->a_events, ap->a_p);
921 break;
922
923 default:
924 revents = genfs_poll(v);
925 break;
926 }
927
928 return (revents);
929 }
930
931 int
932 fdesc_inactive(v)
933 void *v;
934 {
935 struct vop_inactive_args /* {
936 struct vnode *a_vp;
937 struct proc *a_p;
938 } */ *ap = v;
939 struct vnode *vp = ap->a_vp;
940
941 /*
942 * Clear out the v_type field to avoid
943 * nasty things happening in vgone().
944 */
945 VOP_UNLOCK(vp, 0);
946 vp->v_type = VNON;
947 return (0);
948 }
949
950 int
951 fdesc_reclaim(v)
952 void *v;
953 {
954 struct vop_reclaim_args /* {
955 struct vnode *a_vp;
956 } */ *ap = v;
957 struct vnode *vp = ap->a_vp;
958 struct fdescnode *fd = VTOFDESC(vp);
959
960 LIST_REMOVE(fd, fd_hash);
961 FREE(vp->v_data, M_TEMP);
962 vp->v_data = 0;
963
964 return (0);
965 }
966
967 /*
968 * Return POSIX pathconf information applicable to special devices.
969 */
970 int
971 fdesc_pathconf(v)
972 void *v;
973 {
974 struct vop_pathconf_args /* {
975 struct vnode *a_vp;
976 int a_name;
977 register_t *a_retval;
978 } */ *ap = v;
979
980 switch (ap->a_name) {
981 case _PC_LINK_MAX:
982 *ap->a_retval = LINK_MAX;
983 return (0);
984 case _PC_MAX_CANON:
985 *ap->a_retval = MAX_CANON;
986 return (0);
987 case _PC_MAX_INPUT:
988 *ap->a_retval = MAX_INPUT;
989 return (0);
990 case _PC_PIPE_BUF:
991 *ap->a_retval = PIPE_BUF;
992 return (0);
993 case _PC_CHOWN_RESTRICTED:
994 *ap->a_retval = 1;
995 return (0);
996 case _PC_VDISABLE:
997 *ap->a_retval = _POSIX_VDISABLE;
998 return (0);
999 case _PC_SYNC_IO:
1000 *ap->a_retval = 1;
1001 return (0);
1002 default:
1003 return (EINVAL);
1004 }
1005 /* NOTREACHED */
1006 }
1007
1008 /*
1009 * Print out the contents of a /dev/fd vnode.
1010 */
1011 /* ARGSUSED */
1012 int
1013 fdesc_print(v)
1014 void *v;
1015 {
1016 printf("tag VT_NON, fdesc vnode\n");
1017 return (0);
1018 }
1019
1020 int
1021 fdesc_link(v)
1022 void *v;
1023 {
1024 struct vop_link_args /* {
1025 struct vnode *a_dvp;
1026 struct vnode *a_vp;
1027 struct componentname *a_cnp;
1028 } */ *ap = v;
1029
1030 VOP_ABORTOP(ap->a_dvp, ap->a_cnp);
1031 vput(ap->a_dvp);
1032 return (EROFS);
1033 }
1034
1035 int
1036 fdesc_symlink(v)
1037 void *v;
1038 {
1039 struct vop_symlink_args /* {
1040 struct vnode *a_dvp;
1041 struct vnode **a_vpp;
1042 struct componentname *a_cnp;
1043 struct vattr *a_vap;
1044 char *a_target;
1045 } */ *ap = v;
1046
1047 VOP_ABORTOP(ap->a_dvp, ap->a_cnp);
1048 vput(ap->a_dvp);
1049 return (EROFS);
1050 }
1051