fifo_vnops.c revision 1.62 1 /* $NetBSD: fifo_vnops.c,v 1.62 2008/02/11 23:53:32 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.62 2008/02/11 23:53:32 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 #include <sys/atomic.h>
55
56 #include <miscfs/fifofs/fifo.h>
57 #include <miscfs/genfs/genfs.h>
58
59 /*
60 * This structure is associated with the FIFO vnode and stores
61 * the state associated with the FIFO.
62 */
63 struct fifoinfo {
64 struct socket *fi_readsock;
65 struct socket *fi_writesock;
66 long fi_readers;
67 long fi_writers;
68 };
69
70 int (**fifo_vnodeop_p)(void *);
71 const struct vnodeopv_entry_desc fifo_vnodeop_entries[] = {
72 { &vop_default_desc, vn_default_error },
73 { &vop_lookup_desc, fifo_lookup }, /* lookup */
74 { &vop_create_desc, fifo_create }, /* create */
75 { &vop_mknod_desc, fifo_mknod }, /* mknod */
76 { &vop_open_desc, fifo_open }, /* open */
77 { &vop_close_desc, fifo_close }, /* close */
78 { &vop_access_desc, fifo_access }, /* access */
79 { &vop_getattr_desc, fifo_getattr }, /* getattr */
80 { &vop_setattr_desc, fifo_setattr }, /* setattr */
81 { &vop_read_desc, fifo_read }, /* read */
82 { &vop_write_desc, fifo_write }, /* write */
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_bwrite_desc, fifo_bwrite }, /* bwrite */
110 { &vop_putpages_desc, fifo_putpages }, /* putpages */
111 { (struct vnodeop_desc*)NULL, (int(*)(void *))NULL }
112 };
113 const struct vnodeopv_desc fifo_vnodeop_opv_desc =
114 { &fifo_vnodeop_p, fifo_vnodeop_entries };
115
116 /*
117 * Trivial lookup routine that always fails.
118 */
119 /* ARGSUSED */
120 int
121 fifo_lookup(void *v)
122 {
123 struct vop_lookup_args /* {
124 struct vnode *a_dvp;
125 struct vnode **a_vpp;
126 struct componentname *a_cnp;
127 } */ *ap = v;
128
129 *ap->a_vpp = NULL;
130 return (ENOTDIR);
131 }
132
133 /*
134 * Open called to set up a new instance of a fifo or
135 * to find an active instance of a fifo.
136 */
137 /* ARGSUSED */
138 int
139 fifo_open(void *v)
140 {
141 struct vop_open_args /* {
142 struct vnode *a_vp;
143 int a_mode;
144 kauth_cred_t a_cred;
145 } */ *ap = v;
146 struct lwp *l = curlwp;
147 struct vnode *vp;
148 struct fifoinfo *fip;
149 struct proc *p;
150 struct socket *rso, *wso;
151 int error;
152
153 vp = ap->a_vp;
154 p = l->l_proc;
155
156 KERNEL_LOCK(1, NULL);
157 if ((fip = vp->v_fifoinfo) == NULL) {
158 MALLOC(fip, struct fifoinfo *, sizeof(*fip), M_VNODE, M_WAITOK);
159 vp->v_fifoinfo = fip;
160 error = socreate(AF_LOCAL, &rso, SOCK_STREAM, 0, l);
161 if (error != 0) {
162 free(fip, M_VNODE);
163 vp->v_fifoinfo = NULL;
164 goto done;
165 }
166 fip->fi_readsock = rso;
167 error = socreate(AF_LOCAL, &wso, SOCK_STREAM, 0, l);
168 if (error != 0) {
169 (void)soclose(rso);
170 free(fip, M_VNODE);
171 vp->v_fifoinfo = NULL;
172 goto done;
173 }
174 fip->fi_writesock = wso;
175 if ((error = unp_connect2(wso, rso, PRU_CONNECT2)) != 0) {
176 (void)soclose(wso);
177 (void)soclose(rso);
178 free(fip, M_VNODE);
179 vp->v_fifoinfo = NULL;
180 goto done;
181 }
182 fip->fi_readers = fip->fi_writers = 0;
183 wso->so_state |= SS_CANTRCVMORE;
184 rso->so_state |= SS_CANTSENDMORE;
185 }
186 if (ap->a_mode & FREAD) {
187 if (fip->fi_readers++ == 0) {
188 fip->fi_writesock->so_state &= ~SS_CANTSENDMORE;
189 if (fip->fi_writers > 0)
190 wakeup(&fip->fi_writers);
191 }
192 }
193 if (ap->a_mode & FWRITE) {
194 if (fip->fi_writers++ == 0) {
195 fip->fi_readsock->so_state &= ~SS_CANTRCVMORE;
196 if (fip->fi_readers > 0)
197 wakeup(&fip->fi_readers);
198 }
199 }
200 if (ap->a_mode & FREAD) {
201 if (ap->a_mode & O_NONBLOCK) {
202 } else {
203 while (!soreadable(fip->fi_readsock) && fip->fi_writers == 0) {
204 VOP_UNLOCK(vp, 0);
205 error = tsleep(&fip->fi_readers,
206 PCATCH | PSOCK, "fifor", 0);
207 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
208 if (error)
209 goto bad;
210 }
211 }
212 }
213 if (ap->a_mode & FWRITE) {
214 if (ap->a_mode & O_NONBLOCK) {
215 if (fip->fi_readers == 0) {
216 error = ENXIO;
217 goto bad;
218 }
219 } else {
220 while (fip->fi_readers == 0) {
221 VOP_UNLOCK(vp, 0);
222 error = tsleep(&fip->fi_writers,
223 PCATCH | PSOCK, "fifow", 0);
224 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
225 if (error)
226 goto bad;
227 }
228 }
229 }
230 KERNEL_UNLOCK_ONE(NULL);
231 return (0);
232 bad:
233 VOP_CLOSE(vp, ap->a_mode, ap->a_cred);
234 done:
235 KERNEL_UNLOCK_ONE(NULL);
236 return (error);
237 }
238
239 /*
240 * Vnode op for read
241 */
242 /* ARGSUSED */
243 int
244 fifo_read(void *v)
245 {
246 struct vop_read_args /* {
247 struct vnode *a_vp;
248 struct uio *a_uio;
249 int a_ioflag;
250 kauth_cred_t a_cred;
251 } */ *ap = v;
252 struct uio *uio;
253 struct socket *rso;
254 int error;
255 size_t startresid;
256
257 uio = ap->a_uio;
258 rso = ap->a_vp->v_fifoinfo->fi_readsock;
259 #ifdef DIAGNOSTIC
260 if (uio->uio_rw != UIO_READ)
261 panic("fifo_read mode");
262 #endif
263 if (uio->uio_resid == 0)
264 return (0);
265 KERNEL_LOCK(1, NULL);
266 if (ap->a_ioflag & IO_NDELAY)
267 rso->so_nbio = 1;
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_nbio = 0;
280 if (error == EWOULDBLOCK &&
281 ap->a_vp->v_fifoinfo->fi_writers == 0)
282 error = 0;
283 }
284 KERNEL_UNLOCK_ONE(NULL);
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 kauth_cred_t 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 KERNEL_LOCK(1, NULL);
310 if (ap->a_ioflag & IO_NDELAY)
311 wso->so_nbio = 1;
312 VOP_UNLOCK(ap->a_vp, 0);
313 error = (*wso->so_send)(wso, (struct mbuf *)0, ap->a_uio, 0,
314 (struct mbuf *)0, 0, curlwp /*XXX*/);
315 vn_lock(ap->a_vp, LK_EXCLUSIVE | LK_RETRY);
316 if (ap->a_ioflag & IO_NDELAY)
317 wso->so_nbio = 0;
318 KERNEL_UNLOCK_ONE(NULL);
319 return (error);
320 }
321
322 /*
323 * Device ioctl operation.
324 */
325 /* ARGSUSED */
326 int
327 fifo_ioctl(void *v)
328 {
329 struct vop_ioctl_args /* {
330 struct vnode *a_vp;
331 u_long a_command;
332 void *a_data;
333 int a_fflag;
334 kauth_cred_t a_cred;
335 struct lwp *a_l;
336 } */ *ap = v;
337 struct file filetmp;
338 int error;
339
340 if (ap->a_command == FIONBIO)
341 return (0);
342 if (ap->a_fflag & FREAD) {
343 filetmp.f_data = ap->a_vp->v_fifoinfo->fi_readsock;
344 error = soo_ioctl(&filetmp, ap->a_command, ap->a_data, curlwp);
345 if (error)
346 return (error);
347 }
348 if (ap->a_fflag & FWRITE) {
349 filetmp.f_data = ap->a_vp->v_fifoinfo->fi_writesock;
350 error = soo_ioctl(&filetmp, ap->a_command, ap->a_data, curlwp);
351 if (error)
352 return (error);
353 }
354 return (0);
355 }
356
357 /* ARGSUSED */
358 int
359 fifo_poll(void *v)
360 {
361 struct vop_poll_args /* {
362 struct vnode *a_vp;
363 int a_events;
364 struct lwp *a_l;
365 } */ *ap = v;
366 struct file filetmp;
367 int revents;
368
369 revents = 0;
370 if (ap->a_events & (POLLIN | POLLPRI | POLLRDNORM | POLLRDBAND)) {
371 filetmp.f_data = ap->a_vp->v_fifoinfo->fi_readsock;
372 if (filetmp.f_data)
373 revents |= soo_poll(&filetmp, ap->a_events, curlwp);
374 }
375 if (ap->a_events & (POLLOUT | POLLWRNORM | POLLWRBAND)) {
376 filetmp.f_data = ap->a_vp->v_fifoinfo->fi_writesock;
377 if (filetmp.f_data)
378 revents |= soo_poll(&filetmp, ap->a_events, curlwp);
379 }
380
381 return (revents);
382 }
383
384 int
385 fifo_inactive(void *v)
386 {
387 struct vop_inactive_args /* {
388 struct vnode *a_vp;
389 struct lwp *a_l;
390 } */ *ap = v;
391
392 VOP_UNLOCK(ap->a_vp, 0);
393 return (0);
394 }
395
396 /*
397 * This is a noop, simply returning what one has been given.
398 */
399 int
400 fifo_bmap(void *v)
401 {
402 struct vop_bmap_args /* {
403 struct vnode *a_vp;
404 daddr_t a_bn;
405 struct vnode **a_vpp;
406 daddr_t *a_bnp;
407 int *a_runp;
408 } */ *ap = v;
409
410 if (ap->a_vpp != NULL)
411 *ap->a_vpp = ap->a_vp;
412 if (ap->a_bnp != NULL)
413 *ap->a_bnp = ap->a_bn;
414 if (ap->a_runp != NULL)
415 *ap->a_runp = 0;
416 return (0);
417 }
418
419 /*
420 * Device close routine
421 */
422 /* ARGSUSED */
423 int
424 fifo_close(void *v)
425 {
426 struct vop_close_args /* {
427 struct vnode *a_vp;
428 int a_fflag;
429 kauth_cred_t a_cred;
430 struct lwp *a_l;
431 } */ *ap = v;
432 struct vnode *vp;
433 struct fifoinfo *fip;
434 int isrevoke;
435
436 vp = ap->a_vp;
437 fip = vp->v_fifoinfo;
438 isrevoke = (ap->a_fflag & (FREAD | FWRITE | FNONBLOCK)) == FNONBLOCK;
439 KERNEL_LOCK(1, NULL);
440 if (isrevoke) {
441 if (fip->fi_readers != 0) {
442 fip->fi_readers = 0;
443 socantsendmore(fip->fi_writesock);
444 }
445 if (fip->fi_writers != 0) {
446 fip->fi_writers = 0;
447 socantrcvmore(fip->fi_readsock);
448 }
449 } else {
450 if ((ap->a_fflag & FREAD) && --fip->fi_readers == 0)
451 socantsendmore(fip->fi_writesock);
452 if ((ap->a_fflag & FWRITE) && --fip->fi_writers == 0)
453 socantrcvmore(fip->fi_readsock);
454 }
455 /* Shut down if all readers and writers are gone. */
456 if ((fip->fi_readers + fip->fi_writers) == 0) {
457 (void) soclose(fip->fi_readsock);
458 (void) soclose(fip->fi_writesock);
459 FREE(fip, M_VNODE);
460 vp->v_fifoinfo = NULL;
461 }
462 KERNEL_UNLOCK_ONE(NULL);
463 return (0);
464 }
465
466 /*
467 * Print out the contents of a fifo vnode.
468 */
469 int
470 fifo_print(void *v)
471 {
472 struct vop_print_args /* {
473 struct vnode *a_vp;
474 } */ *ap = v;
475
476 printf("tag VT_NON");
477 fifo_printinfo(ap->a_vp);
478 printf("\n");
479 return 0;
480 }
481
482 /*
483 * Print out internal contents of a fifo vnode.
484 */
485 void
486 fifo_printinfo(struct vnode *vp)
487 {
488 struct fifoinfo *fip;
489
490 fip = vp->v_fifoinfo;
491 printf(", fifo with %ld readers and %ld writers",
492 fip->fi_readers, fip->fi_writers);
493 }
494
495 /*
496 * Return POSIX pathconf information applicable to fifo's.
497 */
498 int
499 fifo_pathconf(void *v)
500 {
501 struct vop_pathconf_args /* {
502 struct vnode *a_vp;
503 int a_name;
504 register_t *a_retval;
505 } */ *ap = v;
506
507 switch (ap->a_name) {
508 case _PC_LINK_MAX:
509 *ap->a_retval = LINK_MAX;
510 return (0);
511 case _PC_PIPE_BUF:
512 *ap->a_retval = PIPE_BUF;
513 return (0);
514 case _PC_CHOWN_RESTRICTED:
515 *ap->a_retval = 1;
516 return (0);
517 case _PC_SYNC_IO:
518 *ap->a_retval = 1;
519 return (0);
520 default:
521 return (EINVAL);
522 }
523 /* NOTREACHED */
524 }
525
526 static void
527 filt_fifordetach(struct knote *kn)
528 {
529 struct socket *so;
530
531 so = (struct socket *)kn->kn_hook;
532 SLIST_REMOVE(&so->so_rcv.sb_sel.sel_klist, kn, knote, kn_selnext);
533 if (SLIST_EMPTY(&so->so_rcv.sb_sel.sel_klist))
534 so->so_rcv.sb_flags &= ~SB_KNOTE;
535 }
536
537 static int
538 filt_fiforead(struct knote *kn, long hint)
539 {
540 struct socket *so;
541
542 so = (struct socket *)kn->kn_hook;
543 kn->kn_data = so->so_rcv.sb_cc;
544 if (so->so_state & SS_CANTRCVMORE) {
545 kn->kn_flags |= EV_EOF;
546 return (1);
547 }
548 kn->kn_flags &= ~EV_EOF;
549 return (kn->kn_data > 0);
550 }
551
552 static void
553 filt_fifowdetach(struct knote *kn)
554 {
555 struct socket *so;
556
557 so = (struct socket *)kn->kn_hook;
558 SLIST_REMOVE(&so->so_snd.sb_sel.sel_klist, kn, knote, kn_selnext);
559 if (SLIST_EMPTY(&so->so_snd.sb_sel.sel_klist))
560 so->so_snd.sb_flags &= ~SB_KNOTE;
561 }
562
563 static int
564 filt_fifowrite(struct knote *kn, long hint)
565 {
566 struct socket *so;
567
568 so = (struct socket *)kn->kn_hook;
569 kn->kn_data = sbspace(&so->so_snd);
570 if (so->so_state & SS_CANTSENDMORE) {
571 kn->kn_flags |= EV_EOF;
572 return (1);
573 }
574 kn->kn_flags &= ~EV_EOF;
575 return (kn->kn_data >= so->so_snd.sb_lowat);
576 }
577
578 static const struct filterops fiforead_filtops =
579 { 1, NULL, filt_fifordetach, filt_fiforead };
580 static const struct filterops fifowrite_filtops =
581 { 1, NULL, filt_fifowdetach, filt_fifowrite };
582
583 /* ARGSUSED */
584 int
585 fifo_kqfilter(void *v)
586 {
587 struct vop_kqfilter_args /* {
588 struct vnode *a_vp;
589 struct knote *a_kn;
590 } */ *ap = v;
591 struct socket *so;
592 struct sockbuf *sb;
593
594 so = (struct socket *)ap->a_vp->v_fifoinfo->fi_readsock;
595 switch (ap->a_kn->kn_filter) {
596 case EVFILT_READ:
597 ap->a_kn->kn_fop = &fiforead_filtops;
598 sb = &so->so_rcv;
599 break;
600 case EVFILT_WRITE:
601 ap->a_kn->kn_fop = &fifowrite_filtops;
602 sb = &so->so_snd;
603 break;
604 default:
605 return (EINVAL);
606 }
607
608 ap->a_kn->kn_hook = so;
609
610 SLIST_INSERT_HEAD(&sb->sb_sel.sel_klist, ap->a_kn, kn_selnext);
611 sb->sb_flags |= SB_KNOTE;
612 return (0);
613 }
614