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