Annotation of src/sys/miscfs/fifofs/fifo_vnops.c, Revision 1.25
1.25 ! fvdl 1: /* $NetBSD: fifo_vnops.c,v 1.24 1997/10/09 13:12:01 mycroft Exp $ */
1.9 cgd 2:
1.1 cgd 3: /*
1.25 ! fvdl 4: * Copyright (c) 1990, 1993, 1995
1.8 mycroft 5: * The Regents of the University of California. All rights reserved.
1.1 cgd 6: *
7: * Redistribution and use in source and binary forms, with or without
8: * modification, are permitted provided that the following conditions
9: * are met:
10: * 1. Redistributions of source code must retain the above copyright
11: * notice, this list of conditions and the following disclaimer.
12: * 2. Redistributions in binary form must reproduce the above copyright
13: * notice, this list of conditions and the following disclaimer in the
14: * documentation and/or other materials provided with the distribution.
15: * 3. All advertising materials mentioning features or use of this software
16: * must display the following acknowledgement:
17: * This product includes software developed by the University of
18: * California, Berkeley and its contributors.
19: * 4. Neither the name of the University nor the names of its contributors
20: * may be used to endorse or promote products derived from this software
21: * without specific prior written permission.
22: *
23: * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24: * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26: * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29: * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31: * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32: * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33: * SUCH DAMAGE.
34: *
1.25 ! fvdl 35: * @(#)fifo_vnops.c 8.10 (Berkeley) 5/27/95
1.1 cgd 36: */
37:
1.6 mycroft 38: #include <sys/param.h>
1.25 ! fvdl 39: #include <sys/systm.h>
1.8 mycroft 40: #include <sys/proc.h>
1.6 mycroft 41: #include <sys/time.h>
42: #include <sys/namei.h>
43: #include <sys/vnode.h>
44: #include <sys/socket.h>
45: #include <sys/socketvar.h>
46: #include <sys/stat.h>
47: #include <sys/ioctl.h>
48: #include <sys/file.h>
49: #include <sys/errno.h>
50: #include <sys/malloc.h>
1.17 christos 51: #include <sys/un.h>
1.20 mycroft 52: #include <sys/poll.h>
1.19 mycroft 53:
1.6 mycroft 54: #include <miscfs/fifofs/fifo.h>
1.19 mycroft 55: #include <miscfs/genfs/genfs.h>
1.1 cgd 56:
57: /*
58: * This structure is associated with the FIFO vnode and stores
59: * the state associated with the FIFO.
60: */
61: struct fifoinfo {
62: struct socket *fi_readsock;
63: struct socket *fi_writesock;
64: long fi_readers;
65: long fi_writers;
66: };
67:
1.17 christos 68: int (**fifo_vnodeop_p) __P((void *));
1.8 mycroft 69: struct vnodeopv_entry_desc fifo_vnodeop_entries[] = {
70: { &vop_default_desc, vn_default_error },
71: { &vop_lookup_desc, fifo_lookup }, /* lookup */
72: { &vop_create_desc, fifo_create }, /* create */
73: { &vop_mknod_desc, fifo_mknod }, /* mknod */
74: { &vop_open_desc, fifo_open }, /* open */
75: { &vop_close_desc, fifo_close }, /* close */
76: { &vop_access_desc, fifo_access }, /* access */
77: { &vop_getattr_desc, fifo_getattr }, /* getattr */
78: { &vop_setattr_desc, fifo_setattr }, /* setattr */
79: { &vop_read_desc, fifo_read }, /* read */
80: { &vop_write_desc, fifo_write }, /* write */
1.13 mycroft 81: { &vop_lease_desc, fifo_lease_check }, /* lease */
1.8 mycroft 82: { &vop_ioctl_desc, fifo_ioctl }, /* ioctl */
1.20 mycroft 83: { &vop_poll_desc, fifo_poll }, /* poll */
1.25 ! fvdl 84: { &vop_revoke_desc, fifo_revoke }, /* revoke */
1.8 mycroft 85: { &vop_mmap_desc, fifo_mmap }, /* mmap */
86: { &vop_fsync_desc, fifo_fsync }, /* fsync */
87: { &vop_seek_desc, fifo_seek }, /* seek */
88: { &vop_remove_desc, fifo_remove }, /* remove */
89: { &vop_link_desc, fifo_link }, /* link */
90: { &vop_rename_desc, fifo_rename }, /* rename */
91: { &vop_mkdir_desc, fifo_mkdir }, /* mkdir */
92: { &vop_rmdir_desc, fifo_rmdir }, /* rmdir */
93: { &vop_symlink_desc, fifo_symlink }, /* symlink */
94: { &vop_readdir_desc, fifo_readdir }, /* readdir */
95: { &vop_readlink_desc, fifo_readlink }, /* readlink */
96: { &vop_abortop_desc, fifo_abortop }, /* abortop */
97: { &vop_inactive_desc, fifo_inactive }, /* inactive */
98: { &vop_reclaim_desc, fifo_reclaim }, /* reclaim */
99: { &vop_lock_desc, fifo_lock }, /* lock */
100: { &vop_unlock_desc, fifo_unlock }, /* unlock */
101: { &vop_bmap_desc, fifo_bmap }, /* bmap */
102: { &vop_strategy_desc, fifo_strategy }, /* strategy */
103: { &vop_print_desc, fifo_print }, /* print */
104: { &vop_islocked_desc, fifo_islocked }, /* islocked */
105: { &vop_pathconf_desc, fifo_pathconf }, /* pathconf */
106: { &vop_advlock_desc, fifo_advlock }, /* advlock */
107: { &vop_blkatoff_desc, fifo_blkatoff }, /* blkatoff */
108: { &vop_valloc_desc, fifo_valloc }, /* valloc */
109: { &vop_vfree_desc, fifo_vfree }, /* vfree */
110: { &vop_truncate_desc, fifo_truncate }, /* truncate */
111: { &vop_update_desc, fifo_update }, /* update */
112: { &vop_bwrite_desc, fifo_bwrite }, /* bwrite */
1.17 christos 113: { (struct vnodeop_desc*)NULL, (int(*) __P((void *)))NULL }
1.1 cgd 114: };
1.8 mycroft 115: struct vnodeopv_desc fifo_vnodeop_opv_desc =
116: { &fifo_vnodeop_p, fifo_vnodeop_entries };
1.1 cgd 117:
118: /*
119: * Trivial lookup routine that always fails.
120: */
121: /* ARGSUSED */
1.17 christos 122: int
123: fifo_lookup(v)
124: void *v;
125: {
1.8 mycroft 126: struct vop_lookup_args /* {
127: struct vnode * a_dvp;
128: struct vnode ** a_vpp;
129: struct componentname * a_cnp;
1.17 christos 130: } */ *ap = v;
1.8 mycroft 131:
132: *ap->a_vpp = NULL;
1.1 cgd 133: return (ENOTDIR);
134: }
135:
136: /*
137: * Open called to set up a new instance of a fifo or
138: * to find an active instance of a fifo.
139: */
140: /* ARGSUSED */
1.17 christos 141: int
142: fifo_open(v)
143: void *v;
144: {
1.8 mycroft 145: struct vop_open_args /* {
146: struct vnode *a_vp;
147: int a_mode;
148: struct ucred *a_cred;
149: struct proc *a_p;
1.17 christos 150: } */ *ap = v;
1.25 ! fvdl 151: struct vnode *vp = ap->a_vp;
! 152: struct fifoinfo *fip;
! 153: struct proc *p = ap->a_p;
1.1 cgd 154: struct socket *rso, *wso;
155: int error;
1.24 mycroft 156: static const char openstr[] = "fifo";
1.1 cgd 157:
158: if ((fip = vp->v_fifoinfo) == NULL) {
159: MALLOC(fip, struct fifoinfo *, sizeof(*fip), M_VNODE, M_WAITOK);
160: vp->v_fifoinfo = fip;
1.17 christos 161: if ((error = socreate(AF_LOCAL, &rso, SOCK_STREAM, 0)) != 0) {
1.1 cgd 162: free(fip, M_VNODE);
163: vp->v_fifoinfo = NULL;
164: return (error);
165: }
166: fip->fi_readsock = rso;
1.17 christos 167: if ((error = socreate(AF_LOCAL, &wso, SOCK_STREAM, 0)) != 0) {
1.1 cgd 168: (void)soclose(rso);
169: free(fip, M_VNODE);
170: vp->v_fifoinfo = NULL;
171: return (error);
172: }
173: fip->fi_writesock = wso;
1.17 christos 174: if ((error = unp_connect2(wso, rso)) != 0) {
1.1 cgd 175: (void)soclose(wso);
176: (void)soclose(rso);
177: free(fip, M_VNODE);
178: vp->v_fifoinfo = NULL;
179: return (error);
180: }
1.8 mycroft 181: fip->fi_readers = fip->fi_writers = 0;
1.1 cgd 182: wso->so_state |= SS_CANTRCVMORE;
183: rso->so_state |= SS_CANTSENDMORE;
184: }
1.8 mycroft 185: if (ap->a_mode & FREAD) {
1.16 mycroft 186: if (fip->fi_readers++ == 0) {
1.1 cgd 187: fip->fi_writesock->so_state &= ~SS_CANTSENDMORE;
188: if (fip->fi_writers > 0)
189: wakeup((caddr_t)&fip->fi_writers);
190: }
1.16 mycroft 191: }
192: if (ap->a_mode & FWRITE) {
193: if (fip->fi_writers++ == 0) {
194: fip->fi_readsock->so_state &= ~SS_CANTRCVMORE;
195: if (fip->fi_readers > 0)
196: wakeup((caddr_t)&fip->fi_readers);
1.2 cgd 197: }
1.16 mycroft 198: }
199: if (ap->a_mode & FREAD) {
200: if (ap->a_mode & O_NONBLOCK) {
1.1 cgd 201: } else {
1.16 mycroft 202: while (fip->fi_writers == 0) {
1.25 ! fvdl 203: VOP_UNLOCK(vp, 0);
1.16 mycroft 204: error = tsleep((caddr_t)&fip->fi_readers,
205: PCATCH | PSOCK, openstr, 0);
1.25 ! fvdl 206: vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
1.16 mycroft 207: if (error)
208: goto bad;
1.1 cgd 209: }
1.16 mycroft 210: }
211: }
212: if (ap->a_mode & FWRITE) {
213: if (ap->a_mode & O_NONBLOCK) {
214: if (fip->fi_readers == 0) {
215: error = ENXIO;
216: goto bad;
217: }
218: } else {
1.2 cgd 219: while (fip->fi_readers == 0) {
1.25 ! fvdl 220: VOP_UNLOCK(vp, 0);
1.2 cgd 221: error = tsleep((caddr_t)&fip->fi_writers,
1.8 mycroft 222: PCATCH | PSOCK, openstr, 0);
1.25 ! fvdl 223: vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
1.8 mycroft 224: if (error)
1.16 mycroft 225: goto bad;
1.2 cgd 226: }
1.1 cgd 227: }
228: }
1.16 mycroft 229: return (0);
230: bad:
1.25 ! fvdl 231: VOP_CLOSE(vp, ap->a_mode, ap->a_cred, p);
1.1 cgd 232: return (error);
233: }
234:
235: /*
236: * Vnode op for read
237: */
238: /* ARGSUSED */
1.17 christos 239: int
240: fifo_read(v)
241: void *v;
242: {
1.8 mycroft 243: struct vop_read_args /* {
244: struct vnode *a_vp;
245: struct uio *a_uio;
246: int a_ioflag;
247: struct ucred *a_cred;
1.17 christos 248: } */ *ap = v;
1.25 ! fvdl 249: struct uio *uio = ap->a_uio;
! 250: struct socket *rso = ap->a_vp->v_fifoinfo->fi_readsock;
1.1 cgd 251: int error, startresid;
252:
253: #ifdef DIAGNOSTIC
254: if (uio->uio_rw != UIO_READ)
255: panic("fifo_read mode");
256: #endif
257: if (uio->uio_resid == 0)
258: return (0);
1.8 mycroft 259: if (ap->a_ioflag & IO_NDELAY)
1.1 cgd 260: rso->so_state |= SS_NBIO;
261: startresid = uio->uio_resid;
1.25 ! fvdl 262: VOP_UNLOCK(ap->a_vp, 0);
1.13 mycroft 263: error = soreceive(rso, (struct mbuf **)0, uio, (struct mbuf **)0,
264: (struct mbuf **)0, (int *)0);
1.25 ! fvdl 265: vn_lock(ap->a_vp, LK_EXCLUSIVE | LK_RETRY);
1.1 cgd 266: /*
267: * Clear EOF indication after first such return.
268: */
269: if (uio->uio_resid == startresid)
270: rso->so_state &= ~SS_CANTRCVMORE;
1.23 kleink 271: if (ap->a_ioflag & IO_NDELAY) {
1.1 cgd 272: rso->so_state &= ~SS_NBIO;
1.23 kleink 273: if (error == EWOULDBLOCK &&
274: ap->a_vp->v_fifoinfo->fi_writers == 0)
275: error = 0;
276: }
1.1 cgd 277: return (error);
278: }
279:
280: /*
281: * Vnode op for write
282: */
283: /* ARGSUSED */
1.17 christos 284: int
285: fifo_write(v)
286: void *v;
287: {
1.8 mycroft 288: struct vop_write_args /* {
289: struct vnode *a_vp;
290: struct uio *a_uio;
291: int a_ioflag;
292: struct ucred *a_cred;
1.17 christos 293: } */ *ap = v;
1.8 mycroft 294: struct socket *wso = ap->a_vp->v_fifoinfo->fi_writesock;
1.1 cgd 295: int error;
296:
297: #ifdef DIAGNOSTIC
1.8 mycroft 298: if (ap->a_uio->uio_rw != UIO_WRITE)
1.1 cgd 299: panic("fifo_write mode");
300: #endif
1.8 mycroft 301: if (ap->a_ioflag & IO_NDELAY)
1.1 cgd 302: wso->so_state |= SS_NBIO;
1.25 ! fvdl 303: VOP_UNLOCK(ap->a_vp, 0);
1.8 mycroft 304: error = sosend(wso, (struct mbuf *)0, ap->a_uio, 0, (struct mbuf *)0, 0);
1.25 ! fvdl 305: vn_lock(ap->a_vp, LK_EXCLUSIVE | LK_RETRY);
1.8 mycroft 306: if (ap->a_ioflag & IO_NDELAY)
1.1 cgd 307: wso->so_state &= ~SS_NBIO;
308: return (error);
309: }
310:
311: /*
312: * Device ioctl operation.
313: */
314: /* ARGSUSED */
1.17 christos 315: int
316: fifo_ioctl(v)
317: void *v;
318: {
1.8 mycroft 319: struct vop_ioctl_args /* {
320: struct vnode *a_vp;
1.11 cgd 321: u_long a_command;
1.8 mycroft 322: caddr_t a_data;
323: int a_fflag;
324: struct ucred *a_cred;
325: struct proc *a_p;
1.17 christos 326: } */ *ap = v;
1.1 cgd 327: struct file filetmp;
1.16 mycroft 328: int error;
1.1 cgd 329:
1.8 mycroft 330: if (ap->a_command == FIONBIO)
1.1 cgd 331: return (0);
1.16 mycroft 332: if (ap->a_fflag & FREAD) {
1.8 mycroft 333: filetmp.f_data = (caddr_t)ap->a_vp->v_fifoinfo->fi_readsock;
1.16 mycroft 334: error = soo_ioctl(&filetmp, ap->a_command, ap->a_data, ap->a_p);
335: if (error)
336: return (error);
337: }
338: if (ap->a_fflag & FWRITE) {
1.8 mycroft 339: filetmp.f_data = (caddr_t)ap->a_vp->v_fifoinfo->fi_writesock;
1.16 mycroft 340: error = soo_ioctl(&filetmp, ap->a_command, ap->a_data, ap->a_p);
341: if (error)
342: return (error);
343: }
344: return (0);
1.1 cgd 345: }
346:
347: /* ARGSUSED */
1.17 christos 348: int
1.20 mycroft 349: fifo_poll(v)
1.17 christos 350: void *v;
351: {
1.20 mycroft 352: struct vop_poll_args /* {
1.8 mycroft 353: struct vnode *a_vp;
1.20 mycroft 354: int a_events;
1.8 mycroft 355: struct proc *a_p;
1.17 christos 356: } */ *ap = v;
1.1 cgd 357: struct file filetmp;
1.20 mycroft 358: int revents = 0;
1.1 cgd 359:
1.20 mycroft 360: if (ap->a_events & (POLLIN | POLLPRI | POLLRDNORM | POLLRDBAND)) {
1.8 mycroft 361: filetmp.f_data = (caddr_t)ap->a_vp->v_fifoinfo->fi_readsock;
1.20 mycroft 362: if (filetmp.f_data)
363: revents |= soo_poll(&filetmp, ap->a_events, ap->a_p);
1.16 mycroft 364: }
1.20 mycroft 365: if (ap->a_events & (POLLOUT | POLLWRNORM | POLLWRBAND)) {
1.8 mycroft 366: filetmp.f_data = (caddr_t)ap->a_vp->v_fifoinfo->fi_writesock;
1.20 mycroft 367: if (filetmp.f_data)
368: revents |= soo_poll(&filetmp, ap->a_events, ap->a_p);
1.16 mycroft 369: }
1.20 mycroft 370:
371: return (revents);
1.1 cgd 372: }
373:
1.25 ! fvdl 374: int
! 375: fifo_inactive(v)
! 376: void *v;
! 377: {
! 378: struct vop_inactive_args /* {
! 379: struct vnode *a_vp;
! 380: struct proc *a_p;
! 381: } */ *ap = v;
! 382:
! 383: VOP_UNLOCK(ap->a_vp, 0);
! 384: return (0);
! 385: }
! 386:
1.1 cgd 387: /*
388: * This is a noop, simply returning what one has been given.
389: */
1.17 christos 390: int
391: fifo_bmap(v)
392: void *v;
393: {
1.8 mycroft 394: struct vop_bmap_args /* {
395: struct vnode *a_vp;
396: daddr_t a_bn;
397: struct vnode **a_vpp;
398: daddr_t *a_bnp;
1.25 ! fvdl 399: int *a_runp;
1.17 christos 400: } */ *ap = v;
1.1 cgd 401:
1.8 mycroft 402: if (ap->a_vpp != NULL)
403: *ap->a_vpp = ap->a_vp;
404: if (ap->a_bnp != NULL)
405: *ap->a_bnp = ap->a_bn;
1.25 ! fvdl 406: if (ap->a_runp != NULL)
! 407: *ap->a_runp = 0;
1.1 cgd 408: return (0);
409: }
410:
411: /*
412: * Device close routine
413: */
414: /* ARGSUSED */
1.17 christos 415: int
416: fifo_close(v)
417: void *v;
418: {
1.8 mycroft 419: struct vop_close_args /* {
420: struct vnode *a_vp;
421: int a_fflag;
422: struct ucred *a_cred;
423: struct proc *a_p;
1.17 christos 424: } */ *ap = v;
1.8 mycroft 425: register struct vnode *vp = ap->a_vp;
1.1 cgd 426: register struct fifoinfo *fip = vp->v_fifoinfo;
427: int error1, error2;
428:
1.16 mycroft 429: if (ap->a_fflag & FREAD) {
430: if (--fip->fi_readers == 0)
431: socantsendmore(fip->fi_writesock);
432: }
1.8 mycroft 433: if (ap->a_fflag & FWRITE) {
1.16 mycroft 434: if (--fip->fi_writers == 0)
1.1 cgd 435: socantrcvmore(fip->fi_readsock);
436: }
437: if (vp->v_usecount > 1)
438: return (0);
439: error1 = soclose(fip->fi_readsock);
440: error2 = soclose(fip->fi_writesock);
441: FREE(fip, M_VNODE);
442: vp->v_fifoinfo = NULL;
443: if (error1)
444: return (error1);
445: return (error2);
446: }
447:
448: /*
449: * Print out the contents of a fifo vnode.
450: */
1.17 christos 451: int
452: fifo_print(v)
453: void *v;
454: {
1.8 mycroft 455: struct vop_print_args /* {
456: struct vnode *a_vp;
1.17 christos 457: } */ *ap = v;
1.1 cgd 458:
1.22 christos 459: printf("tag VT_NON");
1.8 mycroft 460: fifo_printinfo(ap->a_vp);
1.22 christos 461: printf("\n");
1.17 christos 462: return 0;
1.1 cgd 463: }
464:
465: /*
466: * Print out internal contents of a fifo vnode.
467: */
1.17 christos 468: void
1.1 cgd 469: fifo_printinfo(vp)
470: struct vnode *vp;
471: {
472: register struct fifoinfo *fip = vp->v_fifoinfo;
473:
1.22 christos 474: printf(", fifo with %ld readers and %ld writers",
1.21 christos 475: fip->fi_readers, fip->fi_writers);
1.1 cgd 476: }
477:
478: /*
1.8 mycroft 479: * Return POSIX pathconf information applicable to fifo's.
480: */
1.17 christos 481: int
482: fifo_pathconf(v)
483: void *v;
484: {
1.8 mycroft 485: struct vop_pathconf_args /* {
486: struct vnode *a_vp;
487: int a_name;
1.10 cgd 488: register_t *a_retval;
1.17 christos 489: } */ *ap = v;
1.8 mycroft 490:
491: switch (ap->a_name) {
492: case _PC_LINK_MAX:
493: *ap->a_retval = LINK_MAX;
494: return (0);
495: case _PC_PIPE_BUF:
496: *ap->a_retval = PIPE_BUF;
497: return (0);
498: case _PC_CHOWN_RESTRICTED:
499: *ap->a_retval = 1;
500: return (0);
501: default:
502: return (EINVAL);
503: }
1.1 cgd 504: /* NOTREACHED */
505: }
CVSweb <webmaster@jp.NetBSD.org>