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