fifo_vnops.c revision 1.16 1 /* $NetBSD: fifo_vnops.c,v 1.16 1995/04/14 23:30:14 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 ((fip = vp->v_fifoinfo) == NULL) {
149 MALLOC(fip, struct fifoinfo *, sizeof(*fip), M_VNODE, M_WAITOK);
150 vp->v_fifoinfo = fip;
151 if (error = socreate(AF_LOCAL, &rso, SOCK_STREAM, 0)) {
152 free(fip, M_VNODE);
153 vp->v_fifoinfo = NULL;
154 return (error);
155 }
156 fip->fi_readsock = rso;
157 if (error = socreate(AF_LOCAL, &wso, SOCK_STREAM, 0)) {
158 (void)soclose(rso);
159 free(fip, M_VNODE);
160 vp->v_fifoinfo = NULL;
161 return (error);
162 }
163 fip->fi_writesock = wso;
164 if (error = unp_connect2(wso, rso)) {
165 (void)soclose(wso);
166 (void)soclose(rso);
167 free(fip, M_VNODE);
168 vp->v_fifoinfo = NULL;
169 return (error);
170 }
171 fip->fi_readers = fip->fi_writers = 0;
172 wso->so_state |= SS_CANTRCVMORE;
173 rso->so_state |= SS_CANTSENDMORE;
174 }
175 if (ap->a_mode & FREAD) {
176 if (fip->fi_readers++ == 0) {
177 fip->fi_writesock->so_state &= ~SS_CANTSENDMORE;
178 if (fip->fi_writers > 0)
179 wakeup((caddr_t)&fip->fi_writers);
180 }
181 }
182 if (ap->a_mode & FWRITE) {
183 if (fip->fi_writers++ == 0) {
184 fip->fi_readsock->so_state &= ~SS_CANTRCVMORE;
185 if (fip->fi_readers > 0)
186 wakeup((caddr_t)&fip->fi_readers);
187 }
188 }
189 if (ap->a_mode & FREAD) {
190 if (ap->a_mode & O_NONBLOCK) {
191 } else {
192 while (fip->fi_writers == 0) {
193 VOP_UNLOCK(vp);
194 error = tsleep((caddr_t)&fip->fi_readers,
195 PCATCH | PSOCK, openstr, 0);
196 VOP_LOCK(vp);
197 if (error)
198 goto bad;
199 }
200 }
201 }
202 if (ap->a_mode & FWRITE) {
203 if (ap->a_mode & O_NONBLOCK) {
204 if (fip->fi_readers == 0) {
205 error = ENXIO;
206 goto bad;
207 }
208 } else {
209 while (fip->fi_readers == 0) {
210 VOP_UNLOCK(vp);
211 error = tsleep((caddr_t)&fip->fi_writers,
212 PCATCH | PSOCK, openstr, 0);
213 VOP_LOCK(vp);
214 if (error)
215 goto bad;
216 }
217 }
218 }
219 return (0);
220 bad:
221 VOP_CLOSE(vp, ap->a_mode, ap->a_cred, ap->a_p);
222 return (error);
223 }
224
225 /*
226 * Vnode op for read
227 */
228 /* ARGSUSED */
229 fifo_read(ap)
230 struct vop_read_args /* {
231 struct vnode *a_vp;
232 struct uio *a_uio;
233 int a_ioflag;
234 struct ucred *a_cred;
235 } */ *ap;
236 {
237 register struct uio *uio = ap->a_uio;
238 register struct socket *rso = ap->a_vp->v_fifoinfo->fi_readsock;
239 int error, startresid;
240
241 #ifdef DIAGNOSTIC
242 if (uio->uio_rw != UIO_READ)
243 panic("fifo_read mode");
244 #endif
245 if (uio->uio_resid == 0)
246 return (0);
247 if (ap->a_ioflag & IO_NDELAY)
248 rso->so_state |= SS_NBIO;
249 startresid = uio->uio_resid;
250 VOP_UNLOCK(ap->a_vp);
251 error = soreceive(rso, (struct mbuf **)0, uio, (struct mbuf **)0,
252 (struct mbuf **)0, (int *)0);
253 VOP_LOCK(ap->a_vp);
254 /*
255 * Clear EOF indication after first such return.
256 */
257 if (uio->uio_resid == startresid)
258 rso->so_state &= ~SS_CANTRCVMORE;
259 if (ap->a_ioflag & IO_NDELAY)
260 rso->so_state &= ~SS_NBIO;
261 return (error);
262 }
263
264 /*
265 * Vnode op for write
266 */
267 /* ARGSUSED */
268 fifo_write(ap)
269 struct vop_write_args /* {
270 struct vnode *a_vp;
271 struct uio *a_uio;
272 int a_ioflag;
273 struct ucred *a_cred;
274 } */ *ap;
275 {
276 struct socket *wso = ap->a_vp->v_fifoinfo->fi_writesock;
277 int error;
278
279 #ifdef DIAGNOSTIC
280 if (ap->a_uio->uio_rw != UIO_WRITE)
281 panic("fifo_write mode");
282 #endif
283 if (ap->a_ioflag & IO_NDELAY)
284 wso->so_state |= SS_NBIO;
285 VOP_UNLOCK(ap->a_vp);
286 error = sosend(wso, (struct mbuf *)0, ap->a_uio, 0, (struct mbuf *)0, 0);
287 VOP_LOCK(ap->a_vp);
288 if (ap->a_ioflag & IO_NDELAY)
289 wso->so_state &= ~SS_NBIO;
290 return (error);
291 }
292
293 /*
294 * Device ioctl operation.
295 */
296 /* ARGSUSED */
297 fifo_ioctl(ap)
298 struct vop_ioctl_args /* {
299 struct vnode *a_vp;
300 u_long 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 int error;
309
310 if (ap->a_command == FIONBIO)
311 return (0);
312 if (ap->a_fflag & FREAD) {
313 filetmp.f_data = (caddr_t)ap->a_vp->v_fifoinfo->fi_readsock;
314 error = soo_ioctl(&filetmp, ap->a_command, ap->a_data, ap->a_p);
315 if (error)
316 return (error);
317 }
318 if (ap->a_fflag & FWRITE) {
319 filetmp.f_data = (caddr_t)ap->a_vp->v_fifoinfo->fi_writesock;
320 error = soo_ioctl(&filetmp, ap->a_command, ap->a_data, ap->a_p);
321 if (error)
322 return (error);
323 }
324 return (0);
325 }
326
327 /* ARGSUSED */
328 fifo_select(ap)
329 struct vop_select_args /* {
330 struct vnode *a_vp;
331 int a_which;
332 int a_fflags;
333 struct ucred *a_cred;
334 struct proc *a_p;
335 } */ *ap;
336 {
337 struct file filetmp;
338 int ready;
339
340 if (ap->a_fflags & FREAD) {
341 filetmp.f_data = (caddr_t)ap->a_vp->v_fifoinfo->fi_readsock;
342 ready = soo_select(&filetmp, ap->a_which, ap->a_p);
343 if (ready)
344 return (ready);
345 }
346 if (ap->a_fflags & FWRITE) {
347 filetmp.f_data = (caddr_t)ap->a_vp->v_fifoinfo->fi_writesock;
348 ready = soo_select(&filetmp, ap->a_which, ap->a_p);
349 if (ready)
350 return (ready);
351 }
352 return (0);
353 }
354
355 /*
356 * This is a noop, simply returning what one has been given.
357 */
358 fifo_bmap(ap)
359 struct vop_bmap_args /* {
360 struct vnode *a_vp;
361 daddr_t a_bn;
362 struct vnode **a_vpp;
363 daddr_t *a_bnp;
364 } */ *ap;
365 {
366
367 if (ap->a_vpp != NULL)
368 *ap->a_vpp = ap->a_vp;
369 if (ap->a_bnp != NULL)
370 *ap->a_bnp = ap->a_bn;
371 return (0);
372 }
373
374 /*
375 * At the moment we do not do any locking.
376 */
377 /* ARGSUSED */
378 fifo_lock(ap)
379 struct vop_lock_args /* {
380 struct vnode *a_vp;
381 } */ *ap;
382 {
383
384 return (0);
385 }
386
387 /* ARGSUSED */
388 fifo_unlock(ap)
389 struct vop_unlock_args /* {
390 struct vnode *a_vp;
391 } */ *ap;
392 {
393
394 return (0);
395 }
396
397 /*
398 * Device close routine
399 */
400 /* ARGSUSED */
401 fifo_close(ap)
402 struct vop_close_args /* {
403 struct vnode *a_vp;
404 int a_fflag;
405 struct ucred *a_cred;
406 struct proc *a_p;
407 } */ *ap;
408 {
409 register struct vnode *vp = ap->a_vp;
410 register struct fifoinfo *fip = vp->v_fifoinfo;
411 int error1, error2;
412
413 if (ap->a_fflag & FREAD) {
414 if (--fip->fi_readers == 0)
415 socantsendmore(fip->fi_writesock);
416 }
417 if (ap->a_fflag & FWRITE) {
418 if (--fip->fi_writers == 0)
419 socantrcvmore(fip->fi_readsock);
420 }
421 if (vp->v_usecount > 1)
422 return (0);
423 error1 = soclose(fip->fi_readsock);
424 error2 = soclose(fip->fi_writesock);
425 FREE(fip, M_VNODE);
426 vp->v_fifoinfo = NULL;
427 if (error1)
428 return (error1);
429 return (error2);
430 }
431
432 /*
433 * Print out the contents of a fifo vnode.
434 */
435 fifo_print(ap)
436 struct vop_print_args /* {
437 struct vnode *a_vp;
438 } */ *ap;
439 {
440
441 printf("tag VT_NON");
442 fifo_printinfo(ap->a_vp);
443 printf("\n");
444 }
445
446 /*
447 * Print out internal contents of a fifo vnode.
448 */
449 fifo_printinfo(vp)
450 struct vnode *vp;
451 {
452 register struct fifoinfo *fip = vp->v_fifoinfo;
453
454 printf(", fifo with %d readers and %d writers",
455 fip->fi_readers, fip->fi_writers);
456 }
457
458 /*
459 * Return POSIX pathconf information applicable to fifo's.
460 */
461 fifo_pathconf(ap)
462 struct vop_pathconf_args /* {
463 struct vnode *a_vp;
464 int a_name;
465 register_t *a_retval;
466 } */ *ap;
467 {
468
469 switch (ap->a_name) {
470 case _PC_LINK_MAX:
471 *ap->a_retval = LINK_MAX;
472 return (0);
473 case _PC_PIPE_BUF:
474 *ap->a_retval = PIPE_BUF;
475 return (0);
476 case _PC_CHOWN_RESTRICTED:
477 *ap->a_retval = 1;
478 return (0);
479 default:
480 return (EINVAL);
481 }
482 /* NOTREACHED */
483 }
484
485 /*
486 * Fifo failed operation
487 */
488 fifo_ebadf()
489 {
490
491 return (EBADF);
492 }
493
494 /*
495 * Fifo advisory byte-level locks.
496 */
497 /* ARGSUSED */
498 fifo_advlock(ap)
499 struct vop_advlock_args /* {
500 struct vnode *a_vp;
501 caddr_t a_id;
502 int a_op;
503 struct flock *a_fl;
504 int a_flags;
505 } */ *ap;
506 {
507
508 return (EOPNOTSUPP);
509 }
510
511 /*
512 * Fifo bad operation
513 */
514 fifo_badop()
515 {
516
517 panic("fifo_badop called");
518 /* NOTREACHED */
519 }
520