xref: /original-bsd/sys/miscfs/fifofs/fifo_vnops.c (revision 0ac4996f)
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.8 (Berkeley) 05/14/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 /*
309  * This is a noop, simply returning what one has been given.
310  */
311 fifo_bmap(ap)
312 	struct vop_bmap_args /* {
313 		struct vnode *a_vp;
314 		daddr_t  a_bn;
315 		struct vnode **a_vpp;
316 		daddr_t *a_bnp;
317 		int *a_runp;
318 	} */ *ap;
319 {
320 
321 	if (ap->a_vpp != NULL)
322 		*ap->a_vpp = ap->a_vp;
323 	if (ap->a_bnp != NULL)
324 		*ap->a_bnp = ap->a_bn;
325 	if (ap->a_runp != NULL)
326 		*ap->a_runp = 0;
327 	return (0);
328 }
329 
330 /*
331  * Device close routine
332  */
333 /* ARGSUSED */
334 fifo_close(ap)
335 	struct vop_close_args /* {
336 		struct vnode *a_vp;
337 		int  a_fflag;
338 		struct ucred *a_cred;
339 		struct proc *a_p;
340 	} */ *ap;
341 {
342 	register struct vnode *vp = ap->a_vp;
343 	register struct fifoinfo *fip = vp->v_fifoinfo;
344 	int error1, error2;
345 
346 	if (ap->a_fflag & FWRITE) {
347 		fip->fi_writers--;
348 		if (fip->fi_writers == 0)
349 			socantrcvmore(fip->fi_readsock);
350 	} else {
351 		fip->fi_readers--;
352 		if (fip->fi_readers == 0)
353 			socantsendmore(fip->fi_writesock);
354 	}
355 	if (vp->v_usecount > 1)
356 		return (0);
357 	error1 = soclose(fip->fi_readsock);
358 	error2 = soclose(fip->fi_writesock);
359 	FREE(fip, M_VNODE);
360 	vp->v_fifoinfo = NULL;
361 	if (error1)
362 		return (error1);
363 	return (error2);
364 }
365 
366 /*
367  * Print out the contents of a fifo vnode.
368  */
369 fifo_print(ap)
370 	struct vop_print_args /* {
371 		struct vnode *a_vp;
372 	} */ *ap;
373 {
374 
375 	printf("tag VT_NON");
376 	fifo_printinfo(ap->a_vp);
377 	printf("\n");
378 }
379 
380 /*
381  * Print out internal contents of a fifo vnode.
382  */
383 fifo_printinfo(vp)
384 	struct vnode *vp;
385 {
386 	register struct fifoinfo *fip = vp->v_fifoinfo;
387 
388 	printf(", fifo with %d readers and %d writers",
389 		fip->fi_readers, fip->fi_writers);
390 }
391 
392 /*
393  * Return POSIX pathconf information applicable to fifo's.
394  */
395 fifo_pathconf(ap)
396 	struct vop_pathconf_args /* {
397 		struct vnode *a_vp;
398 		int a_name;
399 		int *a_retval;
400 	} */ *ap;
401 {
402 
403 	switch (ap->a_name) {
404 	case _PC_LINK_MAX:
405 		*ap->a_retval = LINK_MAX;
406 		return (0);
407 	case _PC_PIPE_BUF:
408 		*ap->a_retval = PIPE_BUF;
409 		return (0);
410 	case _PC_CHOWN_RESTRICTED:
411 		*ap->a_retval = 1;
412 		return (0);
413 	default:
414 		return (EINVAL);
415 	}
416 	/* NOTREACHED */
417 }
418 
419 /*
420  * Fifo failed operation
421  */
422 fifo_ebadf()
423 {
424 
425 	return (EBADF);
426 }
427 
428 /*
429  * Fifo advisory byte-level locks.
430  */
431 /* ARGSUSED */
432 fifo_advlock(ap)
433 	struct vop_advlock_args /* {
434 		struct vnode *a_vp;
435 		caddr_t  a_id;
436 		int  a_op;
437 		struct flock *a_fl;
438 		int  a_flags;
439 	} */ *ap;
440 {
441 
442 	return (EOPNOTSUPP);
443 }
444 
445 /*
446  * Fifo bad operation
447  */
448 fifo_badop()
449 {
450 
451 	panic("fifo_badop called");
452 	/* NOTREACHED */
453 }
454