1 /* $NetBSD: fifo_vnops.c,v 1.46 2004/03/06 00:38:29 wrstuden 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.46 2004/03/06 00:38:29 wrstuden 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_opencount;
66 long fi_readers;
67 long fi_writers;
68 };
69
70 int (**fifo_vnodeop_p) __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_lease_desc, fifo_lease_check }, /* lease */
84 { &vop_ioctl_desc, fifo_ioctl }, /* ioctl */
85 { &vop_poll_desc, fifo_poll }, /* poll */
86 { &vop_kqfilter_desc, fifo_kqfilter }, /* kqfilter */
87 { &vop_revoke_desc, fifo_revoke }, /* revoke */
88 { &vop_mmap_desc, fifo_mmap }, /* mmap */
89 { &vop_fsync_desc, fifo_fsync }, /* fsync */
90 { &vop_seek_desc, fifo_seek }, /* seek */
91 { &vop_remove_desc, fifo_remove }, /* remove */
92 { &vop_link_desc, fifo_link }, /* link */
93 { &vop_rename_desc, fifo_rename }, /* rename */
94 { &vop_mkdir_desc, fifo_mkdir }, /* mkdir */
95 { &vop_rmdir_desc, fifo_rmdir }, /* rmdir */
96 { &vop_symlink_desc, fifo_symlink }, /* symlink */
97 { &vop_readdir_desc, fifo_readdir }, /* readdir */
98 { &vop_readlink_desc, fifo_readlink }, /* readlink */
99 { &vop_abortop_desc, fifo_abortop }, /* abortop */
100 { &vop_inactive_desc, fifo_inactive }, /* inactive */
101 { &vop_reclaim_desc, fifo_reclaim }, /* reclaim */
102 { &vop_lock_desc, fifo_lock }, /* lock */
103 { &vop_unlock_desc, fifo_unlock }, /* unlock */
104 { &vop_bmap_desc, fifo_bmap }, /* bmap */
105 { &vop_strategy_desc, fifo_strategy }, /* strategy */
106 { &vop_print_desc, fifo_print }, /* print */
107 { &vop_islocked_desc, fifo_islocked }, /* islocked */
108 { &vop_pathconf_desc, fifo_pathconf }, /* pathconf */
109 { &vop_advlock_desc, fifo_advlock }, /* advlock */
110 { &vop_blkatoff_desc, fifo_blkatoff }, /* blkatoff */
111 { &vop_valloc_desc, fifo_valloc }, /* valloc */
112 { &vop_vfree_desc, fifo_vfree }, /* vfree */
113 { &vop_truncate_desc, fifo_truncate }, /* truncate */
114 { &vop_update_desc, fifo_update }, /* update */
115 { &vop_bwrite_desc, fifo_bwrite }, /* bwrite */
116 { &vop_putpages_desc, fifo_putpages }, /* putpages */
117 { (struct vnodeop_desc*)NULL, (int(*) __P((void *)))NULL }
118 };
119 const struct vnodeopv_desc fifo_vnodeop_opv_desc =
120 { &fifo_vnodeop_p, fifo_vnodeop_entries };
121
122 /*
123 * Trivial lookup routine that always fails.
124 */
125 /* ARGSUSED */
126 int
127 fifo_lookup(void *v)
128 {
129 struct vop_lookup_args /* {
130 struct vnode *a_dvp;
131 struct vnode **a_vpp;
132 struct componentname *a_cnp;
133 } */ *ap = v;
134
135 *ap->a_vpp = NULL;
136 return (ENOTDIR);
137 }
138
139 /*
140 * Open called to set up a new instance of a fifo or
141 * to find an active instance of a fifo.
142 */
143 /* ARGSUSED */
144 int
145 fifo_open(void *v)
146 {
147 struct vop_open_args /* {
148 struct vnode *a_vp;
149 int a_mode;
150 struct ucred *a_cred;
151 struct proc *a_p;
152 } */ *ap = v;
153 struct vnode *vp;
154 struct fifoinfo *fip;
155 struct proc *p;
156 struct socket *rso, *wso;
157 int error;
158
159 vp = ap->a_vp;
160 p = ap->a_p;
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 if ((error = socreate(AF_LOCAL, &rso, SOCK_STREAM, 0)) != 0) {
166 free(fip, M_VNODE);
167 vp->v_fifoinfo = NULL;
168 return (error);
169 }
170 fip->fi_readsock = rso;
171 if ((error = socreate(AF_LOCAL, &wso, SOCK_STREAM, 0)) != 0) {
172 (void)soclose(rso);
173 free(fip, M_VNODE);
174 vp->v_fifoinfo = NULL;
175 return (error);
176 }
177 fip->fi_writesock = wso;
178 if ((error = unp_connect2(wso, rso, PRU_CONNECT2)) != 0) {
179 (void)soclose(wso);
180 (void)soclose(rso);
181 free(fip, M_VNODE);
182 vp->v_fifoinfo = NULL;
183 return (error);
184 }
185 fip->fi_readers = fip->fi_writers = 0;
186 fip->fi_opencount = 0;
187 wso->so_state |= SS_CANTRCVMORE;
188 rso->so_state |= SS_CANTSENDMORE;
189 }
190 fip->fi_opencount++;
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((caddr_t)&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((caddr_t)&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((caddr_t)&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((caddr_t)&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, p);
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);
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 caddr_t a_data;
331 int a_fflag;
332 struct ucred *a_cred;
333 struct proc *a_p;
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 = (caddr_t)ap->a_vp->v_fifoinfo->fi_readsock;
342 error = soo_ioctl(&filetmp, ap->a_command, ap->a_data, ap->a_p);
343 if (error)
344 return (error);
345 }
346 if (ap->a_fflag & FWRITE) {
347 filetmp.f_data = (caddr_t)ap->a_vp->v_fifoinfo->fi_writesock;
348 error = soo_ioctl(&filetmp, ap->a_command, ap->a_data, ap->a_p);
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 proc *a_p;
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 = (caddr_t)ap->a_vp->v_fifoinfo->fi_readsock;
370 if (filetmp.f_data)
371 revents |= soo_poll(&filetmp, ap->a_events, ap->a_p);
372 }
373 if (ap->a_events & (POLLOUT | POLLWRNORM | POLLWRBAND)) {
374 filetmp.f_data = (caddr_t)ap->a_vp->v_fifoinfo->fi_writesock;
375 if (filetmp.f_data)
376 revents |= soo_poll(&filetmp, ap->a_events, ap->a_p);
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 proc *a_p;
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 proc *a_p;
429 } */ *ap = v;
430 struct vnode *vp;
431 struct fifoinfo *fip;
432
433 vp = ap->a_vp;
434 fip = vp->v_fifoinfo;
435 if (ap->a_fflag & FREAD) {
436 if (--fip->fi_readers == 0)
437 socantsendmore(fip->fi_writesock);
438 }
439 if (ap->a_fflag & FWRITE) {
440 if (--fip->fi_writers == 0)
441 socantrcvmore(fip->fi_readsock);
442 }
443 /*
444 * shut down if either last close, or if close called from
445 * vclean()
446 */
447 if ((--fip->fi_opencount == 0)
448 || ((ap->a_fflag & (FREAD | FWRITE | FNONBLOCK)) == FNONBLOCK)) {
449 (void) soclose(fip->fi_readsock);
450 (void) soclose(fip->fi_writesock);
451 FREE(fip, M_VNODE);
452 vp->v_fifoinfo = NULL;
453 }
454 return (0);
455 }
456
457 /*
458 * Print out the contents of a fifo vnode.
459 */
460 int
461 fifo_print(void *v)
462 {
463 struct vop_print_args /* {
464 struct vnode *a_vp;
465 } */ *ap = v;
466
467 printf("tag VT_NON");
468 fifo_printinfo(ap->a_vp);
469 printf("\n");
470 return 0;
471 }
472
473 /*
474 * Print out internal contents of a fifo vnode.
475 */
476 void
477 fifo_printinfo(struct vnode *vp)
478 {
479 struct fifoinfo *fip;
480
481 fip = vp->v_fifoinfo;
482 printf(", fifo with %ld readers and %ld writers",
483 fip->fi_readers, fip->fi_writers);
484 }
485
486 /*
487 * Return POSIX pathconf information applicable to fifo's.
488 */
489 int
490 fifo_pathconf(void *v)
491 {
492 struct vop_pathconf_args /* {
493 struct vnode *a_vp;
494 int a_name;
495 register_t *a_retval;
496 } */ *ap = v;
497
498 switch (ap->a_name) {
499 case _PC_LINK_MAX:
500 *ap->a_retval = LINK_MAX;
501 return (0);
502 case _PC_PIPE_BUF:
503 *ap->a_retval = PIPE_BUF;
504 return (0);
505 case _PC_CHOWN_RESTRICTED:
506 *ap->a_retval = 1;
507 return (0);
508 case _PC_SYNC_IO:
509 *ap->a_retval = 1;
510 return (0);
511 default:
512 return (EINVAL);
513 }
514 /* NOTREACHED */
515 }
516
517 static void
518 filt_fifordetach(struct knote *kn)
519 {
520 struct socket *so;
521
522 so = (struct socket *)kn->kn_hook;
523 SLIST_REMOVE(&so->so_rcv.sb_sel.sel_klist, kn, knote, kn_selnext);
524 if (SLIST_EMPTY(&so->so_rcv.sb_sel.sel_klist))
525 so->so_rcv.sb_flags &= ~SB_KNOTE;
526 }
527
528 static int
529 filt_fiforead(struct knote *kn, long hint)
530 {
531 struct socket *so;
532
533 so = (struct socket *)kn->kn_hook;
534 kn->kn_data = so->so_rcv.sb_cc;
535 if (so->so_state & SS_CANTRCVMORE) {
536 kn->kn_flags |= EV_EOF;
537 return (1);
538 }
539 kn->kn_flags &= ~EV_EOF;
540 return (kn->kn_data > 0);
541 }
542
543 static void
544 filt_fifowdetach(struct knote *kn)
545 {
546 struct socket *so;
547
548 so = (struct socket *)kn->kn_hook;
549 SLIST_REMOVE(&so->so_snd.sb_sel.sel_klist, kn, knote, kn_selnext);
550 if (SLIST_EMPTY(&so->so_snd.sb_sel.sel_klist))
551 so->so_snd.sb_flags &= ~SB_KNOTE;
552 }
553
554 static int
555 filt_fifowrite(struct knote *kn, long hint)
556 {
557 struct socket *so;
558
559 so = (struct socket *)kn->kn_hook;
560 kn->kn_data = sbspace(&so->so_snd);
561 if (so->so_state & SS_CANTSENDMORE) {
562 kn->kn_flags |= EV_EOF;
563 return (1);
564 }
565 kn->kn_flags &= ~EV_EOF;
566 return (kn->kn_data >= so->so_snd.sb_lowat);
567 }
568
569 static const struct filterops fiforead_filtops =
570 { 1, NULL, filt_fifordetach, filt_fiforead };
571 static const struct filterops fifowrite_filtops =
572 { 1, NULL, filt_fifowdetach, filt_fifowrite };
573
574 /* ARGSUSED */
575 int
576 fifo_kqfilter(void *v)
577 {
578 struct vop_kqfilter_args /* {
579 struct vnode *a_vp;
580 struct knote *a_kn;
581 } */ *ap = v;
582 struct socket *so;
583 struct sockbuf *sb;
584
585 so = (struct socket *)ap->a_vp->v_fifoinfo->fi_readsock;
586 switch (ap->a_kn->kn_filter) {
587 case EVFILT_READ:
588 ap->a_kn->kn_fop = &fiforead_filtops;
589 sb = &so->so_rcv;
590 break;
591 case EVFILT_WRITE:
592 ap->a_kn->kn_fop = &fifowrite_filtops;
593 sb = &so->so_snd;
594 break;
595 default:
596 return (1);
597 }
598
599 ap->a_kn->kn_hook = so;
600
601 SLIST_INSERT_HEAD(&sb->sb_sel.sel_klist, ap->a_kn, kn_selnext);
602 sb->sb_flags |= SB_KNOTE;
603 return (0);
604 }
Cache object: 5580fb33cd3f5e4158930bc865660f52
|