fifo_vnops.c revision 1.51.4.3 1 /* $NetBSD: fifo_vnops.c,v 1.51.4.3 2008/02/04 09:24:28 yamt 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.51.4.3 2008/02/04 09:24:28 yamt 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)(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_ioctl_desc, fifo_ioctl }, /* ioctl */
83 { &vop_poll_desc, fifo_poll }, /* poll */
84 { &vop_kqfilter_desc, fifo_kqfilter }, /* kqfilter */
85 { &vop_revoke_desc, fifo_revoke }, /* revoke */
86 { &vop_mmap_desc, fifo_mmap }, /* mmap */
87 { &vop_fsync_desc, fifo_fsync }, /* fsync */
88 { &vop_seek_desc, fifo_seek }, /* seek */
89 { &vop_remove_desc, fifo_remove }, /* remove */
90 { &vop_link_desc, fifo_link }, /* link */
91 { &vop_rename_desc, fifo_rename }, /* rename */
92 { &vop_mkdir_desc, fifo_mkdir }, /* mkdir */
93 { &vop_rmdir_desc, fifo_rmdir }, /* rmdir */
94 { &vop_symlink_desc, fifo_symlink }, /* symlink */
95 { &vop_readdir_desc, fifo_readdir }, /* readdir */
96 { &vop_readlink_desc, fifo_readlink }, /* readlink */
97 { &vop_abortop_desc, fifo_abortop }, /* abortop */
98 { &vop_inactive_desc, fifo_inactive }, /* inactive */
99 { &vop_reclaim_desc, fifo_reclaim }, /* reclaim */
100 { &vop_lock_desc, fifo_lock }, /* lock */
101 { &vop_unlock_desc, fifo_unlock }, /* unlock */
102 { &vop_bmap_desc, fifo_bmap }, /* bmap */
103 { &vop_strategy_desc, fifo_strategy }, /* strategy */
104 { &vop_print_desc, fifo_print }, /* print */
105 { &vop_islocked_desc, fifo_islocked }, /* islocked */
106 { &vop_pathconf_desc, fifo_pathconf }, /* pathconf */
107 { &vop_advlock_desc, fifo_advlock }, /* advlock */
108 { &vop_bwrite_desc, fifo_bwrite }, /* bwrite */
109 { &vop_putpages_desc, fifo_putpages }, /* putpages */
110 { (struct vnodeop_desc*)NULL, (int(*)(void *))NULL }
111 };
112 const struct vnodeopv_desc fifo_vnodeop_opv_desc =
113 { &fifo_vnodeop_p, fifo_vnodeop_entries };
114
115 /*
116 * Trivial lookup routine that always fails.
117 */
118 /* ARGSUSED */
119 int
120 fifo_lookup(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(void *v)
139 {
140 struct vop_open_args /* {
141 struct vnode *a_vp;
142 int a_mode;
143 kauth_cred_t a_cred;
144 } */ *ap = v;
145 struct lwp *l = curlwp;
146 struct vnode *vp;
147 struct fifoinfo *fip;
148 struct proc *p;
149 struct socket *rso, *wso;
150 int error;
151
152 vp = ap->a_vp;
153 p = l->l_proc;
154
155 if ((fip = vp->v_fifoinfo) == NULL) {
156 MALLOC(fip, struct fifoinfo *, sizeof(*fip), M_VNODE, M_WAITOK);
157 vp->v_fifoinfo = fip;
158 error = socreate(AF_LOCAL, &rso, SOCK_STREAM, 0, l);
159 if (error != 0) {
160 free(fip, M_VNODE);
161 vp->v_fifoinfo = NULL;
162 return (error);
163 }
164 fip->fi_readsock = rso;
165 error = socreate(AF_LOCAL, &wso, SOCK_STREAM, 0, l);
166 if (error != 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, PRU_CONNECT2)) != 0) {
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 if (ap->a_mode & FREAD) {
185 if (fip->fi_readers++ == 0) {
186 fip->fi_writesock->so_state &= ~SS_CANTSENDMORE;
187 if (fip->fi_writers > 0)
188 wakeup(&fip->fi_writers);
189 }
190 }
191 if (ap->a_mode & FWRITE) {
192 if (fip->fi_writers++ == 0) {
193 fip->fi_readsock->so_state &= ~SS_CANTRCVMORE;
194 if (fip->fi_readers > 0)
195 wakeup(&fip->fi_readers);
196 }
197 }
198 if (ap->a_mode & FREAD) {
199 if (ap->a_mode & O_NONBLOCK) {
200 } else {
201 while (!soreadable(fip->fi_readsock) && fip->fi_writers == 0) {
202 VOP_UNLOCK(vp, 0);
203 error = tsleep(&fip->fi_readers,
204 PCATCH | PSOCK, "fifor", 0);
205 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
206 if (error)
207 goto bad;
208 }
209 }
210 }
211 if (ap->a_mode & FWRITE) {
212 if (ap->a_mode & O_NONBLOCK) {
213 if (fip->fi_readers == 0) {
214 error = ENXIO;
215 goto bad;
216 }
217 } else {
218 while (fip->fi_readers == 0) {
219 VOP_UNLOCK(vp, 0);
220 error = tsleep(&fip->fi_writers,
221 PCATCH | PSOCK, "fifow", 0);
222 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
223 if (error)
224 goto bad;
225 }
226 }
227 }
228 return (0);
229 bad:
230 VOP_CLOSE(vp, ap->a_mode, ap->a_cred);
231 return (error);
232 }
233
234 /*
235 * Vnode op for read
236 */
237 /* ARGSUSED */
238 int
239 fifo_read(void *v)
240 {
241 struct vop_read_args /* {
242 struct vnode *a_vp;
243 struct uio *a_uio;
244 int a_ioflag;
245 kauth_cred_t a_cred;
246 } */ *ap = v;
247 struct uio *uio;
248 struct socket *rso;
249 int error;
250 size_t startresid;
251
252 uio = ap->a_uio;
253 rso = ap->a_vp->v_fifoinfo->fi_readsock;
254 #ifdef DIAGNOSTIC
255 if (uio->uio_rw != UIO_READ)
256 panic("fifo_read mode");
257 #endif
258 if (uio->uio_resid == 0)
259 return (0);
260 if (ap->a_ioflag & IO_NDELAY)
261 rso->so_state |= SS_NBIO;
262 startresid = uio->uio_resid;
263 VOP_UNLOCK(ap->a_vp, 0);
264 error = (*rso->so_receive)(rso, (struct mbuf **)0, uio,
265 (struct mbuf **)0, (struct mbuf **)0, (int *)0);
266 vn_lock(ap->a_vp, LK_EXCLUSIVE | LK_RETRY);
267 /*
268 * Clear EOF indication after first such return.
269 */
270 if (uio->uio_resid == startresid)
271 rso->so_state &= ~SS_CANTRCVMORE;
272 if (ap->a_ioflag & IO_NDELAY) {
273 rso->so_state &= ~SS_NBIO;
274 if (error == EWOULDBLOCK &&
275 ap->a_vp->v_fifoinfo->fi_writers == 0)
276 error = 0;
277 }
278 return (error);
279 }
280
281 /*
282 * Vnode op for write
283 */
284 /* ARGSUSED */
285 int
286 fifo_write(void *v)
287 {
288 struct vop_write_args /* {
289 struct vnode *a_vp;
290 struct uio *a_uio;
291 int a_ioflag;
292 kauth_cred_t a_cred;
293 } */ *ap = v;
294 struct socket *wso;
295 int error;
296
297 wso = ap->a_vp->v_fifoinfo->fi_writesock;
298 #ifdef DIAGNOSTIC
299 if (ap->a_uio->uio_rw != UIO_WRITE)
300 panic("fifo_write mode");
301 #endif
302 if (ap->a_ioflag & IO_NDELAY)
303 wso->so_state |= SS_NBIO;
304 VOP_UNLOCK(ap->a_vp, 0);
305 error = (*wso->so_send)(wso, (struct mbuf *)0, ap->a_uio, 0,
306 (struct mbuf *)0, 0, curlwp /*XXX*/);
307 vn_lock(ap->a_vp, LK_EXCLUSIVE | LK_RETRY);
308 if (ap->a_ioflag & IO_NDELAY)
309 wso->so_state &= ~SS_NBIO;
310 return (error);
311 }
312
313 /*
314 * Device ioctl operation.
315 */
316 /* ARGSUSED */
317 int
318 fifo_ioctl(void *v)
319 {
320 struct vop_ioctl_args /* {
321 struct vnode *a_vp;
322 u_long a_command;
323 void *a_data;
324 int a_fflag;
325 kauth_cred_t a_cred;
326 struct lwp *a_l;
327 } */ *ap = v;
328 struct file filetmp;
329 int error;
330
331 if (ap->a_command == FIONBIO)
332 return (0);
333 if (ap->a_fflag & FREAD) {
334 filetmp.f_data = ap->a_vp->v_fifoinfo->fi_readsock;
335 error = soo_ioctl(&filetmp, ap->a_command, ap->a_data, curlwp);
336 if (error)
337 return (error);
338 }
339 if (ap->a_fflag & FWRITE) {
340 filetmp.f_data = ap->a_vp->v_fifoinfo->fi_writesock;
341 error = soo_ioctl(&filetmp, ap->a_command, ap->a_data, curlwp);
342 if (error)
343 return (error);
344 }
345 return (0);
346 }
347
348 /* ARGSUSED */
349 int
350 fifo_poll(void *v)
351 {
352 struct vop_poll_args /* {
353 struct vnode *a_vp;
354 int a_events;
355 struct lwp *a_l;
356 } */ *ap = v;
357 struct file filetmp;
358 int revents;
359
360 revents = 0;
361 if (ap->a_events & (POLLIN | POLLPRI | POLLRDNORM | POLLRDBAND)) {
362 filetmp.f_data = ap->a_vp->v_fifoinfo->fi_readsock;
363 if (filetmp.f_data)
364 revents |= soo_poll(&filetmp, ap->a_events, curlwp);
365 }
366 if (ap->a_events & (POLLOUT | POLLWRNORM | POLLWRBAND)) {
367 filetmp.f_data = ap->a_vp->v_fifoinfo->fi_writesock;
368 if (filetmp.f_data)
369 revents |= soo_poll(&filetmp, ap->a_events, curlwp);
370 }
371
372 return (revents);
373 }
374
375 int
376 fifo_inactive(void *v)
377 {
378 struct vop_inactive_args /* {
379 struct vnode *a_vp;
380 struct lwp *a_l;
381 } */ *ap = v;
382
383 VOP_UNLOCK(ap->a_vp, 0);
384 return (0);
385 }
386
387 /*
388 * This is a noop, simply returning what one has been given.
389 */
390 int
391 fifo_bmap(void *v)
392 {
393 struct vop_bmap_args /* {
394 struct vnode *a_vp;
395 daddr_t a_bn;
396 struct vnode **a_vpp;
397 daddr_t *a_bnp;
398 int *a_runp;
399 } */ *ap = v;
400
401 if (ap->a_vpp != NULL)
402 *ap->a_vpp = ap->a_vp;
403 if (ap->a_bnp != NULL)
404 *ap->a_bnp = ap->a_bn;
405 if (ap->a_runp != NULL)
406 *ap->a_runp = 0;
407 return (0);
408 }
409
410 /*
411 * Device close routine
412 */
413 /* ARGSUSED */
414 int
415 fifo_close(void *v)
416 {
417 struct vop_close_args /* {
418 struct vnode *a_vp;
419 int a_fflag;
420 kauth_cred_t a_cred;
421 struct lwp *a_l;
422 } */ *ap = v;
423 struct vnode *vp;
424 struct fifoinfo *fip;
425 int isrevoke;
426
427 vp = ap->a_vp;
428 fip = vp->v_fifoinfo;
429 isrevoke = (ap->a_fflag & (FREAD | FWRITE | FNONBLOCK)) == FNONBLOCK;
430 if (isrevoke) {
431 if (fip->fi_readers != 0) {
432 fip->fi_readers = 0;
433 socantsendmore(fip->fi_writesock);
434 }
435 if (fip->fi_writers != 0) {
436 fip->fi_writers = 0;
437 socantrcvmore(fip->fi_readsock);
438 }
439 } else {
440 if ((ap->a_fflag & FREAD) && --fip->fi_readers == 0)
441 socantsendmore(fip->fi_writesock);
442 if ((ap->a_fflag & FWRITE) && --fip->fi_writers == 0)
443 socantrcvmore(fip->fi_readsock);
444 }
445 /* Shut down if all readers and writers are gone. */
446 if ((fip->fi_readers + fip->fi_writers) == 0) {
447 (void) soclose(fip->fi_readsock);
448 (void) soclose(fip->fi_writesock);
449 FREE(fip, M_VNODE);
450 vp->v_fifoinfo = NULL;
451 }
452 return (0);
453 }
454
455 /*
456 * Print out the contents of a fifo vnode.
457 */
458 int
459 fifo_print(void *v)
460 {
461 struct vop_print_args /* {
462 struct vnode *a_vp;
463 } */ *ap = v;
464
465 printf("tag VT_NON");
466 fifo_printinfo(ap->a_vp);
467 printf("\n");
468 return 0;
469 }
470
471 /*
472 * Print out internal contents of a fifo vnode.
473 */
474 void
475 fifo_printinfo(struct vnode *vp)
476 {
477 struct fifoinfo *fip;
478
479 fip = vp->v_fifoinfo;
480 printf(", fifo with %ld readers and %ld writers",
481 fip->fi_readers, fip->fi_writers);
482 }
483
484 /*
485 * Return POSIX pathconf information applicable to fifo's.
486 */
487 int
488 fifo_pathconf(void *v)
489 {
490 struct vop_pathconf_args /* {
491 struct vnode *a_vp;
492 int a_name;
493 register_t *a_retval;
494 } */ *ap = v;
495
496 switch (ap->a_name) {
497 case _PC_LINK_MAX:
498 *ap->a_retval = LINK_MAX;
499 return (0);
500 case _PC_PIPE_BUF:
501 *ap->a_retval = PIPE_BUF;
502 return (0);
503 case _PC_CHOWN_RESTRICTED:
504 *ap->a_retval = 1;
505 return (0);
506 case _PC_SYNC_IO:
507 *ap->a_retval = 1;
508 return (0);
509 default:
510 return (EINVAL);
511 }
512 /* NOTREACHED */
513 }
514
515 static void
516 filt_fifordetach(struct knote *kn)
517 {
518 struct socket *so;
519
520 so = (struct socket *)kn->kn_hook;
521 SLIST_REMOVE(&so->so_rcv.sb_sel.sel_klist, kn, knote, kn_selnext);
522 if (SLIST_EMPTY(&so->so_rcv.sb_sel.sel_klist))
523 so->so_rcv.sb_flags &= ~SB_KNOTE;
524 }
525
526 static int
527 filt_fiforead(struct knote *kn, long hint)
528 {
529 struct socket *so;
530
531 so = (struct socket *)kn->kn_hook;
532 kn->kn_data = so->so_rcv.sb_cc;
533 if (so->so_state & SS_CANTRCVMORE) {
534 kn->kn_flags |= EV_EOF;
535 return (1);
536 }
537 kn->kn_flags &= ~EV_EOF;
538 return (kn->kn_data > 0);
539 }
540
541 static void
542 filt_fifowdetach(struct knote *kn)
543 {
544 struct socket *so;
545
546 so = (struct socket *)kn->kn_hook;
547 SLIST_REMOVE(&so->so_snd.sb_sel.sel_klist, kn, knote, kn_selnext);
548 if (SLIST_EMPTY(&so->so_snd.sb_sel.sel_klist))
549 so->so_snd.sb_flags &= ~SB_KNOTE;
550 }
551
552 static int
553 filt_fifowrite(struct knote *kn, long hint)
554 {
555 struct socket *so;
556
557 so = (struct socket *)kn->kn_hook;
558 kn->kn_data = sbspace(&so->so_snd);
559 if (so->so_state & SS_CANTSENDMORE) {
560 kn->kn_flags |= EV_EOF;
561 return (1);
562 }
563 kn->kn_flags &= ~EV_EOF;
564 return (kn->kn_data >= so->so_snd.sb_lowat);
565 }
566
567 static const struct filterops fiforead_filtops =
568 { 1, NULL, filt_fifordetach, filt_fiforead };
569 static const struct filterops fifowrite_filtops =
570 { 1, NULL, filt_fifowdetach, filt_fifowrite };
571
572 /* ARGSUSED */
573 int
574 fifo_kqfilter(void *v)
575 {
576 struct vop_kqfilter_args /* {
577 struct vnode *a_vp;
578 struct knote *a_kn;
579 } */ *ap = v;
580 struct socket *so;
581 struct sockbuf *sb;
582
583 so = (struct socket *)ap->a_vp->v_fifoinfo->fi_readsock;
584 switch (ap->a_kn->kn_filter) {
585 case EVFILT_READ:
586 ap->a_kn->kn_fop = &fiforead_filtops;
587 sb = &so->so_rcv;
588 break;
589 case EVFILT_WRITE:
590 ap->a_kn->kn_fop = &fifowrite_filtops;
591 sb = &so->so_snd;
592 break;
593 default:
594 return (EINVAL);
595 }
596
597 ap->a_kn->kn_hook = so;
598
599 SLIST_INSERT_HEAD(&sb->sb_sel.sel_klist, ap->a_kn, kn_selnext);
600 sb->sb_flags |= SB_KNOTE;
601 return (0);
602 }
603