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