fdesc_vnops.c revision 1.137 1 /* $NetBSD: fdesc_vnops.c,v 1.137 2021/06/29 22:34:08 dholland 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.137 2021/06/29 22:34:08 dholland 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. The error
330 * return ensures that the vnode for this device will be
331 * released by vn_open. Open will detect this special error and
332 * take the actions in dupfdopen. Other callers of vn_open or
333 * VOP_OPEN will simply report the error.
334 */
335 curlwp->l_dupfd = VTOFDESC(vp)->fd_fd; /* XXX */
336 return EDUPFD;
337
338 case Fctty:
339 return cdev_open(devctty, ap->a_mode, 0, curlwp);
340 case Froot:
341 case Fdevfd:
342 case Flink:
343 break;
344 }
345
346 return (0);
347 }
348
349 static int
350 fdesc_attr(int fd, struct vattr *vap, kauth_cred_t cred)
351 {
352 file_t *fp;
353 struct stat stb;
354 int error;
355
356 if ((fp = fd_getfile(fd)) == NULL)
357 return (EBADF);
358
359 switch (fp->f_type) {
360 case DTYPE_VNODE:
361 vn_lock(fp->f_vnode, LK_SHARED | LK_RETRY);
362 error = VOP_GETATTR(fp->f_vnode, vap, cred);
363 VOP_UNLOCK(fp->f_vnode);
364 if (error == 0 && vap->va_type == VDIR) {
365 /*
366 * directories can cause loops in the namespace,
367 * so turn off the 'x' bits to avoid trouble.
368 */
369 vap->va_mode &= ~(S_IXUSR|S_IXGRP|S_IXOTH);
370 }
371 break;
372
373 default:
374 memset(&stb, 0, sizeof(stb));
375 error = (*fp->f_ops->fo_stat)(fp, &stb);
376 if (error)
377 break;
378
379 vattr_null(vap);
380 switch(fp->f_type) {
381 case DTYPE_SOCKET:
382 vap->va_type = VSOCK;
383 break;
384 case DTYPE_PIPE:
385 vap->va_type = VFIFO;
386 break;
387 default:
388 /* use VNON perhaps? */
389 vap->va_type = VBAD;
390 break;
391 }
392 vap->va_mode = stb.st_mode;
393 vap->va_nlink = stb.st_nlink;
394 vap->va_uid = stb.st_uid;
395 vap->va_gid = stb.st_gid;
396 vap->va_fsid = stb.st_dev;
397 vap->va_fileid = stb.st_ino;
398 vap->va_size = stb.st_size;
399 vap->va_blocksize = stb.st_blksize;
400 vap->va_atime = stb.st_atimespec;
401 vap->va_mtime = stb.st_mtimespec;
402 vap->va_ctime = stb.st_ctimespec;
403 vap->va_gen = stb.st_gen;
404 vap->va_flags = stb.st_flags;
405 vap->va_rdev = stb.st_rdev;
406 vap->va_bytes = stb.st_blocks * stb.st_blksize;
407 break;
408 }
409
410 fd_putfile(fd);
411 return (error);
412 }
413
414 int
415 fdesc_getattr(void *v)
416 {
417 struct vop_getattr_args /* {
418 struct vnode *a_vp;
419 struct vattr *a_vap;
420 kauth_cred_t a_cred;
421 struct lwp *a_l;
422 } */ *ap = v;
423 struct vnode *vp = ap->a_vp;
424 struct vattr *vap = ap->a_vap;
425 unsigned fd;
426 int error = 0;
427 struct timeval tv;
428
429 switch (VTOFDESC(vp)->fd_type) {
430 case Froot:
431 case Fdevfd:
432 case Flink:
433 case Fctty:
434 vattr_null(vap);
435 vap->va_fileid = VTOFDESC(vp)->fd_ix;
436
437 #define R_ALL (S_IRUSR|S_IRGRP|S_IROTH)
438 #define W_ALL (S_IWUSR|S_IWGRP|S_IWOTH)
439 #define X_ALL (S_IXUSR|S_IXGRP|S_IXOTH)
440
441 switch (VTOFDESC(vp)->fd_type) {
442 case Flink:
443 vap->va_mode = R_ALL|X_ALL;
444 vap->va_type = VLNK;
445 vap->va_rdev = 0;
446 vap->va_nlink = 1;
447 vap->va_size = strlen(VTOFDESC(vp)->fd_link);
448 break;
449
450 case Fctty:
451 vap->va_mode = R_ALL|W_ALL;
452 vap->va_type = VCHR;
453 vap->va_rdev = devctty;
454 vap->va_nlink = 1;
455 vap->va_size = 0;
456 break;
457
458 default:
459 vap->va_mode = R_ALL|X_ALL;
460 vap->va_type = VDIR;
461 vap->va_rdev = 0;
462 vap->va_nlink = 2;
463 vap->va_size = DEV_BSIZE;
464 break;
465 }
466 vap->va_uid = 0;
467 vap->va_gid = 0;
468 vap->va_fsid = vp->v_mount->mnt_stat.f_fsidx.__fsid_val[0];
469 vap->va_blocksize = DEV_BSIZE;
470 getmicroboottime(&tv);
471 vap->va_atime.tv_sec = tv.tv_sec;
472 vap->va_atime.tv_nsec = 0;
473 vap->va_mtime = vap->va_atime;
474 vap->va_ctime = vap->va_mtime;
475 vap->va_gen = 0;
476 vap->va_flags = 0;
477 vap->va_bytes = 0;
478 break;
479
480 case Fdesc:
481 fd = VTOFDESC(vp)->fd_fd;
482 error = fdesc_attr(fd, vap, ap->a_cred);
483 break;
484
485 default:
486 panic("fdesc_getattr");
487 break;
488 }
489
490 if (error == 0)
491 vp->v_type = vap->va_type;
492
493 return (error);
494 }
495
496 int
497 fdesc_setattr(void *v)
498 {
499 struct vop_setattr_args /* {
500 struct vnode *a_vp;
501 struct vattr *a_vap;
502 kauth_cred_t a_cred;
503 } */ *ap = v;
504 file_t *fp;
505 unsigned fd;
506
507 /*
508 * Can't mess with the root vnode
509 */
510 switch (VTOFDESC(ap->a_vp)->fd_type) {
511 case Fdesc:
512 break;
513
514 case Fctty:
515 return (0);
516
517 default:
518 return (EACCES);
519 }
520
521 fd = VTOFDESC(ap->a_vp)->fd_fd;
522 if ((fp = fd_getfile(fd)) == NULL)
523 return (EBADF);
524
525 /*
526 * XXX: Can't reasonably set the attr's on any types currently.
527 * On vnode's this will cause truncation and socket/pipes make
528 * no sense.
529 */
530 fd_putfile(fd);
531 return (0);
532 }
533
534
535 struct fdesc_target {
536 ino_t ft_fileno;
537 u_char ft_type;
538 u_char ft_namlen;
539 const char *ft_name;
540 } fdesc_targets[] = {
541 #define N(s) sizeof(s)-1, s
542 { FD_DEVFD, DT_DIR, N("fd") },
543 { FD_STDIN, DT_LNK, N("stdin") },
544 { FD_STDOUT, DT_LNK, N("stdout") },
545 { FD_STDERR, DT_LNK, N("stderr") },
546 { FD_CTTY, DT_UNKNOWN, N("tty") },
547 #undef N
548 #define UIO_MX _DIRENT_RECLEN((struct dirent *)NULL, sizeof("stderr") - 1)
549 };
550 static int nfdesc_targets = sizeof(fdesc_targets) / sizeof(fdesc_targets[0]);
551
552 int
553 fdesc_readdir(void *v)
554 {
555 struct vop_readdir_args /* {
556 struct vnode *a_vp;
557 struct uio *a_uio;
558 kauth_cred_t a_cred;
559 int *a_eofflag;
560 off_t **a_cookies;
561 int *a_ncookies;
562 } */ *ap = v;
563 struct uio *uio = ap->a_uio;
564 struct dirent d;
565 off_t i;
566 int j;
567 int error;
568 off_t *cookies = NULL;
569 int ncookies;
570 fdtab_t *dt;
571
572 switch (VTOFDESC(ap->a_vp)->fd_type) {
573 case Fctty:
574 return 0;
575
576 case Fdesc:
577 return ENOTDIR;
578
579 default:
580 break;
581 }
582
583 dt = atomic_load_consume(&curlwp->l_fd->fd_dt);
584
585 if (uio->uio_resid < UIO_MX)
586 return EINVAL;
587 if (uio->uio_offset < 0)
588 return EINVAL;
589
590 error = 0;
591 i = uio->uio_offset;
592 (void)memset(&d, 0, UIO_MX);
593 d.d_reclen = UIO_MX;
594 if (ap->a_ncookies)
595 ncookies = uio->uio_resid / UIO_MX;
596 else
597 ncookies = 0;
598
599 if (VTOFDESC(ap->a_vp)->fd_type == Froot) {
600 struct fdesc_target *ft;
601
602 if (i >= nfdesc_targets)
603 return 0;
604
605 if (ap->a_ncookies) {
606 ncookies = uimin(ncookies, (nfdesc_targets - i));
607 cookies = malloc(ncookies * sizeof(off_t),
608 M_TEMP, M_WAITOK);
609 *ap->a_cookies = cookies;
610 *ap->a_ncookies = ncookies;
611 }
612
613 for (ft = &fdesc_targets[i]; uio->uio_resid >= UIO_MX &&
614 i < nfdesc_targets; ft++, i++) {
615 switch (ft->ft_fileno) {
616 case FD_CTTY:
617 if (cttyvp(curproc) == NULL)
618 continue;
619 break;
620
621 case FD_STDIN:
622 case FD_STDOUT:
623 case FD_STDERR:
624 if ((ft->ft_fileno - FD_STDIN) >=
625 dt->dt_nfiles)
626 continue;
627 if (dt->dt_ff[ft->ft_fileno - FD_STDIN]
628 == NULL || dt->dt_ff[ft->ft_fileno -
629 FD_STDIN]->ff_file == NULL)
630 continue;
631 break;
632 }
633
634 d.d_fileno = ft->ft_fileno;
635 d.d_namlen = ft->ft_namlen;
636 (void)memcpy(d.d_name, ft->ft_name, ft->ft_namlen + 1);
637 d.d_type = ft->ft_type;
638
639 if ((error = uiomove(&d, UIO_MX, uio)) != 0)
640 break;
641 if (cookies)
642 *cookies++ = i + 1;
643 }
644 } else {
645 if (ap->a_ncookies) {
646 ncookies = uimin(ncookies, dt->dt_nfiles + 2);
647 cookies = malloc(ncookies * sizeof(off_t),
648 M_TEMP, M_WAITOK);
649 *ap->a_cookies = cookies;
650 *ap->a_ncookies = ncookies;
651 }
652 for (; i - 2 < dt->dt_nfiles && uio->uio_resid >= UIO_MX; i++) {
653 switch (i) {
654 case 0:
655 case 1:
656 d.d_fileno = FD_ROOT; /* XXX */
657 d.d_namlen = i + 1;
658 (void)memcpy(d.d_name, "..", d.d_namlen);
659 d.d_name[i + 1] = '\0';
660 d.d_type = DT_DIR;
661 break;
662
663 default:
664 j = (int)i - 2;
665 if (dt->dt_ff[j] == NULL ||
666 dt->dt_ff[j]->ff_file == NULL)
667 continue;
668 d.d_fileno = j + FD_STDIN;
669 d.d_namlen = snprintf(d.d_name,
670 sizeof(d.d_name), "%d", j);
671 d.d_type = DT_UNKNOWN;
672 break;
673 }
674
675 if ((error = uiomove(&d, UIO_MX, uio)) != 0)
676 break;
677 if (cookies)
678 *cookies++ = i + 1;
679 }
680 }
681
682 if (ap->a_ncookies && error) {
683 free(*ap->a_cookies, M_TEMP);
684 *ap->a_ncookies = 0;
685 *ap->a_cookies = NULL;
686 }
687
688 uio->uio_offset = i;
689 return error;
690 }
691
692 int
693 fdesc_readlink(void *v)
694 {
695 struct vop_readlink_args /* {
696 struct vnode *a_vp;
697 struct uio *a_uio;
698 kauth_cred_t a_cred;
699 } */ *ap = v;
700 struct vnode *vp = ap->a_vp;
701 int error;
702
703 if (vp->v_type != VLNK)
704 return (EPERM);
705
706 if (VTOFDESC(vp)->fd_type == Flink) {
707 const char *ln = VTOFDESC(vp)->fd_link;
708 error = uiomove(__UNCONST(ln), strlen(ln), ap->a_uio);
709 } else {
710 error = EOPNOTSUPP;
711 }
712
713 return (error);
714 }
715
716 int
717 fdesc_read(void *v)
718 {
719 struct vop_read_args /* {
720 struct vnode *a_vp;
721 struct uio *a_uio;
722 int a_ioflag;
723 kauth_cred_t a_cred;
724 } */ *ap = v;
725 int error = EOPNOTSUPP;
726 struct vnode *vp = ap->a_vp;
727
728 switch (VTOFDESC(vp)->fd_type) {
729 case Fctty:
730 VOP_UNLOCK(vp);
731 error = cdev_read(devctty, ap->a_uio, ap->a_ioflag);
732 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
733 break;
734
735 default:
736 error = EOPNOTSUPP;
737 break;
738 }
739
740 return (error);
741 }
742
743 int
744 fdesc_write(void *v)
745 {
746 struct vop_write_args /* {
747 struct vnode *a_vp;
748 struct uio *a_uio;
749 int a_ioflag;
750 kauth_cred_t a_cred;
751 } */ *ap = v;
752 int error = EOPNOTSUPP;
753 struct vnode *vp = ap->a_vp;
754
755 switch (VTOFDESC(vp)->fd_type) {
756 case Fctty:
757 VOP_UNLOCK(vp);
758 error = cdev_write(devctty, ap->a_uio, ap->a_ioflag);
759 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
760 break;
761
762 default:
763 error = EOPNOTSUPP;
764 break;
765 }
766
767 return (error);
768 }
769
770 int
771 fdesc_ioctl(void *v)
772 {
773 struct vop_ioctl_args /* {
774 struct vnode *a_vp;
775 u_long a_command;
776 void *a_data;
777 int a_fflag;
778 kauth_cred_t a_cred;
779 } */ *ap = v;
780 int error = EOPNOTSUPP;
781
782 switch (VTOFDESC(ap->a_vp)->fd_type) {
783 case Fctty:
784 error = cdev_ioctl(devctty, ap->a_command, ap->a_data,
785 ap->a_fflag, curlwp);
786 break;
787
788 default:
789 error = EOPNOTSUPP;
790 break;
791 }
792
793 return (error);
794 }
795
796 int
797 fdesc_poll(void *v)
798 {
799 struct vop_poll_args /* {
800 struct vnode *a_vp;
801 int a_events;
802 } */ *ap = v;
803 int revents;
804
805 switch (VTOFDESC(ap->a_vp)->fd_type) {
806 case Fctty:
807 revents = cdev_poll(devctty, ap->a_events, curlwp);
808 break;
809
810 default:
811 revents = genfs_poll(v);
812 break;
813 }
814
815 return (revents);
816 }
817
818 int
819 fdesc_kqfilter(void *v)
820 {
821 struct vop_kqfilter_args /* {
822 struct vnode *a_vp;
823 struct knote *a_kn;
824 } */ *ap = v;
825 int error, fd;
826 file_t *fp;
827
828 switch (VTOFDESC(ap->a_vp)->fd_type) {
829 case Fctty:
830 error = cdev_kqfilter(devctty, ap->a_kn);
831 break;
832
833 case Fdesc:
834 /* just invoke kqfilter for the underlying descriptor */
835 fd = VTOFDESC(ap->a_vp)->fd_fd;
836 if ((fp = fd_getfile(fd)) == NULL)
837 return (1);
838 error = (*fp->f_ops->fo_kqfilter)(fp, ap->a_kn);
839 fd_putfile(fd);
840 break;
841
842 default:
843 return (genfs_kqfilter(v));
844 }
845
846 return (error);
847 }
848
849 int
850 fdesc_inactive(void *v)
851 {
852 struct vop_inactive_v2_args /* {
853 struct vnode *a_vp;
854 } */ *ap = v;
855 struct vnode *vp = ap->a_vp;
856 struct fdescnode *fd = VTOFDESC(vp);
857
858 /*
859 * Clear out the v_type field to avoid
860 * nasty things happening on reclaim.
861 */
862 if (fd->fd_type == Fctty || fd->fd_type == Fdesc)
863 vp->v_type = VNON;
864
865 return (0);
866 }
867
868 int
869 fdesc_reclaim(void *v)
870 {
871 struct vop_reclaim_v2_args /* {
872 struct vnode *a_vp;
873 } */ *ap = v;
874 struct vnode *vp = ap->a_vp;
875 struct fdescnode *fd = VTOFDESC(vp);
876
877 VOP_UNLOCK(vp);
878
879 vp->v_data = NULL;
880 kmem_free(fd, sizeof(struct fdescnode));
881
882 return (0);
883 }
884
885 /*
886 * Return POSIX pathconf information applicable to special devices.
887 */
888 int
889 fdesc_pathconf(void *v)
890 {
891 struct vop_pathconf_args /* {
892 struct vnode *a_vp;
893 int a_name;
894 register_t *a_retval;
895 } */ *ap = v;
896
897 switch (ap->a_name) {
898 case _PC_LINK_MAX:
899 *ap->a_retval = LINK_MAX;
900 return (0);
901 case _PC_MAX_CANON:
902 *ap->a_retval = MAX_CANON;
903 return (0);
904 case _PC_MAX_INPUT:
905 *ap->a_retval = MAX_INPUT;
906 return (0);
907 case _PC_PIPE_BUF:
908 *ap->a_retval = PIPE_BUF;
909 return (0);
910 case _PC_CHOWN_RESTRICTED:
911 *ap->a_retval = 1;
912 return (0);
913 case _PC_VDISABLE:
914 *ap->a_retval = _POSIX_VDISABLE;
915 return (0);
916 case _PC_SYNC_IO:
917 *ap->a_retval = 1;
918 return (0);
919 default:
920 return genfs_pathconf(ap);
921 }
922 /* NOTREACHED */
923 }
924
925 /*
926 * Print out the contents of a /dev/fd vnode.
927 */
928 /* ARGSUSED */
929 int
930 fdesc_print(void *v)
931 {
932 printf("tag VT_NON, fdesc vnode\n");
933 return (0);
934 }
935
936 int
937 fdesc_link(void *v)
938 {
939 struct vop_link_v2_args /* {
940 struct vnode *a_dvp;
941 struct vnode *a_vp;
942 struct componentname *a_cnp;
943 } */ *ap = v;
944
945 VOP_ABORTOP(ap->a_dvp, ap->a_cnp);
946 return (EROFS);
947 }
948
949 int
950 fdesc_symlink(void *v)
951 {
952 struct vop_symlink_v3_args /* {
953 struct vnode *a_dvp;
954 struct vnode **a_vpp;
955 struct componentname *a_cnp;
956 struct vattr *a_vap;
957 char *a_target;
958 } */ *ap = v;
959
960 VOP_ABORTOP(ap->a_dvp, ap->a_cnp);
961 return (EROFS);
962 }
963