fdesc_vnops.c revision 1.84.2.6 1 /* $NetBSD: fdesc_vnops.c,v 1.84.2.6 2007/12/07 17:34:03 yamt 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.84.2.6 2007/12/07 17:34:03 yamt 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 uvm_vnp_setsize(*vpp, 0);
250 VOP_LOCK(*vpp, LK_EXCLUSIVE);
251 LIST_INSERT_HEAD(fc, fd, fd_hash);
252
253 out:;
254 fdcache_lock &= ~FDL_LOCKED;
255
256 if (fdcache_lock & FDL_WANT) {
257 fdcache_lock &= ~FDL_WANT;
258 wakeup(&fdcache_lock);
259 }
260
261 return (error);
262 }
263
264 /*
265 * vp is the current namei directory
266 * ndp is the name to locate in that directory...
267 */
268 int
269 fdesc_lookup(v)
270 void *v;
271 {
272 struct vop_lookup_args /* {
273 struct vnode * a_dvp;
274 struct vnode ** a_vpp;
275 struct componentname * a_cnp;
276 } */ *ap = v;
277 struct vnode **vpp = ap->a_vpp;
278 struct vnode *dvp = ap->a_dvp;
279 struct componentname *cnp = ap->a_cnp;
280 struct lwp *l = cnp->cn_lwp;
281 const char *pname = cnp->cn_nameptr;
282 struct proc *p = l->l_proc;
283 int numfiles = p->p_fd->fd_nfiles;
284 unsigned fd = 0;
285 int error;
286 struct vnode *fvp;
287 const char *ln;
288
289 if (cnp->cn_namelen == 1 && *pname == '.') {
290 *vpp = dvp;
291 VREF(dvp);
292 return (0);
293 }
294
295 switch (VTOFDESC(dvp)->fd_type) {
296 default:
297 case Flink:
298 case Fdesc:
299 case Fctty:
300 error = ENOTDIR;
301 goto bad;
302
303 case Froot:
304 if (cnp->cn_namelen == 2 && memcmp(pname, "fd", 2) == 0) {
305 error = fdesc_allocvp(Fdevfd, FD_DEVFD, dvp->v_mount, &fvp);
306 if (error)
307 goto bad;
308 *vpp = fvp;
309 fvp->v_type = VDIR;
310 goto good;
311 }
312
313 if (cnp->cn_namelen == 3 && memcmp(pname, "tty", 3) == 0) {
314 struct vnode *ttyvp = cttyvp(p);
315 if (ttyvp == NULL) {
316 error = ENXIO;
317 goto bad;
318 }
319 error = fdesc_allocvp(Fctty, FD_CTTY, dvp->v_mount, &fvp);
320 if (error)
321 goto bad;
322 *vpp = fvp;
323 fvp->v_type = VCHR;
324 goto good;
325 }
326
327 ln = 0;
328 switch (cnp->cn_namelen) {
329 case 5:
330 if (memcmp(pname, "stdin", 5) == 0) {
331 ln = "fd/0";
332 fd = FD_STDIN;
333 }
334 break;
335 case 6:
336 if (memcmp(pname, "stdout", 6) == 0) {
337 ln = "fd/1";
338 fd = FD_STDOUT;
339 } else
340 if (memcmp(pname, "stderr", 6) == 0) {
341 ln = "fd/2";
342 fd = FD_STDERR;
343 }
344 break;
345 }
346
347 if (ln) {
348 error = fdesc_allocvp(Flink, fd, dvp->v_mount, &fvp);
349 if (error)
350 goto bad;
351 /* XXXUNCONST */
352 VTOFDESC(fvp)->fd_link = __UNCONST(ln);
353 *vpp = fvp;
354 fvp->v_type = VLNK;
355 goto good;
356 } else {
357 error = ENOENT;
358 goto bad;
359 }
360
361 /* FALL THROUGH */
362
363 case Fdevfd:
364 if (cnp->cn_namelen == 2 && memcmp(pname, "..", 2) == 0) {
365 VOP_UNLOCK(dvp, 0);
366 error = fdesc_root(dvp->v_mount, vpp);
367 vn_lock(dvp, LK_EXCLUSIVE | LK_RETRY);
368 if (error)
369 goto bad;
370 return (error);
371 }
372
373 fd = 0;
374 while (*pname >= '0' && *pname <= '9') {
375 fd = 10 * fd + *pname++ - '0';
376 if (fd >= numfiles)
377 break;
378 }
379
380 if (*pname != '\0') {
381 error = ENOENT;
382 goto bad;
383 }
384
385 if (fd >= numfiles || p->p_fd->fd_ofiles[fd] == NULL ||
386 FILE_IS_USABLE(p->p_fd->fd_ofiles[fd]) == 0) {
387 error = EBADF;
388 goto bad;
389 }
390
391 error = fdesc_allocvp(Fdesc, FD_DESC+fd, dvp->v_mount, &fvp);
392 if (error)
393 goto bad;
394 VTOFDESC(fvp)->fd_fd = fd;
395 *vpp = fvp;
396 goto good;
397 }
398
399 bad:
400 *vpp = NULL;
401 return (error);
402
403 good:
404 return (0);
405 }
406
407 int
408 fdesc_open(v)
409 void *v;
410 {
411 struct vop_open_args /* {
412 struct vnode *a_vp;
413 int a_mode;
414 kauth_cred_t a_cred;
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, curlwp);
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);
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, curlwp);
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 } */ *ap = v;
604 struct filedesc *fdp = curlwp->l_proc->p_fd;
605 struct file *fp;
606 unsigned fd;
607
608 /*
609 * Can't mess with the root vnode
610 */
611 switch (VTOFDESC(ap->a_vp)->fd_type) {
612 case Fdesc:
613 break;
614
615 case Fctty:
616 return (0);
617
618 default:
619 return (EACCES);
620 }
621
622 fd = VTOFDESC(ap->a_vp)->fd_fd;
623 if ((fp = fd_getfile(fdp, fd)) == NULL)
624 return (EBADF);
625
626 /*
627 * XXX: Can't reasonably set the attr's on any types currently.
628 * On vnode's this will cause truncation and socket/pipes make
629 * no sense.
630 */
631 mutex_exit(&fp->f_lock);
632 return (0);
633 }
634
635
636 struct fdesc_target {
637 ino_t ft_fileno;
638 u_char ft_type;
639 u_char ft_namlen;
640 const char *ft_name;
641 } fdesc_targets[] = {
642 #define N(s) sizeof(s)-1, s
643 { FD_DEVFD, DT_DIR, N("fd") },
644 { FD_STDIN, DT_LNK, N("stdin") },
645 { FD_STDOUT, DT_LNK, N("stdout") },
646 { FD_STDERR, DT_LNK, N("stderr") },
647 { FD_CTTY, DT_UNKNOWN, N("tty") },
648 #undef N
649 #define UIO_MX _DIRENT_RECLEN((struct dirent *)NULL, sizeof("stderr") - 1)
650 };
651 static int nfdesc_targets = sizeof(fdesc_targets) / sizeof(fdesc_targets[0]);
652
653 int
654 fdesc_readdir(v)
655 void *v;
656 {
657 struct vop_readdir_args /* {
658 struct vnode *a_vp;
659 struct uio *a_uio;
660 kauth_cred_t a_cred;
661 int *a_eofflag;
662 off_t **a_cookies;
663 int *a_ncookies;
664 } */ *ap = v;
665 struct uio *uio = ap->a_uio;
666 struct dirent d;
667 struct filedesc *fdp;
668 off_t i;
669 int j;
670 int error;
671 off_t *cookies = NULL;
672 int ncookies;
673
674 switch (VTOFDESC(ap->a_vp)->fd_type) {
675 case Fctty:
676 return 0;
677
678 case Fdesc:
679 return ENOTDIR;
680
681 default:
682 break;
683 }
684
685 fdp = curproc->p_fd;
686
687 if (uio->uio_resid < UIO_MX)
688 return EINVAL;
689 if (uio->uio_offset < 0)
690 return EINVAL;
691
692 error = 0;
693 i = uio->uio_offset;
694 (void)memset(&d, 0, UIO_MX);
695 d.d_reclen = UIO_MX;
696 if (ap->a_ncookies)
697 ncookies = uio->uio_resid / UIO_MX;
698 else
699 ncookies = 0;
700
701 if (VTOFDESC(ap->a_vp)->fd_type == Froot) {
702 struct fdesc_target *ft;
703
704 if (i >= nfdesc_targets)
705 return 0;
706
707 if (ap->a_ncookies) {
708 ncookies = min(ncookies, (nfdesc_targets - i));
709 cookies = malloc(ncookies * sizeof(off_t),
710 M_TEMP, M_WAITOK);
711 *ap->a_cookies = cookies;
712 *ap->a_ncookies = ncookies;
713 }
714
715 for (ft = &fdesc_targets[i]; uio->uio_resid >= UIO_MX &&
716 i < nfdesc_targets; ft++, i++) {
717 switch (ft->ft_fileno) {
718 case FD_CTTY:
719 if (cttyvp(curproc) == NULL)
720 continue;
721 break;
722
723 case FD_STDIN:
724 case FD_STDOUT:
725 case FD_STDERR:
726 if (fdp == NULL)
727 continue;
728 if ((ft->ft_fileno - FD_STDIN) >=
729 fdp->fd_nfiles)
730 continue;
731 if (fdp->fd_ofiles[ft->ft_fileno - FD_STDIN]
732 == NULL
733 || FILE_IS_USABLE(
734 fdp->fd_ofiles[ft->ft_fileno - FD_STDIN])
735 == 0)
736 continue;
737 break;
738 }
739
740 d.d_fileno = ft->ft_fileno;
741 d.d_namlen = ft->ft_namlen;
742 (void)memcpy(d.d_name, ft->ft_name, ft->ft_namlen + 1);
743 d.d_type = ft->ft_type;
744
745 if ((error = uiomove(&d, UIO_MX, uio)) != 0)
746 break;
747 if (cookies)
748 *cookies++ = i + 1;
749 }
750 } else {
751 int nfdp = fdp ? fdp->fd_nfiles : 0;
752 if (ap->a_ncookies) {
753 ncookies = min(ncookies, nfdp + 2);
754 cookies = malloc(ncookies * sizeof(off_t),
755 M_TEMP, M_WAITOK);
756 *ap->a_cookies = cookies;
757 *ap->a_ncookies = ncookies;
758 }
759 for (; i - 2 < nfdp && uio->uio_resid >= UIO_MX; i++) {
760 switch (i) {
761 case 0:
762 case 1:
763 d.d_fileno = FD_ROOT; /* XXX */
764 d.d_namlen = i + 1;
765 (void)memcpy(d.d_name, "..", d.d_namlen);
766 d.d_name[i + 1] = '\0';
767 d.d_type = DT_DIR;
768 break;
769
770 default:
771 KASSERT(fdp != NULL);
772 j = (int)i - 2;
773 if (fdp == NULL || fdp->fd_ofiles[j] == NULL ||
774 FILE_IS_USABLE(fdp->fd_ofiles[j]) == 0)
775 continue;
776 d.d_fileno = j + FD_STDIN;
777 d.d_namlen = sprintf(d.d_name, "%d", j);
778 d.d_type = DT_UNKNOWN;
779 break;
780 }
781
782 if ((error = uiomove(&d, UIO_MX, uio)) != 0)
783 break;
784 if (cookies)
785 *cookies++ = i + 1;
786 }
787 }
788
789 if (ap->a_ncookies && error) {
790 free(*ap->a_cookies, M_TEMP);
791 *ap->a_ncookies = 0;
792 *ap->a_cookies = NULL;
793 }
794
795 uio->uio_offset = i;
796 return error;
797 }
798
799 int
800 fdesc_readlink(v)
801 void *v;
802 {
803 struct vop_readlink_args /* {
804 struct vnode *a_vp;
805 struct uio *a_uio;
806 kauth_cred_t a_cred;
807 } */ *ap = v;
808 struct vnode *vp = ap->a_vp;
809 int error;
810
811 if (vp->v_type != VLNK)
812 return (EPERM);
813
814 if (VTOFDESC(vp)->fd_type == Flink) {
815 char *ln = VTOFDESC(vp)->fd_link;
816 error = uiomove(ln, strlen(ln), ap->a_uio);
817 } else {
818 error = EOPNOTSUPP;
819 }
820
821 return (error);
822 }
823
824 int
825 fdesc_read(v)
826 void *v;
827 {
828 struct vop_read_args /* {
829 struct vnode *a_vp;
830 struct uio *a_uio;
831 int a_ioflag;
832 kauth_cred_t a_cred;
833 } */ *ap = v;
834 int error = EOPNOTSUPP;
835 struct vnode *vp = ap->a_vp;
836
837 switch (VTOFDESC(vp)->fd_type) {
838 case Fctty:
839 VOP_UNLOCK(vp, 0);
840 error = cdev_read(devctty, ap->a_uio, ap->a_ioflag);
841 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
842 break;
843
844 default:
845 error = EOPNOTSUPP;
846 break;
847 }
848
849 return (error);
850 }
851
852 int
853 fdesc_write(v)
854 void *v;
855 {
856 struct vop_write_args /* {
857 struct vnode *a_vp;
858 struct uio *a_uio;
859 int a_ioflag;
860 kauth_cred_t a_cred;
861 } */ *ap = v;
862 int error = EOPNOTSUPP;
863 struct vnode *vp = ap->a_vp;
864
865 switch (VTOFDESC(vp)->fd_type) {
866 case Fctty:
867 VOP_UNLOCK(vp, 0);
868 error = cdev_write(devctty, ap->a_uio, ap->a_ioflag);
869 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
870 break;
871
872 default:
873 error = EOPNOTSUPP;
874 break;
875 }
876
877 return (error);
878 }
879
880 int
881 fdesc_ioctl(v)
882 void *v;
883 {
884 struct vop_ioctl_args /* {
885 struct vnode *a_vp;
886 u_long a_command;
887 void *a_data;
888 int a_fflag;
889 kauth_cred_t a_cred;
890 } */ *ap = v;
891 int error = EOPNOTSUPP;
892
893 switch (VTOFDESC(ap->a_vp)->fd_type) {
894 case Fctty:
895 error = cdev_ioctl(devctty, ap->a_command, ap->a_data,
896 ap->a_fflag, curlwp);
897 break;
898
899 default:
900 error = EOPNOTSUPP;
901 break;
902 }
903
904 return (error);
905 }
906
907 int
908 fdesc_poll(v)
909 void *v;
910 {
911 struct vop_poll_args /* {
912 struct vnode *a_vp;
913 int a_events;
914 } */ *ap = v;
915 int revents;
916
917 switch (VTOFDESC(ap->a_vp)->fd_type) {
918 case Fctty:
919 revents = cdev_poll(devctty, ap->a_events, curlwp);
920 break;
921
922 default:
923 revents = genfs_poll(v);
924 break;
925 }
926
927 return (revents);
928 }
929
930 int
931 fdesc_kqfilter(v)
932 void *v;
933 {
934 struct vop_kqfilter_args /* {
935 struct vnode *a_vp;
936 struct knote *a_kn;
937 } */ *ap = v;
938 int error;
939 struct proc *p;
940 struct lwp *l;
941 struct file *fp;
942
943 switch (VTOFDESC(ap->a_vp)->fd_type) {
944 case Fctty:
945 error = cdev_kqfilter(devctty, ap->a_kn);
946 break;
947
948 case Fdesc:
949 /* just invoke kqfilter for the underlying descriptor */
950 l = curlwp; /* XXX hopefully ok to use curproc here */
951 p = l->l_proc;
952 if ((fp = fd_getfile(p->p_fd, VTOFDESC(ap->a_vp)->fd_fd)) == NULL)
953 return (1);
954
955 FILE_USE(fp);
956 error = (*fp->f_ops->fo_kqfilter)(fp, ap->a_kn);
957 FILE_UNUSE(fp, l);
958 break;
959
960 default:
961 return (genfs_kqfilter(v));
962 }
963
964 return (error);
965 }
966
967 int
968 fdesc_inactive(v)
969 void *v;
970 {
971 struct vop_inactive_args /* {
972 struct vnode *a_vp;
973 } */ *ap = v;
974 struct vnode *vp = ap->a_vp;
975
976 /*
977 * Clear out the v_type field to avoid
978 * nasty things happening in vgone().
979 */
980 VOP_UNLOCK(vp, 0);
981 vp->v_type = VNON;
982 return (0);
983 }
984
985 int
986 fdesc_reclaim(v)
987 void *v;
988 {
989 struct vop_reclaim_args /* {
990 struct vnode *a_vp;
991 } */ *ap = v;
992 struct vnode *vp = ap->a_vp;
993 struct fdescnode *fd = VTOFDESC(vp);
994
995 LIST_REMOVE(fd, fd_hash);
996 FREE(vp->v_data, M_TEMP);
997 vp->v_data = 0;
998
999 return (0);
1000 }
1001
1002 /*
1003 * Return POSIX pathconf information applicable to special devices.
1004 */
1005 int
1006 fdesc_pathconf(v)
1007 void *v;
1008 {
1009 struct vop_pathconf_args /* {
1010 struct vnode *a_vp;
1011 int a_name;
1012 register_t *a_retval;
1013 } */ *ap = v;
1014
1015 switch (ap->a_name) {
1016 case _PC_LINK_MAX:
1017 *ap->a_retval = LINK_MAX;
1018 return (0);
1019 case _PC_MAX_CANON:
1020 *ap->a_retval = MAX_CANON;
1021 return (0);
1022 case _PC_MAX_INPUT:
1023 *ap->a_retval = MAX_INPUT;
1024 return (0);
1025 case _PC_PIPE_BUF:
1026 *ap->a_retval = PIPE_BUF;
1027 return (0);
1028 case _PC_CHOWN_RESTRICTED:
1029 *ap->a_retval = 1;
1030 return (0);
1031 case _PC_VDISABLE:
1032 *ap->a_retval = _POSIX_VDISABLE;
1033 return (0);
1034 case _PC_SYNC_IO:
1035 *ap->a_retval = 1;
1036 return (0);
1037 default:
1038 return (EINVAL);
1039 }
1040 /* NOTREACHED */
1041 }
1042
1043 /*
1044 * Print out the contents of a /dev/fd vnode.
1045 */
1046 /* ARGSUSED */
1047 int
1048 fdesc_print(void *v)
1049 {
1050 printf("tag VT_NON, fdesc vnode\n");
1051 return (0);
1052 }
1053
1054 int
1055 fdesc_link(v)
1056 void *v;
1057 {
1058 struct vop_link_args /* {
1059 struct vnode *a_dvp;
1060 struct vnode *a_vp;
1061 struct componentname *a_cnp;
1062 } */ *ap = v;
1063
1064 VOP_ABORTOP(ap->a_dvp, ap->a_cnp);
1065 vput(ap->a_dvp);
1066 return (EROFS);
1067 }
1068
1069 int
1070 fdesc_symlink(v)
1071 void *v;
1072 {
1073 struct vop_symlink_args /* {
1074 struct vnode *a_dvp;
1075 struct vnode **a_vpp;
1076 struct componentname *a_cnp;
1077 struct vattr *a_vap;
1078 char *a_target;
1079 } */ *ap = v;
1080
1081 VOP_ABORTOP(ap->a_dvp, ap->a_cnp);
1082 vput(ap->a_dvp);
1083 return (EROFS);
1084 }
1085