]> CyberLeo.Net >> Repos - FreeBSD/releng/10.2.git/blob - sys/fs/fifofs/fifo_vnops.c
- Copy stable/10@285827 to releng/10.2 in preparation for 10.2-RC1
[FreeBSD/releng/10.2.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  * Copyright (c) 2012 Giovanni Trematerra
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 4. Neither the name of the University nor the names of its contributors
17  *    may be used to endorse or promote products derived from this software
18  *    without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30  * SUCH DAMAGE.
31  *
32  *      @(#)fifo_vnops.c        8.10 (Berkeley) 5/27/95
33  * $FreeBSD$
34  */
35
36 #include <sys/param.h>
37 #include <sys/event.h>
38 #include <sys/file.h>
39 #include <sys/filedesc.h>
40 #include <sys/filio.h>
41 #include <sys/fcntl.h>
42 #include <sys/kernel.h>
43 #include <sys/lock.h>
44 #include <sys/mutex.h>
45 #include <sys/malloc.h>
46 #include <sys/selinfo.h>
47 #include <sys/pipe.h>
48 #include <sys/proc.h>
49 #include <sys/signalvar.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
56 /*
57  * This structure is associated with the FIFO vnode and stores
58  * the state associated with the FIFO.
59  * Notes about locking:
60  *   - fi_pipe is invariant since init time.
61  *   - fi_readers and fi_writers are protected by the vnode lock.
62  */
63 struct fifoinfo {
64         struct pipe *fi_pipe;
65         long    fi_readers;
66         long    fi_writers;
67 };
68
69 static vop_print_t      fifo_print;
70 static vop_open_t       fifo_open;
71 static vop_close_t      fifo_close;
72 static vop_pathconf_t   fifo_pathconf;
73 static vop_advlock_t    fifo_advlock;
74
75 struct vop_vector fifo_specops = {
76         .vop_default =          &default_vnodeops,
77
78         .vop_advlock =          fifo_advlock,
79         .vop_close =            fifo_close,
80         .vop_create =           VOP_PANIC,
81         .vop_getattr =          VOP_EBADF,
82         .vop_ioctl =            VOP_PANIC,
83         .vop_kqfilter =         VOP_PANIC,
84         .vop_link =             VOP_PANIC,
85         .vop_mkdir =            VOP_PANIC,
86         .vop_mknod =            VOP_PANIC,
87         .vop_open =             fifo_open,
88         .vop_pathconf =         fifo_pathconf,
89         .vop_print =            fifo_print,
90         .vop_read =             VOP_PANIC,
91         .vop_readdir =          VOP_PANIC,
92         .vop_readlink =         VOP_PANIC,
93         .vop_reallocblks =      VOP_PANIC,
94         .vop_reclaim =          VOP_NULL,
95         .vop_remove =           VOP_PANIC,
96         .vop_rename =           VOP_PANIC,
97         .vop_rmdir =            VOP_PANIC,
98         .vop_setattr =          VOP_EBADF,
99         .vop_symlink =          VOP_PANIC,
100         .vop_write =            VOP_PANIC,
101 };
102
103 /*
104  * Dispose of fifo resources.
105  */
106 static void
107 fifo_cleanup(struct vnode *vp)
108 {
109         struct fifoinfo *fip;
110
111         ASSERT_VOP_ELOCKED(vp, "fifo_cleanup");
112         fip = vp->v_fifoinfo;
113         if (fip->fi_readers == 0 && fip->fi_writers == 0) {
114                 vp->v_fifoinfo = NULL;
115                 pipe_dtor(fip->fi_pipe);
116                 free(fip, M_VNODE);
117         }
118 }
119
120 /*
121  * Open called to set up a new instance of a fifo or
122  * to find an active instance of a fifo.
123  */
124 /* ARGSUSED */
125 static int
126 fifo_open(ap)
127         struct vop_open_args /* {
128                 struct vnode *a_vp;
129                 int  a_mode;
130                 struct ucred *a_cred;
131                 struct thread *a_td;
132                 struct file *a_fp;
133         } */ *ap;
134 {
135         struct vnode *vp;
136         struct file *fp;
137         struct thread *td;
138         struct fifoinfo *fip;
139         struct pipe *fpipe;
140         int error, stops_deferred;
141
142         vp = ap->a_vp;
143         fp = ap->a_fp;
144         td = ap->a_td;
145         ASSERT_VOP_ELOCKED(vp, "fifo_open");
146         if (fp == NULL || (ap->a_mode & FEXEC) != 0)
147                 return (EINVAL);
148         if ((fip = vp->v_fifoinfo) == NULL) {
149                 pipe_named_ctor(&fpipe, td);
150                 fip = malloc(sizeof(*fip), M_VNODE, M_WAITOK);
151                 fip->fi_pipe = fpipe;
152                 fpipe->pipe_wgen = fip->fi_readers = fip->fi_writers = 0;
153                 KASSERT(vp->v_fifoinfo == NULL, ("fifo_open: v_fifoinfo race"));
154                 vp->v_fifoinfo = fip;
155         }
156         fpipe = fip->fi_pipe;
157         KASSERT(fpipe != NULL, ("fifo_open: pipe is NULL"));
158
159         /*
160          * Use the pipe mutex here, in addition to the vnode lock,
161          * in order to allow vnode lock dropping before msleep() calls
162          * and still avoiding missed wakeups.
163          */
164         PIPE_LOCK(fpipe);
165         if (ap->a_mode & FREAD) {
166                 fip->fi_readers++;
167                 if (fip->fi_readers == 1) {
168                         fpipe->pipe_state &= ~PIPE_EOF;
169                         if (fip->fi_writers > 0)
170                                 wakeup(&fip->fi_writers);
171                 }
172                 fp->f_seqcount = fpipe->pipe_wgen - fip->fi_writers;
173         }
174         if (ap->a_mode & FWRITE) {
175                 if ((ap->a_mode & O_NONBLOCK) && fip->fi_readers == 0) {
176                         PIPE_UNLOCK(fpipe);
177                         if (fip->fi_writers == 0)
178                                 fifo_cleanup(vp);
179                         return (ENXIO);
180                 }
181                 fip->fi_writers++;
182                 if (fip->fi_writers == 1) {
183                         fpipe->pipe_state &= ~PIPE_EOF;
184                         if (fip->fi_readers > 0)
185                                 wakeup(&fip->fi_readers);
186                 }
187         }
188         if ((ap->a_mode & O_NONBLOCK) == 0) {
189                 if ((ap->a_mode & FREAD) && fip->fi_writers == 0) {
190                         VOP_UNLOCK(vp, 0);
191                         stops_deferred = sigallowstop();
192                         error = msleep(&fip->fi_readers, PIPE_MTX(fpipe),
193                             PDROP | PCATCH | PSOCK, "fifoor", 0);
194                         if (stops_deferred)
195                                 sigdeferstop();
196                         vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
197                         if (error) {
198                                 fip->fi_readers--;
199                                 if (fip->fi_readers == 0) {
200                                         PIPE_LOCK(fpipe);
201                                         fpipe->pipe_state |= PIPE_EOF;
202                                         if (fpipe->pipe_state & PIPE_WANTW)
203                                                 wakeup(fpipe);
204                                         PIPE_UNLOCK(fpipe);
205                                         fifo_cleanup(vp);
206                                 }
207                                 return (error);
208                         }
209                         PIPE_LOCK(fpipe);
210                         /*
211                          * We must have got woken up because we had a writer.
212                          * That (and not still having one) is the condition
213                          * that we must wait for.
214                          */
215                 }
216                 if ((ap->a_mode & FWRITE) && fip->fi_readers == 0) {
217                         VOP_UNLOCK(vp, 0);
218                         stops_deferred = sigallowstop();
219                         error = msleep(&fip->fi_writers, PIPE_MTX(fpipe),
220                             PDROP | PCATCH | PSOCK, "fifoow", 0);
221                         if (stops_deferred)
222                                 sigdeferstop();
223                         vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
224                         if (error) {
225                                 fip->fi_writers--;
226                                 if (fip->fi_writers == 0) {
227                                         PIPE_LOCK(fpipe);
228                                         fpipe->pipe_state |= PIPE_EOF;
229                                         if (fpipe->pipe_state & PIPE_WANTR)
230                                                 wakeup(fpipe);
231                                         fpipe->pipe_wgen++;
232                                         PIPE_UNLOCK(fpipe);
233                                         fifo_cleanup(vp);
234                                 }
235                                 return (error);
236                         }
237                         /*
238                          * We must have got woken up because we had
239                          * a reader.  That (and not still having one)
240                          * is the condition that we must wait for.
241                          */
242                         PIPE_LOCK(fpipe);
243                 }
244         }
245         PIPE_UNLOCK(fpipe);
246         KASSERT(fp != NULL, ("can't fifo/vnode bypass"));
247         KASSERT(fp->f_ops == &badfileops, ("not badfileops in fifo_open"));
248         finit(fp, fp->f_flag, DTYPE_FIFO, fpipe, &pipeops);
249         return (0);
250 }
251
252 /*
253  * Device close routine
254  */
255 /* ARGSUSED */
256 static int
257 fifo_close(ap)
258         struct vop_close_args /* {
259                 struct vnode *a_vp;
260                 int  a_fflag;
261                 struct ucred *a_cred;
262                 struct thread *a_td;
263         } */ *ap;
264 {
265         struct vnode *vp;
266         struct fifoinfo *fip;
267         struct pipe *cpipe;
268
269         vp = ap->a_vp;
270         fip = vp->v_fifoinfo;
271         cpipe = fip->fi_pipe;
272         ASSERT_VOP_ELOCKED(vp, "fifo_close");
273         if (ap->a_fflag & FREAD) {
274                 fip->fi_readers--;
275                 if (fip->fi_readers == 0) {
276                         PIPE_LOCK(cpipe);
277                         cpipe->pipe_state |= PIPE_EOF;
278                         if ((cpipe->pipe_state & PIPE_WANTW)) {
279                                 cpipe->pipe_state &= ~PIPE_WANTW;
280                                 wakeup(cpipe);
281                         }
282                         pipeselwakeup(cpipe);
283                         PIPE_UNLOCK(cpipe);
284                 }
285         }
286         if (ap->a_fflag & FWRITE) {
287                 fip->fi_writers--;
288                 if (fip->fi_writers == 0) {
289                         PIPE_LOCK(cpipe);
290                         cpipe->pipe_state |= PIPE_EOF;
291                         if ((cpipe->pipe_state & PIPE_WANTR)) {
292                                 cpipe->pipe_state &= ~PIPE_WANTR;
293                                 wakeup(cpipe);
294                         }
295                         cpipe->pipe_wgen++;
296                         pipeselwakeup(cpipe);
297                         PIPE_UNLOCK(cpipe);
298                 }
299         }
300         fifo_cleanup(vp);
301         return (0);
302 }
303
304 /*
305  * Print out internal contents of a fifo vnode.
306  */
307 int
308 fifo_printinfo(vp)
309         struct vnode *vp;
310 {
311         register struct fifoinfo *fip = vp->v_fifoinfo;
312
313         if (fip == NULL){
314                 printf(", NULL v_fifoinfo");
315                 return (0);
316         }
317         printf(", fifo with %ld readers and %ld writers",
318                 fip->fi_readers, fip->fi_writers);
319         return (0);
320 }
321
322 /*
323  * Print out the contents of a fifo vnode.
324  */
325 static int
326 fifo_print(ap)
327         struct vop_print_args /* {
328                 struct vnode *a_vp;
329         } */ *ap;
330 {
331         printf("    ");
332         fifo_printinfo(ap->a_vp);
333         printf("\n");
334         return (0);
335 }
336
337 /*
338  * Return POSIX pathconf information applicable to fifo's.
339  */
340 static int
341 fifo_pathconf(ap)
342         struct vop_pathconf_args /* {
343                 struct vnode *a_vp;
344                 int a_name;
345                 int *a_retval;
346         } */ *ap;
347 {
348
349         switch (ap->a_name) {
350         case _PC_LINK_MAX:
351                 *ap->a_retval = LINK_MAX;
352                 return (0);
353         case _PC_PIPE_BUF:
354                 *ap->a_retval = PIPE_BUF;
355                 return (0);
356         case _PC_CHOWN_RESTRICTED:
357                 *ap->a_retval = 1;
358                 return (0);
359         default:
360                 return (EINVAL);
361         }
362         /* NOTREACHED */
363 }
364
365 /*
366  * Fifo advisory byte-level locks.
367  */
368 /* ARGSUSED */
369 static int
370 fifo_advlock(ap)
371         struct vop_advlock_args /* {
372                 struct vnode *a_vp;
373                 caddr_t  a_id;
374                 int  a_op;
375                 struct flock *a_fl;
376                 int  a_flags;
377         } */ *ap;
378 {
379
380         return (ap->a_flags & F_FLOCK ? EOPNOTSUPP : EINVAL);
381 }
382