fifo_vnops.c revision 259814
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: stable/10/sys/fs/fifofs/fifo_vnops.c 259814 2013-12-24 07:25:49Z kib $
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 */
63struct fifoinfo {
64	struct pipe *fi_pipe;
65	long	fi_readers;
66	long	fi_writers;
67};
68
69static vop_print_t	fifo_print;
70static vop_open_t	fifo_open;
71static vop_close_t	fifo_close;
72static vop_pathconf_t	fifo_pathconf;
73static vop_advlock_t	fifo_advlock;
74
75struct 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 */
106static void
107fifo_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 */
125static int
126fifo_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;
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		error = pipe_named_ctor(&fpipe, td);
150		if (error != 0)
151			return (error);
152		fip = malloc(sizeof(*fip), M_VNODE, M_WAITOK);
153		fip->fi_pipe = fpipe;
154		fpipe->pipe_wgen = fip->fi_readers = fip->fi_writers = 0;
155 		KASSERT(vp->v_fifoinfo == NULL, ("fifo_open: v_fifoinfo race"));
156		vp->v_fifoinfo = fip;
157	}
158	fpipe = fip->fi_pipe;
159 	KASSERT(fpipe != NULL, ("fifo_open: pipe is NULL"));
160
161	/*
162	 * Use the pipe mutex here, in addition to the vnode lock,
163	 * in order to allow vnode lock dropping before msleep() calls
164	 * and still avoiding missed wakeups.
165	 */
166	PIPE_LOCK(fpipe);
167	if (ap->a_mode & FREAD) {
168		fip->fi_readers++;
169		if (fip->fi_readers == 1) {
170			fpipe->pipe_state &= ~PIPE_EOF;
171			if (fip->fi_writers > 0)
172				wakeup(&fip->fi_writers);
173		}
174		fp->f_seqcount = fpipe->pipe_wgen - fip->fi_writers;
175	}
176	if (ap->a_mode & FWRITE) {
177		if ((ap->a_mode & O_NONBLOCK) && fip->fi_readers == 0) {
178			PIPE_UNLOCK(fpipe);
179			if (fip->fi_writers == 0)
180				fifo_cleanup(vp);
181			return (ENXIO);
182		}
183		fip->fi_writers++;
184		if (fip->fi_writers == 1) {
185			fpipe->pipe_state &= ~PIPE_EOF;
186			if (fip->fi_readers > 0)
187				wakeup(&fip->fi_readers);
188		}
189	}
190	if ((ap->a_mode & O_NONBLOCK) == 0) {
191		if ((ap->a_mode & FREAD) && fip->fi_writers == 0) {
192			VOP_UNLOCK(vp, 0);
193			error = msleep(&fip->fi_readers, PIPE_MTX(fpipe),
194			    PDROP | PCATCH | PSOCK, "fifoor", 0);
195			vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
196			if (error) {
197				fip->fi_readers--;
198				if (fip->fi_readers == 0) {
199					PIPE_LOCK(fpipe);
200					fpipe->pipe_state |= PIPE_EOF;
201					if (fpipe->pipe_state & PIPE_WANTW)
202						wakeup(fpipe);
203					PIPE_UNLOCK(fpipe);
204					fifo_cleanup(vp);
205				}
206				return (error);
207			}
208			PIPE_LOCK(fpipe);
209			/*
210			 * We must have got woken up because we had a writer.
211			 * That (and not still having one) is the condition
212			 * that we must wait for.
213			 */
214		}
215		if ((ap->a_mode & FWRITE) && fip->fi_readers == 0) {
216			VOP_UNLOCK(vp, 0);
217			error = msleep(&fip->fi_writers, PIPE_MTX(fpipe),
218			    PDROP | PCATCH | PSOCK, "fifoow", 0);
219			vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
220			if (error) {
221				fip->fi_writers--;
222				if (fip->fi_writers == 0) {
223					PIPE_LOCK(fpipe);
224					fpipe->pipe_state |= PIPE_EOF;
225					if (fpipe->pipe_state & PIPE_WANTR)
226						wakeup(fpipe);
227					fpipe->pipe_wgen++;
228					PIPE_UNLOCK(fpipe);
229					fifo_cleanup(vp);
230				}
231				return (error);
232			}
233			/*
234			 * We must have got woken up because we had
235			 * a reader.  That (and not still having one)
236			 * is the condition that we must wait for.
237			 */
238			PIPE_LOCK(fpipe);
239		}
240	}
241	PIPE_UNLOCK(fpipe);
242	KASSERT(fp != NULL, ("can't fifo/vnode bypass"));
243	KASSERT(fp->f_ops == &badfileops, ("not badfileops in fifo_open"));
244	finit(fp, fp->f_flag, DTYPE_FIFO, fpipe, &pipeops);
245	return (0);
246}
247
248/*
249 * Device close routine
250 */
251/* ARGSUSED */
252static int
253fifo_close(ap)
254	struct vop_close_args /* {
255		struct vnode *a_vp;
256		int  a_fflag;
257		struct ucred *a_cred;
258		struct thread *a_td;
259	} */ *ap;
260{
261	struct vnode *vp;
262	struct fifoinfo *fip;
263	struct pipe *cpipe;
264
265	vp = ap->a_vp;
266	fip = vp->v_fifoinfo;
267	cpipe = fip->fi_pipe;
268	ASSERT_VOP_ELOCKED(vp, "fifo_close");
269	if (ap->a_fflag & FREAD) {
270		fip->fi_readers--;
271		if (fip->fi_readers == 0) {
272			PIPE_LOCK(cpipe);
273			cpipe->pipe_state |= PIPE_EOF;
274			if ((cpipe->pipe_state & PIPE_WANTW)) {
275				cpipe->pipe_state &= ~PIPE_WANTW;
276				wakeup(cpipe);
277			}
278			pipeselwakeup(cpipe);
279			PIPE_UNLOCK(cpipe);
280		}
281	}
282	if (ap->a_fflag & FWRITE) {
283		fip->fi_writers--;
284		if (fip->fi_writers == 0) {
285			PIPE_LOCK(cpipe);
286			cpipe->pipe_state |= PIPE_EOF;
287			if ((cpipe->pipe_state & PIPE_WANTR)) {
288				cpipe->pipe_state &= ~PIPE_WANTR;
289				wakeup(cpipe);
290			}
291			cpipe->pipe_wgen++;
292			pipeselwakeup(cpipe);
293			PIPE_UNLOCK(cpipe);
294		}
295	}
296	fifo_cleanup(vp);
297	return (0);
298}
299
300/*
301 * Print out internal contents of a fifo vnode.
302 */
303int
304fifo_printinfo(vp)
305	struct vnode *vp;
306{
307	register struct fifoinfo *fip = vp->v_fifoinfo;
308
309	if (fip == NULL){
310		printf(", NULL v_fifoinfo");
311		return (0);
312	}
313	printf(", fifo with %ld readers and %ld writers",
314		fip->fi_readers, fip->fi_writers);
315	return (0);
316}
317
318/*
319 * Print out the contents of a fifo vnode.
320 */
321static int
322fifo_print(ap)
323	struct vop_print_args /* {
324		struct vnode *a_vp;
325	} */ *ap;
326{
327	printf("    ");
328	fifo_printinfo(ap->a_vp);
329	printf("\n");
330	return (0);
331}
332
333/*
334 * Return POSIX pathconf information applicable to fifo's.
335 */
336static int
337fifo_pathconf(ap)
338	struct vop_pathconf_args /* {
339		struct vnode *a_vp;
340		int a_name;
341		int *a_retval;
342	} */ *ap;
343{
344
345	switch (ap->a_name) {
346	case _PC_LINK_MAX:
347		*ap->a_retval = LINK_MAX;
348		return (0);
349	case _PC_PIPE_BUF:
350		*ap->a_retval = PIPE_BUF;
351		return (0);
352	case _PC_CHOWN_RESTRICTED:
353		*ap->a_retval = 1;
354		return (0);
355	default:
356		return (EINVAL);
357	}
358	/* NOTREACHED */
359}
360
361/*
362 * Fifo advisory byte-level locks.
363 */
364/* ARGSUSED */
365static int
366fifo_advlock(ap)
367	struct vop_advlock_args /* {
368		struct vnode *a_vp;
369		caddr_t  a_id;
370		int  a_op;
371		struct flock *a_fl;
372		int  a_flags;
373	} */ *ap;
374{
375
376	return (ap->a_flags & F_FLOCK ? EOPNOTSUPP : EINVAL);
377}
378
379