xref: /original-bsd/sys/miscfs/fifofs/fifo_vnops.c (revision 9a35f7df)
1 /*
2  * Copyright (c) 1990, 1993, 1995
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * %sccs.include.redist.c%
6  *
7  *	@(#)fifo_vnops.c	8.9 (Berkeley) 05/22/95
8  */
9 
10 #include <sys/param.h>
11 #include <sys/systm.h>
12 #include <sys/proc.h>
13 #include <sys/time.h>
14 #include <sys/namei.h>
15 #include <sys/vnode.h>
16 #include <sys/socket.h>
17 #include <sys/socketvar.h>
18 #include <sys/stat.h>
19 #include <sys/ioctl.h>
20 #include <sys/file.h>
21 #include <sys/errno.h>
22 #include <sys/malloc.h>
23 #include <miscfs/fifofs/fifo.h>
24 
25 /*
26  * This structure is associated with the FIFO vnode and stores
27  * the state associated with the FIFO.
28  */
29 struct fifoinfo {
30 	struct socket	*fi_readsock;
31 	struct socket	*fi_writesock;
32 	long		fi_readers;
33 	long		fi_writers;
34 };
35 
36 int (**fifo_vnodeop_p)();
37 struct vnodeopv_entry_desc fifo_vnodeop_entries[] = {
38 	{ &vop_default_desc, vn_default_error },
39 	{ &vop_lookup_desc, fifo_lookup },		/* lookup */
40 	{ &vop_create_desc, fifo_create },		/* create */
41 	{ &vop_mknod_desc, fifo_mknod },		/* mknod */
42 	{ &vop_open_desc, fifo_open },			/* open */
43 	{ &vop_close_desc, fifo_close },		/* close */
44 	{ &vop_access_desc, fifo_access },		/* access */
45 	{ &vop_getattr_desc, fifo_getattr },		/* getattr */
46 	{ &vop_setattr_desc, fifo_setattr },		/* setattr */
47 	{ &vop_read_desc, fifo_read },			/* read */
48 	{ &vop_write_desc, fifo_write },		/* write */
49 	{ &vop_lease_desc, fifo_lease_check },		/* lease */
50 	{ &vop_ioctl_desc, fifo_ioctl },		/* ioctl */
51 	{ &vop_select_desc, fifo_select },		/* select */
52 	{ &vop_revoke_desc, fifo_revoke },		/* revoke */
53 	{ &vop_mmap_desc, fifo_mmap },			/* mmap */
54 	{ &vop_fsync_desc, fifo_fsync },		/* fsync */
55 	{ &vop_seek_desc, fifo_seek },			/* seek */
56 	{ &vop_remove_desc, fifo_remove },		/* remove */
57 	{ &vop_link_desc, fifo_link },			/* link */
58 	{ &vop_rename_desc, fifo_rename },		/* rename */
59 	{ &vop_mkdir_desc, fifo_mkdir },		/* mkdir */
60 	{ &vop_rmdir_desc, fifo_rmdir },		/* rmdir */
61 	{ &vop_symlink_desc, fifo_symlink },		/* symlink */
62 	{ &vop_readdir_desc, fifo_readdir },		/* readdir */
63 	{ &vop_readlink_desc, fifo_readlink },		/* readlink */
64 	{ &vop_abortop_desc, fifo_abortop },		/* abortop */
65 	{ &vop_inactive_desc, fifo_inactive },		/* inactive */
66 	{ &vop_reclaim_desc, fifo_reclaim },		/* reclaim */
67 	{ &vop_lock_desc, fifo_lock },			/* lock */
68 	{ &vop_unlock_desc, fifo_unlock },		/* unlock */
69 	{ &vop_bmap_desc, fifo_bmap },			/* bmap */
70 	{ &vop_strategy_desc, fifo_strategy },		/* strategy */
71 	{ &vop_print_desc, fifo_print },		/* print */
72 	{ &vop_islocked_desc, fifo_islocked },		/* islocked */
73 	{ &vop_pathconf_desc, fifo_pathconf },		/* pathconf */
74 	{ &vop_advlock_desc, fifo_advlock },		/* advlock */
75 	{ &vop_blkatoff_desc, fifo_blkatoff },		/* blkatoff */
76 	{ &vop_valloc_desc, fifo_valloc },		/* valloc */
77 	{ &vop_vfree_desc, fifo_vfree },		/* vfree */
78 	{ &vop_truncate_desc, fifo_truncate },		/* truncate */
79 	{ &vop_update_desc, fifo_update },		/* update */
80 	{ &vop_bwrite_desc, fifo_bwrite },		/* bwrite */
81 	{ (struct vnodeop_desc*)NULL, (int(*)())NULL }
82 };
83 struct vnodeopv_desc fifo_vnodeop_opv_desc =
84 	{ &fifo_vnodeop_p, fifo_vnodeop_entries };
85 
86 /*
87  * Trivial lookup routine that always fails.
88  */
89 /* ARGSUSED */
90 fifo_lookup(ap)
91 	struct vop_lookup_args /* {
92 		struct vnode * a_dvp;
93 		struct vnode ** a_vpp;
94 		struct componentname * a_cnp;
95 	} */ *ap;
96 {
97 
98 	*ap->a_vpp = NULL;
99 	return (ENOTDIR);
100 }
101 
102 /*
103  * Open called to set up a new instance of a fifo or
104  * to find an active instance of a fifo.
105  */
106 /* ARGSUSED */
107 fifo_open(ap)
108 	struct vop_open_args /* {
109 		struct vnode *a_vp;
110 		int  a_mode;
111 		struct ucred *a_cred;
112 		struct proc *a_p;
113 	} */ *ap;
114 {
115 	struct vnode *vp = ap->a_vp;
116 	struct fifoinfo *fip;
117 	struct proc *p = ap->a_p;
118 	struct socket *rso, *wso;
119 	int error;
120 	static char openstr[] = "fifo";
121 
122 	if ((ap->a_mode & (FREAD|FWRITE)) == (FREAD|FWRITE))
123 		return (EINVAL);
124 	if ((fip = vp->v_fifoinfo) == NULL) {
125 		MALLOC(fip, struct fifoinfo *, sizeof(*fip), M_VNODE, M_WAITOK);
126 		vp->v_fifoinfo = fip;
127 		if (error = socreate(AF_LOCAL, &rso, SOCK_STREAM, 0)) {
128 			free(fip, M_VNODE);
129 			vp->v_fifoinfo = NULL;
130 			return (error);
131 		}
132 		fip->fi_readsock = rso;
133 		if (error = socreate(AF_LOCAL, &wso, SOCK_STREAM, 0)) {
134 			(void)soclose(rso);
135 			free(fip, M_VNODE);
136 			vp->v_fifoinfo = NULL;
137 			return (error);
138 		}
139 		fip->fi_writesock = wso;
140 		if (error = unp_connect2(wso, rso)) {
141 			(void)soclose(wso);
142 			(void)soclose(rso);
143 			free(fip, M_VNODE);
144 			vp->v_fifoinfo = NULL;
145 			return (error);
146 		}
147 		fip->fi_readers = fip->fi_writers = 0;
148 		wso->so_state |= SS_CANTRCVMORE;
149 		rso->so_state |= SS_CANTSENDMORE;
150 	}
151 	error = 0;
152 	if (ap->a_mode & FREAD) {
153 		fip->fi_readers++;
154 		if (fip->fi_readers == 1) {
155 			fip->fi_writesock->so_state &= ~SS_CANTSENDMORE;
156 			if (fip->fi_writers > 0)
157 				wakeup((caddr_t)&fip->fi_writers);
158 		}
159 		if (ap->a_mode & O_NONBLOCK)
160 			return (0);
161 		while (fip->fi_writers == 0) {
162 			VOP_UNLOCK(vp, 0, p);
163 			error = tsleep((caddr_t)&fip->fi_readers,
164 			    PCATCH | PSOCK, openstr, 0);
165 			vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, p);
166 			if (error)
167 				break;
168 		}
169 	} else {
170 		fip->fi_writers++;
171 		if (fip->fi_readers == 0 && (ap->a_mode & O_NONBLOCK)) {
172 			error = ENXIO;
173 		} else {
174 			if (fip->fi_writers == 1) {
175 				fip->fi_readsock->so_state &= ~SS_CANTRCVMORE;
176 				if (fip->fi_readers > 0)
177 					wakeup((caddr_t)&fip->fi_readers);
178 			}
179 			while (fip->fi_readers == 0) {
180 				VOP_UNLOCK(vp, 0, p);
181 				error = tsleep((caddr_t)&fip->fi_writers,
182 				    PCATCH | PSOCK, openstr, 0);
183 				vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, p);
184 				if (error)
185 					break;
186 			}
187 		}
188 	}
189 	if (error)
190 		VOP_CLOSE(vp, ap->a_mode, ap->a_cred, p);
191 	return (error);
192 }
193 
194 /*
195  * Vnode op for read
196  */
197 /* ARGSUSED */
198 fifo_read(ap)
199 	struct vop_read_args /* {
200 		struct vnode *a_vp;
201 		struct uio *a_uio;
202 		int  a_ioflag;
203 		struct ucred *a_cred;
204 	} */ *ap;
205 {
206 	struct uio *uio = ap->a_uio;
207 	struct socket *rso = ap->a_vp->v_fifoinfo->fi_readsock;
208 	struct proc *p = uio->uio_procp;
209 	int error, startresid;
210 
211 #ifdef DIAGNOSTIC
212 	if (uio->uio_rw != UIO_READ)
213 		panic("fifo_read mode");
214 #endif
215 	if (uio->uio_resid == 0)
216 		return (0);
217 	if (ap->a_ioflag & IO_NDELAY)
218 		rso->so_state |= SS_NBIO;
219 	startresid = uio->uio_resid;
220 	VOP_UNLOCK(ap->a_vp, 0, p);
221 	error = soreceive(rso, (struct mbuf **)0, uio, (struct mbuf **)0,
222 	    (struct mbuf **)0, (int *)0);
223 	vn_lock(ap->a_vp, LK_EXCLUSIVE | LK_RETRY, p);
224 	/*
225 	 * Clear EOF indication after first such return.
226 	 */
227 	if (uio->uio_resid == startresid)
228 		rso->so_state &= ~SS_CANTRCVMORE;
229 	if (ap->a_ioflag & IO_NDELAY)
230 		rso->so_state &= ~SS_NBIO;
231 	return (error);
232 }
233 
234 /*
235  * Vnode op for write
236  */
237 /* ARGSUSED */
238 fifo_write(ap)
239 	struct vop_write_args /* {
240 		struct vnode *a_vp;
241 		struct uio *a_uio;
242 		int  a_ioflag;
243 		struct ucred *a_cred;
244 	} */ *ap;
245 {
246 	struct socket *wso = ap->a_vp->v_fifoinfo->fi_writesock;
247 	struct proc *p = ap->a_uio->uio_procp;
248 	int error;
249 
250 #ifdef DIAGNOSTIC
251 	if (ap->a_uio->uio_rw != UIO_WRITE)
252 		panic("fifo_write mode");
253 #endif
254 	if (ap->a_ioflag & IO_NDELAY)
255 		wso->so_state |= SS_NBIO;
256 	VOP_UNLOCK(ap->a_vp, 0, p);
257 	error = sosend(wso, (struct mbuf *)0, ap->a_uio, 0, (struct mbuf *)0, 0);
258 	vn_lock(ap->a_vp, LK_EXCLUSIVE | LK_RETRY, p);
259 	if (ap->a_ioflag & IO_NDELAY)
260 		wso->so_state &= ~SS_NBIO;
261 	return (error);
262 }
263 
264 /*
265  * Device ioctl operation.
266  */
267 /* ARGSUSED */
268 fifo_ioctl(ap)
269 	struct vop_ioctl_args /* {
270 		struct vnode *a_vp;
271 		int  a_command;
272 		caddr_t  a_data;
273 		int  a_fflag;
274 		struct ucred *a_cred;
275 		struct proc *a_p;
276 	} */ *ap;
277 {
278 	struct file filetmp;
279 
280 	if (ap->a_command == FIONBIO)
281 		return (0);
282 	if (ap->a_fflag & FREAD)
283 		filetmp.f_data = (caddr_t)ap->a_vp->v_fifoinfo->fi_readsock;
284 	else
285 		filetmp.f_data = (caddr_t)ap->a_vp->v_fifoinfo->fi_writesock;
286 	return (soo_ioctl(&filetmp, ap->a_command, ap->a_data, ap->a_p));
287 }
288 
289 /* ARGSUSED */
290 fifo_select(ap)
291 	struct vop_select_args /* {
292 		struct vnode *a_vp;
293 		int  a_which;
294 		int  a_fflags;
295 		struct ucred *a_cred;
296 		struct proc *a_p;
297 	} */ *ap;
298 {
299 	struct file filetmp;
300 
301 	if (ap->a_fflags & FREAD)
302 		filetmp.f_data = (caddr_t)ap->a_vp->v_fifoinfo->fi_readsock;
303 	else
304 		filetmp.f_data = (caddr_t)ap->a_vp->v_fifoinfo->fi_writesock;
305 	return (soo_select(&filetmp, ap->a_which, ap->a_p));
306 }
307 
308 int
309 fifo_inactive(ap)
310 	struct vop_inactive_args /* {
311 		struct vnode *a_vp;
312 		struct proc *a_p;
313 	} */ *ap;
314 {
315 
316 	VOP_UNLOCK(ap->a_vp, 0, ap->a_p);
317 	return (0);
318 }
319 
320 /*
321  * This is a noop, simply returning what one has been given.
322  */
323 fifo_bmap(ap)
324 	struct vop_bmap_args /* {
325 		struct vnode *a_vp;
326 		daddr_t  a_bn;
327 		struct vnode **a_vpp;
328 		daddr_t *a_bnp;
329 		int *a_runp;
330 	} */ *ap;
331 {
332 
333 	if (ap->a_vpp != NULL)
334 		*ap->a_vpp = ap->a_vp;
335 	if (ap->a_bnp != NULL)
336 		*ap->a_bnp = ap->a_bn;
337 	if (ap->a_runp != NULL)
338 		*ap->a_runp = 0;
339 	return (0);
340 }
341 
342 /*
343  * Device close routine
344  */
345 /* ARGSUSED */
346 fifo_close(ap)
347 	struct vop_close_args /* {
348 		struct vnode *a_vp;
349 		int  a_fflag;
350 		struct ucred *a_cred;
351 		struct proc *a_p;
352 	} */ *ap;
353 {
354 	register struct vnode *vp = ap->a_vp;
355 	register struct fifoinfo *fip = vp->v_fifoinfo;
356 	int error1, error2;
357 
358 	if (ap->a_fflag & FWRITE) {
359 		fip->fi_writers--;
360 		if (fip->fi_writers == 0)
361 			socantrcvmore(fip->fi_readsock);
362 	} else {
363 		fip->fi_readers--;
364 		if (fip->fi_readers == 0)
365 			socantsendmore(fip->fi_writesock);
366 	}
367 	if (vp->v_usecount > 1)
368 		return (0);
369 	error1 = soclose(fip->fi_readsock);
370 	error2 = soclose(fip->fi_writesock);
371 	FREE(fip, M_VNODE);
372 	vp->v_fifoinfo = NULL;
373 	if (error1)
374 		return (error1);
375 	return (error2);
376 }
377 
378 /*
379  * Print out the contents of a fifo vnode.
380  */
381 fifo_print(ap)
382 	struct vop_print_args /* {
383 		struct vnode *a_vp;
384 	} */ *ap;
385 {
386 
387 	printf("tag VT_NON");
388 	fifo_printinfo(ap->a_vp);
389 	printf("\n");
390 }
391 
392 /*
393  * Print out internal contents of a fifo vnode.
394  */
395 fifo_printinfo(vp)
396 	struct vnode *vp;
397 {
398 	register struct fifoinfo *fip = vp->v_fifoinfo;
399 
400 	printf(", fifo with %d readers and %d writers",
401 		fip->fi_readers, fip->fi_writers);
402 }
403 
404 /*
405  * Return POSIX pathconf information applicable to fifo's.
406  */
407 fifo_pathconf(ap)
408 	struct vop_pathconf_args /* {
409 		struct vnode *a_vp;
410 		int a_name;
411 		int *a_retval;
412 	} */ *ap;
413 {
414 
415 	switch (ap->a_name) {
416 	case _PC_LINK_MAX:
417 		*ap->a_retval = LINK_MAX;
418 		return (0);
419 	case _PC_PIPE_BUF:
420 		*ap->a_retval = PIPE_BUF;
421 		return (0);
422 	case _PC_CHOWN_RESTRICTED:
423 		*ap->a_retval = 1;
424 		return (0);
425 	default:
426 		return (EINVAL);
427 	}
428 	/* NOTREACHED */
429 }
430 
431 /*
432  * Fifo failed operation
433  */
434 fifo_ebadf()
435 {
436 
437 	return (EBADF);
438 }
439 
440 /*
441  * Fifo advisory byte-level locks.
442  */
443 /* ARGSUSED */
444 fifo_advlock(ap)
445 	struct vop_advlock_args /* {
446 		struct vnode *a_vp;
447 		caddr_t  a_id;
448 		int  a_op;
449 		struct flock *a_fl;
450 		int  a_flags;
451 	} */ *ap;
452 {
453 
454 	return (EOPNOTSUPP);
455 }
456 
457 /*
458  * Fifo bad operation
459  */
460 fifo_badop()
461 {
462 
463 	panic("fifo_badop called");
464 	/* NOTREACHED */
465 }
466