]> CyberLeo.Net >> Repos - FreeBSD/releng/8.1.git/blob - sys/fs/fifofs/fifo_vnops.c
Copy stable/8 to releng/8.1 in preparation for 8.1-RC1.
[FreeBSD/releng/8.1.git] / sys / fs / fifofs / fifo_vnops.c
1 /*-
2  * Copyright (c) 1990, 1993, 1995
3  *      The Regents of the University of California.
4  * Copyright (c) 2005 Robert N. M. Watson
5  * 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  * 4. 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  * $FreeBSD$
33  */
34
35 #include <sys/param.h>
36 #include <sys/event.h>
37 #include <sys/file.h>
38 #include <sys/filedesc.h>
39 #include <sys/filio.h>
40 #include <sys/fcntl.h>
41 #include <sys/kernel.h>
42 #include <sys/lock.h>
43 #include <sys/mutex.h>
44 #include <sys/malloc.h>
45 #include <sys/poll.h>
46 #include <sys/proc.h>
47 #include <sys/signalvar.h>
48 #include <sys/socket.h>
49 #include <sys/socketvar.h>
50 #include <sys/sx.h>
51 #include <sys/systm.h>
52 #include <sys/un.h>
53 #include <sys/unistd.h>
54 #include <sys/vnode.h>
55 #include <fs/fifofs/fifo.h>
56
57 static fo_rdwr_t        fifo_read_f;
58 static fo_rdwr_t        fifo_write_f;
59 static fo_ioctl_t       fifo_ioctl_f;
60 static fo_poll_t        fifo_poll_f;
61 static fo_kqfilter_t    fifo_kqfilter_f;
62 static fo_stat_t        fifo_stat_f;
63 static fo_close_t       fifo_close_f;
64 static fo_truncate_t    fifo_truncate_f;
65
66 struct fileops fifo_ops_f = {
67         .fo_read =      fifo_read_f,
68         .fo_write =     fifo_write_f,
69         .fo_truncate =  fifo_truncate_f,
70         .fo_ioctl =     fifo_ioctl_f,
71         .fo_poll =      fifo_poll_f,
72         .fo_kqfilter =  fifo_kqfilter_f,
73         .fo_stat =      fifo_stat_f,
74         .fo_close =     fifo_close_f,
75         .fo_flags =     DFLAG_PASSABLE
76 };
77
78 /*
79  * This structure is associated with the FIFO vnode and stores
80  * the state associated with the FIFO.
81  * Notes about locking:
82  *   - fi_readsock and fi_writesock are invariant since init time.
83  *   - fi_readers and fi_writers are vnode lock protected.
84  *   - fi_wgen is fif_mtx lock protected.
85  */
86 struct fifoinfo {
87         struct socket   *fi_readsock;
88         struct socket   *fi_writesock;
89         long            fi_readers;
90         long            fi_writers;
91         int             fi_wgen;
92 };
93
94 static vop_print_t      fifo_print;
95 static vop_open_t       fifo_open;
96 static vop_close_t      fifo_close;
97 static vop_pathconf_t   fifo_pathconf;
98 static vop_advlock_t    fifo_advlock;
99
100 static void     filt_fifordetach(struct knote *kn);
101 static int      filt_fiforead(struct knote *kn, long hint);
102 static void     filt_fifowdetach(struct knote *kn);
103 static int      filt_fifowrite(struct knote *kn, long hint);
104 static void     filt_fifodetach_notsup(struct knote *kn);
105 static int      filt_fifo_notsup(struct knote *kn, long hint);
106
107 static struct filterops fiforead_filtops =
108         { 1, NULL, filt_fifordetach, filt_fiforead };
109 static struct filterops fifowrite_filtops =
110         { 1, NULL, filt_fifowdetach, filt_fifowrite };
111 static struct filterops fifo_notsup_filtops =
112         { 1, NULL, filt_fifodetach_notsup, filt_fifo_notsup };
113
114 struct vop_vector fifo_specops = {
115         .vop_default =          &default_vnodeops,
116
117         .vop_advlock =          fifo_advlock,
118         .vop_close =            fifo_close,
119         .vop_create =           VOP_PANIC,
120         .vop_getattr =          VOP_EBADF,
121         .vop_ioctl =            VOP_PANIC,
122         .vop_kqfilter =         VOP_PANIC,
123         .vop_link =             VOP_PANIC,
124         .vop_mkdir =            VOP_PANIC,
125         .vop_mknod =            VOP_PANIC,
126         .vop_open =             fifo_open,
127         .vop_pathconf =         fifo_pathconf,
128         .vop_print =            fifo_print,
129         .vop_read =             VOP_PANIC,
130         .vop_readdir =          VOP_PANIC,
131         .vop_readlink =         VOP_PANIC,
132         .vop_reallocblks =      VOP_PANIC,
133         .vop_reclaim =          VOP_NULL,
134         .vop_remove =           VOP_PANIC,
135         .vop_rename =           VOP_PANIC,
136         .vop_rmdir =            VOP_PANIC,
137         .vop_setattr =          VOP_EBADF,
138         .vop_symlink =          VOP_PANIC,
139         .vop_write =            VOP_PANIC,
140 };
141
142 struct mtx fifo_mtx;
143 MTX_SYSINIT(fifo, &fifo_mtx, "fifo mutex", MTX_DEF);
144
145 /*
146  * Dispose of fifo resources.
147  */
148 static void
149 fifo_cleanup(struct vnode *vp)
150 {
151         struct fifoinfo *fip = vp->v_fifoinfo;
152
153         ASSERT_VOP_ELOCKED(vp, "fifo_cleanup");
154         if (fip->fi_readers == 0 && fip->fi_writers == 0) {
155                 vp->v_fifoinfo = NULL;
156                 (void)soclose(fip->fi_readsock);
157                 (void)soclose(fip->fi_writesock);
158                 free(fip, M_VNODE);
159         }
160 }
161
162 /*
163  * Open called to set up a new instance of a fifo or
164  * to find an active instance of a fifo.
165  */
166 /* ARGSUSED */
167 static int
168 fifo_open(ap)
169         struct vop_open_args /* {
170                 struct vnode *a_vp;
171                 int  a_mode;
172                 struct ucred *a_cred;
173                 struct thread *a_td;
174                 struct file *a_fp;
175         } */ *ap;
176 {
177         struct vnode *vp = ap->a_vp;
178         struct fifoinfo *fip;
179         struct thread *td = ap->a_td;
180         struct ucred *cred = ap->a_cred;
181         struct file *fp = ap->a_fp;
182         struct socket *rso, *wso;
183         int error;
184
185         ASSERT_VOP_ELOCKED(vp, "fifo_open");
186         if (fp == NULL)
187                 return (EINVAL);
188         if ((fip = vp->v_fifoinfo) == NULL) {
189                 fip = malloc(sizeof(*fip), M_VNODE, M_WAITOK);
190                 error = socreate(AF_LOCAL, &rso, SOCK_STREAM, 0, cred, td);
191                 if (error)
192                         goto fail1;
193                 fip->fi_readsock = rso;
194                 error = socreate(AF_LOCAL, &wso, SOCK_STREAM, 0, cred, td);
195                 if (error)
196                         goto fail2;
197                 fip->fi_writesock = wso;
198                 error = soconnect2(wso, rso);
199                 /* Close the direction we do not use, so we can get POLLHUP. */
200                 if (error == 0)
201                         error = soshutdown(rso, SHUT_WR);
202                 if (error) {
203                         (void)soclose(wso);
204 fail2:
205                         (void)soclose(rso);
206 fail1:
207                         free(fip, M_VNODE);
208                         return (error);
209                 }
210                 fip->fi_readers = fip->fi_writers = 0;
211                 wso->so_snd.sb_lowat = PIPE_BUF;
212                 SOCKBUF_LOCK(&rso->so_rcv);
213                 rso->so_rcv.sb_state |= SBS_CANTRCVMORE;
214                 SOCKBUF_UNLOCK(&rso->so_rcv);
215                 KASSERT(vp->v_fifoinfo == NULL,
216                     ("fifo_open: v_fifoinfo race"));
217                 vp->v_fifoinfo = fip;
218         }
219
220         /*
221          * Use the fifo_mtx lock here, in addition to the vnode lock,
222          * in order to allow vnode lock dropping before msleep() calls
223          * and still avoiding missed wakeups.
224          */
225         mtx_lock(&fifo_mtx);
226         if (ap->a_mode & FREAD) {
227                 fip->fi_readers++;
228                 if (fip->fi_readers == 1) {
229                         SOCKBUF_LOCK(&fip->fi_writesock->so_snd);
230                         fip->fi_writesock->so_snd.sb_state &= ~SBS_CANTSENDMORE;
231                         SOCKBUF_UNLOCK(&fip->fi_writesock->so_snd);
232                         if (fip->fi_writers > 0) {
233                                 wakeup(&fip->fi_writers);
234                                 sowwakeup(fip->fi_writesock);
235                         }
236                 }
237                 fp->f_seqcount = fip->fi_wgen - fip->fi_writers;
238         }
239         if (ap->a_mode & FWRITE) {
240                 if ((ap->a_mode & O_NONBLOCK) && fip->fi_readers == 0) {
241                         mtx_unlock(&fifo_mtx);
242                         if (fip->fi_writers == 0)
243                                 fifo_cleanup(vp);
244                         return (ENXIO);
245                 }
246                 fip->fi_writers++;
247                 if (fip->fi_writers == 1) {
248                         SOCKBUF_LOCK(&fip->fi_readsock->so_rcv);
249                         fip->fi_readsock->so_rcv.sb_state &= ~SBS_CANTRCVMORE;
250                         SOCKBUF_UNLOCK(&fip->fi_readsock->so_rcv);
251                         if (fip->fi_readers > 0) {
252                                 wakeup(&fip->fi_readers);
253                                 sorwakeup(fip->fi_readsock);
254                         }
255                 }
256         }
257         if ((ap->a_mode & O_NONBLOCK) == 0) {
258                 if ((ap->a_mode & FREAD) && fip->fi_writers == 0) {
259                         VOP_UNLOCK(vp, 0);
260                         error = msleep(&fip->fi_readers, &fifo_mtx,
261                             PDROP | PCATCH | PSOCK, "fifoor", 0);
262                         vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
263                         if (error) {
264                                 fip->fi_readers--;
265                                 if (fip->fi_readers == 0) {
266                                         socantsendmore(fip->fi_writesock);
267                                         fifo_cleanup(vp);
268                                 }
269                                 return (error);
270                         }
271                         mtx_lock(&fifo_mtx);
272                         /*
273                          * We must have got woken up because we had a writer.
274                          * That (and not still having one) is the condition
275                          * that we must wait for.
276                          */
277                 }
278                 if ((ap->a_mode & FWRITE) && fip->fi_readers == 0) {
279                         VOP_UNLOCK(vp, 0);
280                         error = msleep(&fip->fi_writers, &fifo_mtx,
281                             PDROP | PCATCH | PSOCK, "fifoow", 0);
282                         vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
283                         if (error) {
284                                 fip->fi_writers--;
285                                 if (fip->fi_writers == 0) {
286                                         socantrcvmore(fip->fi_readsock);
287                                         mtx_lock(&fifo_mtx);
288                                         fip->fi_wgen++;
289                                         mtx_unlock(&fifo_mtx);
290                                         fifo_cleanup(vp);
291                                 }
292                                 return (error);
293                         }
294                         /*
295                          * We must have got woken up because we had
296                          * a reader.  That (and not still having one)
297                          * is the condition that we must wait for.
298                          */
299                         mtx_lock(&fifo_mtx);
300                 }
301         }
302         mtx_unlock(&fifo_mtx);
303         KASSERT(fp != NULL, ("can't fifo/vnode bypass"));
304         KASSERT(fp->f_ops == &badfileops, ("not badfileops in fifo_open"));
305         finit(fp, fp->f_flag, DTYPE_FIFO, fip, &fifo_ops_f);
306         return (0);
307 }
308
309 static void
310 filt_fifordetach(struct knote *kn)
311 {
312         struct socket *so = (struct socket *)kn->kn_hook;
313
314         SOCKBUF_LOCK(&so->so_rcv);
315         knlist_remove(&so->so_rcv.sb_sel.si_note, kn, 1);
316         if (knlist_empty(&so->so_rcv.sb_sel.si_note))
317                 so->so_rcv.sb_flags &= ~SB_KNOTE;
318         SOCKBUF_UNLOCK(&so->so_rcv);
319 }
320
321 static int
322 filt_fiforead(struct knote *kn, long hint)
323 {
324         struct socket *so = (struct socket *)kn->kn_hook;
325
326         SOCKBUF_LOCK_ASSERT(&so->so_rcv);
327         kn->kn_data = so->so_rcv.sb_cc;
328         if (so->so_rcv.sb_state & SBS_CANTRCVMORE) {
329                 kn->kn_flags |= EV_EOF;
330                 return (1);
331         } else {
332                 kn->kn_flags &= ~EV_EOF;
333                 return (kn->kn_data > 0);
334         }
335 }
336
337 static void
338 filt_fifowdetach(struct knote *kn)
339 {
340         struct socket *so = (struct socket *)kn->kn_hook;
341
342         SOCKBUF_LOCK(&so->so_snd);
343         knlist_remove(&so->so_snd.sb_sel.si_note, kn, 1);
344         if (knlist_empty(&so->so_snd.sb_sel.si_note))
345                 so->so_snd.sb_flags &= ~SB_KNOTE;
346         SOCKBUF_UNLOCK(&so->so_snd);
347 }
348
349 static int
350 filt_fifowrite(struct knote *kn, long hint)
351 {
352         struct socket *so = (struct socket *)kn->kn_hook;
353
354         SOCKBUF_LOCK_ASSERT(&so->so_snd);
355         kn->kn_data = sbspace(&so->so_snd);
356         if (so->so_snd.sb_state & SBS_CANTSENDMORE) {
357                 kn->kn_flags |= EV_EOF;
358                 return (1);
359         } else {
360                 kn->kn_flags &= ~EV_EOF;
361                 return (kn->kn_data >= so->so_snd.sb_lowat);
362         }
363 }
364
365 static void
366 filt_fifodetach_notsup(struct knote *kn)
367 {
368
369 }
370
371 static int
372 filt_fifo_notsup(struct knote *kn, long hint)
373 {
374
375         return (0);
376 }
377
378 /*
379  * Device close routine
380  */
381 /* ARGSUSED */
382 static int
383 fifo_close(ap)
384         struct vop_close_args /* {
385                 struct vnode *a_vp;
386                 int  a_fflag;
387                 struct ucred *a_cred;
388                 struct thread *a_td;
389         } */ *ap;
390 {
391         struct vnode *vp = ap->a_vp;
392         struct fifoinfo *fip = vp->v_fifoinfo;
393
394         ASSERT_VOP_ELOCKED(vp, "fifo_close");
395         if (fip == NULL) {
396                 printf("fifo_close: no v_fifoinfo %p\n", vp);
397                 return (0);
398         }
399         if (ap->a_fflag & FREAD) {
400                 fip->fi_readers--;
401                 if (fip->fi_readers == 0)
402                         socantsendmore(fip->fi_writesock);
403         }
404         if (ap->a_fflag & FWRITE) {
405                 fip->fi_writers--;
406                 if (fip->fi_writers == 0) {
407                         socantrcvmore(fip->fi_readsock);
408                         mtx_lock(&fifo_mtx);
409                         fip->fi_wgen++;
410                         mtx_unlock(&fifo_mtx);
411                 }
412         }
413         fifo_cleanup(vp);
414         return (0);
415 }
416
417 /*
418  * Print out internal contents of a fifo vnode.
419  */
420 int
421 fifo_printinfo(vp)
422         struct vnode *vp;
423 {
424         register struct fifoinfo *fip = vp->v_fifoinfo;
425
426         if (fip == NULL){
427                 printf(", NULL v_fifoinfo");
428                 return (0);
429         }
430         printf(", fifo with %ld readers and %ld writers",
431                 fip->fi_readers, fip->fi_writers);
432         return (0);
433 }
434
435 /*
436  * Print out the contents of a fifo vnode.
437  */
438 static int
439 fifo_print(ap)
440         struct vop_print_args /* {
441                 struct vnode *a_vp;
442         } */ *ap;
443 {
444         printf("    ");
445         fifo_printinfo(ap->a_vp);
446         printf("\n");
447         return (0);
448 }
449
450 /*
451  * Return POSIX pathconf information applicable to fifo's.
452  */
453 static int
454 fifo_pathconf(ap)
455         struct vop_pathconf_args /* {
456                 struct vnode *a_vp;
457                 int a_name;
458                 int *a_retval;
459         } */ *ap;
460 {
461
462         switch (ap->a_name) {
463         case _PC_LINK_MAX:
464                 *ap->a_retval = LINK_MAX;
465                 return (0);
466         case _PC_PIPE_BUF:
467                 *ap->a_retval = PIPE_BUF;
468                 return (0);
469         case _PC_CHOWN_RESTRICTED:
470                 *ap->a_retval = 1;
471                 return (0);
472         default:
473                 return (EINVAL);
474         }
475         /* NOTREACHED */
476 }
477
478 /*
479  * Fifo advisory byte-level locks.
480  */
481 /* ARGSUSED */
482 static int
483 fifo_advlock(ap)
484         struct vop_advlock_args /* {
485                 struct vnode *a_vp;
486                 caddr_t  a_id;
487                 int  a_op;
488                 struct flock *a_fl;
489                 int  a_flags;
490         } */ *ap;
491 {
492
493         return (ap->a_flags & F_FLOCK ? EOPNOTSUPP : EINVAL);
494 }
495
496 static int
497 fifo_close_f(struct file *fp, struct thread *td)
498 {
499
500         return (vnops.fo_close(fp, td));
501 }
502
503 /*
504  * The implementation of ioctl() for named fifos is complicated by the fact
505  * that we permit O_RDWR fifo file descriptors, meaning that the actions of
506  * ioctls may have to be applied to both the underlying sockets rather than
507  * just one.  The original implementation simply forward the ioctl to one
508  * or both sockets based on fp->f_flag.  We now consider each ioctl
509  * separately, as the composition effect requires careful ordering.
510  *
511  * We do not blindly pass all ioctls through to the socket in order to avoid
512  * providing unnecessary ioctls that might be improperly depended on by
513  * applications (such as socket-specific, routing, and interface ioctls).
514  *
515  * Unlike sys_pipe.c, fifos do not implement the deprecated TIOCSPGRP and
516  * TIOCGPGRP ioctls.  Earlier implementations of fifos did forward SIOCSPGRP
517  * and SIOCGPGRP ioctls, so we might need to re-add those here.
518  */
519 static int
520 fifo_ioctl_f(struct file *fp, u_long com, void *data, struct ucred *cred,
521     struct thread *td)
522 {
523         struct fifoinfo *fi;
524         struct file filetmp;    /* Local, so need not be locked. */
525         int error;
526
527         error = ENOTTY;
528         fi = fp->f_data;
529
530         switch (com) {
531         case FIONBIO:
532                 /*
533                  * Non-blocking I/O is implemented at the fifo layer using
534                  * MSG_NBIO, so does not need to be forwarded down the stack.
535                  */
536                 return (0);
537
538         case FIOASYNC:
539         case FIOSETOWN:
540         case FIOGETOWN:
541                 /*
542                  * These socket ioctls don't have any ordering requirements,
543                  * so are called in an arbitrary order, and only on the
544                  * sockets indicated by the file descriptor rights.
545                  *
546                  * XXXRW: If O_RDWR and the read socket accepts an ioctl but
547                  * the write socket doesn't, the socketpair is left in an
548                  * inconsistent state.
549                  */
550                 if (fp->f_flag & FREAD) {
551                         filetmp.f_data = fi->fi_readsock;
552                         filetmp.f_cred = cred;
553                         error = soo_ioctl(&filetmp, com, data, cred, td);
554                         if (error)
555                                 return (error);
556                 }
557                 if (fp->f_flag & FWRITE) {
558                         filetmp.f_data = fi->fi_writesock;
559                         filetmp.f_cred = cred;
560                         error = soo_ioctl(&filetmp, com, data, cred, td);
561                 }
562                 return (error);
563
564         case FIONREAD:
565                 /*
566                  * FIONREAD will return 0 for non-readable descriptors, and
567                  * the results of FIONREAD on the read socket for readable
568                  * descriptors.
569                  */
570                 if (!(fp->f_flag & FREAD)) {
571                         *(int *)data = 0;
572                         return (0);
573                 }
574                 filetmp.f_data = fi->fi_readsock;
575                 filetmp.f_cred = cred;
576                 return (soo_ioctl(&filetmp, com, data, cred, td));
577
578         default:
579                 return (ENOTTY);
580         }
581 }
582
583 /*
584  * Because fifos are now a file descriptor layer object, EVFILT_VNODE is not
585  * implemented.  Likely, fifo_kqfilter() should be removed, and
586  * fifo_kqfilter_f() should know how to forward the request to the underling
587  * vnode using f_vnode in the file descriptor here.
588  */
589 static int
590 fifo_kqfilter_f(struct file *fp, struct knote *kn)
591 {
592         struct fifoinfo *fi;
593         struct socket *so;
594         struct sockbuf *sb;
595
596         fi = fp->f_data;
597
598         /*
599          * If a filter is requested that is not supported by this file
600          * descriptor, don't return an error, but also don't ever generate an
601          * event.
602          */
603         if ((kn->kn_filter == EVFILT_READ) && !(fp->f_flag & FREAD)) {
604                 kn->kn_fop = &fifo_notsup_filtops;
605                 return (0);
606         }
607
608         if ((kn->kn_filter == EVFILT_WRITE) && !(fp->f_flag & FWRITE)) {
609                 kn->kn_fop = &fifo_notsup_filtops;
610                 return (0);
611         }
612
613         switch (kn->kn_filter) {
614         case EVFILT_READ:
615                 kn->kn_fop = &fiforead_filtops;
616                 so = fi->fi_readsock;
617                 sb = &so->so_rcv;
618                 break;
619         case EVFILT_WRITE:
620                 kn->kn_fop = &fifowrite_filtops;
621                 so = fi->fi_writesock;
622                 sb = &so->so_snd;
623                 break;
624         default:
625                 return (EINVAL);
626         }
627
628         kn->kn_hook = (caddr_t)so;
629
630         SOCKBUF_LOCK(sb);
631         knlist_add(&sb->sb_sel.si_note, kn, 1);
632         sb->sb_flags |= SB_KNOTE;
633         SOCKBUF_UNLOCK(sb);
634
635         return (0);
636 }
637
638 static int
639 fifo_poll_f(struct file *fp, int events, struct ucred *cred, struct thread *td)
640 {
641         struct fifoinfo *fip;
642         struct file filetmp;
643         int levents, revents = 0;
644
645         fip = fp->f_data;
646         levents = events &
647             (POLLIN | POLLINIGNEOF | POLLPRI | POLLRDNORM | POLLRDBAND);
648         if ((fp->f_flag & FREAD) && levents) {
649                 filetmp.f_data = fip->fi_readsock;
650                 filetmp.f_cred = cred;
651                 mtx_lock(&fifo_mtx);
652                 if (fp->f_seqcount == fip->fi_wgen)
653                         levents |= POLLINIGNEOF;
654                 mtx_unlock(&fifo_mtx);
655                 revents |= soo_poll(&filetmp, levents, cred, td);
656         }
657         levents = events & (POLLOUT | POLLWRNORM | POLLWRBAND);
658         if ((fp->f_flag & FWRITE) && levents) {
659                 filetmp.f_data = fip->fi_writesock;
660                 filetmp.f_cred = cred;
661                 revents |= soo_poll(&filetmp, levents, cred, td);
662         }
663         return (revents);
664 }
665
666 static int
667 fifo_read_f(struct file *fp, struct uio *uio, struct ucred *cred, int flags, struct thread *td)
668 {
669         struct fifoinfo *fip;
670         int sflags;
671
672         fip = fp->f_data;
673         KASSERT(uio->uio_rw == UIO_READ,("fifo_read mode"));
674         if (uio->uio_resid == 0)
675                 return (0);
676         sflags = (fp->f_flag & FNONBLOCK) ? MSG_NBIO : 0;
677         return (soreceive(fip->fi_readsock, NULL, uio, NULL, NULL, &sflags));
678 }
679
680 static int
681 fifo_stat_f(struct file *fp, struct stat *sb, struct ucred *cred, struct thread *td)
682 {
683
684         return (vnops.fo_stat(fp, sb, cred, td));
685 }
686
687 static int
688 fifo_truncate_f(struct file *fp, off_t length, struct ucred *cred, struct thread *td)
689 {
690
691         return (vnops.fo_truncate(fp, length, cred, td));
692 }
693
694 static int
695 fifo_write_f(struct file *fp, struct uio *uio, struct ucred *cred, int flags, struct thread *td)
696 {
697         struct fifoinfo *fip;
698         int sflags;
699
700         fip = fp->f_data;
701         KASSERT(uio->uio_rw == UIO_WRITE,("fifo_write mode"));
702         sflags = (fp->f_flag & FNONBLOCK) ? MSG_NBIO : 0;
703         return (sosend(fip->fi_writesock, NULL, uio, 0, NULL, sflags, td));
704 }