kevent: Restore old EV_EOF semantics
[dragonfly.git] / sys / vfs / fifofs / fifo_vnops.c
1 /*
2  * Copyright (c) 1990, 1993, 1995
3  *      The Regents of the University of California.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. All advertising materials mentioning features or use of this software
14  *    must display the following acknowledgement:
15  *      This product includes software developed by the University of
16  *      California, Berkeley and its contributors.
17  * 4. Neither the name of the University nor the names of its contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  */
33 /*
34  * Filesystem FIFO type ops.  All entry points are MPSAFE.  We primarily
35  * use v_token to interlock operations.
36  */
37 #include <sys/param.h>
38 #include <sys/systm.h>
39 #include <sys/unistd.h>
40 #include <sys/kernel.h>
41 #include <sys/lock.h>
42 #include <sys/malloc.h>
43 #include <sys/thread2.h>
44 #include <sys/vnode.h>
45 #include <sys/socket.h>
46 #include <sys/socketvar.h>
47 #include <sys/filio.h>
48 #include <sys/fcntl.h>
49 #include <sys/file.h>
50 #include <sys/event.h>
51 #include <sys/un.h>
52
53 #include <sys/thread2.h>
54 #include <sys/socketvar2.h>
55
56 #include "fifo.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 static int      fifo_badop (void);
70 static int      fifo_print (struct vop_print_args *);
71 static int      fifo_lookup (struct vop_old_lookup_args *);
72 static int      fifo_open (struct vop_open_args *);
73 static int      fifo_close (struct vop_close_args *);
74 static int      fifo_read (struct vop_read_args *);
75 static int      fifo_write (struct vop_write_args *);
76 static int      fifo_ioctl (struct vop_ioctl_args *);
77 static int      fifo_kqfilter (struct vop_kqfilter_args *);
78 static int      fifo_inactive (struct  vop_inactive_args *);
79 static int      fifo_bmap (struct vop_bmap_args *);
80 static int      fifo_pathconf (struct vop_pathconf_args *);
81 static int      fifo_advlock (struct vop_advlock_args *);
82
83 static void     filt_fifordetach(struct knote *kn);
84 static int      filt_fiforead(struct knote *kn, long hint);
85 static void     filt_fifowdetach(struct knote *kn);
86 static int      filt_fifowrite(struct knote *kn, long hint);
87
88 static struct filterops fiforead_filtops =
89         { FILTEROP_ISFD, NULL, filt_fifordetach, filt_fiforead };
90 static struct filterops fifowrite_filtops =
91         { FILTEROP_ISFD, NULL, filt_fifowdetach, filt_fifowrite };
92   
93 struct vop_ops fifo_vnode_vops = {
94         .vop_default =          vop_defaultop,
95         .vop_access =           (void *)vop_ebadf,
96         .vop_advlock =          fifo_advlock,
97         .vop_bmap =             fifo_bmap,
98         .vop_close =            fifo_close,
99         .vop_old_create =       (void *)fifo_badop,
100         .vop_getattr =          (void *)vop_ebadf,
101         .vop_inactive =         fifo_inactive,
102         .vop_ioctl =            fifo_ioctl,
103         .vop_kqfilter =         fifo_kqfilter,
104         .vop_old_link =         (void *)fifo_badop,
105         .vop_old_lookup =       fifo_lookup,
106         .vop_old_mkdir =        (void *)fifo_badop,
107         .vop_old_mknod =        (void *)fifo_badop,
108         .vop_open =             fifo_open,
109         .vop_pathconf =         fifo_pathconf,
110         .vop_print =            fifo_print,
111         .vop_read =             fifo_read,
112         .vop_readdir =          (void *)fifo_badop,
113         .vop_readlink =         (void *)fifo_badop,
114         .vop_reallocblks =      (void *)fifo_badop,
115         .vop_reclaim =          (void *)vop_null,
116         .vop_old_remove =       (void *)fifo_badop,
117         .vop_old_rename =       (void *)fifo_badop,
118         .vop_old_rmdir =        (void *)fifo_badop,
119         .vop_setattr =          (void *)vop_ebadf,
120         .vop_old_symlink =      (void *)fifo_badop,
121         .vop_write =            fifo_write
122 };
123
124 VNODEOP_SET(fifo_vnode_vops);
125
126 static MALLOC_DEFINE(M_FIFOINFO, "Fifo info", "Fifo info entries");
127
128 /*
129  * fifo_vnoperate()
130  */
131 int
132 fifo_vnoperate(struct vop_generic_args *ap)
133 {
134         return (VOCALL(&fifo_vnode_vops, ap));
135 }
136
137 /*
138  * Trivial lookup routine that always fails.
139  *
140  * fifo_lookup(struct vnode *a_dvp, struct vnode **a_vpp,
141  *             struct componentname *a_cnp)
142  */
143 /* ARGSUSED */
144 static int
145 fifo_lookup(struct vop_old_lookup_args *ap)
146 {
147         *ap->a_vpp = NULL;
148         return (ENOTDIR);
149 }
150
151 /*
152  * Open called to set up a new instance of a fifo or
153  * to find an active instance of a fifo.
154  *
155  * fifo_open(struct vnode *a_vp, int a_mode, struct ucred *a_cred,
156  *           struct file *a_fp)
157  */
158 /* ARGSUSED */
159 static int
160 fifo_open(struct vop_open_args *ap)
161 {
162         struct thread *td = curthread;
163         struct vnode *vp = ap->a_vp;
164         struct fifoinfo *fip;
165         struct socket *rso, *wso;
166         int error;
167
168         lwkt_gettoken(&vp->v_token);
169         if ((fip = vp->v_fifoinfo) == NULL) {
170                 MALLOC(fip, struct fifoinfo *, sizeof(*fip), M_FIFOINFO, M_WAITOK);
171                 vp->v_fifoinfo = fip;
172                 error = socreate(AF_LOCAL, &rso, SOCK_STREAM, 0, td);
173                 if (error) {
174                         kfree(fip, M_FIFOINFO);
175                         vp->v_fifoinfo = NULL;
176                         goto done;
177                 }
178                 fip->fi_readsock = rso;
179                 error = socreate(AF_LOCAL, &wso, SOCK_STREAM, 0, td);
180                 if (error) {
181                         soclose(rso, FNONBLOCK);
182                         kfree(fip, M_FIFOINFO);
183                         vp->v_fifoinfo = NULL;
184                         goto done;
185                 }
186                 fip->fi_writesock = wso;
187                 error = unp_connect2(wso, rso);
188                 if (error) {
189                         soclose(wso, FNONBLOCK);
190                         soclose(rso, FNONBLOCK);
191                         kfree(fip, M_FIFOINFO);
192                         vp->v_fifoinfo = NULL;
193                         goto done;
194                 }
195                 fip->fi_readers = fip->fi_writers = 0;
196                 wso->so_snd.ssb_lowat = PIPE_BUF;
197                 sosetstate(rso, SS_CANTRCVMORE);
198         }
199         if (ap->a_mode & FREAD) {
200                 fip->fi_readers++;
201                 if (fip->fi_readers == 1) {
202                         soisreconnected(fip->fi_writesock);
203                         if (fip->fi_writers > 0) {
204                                 wakeup((caddr_t)&fip->fi_writers);
205                                 sowwakeup(fip->fi_writesock);
206                         }
207                 }
208         }
209         if (ap->a_mode & FWRITE) {
210                 fip->fi_writers++;
211                 if (fip->fi_writers == 1) {
212                         soisreconnected(fip->fi_readsock);
213                         if (fip->fi_readers > 0) {
214                                 wakeup((caddr_t)&fip->fi_readers);
215                                 sorwakeup(fip->fi_writesock);
216                         }
217                 }
218         }
219         if ((ap->a_mode & FREAD) && (ap->a_mode & O_NONBLOCK) == 0) {
220                 if (fip->fi_writers == 0) {
221                         vn_unlock(vp);
222                         error = tsleep((caddr_t)&fip->fi_readers,
223                             PCATCH, "fifoor", 0);
224                         vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
225                         if (error)
226                                 goto bad;
227                         /*
228                          * We must have got woken up because we had a writer.
229                          * That (and not still having one) is the condition
230                          * that we must wait for.
231                          */
232                 }
233         }
234         if (ap->a_mode & FWRITE) {
235                 if (ap->a_mode & O_NONBLOCK) {
236                         if (fip->fi_readers == 0) {
237                                 error = ENXIO;
238                                 goto bad;
239                         }
240                 } else {
241                         if (fip->fi_readers == 0) {
242                                 vn_unlock(vp);
243                                 error = tsleep((caddr_t)&fip->fi_writers,
244                                     PCATCH, "fifoow", 0);
245                                 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
246                                 if (error)
247                                         goto bad;
248                                 /*
249                                  * We must have got woken up because we had
250                                  * a reader.  That (and not still having one)
251                                  * is the condition that we must wait for.
252                                  */
253                         }
254                 }
255         }
256         vsetflags(vp, VNOTSEEKABLE);
257         error = vop_stdopen(ap);
258         lwkt_reltoken(&vp->v_token);
259         return (error);
260 bad:
261         vop_stdopen(ap);        /* bump opencount/writecount as appropriate */
262         VOP_CLOSE(vp, ap->a_mode);
263 done:
264         lwkt_reltoken(&vp->v_token);
265         return (error);
266 }
267
268 /*
269  * Vnode op for read
270  *
271  * fifo_read(struct vnode *a_vp, struct uio *a_uio, int a_ioflag,
272  *           struct ucred *a_cred)
273  */
274 /* ARGSUSED */
275 static int
276 fifo_read(struct vop_read_args *ap)
277 {
278         struct uio *uio = ap->a_uio;
279         struct vnode *vp = ap->a_vp;
280         struct socket *rso = vp->v_fifoinfo->fi_readsock;
281         int error;
282         int flags;
283
284 #ifdef DIAGNOSTIC
285         if (uio->uio_rw != UIO_READ)
286                 panic("fifo_read mode");
287 #endif
288         if (uio->uio_resid == 0)
289                 return (0);
290         if (ap->a_ioflag & IO_NDELAY)
291                 flags = MSG_FNONBLOCKING;
292         else
293                 flags = 0;
294         vn_unlock(vp);
295         lwkt_gettoken(&vp->v_token);
296         error = soreceive(rso, NULL, uio, NULL, NULL, &flags);
297         lwkt_reltoken(&vp->v_token);
298         vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
299         return (error);
300 }
301
302 /*
303  * Vnode op for write
304  *
305  * fifo_write(struct vnode *a_vp, struct uio *a_uio, int a_ioflag,
306  *            struct ucred *a_cred)
307  */
308 /* ARGSUSED */
309 static int
310 fifo_write(struct vop_write_args *ap)
311 {
312         struct thread *td = ap->a_uio->uio_td;
313         struct vnode *vp = ap->a_vp;
314         struct socket *wso = vp->v_fifoinfo->fi_writesock;
315         int error;
316         int flags;
317
318 #ifdef DIAGNOSTIC
319         if (ap->a_uio->uio_rw != UIO_WRITE)
320                 panic("fifo_write mode");
321 #endif
322         if (ap->a_ioflag & IO_NDELAY)
323                 flags = MSG_FNONBLOCKING;
324         else
325                 flags = 0;
326         vn_unlock(vp);
327         lwkt_gettoken(&vp->v_token);
328         error = sosend(wso, NULL, ap->a_uio, 0, NULL, flags, td);
329         lwkt_reltoken(&vp->v_token);
330         vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
331         return (error);
332 }
333
334 /*
335  * Device ioctl operation.
336  *
337  * fifo_ioctl(struct vnode *a_vp, int a_command, caddr_t a_data, int a_fflag,
338  *            struct ucred *a_cred, struct sysmsg *a_sysmsg)
339  */
340 /* ARGSUSED */
341 static int
342 fifo_ioctl(struct vop_ioctl_args *ap)
343 {
344         struct file filetmp;    /* Local */
345         struct vnode *vp = ap->a_vp;
346         int error;
347
348         if (ap->a_fflag & FREAD) {
349                 filetmp.f_data = vp->v_fifoinfo->fi_readsock;
350                 lwkt_gettoken(&vp->v_token);
351                 error = soo_ioctl(&filetmp, ap->a_command, ap->a_data,
352                                   ap->a_cred, ap->a_sysmsg);
353                 lwkt_reltoken(&vp->v_token);
354                 if (error)
355                         return (error);
356         }
357         if (ap->a_fflag & FWRITE) {
358                 filetmp.f_data = vp->v_fifoinfo->fi_writesock;
359                 lwkt_gettoken(&vp->v_token);
360                 error = soo_ioctl(&filetmp, ap->a_command, ap->a_data,
361                                   ap->a_cred, ap->a_sysmsg);
362                 lwkt_reltoken(&vp->v_token);
363                 if (error)
364                         return (error);
365         }
366         return (0);
367 }
368
369 /*
370  * fifo_kqfilter(struct vnode *a_vp, struct knote *a_kn)
371  */
372 /* ARGSUSED */
373 static int
374 fifo_kqfilter(struct vop_kqfilter_args *ap)
375 {
376         struct vnode *vp = ap->a_vp;
377         struct fifoinfo *fi = vp->v_fifoinfo;
378         struct socket *so;
379         struct signalsockbuf *ssb;
380
381         lwkt_gettoken(&vp->v_token);
382
383         switch (ap->a_kn->kn_filter) {
384         case EVFILT_READ:
385                 ap->a_kn->kn_fop = &fiforead_filtops;
386                 so = fi->fi_readsock;
387                 ssb = &so->so_rcv;
388                 break;
389         case EVFILT_WRITE:
390                 ap->a_kn->kn_fop = &fifowrite_filtops;
391                 so = fi->fi_writesock;
392                 ssb = &so->so_snd;
393                 break;
394         default:
395                 lwkt_reltoken(&vp->v_token);
396                 return (EOPNOTSUPP);
397         }
398
399         ap->a_kn->kn_hook = (caddr_t)vp;
400         ssb_insert_knote(ssb, ap->a_kn);
401
402         lwkt_reltoken(&vp->v_token);
403         return (0);
404 }
405
406 static void
407 filt_fifordetach(struct knote *kn)
408 {
409         struct vnode *vp = (void *)kn->kn_hook;
410         struct socket *so = vp->v_fifoinfo->fi_readsock;
411
412         lwkt_gettoken(&vp->v_token);
413         ssb_remove_knote(&so->so_rcv, kn);
414         lwkt_reltoken(&vp->v_token);
415 }
416
417 static int
418 filt_fiforead(struct knote *kn, long hint)
419 {
420         struct vnode *vp = (void *)kn->kn_hook;
421         struct socket *so = vp->v_fifoinfo->fi_readsock;
422
423         lwkt_gettoken(&vp->v_token);
424         kn->kn_data = so->so_rcv.ssb_cc;
425         if (so->so_state & SS_ISDISCONNECTED) {
426                 if (kn->kn_data == 0)
427                         kn->kn_flags |= EV_NODATA;
428                 kn->kn_flags |= EV_EOF;
429                 lwkt_reltoken(&vp->v_token);
430                 return (1);
431         }
432         kn->kn_flags &= ~(EV_EOF | EV_NODATA);
433         lwkt_reltoken(&vp->v_token);
434         return (kn->kn_data > 0);
435 }
436
437 static void
438 filt_fifowdetach(struct knote *kn)
439 {
440         struct vnode *vp = (void *)kn->kn_hook;
441         struct socket *so = vp->v_fifoinfo->fi_writesock;
442
443         lwkt_gettoken(&vp->v_token);
444         ssb_remove_knote(&so->so_snd, kn);
445         lwkt_reltoken(&vp->v_token);
446 }
447
448 static int
449 filt_fifowrite(struct knote *kn, long hint)
450 {
451         struct vnode *vp = (void *)kn->kn_hook;
452         struct socket *so = vp->v_fifoinfo->fi_writesock;
453
454         lwkt_gettoken(&vp->v_token);
455         kn->kn_data = ssb_space(&so->so_snd);
456         if (so->so_state & SS_ISDISCONNECTED) {
457                 kn->kn_flags |= (EV_EOF | EV_NODATA);
458                 lwkt_reltoken(&vp->v_token);
459                 return (1);
460         }
461         kn->kn_flags &= ~(EV_EOF | EV_NODATA);
462         lwkt_reltoken(&vp->v_token);
463         return (kn->kn_data >= so->so_snd.ssb_lowat);
464 }
465
466 /*
467  * fifo_inactive(struct vnode *a_vp)
468  */
469 static int
470 fifo_inactive(struct vop_inactive_args *ap)
471 {
472         return (0);
473 }
474
475 /*
476  * This is a noop, simply returning what one has been given.
477  *
478  * fifo_bmap(struct vnode *a_vp, off_t a_loffset, 
479  *           off_t *a_doffsetp, int *a_runp, int *a_runb)
480  */
481 static int
482 fifo_bmap(struct vop_bmap_args *ap)
483 {
484         if (ap->a_doffsetp != NULL)
485                 *ap->a_doffsetp = ap->a_loffset;
486         if (ap->a_runp != NULL)
487                 *ap->a_runp = 0;
488         if (ap->a_runb != NULL)
489                 *ap->a_runb = 0;
490         return (0);
491 }
492
493 /*
494  * Device close routine
495  *
496  * fifo_close(struct vnode *a_vp, int a_fflag)
497  */
498 /* ARGSUSED */
499 static int
500 fifo_close(struct vop_close_args *ap)
501 {
502         struct vnode *vp = ap->a_vp;
503         struct fifoinfo *fip;
504         int error1, error2;
505
506         lwkt_gettoken(&vp->v_token);
507         fip = vp->v_fifoinfo;
508         if (ap->a_fflag & FREAD) {
509                 fip->fi_readers--;
510                 if (fip->fi_readers == 0)
511                         soisdisconnected(fip->fi_writesock);
512         }
513         if (ap->a_fflag & FWRITE) {
514                 fip->fi_writers--;
515                 if (fip->fi_writers == 0)
516                         soisdisconnected(fip->fi_readsock);
517         }
518         if (vp->v_sysref.refcnt > 1) {
519                 vop_stdclose(ap);
520                 lwkt_reltoken(&vp->v_token);
521                 return (0);
522         }
523         error1 = soclose(fip->fi_readsock, FNONBLOCK);
524         error2 = soclose(fip->fi_writesock, FNONBLOCK);
525         FREE(fip, M_FIFOINFO);
526         vp->v_fifoinfo = NULL;
527         if (error1) {
528                 error2 = error1;
529         } else {
530                 vop_stdclose(ap);
531         }
532         lwkt_reltoken(&vp->v_token);
533         return (error2);
534 }
535
536
537 /*
538  * Print out internal contents of a fifo vnode.
539  */
540 int
541 fifo_printinfo(struct vnode *vp)
542 {
543         struct fifoinfo *fip = vp->v_fifoinfo;
544
545         kprintf(", fifo with %ld readers and %ld writers",
546                 fip->fi_readers, fip->fi_writers);
547         return (0);
548 }
549
550 /*
551  * Print out the contents of a fifo vnode.
552  *
553  * fifo_print(struct vnode *a_vp)
554  */
555 static int
556 fifo_print(struct vop_print_args *ap)
557 {
558         kprintf("tag VT_NON");
559         fifo_printinfo(ap->a_vp);
560         kprintf("\n");
561         return (0);
562 }
563
564 /*
565  * Return POSIX pathconf information applicable to fifo's.
566  *
567  * fifo_pathconf(struct vnode *a_vp, int a_name, int *a_retval)
568  */
569 int
570 fifo_pathconf(struct vop_pathconf_args *ap)
571 {
572         switch (ap->a_name) {
573         case _PC_LINK_MAX:
574                 *ap->a_retval = LINK_MAX;
575                 return (0);
576         case _PC_PIPE_BUF:
577                 *ap->a_retval = PIPE_BUF;
578                 return (0);
579         case _PC_CHOWN_RESTRICTED:
580                 *ap->a_retval = 1;
581                 return (0);
582         default:
583                 return (EINVAL);
584         }
585         /* NOTREACHED */
586 }
587
588 /*
589  * Fifo advisory byte-level locks.
590  *
591  * fifo_advlock(struct vnode *a_vp, caddr_t a_id, int a_op, struct flock *a_fl,
592  *              int a_flags)
593  */
594 /* ARGSUSED */
595 static int
596 fifo_advlock(struct vop_advlock_args *ap)
597 {
598         return ((ap->a_flags & F_POSIX) ? EINVAL : EOPNOTSUPP);
599 }
600
601 /*
602  * Fifo bad operation
603  */
604 static int
605 fifo_badop(void)
606 {
607         panic("fifo_badop called");
608         /* NOTREACHED */
609 }