1 /* $NetBSD: fifo_vnops.c,v 1.51 2005/02/26 22:59:00 perry 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 2005/02/26 22:59:00 perry 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 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 error = socreate(AF_LOCAL, &rso, SOCK_STREAM, 0, p);
165 if (error != 0) {
166 free(fip, M_VNODE);
167 vp->v_fifoinfo = NULL;
168 return (error);
169 }
170 fip->fi_readsock = rso;
171 error = socreate(AF_LOCAL, &wso, SOCK_STREAM, 0, p);
172 if (error != 0) {
173 (void)soclose(rso);
174 free(fip, M_VNODE);
175 vp->v_fifoinfo = NULL;
176 return (error);
177 }
178 fip->fi_writesock = wso;
179 if ((error = unp_connect2(wso, rso, PRU_CONNECT2)) != 0) {
180 (void)soclose(wso);
181 (void)soclose(rso);
182 free(fip, M_VNODE);
183 vp->v_fifoinfo = NULL;
184 return (error);
185 }
186 fip->fi_readers = fip->fi_writers = 0;
187 wso->so_state |= SS_CANTRCVMORE;
188 rso->so_state |= SS_CANTSENDMORE;
189 }
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(&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(&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(&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(&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, curproc /*XXX*/);
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 void *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 = 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 = 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 = 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 = 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 int isrevoke;
432
433 vp = ap->a_vp;
434 fip = vp->v_fifoinfo;
435 isrevoke = (ap->a_fflag & (FREAD | FWRITE | FNONBLOCK)) == FNONBLOCK;
436 if (isrevoke) {
437 if (fip->fi_readers != 0) {
438 fip->fi_readers = 0;
439 socantsendmore(fip->fi_writesock);
440 }
441 if (fip->fi_writers != 0) {
442 fip->fi_writers = 0;
443 socantrcvmore(fip->fi_readsock);
444 }
445 } else {
446 if ((ap->a_fflag & FREAD) && --fip->fi_readers == 0)
447 socantsendmore(fip->fi_writesock);
448 if ((ap->a_fflag & FWRITE) && --fip->fi_writers == 0)
449 socantrcvmore(fip->fi_readsock);
450 }
451 /* Shut down if all readers and writers are gone. */
452 if ((fip->fi_readers + fip->fi_writers) == 0) {
453 (void) soclose(fip->fi_readsock);
454 (void) soclose(fip->fi_writesock);
455 FREE(fip, M_VNODE);
456 vp->v_fifoinfo = NULL;
457 }
458 return (0);
459 }
460
461 /*
462 * Print out the contents of a fifo vnode.
463 */
464 int
465 fifo_print(void *v)
466 {
467 struct vop_print_args /* {
468 struct vnode *a_vp;
469 } */ *ap = v;
470
471 printf("tag VT_NON");
472 fifo_printinfo(ap->a_vp);
473 printf("\n");
474 return 0;
475 }
476
477 /*
478 * Print out internal contents of a fifo vnode.
479 */
480 void
481 fifo_printinfo(struct vnode *vp)
482 {
483 struct fifoinfo *fip;
484
485 fip = vp->v_fifoinfo;
486 printf(", fifo with %ld readers and %ld writers",
487 fip->fi_readers, fip->fi_writers);
488 }
489
490 /*
491 * Return POSIX pathconf information applicable to fifo's.
492 */
493 int
494 fifo_pathconf(void *v)
495 {
496 struct vop_pathconf_args /* {
497 struct vnode *a_vp;
498 int a_name;
499 register_t *a_retval;
500 } */ *ap = v;
501
502 switch (ap->a_name) {
503 case _PC_LINK_MAX:
504 *ap->a_retval = LINK_MAX;
505 return (0);
506 case _PC_PIPE_BUF:
507 *ap->a_retval = PIPE_BUF;
508 return (0);
509 case _PC_CHOWN_RESTRICTED:
510 *ap->a_retval = 1;
511 return (0);
512 case _PC_SYNC_IO:
513 *ap->a_retval = 1;
514 return (0);
515 default:
516 return (EINVAL);
517 }
518 /* NOTREACHED */
519 }
520
521 static void
522 filt_fifordetach(struct knote *kn)
523 {
524 struct socket *so;
525
526 so = (struct socket *)kn->kn_hook;
527 SLIST_REMOVE(&so->so_rcv.sb_sel.sel_klist, kn, knote, kn_selnext);
528 if (SLIST_EMPTY(&so->so_rcv.sb_sel.sel_klist))
529 so->so_rcv.sb_flags &= ~SB_KNOTE;
530 }
531
532 static int
533 filt_fiforead(struct knote *kn, long hint)
534 {
535 struct socket *so;
536
537 so = (struct socket *)kn->kn_hook;
538 kn->kn_data = so->so_rcv.sb_cc;
539 if (so->so_state & SS_CANTRCVMORE) {
540 kn->kn_flags |= EV_EOF;
541 return (1);
542 }
543 kn->kn_flags &= ~EV_EOF;
544 return (kn->kn_data > 0);
545 }
546
547 static void
548 filt_fifowdetach(struct knote *kn)
549 {
550 struct socket *so;
551
552 so = (struct socket *)kn->kn_hook;
553 SLIST_REMOVE(&so->so_snd.sb_sel.sel_klist, kn, knote, kn_selnext);
554 if (SLIST_EMPTY(&so->so_snd.sb_sel.sel_klist))
555 so->so_snd.sb_flags &= ~SB_KNOTE;
556 }
557
558 static int
559 filt_fifowrite(struct knote *kn, long hint)
560 {
561 struct socket *so;
562
563 so = (struct socket *)kn->kn_hook;
564 kn->kn_data = sbspace(&so->so_snd);
565 if (so->so_state & SS_CANTSENDMORE) {
566 kn->kn_flags |= EV_EOF;
567 return (1);
568 }
569 kn->kn_flags &= ~EV_EOF;
570 return (kn->kn_data >= so->so_snd.sb_lowat);
571 }
572
573 static const struct filterops fiforead_filtops =
574 { 1, NULL, filt_fifordetach, filt_fiforead };
575 static const struct filterops fifowrite_filtops =
576 { 1, NULL, filt_fifowdetach, filt_fifowrite };
577
578 /* ARGSUSED */
579 int
580 fifo_kqfilter(void *v)
581 {
582 struct vop_kqfilter_args /* {
583 struct vnode *a_vp;
584 struct knote *a_kn;
585 } */ *ap = v;
586 struct socket *so;
587 struct sockbuf *sb;
588
589 so = (struct socket *)ap->a_vp->v_fifoinfo->fi_readsock;
590 switch (ap->a_kn->kn_filter) {
591 case EVFILT_READ:
592 ap->a_kn->kn_fop = &fiforead_filtops;
593 sb = &so->so_rcv;
594 break;
595 case EVFILT_WRITE:
596 ap->a_kn->kn_fop = &fifowrite_filtops;
597 sb = &so->so_snd;
598 break;
599 default:
600 return (1);
601 }
602
603 ap->a_kn->kn_hook = so;
604
605 SLIST_INSERT_HEAD(&sb->sb_sel.sel_klist, ap->a_kn, kn_selnext);
606 sb->sb_flags |= SB_KNOTE;
607 return (0);
608 }
Cache object: f463e77582b271d3509b1716279f5b9f
|