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