fdesc_vnops.c revision 1.120 1 /* $NetBSD: fdesc_vnops.c,v 1.120 2014/07/13 11:23:01 hannken 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.120 2014/07/13 11:23:01 hannken 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 #include <sys/atomic.h>
66
67 #include <miscfs/fdesc/fdesc.h>
68 #include <miscfs/genfs/genfs.h>
69
70 #define cttyvp(p) ((p)->p_lflag & PL_CONTROLT ? (p)->p_session->s_ttyvp : NULL)
71
72 dev_t devctty;
73
74 #if (FD_STDIN != FD_STDOUT-1) || (FD_STDOUT != FD_STDERR-1)
75 FD_STDIN, FD_STDOUT, FD_STDERR must be a sequence n, n+1, n+2
76 #endif
77
78 int fdesc_lookup(void *);
79 #define fdesc_create genfs_eopnotsupp
80 #define fdesc_mknod genfs_eopnotsupp
81 int fdesc_open(void *);
82 #define fdesc_close genfs_nullop
83 #define fdesc_access genfs_nullop
84 int fdesc_getattr(void *);
85 int fdesc_setattr(void *);
86 int fdesc_read(void *);
87 int fdesc_write(void *);
88 int fdesc_ioctl(void *);
89 int fdesc_poll(void *);
90 int fdesc_kqfilter(void *);
91 #define fdesc_mmap genfs_eopnotsupp
92 #define fdesc_fcntl genfs_fcntl
93 #define fdesc_fsync genfs_nullop
94 #define fdesc_seek genfs_seek
95 #define fdesc_remove genfs_eopnotsupp
96 int fdesc_link(void *);
97 #define fdesc_rename genfs_eopnotsupp
98 #define fdesc_mkdir genfs_eopnotsupp
99 #define fdesc_rmdir genfs_eopnotsupp
100 int fdesc_symlink(void *);
101 int fdesc_readdir(void *);
102 int fdesc_readlink(void *);
103 #define fdesc_abortop genfs_abortop
104 int fdesc_inactive(void *);
105 int fdesc_reclaim(void *);
106 #define fdesc_lock genfs_lock
107 #define fdesc_unlock genfs_unlock
108 #define fdesc_bmap genfs_badop
109 #define fdesc_strategy genfs_badop
110 int fdesc_print(void *);
111 int fdesc_pathconf(void *);
112 #define fdesc_islocked genfs_islocked
113 #define fdesc_advlock genfs_einval
114 #define fdesc_bwrite genfs_eopnotsupp
115 #define fdesc_revoke genfs_revoke
116 #define fdesc_putpages genfs_null_putpages
117
118 static int fdesc_attr(int, struct vattr *, kauth_cred_t);
119
120 int (**fdesc_vnodeop_p)(void *);
121 const struct vnodeopv_entry_desc fdesc_vnodeop_entries[] = {
122 { &vop_default_desc, vn_default_error },
123 { &vop_lookup_desc, fdesc_lookup }, /* lookup */
124 { &vop_create_desc, fdesc_create }, /* create */
125 { &vop_mknod_desc, fdesc_mknod }, /* mknod */
126 { &vop_open_desc, fdesc_open }, /* open */
127 { &vop_close_desc, fdesc_close }, /* close */
128 { &vop_access_desc, fdesc_access }, /* access */
129 { &vop_getattr_desc, fdesc_getattr }, /* getattr */
130 { &vop_setattr_desc, fdesc_setattr }, /* setattr */
131 { &vop_read_desc, fdesc_read }, /* read */
132 { &vop_write_desc, fdesc_write }, /* write */
133 { &vop_ioctl_desc, fdesc_ioctl }, /* ioctl */
134 { &vop_fcntl_desc, fdesc_fcntl }, /* fcntl */
135 { &vop_poll_desc, fdesc_poll }, /* poll */
136 { &vop_kqfilter_desc, fdesc_kqfilter }, /* kqfilter */
137 { &vop_revoke_desc, fdesc_revoke }, /* revoke */
138 { &vop_mmap_desc, fdesc_mmap }, /* mmap */
139 { &vop_fsync_desc, fdesc_fsync }, /* fsync */
140 { &vop_seek_desc, fdesc_seek }, /* seek */
141 { &vop_remove_desc, fdesc_remove }, /* remove */
142 { &vop_link_desc, fdesc_link }, /* link */
143 { &vop_rename_desc, fdesc_rename }, /* rename */
144 { &vop_mkdir_desc, fdesc_mkdir }, /* mkdir */
145 { &vop_rmdir_desc, fdesc_rmdir }, /* rmdir */
146 { &vop_symlink_desc, fdesc_symlink }, /* symlink */
147 { &vop_readdir_desc, fdesc_readdir }, /* readdir */
148 { &vop_readlink_desc, fdesc_readlink }, /* readlink */
149 { &vop_abortop_desc, fdesc_abortop }, /* abortop */
150 { &vop_inactive_desc, fdesc_inactive }, /* inactive */
151 { &vop_reclaim_desc, fdesc_reclaim }, /* reclaim */
152 { &vop_lock_desc, fdesc_lock }, /* lock */
153 { &vop_unlock_desc, fdesc_unlock }, /* unlock */
154 { &vop_bmap_desc, fdesc_bmap }, /* bmap */
155 { &vop_strategy_desc, fdesc_strategy }, /* strategy */
156 { &vop_print_desc, fdesc_print }, /* print */
157 { &vop_islocked_desc, fdesc_islocked }, /* islocked */
158 { &vop_pathconf_desc, fdesc_pathconf }, /* pathconf */
159 { &vop_advlock_desc, fdesc_advlock }, /* advlock */
160 { &vop_bwrite_desc, fdesc_bwrite }, /* bwrite */
161 { &vop_putpages_desc, fdesc_putpages }, /* putpages */
162 { NULL, NULL }
163 };
164
165 const struct vnodeopv_desc fdesc_vnodeop_opv_desc =
166 { &fdesc_vnodeop_p, fdesc_vnodeop_entries };
167
168 /*
169 * Initialise cache headers
170 */
171 void
172 fdesc_init(void)
173 {
174 int cttymajor;
175
176 /* locate the major number */
177 cttymajor = devsw_name2chr("ctty", NULL, 0);
178 devctty = makedev(cttymajor, 0);
179 }
180
181 void
182 fdesc_done(void)
183 {
184 }
185
186 /*
187 * vp is the current namei directory
188 * ndp is the name to locate in that directory...
189 */
190 int
191 fdesc_lookup(void *v)
192 {
193 struct vop_lookup_v2_args /* {
194 struct vnode * a_dvp;
195 struct vnode ** a_vpp;
196 struct componentname * a_cnp;
197 } */ *ap = v;
198 struct vnode **vpp = ap->a_vpp;
199 struct vnode *dvp = ap->a_dvp;
200 struct componentname *cnp = ap->a_cnp;
201 struct lwp *l = curlwp;
202 const char *pname = cnp->cn_nameptr;
203 struct proc *p = l->l_proc;
204 unsigned fd = 0;
205 int error, ix = -1;
206 fdtab_t *dt;
207
208 dt = curlwp->l_fd->fd_dt;
209
210 if (cnp->cn_namelen == 1 && *pname == '.') {
211 *vpp = dvp;
212 vref(dvp);
213 return (0);
214 }
215
216 switch (VTOFDESC(dvp)->fd_type) {
217 default:
218 case Flink:
219 case Fdesc:
220 case Fctty:
221 error = ENOTDIR;
222 goto bad;
223
224 case Froot:
225 if (cnp->cn_namelen == 2 && memcmp(pname, "fd", 2) == 0) {
226 ix = FD_DEVFD;
227 goto good;
228 }
229
230 if (cnp->cn_namelen == 3 && memcmp(pname, "tty", 3) == 0) {
231 struct vnode *ttyvp = cttyvp(p);
232 if (ttyvp == NULL) {
233 error = ENXIO;
234 goto bad;
235 }
236 ix = FD_CTTY;
237 goto good;
238 }
239
240 switch (cnp->cn_namelen) {
241 case 5:
242 if (memcmp(pname, "stdin", 5) == 0) {
243 ix = FD_STDIN;
244 goto good;
245 }
246 break;
247 case 6:
248 if (memcmp(pname, "stdout", 6) == 0) {
249 ix = FD_STDOUT;
250 goto good;
251 } else if (memcmp(pname, "stderr", 6) == 0) {
252 ix = FD_STDERR;
253 goto good;
254 }
255 break;
256 }
257
258 error = ENOENT;
259 goto bad;
260
261 case Fdevfd:
262 if (cnp->cn_namelen == 2 && memcmp(pname, "..", 2) == 0) {
263 ix = FD_ROOT;
264 goto good;
265 }
266
267 fd = 0;
268 while (*pname >= '0' && *pname <= '9') {
269 fd = 10 * fd + *pname++ - '0';
270 if (fd >= dt->dt_nfiles)
271 break;
272 }
273
274 if (*pname != '\0') {
275 error = ENOENT;
276 goto bad;
277 }
278
279 if (fd >= dt->dt_nfiles || dt->dt_ff[fd] == NULL ||
280 dt->dt_ff[fd]->ff_file == NULL) {
281 error = EBADF;
282 goto bad;
283 }
284
285 ix = FD_DESC + fd;
286 goto good;
287 }
288
289 bad:
290 *vpp = NULL;
291 return error;
292
293 good:
294 KASSERT(ix != -1);
295 error = vcache_get(dvp->v_mount, &ix, sizeof(ix), vpp);
296 return error;
297 }
298
299 int
300 fdesc_open(void *v)
301 {
302 struct vop_open_args /* {
303 struct vnode *a_vp;
304 int a_mode;
305 kauth_cred_t a_cred;
306 } */ *ap = v;
307 struct vnode *vp = ap->a_vp;
308
309 switch (VTOFDESC(vp)->fd_type) {
310 case Fdesc:
311 /*
312 * XXX Kludge: set dupfd to contain the value of the
313 * the file descriptor being sought for duplication. The error
314 * return ensures that the vnode for this device will be
315 * released by vn_open. Open will detect this special error and
316 * take the actions in dupfdopen. Other callers of vn_open or
317 * VOP_OPEN will simply report the error.
318 */
319 curlwp->l_dupfd = VTOFDESC(vp)->fd_fd; /* XXX */
320 return EDUPFD;
321
322 case Fctty:
323 return cdev_open(devctty, ap->a_mode, 0, curlwp);
324 case Froot:
325 case Fdevfd:
326 case Flink:
327 break;
328 }
329
330 return (0);
331 }
332
333 static int
334 fdesc_attr(int fd, struct vattr *vap, kauth_cred_t cred)
335 {
336 file_t *fp;
337 struct stat stb;
338 int error;
339
340 if ((fp = fd_getfile(fd)) == NULL)
341 return (EBADF);
342
343 switch (fp->f_type) {
344 case DTYPE_VNODE:
345 vn_lock((struct vnode *) fp->f_data, LK_SHARED | LK_RETRY);
346 error = VOP_GETATTR((struct vnode *) fp->f_data, vap, cred);
347 VOP_UNLOCK((struct vnode *) fp->f_data);
348 if (error == 0 && vap->va_type == VDIR) {
349 /*
350 * directories can cause loops in the namespace,
351 * so turn off the 'x' bits to avoid trouble.
352 */
353 vap->va_mode &= ~(S_IXUSR|S_IXGRP|S_IXOTH);
354 }
355 break;
356
357 default:
358 memset(&stb, 0, sizeof(stb));
359 error = (*fp->f_ops->fo_stat)(fp, &stb);
360 if (error)
361 break;
362
363 vattr_null(vap);
364 switch(fp->f_type) {
365 case DTYPE_SOCKET:
366 vap->va_type = VSOCK;
367 break;
368 case DTYPE_PIPE:
369 vap->va_type = VFIFO;
370 break;
371 default:
372 /* use VNON perhaps? */
373 vap->va_type = VBAD;
374 break;
375 }
376 vap->va_mode = stb.st_mode;
377 vap->va_nlink = stb.st_nlink;
378 vap->va_uid = stb.st_uid;
379 vap->va_gid = stb.st_gid;
380 vap->va_fsid = stb.st_dev;
381 vap->va_fileid = stb.st_ino;
382 vap->va_size = stb.st_size;
383 vap->va_blocksize = stb.st_blksize;
384 vap->va_atime = stb.st_atimespec;
385 vap->va_mtime = stb.st_mtimespec;
386 vap->va_ctime = stb.st_ctimespec;
387 vap->va_gen = stb.st_gen;
388 vap->va_flags = stb.st_flags;
389 vap->va_rdev = stb.st_rdev;
390 vap->va_bytes = stb.st_blocks * stb.st_blksize;
391 break;
392 }
393
394 fd_putfile(fd);
395 return (error);
396 }
397
398 int
399 fdesc_getattr(void *v)
400 {
401 struct vop_getattr_args /* {
402 struct vnode *a_vp;
403 struct vattr *a_vap;
404 kauth_cred_t a_cred;
405 struct lwp *a_l;
406 } */ *ap = v;
407 struct vnode *vp = ap->a_vp;
408 struct vattr *vap = ap->a_vap;
409 unsigned fd;
410 int error = 0;
411
412 switch (VTOFDESC(vp)->fd_type) {
413 case Froot:
414 case Fdevfd:
415 case Flink:
416 case Fctty:
417 vattr_null(vap);
418 vap->va_fileid = VTOFDESC(vp)->fd_ix;
419
420 #define R_ALL (S_IRUSR|S_IRGRP|S_IROTH)
421 #define W_ALL (S_IWUSR|S_IWGRP|S_IWOTH)
422 #define X_ALL (S_IXUSR|S_IXGRP|S_IXOTH)
423
424 switch (VTOFDESC(vp)->fd_type) {
425 case Flink:
426 vap->va_mode = R_ALL|X_ALL;
427 vap->va_type = VLNK;
428 vap->va_rdev = 0;
429 vap->va_nlink = 1;
430 vap->va_size = strlen(VTOFDESC(vp)->fd_link);
431 break;
432
433 case Fctty:
434 vap->va_mode = R_ALL|W_ALL;
435 vap->va_type = VCHR;
436 vap->va_rdev = devctty;
437 vap->va_nlink = 1;
438 vap->va_size = 0;
439 break;
440
441 default:
442 vap->va_mode = R_ALL|X_ALL;
443 vap->va_type = VDIR;
444 vap->va_rdev = 0;
445 vap->va_nlink = 2;
446 vap->va_size = DEV_BSIZE;
447 break;
448 }
449 vap->va_uid = 0;
450 vap->va_gid = 0;
451 vap->va_fsid = vp->v_mount->mnt_stat.f_fsidx.__fsid_val[0];
452 vap->va_blocksize = DEV_BSIZE;
453 vap->va_atime.tv_sec = boottime.tv_sec;
454 vap->va_atime.tv_nsec = 0;
455 vap->va_mtime = vap->va_atime;
456 vap->va_ctime = vap->va_mtime;
457 vap->va_gen = 0;
458 vap->va_flags = 0;
459 vap->va_bytes = 0;
460 break;
461
462 case Fdesc:
463 fd = VTOFDESC(vp)->fd_fd;
464 error = fdesc_attr(fd, vap, ap->a_cred);
465 break;
466
467 default:
468 panic("fdesc_getattr");
469 break;
470 }
471
472 if (error == 0)
473 vp->v_type = vap->va_type;
474
475 return (error);
476 }
477
478 int
479 fdesc_setattr(void *v)
480 {
481 struct vop_setattr_args /* {
482 struct vnode *a_vp;
483 struct vattr *a_vap;
484 kauth_cred_t a_cred;
485 } */ *ap = v;
486 file_t *fp;
487 unsigned fd;
488
489 /*
490 * Can't mess with the root vnode
491 */
492 switch (VTOFDESC(ap->a_vp)->fd_type) {
493 case Fdesc:
494 break;
495
496 case Fctty:
497 return (0);
498
499 default:
500 return (EACCES);
501 }
502
503 fd = VTOFDESC(ap->a_vp)->fd_fd;
504 if ((fp = fd_getfile(fd)) == NULL)
505 return (EBADF);
506
507 /*
508 * XXX: Can't reasonably set the attr's on any types currently.
509 * On vnode's this will cause truncation and socket/pipes make
510 * no sense.
511 */
512 fd_putfile(fd);
513 return (0);
514 }
515
516
517 struct fdesc_target {
518 ino_t ft_fileno;
519 u_char ft_type;
520 u_char ft_namlen;
521 const char *ft_name;
522 } fdesc_targets[] = {
523 #define N(s) sizeof(s)-1, s
524 { FD_DEVFD, DT_DIR, N("fd") },
525 { FD_STDIN, DT_LNK, N("stdin") },
526 { FD_STDOUT, DT_LNK, N("stdout") },
527 { FD_STDERR, DT_LNK, N("stderr") },
528 { FD_CTTY, DT_UNKNOWN, N("tty") },
529 #undef N
530 #define UIO_MX _DIRENT_RECLEN((struct dirent *)NULL, sizeof("stderr") - 1)
531 };
532 static int nfdesc_targets = sizeof(fdesc_targets) / sizeof(fdesc_targets[0]);
533
534 int
535 fdesc_readdir(void *v)
536 {
537 struct vop_readdir_args /* {
538 struct vnode *a_vp;
539 struct uio *a_uio;
540 kauth_cred_t a_cred;
541 int *a_eofflag;
542 off_t **a_cookies;
543 int *a_ncookies;
544 } */ *ap = v;
545 struct uio *uio = ap->a_uio;
546 struct dirent d;
547 off_t i;
548 int j;
549 int error;
550 off_t *cookies = NULL;
551 int ncookies;
552 fdtab_t *dt;
553
554 switch (VTOFDESC(ap->a_vp)->fd_type) {
555 case Fctty:
556 return 0;
557
558 case Fdesc:
559 return ENOTDIR;
560
561 default:
562 break;
563 }
564
565 dt = curlwp->l_fd->fd_dt;
566
567 if (uio->uio_resid < UIO_MX)
568 return EINVAL;
569 if (uio->uio_offset < 0)
570 return EINVAL;
571
572 error = 0;
573 i = uio->uio_offset;
574 (void)memset(&d, 0, UIO_MX);
575 d.d_reclen = UIO_MX;
576 if (ap->a_ncookies)
577 ncookies = uio->uio_resid / UIO_MX;
578 else
579 ncookies = 0;
580
581 if (VTOFDESC(ap->a_vp)->fd_type == Froot) {
582 struct fdesc_target *ft;
583
584 if (i >= nfdesc_targets)
585 return 0;
586
587 if (ap->a_ncookies) {
588 ncookies = min(ncookies, (nfdesc_targets - i));
589 cookies = malloc(ncookies * sizeof(off_t),
590 M_TEMP, M_WAITOK);
591 *ap->a_cookies = cookies;
592 *ap->a_ncookies = ncookies;
593 }
594
595 for (ft = &fdesc_targets[i]; uio->uio_resid >= UIO_MX &&
596 i < nfdesc_targets; ft++, i++) {
597 switch (ft->ft_fileno) {
598 case FD_CTTY:
599 if (cttyvp(curproc) == NULL)
600 continue;
601 break;
602
603 case FD_STDIN:
604 case FD_STDOUT:
605 case FD_STDERR:
606 if ((ft->ft_fileno - FD_STDIN) >=
607 dt->dt_nfiles)
608 continue;
609 if (dt->dt_ff[ft->ft_fileno - FD_STDIN]
610 == NULL || dt->dt_ff[ft->ft_fileno -
611 FD_STDIN]->ff_file == NULL)
612 continue;
613 break;
614 }
615
616 d.d_fileno = ft->ft_fileno;
617 d.d_namlen = ft->ft_namlen;
618 (void)memcpy(d.d_name, ft->ft_name, ft->ft_namlen + 1);
619 d.d_type = ft->ft_type;
620
621 if ((error = uiomove(&d, UIO_MX, uio)) != 0)
622 break;
623 if (cookies)
624 *cookies++ = i + 1;
625 }
626 } else {
627 membar_consumer();
628 if (ap->a_ncookies) {
629 ncookies = min(ncookies, dt->dt_nfiles + 2);
630 cookies = malloc(ncookies * sizeof(off_t),
631 M_TEMP, M_WAITOK);
632 *ap->a_cookies = cookies;
633 *ap->a_ncookies = ncookies;
634 }
635 for (; i - 2 < dt->dt_nfiles && uio->uio_resid >= UIO_MX; i++) {
636 switch (i) {
637 case 0:
638 case 1:
639 d.d_fileno = FD_ROOT; /* XXX */
640 d.d_namlen = i + 1;
641 (void)memcpy(d.d_name, "..", d.d_namlen);
642 d.d_name[i + 1] = '\0';
643 d.d_type = DT_DIR;
644 break;
645
646 default:
647 j = (int)i - 2;
648 if (dt->dt_ff[j] == NULL ||
649 dt->dt_ff[j]->ff_file == NULL)
650 continue;
651 d.d_fileno = j + FD_STDIN;
652 d.d_namlen = snprintf(d.d_name,
653 sizeof(d.d_name), "%d", j);
654 d.d_type = DT_UNKNOWN;
655 break;
656 }
657
658 if ((error = uiomove(&d, UIO_MX, uio)) != 0)
659 break;
660 if (cookies)
661 *cookies++ = i + 1;
662 }
663 }
664
665 if (ap->a_ncookies && error) {
666 free(*ap->a_cookies, M_TEMP);
667 *ap->a_ncookies = 0;
668 *ap->a_cookies = NULL;
669 }
670
671 uio->uio_offset = i;
672 return error;
673 }
674
675 int
676 fdesc_readlink(void *v)
677 {
678 struct vop_readlink_args /* {
679 struct vnode *a_vp;
680 struct uio *a_uio;
681 kauth_cred_t a_cred;
682 } */ *ap = v;
683 struct vnode *vp = ap->a_vp;
684 int error;
685
686 if (vp->v_type != VLNK)
687 return (EPERM);
688
689 if (VTOFDESC(vp)->fd_type == Flink) {
690 const char *ln = VTOFDESC(vp)->fd_link;
691 error = uiomove(__UNCONST(ln), strlen(ln), ap->a_uio);
692 } else {
693 error = EOPNOTSUPP;
694 }
695
696 return (error);
697 }
698
699 int
700 fdesc_read(void *v)
701 {
702 struct vop_read_args /* {
703 struct vnode *a_vp;
704 struct uio *a_uio;
705 int a_ioflag;
706 kauth_cred_t a_cred;
707 } */ *ap = v;
708 int error = EOPNOTSUPP;
709 struct vnode *vp = ap->a_vp;
710
711 switch (VTOFDESC(vp)->fd_type) {
712 case Fctty:
713 VOP_UNLOCK(vp);
714 error = cdev_read(devctty, ap->a_uio, ap->a_ioflag);
715 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
716 break;
717
718 default:
719 error = EOPNOTSUPP;
720 break;
721 }
722
723 return (error);
724 }
725
726 int
727 fdesc_write(void *v)
728 {
729 struct vop_write_args /* {
730 struct vnode *a_vp;
731 struct uio *a_uio;
732 int a_ioflag;
733 kauth_cred_t a_cred;
734 } */ *ap = v;
735 int error = EOPNOTSUPP;
736 struct vnode *vp = ap->a_vp;
737
738 switch (VTOFDESC(vp)->fd_type) {
739 case Fctty:
740 VOP_UNLOCK(vp);
741 error = cdev_write(devctty, ap->a_uio, ap->a_ioflag);
742 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
743 break;
744
745 default:
746 error = EOPNOTSUPP;
747 break;
748 }
749
750 return (error);
751 }
752
753 int
754 fdesc_ioctl(void *v)
755 {
756 struct vop_ioctl_args /* {
757 struct vnode *a_vp;
758 u_long a_command;
759 void *a_data;
760 int a_fflag;
761 kauth_cred_t a_cred;
762 } */ *ap = v;
763 int error = EOPNOTSUPP;
764
765 switch (VTOFDESC(ap->a_vp)->fd_type) {
766 case Fctty:
767 error = cdev_ioctl(devctty, ap->a_command, ap->a_data,
768 ap->a_fflag, curlwp);
769 break;
770
771 default:
772 error = EOPNOTSUPP;
773 break;
774 }
775
776 return (error);
777 }
778
779 int
780 fdesc_poll(void *v)
781 {
782 struct vop_poll_args /* {
783 struct vnode *a_vp;
784 int a_events;
785 } */ *ap = v;
786 int revents;
787
788 switch (VTOFDESC(ap->a_vp)->fd_type) {
789 case Fctty:
790 revents = cdev_poll(devctty, ap->a_events, curlwp);
791 break;
792
793 default:
794 revents = genfs_poll(v);
795 break;
796 }
797
798 return (revents);
799 }
800
801 int
802 fdesc_kqfilter(void *v)
803 {
804 struct vop_kqfilter_args /* {
805 struct vnode *a_vp;
806 struct knote *a_kn;
807 } */ *ap = v;
808 int error, fd;
809 file_t *fp;
810
811 switch (VTOFDESC(ap->a_vp)->fd_type) {
812 case Fctty:
813 error = cdev_kqfilter(devctty, ap->a_kn);
814 break;
815
816 case Fdesc:
817 /* just invoke kqfilter for the underlying descriptor */
818 fd = VTOFDESC(ap->a_vp)->fd_fd;
819 if ((fp = fd_getfile(fd)) == NULL)
820 return (1);
821 error = (*fp->f_ops->fo_kqfilter)(fp, ap->a_kn);
822 fd_putfile(fd);
823 break;
824
825 default:
826 return (genfs_kqfilter(v));
827 }
828
829 return (error);
830 }
831
832 int
833 fdesc_inactive(void *v)
834 {
835 struct vop_inactive_args /* {
836 struct vnode *a_vp;
837 } */ *ap = v;
838 struct vnode *vp = ap->a_vp;
839 struct fdescnode *fd = VTOFDESC(vp);
840
841 /*
842 * Clear out the v_type field to avoid
843 * nasty things happening on reclaim.
844 */
845 if (fd->fd_type == Fctty || fd->fd_type == Fdesc)
846 vp->v_type = VNON;
847 VOP_UNLOCK(vp);
848 return (0);
849 }
850
851 int
852 fdesc_reclaim(void *v)
853 {
854 struct vop_reclaim_args /* {
855 struct vnode *a_vp;
856 } */ *ap = v;
857 struct vnode *vp = ap->a_vp;
858 struct fdescnode *fd = VTOFDESC(vp);
859
860 vp->v_data = NULL;
861 vcache_remove(vp->v_mount, &fd->fd_ix, sizeof(fd->fd_ix));
862 kmem_free(fd, sizeof(struct fdescnode));
863
864 return (0);
865 }
866
867 /*
868 * Return POSIX pathconf information applicable to special devices.
869 */
870 int
871 fdesc_pathconf(void *v)
872 {
873 struct vop_pathconf_args /* {
874 struct vnode *a_vp;
875 int a_name;
876 register_t *a_retval;
877 } */ *ap = v;
878
879 switch (ap->a_name) {
880 case _PC_LINK_MAX:
881 *ap->a_retval = LINK_MAX;
882 return (0);
883 case _PC_MAX_CANON:
884 *ap->a_retval = MAX_CANON;
885 return (0);
886 case _PC_MAX_INPUT:
887 *ap->a_retval = MAX_INPUT;
888 return (0);
889 case _PC_PIPE_BUF:
890 *ap->a_retval = PIPE_BUF;
891 return (0);
892 case _PC_CHOWN_RESTRICTED:
893 *ap->a_retval = 1;
894 return (0);
895 case _PC_VDISABLE:
896 *ap->a_retval = _POSIX_VDISABLE;
897 return (0);
898 case _PC_SYNC_IO:
899 *ap->a_retval = 1;
900 return (0);
901 default:
902 return (EINVAL);
903 }
904 /* NOTREACHED */
905 }
906
907 /*
908 * Print out the contents of a /dev/fd vnode.
909 */
910 /* ARGSUSED */
911 int
912 fdesc_print(void *v)
913 {
914 printf("tag VT_NON, fdesc vnode\n");
915 return (0);
916 }
917
918 int
919 fdesc_link(void *v)
920 {
921 struct vop_link_args /* {
922 struct vnode *a_dvp;
923 struct vnode *a_vp;
924 struct componentname *a_cnp;
925 } */ *ap = v;
926
927 VOP_ABORTOP(ap->a_dvp, ap->a_cnp);
928 vput(ap->a_dvp);
929 return (EROFS);
930 }
931
932 int
933 fdesc_symlink(void *v)
934 {
935 struct vop_symlink_v3_args /* {
936 struct vnode *a_dvp;
937 struct vnode **a_vpp;
938 struct componentname *a_cnp;
939 struct vattr *a_vap;
940 char *a_target;
941 } */ *ap = v;
942
943 VOP_ABORTOP(ap->a_dvp, ap->a_cnp);
944 return (EROFS);
945 }
946