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