Annotation of src/sys/miscfs/fdesc/fdesc_vnops.c, Revision 1.113
1.113 ! rmind 1: /* $NetBSD$ */
1.15 cgd 2:
1.1 cgd 3: /*
1.14 mycroft 4: * Copyright (c) 1992, 1993
5: * The Regents of the University of California. All rights reserved.
1.1 cgd 6: *
1.8 cgd 7: * This code is derived from software donated to Berkeley by
1.1 cgd 8: * Jan-Simon Pendry.
9: *
1.2 cgd 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.
1.78 agc 18: * 3. Neither the name of the University nor the names of its contributors
1.2 cgd 19: * may be used to endorse or promote products derived from this software
20: * without specific prior written permission.
1.1 cgd 21: *
1.2 cgd 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.
1.1 cgd 33: *
1.44 fvdl 34: * @(#)fdesc_vnops.c 8.17 (Berkeley) 5/22/95
1.22 mycroft 35: *
36: * #Id: fdesc_vnops.c,v 1.12 1993/04/06 16:17:17 jsp Exp #
1.1 cgd 37: */
38:
39: /*
40: * /dev/fd Filesystem
41: */
1.65 lukem 42:
43: #include <sys/cdefs.h>
1.113 ! rmind 44: __KERNEL_RCSID(0, "$NetBSD$");
1.1 cgd 45:
1.8 cgd 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>
1.30 christos 52: #include <sys/socketvar.h>
1.8 cgd 53: #include <sys/filedesc.h>
54: #include <sys/vnode.h>
1.14 mycroft 55: #include <sys/malloc.h>
1.32 mrg 56: #include <sys/conf.h>
1.8 cgd 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>
1.14 mycroft 62: #include <sys/dirent.h>
1.30 christos 63: #include <sys/tty.h>
1.92 elad 64: #include <sys/kauth.h>
1.102 ad 65: #include <sys/atomic.h>
1.34 mycroft 66:
1.8 cgd 67: #include <miscfs/fdesc/fdesc.h>
1.34 mycroft 68: #include <miscfs/genfs/genfs.h>
1.8 cgd 69:
1.96 ad 70: #define cttyvp(p) ((p)->p_lflag & PL_CONTROLT ? (p)->p_session->s_ttyvp : NULL)
1.8 cgd 71:
1.111 hannken 72: static kmutex_t fdcache_lock;
1.14 mycroft 73:
74: dev_t devctty;
1.8 cgd 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:
1.16 mycroft 80: #define NFDCACHE 4
1.14 mycroft 81:
1.17 mycroft 82: #define FD_NHASH(ix) \
83: (&fdhashtbl[(ix) & fdhash])
84: LIST_HEAD(fdhashhead, fdescnode) *fdhashtbl;
85: u_long fdhash;
1.14 mycroft 86:
1.86 xtraeme 87: int fdesc_lookup(void *);
1.74 jdolecek 88: #define fdesc_create genfs_eopnotsupp
89: #define fdesc_mknod genfs_eopnotsupp
1.86 xtraeme 90: int fdesc_open(void *);
1.34 mycroft 91: #define fdesc_close genfs_nullop
92: #define fdesc_access genfs_nullop
1.86 xtraeme 93: int fdesc_getattr(void *);
94: int fdesc_setattr(void *);
95: int fdesc_read(void *);
96: int fdesc_write(void *);
97: int fdesc_ioctl(void *);
98: int fdesc_poll(void *);
99: int fdesc_kqfilter(void *);
1.34 mycroft 100: #define fdesc_mmap genfs_eopnotsupp
1.50 wrstuden 101: #define fdesc_fcntl genfs_fcntl
1.34 mycroft 102: #define fdesc_fsync genfs_nullop
1.40 fvdl 103: #define fdesc_seek genfs_seek
1.74 jdolecek 104: #define fdesc_remove genfs_eopnotsupp
1.86 xtraeme 105: int fdesc_link(void *);
1.74 jdolecek 106: #define fdesc_rename genfs_eopnotsupp
107: #define fdesc_mkdir genfs_eopnotsupp
108: #define fdesc_rmdir genfs_eopnotsupp
1.86 xtraeme 109: int fdesc_symlink(void *);
110: int fdesc_readdir(void *);
111: int fdesc_readlink(void *);
1.34 mycroft 112: #define fdesc_abortop genfs_abortop
1.86 xtraeme 113: int fdesc_inactive(void *);
114: int fdesc_reclaim(void *);
1.48 wrstuden 115: #define fdesc_lock genfs_lock
116: #define fdesc_unlock genfs_unlock
1.34 mycroft 117: #define fdesc_bmap genfs_badop
118: #define fdesc_strategy genfs_badop
1.86 xtraeme 119: int fdesc_print(void *);
120: int fdesc_pathconf(void *);
1.48 wrstuden 121: #define fdesc_islocked genfs_islocked
1.47 kleink 122: #define fdesc_advlock genfs_einval
1.34 mycroft 123: #define fdesc_bwrite genfs_eopnotsupp
1.44 fvdl 124: #define fdesc_revoke genfs_revoke
1.67 chs 125: #define fdesc_putpages genfs_null_putpages
1.30 christos 126:
1.102 ad 127: static int fdesc_attr(int, struct vattr *, kauth_cred_t);
1.30 christos 128:
1.86 xtraeme 129: int (**fdesc_vnodeop_p)(void *);
1.58 jdolecek 130: const struct vnodeopv_entry_desc fdesc_vnodeop_entries[] = {
1.30 christos 131: { &vop_default_desc, vn_default_error },
1.34 mycroft 132: { &vop_lookup_desc, fdesc_lookup }, /* lookup */
133: { &vop_create_desc, fdesc_create }, /* create */
134: { &vop_mknod_desc, fdesc_mknod }, /* mknod */
135: { &vop_open_desc, fdesc_open }, /* open */
136: { &vop_close_desc, fdesc_close }, /* close */
137: { &vop_access_desc, fdesc_access }, /* access */
138: { &vop_getattr_desc, fdesc_getattr }, /* getattr */
139: { &vop_setattr_desc, fdesc_setattr }, /* setattr */
140: { &vop_read_desc, fdesc_read }, /* read */
141: { &vop_write_desc, fdesc_write }, /* write */
142: { &vop_ioctl_desc, fdesc_ioctl }, /* ioctl */
1.50 wrstuden 143: { &vop_fcntl_desc, fdesc_fcntl }, /* fcntl */
1.35 mycroft 144: { &vop_poll_desc, fdesc_poll }, /* poll */
1.71 jdolecek 145: { &vop_kqfilter_desc, fdesc_kqfilter }, /* kqfilter */
1.44 fvdl 146: { &vop_revoke_desc, fdesc_revoke }, /* revoke */
1.34 mycroft 147: { &vop_mmap_desc, fdesc_mmap }, /* mmap */
148: { &vop_fsync_desc, fdesc_fsync }, /* fsync */
149: { &vop_seek_desc, fdesc_seek }, /* seek */
150: { &vop_remove_desc, fdesc_remove }, /* remove */
151: { &vop_link_desc, fdesc_link }, /* link */
152: { &vop_rename_desc, fdesc_rename }, /* rename */
153: { &vop_mkdir_desc, fdesc_mkdir }, /* mkdir */
154: { &vop_rmdir_desc, fdesc_rmdir }, /* rmdir */
155: { &vop_symlink_desc, fdesc_symlink }, /* symlink */
156: { &vop_readdir_desc, fdesc_readdir }, /* readdir */
157: { &vop_readlink_desc, fdesc_readlink }, /* readlink */
158: { &vop_abortop_desc, fdesc_abortop }, /* abortop */
159: { &vop_inactive_desc, fdesc_inactive }, /* inactive */
160: { &vop_reclaim_desc, fdesc_reclaim }, /* reclaim */
161: { &vop_lock_desc, fdesc_lock }, /* lock */
162: { &vop_unlock_desc, fdesc_unlock }, /* unlock */
163: { &vop_bmap_desc, fdesc_bmap }, /* bmap */
164: { &vop_strategy_desc, fdesc_strategy }, /* strategy */
165: { &vop_print_desc, fdesc_print }, /* print */
166: { &vop_islocked_desc, fdesc_islocked }, /* islocked */
167: { &vop_pathconf_desc, fdesc_pathconf }, /* pathconf */
168: { &vop_advlock_desc, fdesc_advlock }, /* advlock */
169: { &vop_bwrite_desc, fdesc_bwrite }, /* bwrite */
1.67 chs 170: { &vop_putpages_desc, fdesc_putpages }, /* putpages */
171: { NULL, NULL }
1.30 christos 172: };
173:
1.58 jdolecek 174: const struct vnodeopv_desc fdesc_vnodeop_opv_desc =
1.30 christos 175: { &fdesc_vnodeop_p, fdesc_vnodeop_entries };
176:
1.14 mycroft 177: /*
178: * Initialise cache headers
179: */
1.30 christos 180: void
1.106 cegger 181: fdesc_init(void)
1.14 mycroft 182: {
1.32 mrg 183: int cttymajor;
1.14 mycroft 184:
1.32 mrg 185: /* locate the major number */
1.108 pooka 186: cttymajor = devsw_name2chr("ctty", NULL, 0);
1.32 mrg 187: devctty = makedev(cttymajor, 0);
1.111 hannken 188: mutex_init(&fdcache_lock, MUTEX_DEFAULT, IPL_NONE);
1.103 ad 189: fdhashtbl = hashinit(NFDCACHE, HASH_LIST, true, &fdhash);
1.54 jdolecek 190: }
191:
192: /*
193: * Free hash table.
194: */
195: void
1.106 cegger 196: fdesc_done(void)
1.54 jdolecek 197: {
1.103 ad 198: hashdone(fdhashtbl, HASH_LIST, fdhash);
1.111 hannken 199: mutex_destroy(&fdcache_lock);
1.14 mycroft 200: }
201:
1.48 wrstuden 202: /*
203: * Return a locked vnode of the correct type.
204: */
1.14 mycroft 205: int
1.105 dsl 206: fdesc_allocvp(fdntype ftype, int ix, struct mount *mp, struct vnode **vpp)
1.8 cgd 207: {
1.17 mycroft 208: struct fdhashhead *fc;
1.14 mycroft 209: struct fdescnode *fd;
1.8 cgd 210: int error = 0;
211:
1.17 mycroft 212: fc = FD_NHASH(ix);
1.8 cgd 213: loop:
1.111 hannken 214: mutex_enter(&fdcache_lock);
215: LIST_FOREACH(fd, fc, fd_hash) {
1.14 mycroft 216: if (fd->fd_ix == ix && fd->fd_vnode->v_mount == mp) {
1.113 ! rmind 217: mutex_enter(fd->fd_vnode->v_interlock);
1.111 hannken 218: mutex_exit(&fdcache_lock);
1.112 hannken 219: if (vget(fd->fd_vnode, LK_EXCLUSIVE))
1.8 cgd 220: goto loop;
1.14 mycroft 221: *vpp = fd->fd_vnode;
1.111 hannken 222: return 0;
1.8 cgd 223: }
224: }
1.111 hannken 225: mutex_exit(&fdcache_lock);
226:
1.113 ! rmind 227: error = getnewvnode(VT_FDESC, mp, fdesc_vnodeop_p, NULL, vpp);
1.111 hannken 228: if (error)
229: return error;
1.8 cgd 230:
1.111 hannken 231: mutex_enter(&fdcache_lock);
232: LIST_FOREACH(fd, fc, fd_hash) {
233: if (fd->fd_ix == ix && fd->fd_vnode->v_mount == mp) {
234: /*
235: * Another thread beat us, push back freshly
236: * allocated vnode and retry.
237: */
238: mutex_exit(&fdcache_lock);
239: ungetnewvnode(*vpp);
240: goto loop;
241: }
1.8 cgd 242: }
1.1 cgd 243:
1.104 cegger 244: fd = malloc(sizeof(struct fdescnode), M_TEMP, M_WAITOK);
1.14 mycroft 245: (*vpp)->v_data = fd;
246: fd->fd_vnode = *vpp;
247: fd->fd_type = ftype;
248: fd->fd_fd = -1;
249: fd->fd_link = 0;
250: fd->fd_ix = ix;
1.98 pooka 251: uvm_vnp_setsize(*vpp, 0);
1.48 wrstuden 252: VOP_LOCK(*vpp, LK_EXCLUSIVE);
1.17 mycroft 253: LIST_INSERT_HEAD(fc, fd, fd_hash);
1.111 hannken 254: mutex_exit(&fdcache_lock);
1.8 cgd 255:
1.111 hannken 256: return 0;
1.8 cgd 257: }
1.1 cgd 258:
259: /*
260: * vp is the current namei directory
261: * ndp is the name to locate in that directory...
262: */
1.14 mycroft 263: int
1.105 dsl 264: fdesc_lookup(void *v)
1.30 christos 265: {
1.14 mycroft 266: struct vop_lookup_args /* {
267: struct vnode * a_dvp;
268: struct vnode ** a_vpp;
269: struct componentname * a_cnp;
1.30 christos 270: } */ *ap = v;
1.14 mycroft 271: struct vnode **vpp = ap->a_vpp;
272: struct vnode *dvp = ap->a_dvp;
1.44 fvdl 273: struct componentname *cnp = ap->a_cnp;
1.101 pooka 274: struct lwp *l = curlwp;
1.44 fvdl 275: const char *pname = cnp->cn_nameptr;
1.89 christos 276: struct proc *p = l->l_proc;
1.30 christos 277: unsigned fd = 0;
1.1 cgd 278: int error;
279: struct vnode *fvp;
1.84 christos 280: const char *ln;
1.107 ad 281: fdtab_t *dt;
282:
283: dt = curlwp->l_fd->fd_dt;
1.1 cgd 284:
1.44 fvdl 285: if (cnp->cn_namelen == 1 && *pname == '.') {
1.14 mycroft 286: *vpp = dvp;
1.109 pooka 287: vref(dvp);
1.1 cgd 288: return (0);
289: }
290:
1.8 cgd 291: switch (VTOFDESC(dvp)->fd_type) {
292: default:
293: case Flink:
294: case Fdesc:
295: case Fctty:
296: error = ENOTDIR;
297: goto bad;
298:
299: case Froot:
1.46 perry 300: if (cnp->cn_namelen == 2 && memcmp(pname, "fd", 2) == 0) {
1.8 cgd 301: error = fdesc_allocvp(Fdevfd, FD_DEVFD, dvp->v_mount, &fvp);
302: if (error)
303: goto bad;
1.14 mycroft 304: *vpp = fvp;
1.8 cgd 305: fvp->v_type = VDIR;
1.48 wrstuden 306: goto good;
1.8 cgd 307: }
308:
1.46 perry 309: if (cnp->cn_namelen == 3 && memcmp(pname, "tty", 3) == 0) {
1.8 cgd 310: struct vnode *ttyvp = cttyvp(p);
311: if (ttyvp == NULL) {
312: error = ENXIO;
313: goto bad;
314: }
315: error = fdesc_allocvp(Fctty, FD_CTTY, dvp->v_mount, &fvp);
316: if (error)
317: goto bad;
1.14 mycroft 318: *vpp = fvp;
1.32 mrg 319: fvp->v_type = VCHR;
1.48 wrstuden 320: goto good;
1.8 cgd 321: }
322:
323: ln = 0;
1.44 fvdl 324: switch (cnp->cn_namelen) {
1.8 cgd 325: case 5:
1.46 perry 326: if (memcmp(pname, "stdin", 5) == 0) {
1.8 cgd 327: ln = "fd/0";
328: fd = FD_STDIN;
329: }
1.1 cgd 330: break;
1.8 cgd 331: case 6:
1.46 perry 332: if (memcmp(pname, "stdout", 6) == 0) {
1.8 cgd 333: ln = "fd/1";
334: fd = FD_STDOUT;
335: } else
1.46 perry 336: if (memcmp(pname, "stderr", 6) == 0) {
1.8 cgd 337: ln = "fd/2";
338: fd = FD_STDERR;
339: }
340: break;
341: }
342:
343: if (ln) {
344: error = fdesc_allocvp(Flink, fd, dvp->v_mount, &fvp);
345: if (error)
346: goto bad;
1.84 christos 347: /* XXXUNCONST */
348: VTOFDESC(fvp)->fd_link = __UNCONST(ln);
1.14 mycroft 349: *vpp = fvp;
1.8 cgd 350: fvp->v_type = VLNK;
1.48 wrstuden 351: goto good;
1.8 cgd 352: } else {
353: error = ENOENT;
354: goto bad;
355: }
356:
1.14 mycroft 357: /* FALL THROUGH */
1.8 cgd 358:
359: case Fdevfd:
1.46 perry 360: if (cnp->cn_namelen == 2 && memcmp(pname, "..", 2) == 0) {
1.110 hannken 361: VOP_UNLOCK(dvp);
1.76 thorpej 362: error = fdesc_root(dvp->v_mount, vpp);
1.95 chs 363: vn_lock(dvp, LK_EXCLUSIVE | LK_RETRY);
1.44 fvdl 364: if (error)
365: goto bad;
1.48 wrstuden 366: return (error);
1.8 cgd 367: }
368:
369: fd = 0;
370: while (*pname >= '0' && *pname <= '9') {
371: fd = 10 * fd + *pname++ - '0';
1.107 ad 372: if (fd >= dt->dt_nfiles)
1.8 cgd 373: break;
374: }
1.1 cgd 375:
1.8 cgd 376: if (*pname != '\0') {
377: error = ENOENT;
378: goto bad;
379: }
1.1 cgd 380:
1.107 ad 381: if (fd >= dt->dt_nfiles || dt->dt_ff[fd] == NULL ||
382: dt->dt_ff[fd]->ff_file == NULL) {
1.8 cgd 383: error = EBADF;
384: goto bad;
385: }
1.1 cgd 386:
1.8 cgd 387: error = fdesc_allocvp(Fdesc, FD_DESC+fd, dvp->v_mount, &fvp);
388: if (error)
389: goto bad;
390: VTOFDESC(fvp)->fd_fd = fd;
1.14 mycroft 391: *vpp = fvp;
1.48 wrstuden 392: goto good;
1.8 cgd 393: }
1.1 cgd 394:
1.95 chs 395: bad:
1.14 mycroft 396: *vpp = NULL;
1.1 cgd 397: return (error);
1.48 wrstuden 398:
1.95 chs 399: good:
1.48 wrstuden 400: return (0);
1.1 cgd 401: }
402:
1.14 mycroft 403: int
1.105 dsl 404: fdesc_open(void *v)
1.30 christos 405: {
1.14 mycroft 406: struct vop_open_args /* {
407: struct vnode *a_vp;
408: int a_mode;
1.92 elad 409: kauth_cred_t a_cred;
1.30 christos 410: } */ *ap = v;
1.14 mycroft 411: struct vnode *vp = ap->a_vp;
1.8 cgd 412:
413: switch (VTOFDESC(vp)->fd_type) {
414: case Fdesc:
1.23 mycroft 415: /*
1.79 jdolecek 416: * XXX Kludge: set dupfd to contain the value of the
1.83 perry 417: * the file descriptor being sought for duplication. The error
1.23 mycroft 418: * return ensures that the vnode for this device will be
419: * released by vn_open. Open will detect this special error and
420: * take the actions in dupfdopen. Other callers of vn_open or
421: * VOP_OPEN will simply report the error.
422: */
1.79 jdolecek 423: curlwp->l_dupfd = VTOFDESC(vp)->fd_fd; /* XXX */
1.82 christos 424: return EDUPFD;
1.1 cgd 425:
1.8 cgd 426: case Fctty:
1.100 pooka 427: return cdev_open(devctty, ap->a_mode, 0, curlwp);
1.30 christos 428: case Froot:
429: case Fdevfd:
430: case Flink:
431: break;
1.8 cgd 432: }
1.1 cgd 433:
1.21 mycroft 434: return (0);
1.1 cgd 435: }
436:
437: static int
1.105 dsl 438: fdesc_attr(int fd, struct vattr *vap, kauth_cred_t cred)
1.1 cgd 439: {
1.102 ad 440: file_t *fp;
1.8 cgd 441: struct stat stb;
1.1 cgd 442: int error;
443:
1.102 ad 444: if ((fp = fd_getfile(fd)) == NULL)
1.1 cgd 445: return (EBADF);
446:
447: switch (fp->f_type) {
448: case DTYPE_VNODE:
1.100 pooka 449: error = VOP_GETATTR((struct vnode *) fp->f_data, vap, cred);
1.8 cgd 450: if (error == 0 && vap->va_type == VDIR) {
451: /*
1.22 mycroft 452: * directories can cause loops in the namespace,
453: * so turn off the 'x' bits to avoid trouble.
1.8 cgd 454: */
1.41 mycroft 455: vap->va_mode &= ~(S_IXUSR|S_IXGRP|S_IXOTH);
1.8 cgd 456: }
1.1 cgd 457: break;
458:
1.69 jdolecek 459: default:
460: memset(&stb, 0, sizeof(stb));
1.102 ad 461: error = (*fp->f_ops->fo_stat)(fp, &stb);
1.69 jdolecek 462: if (error)
463: break;
464:
465: vattr_null(vap);
466: switch(fp->f_type) {
467: case DTYPE_SOCKET:
468: vap->va_type = VSOCK;
469: break;
470: case DTYPE_PIPE:
471: vap->va_type = VFIFO;
472: break;
473: default:
474: /* use VNON perhaps? */
475: vap->va_type = VBAD;
476: break;
1.8 cgd 477: }
1.69 jdolecek 478: vap->va_mode = stb.st_mode;
479: vap->va_nlink = stb.st_nlink;
480: vap->va_uid = stb.st_uid;
481: vap->va_gid = stb.st_gid;
482: vap->va_fsid = stb.st_dev;
483: vap->va_fileid = stb.st_ino;
484: vap->va_size = stb.st_size;
485: vap->va_blocksize = stb.st_blksize;
486: vap->va_atime = stb.st_atimespec;
487: vap->va_mtime = stb.st_mtimespec;
488: vap->va_ctime = stb.st_ctimespec;
489: vap->va_gen = stb.st_gen;
490: vap->va_flags = stb.st_flags;
491: vap->va_rdev = stb.st_rdev;
492: vap->va_bytes = stb.st_blocks * stb.st_blksize;
1.1 cgd 493: break;
494: }
495:
1.102 ad 496: fd_putfile(fd);
1.1 cgd 497: return (error);
498: }
499:
1.14 mycroft 500: int
1.105 dsl 501: fdesc_getattr(void *v)
1.30 christos 502: {
1.14 mycroft 503: struct vop_getattr_args /* {
504: struct vnode *a_vp;
505: struct vattr *a_vap;
1.92 elad 506: kauth_cred_t a_cred;
1.89 christos 507: struct lwp *a_l;
1.30 christos 508: } */ *ap = v;
1.14 mycroft 509: struct vnode *vp = ap->a_vp;
510: struct vattr *vap = ap->a_vap;
1.1 cgd 511: unsigned fd;
1.8 cgd 512: int error = 0;
1.1 cgd 513:
1.8 cgd 514: switch (VTOFDESC(vp)->fd_type) {
515: case Froot:
516: case Fdevfd:
517: case Flink:
518: case Fctty:
1.109 pooka 519: vattr_null(vap);
1.8 cgd 520: vap->va_fileid = VTOFDESC(vp)->fd_ix;
521:
1.30 christos 522: #define R_ALL (S_IRUSR|S_IRGRP|S_IROTH)
523: #define W_ALL (S_IWUSR|S_IWGRP|S_IWOTH)
524: #define X_ALL (S_IXUSR|S_IXGRP|S_IXOTH)
525:
1.8 cgd 526: switch (VTOFDESC(vp)->fd_type) {
527: case Flink:
1.30 christos 528: vap->va_mode = R_ALL|X_ALL;
1.8 cgd 529: vap->va_type = VLNK;
1.32 mrg 530: vap->va_rdev = 0;
1.8 cgd 531: vap->va_nlink = 1;
532: vap->va_size = strlen(VTOFDESC(vp)->fd_link);
533: break;
534:
535: case Fctty:
1.30 christos 536: vap->va_mode = R_ALL|W_ALL;
1.32 mrg 537: vap->va_type = VCHR;
538: vap->va_rdev = devctty;
1.8 cgd 539: vap->va_nlink = 1;
540: vap->va_size = 0;
541: break;
542:
543: default:
1.30 christos 544: vap->va_mode = R_ALL|X_ALL;
1.8 cgd 545: vap->va_type = VDIR;
1.32 mrg 546: vap->va_rdev = 0;
1.8 cgd 547: vap->va_nlink = 2;
548: vap->va_size = DEV_BSIZE;
549: break;
550: }
1.1 cgd 551: vap->va_uid = 0;
552: vap->va_gid = 0;
1.80 christos 553: vap->va_fsid = vp->v_mount->mnt_stat.f_fsidx.__fsid_val[0];
1.1 cgd 554: vap->va_blocksize = DEV_BSIZE;
1.28 jtc 555: vap->va_atime.tv_sec = boottime.tv_sec;
556: vap->va_atime.tv_nsec = 0;
1.1 cgd 557: vap->va_mtime = vap->va_atime;
1.14 mycroft 558: vap->va_ctime = vap->va_mtime;
1.1 cgd 559: vap->va_gen = 0;
560: vap->va_flags = 0;
561: vap->va_bytes = 0;
1.8 cgd 562: break;
563:
564: case Fdesc:
565: fd = VTOFDESC(vp)->fd_fd;
1.102 ad 566: error = fdesc_attr(fd, vap, ap->a_cred);
1.8 cgd 567: break;
568:
569: default:
570: panic("fdesc_getattr");
1.83 perry 571: break;
1.1 cgd 572: }
573:
574: if (error == 0)
575: vp->v_type = vap->va_type;
1.8 cgd 576:
1.1 cgd 577: return (error);
578: }
579:
1.14 mycroft 580: int
1.105 dsl 581: fdesc_setattr(void *v)
1.30 christos 582: {
1.14 mycroft 583: struct vop_setattr_args /* {
584: struct vnode *a_vp;
585: struct vattr *a_vap;
1.92 elad 586: kauth_cred_t a_cred;
1.30 christos 587: } */ *ap = v;
1.102 ad 588: file_t *fp;
1.1 cgd 589: unsigned fd;
590:
591: /*
592: * Can't mess with the root vnode
593: */
1.14 mycroft 594: switch (VTOFDESC(ap->a_vp)->fd_type) {
1.8 cgd 595: case Fdesc:
596: break;
597:
598: case Fctty:
599: return (0);
600:
601: default:
1.1 cgd 602: return (EACCES);
1.8 cgd 603: }
1.1 cgd 604:
1.14 mycroft 605: fd = VTOFDESC(ap->a_vp)->fd_fd;
1.102 ad 606: if ((fp = fd_getfile(fd)) == NULL)
1.1 cgd 607: return (EBADF);
608:
609: /*
1.68 jmc 610: * XXX: Can't reasonably set the attr's on any types currently.
611: * On vnode's this will cause truncation and socket/pipes make
612: * no sense.
1.1 cgd 613: */
1.102 ad 614: fd_putfile(fd);
1.69 jdolecek 615: return (0);
1.1 cgd 616: }
617:
1.8 cgd 618:
1.25 mycroft 619: struct fdesc_target {
620: ino_t ft_fileno;
621: u_char ft_type;
622: u_char ft_namlen;
1.84 christos 623: const char *ft_name;
1.25 mycroft 624: } fdesc_targets[] = {
625: #define N(s) sizeof(s)-1, s
626: { FD_DEVFD, DT_DIR, N("fd") },
1.27 mycroft 627: { FD_STDIN, DT_LNK, N("stdin") },
628: { FD_STDOUT, DT_LNK, N("stdout") },
629: { FD_STDERR, DT_LNK, N("stderr") },
1.25 mycroft 630: { FD_CTTY, DT_UNKNOWN, N("tty") },
631: #undef N
1.85 christos 632: #define UIO_MX _DIRENT_RECLEN((struct dirent *)NULL, sizeof("stderr") - 1)
1.8 cgd 633: };
1.25 mycroft 634: static int nfdesc_targets = sizeof(fdesc_targets) / sizeof(fdesc_targets[0]);
1.8 cgd 635:
1.14 mycroft 636: int
1.105 dsl 637: fdesc_readdir(void *v)
1.30 christos 638: {
1.14 mycroft 639: struct vop_readdir_args /* {
640: struct vnode *a_vp;
641: struct uio *a_uio;
1.92 elad 642: kauth_cred_t a_cred;
1.22 mycroft 643: int *a_eofflag;
1.44 fvdl 644: off_t **a_cookies;
645: int *a_ncookies;
1.30 christos 646: } */ *ap = v;
1.14 mycroft 647: struct uio *uio = ap->a_uio;
1.25 mycroft 648: struct dirent d;
1.53 sommerfe 649: off_t i;
1.87 christos 650: int j;
1.1 cgd 651: int error;
1.44 fvdl 652: off_t *cookies = NULL;
1.87 christos 653: int ncookies;
1.107 ad 654: fdtab_t *dt;
1.11 ws 655:
1.14 mycroft 656: switch (VTOFDESC(ap->a_vp)->fd_type) {
1.8 cgd 657: case Fctty:
1.87 christos 658: return 0;
1.8 cgd 659:
660: case Fdesc:
1.87 christos 661: return ENOTDIR;
1.30 christos 662:
663: default:
664: break;
1.8 cgd 665: }
1.1 cgd 666:
1.107 ad 667: dt = curlwp->l_fd->fd_dt;
1.8 cgd 668:
1.25 mycroft 669: if (uio->uio_resid < UIO_MX)
1.87 christos 670: return EINVAL;
1.26 mycroft 671: if (uio->uio_offset < 0)
1.87 christos 672: return EINVAL;
1.25 mycroft 673:
674: error = 0;
1.26 mycroft 675: i = uio->uio_offset;
1.87 christos 676: (void)memset(&d, 0, UIO_MX);
1.25 mycroft 677: d.d_reclen = UIO_MX;
1.44 fvdl 678: if (ap->a_ncookies)
1.87 christos 679: ncookies = uio->uio_resid / UIO_MX;
680: else
681: ncookies = 0;
1.25 mycroft 682:
1.14 mycroft 683: if (VTOFDESC(ap->a_vp)->fd_type == Froot) {
1.25 mycroft 684: struct fdesc_target *ft;
1.51 christos 685:
686: if (i >= nfdesc_targets)
1.52 sommerfe 687: return 0;
1.3 cgd 688:
1.44 fvdl 689: if (ap->a_ncookies) {
690: ncookies = min(ncookies, (nfdesc_targets - i));
1.56 thorpej 691: cookies = malloc(ncookies * sizeof(off_t),
1.44 fvdl 692: M_TEMP, M_WAITOK);
693: *ap->a_cookies = cookies;
694: *ap->a_ncookies = ncookies;
695: }
696:
1.87 christos 697: for (ft = &fdesc_targets[i]; uio->uio_resid >= UIO_MX &&
698: i < nfdesc_targets; ft++, i++) {
1.25 mycroft 699: switch (ft->ft_fileno) {
1.8 cgd 700: case FD_CTTY:
1.90 yamt 701: if (cttyvp(curproc) == NULL)
1.8 cgd 702: continue;
703: break;
704:
705: case FD_STDIN:
706: case FD_STDOUT:
707: case FD_STDERR:
1.87 christos 708: if ((ft->ft_fileno - FD_STDIN) >=
1.107 ad 709: dt->dt_nfiles)
1.87 christos 710: continue;
1.107 ad 711: if (dt->dt_ff[ft->ft_fileno - FD_STDIN]
712: == NULL || dt->dt_ff[ft->ft_fileno -
1.102 ad 713: FD_STDIN]->ff_file == NULL)
1.8 cgd 714: continue;
715: break;
716: }
1.25 mycroft 717:
718: d.d_fileno = ft->ft_fileno;
719: d.d_namlen = ft->ft_namlen;
1.87 christos 720: (void)memcpy(d.d_name, ft->ft_name, ft->ft_namlen + 1);
1.25 mycroft 721: d.d_type = ft->ft_type;
722:
1.81 jrf 723: if ((error = uiomove(&d, UIO_MX, uio)) != 0)
1.8 cgd 724: break;
1.44 fvdl 725: if (cookies)
1.26 mycroft 726: *cookies++ = i + 1;
1.3 cgd 727: }
1.25 mycroft 728: } else {
1.102 ad 729: membar_consumer();
1.44 fvdl 730: if (ap->a_ncookies) {
1.107 ad 731: ncookies = min(ncookies, dt->dt_nfiles + 2);
1.56 thorpej 732: cookies = malloc(ncookies * sizeof(off_t),
733: M_TEMP, M_WAITOK);
1.44 fvdl 734: *ap->a_cookies = cookies;
735: *ap->a_ncookies = ncookies;
736: }
1.107 ad 737: for (; i - 2 < dt->dt_nfiles && uio->uio_resid >= UIO_MX; i++) {
1.25 mycroft 738: switch (i) {
739: case 0:
740: case 1:
741: d.d_fileno = FD_ROOT; /* XXX */
742: d.d_namlen = i + 1;
1.87 christos 743: (void)memcpy(d.d_name, "..", d.d_namlen);
1.25 mycroft 744: d.d_name[i + 1] = '\0';
745: d.d_type = DT_DIR;
746: break;
1.83 perry 747:
1.25 mycroft 748: default:
1.87 christos 749: j = (int)i - 2;
1.107 ad 750: if (dt->dt_ff[j] == NULL ||
751: dt->dt_ff[j]->ff_file == NULL)
1.25 mycroft 752: continue;
1.87 christos 753: d.d_fileno = j + FD_STDIN;
754: d.d_namlen = sprintf(d.d_name, "%d", j);
1.25 mycroft 755: d.d_type = DT_UNKNOWN;
756: break;
757: }
1.8 cgd 758:
1.81 jrf 759: if ((error = uiomove(&d, UIO_MX, uio)) != 0)
1.1 cgd 760: break;
1.44 fvdl 761: if (cookies)
1.26 mycroft 762: *cookies++ = i + 1;
1.1 cgd 763: }
1.8 cgd 764: }
765:
1.44 fvdl 766: if (ap->a_ncookies && error) {
1.56 thorpej 767: free(*ap->a_cookies, M_TEMP);
1.44 fvdl 768: *ap->a_ncookies = 0;
769: *ap->a_cookies = NULL;
770: }
771:
1.26 mycroft 772: uio->uio_offset = i;
1.87 christos 773: return error;
1.8 cgd 774: }
775:
1.14 mycroft 776: int
1.105 dsl 777: fdesc_readlink(void *v)
1.30 christos 778: {
1.14 mycroft 779: struct vop_readlink_args /* {
780: struct vnode *a_vp;
781: struct uio *a_uio;
1.92 elad 782: kauth_cred_t a_cred;
1.30 christos 783: } */ *ap = v;
1.14 mycroft 784: struct vnode *vp = ap->a_vp;
1.8 cgd 785: int error;
786:
1.14 mycroft 787: if (vp->v_type != VLNK)
788: return (EPERM);
789:
1.8 cgd 790: if (VTOFDESC(vp)->fd_type == Flink) {
791: char *ln = VTOFDESC(vp)->fd_link;
1.14 mycroft 792: error = uiomove(ln, strlen(ln), ap->a_uio);
1.8 cgd 793: } else {
794: error = EOPNOTSUPP;
795: }
796:
797: return (error);
798: }
799:
1.14 mycroft 800: int
1.105 dsl 801: fdesc_read(void *v)
1.30 christos 802: {
1.14 mycroft 803: struct vop_read_args /* {
804: struct vnode *a_vp;
805: struct uio *a_uio;
806: int a_ioflag;
1.92 elad 807: kauth_cred_t a_cred;
1.30 christos 808: } */ *ap = v;
1.8 cgd 809: int error = EOPNOTSUPP;
1.49 thorpej 810: struct vnode *vp = ap->a_vp;
1.8 cgd 811:
1.49 thorpej 812: switch (VTOFDESC(vp)->fd_type) {
1.8 cgd 813: case Fctty:
1.110 hannken 814: VOP_UNLOCK(vp);
1.97 ad 815: error = cdev_read(devctty, ap->a_uio, ap->a_ioflag);
1.49 thorpej 816: vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
1.8 cgd 817: break;
818:
819: default:
820: error = EOPNOTSUPP;
821: break;
822: }
1.83 perry 823:
1.8 cgd 824: return (error);
825: }
826:
1.14 mycroft 827: int
1.105 dsl 828: fdesc_write(void *v)
1.30 christos 829: {
1.14 mycroft 830: struct vop_write_args /* {
831: struct vnode *a_vp;
832: struct uio *a_uio;
833: int a_ioflag;
1.92 elad 834: kauth_cred_t a_cred;
1.30 christos 835: } */ *ap = v;
1.8 cgd 836: int error = EOPNOTSUPP;
1.49 thorpej 837: struct vnode *vp = ap->a_vp;
1.8 cgd 838:
1.49 thorpej 839: switch (VTOFDESC(vp)->fd_type) {
1.8 cgd 840: case Fctty:
1.110 hannken 841: VOP_UNLOCK(vp);
1.97 ad 842: error = cdev_write(devctty, ap->a_uio, ap->a_ioflag);
1.49 thorpej 843: vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
1.8 cgd 844: break;
845:
846: default:
847: error = EOPNOTSUPP;
848: break;
849: }
1.83 perry 850:
1.8 cgd 851: return (error);
852: }
853:
1.14 mycroft 854: int
1.105 dsl 855: fdesc_ioctl(void *v)
1.30 christos 856: {
1.14 mycroft 857: struct vop_ioctl_args /* {
858: struct vnode *a_vp;
1.19 cgd 859: u_long a_command;
1.81 jrf 860: void *a_data;
1.14 mycroft 861: int a_fflag;
1.92 elad 862: kauth_cred_t a_cred;
1.30 christos 863: } */ *ap = v;
1.8 cgd 864: int error = EOPNOTSUPP;
865:
1.14 mycroft 866: switch (VTOFDESC(ap->a_vp)->fd_type) {
1.8 cgd 867: case Fctty:
1.97 ad 868: error = cdev_ioctl(devctty, ap->a_command, ap->a_data,
1.100 pooka 869: ap->a_fflag, curlwp);
1.8 cgd 870: break;
871:
872: default:
873: error = EOPNOTSUPP;
874: break;
1.1 cgd 875: }
1.83 perry 876:
1.8 cgd 877: return (error);
878: }
879:
1.14 mycroft 880: int
1.105 dsl 881: fdesc_poll(void *v)
1.30 christos 882: {
1.35 mycroft 883: struct vop_poll_args /* {
1.14 mycroft 884: struct vnode *a_vp;
1.35 mycroft 885: int a_events;
1.30 christos 886: } */ *ap = v;
1.35 mycroft 887: int revents;
1.8 cgd 888:
1.14 mycroft 889: switch (VTOFDESC(ap->a_vp)->fd_type) {
1.8 cgd 890: case Fctty:
1.100 pooka 891: revents = cdev_poll(devctty, ap->a_events, curlwp);
1.8 cgd 892: break;
1.1 cgd 893:
1.8 cgd 894: default:
1.35 mycroft 895: revents = genfs_poll(v);
1.8 cgd 896: break;
897: }
1.34 mycroft 898:
1.35 mycroft 899: return (revents);
1.71 jdolecek 900: }
901:
902: int
1.105 dsl 903: fdesc_kqfilter(void *v)
1.71 jdolecek 904: {
905: struct vop_kqfilter_args /* {
906: struct vnode *a_vp;
907: struct knote *a_kn;
908: } */ *ap = v;
1.102 ad 909: int error, fd;
910: file_t *fp;
1.71 jdolecek 911:
912: switch (VTOFDESC(ap->a_vp)->fd_type) {
913: case Fctty:
1.97 ad 914: error = cdev_kqfilter(devctty, ap->a_kn);
1.71 jdolecek 915: break;
916:
917: case Fdesc:
918: /* just invoke kqfilter for the underlying descriptor */
1.102 ad 919: fd = VTOFDESC(ap->a_vp)->fd_fd;
920: if ((fp = fd_getfile(fd)) == NULL)
1.71 jdolecek 921: return (1);
922: error = (*fp->f_ops->fo_kqfilter)(fp, ap->a_kn);
1.102 ad 923: fd_putfile(fd);
1.71 jdolecek 924: break;
925:
926: default:
927: return (genfs_kqfilter(v));
928: }
929:
930: return (error);
1.1 cgd 931: }
932:
1.14 mycroft 933: int
1.105 dsl 934: fdesc_inactive(void *v)
1.30 christos 935: {
1.14 mycroft 936: struct vop_inactive_args /* {
937: struct vnode *a_vp;
1.30 christos 938: } */ *ap = v;
1.14 mycroft 939: struct vnode *vp = ap->a_vp;
940:
1.1 cgd 941: /*
942: * Clear out the v_type field to avoid
943: * nasty things happening in vgone().
944: */
1.110 hannken 945: VOP_UNLOCK(vp);
1.1 cgd 946: vp->v_type = VNON;
947: return (0);
948: }
949:
1.14 mycroft 950: int
1.105 dsl 951: fdesc_reclaim(void *v)
1.30 christos 952: {
1.14 mycroft 953: struct vop_reclaim_args /* {
954: struct vnode *a_vp;
1.30 christos 955: } */ *ap = v;
1.14 mycroft 956: struct vnode *vp = ap->a_vp;
1.17 mycroft 957: struct fdescnode *fd = VTOFDESC(vp);
1.14 mycroft 958:
1.111 hannken 959: mutex_enter(&fdcache_lock);
1.17 mycroft 960: LIST_REMOVE(fd, fd_hash);
1.104 cegger 961: free(vp->v_data, M_TEMP);
1.14 mycroft 962: vp->v_data = 0;
1.111 hannken 963: mutex_exit(&fdcache_lock);
1.14 mycroft 964:
965: return (0);
966: }
967:
968: /*
969: * Return POSIX pathconf information applicable to special devices.
970: */
1.30 christos 971: int
1.105 dsl 972: fdesc_pathconf(void *v)
1.30 christos 973: {
1.14 mycroft 974: struct vop_pathconf_args /* {
975: struct vnode *a_vp;
976: int a_name;
1.18 cgd 977: register_t *a_retval;
1.30 christos 978: } */ *ap = v;
1.8 cgd 979:
1.14 mycroft 980: switch (ap->a_name) {
981: case _PC_LINK_MAX:
982: *ap->a_retval = LINK_MAX;
983: return (0);
984: case _PC_MAX_CANON:
985: *ap->a_retval = MAX_CANON;
986: return (0);
987: case _PC_MAX_INPUT:
988: *ap->a_retval = MAX_INPUT;
989: return (0);
990: case _PC_PIPE_BUF:
991: *ap->a_retval = PIPE_BUF;
992: return (0);
993: case _PC_CHOWN_RESTRICTED:
994: *ap->a_retval = 1;
995: return (0);
996: case _PC_VDISABLE:
997: *ap->a_retval = _POSIX_VDISABLE;
1.45 kleink 998: return (0);
999: case _PC_SYNC_IO:
1000: *ap->a_retval = 1;
1.14 mycroft 1001: return (0);
1002: default:
1003: return (EINVAL);
1.8 cgd 1004: }
1.14 mycroft 1005: /* NOTREACHED */
1.8 cgd 1006: }
1007:
1.1 cgd 1008: /*
1009: * Print out the contents of a /dev/fd vnode.
1010: */
1011: /* ARGSUSED */
1.14 mycroft 1012: int
1.94 christos 1013: fdesc_print(void *v)
1.1 cgd 1014: {
1.37 christos 1015: printf("tag VT_NON, fdesc vnode\n");
1.14 mycroft 1016: return (0);
1017: }
1018:
1019: int
1.105 dsl 1020: fdesc_link(void *v)
1.30 christos 1021: {
1.29 mycroft 1022: struct vop_link_args /* {
1023: struct vnode *a_dvp;
1.83 perry 1024: struct vnode *a_vp;
1.29 mycroft 1025: struct componentname *a_cnp;
1.30 christos 1026: } */ *ap = v;
1.83 perry 1027:
1.29 mycroft 1028: VOP_ABORTOP(ap->a_dvp, ap->a_cnp);
1029: vput(ap->a_dvp);
1030: return (EROFS);
1031: }
1032:
1033: int
1.105 dsl 1034: fdesc_symlink(void *v)
1.30 christos 1035: {
1.29 mycroft 1036: struct vop_symlink_args /* {
1037: struct vnode *a_dvp;
1038: struct vnode **a_vpp;
1039: struct componentname *a_cnp;
1040: struct vattr *a_vap;
1041: char *a_target;
1.30 christos 1042: } */ *ap = v;
1.83 perry 1043:
1.29 mycroft 1044: VOP_ABORTOP(ap->a_dvp, ap->a_cnp);
1045: vput(ap->a_dvp);
1046: return (EROFS);
1.1 cgd 1047: }
CVSweb <webmaster@jp.NetBSD.org>