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