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