fifo_vnops.c revision 1.42 1 /* $NetBSD: fifo_vnops.c,v 1.42 2003/08/07 16:32:34 agc Exp $ */
2
3 /*
4 * Copyright (c) 1990, 1993, 1995
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. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
30 *
31 * @(#)fifo_vnops.c 8.10 (Berkeley) 5/27/95
32 */
33
34 #include <sys/cdefs.h>
35 __KERNEL_RCSID(0, "$NetBSD: fifo_vnops.c,v 1.42 2003/08/07 16:32:34 agc Exp $");
36
37 #include <sys/param.h>
38 #include <sys/systm.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/ioctl.h>
47 #include <sys/file.h>
48 #include <sys/errno.h>
49 #include <sys/malloc.h>
50 #include <sys/un.h>
51 #include <sys/poll.h>
52 #include <sys/event.h>
53
54 #include <miscfs/fifofs/fifo.h>
55 #include <miscfs/genfs/genfs.h>
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_opencount;
65 long fi_readers;
66 long fi_writers;
67 };
68
69 int (**fifo_vnodeop_p) __P((void *));
70 const struct vnodeopv_entry_desc fifo_vnodeop_entries[] = {
71 { &vop_default_desc, vn_default_error },
72 { &vop_lookup_desc, fifo_lookup }, /* lookup */
73 { &vop_create_desc, fifo_create }, /* create */
74 { &vop_mknod_desc, fifo_mknod }, /* mknod */
75 { &vop_open_desc, fifo_open }, /* open */
76 { &vop_close_desc, fifo_close }, /* close */
77 { &vop_access_desc, fifo_access }, /* access */
78 { &vop_getattr_desc, fifo_getattr }, /* getattr */
79 { &vop_setattr_desc, fifo_setattr }, /* setattr */
80 { &vop_read_desc, fifo_read }, /* read */
81 { &vop_write_desc, fifo_write }, /* write */
82 { &vop_lease_desc, fifo_lease_check }, /* lease */
83 { &vop_ioctl_desc, fifo_ioctl }, /* ioctl */
84 { &vop_poll_desc, fifo_poll }, /* poll */
85 { &vop_kqfilter_desc, fifo_kqfilter }, /* kqfilter */
86 { &vop_revoke_desc, fifo_revoke }, /* revoke */
87 { &vop_mmap_desc, fifo_mmap }, /* mmap */
88 { &vop_fsync_desc, fifo_fsync }, /* fsync */
89 { &vop_seek_desc, fifo_seek }, /* seek */
90 { &vop_remove_desc, fifo_remove }, /* remove */
91 { &vop_link_desc, fifo_link }, /* link */
92 { &vop_rename_desc, fifo_rename }, /* rename */
93 { &vop_mkdir_desc, fifo_mkdir }, /* mkdir */
94 { &vop_rmdir_desc, fifo_rmdir }, /* rmdir */
95 { &vop_symlink_desc, fifo_symlink }, /* symlink */
96 { &vop_readdir_desc, fifo_readdir }, /* readdir */
97 { &vop_readlink_desc, fifo_readlink }, /* readlink */
98 { &vop_abortop_desc, fifo_abortop }, /* abortop */
99 { &vop_inactive_desc, fifo_inactive }, /* inactive */
100 { &vop_reclaim_desc, fifo_reclaim }, /* reclaim */
101 { &vop_lock_desc, fifo_lock }, /* lock */
102 { &vop_unlock_desc, fifo_unlock }, /* unlock */
103 { &vop_bmap_desc, fifo_bmap }, /* bmap */
104 { &vop_strategy_desc, fifo_strategy }, /* strategy */
105 { &vop_print_desc, fifo_print }, /* print */
106 { &vop_islocked_desc, fifo_islocked }, /* islocked */
107 { &vop_pathconf_desc, fifo_pathconf }, /* pathconf */
108 { &vop_advlock_desc, fifo_advlock }, /* advlock */
109 { &vop_blkatoff_desc, fifo_blkatoff }, /* blkatoff */
110 { &vop_valloc_desc, fifo_valloc }, /* valloc */
111 { &vop_vfree_desc, fifo_vfree }, /* vfree */
112 { &vop_truncate_desc, fifo_truncate }, /* truncate */
113 { &vop_update_desc, fifo_update }, /* update */
114 { &vop_bwrite_desc, fifo_bwrite }, /* bwrite */
115 { &vop_putpages_desc, fifo_putpages }, /* putpages */
116 { (struct vnodeop_desc*)NULL, (int(*) __P((void *)))NULL }
117 };
118 const struct vnodeopv_desc fifo_vnodeop_opv_desc =
119 { &fifo_vnodeop_p, fifo_vnodeop_entries };
120
121 /*
122 * Trivial lookup routine that always fails.
123 */
124 /* ARGSUSED */
125 int
126 fifo_lookup(void *v)
127 {
128 struct vop_lookup_args /* {
129 struct vnode *a_dvp;
130 struct vnode **a_vpp;
131 struct componentname *a_cnp;
132 } */ *ap = v;
133
134 *ap->a_vpp = NULL;
135 return (ENOTDIR);
136 }
137
138 /*
139 * Open called to set up a new instance of a fifo or
140 * to find an active instance of a fifo.
141 */
142 /* ARGSUSED */
143 int
144 fifo_open(void *v)
145 {
146 struct vop_open_args /* {
147 struct vnode *a_vp;
148 int a_mode;
149 struct ucred *a_cred;
150 struct proc *a_p;
151 } */ *ap = v;
152 struct vnode *vp;
153 struct fifoinfo *fip;
154 struct proc *p;
155 struct socket *rso, *wso;
156 int error;
157
158 vp = ap->a_vp;
159 p = ap->a_p;
160
161 if ((fip = vp->v_fifoinfo) == NULL) {
162 MALLOC(fip, struct fifoinfo *, sizeof(*fip), M_VNODE, M_WAITOK);
163 vp->v_fifoinfo = fip;
164 if ((error = socreate(AF_LOCAL, &rso, SOCK_STREAM, 0)) != 0) {
165 free(fip, M_VNODE);
166 vp->v_fifoinfo = NULL;
167 return (error);
168 }
169 fip->fi_readsock = rso;
170 if ((error = socreate(AF_LOCAL, &wso, SOCK_STREAM, 0)) != 0) {
171 (void)soclose(rso);
172 free(fip, M_VNODE);
173 vp->v_fifoinfo = NULL;
174 return (error);
175 }
176 fip->fi_writesock = wso;
177 if ((error = unp_connect2(wso, rso)) != 0) {
178 (void)soclose(wso);
179 (void)soclose(rso);
180 free(fip, M_VNODE);
181 vp->v_fifoinfo = NULL;
182 return (error);
183 }
184 fip->fi_readers = fip->fi_writers = 0;
185 fip->fi_opencount = 0;
186 wso->so_state |= SS_CANTRCVMORE;
187 rso->so_state |= SS_CANTSENDMORE;
188 }
189 fip->fi_opencount++;
190 if (ap->a_mode & FREAD) {
191 if (fip->fi_readers++ == 0) {
192 fip->fi_writesock->so_state &= ~SS_CANTSENDMORE;
193 if (fip->fi_writers > 0)
194 wakeup((caddr_t)&fip->fi_writers);
195 }
196 }
197 if (ap->a_mode & FWRITE) {
198 if (fip->fi_writers++ == 0) {
199 fip->fi_readsock->so_state &= ~SS_CANTRCVMORE;
200 if (fip->fi_readers > 0)
201 wakeup((caddr_t)&fip->fi_readers);
202 }
203 }
204 if (ap->a_mode & FREAD) {
205 if (ap->a_mode & O_NONBLOCK) {
206 } else {
207 while (!soreadable(fip->fi_readsock) && fip->fi_writers == 0) {
208 VOP_UNLOCK(vp, 0);
209 error = tsleep((caddr_t)&fip->fi_readers,
210 PCATCH | PSOCK, "fifor", 0);
211 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
212 if (error)
213 goto bad;
214 }
215 }
216 }
217 if (ap->a_mode & FWRITE) {
218 if (ap->a_mode & O_NONBLOCK) {
219 if (fip->fi_readers == 0) {
220 error = ENXIO;
221 goto bad;
222 }
223 } else {
224 while (fip->fi_readers == 0) {
225 VOP_UNLOCK(vp, 0);
226 error = tsleep((caddr_t)&fip->fi_writers,
227 PCATCH | PSOCK, "fifow", 0);
228 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
229 if (error)
230 goto bad;
231 }
232 }
233 }
234 return (0);
235 bad:
236 VOP_CLOSE(vp, ap->a_mode, ap->a_cred, p);
237 return (error);
238 }
239
240 /*
241 * Vnode op for read
242 */
243 /* ARGSUSED */
244 int
245 fifo_read(void *v)
246 {
247 struct vop_read_args /* {
248 struct vnode *a_vp;
249 struct uio *a_uio;
250 int a_ioflag;
251 struct ucred *a_cred;
252 } */ *ap = v;
253 struct uio *uio;
254 struct socket *rso;
255 int error;
256 size_t startresid;
257
258 uio = ap->a_uio;
259 rso = ap->a_vp->v_fifoinfo->fi_readsock;
260 #ifdef DIAGNOSTIC
261 if (uio->uio_rw != UIO_READ)
262 panic("fifo_read mode");
263 #endif
264 if (uio->uio_resid == 0)
265 return (0);
266 if (ap->a_ioflag & IO_NDELAY)
267 rso->so_state |= SS_NBIO;
268 startresid = uio->uio_resid;
269 VOP_UNLOCK(ap->a_vp, 0);
270 error = (*rso->so_receive)(rso, (struct mbuf **)0, uio,
271 (struct mbuf **)0, (struct mbuf **)0, (int *)0);
272 vn_lock(ap->a_vp, LK_EXCLUSIVE | LK_RETRY);
273 /*
274 * Clear EOF indication after first such return.
275 */
276 if (uio->uio_resid == startresid)
277 rso->so_state &= ~SS_CANTRCVMORE;
278 if (ap->a_ioflag & IO_NDELAY) {
279 rso->so_state &= ~SS_NBIO;
280 if (error == EWOULDBLOCK &&
281 ap->a_vp->v_fifoinfo->fi_writers == 0)
282 error = 0;
283 }
284 return (error);
285 }
286
287 /*
288 * Vnode op for write
289 */
290 /* ARGSUSED */
291 int
292 fifo_write(void *v)
293 {
294 struct vop_write_args /* {
295 struct vnode *a_vp;
296 struct uio *a_uio;
297 int a_ioflag;
298 struct ucred *a_cred;
299 } */ *ap = v;
300 struct socket *wso;
301 int error;
302
303 wso = ap->a_vp->v_fifoinfo->fi_writesock;
304 #ifdef DIAGNOSTIC
305 if (ap->a_uio->uio_rw != UIO_WRITE)
306 panic("fifo_write mode");
307 #endif
308 if (ap->a_ioflag & IO_NDELAY)
309 wso->so_state |= SS_NBIO;
310 VOP_UNLOCK(ap->a_vp, 0);
311 error = (*wso->so_send)(wso, (struct mbuf *)0, ap->a_uio, 0,
312 (struct mbuf *)0, 0);
313 vn_lock(ap->a_vp, LK_EXCLUSIVE | LK_RETRY);
314 if (ap->a_ioflag & IO_NDELAY)
315 wso->so_state &= ~SS_NBIO;
316 return (error);
317 }
318
319 /*
320 * Device ioctl operation.
321 */
322 /* ARGSUSED */
323 int
324 fifo_ioctl(void *v)
325 {
326 struct vop_ioctl_args /* {
327 struct vnode *a_vp;
328 u_long a_command;
329 caddr_t a_data;
330 int a_fflag;
331 struct ucred *a_cred;
332 struct proc *a_p;
333 } */ *ap = v;
334 struct file filetmp;
335 int error;
336
337 if (ap->a_command == FIONBIO)
338 return (0);
339 if (ap->a_fflag & FREAD) {
340 filetmp.f_data = (caddr_t)ap->a_vp->v_fifoinfo->fi_readsock;
341 error = soo_ioctl(&filetmp, ap->a_command, ap->a_data, ap->a_p);
342 if (error)
343 return (error);
344 }
345 if (ap->a_fflag & FWRITE) {
346 filetmp.f_data = (caddr_t)ap->a_vp->v_fifoinfo->fi_writesock;
347 error = soo_ioctl(&filetmp, ap->a_command, ap->a_data, ap->a_p);
348 if (error)
349 return (error);
350 }
351 return (0);
352 }
353
354 /* ARGSUSED */
355 int
356 fifo_poll(void *v)
357 {
358 struct vop_poll_args /* {
359 struct vnode *a_vp;
360 int a_events;
361 struct proc *a_p;
362 } */ *ap = v;
363 struct file filetmp;
364 int revents;
365
366 revents = 0;
367 if (ap->a_events & (POLLIN | POLLPRI | POLLRDNORM | POLLRDBAND)) {
368 filetmp.f_data = (caddr_t)ap->a_vp->v_fifoinfo->fi_readsock;
369 if (filetmp.f_data)
370 revents |= soo_poll(&filetmp, ap->a_events, ap->a_p);
371 }
372 if (ap->a_events & (POLLOUT | POLLWRNORM | POLLWRBAND)) {
373 filetmp.f_data = (caddr_t)ap->a_vp->v_fifoinfo->fi_writesock;
374 if (filetmp.f_data)
375 revents |= soo_poll(&filetmp, ap->a_events, ap->a_p);
376 }
377
378 return (revents);
379 }
380
381 int
382 fifo_inactive(void *v)
383 {
384 struct vop_inactive_args /* {
385 struct vnode *a_vp;
386 struct proc *a_p;
387 } */ *ap = v;
388
389 VOP_UNLOCK(ap->a_vp, 0);
390 return (0);
391 }
392
393 /*
394 * This is a noop, simply returning what one has been given.
395 */
396 int
397 fifo_bmap(void *v)
398 {
399 struct vop_bmap_args /* {
400 struct vnode *a_vp;
401 daddr_t a_bn;
402 struct vnode **a_vpp;
403 daddr_t *a_bnp;
404 int *a_runp;
405 } */ *ap = v;
406
407 if (ap->a_vpp != NULL)
408 *ap->a_vpp = ap->a_vp;
409 if (ap->a_bnp != NULL)
410 *ap->a_bnp = ap->a_bn;
411 if (ap->a_runp != NULL)
412 *ap->a_runp = 0;
413 return (0);
414 }
415
416 /*
417 * Device close routine
418 */
419 /* ARGSUSED */
420 int
421 fifo_close(void *v)
422 {
423 struct vop_close_args /* {
424 struct vnode *a_vp;
425 int a_fflag;
426 struct ucred *a_cred;
427 struct proc *a_p;
428 } */ *ap = v;
429 struct vnode *vp;
430 struct fifoinfo *fip;
431
432 vp = ap->a_vp;
433 fip = vp->v_fifoinfo;
434 if (ap->a_fflag & FREAD) {
435 if (--fip->fi_readers == 0)
436 socantsendmore(fip->fi_writesock);
437 }
438 if (ap->a_fflag & FWRITE) {
439 if (--fip->fi_writers == 0)
440 socantrcvmore(fip->fi_readsock);
441 }
442 if (--fip->fi_opencount == 0) {
443 (void) soclose(fip->fi_readsock);
444 (void) soclose(fip->fi_writesock);
445 FREE(fip, M_VNODE);
446 vp->v_fifoinfo = NULL;
447 }
448 return (0);
449 }
450
451 /*
452 * Print out the contents of a fifo vnode.
453 */
454 int
455 fifo_print(void *v)
456 {
457 struct vop_print_args /* {
458 struct vnode *a_vp;
459 } */ *ap = v;
460
461 printf("tag VT_NON");
462 fifo_printinfo(ap->a_vp);
463 printf("\n");
464 return 0;
465 }
466
467 /*
468 * Print out internal contents of a fifo vnode.
469 */
470 void
471 fifo_printinfo(struct vnode *vp)
472 {
473 struct fifoinfo *fip;
474
475 fip = vp->v_fifoinfo;
476 printf(", fifo with %ld readers and %ld writers",
477 fip->fi_readers, fip->fi_writers);
478 }
479
480 /*
481 * Return POSIX pathconf information applicable to fifo's.
482 */
483 int
484 fifo_pathconf(void *v)
485 {
486 struct vop_pathconf_args /* {
487 struct vnode *a_vp;
488 int a_name;
489 register_t *a_retval;
490 } */ *ap = v;
491
492 switch (ap->a_name) {
493 case _PC_LINK_MAX:
494 *ap->a_retval = LINK_MAX;
495 return (0);
496 case _PC_PIPE_BUF:
497 *ap->a_retval = PIPE_BUF;
498 return (0);
499 case _PC_CHOWN_RESTRICTED:
500 *ap->a_retval = 1;
501 return (0);
502 case _PC_SYNC_IO:
503 *ap->a_retval = 1;
504 return (0);
505 default:
506 return (EINVAL);
507 }
508 /* NOTREACHED */
509 }
510
511 static void
512 filt_fifordetach(struct knote *kn)
513 {
514 struct socket *so;
515
516 so = (struct socket *)kn->kn_hook;
517 SLIST_REMOVE(&so->so_rcv.sb_sel.sel_klist, kn, knote, kn_selnext);
518 if (SLIST_EMPTY(&so->so_rcv.sb_sel.sel_klist))
519 so->so_rcv.sb_flags &= ~SB_KNOTE;
520 }
521
522 static int
523 filt_fiforead(struct knote *kn, long hint)
524 {
525 struct socket *so;
526
527 so = (struct socket *)kn->kn_hook;
528 kn->kn_data = so->so_rcv.sb_cc;
529 if (so->so_state & SS_CANTRCVMORE) {
530 kn->kn_flags |= EV_EOF;
531 return (1);
532 }
533 kn->kn_flags &= ~EV_EOF;
534 return (kn->kn_data > 0);
535 }
536
537 static void
538 filt_fifowdetach(struct knote *kn)
539 {
540 struct socket *so;
541
542 so = (struct socket *)kn->kn_hook;
543 SLIST_REMOVE(&so->so_snd.sb_sel.sel_klist, kn, knote, kn_selnext);
544 if (SLIST_EMPTY(&so->so_snd.sb_sel.sel_klist))
545 so->so_snd.sb_flags &= ~SB_KNOTE;
546 }
547
548 static int
549 filt_fifowrite(struct knote *kn, long hint)
550 {
551 struct socket *so;
552
553 so = (struct socket *)kn->kn_hook;
554 kn->kn_data = sbspace(&so->so_snd);
555 if (so->so_state & SS_CANTSENDMORE) {
556 kn->kn_flags |= EV_EOF;
557 return (1);
558 }
559 kn->kn_flags &= ~EV_EOF;
560 return (kn->kn_data >= so->so_snd.sb_lowat);
561 }
562
563 static const struct filterops fiforead_filtops =
564 { 1, NULL, filt_fifordetach, filt_fiforead };
565 static const struct filterops fifowrite_filtops =
566 { 1, NULL, filt_fifowdetach, filt_fifowrite };
567
568 /* ARGSUSED */
569 int
570 fifo_kqfilter(void *v)
571 {
572 struct vop_kqfilter_args /* {
573 struct vnode *a_vp;
574 struct knote *a_kn;
575 } */ *ap = v;
576 struct socket *so;
577 struct sockbuf *sb;
578
579 so = (struct socket *)ap->a_vp->v_fifoinfo->fi_readsock;
580 switch (ap->a_kn->kn_filter) {
581 case EVFILT_READ:
582 ap->a_kn->kn_fop = &fiforead_filtops;
583 sb = &so->so_rcv;
584 break;
585 case EVFILT_WRITE:
586 ap->a_kn->kn_fop = &fifowrite_filtops;
587 sb = &so->so_snd;
588 break;
589 default:
590 return (1);
591 }
592
593 ap->a_kn->kn_hook = so;
594
595 SLIST_INSERT_HEAD(&sb->sb_sel.sel_klist, ap->a_kn, kn_selnext);
596 sb->sb_flags |= SB_KNOTE;
597 return (0);
598 }
599