Add the DragonFly cvs id and perform general cleanups on cvs/rcs/sccs ids. Most
[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  *      @(#)fifo_vnops.c        8.10 (Berkeley) 5/27/95
34  * $FreeBSD: src/sys/miscfs/fifofs/fifo_vnops.c,v 1.45.2.4 2003/04/22 10:11:24 bde Exp $
35  * $DragonFly: src/sys/vfs/fifofs/fifo_vnops.c,v 1.2 2003/06/17 04:28:42 dillon Exp $
36  */
37
38 #include <sys/param.h>
39 #include <sys/systm.h>
40 #include <sys/unistd.h>
41 #include <sys/kernel.h>
42 #include <sys/lock.h>
43 #include <sys/malloc.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/poll.h>
52 #include <sys/un.h>
53 #include <miscfs/fifofs/fifo.h>
54
55 /*
56  * This structure is associated with the FIFO vnode and stores
57  * the state associated with the FIFO.
58  */
59 struct fifoinfo {
60         struct socket   *fi_readsock;
61         struct socket   *fi_writesock;
62         long            fi_readers;
63         long            fi_writers;
64 };
65
66 static int      fifo_badop __P((void));
67 static int      fifo_print __P((struct vop_print_args *));
68 static int      fifo_lookup __P((struct vop_lookup_args *));
69 static int      fifo_open __P((struct vop_open_args *));
70 static int      fifo_close __P((struct vop_close_args *));
71 static int      fifo_read __P((struct vop_read_args *));
72 static int      fifo_write __P((struct vop_write_args *));
73 static int      fifo_ioctl __P((struct vop_ioctl_args *));
74 static int      fifo_poll __P((struct vop_poll_args *));
75 static int      fifo_kqfilter __P((struct vop_kqfilter_args *));
76 static int      fifo_inactive __P((struct  vop_inactive_args *));
77 static int      fifo_bmap __P((struct vop_bmap_args *));
78 static int      fifo_pathconf __P((struct vop_pathconf_args *));
79 static int      fifo_advlock __P((struct vop_advlock_args *));
80
81 static void     filt_fifordetach(struct knote *kn);
82 static int      filt_fiforead(struct knote *kn, long hint);
83 static void     filt_fifowdetach(struct knote *kn);
84 static int      filt_fifowrite(struct knote *kn, long hint);
85
86 static struct filterops fiforead_filtops =
87         { 1, NULL, filt_fifordetach, filt_fiforead };
88 static struct filterops fifowrite_filtops =
89         { 1, NULL, filt_fifowdetach, filt_fifowrite };
90   
91 vop_t **fifo_vnodeop_p;
92 static struct vnodeopv_entry_desc fifo_vnodeop_entries[] = {
93         { &vop_default_desc,            (vop_t *) vop_defaultop },
94         { &vop_access_desc,             (vop_t *) vop_ebadf },
95         { &vop_advlock_desc,            (vop_t *) fifo_advlock },
96         { &vop_bmap_desc,               (vop_t *) fifo_bmap },
97         { &vop_close_desc,              (vop_t *) fifo_close },
98         { &vop_create_desc,             (vop_t *) fifo_badop },
99         { &vop_getattr_desc,            (vop_t *) vop_ebadf },
100         { &vop_inactive_desc,           (vop_t *) fifo_inactive },
101         { &vop_ioctl_desc,              (vop_t *) fifo_ioctl },
102         { &vop_kqfilter_desc,           (vop_t *) fifo_kqfilter },
103         { &vop_lease_desc,              (vop_t *) vop_null },
104         { &vop_link_desc,               (vop_t *) fifo_badop },
105         { &vop_lookup_desc,             (vop_t *) fifo_lookup },
106         { &vop_mkdir_desc,              (vop_t *) fifo_badop },
107         { &vop_mknod_desc,              (vop_t *) fifo_badop },
108         { &vop_open_desc,               (vop_t *) fifo_open },
109         { &vop_pathconf_desc,           (vop_t *) fifo_pathconf },
110         { &vop_poll_desc,               (vop_t *) fifo_poll },
111         { &vop_print_desc,              (vop_t *) fifo_print },
112         { &vop_read_desc,               (vop_t *) fifo_read },
113         { &vop_readdir_desc,            (vop_t *) fifo_badop },
114         { &vop_readlink_desc,           (vop_t *) fifo_badop },
115         { &vop_reallocblks_desc,        (vop_t *) fifo_badop },
116         { &vop_reclaim_desc,            (vop_t *) vop_null },
117         { &vop_remove_desc,             (vop_t *) fifo_badop },
118         { &vop_rename_desc,             (vop_t *) fifo_badop },
119         { &vop_rmdir_desc,              (vop_t *) fifo_badop },
120         { &vop_setattr_desc,            (vop_t *) vop_ebadf },
121         { &vop_symlink_desc,            (vop_t *) fifo_badop },
122         { &vop_write_desc,              (vop_t *) fifo_write },
123         { NULL, NULL }
124 };
125 static struct vnodeopv_desc fifo_vnodeop_opv_desc =
126         { &fifo_vnodeop_p, fifo_vnodeop_entries };
127
128 VNODEOP_SET(fifo_vnodeop_opv_desc);
129
130 int
131 fifo_vnoperate(ap)
132         struct vop_generic_args /* {
133                 struct vnodeop_desc *a_desc;
134                 <other random data follows, presumably>
135         } */ *ap;
136 {
137         return (VOCALL(fifo_vnodeop_p, ap->a_desc->vdesc_offset, ap));
138 }
139
140 /*
141  * Trivial lookup routine that always fails.
142  */
143 /* ARGSUSED */
144 static int
145 fifo_lookup(ap)
146         struct vop_lookup_args /* {
147                 struct vnode * a_dvp;
148                 struct vnode ** a_vpp;
149                 struct componentname * a_cnp;
150         } */ *ap;
151 {
152
153         *ap->a_vpp = NULL;
154         return (ENOTDIR);
155 }
156
157 /*
158  * Open called to set up a new instance of a fifo or
159  * to find an active instance of a fifo.
160  */
161 /* ARGSUSED */
162 static int
163 fifo_open(ap)
164         struct vop_open_args /* {
165                 struct vnode *a_vp;
166                 int  a_mode;
167                 struct ucred *a_cred;
168                 struct proc *a_p;
169         } */ *ap;
170 {
171         struct vnode *vp = ap->a_vp;
172         struct fifoinfo *fip;
173         struct proc *p = ap->a_p;
174         struct socket *rso, *wso;
175         int error;
176
177         if ((fip = vp->v_fifoinfo) == NULL) {
178                 MALLOC(fip, struct fifoinfo *, sizeof(*fip), M_VNODE, M_WAITOK);
179                 vp->v_fifoinfo = fip;
180                 error = socreate(AF_LOCAL, &rso, SOCK_STREAM, 0, ap->a_p);
181                 if (error) {
182                         free(fip, M_VNODE);
183                         vp->v_fifoinfo = NULL;
184                         return (error);
185                 }
186                 fip->fi_readsock = rso;
187                 error = socreate(AF_LOCAL, &wso, SOCK_STREAM, 0, ap->a_p);
188                 if (error) {
189                         (void)soclose(rso);
190                         free(fip, M_VNODE);
191                         vp->v_fifoinfo = NULL;
192                         return (error);
193                 }
194                 fip->fi_writesock = wso;
195                 error = unp_connect2(wso, rso);
196                 if (error) {
197                         (void)soclose(wso);
198                         (void)soclose(rso);
199                         free(fip, M_VNODE);
200                         vp->v_fifoinfo = NULL;
201                         return (error);
202                 }
203                 fip->fi_readers = fip->fi_writers = 0;
204                 wso->so_snd.sb_lowat = PIPE_BUF;
205                 rso->so_state |= SS_CANTRCVMORE;
206         }
207         if (ap->a_mode & FREAD) {
208                 fip->fi_readers++;
209                 if (fip->fi_readers == 1) {
210                         fip->fi_writesock->so_state &= ~SS_CANTSENDMORE;
211                         if (fip->fi_writers > 0) {
212                                 wakeup((caddr_t)&fip->fi_writers);
213                                 sowwakeup(fip->fi_writesock);
214                         }
215                 }
216         }
217         if (ap->a_mode & FWRITE) {
218                 fip->fi_writers++;
219                 if (fip->fi_writers == 1) {
220                         fip->fi_readsock->so_state &= ~SS_CANTRCVMORE;
221                         if (fip->fi_readers > 0) {
222                                 wakeup((caddr_t)&fip->fi_readers);
223                                 sorwakeup(fip->fi_writesock);
224                         }
225                 }
226         }
227         if ((ap->a_mode & FREAD) && (ap->a_mode & O_NONBLOCK) == 0) {
228                 if (fip->fi_writers == 0) {
229                         VOP_UNLOCK(vp, 0, p);
230                         error = tsleep((caddr_t)&fip->fi_readers,
231                             PCATCH | PSOCK, "fifoor", 0);
232                         vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, p);
233                         if (error)
234                                 goto bad;
235                         /*
236                          * We must have got woken up because we had a writer.
237                          * That (and not still having one) is the condition
238                          * that we must wait for.
239                          */
240                 }
241         }
242         if (ap->a_mode & FWRITE) {
243                 if (ap->a_mode & O_NONBLOCK) {
244                         if (fip->fi_readers == 0) {
245                                 error = ENXIO;
246                                 goto bad;
247                         }
248                 } else {
249                         if (fip->fi_readers == 0) {
250                                 VOP_UNLOCK(vp, 0, p);
251                                 error = tsleep((caddr_t)&fip->fi_writers,
252                                     PCATCH | PSOCK, "fifoow", 0);
253                                 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, p);
254                                 if (error)
255                                         goto bad;
256                                 /*
257                                  * We must have got woken up because we had
258                                  * a reader.  That (and not still having one)
259                                  * is the condition that we must wait for.
260                                  */
261                         }
262                 }
263         }
264         return (0);
265 bad:
266         VOP_CLOSE(vp, ap->a_mode, ap->a_cred, p);
267         return (error);
268 }
269
270 /*
271  * Vnode op for read
272  */
273 /* ARGSUSED */
274 static int
275 fifo_read(ap)
276         struct vop_read_args /* {
277                 struct vnode *a_vp;
278                 struct uio *a_uio;
279                 int  a_ioflag;
280                 struct ucred *a_cred;
281         } */ *ap;
282 {
283         struct uio *uio = ap->a_uio;
284         struct socket *rso = ap->a_vp->v_fifoinfo->fi_readsock;
285         struct proc *p = uio->uio_procp;
286         int error, startresid;
287
288 #ifdef DIAGNOSTIC
289         if (uio->uio_rw != UIO_READ)
290                 panic("fifo_read mode");
291 #endif
292         if (uio->uio_resid == 0)
293                 return (0);
294         if (ap->a_ioflag & IO_NDELAY)
295                 rso->so_state |= SS_NBIO;
296         startresid = uio->uio_resid;
297         VOP_UNLOCK(ap->a_vp, 0, p);
298         error = soreceive(rso, (struct sockaddr **)0, uio, (struct mbuf **)0,
299             (struct mbuf **)0, (int *)0);
300         vn_lock(ap->a_vp, LK_EXCLUSIVE | LK_RETRY, p);
301         if (ap->a_ioflag & IO_NDELAY)
302                 rso->so_state &= ~SS_NBIO;
303         return (error);
304 }
305
306 /*
307  * Vnode op for write
308  */
309 /* ARGSUSED */
310 static int
311 fifo_write(ap)
312         struct vop_write_args /* {
313                 struct vnode *a_vp;
314                 struct uio *a_uio;
315                 int  a_ioflag;
316                 struct ucred *a_cred;
317         } */ *ap;
318 {
319         struct socket *wso = ap->a_vp->v_fifoinfo->fi_writesock;
320         struct proc *p = ap->a_uio->uio_procp;
321         int error;
322
323 #ifdef DIAGNOSTIC
324         if (ap->a_uio->uio_rw != UIO_WRITE)
325                 panic("fifo_write mode");
326 #endif
327         if (ap->a_ioflag & IO_NDELAY)
328                 wso->so_state |= SS_NBIO;
329         VOP_UNLOCK(ap->a_vp, 0, p);
330         error = sosend(wso, (struct sockaddr *)0, ap->a_uio, 0,
331                        (struct mbuf *)0, 0, p);
332         vn_lock(ap->a_vp, LK_EXCLUSIVE | LK_RETRY, p);
333         if (ap->a_ioflag & IO_NDELAY)
334                 wso->so_state &= ~SS_NBIO;
335         return (error);
336 }
337
338 /*
339  * Device ioctl operation.
340  */
341 /* ARGSUSED */
342 static int
343 fifo_ioctl(ap)
344         struct vop_ioctl_args /* {
345                 struct vnode *a_vp;
346                 int  a_command;
347                 caddr_t  a_data;
348                 int  a_fflag;
349                 struct ucred *a_cred;
350                 struct proc *a_p;
351         } */ *ap;
352 {
353         struct file filetmp;
354         int error;
355
356         if (ap->a_command == FIONBIO)
357                 return (0);
358         if (ap->a_fflag & FREAD) {
359                 filetmp.f_data = (caddr_t)ap->a_vp->v_fifoinfo->fi_readsock;
360                 error = soo_ioctl(&filetmp, ap->a_command, ap->a_data, ap->a_p);
361                 if (error)
362                         return (error);
363         }
364         if (ap->a_fflag & FWRITE) {
365                 filetmp.f_data = (caddr_t)ap->a_vp->v_fifoinfo->fi_writesock;
366                 error = soo_ioctl(&filetmp, ap->a_command, ap->a_data, ap->a_p);
367                 if (error)
368                         return (error);
369         }
370         return (0);
371 }
372
373 /* ARGSUSED */
374 static int
375 fifo_kqfilter(ap)
376         struct vop_kqfilter_args /* {
377                 struct vnode *a_vp;
378                 struct knote *a_kn;
379         } */ *ap;
380 {
381         struct fifoinfo *fi = ap->a_vp->v_fifoinfo;
382         struct socket *so;
383         struct sockbuf *sb;
384
385         switch (ap->a_kn->kn_filter) {
386         case EVFILT_READ:
387                 ap->a_kn->kn_fop = &fiforead_filtops;
388                 so = fi->fi_readsock;
389                 sb = &so->so_rcv;
390                 break;
391         case EVFILT_WRITE:
392                 ap->a_kn->kn_fop = &fifowrite_filtops;
393                 so = fi->fi_writesock;
394                 sb = &so->so_snd;
395                 break;
396         default:
397                 return (1);
398         }
399
400         ap->a_kn->kn_hook = (caddr_t)so;
401
402         SLIST_INSERT_HEAD(&sb->sb_sel.si_note, ap->a_kn, kn_selnext);
403         sb->sb_flags |= SB_KNOTE;
404
405         return (0);
406 }
407
408 static void
409 filt_fifordetach(struct knote *kn)
410 {
411         struct socket *so = (struct socket *)kn->kn_hook;
412
413         SLIST_REMOVE(&so->so_rcv.sb_sel.si_note, kn, knote, kn_selnext);
414         if (SLIST_EMPTY(&so->so_rcv.sb_sel.si_note))
415                 so->so_rcv.sb_flags &= ~SB_KNOTE;
416 }
417
418 static int
419 filt_fiforead(struct knote *kn, long hint)
420 {
421         struct socket *so = (struct socket *)kn->kn_hook;
422
423         kn->kn_data = so->so_rcv.sb_cc;
424         if (so->so_state & SS_CANTRCVMORE) {
425                 kn->kn_flags |= EV_EOF;
426                 return (1);
427         }
428         kn->kn_flags &= ~EV_EOF;
429         return (kn->kn_data > 0);
430 }
431
432 static void
433 filt_fifowdetach(struct knote *kn)
434 {
435         struct socket *so = (struct socket *)kn->kn_hook;
436
437         SLIST_REMOVE(&so->so_snd.sb_sel.si_note, kn, knote, kn_selnext);
438         if (SLIST_EMPTY(&so->so_snd.sb_sel.si_note))
439                 so->so_snd.sb_flags &= ~SB_KNOTE;
440 }
441
442 static int
443 filt_fifowrite(struct knote *kn, long hint)
444 {
445         struct socket *so = (struct socket *)kn->kn_hook;
446
447         kn->kn_data = sbspace(&so->so_snd);
448         if (so->so_state & SS_CANTSENDMORE) {
449                 kn->kn_flags |= EV_EOF;
450                 return (1);
451         }
452         kn->kn_flags &= ~EV_EOF;
453         return (kn->kn_data >= so->so_snd.sb_lowat);
454 }
455
456 /* ARGSUSED */
457 static int
458 fifo_poll(ap)
459         struct vop_poll_args /* {
460                 struct vnode *a_vp;
461                 int  a_events;
462                 struct ucred *a_cred;
463                 struct proc *a_p;
464         } */ *ap;
465 {
466         struct file filetmp;
467         int revents = 0;
468
469         if (ap->a_events & (POLLIN | POLLPRI | POLLRDNORM | POLLRDBAND)) {
470                 filetmp.f_data = (caddr_t)ap->a_vp->v_fifoinfo->fi_readsock;
471                 if (filetmp.f_data)
472                         revents |= soo_poll(&filetmp, ap->a_events, ap->a_cred,
473                             ap->a_p);
474         }
475         if (ap->a_events & (POLLOUT | POLLWRNORM | POLLWRBAND)) {
476                 filetmp.f_data = (caddr_t)ap->a_vp->v_fifoinfo->fi_writesock;
477                 if (filetmp.f_data)
478                         revents |= soo_poll(&filetmp, ap->a_events, ap->a_cred,
479                             ap->a_p);
480         }
481         return (revents);
482 }
483
484 static int
485 fifo_inactive(ap)
486         struct vop_inactive_args /* {
487                 struct vnode *a_vp;
488                 struct proc *a_p;
489         } */ *ap;
490 {
491
492         VOP_UNLOCK(ap->a_vp, 0, ap->a_p);
493         return (0);
494 }
495
496 /*
497  * This is a noop, simply returning what one has been given.
498  */
499 static int
500 fifo_bmap(ap)
501         struct vop_bmap_args /* {
502                 struct vnode *a_vp;
503                 daddr_t  a_bn;
504                 struct vnode **a_vpp;
505                 daddr_t *a_bnp;
506                 int *a_runp;
507                 int *a_runb;
508         } */ *ap;
509 {
510
511         if (ap->a_vpp != NULL)
512                 *ap->a_vpp = ap->a_vp;
513         if (ap->a_bnp != NULL)
514                 *ap->a_bnp = ap->a_bn;
515         if (ap->a_runp != NULL)
516                 *ap->a_runp = 0;
517         if (ap->a_runb != NULL)
518                 *ap->a_runb = 0;
519         return (0);
520 }
521
522 /*
523  * Device close routine
524  */
525 /* ARGSUSED */
526 static int
527 fifo_close(ap)
528         struct vop_close_args /* {
529                 struct vnode *a_vp;
530                 int  a_fflag;
531                 struct ucred *a_cred;
532                 struct proc *a_p;
533         } */ *ap;
534 {
535         register struct vnode *vp = ap->a_vp;
536         register struct fifoinfo *fip = vp->v_fifoinfo;
537         int error1, error2;
538
539         if (ap->a_fflag & FREAD) {
540                 fip->fi_readers--;
541                 if (fip->fi_readers == 0)
542                         socantsendmore(fip->fi_writesock);
543         }
544         if (ap->a_fflag & FWRITE) {
545                 fip->fi_writers--;
546                 if (fip->fi_writers == 0)
547                         socantrcvmore(fip->fi_readsock);
548         }
549         if (vp->v_usecount > 1)
550                 return (0);
551         error1 = soclose(fip->fi_readsock);
552         error2 = soclose(fip->fi_writesock);
553         FREE(fip, M_VNODE);
554         vp->v_fifoinfo = NULL;
555         if (error1)
556                 return (error1);
557         return (error2);
558 }
559
560
561 /*
562  * Print out internal contents of a fifo vnode.
563  */
564 int
565 fifo_printinfo(vp)
566         struct vnode *vp;
567 {
568         register struct fifoinfo *fip = vp->v_fifoinfo;
569
570         printf(", fifo with %ld readers and %ld writers",
571                 fip->fi_readers, fip->fi_writers);
572         return (0);
573 }
574
575 /*
576  * Print out the contents of a fifo vnode.
577  */
578 static int
579 fifo_print(ap)
580         struct vop_print_args /* {
581                 struct vnode *a_vp;
582         } */ *ap;
583 {
584
585         printf("tag VT_NON");
586         fifo_printinfo(ap->a_vp);
587         printf("\n");
588         return (0);
589 }
590
591 /*
592  * Return POSIX pathconf information applicable to fifo's.
593  */
594 int
595 fifo_pathconf(ap)
596         struct vop_pathconf_args /* {
597                 struct vnode *a_vp;
598                 int a_name;
599                 int *a_retval;
600         } */ *ap;
601 {
602
603         switch (ap->a_name) {
604         case _PC_LINK_MAX:
605                 *ap->a_retval = LINK_MAX;
606                 return (0);
607         case _PC_PIPE_BUF:
608                 *ap->a_retval = PIPE_BUF;
609                 return (0);
610         case _PC_CHOWN_RESTRICTED:
611                 *ap->a_retval = 1;
612                 return (0);
613         default:
614                 return (EINVAL);
615         }
616         /* NOTREACHED */
617 }
618
619 /*
620  * Fifo advisory byte-level locks.
621  */
622 /* ARGSUSED */
623 static int
624 fifo_advlock(ap)
625         struct vop_advlock_args /* {
626                 struct vnode *a_vp;
627                 caddr_t  a_id;
628                 int  a_op;
629                 struct flock *a_fl;
630                 int  a_flags;
631         } */ *ap;
632 {
633
634         return (ap->a_flags & F_FLOCK ? EOPNOTSUPP : EINVAL);
635 }
636
637 /*
638  * Fifo bad operation
639  */
640 static int
641 fifo_badop()
642 {
643
644         panic("fifo_badop called");
645         /* NOTREACHED */
646 }