Annotation of src/sys/miscfs/fifofs/fifo_vnops.c, Revision 1.24
1.24 ! mycroft 1: /* $NetBSD: fifo_vnops.c,v 1.23 1997/05/18 12:19:29 kleink Exp $ */
1.9 cgd 2:
1.1 cgd 3: /*
1.8 mycroft 4: * Copyright (c) 1990, 1993
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.13 mycroft 35: * @(#)fifo_vnops.c 8.4 (Berkeley) 8/10/94
1.1 cgd 36: */
37:
1.6 mycroft 38: #include <sys/param.h>
1.8 mycroft 39: #include <sys/proc.h>
1.6 mycroft 40: #include <sys/time.h>
41: #include <sys/namei.h>
42: #include <sys/vnode.h>
43: #include <sys/socket.h>
44: #include <sys/socketvar.h>
45: #include <sys/stat.h>
46: #include <sys/systm.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.8 mycroft 84: { &vop_mmap_desc, fifo_mmap }, /* mmap */
85: { &vop_fsync_desc, fifo_fsync }, /* fsync */
86: { &vop_seek_desc, fifo_seek }, /* seek */
87: { &vop_remove_desc, fifo_remove }, /* remove */
88: { &vop_link_desc, fifo_link }, /* link */
89: { &vop_rename_desc, fifo_rename }, /* rename */
90: { &vop_mkdir_desc, fifo_mkdir }, /* mkdir */
91: { &vop_rmdir_desc, fifo_rmdir }, /* rmdir */
92: { &vop_symlink_desc, fifo_symlink }, /* symlink */
93: { &vop_readdir_desc, fifo_readdir }, /* readdir */
94: { &vop_readlink_desc, fifo_readlink }, /* readlink */
95: { &vop_abortop_desc, fifo_abortop }, /* abortop */
96: { &vop_inactive_desc, fifo_inactive }, /* inactive */
97: { &vop_reclaim_desc, fifo_reclaim }, /* reclaim */
98: { &vop_lock_desc, fifo_lock }, /* lock */
99: { &vop_unlock_desc, fifo_unlock }, /* unlock */
100: { &vop_bmap_desc, fifo_bmap }, /* bmap */
101: { &vop_strategy_desc, fifo_strategy }, /* strategy */
102: { &vop_print_desc, fifo_print }, /* print */
103: { &vop_islocked_desc, fifo_islocked }, /* islocked */
104: { &vop_pathconf_desc, fifo_pathconf }, /* pathconf */
105: { &vop_advlock_desc, fifo_advlock }, /* advlock */
106: { &vop_blkatoff_desc, fifo_blkatoff }, /* blkatoff */
107: { &vop_valloc_desc, fifo_valloc }, /* valloc */
108: { &vop_vfree_desc, fifo_vfree }, /* vfree */
109: { &vop_truncate_desc, fifo_truncate }, /* truncate */
110: { &vop_update_desc, fifo_update }, /* update */
111: { &vop_bwrite_desc, fifo_bwrite }, /* bwrite */
1.17 christos 112: { (struct vnodeop_desc*)NULL, (int(*) __P((void *)))NULL }
1.1 cgd 113: };
1.8 mycroft 114: struct vnodeopv_desc fifo_vnodeop_opv_desc =
115: { &fifo_vnodeop_p, fifo_vnodeop_entries };
1.1 cgd 116:
117: /*
118: * Trivial lookup routine that always fails.
119: */
120: /* ARGSUSED */
1.17 christos 121: int
122: fifo_lookup(v)
123: void *v;
124: {
1.8 mycroft 125: struct vop_lookup_args /* {
126: struct vnode * a_dvp;
127: struct vnode ** a_vpp;
128: struct componentname * a_cnp;
1.17 christos 129: } */ *ap = v;
1.8 mycroft 130:
131: *ap->a_vpp = NULL;
1.1 cgd 132: return (ENOTDIR);
133: }
134:
135: /*
136: * Open called to set up a new instance of a fifo or
137: * to find an active instance of a fifo.
138: */
139: /* ARGSUSED */
1.17 christos 140: int
141: fifo_open(v)
142: void *v;
143: {
1.8 mycroft 144: struct vop_open_args /* {
145: struct vnode *a_vp;
146: int a_mode;
147: struct ucred *a_cred;
148: struct proc *a_p;
1.17 christos 149: } */ *ap = v;
1.8 mycroft 150: register struct vnode *vp = ap->a_vp;
1.1 cgd 151: register struct fifoinfo *fip;
152: struct socket *rso, *wso;
153: int error;
1.24 ! mycroft 154: static const char openstr[] = "fifo";
1.1 cgd 155:
156: if ((fip = vp->v_fifoinfo) == NULL) {
157: MALLOC(fip, struct fifoinfo *, sizeof(*fip), M_VNODE, M_WAITOK);
158: vp->v_fifoinfo = fip;
1.17 christos 159: if ((error = socreate(AF_LOCAL, &rso, SOCK_STREAM, 0)) != 0) {
1.1 cgd 160: free(fip, M_VNODE);
161: vp->v_fifoinfo = NULL;
162: return (error);
163: }
164: fip->fi_readsock = rso;
1.17 christos 165: if ((error = socreate(AF_LOCAL, &wso, SOCK_STREAM, 0)) != 0) {
1.1 cgd 166: (void)soclose(rso);
167: free(fip, M_VNODE);
168: vp->v_fifoinfo = NULL;
169: return (error);
170: }
171: fip->fi_writesock = wso;
1.17 christos 172: if ((error = unp_connect2(wso, rso)) != 0) {
1.1 cgd 173: (void)soclose(wso);
174: (void)soclose(rso);
175: free(fip, M_VNODE);
176: vp->v_fifoinfo = NULL;
177: return (error);
178: }
1.8 mycroft 179: fip->fi_readers = fip->fi_writers = 0;
1.1 cgd 180: wso->so_state |= SS_CANTRCVMORE;
181: rso->so_state |= SS_CANTSENDMORE;
182: }
1.8 mycroft 183: if (ap->a_mode & FREAD) {
1.16 mycroft 184: if (fip->fi_readers++ == 0) {
1.1 cgd 185: fip->fi_writesock->so_state &= ~SS_CANTSENDMORE;
186: if (fip->fi_writers > 0)
187: wakeup((caddr_t)&fip->fi_writers);
188: }
1.16 mycroft 189: }
190: if (ap->a_mode & FWRITE) {
191: if (fip->fi_writers++ == 0) {
192: fip->fi_readsock->so_state &= ~SS_CANTRCVMORE;
193: if (fip->fi_readers > 0)
194: wakeup((caddr_t)&fip->fi_readers);
1.2 cgd 195: }
1.16 mycroft 196: }
197: if (ap->a_mode & FREAD) {
198: if (ap->a_mode & O_NONBLOCK) {
1.1 cgd 199: } else {
1.16 mycroft 200: while (fip->fi_writers == 0) {
201: VOP_UNLOCK(vp);
202: error = tsleep((caddr_t)&fip->fi_readers,
203: PCATCH | PSOCK, openstr, 0);
204: VOP_LOCK(vp);
205: if (error)
206: goto bad;
1.1 cgd 207: }
1.16 mycroft 208: }
209: }
210: if (ap->a_mode & FWRITE) {
211: if (ap->a_mode & O_NONBLOCK) {
212: if (fip->fi_readers == 0) {
213: error = ENXIO;
214: goto bad;
215: }
216: } else {
1.2 cgd 217: while (fip->fi_readers == 0) {
218: VOP_UNLOCK(vp);
219: error = tsleep((caddr_t)&fip->fi_writers,
1.8 mycroft 220: PCATCH | PSOCK, openstr, 0);
1.2 cgd 221: VOP_LOCK(vp);
1.8 mycroft 222: if (error)
1.16 mycroft 223: goto bad;
1.2 cgd 224: }
1.1 cgd 225: }
226: }
1.16 mycroft 227: return (0);
228: bad:
229: VOP_CLOSE(vp, ap->a_mode, ap->a_cred, ap->a_p);
1.1 cgd 230: return (error);
231: }
232:
233: /*
234: * Vnode op for read
235: */
236: /* ARGSUSED */
1.17 christos 237: int
238: fifo_read(v)
239: void *v;
240: {
1.8 mycroft 241: struct vop_read_args /* {
242: struct vnode *a_vp;
243: struct uio *a_uio;
244: int a_ioflag;
245: struct ucred *a_cred;
1.17 christos 246: } */ *ap = v;
1.8 mycroft 247: register struct uio *uio = ap->a_uio;
248: register struct socket *rso = ap->a_vp->v_fifoinfo->fi_readsock;
1.1 cgd 249: int error, startresid;
250:
251: #ifdef DIAGNOSTIC
252: if (uio->uio_rw != UIO_READ)
253: panic("fifo_read mode");
254: #endif
255: if (uio->uio_resid == 0)
256: return (0);
1.8 mycroft 257: if (ap->a_ioflag & IO_NDELAY)
1.1 cgd 258: rso->so_state |= SS_NBIO;
259: startresid = uio->uio_resid;
1.8 mycroft 260: VOP_UNLOCK(ap->a_vp);
1.13 mycroft 261: error = soreceive(rso, (struct mbuf **)0, uio, (struct mbuf **)0,
262: (struct mbuf **)0, (int *)0);
1.8 mycroft 263: VOP_LOCK(ap->a_vp);
1.1 cgd 264: /*
265: * Clear EOF indication after first such return.
266: */
267: if (uio->uio_resid == startresid)
268: rso->so_state &= ~SS_CANTRCVMORE;
1.23 kleink 269: if (ap->a_ioflag & IO_NDELAY) {
1.1 cgd 270: rso->so_state &= ~SS_NBIO;
1.23 kleink 271: if (error == EWOULDBLOCK &&
272: ap->a_vp->v_fifoinfo->fi_writers == 0)
273: error = 0;
274: }
1.1 cgd 275: return (error);
276: }
277:
278: /*
279: * Vnode op for write
280: */
281: /* ARGSUSED */
1.17 christos 282: int
283: fifo_write(v)
284: void *v;
285: {
1.8 mycroft 286: struct vop_write_args /* {
287: struct vnode *a_vp;
288: struct uio *a_uio;
289: int a_ioflag;
290: struct ucred *a_cred;
1.17 christos 291: } */ *ap = v;
1.8 mycroft 292: struct socket *wso = ap->a_vp->v_fifoinfo->fi_writesock;
1.1 cgd 293: int error;
294:
295: #ifdef DIAGNOSTIC
1.8 mycroft 296: if (ap->a_uio->uio_rw != UIO_WRITE)
1.1 cgd 297: panic("fifo_write mode");
298: #endif
1.8 mycroft 299: if (ap->a_ioflag & IO_NDELAY)
1.1 cgd 300: wso->so_state |= SS_NBIO;
1.8 mycroft 301: VOP_UNLOCK(ap->a_vp);
302: error = sosend(wso, (struct mbuf *)0, ap->a_uio, 0, (struct mbuf *)0, 0);
303: VOP_LOCK(ap->a_vp);
304: if (ap->a_ioflag & IO_NDELAY)
1.1 cgd 305: wso->so_state &= ~SS_NBIO;
306: return (error);
307: }
308:
309: /*
310: * Device ioctl operation.
311: */
312: /* ARGSUSED */
1.17 christos 313: int
314: fifo_ioctl(v)
315: void *v;
316: {
1.8 mycroft 317: struct vop_ioctl_args /* {
318: struct vnode *a_vp;
1.11 cgd 319: u_long a_command;
1.8 mycroft 320: caddr_t a_data;
321: int a_fflag;
322: struct ucred *a_cred;
323: struct proc *a_p;
1.17 christos 324: } */ *ap = v;
1.1 cgd 325: struct file filetmp;
1.16 mycroft 326: int error;
1.1 cgd 327:
1.8 mycroft 328: if (ap->a_command == FIONBIO)
1.1 cgd 329: return (0);
1.16 mycroft 330: if (ap->a_fflag & FREAD) {
1.8 mycroft 331: filetmp.f_data = (caddr_t)ap->a_vp->v_fifoinfo->fi_readsock;
1.16 mycroft 332: error = soo_ioctl(&filetmp, ap->a_command, ap->a_data, ap->a_p);
333: if (error)
334: return (error);
335: }
336: if (ap->a_fflag & FWRITE) {
1.8 mycroft 337: filetmp.f_data = (caddr_t)ap->a_vp->v_fifoinfo->fi_writesock;
1.16 mycroft 338: error = soo_ioctl(&filetmp, ap->a_command, ap->a_data, ap->a_p);
339: if (error)
340: return (error);
341: }
342: return (0);
1.1 cgd 343: }
344:
345: /* ARGSUSED */
1.17 christos 346: int
1.20 mycroft 347: fifo_poll(v)
1.17 christos 348: void *v;
349: {
1.20 mycroft 350: struct vop_poll_args /* {
1.8 mycroft 351: struct vnode *a_vp;
1.20 mycroft 352: int a_events;
1.8 mycroft 353: struct proc *a_p;
1.17 christos 354: } */ *ap = v;
1.1 cgd 355: struct file filetmp;
1.20 mycroft 356: int revents = 0;
1.1 cgd 357:
1.20 mycroft 358: if (ap->a_events & (POLLIN | POLLPRI | POLLRDNORM | POLLRDBAND)) {
1.8 mycroft 359: filetmp.f_data = (caddr_t)ap->a_vp->v_fifoinfo->fi_readsock;
1.20 mycroft 360: if (filetmp.f_data)
361: revents |= soo_poll(&filetmp, ap->a_events, ap->a_p);
1.16 mycroft 362: }
1.20 mycroft 363: if (ap->a_events & (POLLOUT | POLLWRNORM | POLLWRBAND)) {
1.8 mycroft 364: filetmp.f_data = (caddr_t)ap->a_vp->v_fifoinfo->fi_writesock;
1.20 mycroft 365: if (filetmp.f_data)
366: revents |= soo_poll(&filetmp, ap->a_events, ap->a_p);
1.16 mycroft 367: }
1.20 mycroft 368:
369: return (revents);
1.1 cgd 370: }
371:
372: /*
373: * This is a noop, simply returning what one has been given.
374: */
1.17 christos 375: int
376: fifo_bmap(v)
377: void *v;
378: {
1.8 mycroft 379: struct vop_bmap_args /* {
380: struct vnode *a_vp;
381: daddr_t a_bn;
382: struct vnode **a_vpp;
383: daddr_t *a_bnp;
1.17 christos 384: } */ *ap = v;
1.1 cgd 385:
1.8 mycroft 386: if (ap->a_vpp != NULL)
387: *ap->a_vpp = ap->a_vp;
388: if (ap->a_bnp != NULL)
389: *ap->a_bnp = ap->a_bn;
1.1 cgd 390: return (0);
391: }
392:
393: /*
394: * At the moment we do not do any locking.
395: */
396: /* ARGSUSED */
1.17 christos 397: int
398: fifo_lock(v)
399: void *v;
1.1 cgd 400: {
401: return (0);
402: }
403:
404: /* ARGSUSED */
1.17 christos 405: int
406: fifo_unlock(v)
407: void *v;
1.1 cgd 408: {
409:
410: return (0);
411: }
412:
413: /*
414: * Device close routine
415: */
416: /* ARGSUSED */
1.17 christos 417: int
418: fifo_close(v)
419: void *v;
420: {
1.8 mycroft 421: struct vop_close_args /* {
422: struct vnode *a_vp;
423: int a_fflag;
424: struct ucred *a_cred;
425: struct proc *a_p;
1.17 christos 426: } */ *ap = v;
1.8 mycroft 427: register struct vnode *vp = ap->a_vp;
1.1 cgd 428: register struct fifoinfo *fip = vp->v_fifoinfo;
429: int error1, error2;
430:
1.16 mycroft 431: if (ap->a_fflag & FREAD) {
432: if (--fip->fi_readers == 0)
433: socantsendmore(fip->fi_writesock);
434: }
1.8 mycroft 435: if (ap->a_fflag & FWRITE) {
1.16 mycroft 436: if (--fip->fi_writers == 0)
1.1 cgd 437: socantrcvmore(fip->fi_readsock);
438: }
439: if (vp->v_usecount > 1)
440: return (0);
441: error1 = soclose(fip->fi_readsock);
442: error2 = soclose(fip->fi_writesock);
443: FREE(fip, M_VNODE);
444: vp->v_fifoinfo = NULL;
445: if (error1)
446: return (error1);
447: return (error2);
448: }
449:
450: /*
451: * Print out the contents of a fifo vnode.
452: */
1.17 christos 453: int
454: fifo_print(v)
455: void *v;
456: {
1.8 mycroft 457: struct vop_print_args /* {
458: struct vnode *a_vp;
1.17 christos 459: } */ *ap = v;
1.1 cgd 460:
1.22 christos 461: printf("tag VT_NON");
1.8 mycroft 462: fifo_printinfo(ap->a_vp);
1.22 christos 463: printf("\n");
1.17 christos 464: return 0;
1.1 cgd 465: }
466:
467: /*
468: * Print out internal contents of a fifo vnode.
469: */
1.17 christos 470: void
1.1 cgd 471: fifo_printinfo(vp)
472: struct vnode *vp;
473: {
474: register struct fifoinfo *fip = vp->v_fifoinfo;
475:
1.22 christos 476: printf(", fifo with %ld readers and %ld writers",
1.21 christos 477: fip->fi_readers, fip->fi_writers);
1.1 cgd 478: }
479:
480: /*
1.8 mycroft 481: * Return POSIX pathconf information applicable to fifo's.
482: */
1.17 christos 483: int
484: fifo_pathconf(v)
485: void *v;
486: {
1.8 mycroft 487: struct vop_pathconf_args /* {
488: struct vnode *a_vp;
489: int a_name;
1.10 cgd 490: register_t *a_retval;
1.17 christos 491: } */ *ap = v;
1.8 mycroft 492:
493: switch (ap->a_name) {
494: case _PC_LINK_MAX:
495: *ap->a_retval = LINK_MAX;
496: return (0);
497: case _PC_PIPE_BUF:
498: *ap->a_retval = PIPE_BUF;
499: return (0);
500: case _PC_CHOWN_RESTRICTED:
501: *ap->a_retval = 1;
502: return (0);
503: default:
504: return (EINVAL);
505: }
1.1 cgd 506: /* NOTREACHED */
507: }
CVSweb <webmaster@jp.NetBSD.org>