fifo_vnops.c revision 1.15 1 /* $NetBSD: fifo_vnops.c,v 1.15 1995/04/02 19:27:48 mycroft Exp $ */
2
3 /*
4 * Copyright (c) 1990, 1993
5 * The Regents of the University of California. All rights reserved.
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 *
35 * @(#)fifo_vnops.c 8.4 (Berkeley) 8/10/94
36 */
37
38 #include <sys/param.h>
39 #include <sys/proc.h>
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>
51 #include <miscfs/fifofs/fifo.h>
52
53 /*
54 * This structure is associated with the FIFO vnode and stores
55 * the state associated with the FIFO.
56 */
57 struct fifoinfo {
58 struct socket *fi_readsock;
59 struct socket *fi_writesock;
60 long fi_readers;
61 long fi_writers;
62 };
63
64 int (**fifo_vnodeop_p)();
65 struct vnodeopv_entry_desc fifo_vnodeop_entries[] = {
66 { &vop_default_desc, vn_default_error },
67 { &vop_lookup_desc, fifo_lookup }, /* lookup */
68 { &vop_create_desc, fifo_create }, /* create */
69 { &vop_mknod_desc, fifo_mknod }, /* mknod */
70 { &vop_open_desc, fifo_open }, /* open */
71 { &vop_close_desc, fifo_close }, /* close */
72 { &vop_access_desc, fifo_access }, /* access */
73 { &vop_getattr_desc, fifo_getattr }, /* getattr */
74 { &vop_setattr_desc, fifo_setattr }, /* setattr */
75 { &vop_read_desc, fifo_read }, /* read */
76 { &vop_write_desc, fifo_write }, /* write */
77 { &vop_lease_desc, fifo_lease_check }, /* lease */
78 { &vop_ioctl_desc, fifo_ioctl }, /* ioctl */
79 { &vop_select_desc, fifo_select }, /* select */
80 { &vop_mmap_desc, fifo_mmap }, /* mmap */
81 { &vop_fsync_desc, fifo_fsync }, /* fsync */
82 { &vop_seek_desc, fifo_seek }, /* seek */
83 { &vop_remove_desc, fifo_remove }, /* remove */
84 { &vop_link_desc, fifo_link }, /* link */
85 { &vop_rename_desc, fifo_rename }, /* rename */
86 { &vop_mkdir_desc, fifo_mkdir }, /* mkdir */
87 { &vop_rmdir_desc, fifo_rmdir }, /* rmdir */
88 { &vop_symlink_desc, fifo_symlink }, /* symlink */
89 { &vop_readdir_desc, fifo_readdir }, /* readdir */
90 { &vop_readlink_desc, fifo_readlink }, /* readlink */
91 { &vop_abortop_desc, fifo_abortop }, /* abortop */
92 { &vop_inactive_desc, fifo_inactive }, /* inactive */
93 { &vop_reclaim_desc, fifo_reclaim }, /* reclaim */
94 { &vop_lock_desc, fifo_lock }, /* lock */
95 { &vop_unlock_desc, fifo_unlock }, /* unlock */
96 { &vop_bmap_desc, fifo_bmap }, /* bmap */
97 { &vop_strategy_desc, fifo_strategy }, /* strategy */
98 { &vop_print_desc, fifo_print }, /* print */
99 { &vop_islocked_desc, fifo_islocked }, /* islocked */
100 { &vop_pathconf_desc, fifo_pathconf }, /* pathconf */
101 { &vop_advlock_desc, fifo_advlock }, /* advlock */
102 { &vop_blkatoff_desc, fifo_blkatoff }, /* blkatoff */
103 { &vop_valloc_desc, fifo_valloc }, /* valloc */
104 { &vop_vfree_desc, fifo_vfree }, /* vfree */
105 { &vop_truncate_desc, fifo_truncate }, /* truncate */
106 { &vop_update_desc, fifo_update }, /* update */
107 { &vop_bwrite_desc, fifo_bwrite }, /* bwrite */
108 { (struct vnodeop_desc*)NULL, (int(*)())NULL }
109 };
110 struct vnodeopv_desc fifo_vnodeop_opv_desc =
111 { &fifo_vnodeop_p, fifo_vnodeop_entries };
112
113 /*
114 * Trivial lookup routine that always fails.
115 */
116 /* ARGSUSED */
117 fifo_lookup(ap)
118 struct vop_lookup_args /* {
119 struct vnode * a_dvp;
120 struct vnode ** a_vpp;
121 struct componentname * a_cnp;
122 } */ *ap;
123 {
124
125 *ap->a_vpp = NULL;
126 return (ENOTDIR);
127 }
128
129 /*
130 * Open called to set up a new instance of a fifo or
131 * to find an active instance of a fifo.
132 */
133 /* ARGSUSED */
134 fifo_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 #ifdef COMPAT_IBCS2
150 if (ap->a_p->p_emul == EMUL_IBCS2_COFF ||
151 ap->a_p->p_emul == EMUL_IBCS2_XOUT)
152 ap->a_mode = (ap->a_mode & ~FWRITE) | O_NONBLOCK;
153 else
154 #endif
155 return (EINVAL);
156 }
157 if ((fip = vp->v_fifoinfo) == NULL) {
158 MALLOC(fip, struct fifoinfo *, sizeof(*fip), M_VNODE, M_WAITOK);
159 vp->v_fifoinfo = fip;
160 if (error = socreate(AF_LOCAL, &rso, SOCK_STREAM, 0)) {
161 free(fip, M_VNODE);
162 vp->v_fifoinfo = NULL;
163 return (error);
164 }
165 fip->fi_readsock = rso;
166 if (error = socreate(AF_LOCAL, &wso, SOCK_STREAM, 0)) {
167 (void)soclose(rso);
168 free(fip, M_VNODE);
169 vp->v_fifoinfo = NULL;
170 return (error);
171 }
172 fip->fi_writesock = wso;
173 if (error = unp_connect2(wso, rso)) {
174 (void)soclose(wso);
175 (void)soclose(rso);
176 free(fip, M_VNODE);
177 vp->v_fifoinfo = NULL;
178 return (error);
179 }
180 fip->fi_readers = fip->fi_writers = 0;
181 wso->so_state |= SS_CANTRCVMORE;
182 rso->so_state |= SS_CANTSENDMORE;
183 }
184 error = 0;
185 if (ap->a_mode & FREAD) {
186 fip->fi_readers++;
187 if (fip->fi_readers == 1) {
188 fip->fi_writesock->so_state &= ~SS_CANTSENDMORE;
189 if (fip->fi_writers > 0)
190 wakeup((caddr_t)&fip->fi_writers);
191 }
192 if (ap->a_mode & O_NONBLOCK)
193 return (0);
194 while (fip->fi_writers == 0) {
195 VOP_UNLOCK(vp);
196 error = tsleep((caddr_t)&fip->fi_readers,
197 PCATCH | PSOCK, openstr, 0);
198 VOP_LOCK(vp);
199 if (error)
200 break;
201 }
202 } else {
203 fip->fi_writers++;
204 if (fip->fi_readers == 0 && (ap->a_mode & O_NONBLOCK)) {
205 error = ENXIO;
206 } else {
207 if (fip->fi_writers == 1) {
208 fip->fi_readsock->so_state &= ~SS_CANTRCVMORE;
209 if (fip->fi_readers > 0)
210 wakeup((caddr_t)&fip->fi_readers);
211 }
212 while (fip->fi_readers == 0) {
213 VOP_UNLOCK(vp);
214 error = tsleep((caddr_t)&fip->fi_writers,
215 PCATCH | PSOCK, openstr, 0);
216 VOP_LOCK(vp);
217 if (error)
218 break;
219 }
220 }
221 }
222 if (error)
223 VOP_CLOSE(vp, ap->a_mode, ap->a_cred, ap->a_p);
224 return (error);
225 }
226
227 /*
228 * Vnode op for read
229 */
230 /* ARGSUSED */
231 fifo_read(ap)
232 struct vop_read_args /* {
233 struct vnode *a_vp;
234 struct uio *a_uio;
235 int a_ioflag;
236 struct ucred *a_cred;
237 } */ *ap;
238 {
239 register struct uio *uio = ap->a_uio;
240 register struct socket *rso = ap->a_vp->v_fifoinfo->fi_readsock;
241 int error, startresid;
242
243 #ifdef DIAGNOSTIC
244 if (uio->uio_rw != UIO_READ)
245 panic("fifo_read mode");
246 #endif
247 if (uio->uio_resid == 0)
248 return (0);
249 if (ap->a_ioflag & IO_NDELAY)
250 rso->so_state |= SS_NBIO;
251 startresid = uio->uio_resid;
252 VOP_UNLOCK(ap->a_vp);
253 error = soreceive(rso, (struct mbuf **)0, uio, (struct mbuf **)0,
254 (struct mbuf **)0, (int *)0);
255 VOP_LOCK(ap->a_vp);
256 /*
257 * Clear EOF indication after first such return.
258 */
259 if (uio->uio_resid == startresid)
260 rso->so_state &= ~SS_CANTRCVMORE;
261 if (ap->a_ioflag & IO_NDELAY)
262 rso->so_state &= ~SS_NBIO;
263 return (error);
264 }
265
266 /*
267 * Vnode op for write
268 */
269 /* ARGSUSED */
270 fifo_write(ap)
271 struct vop_write_args /* {
272 struct vnode *a_vp;
273 struct uio *a_uio;
274 int a_ioflag;
275 struct ucred *a_cred;
276 } */ *ap;
277 {
278 struct socket *wso = ap->a_vp->v_fifoinfo->fi_writesock;
279 int error;
280
281 #ifdef DIAGNOSTIC
282 if (ap->a_uio->uio_rw != UIO_WRITE)
283 panic("fifo_write mode");
284 #endif
285 if (ap->a_ioflag & IO_NDELAY)
286 wso->so_state |= SS_NBIO;
287 VOP_UNLOCK(ap->a_vp);
288 error = sosend(wso, (struct mbuf *)0, ap->a_uio, 0, (struct mbuf *)0, 0);
289 VOP_LOCK(ap->a_vp);
290 if (ap->a_ioflag & IO_NDELAY)
291 wso->so_state &= ~SS_NBIO;
292 return (error);
293 }
294
295 /*
296 * Device ioctl operation.
297 */
298 /* ARGSUSED */
299 fifo_ioctl(ap)
300 struct vop_ioctl_args /* {
301 struct vnode *a_vp;
302 u_long a_command;
303 caddr_t a_data;
304 int a_fflag;
305 struct ucred *a_cred;
306 struct proc *a_p;
307 } */ *ap;
308 {
309 struct file filetmp;
310
311 if (ap->a_command == FIONBIO)
312 return (0);
313 if (ap->a_fflag & FREAD)
314 filetmp.f_data = (caddr_t)ap->a_vp->v_fifoinfo->fi_readsock;
315 else
316 filetmp.f_data = (caddr_t)ap->a_vp->v_fifoinfo->fi_writesock;
317 return (soo_ioctl(&filetmp, ap->a_command, ap->a_data, ap->a_p));
318 }
319
320 /* ARGSUSED */
321 fifo_select(ap)
322 struct vop_select_args /* {
323 struct vnode *a_vp;
324 int a_which;
325 int a_fflags;
326 struct ucred *a_cred;
327 struct proc *a_p;
328 } */ *ap;
329 {
330 struct file filetmp;
331
332 if (ap->a_fflags & FREAD)
333 filetmp.f_data = (caddr_t)ap->a_vp->v_fifoinfo->fi_readsock;
334 else
335 filetmp.f_data = (caddr_t)ap->a_vp->v_fifoinfo->fi_writesock;
336 return (soo_select(&filetmp, ap->a_which, ap->a_p));
337 }
338
339 /*
340 * This is a noop, simply returning what one has been given.
341 */
342 fifo_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 */
362 fifo_lock(ap)
363 struct vop_lock_args /* {
364 struct vnode *a_vp;
365 } */ *ap;
366 {
367
368 return (0);
369 }
370
371 /* ARGSUSED */
372 fifo_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 */
385 fifo_close(ap)
386 struct vop_close_args /* {
387 struct vnode *a_vp;
388 int a_fflag;
389 struct ucred *a_cred;
390 struct proc *a_p;
391 } */ *ap;
392 {
393 register struct vnode *vp = ap->a_vp;
394 register struct fifoinfo *fip = vp->v_fifoinfo;
395 int error1, error2;
396
397 if (ap->a_fflag & FWRITE) {
398 fip->fi_writers--;
399 if (fip->fi_writers == 0)
400 socantrcvmore(fip->fi_readsock);
401 } else {
402 fip->fi_readers--;
403 if (fip->fi_readers == 0)
404 socantsendmore(fip->fi_writesock);
405 }
406 if (vp->v_usecount > 1)
407 return (0);
408 error1 = soclose(fip->fi_readsock);
409 error2 = soclose(fip->fi_writesock);
410 FREE(fip, M_VNODE);
411 vp->v_fifoinfo = NULL;
412 if (error1)
413 return (error1);
414 return (error2);
415 }
416
417 /*
418 * Print out the contents of a fifo vnode.
419 */
420 fifo_print(ap)
421 struct vop_print_args /* {
422 struct vnode *a_vp;
423 } */ *ap;
424 {
425
426 printf("tag VT_NON");
427 fifo_printinfo(ap->a_vp);
428 printf("\n");
429 }
430
431 /*
432 * Print out internal contents of a fifo vnode.
433 */
434 fifo_printinfo(vp)
435 struct vnode *vp;
436 {
437 register struct fifoinfo *fip = vp->v_fifoinfo;
438
439 printf(", fifo with %d readers and %d writers",
440 fip->fi_readers, fip->fi_writers);
441 }
442
443 /*
444 * Return POSIX pathconf information applicable to fifo's.
445 */
446 fifo_pathconf(ap)
447 struct vop_pathconf_args /* {
448 struct vnode *a_vp;
449 int a_name;
450 register_t *a_retval;
451 } */ *ap;
452 {
453
454 switch (ap->a_name) {
455 case _PC_LINK_MAX:
456 *ap->a_retval = LINK_MAX;
457 return (0);
458 case _PC_PIPE_BUF:
459 *ap->a_retval = PIPE_BUF;
460 return (0);
461 case _PC_CHOWN_RESTRICTED:
462 *ap->a_retval = 1;
463 return (0);
464 default:
465 return (EINVAL);
466 }
467 /* NOTREACHED */
468 }
469
470 /*
471 * Fifo failed operation
472 */
473 fifo_ebadf()
474 {
475
476 return (EBADF);
477 }
478
479 /*
480 * Fifo advisory byte-level locks.
481 */
482 /* ARGSUSED */
483 fifo_advlock(ap)
484 struct vop_advlock_args /* {
485 struct vnode *a_vp;
486 caddr_t a_id;
487 int a_op;
488 struct flock *a_fl;
489 int a_flags;
490 } */ *ap;
491 {
492
493 return (EOPNOTSUPP);
494 }
495
496 /*
497 * Fifo bad operation
498 */
499 fifo_badop()
500 {
501
502 panic("fifo_badop called");
503 /* NOTREACHED */
504 }
505