fifo_vnops.c revision 1.41.2.4 1 /* $NetBSD: fifo_vnops.c,v 1.41.2.4 2004/09/21 13:36:30 skrll 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.41.2.4 2004/09/21 13:36:30 skrll 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/protosw.h>
45 #include <sys/socketvar.h>
46 #include <sys/stat.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 <sys/poll.h>
53 #include <sys/event.h>
54
55 #include <miscfs/fifofs/fifo.h>
56 #include <miscfs/genfs/genfs.h>
57
58 /*
59 * This structure is associated with the FIFO vnode and stores
60 * the state associated with the FIFO.
61 */
62 struct fifoinfo {
63 struct socket *fi_readsock;
64 struct socket *fi_writesock;
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 lwp *a_l;
151 } */ *ap = v;
152 struct vnode *vp;
153 struct fifoinfo *fip;
154 struct lwp *l = ap->a_l;
155 struct proc *p;
156 struct socket *rso, *wso;
157 int error;
158
159 vp = ap->a_vp;
160 p = ap->a_l->l_proc;
161
162 if ((fip = vp->v_fifoinfo) == NULL) {
163 MALLOC(fip, struct fifoinfo *, sizeof(*fip), M_VNODE, M_WAITOK);
164 vp->v_fifoinfo = fip;
165 error = socreate(AF_LOCAL, &rso, SOCK_STREAM, 0, l);
166 if (error != 0) {
167 free(fip, M_VNODE);
168 vp->v_fifoinfo = NULL;
169 return (error);
170 }
171 fip->fi_readsock = rso;
172 error = socreate(AF_LOCAL, &wso, SOCK_STREAM, 0, l);
173 if (error != 0) {
174 (void)soclose(rso);
175 free(fip, M_VNODE);
176 vp->v_fifoinfo = NULL;
177 return (error);
178 }
179 fip->fi_writesock = wso;
180 if ((error = unp_connect2(wso, rso, PRU_CONNECT2)) != 0) {
181 (void)soclose(wso);
182 (void)soclose(rso);
183 free(fip, M_VNODE);
184 vp->v_fifoinfo = NULL;
185 return (error);
186 }
187 fip->fi_readers = fip->fi_writers = 0;
188 wso->so_state |= SS_CANTRCVMORE;
189 rso->so_state |= SS_CANTSENDMORE;
190 }
191 if (ap->a_mode & FREAD) {
192 if (fip->fi_readers++ == 0) {
193 fip->fi_writesock->so_state &= ~SS_CANTSENDMORE;
194 if (fip->fi_writers > 0)
195 wakeup(&fip->fi_writers);
196 }
197 }
198 if (ap->a_mode & FWRITE) {
199 if (fip->fi_writers++ == 0) {
200 fip->fi_readsock->so_state &= ~SS_CANTRCVMORE;
201 if (fip->fi_readers > 0)
202 wakeup(&fip->fi_readers);
203 }
204 }
205 if (ap->a_mode & FREAD) {
206 if (ap->a_mode & O_NONBLOCK) {
207 } else {
208 while (!soreadable(fip->fi_readsock) && fip->fi_writers == 0) {
209 VOP_UNLOCK(vp, 0);
210 error = tsleep(&fip->fi_readers,
211 PCATCH | PSOCK, "fifor", 0);
212 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
213 if (error)
214 goto bad;
215 }
216 }
217 }
218 if (ap->a_mode & FWRITE) {
219 if (ap->a_mode & O_NONBLOCK) {
220 if (fip->fi_readers == 0) {
221 error = ENXIO;
222 goto bad;
223 }
224 } else {
225 while (fip->fi_readers == 0) {
226 VOP_UNLOCK(vp, 0);
227 error = tsleep(&fip->fi_writers,
228 PCATCH | PSOCK, "fifow", 0);
229 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
230 if (error)
231 goto bad;
232 }
233 }
234 }
235 return (0);
236 bad:
237 VOP_CLOSE(vp, ap->a_mode, ap->a_cred, ap->a_l);
238 return (error);
239 }
240
241 /*
242 * Vnode op for read
243 */
244 /* ARGSUSED */
245 int
246 fifo_read(void *v)
247 {
248 struct vop_read_args /* {
249 struct vnode *a_vp;
250 struct uio *a_uio;
251 int a_ioflag;
252 struct ucred *a_cred;
253 } */ *ap = v;
254 struct uio *uio;
255 struct socket *rso;
256 int error;
257 size_t startresid;
258
259 uio = ap->a_uio;
260 rso = ap->a_vp->v_fifoinfo->fi_readsock;
261 #ifdef DIAGNOSTIC
262 if (uio->uio_rw != UIO_READ)
263 panic("fifo_read mode");
264 #endif
265 if (uio->uio_resid == 0)
266 return (0);
267 if (ap->a_ioflag & IO_NDELAY)
268 rso->so_state |= SS_NBIO;
269 startresid = uio->uio_resid;
270 VOP_UNLOCK(ap->a_vp, 0);
271 error = (*rso->so_receive)(rso, (struct mbuf **)0, uio,
272 (struct mbuf **)0, (struct mbuf **)0, (int *)0);
273 vn_lock(ap->a_vp, LK_EXCLUSIVE | LK_RETRY);
274 /*
275 * Clear EOF indication after first such return.
276 */
277 if (uio->uio_resid == startresid)
278 rso->so_state &= ~SS_CANTRCVMORE;
279 if (ap->a_ioflag & IO_NDELAY) {
280 rso->so_state &= ~SS_NBIO;
281 if (error == EWOULDBLOCK &&
282 ap->a_vp->v_fifoinfo->fi_writers == 0)
283 error = 0;
284 }
285 return (error);
286 }
287
288 /*
289 * Vnode op for write
290 */
291 /* ARGSUSED */
292 int
293 fifo_write(void *v)
294 {
295 struct vop_write_args /* {
296 struct vnode *a_vp;
297 struct uio *a_uio;
298 int a_ioflag;
299 struct ucred *a_cred;
300 } */ *ap = v;
301 struct socket *wso;
302 int error;
303
304 wso = ap->a_vp->v_fifoinfo->fi_writesock;
305 #ifdef DIAGNOSTIC
306 if (ap->a_uio->uio_rw != UIO_WRITE)
307 panic("fifo_write mode");
308 #endif
309 if (ap->a_ioflag & IO_NDELAY)
310 wso->so_state |= SS_NBIO;
311 VOP_UNLOCK(ap->a_vp, 0);
312 error = (*wso->so_send)(wso, (struct mbuf *)0, ap->a_uio, 0,
313 (struct mbuf *)0, 0, curlwp /*XXX*/);
314 vn_lock(ap->a_vp, LK_EXCLUSIVE | LK_RETRY);
315 if (ap->a_ioflag & IO_NDELAY)
316 wso->so_state &= ~SS_NBIO;
317 return (error);
318 }
319
320 /*
321 * Device ioctl operation.
322 */
323 /* ARGSUSED */
324 int
325 fifo_ioctl(void *v)
326 {
327 struct vop_ioctl_args /* {
328 struct vnode *a_vp;
329 u_long a_command;
330 void *a_data;
331 int a_fflag;
332 struct ucred *a_cred;
333 struct lwp *a_l;
334 } */ *ap = v;
335 struct file filetmp;
336 int error;
337
338 if (ap->a_command == FIONBIO)
339 return (0);
340 if (ap->a_fflag & FREAD) {
341 filetmp.f_data = ap->a_vp->v_fifoinfo->fi_readsock;
342 error = soo_ioctl(&filetmp, ap->a_command, ap->a_data, ap->a_l);
343 if (error)
344 return (error);
345 }
346 if (ap->a_fflag & FWRITE) {
347 filetmp.f_data = ap->a_vp->v_fifoinfo->fi_writesock;
348 error = soo_ioctl(&filetmp, ap->a_command, ap->a_data, ap->a_l);
349 if (error)
350 return (error);
351 }
352 return (0);
353 }
354
355 /* ARGSUSED */
356 int
357 fifo_poll(void *v)
358 {
359 struct vop_poll_args /* {
360 struct vnode *a_vp;
361 int a_events;
362 struct lwp *a_l;
363 } */ *ap = v;
364 struct file filetmp;
365 int revents;
366
367 revents = 0;
368 if (ap->a_events & (POLLIN | POLLPRI | POLLRDNORM | POLLRDBAND)) {
369 filetmp.f_data = ap->a_vp->v_fifoinfo->fi_readsock;
370 if (filetmp.f_data)
371 revents |= soo_poll(&filetmp, ap->a_events, ap->a_l);
372 }
373 if (ap->a_events & (POLLOUT | POLLWRNORM | POLLWRBAND)) {
374 filetmp.f_data = ap->a_vp->v_fifoinfo->fi_writesock;
375 if (filetmp.f_data)
376 revents |= soo_poll(&filetmp, ap->a_events, ap->a_l);
377 }
378
379 return (revents);
380 }
381
382 int
383 fifo_inactive(void *v)
384 {
385 struct vop_inactive_args /* {
386 struct vnode *a_vp;
387 struct lwp *a_l;
388 } */ *ap = v;
389
390 VOP_UNLOCK(ap->a_vp, 0);
391 return (0);
392 }
393
394 /*
395 * This is a noop, simply returning what one has been given.
396 */
397 int
398 fifo_bmap(void *v)
399 {
400 struct vop_bmap_args /* {
401 struct vnode *a_vp;
402 daddr_t a_bn;
403 struct vnode **a_vpp;
404 daddr_t *a_bnp;
405 int *a_runp;
406 } */ *ap = v;
407
408 if (ap->a_vpp != NULL)
409 *ap->a_vpp = ap->a_vp;
410 if (ap->a_bnp != NULL)
411 *ap->a_bnp = ap->a_bn;
412 if (ap->a_runp != NULL)
413 *ap->a_runp = 0;
414 return (0);
415 }
416
417 /*
418 * Device close routine
419 */
420 /* ARGSUSED */
421 int
422 fifo_close(void *v)
423 {
424 struct vop_close_args /* {
425 struct vnode *a_vp;
426 int a_fflag;
427 struct ucred *a_cred;
428 struct lwp *a_l;
429 } */ *ap = v;
430 struct vnode *vp;
431 struct fifoinfo *fip;
432 int isrevoke;
433
434 vp = ap->a_vp;
435 fip = vp->v_fifoinfo;
436 isrevoke = (ap->a_fflag & (FREAD | FWRITE | FNONBLOCK)) == FNONBLOCK;
437 if (isrevoke) {
438 if (fip->fi_readers != 0) {
439 fip->fi_readers = 0;
440 socantsendmore(fip->fi_writesock);
441 }
442 if (fip->fi_writers != 0) {
443 fip->fi_writers = 0;
444 socantrcvmore(fip->fi_readsock);
445 }
446 } else {
447 if ((ap->a_fflag & FREAD) && --fip->fi_readers == 0)
448 socantsendmore(fip->fi_writesock);
449 if ((ap->a_fflag & FWRITE) && --fip->fi_writers == 0)
450 socantrcvmore(fip->fi_readsock);
451 }
452 /* Shut down if all readers and writers are gone. */
453 if ((fip->fi_readers + fip->fi_writers) == 0) {
454 (void) soclose(fip->fi_readsock);
455 (void) soclose(fip->fi_writesock);
456 FREE(fip, M_VNODE);
457 vp->v_fifoinfo = NULL;
458 }
459 return (0);
460 }
461
462 /*
463 * Print out the contents of a fifo vnode.
464 */
465 int
466 fifo_print(void *v)
467 {
468 struct vop_print_args /* {
469 struct vnode *a_vp;
470 } */ *ap = v;
471
472 printf("tag VT_NON");
473 fifo_printinfo(ap->a_vp);
474 printf("\n");
475 return 0;
476 }
477
478 /*
479 * Print out internal contents of a fifo vnode.
480 */
481 void
482 fifo_printinfo(struct vnode *vp)
483 {
484 struct fifoinfo *fip;
485
486 fip = vp->v_fifoinfo;
487 printf(", fifo with %ld readers and %ld writers",
488 fip->fi_readers, fip->fi_writers);
489 }
490
491 /*
492 * Return POSIX pathconf information applicable to fifo's.
493 */
494 int
495 fifo_pathconf(void *v)
496 {
497 struct vop_pathconf_args /* {
498 struct vnode *a_vp;
499 int a_name;
500 register_t *a_retval;
501 } */ *ap = v;
502
503 switch (ap->a_name) {
504 case _PC_LINK_MAX:
505 *ap->a_retval = LINK_MAX;
506 return (0);
507 case _PC_PIPE_BUF:
508 *ap->a_retval = PIPE_BUF;
509 return (0);
510 case _PC_CHOWN_RESTRICTED:
511 *ap->a_retval = 1;
512 return (0);
513 case _PC_SYNC_IO:
514 *ap->a_retval = 1;
515 return (0);
516 default:
517 return (EINVAL);
518 }
519 /* NOTREACHED */
520 }
521
522 static void
523 filt_fifordetach(struct knote *kn)
524 {
525 struct socket *so;
526
527 so = (struct socket *)kn->kn_hook;
528 SLIST_REMOVE(&so->so_rcv.sb_sel.sel_klist, kn, knote, kn_selnext);
529 if (SLIST_EMPTY(&so->so_rcv.sb_sel.sel_klist))
530 so->so_rcv.sb_flags &= ~SB_KNOTE;
531 }
532
533 static int
534 filt_fiforead(struct knote *kn, long hint)
535 {
536 struct socket *so;
537
538 so = (struct socket *)kn->kn_hook;
539 kn->kn_data = so->so_rcv.sb_cc;
540 if (so->so_state & SS_CANTRCVMORE) {
541 kn->kn_flags |= EV_EOF;
542 return (1);
543 }
544 kn->kn_flags &= ~EV_EOF;
545 return (kn->kn_data > 0);
546 }
547
548 static void
549 filt_fifowdetach(struct knote *kn)
550 {
551 struct socket *so;
552
553 so = (struct socket *)kn->kn_hook;
554 SLIST_REMOVE(&so->so_snd.sb_sel.sel_klist, kn, knote, kn_selnext);
555 if (SLIST_EMPTY(&so->so_snd.sb_sel.sel_klist))
556 so->so_snd.sb_flags &= ~SB_KNOTE;
557 }
558
559 static int
560 filt_fifowrite(struct knote *kn, long hint)
561 {
562 struct socket *so;
563
564 so = (struct socket *)kn->kn_hook;
565 kn->kn_data = sbspace(&so->so_snd);
566 if (so->so_state & SS_CANTSENDMORE) {
567 kn->kn_flags |= EV_EOF;
568 return (1);
569 }
570 kn->kn_flags &= ~EV_EOF;
571 return (kn->kn_data >= so->so_snd.sb_lowat);
572 }
573
574 static const struct filterops fiforead_filtops =
575 { 1, NULL, filt_fifordetach, filt_fiforead };
576 static const struct filterops fifowrite_filtops =
577 { 1, NULL, filt_fifowdetach, filt_fifowrite };
578
579 /* ARGSUSED */
580 int
581 fifo_kqfilter(void *v)
582 {
583 struct vop_kqfilter_args /* {
584 struct vnode *a_vp;
585 struct knote *a_kn;
586 } */ *ap = v;
587 struct socket *so;
588 struct sockbuf *sb;
589
590 so = (struct socket *)ap->a_vp->v_fifoinfo->fi_readsock;
591 switch (ap->a_kn->kn_filter) {
592 case EVFILT_READ:
593 ap->a_kn->kn_fop = &fiforead_filtops;
594 sb = &so->so_rcv;
595 break;
596 case EVFILT_WRITE:
597 ap->a_kn->kn_fop = &fifowrite_filtops;
598 sb = &so->so_snd;
599 break;
600 default:
601 return (1);
602 }
603
604 ap->a_kn->kn_hook = so;
605
606 SLIST_INSERT_HEAD(&sb->sb_sel.sel_klist, ap->a_kn, kn_selnext);
607 sb->sb_flags |= SB_KNOTE;
608 return (0);
609 }
610