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