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