fifo_vnops.c revision 1.34 1 /* $NetBSD: fifo_vnops.c,v 1.34 2002/07/27 16:43:36 chs Exp $ */
2
3 /*
4 * Copyright (c) 1990, 1993, 1995
5 * The Regents of the University of California. All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed by the University of
18 * California, Berkeley and its contributors.
19 * 4. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * SUCH DAMAGE.
34 *
35 * @(#)fifo_vnops.c 8.10 (Berkeley) 5/27/95
36 */
37
38 #include <sys/cdefs.h>
39 __KERNEL_RCSID(0, "$NetBSD: fifo_vnops.c,v 1.34 2002/07/27 16:43:36 chs Exp $");
40
41 #include <sys/param.h>
42 #include <sys/systm.h>
43 #include <sys/proc.h>
44 #include <sys/time.h>
45 #include <sys/namei.h>
46 #include <sys/vnode.h>
47 #include <sys/socket.h>
48 #include <sys/socketvar.h>
49 #include <sys/stat.h>
50 #include <sys/ioctl.h>
51 #include <sys/file.h>
52 #include <sys/errno.h>
53 #include <sys/malloc.h>
54 #include <sys/un.h>
55 #include <sys/poll.h>
56
57 #include <miscfs/fifofs/fifo.h>
58 #include <miscfs/genfs/genfs.h>
59
60 /*
61 * This structure is associated with the FIFO vnode and stores
62 * the state associated with the FIFO.
63 */
64 struct fifoinfo {
65 struct socket *fi_readsock;
66 struct socket *fi_writesock;
67 long fi_opencount;
68 long fi_readers;
69 long fi_writers;
70 };
71
72 int (**fifo_vnodeop_p) __P((void *));
73 const struct vnodeopv_entry_desc fifo_vnodeop_entries[] = {
74 { &vop_default_desc, vn_default_error },
75 { &vop_lookup_desc, fifo_lookup }, /* lookup */
76 { &vop_create_desc, fifo_create }, /* create */
77 { &vop_mknod_desc, fifo_mknod }, /* mknod */
78 { &vop_open_desc, fifo_open }, /* open */
79 { &vop_close_desc, fifo_close }, /* close */
80 { &vop_access_desc, fifo_access }, /* access */
81 { &vop_getattr_desc, fifo_getattr }, /* getattr */
82 { &vop_setattr_desc, fifo_setattr }, /* setattr */
83 { &vop_read_desc, fifo_read }, /* read */
84 { &vop_write_desc, fifo_write }, /* write */
85 { &vop_lease_desc, fifo_lease_check }, /* lease */
86 { &vop_ioctl_desc, fifo_ioctl }, /* ioctl */
87 { &vop_poll_desc, fifo_poll }, /* poll */
88 { &vop_revoke_desc, fifo_revoke }, /* revoke */
89 { &vop_mmap_desc, fifo_mmap }, /* mmap */
90 { &vop_fsync_desc, fifo_fsync }, /* fsync */
91 { &vop_seek_desc, fifo_seek }, /* seek */
92 { &vop_remove_desc, fifo_remove }, /* remove */
93 { &vop_link_desc, fifo_link }, /* link */
94 { &vop_rename_desc, fifo_rename }, /* rename */
95 { &vop_mkdir_desc, fifo_mkdir }, /* mkdir */
96 { &vop_rmdir_desc, fifo_rmdir }, /* rmdir */
97 { &vop_symlink_desc, fifo_symlink }, /* symlink */
98 { &vop_readdir_desc, fifo_readdir }, /* readdir */
99 { &vop_readlink_desc, fifo_readlink }, /* readlink */
100 { &vop_abortop_desc, fifo_abortop }, /* abortop */
101 { &vop_inactive_desc, fifo_inactive }, /* inactive */
102 { &vop_reclaim_desc, fifo_reclaim }, /* reclaim */
103 { &vop_lock_desc, fifo_lock }, /* lock */
104 { &vop_unlock_desc, fifo_unlock }, /* unlock */
105 { &vop_bmap_desc, fifo_bmap }, /* bmap */
106 { &vop_strategy_desc, fifo_strategy }, /* strategy */
107 { &vop_print_desc, fifo_print }, /* print */
108 { &vop_islocked_desc, fifo_islocked }, /* islocked */
109 { &vop_pathconf_desc, fifo_pathconf }, /* pathconf */
110 { &vop_advlock_desc, fifo_advlock }, /* advlock */
111 { &vop_blkatoff_desc, fifo_blkatoff }, /* blkatoff */
112 { &vop_valloc_desc, fifo_valloc }, /* valloc */
113 { &vop_vfree_desc, fifo_vfree }, /* vfree */
114 { &vop_truncate_desc, fifo_truncate }, /* truncate */
115 { &vop_update_desc, fifo_update }, /* update */
116 { &vop_bwrite_desc, fifo_bwrite }, /* bwrite */
117 { &vop_putpages_desc, fifo_putpages }, /* putpages */
118 { (struct vnodeop_desc*)NULL, (int(*) __P((void *)))NULL }
119 };
120 const struct vnodeopv_desc fifo_vnodeop_opv_desc =
121 { &fifo_vnodeop_p, fifo_vnodeop_entries };
122
123 /*
124 * Trivial lookup routine that always fails.
125 */
126 /* ARGSUSED */
127 int
128 fifo_lookup(void *v)
129 {
130 struct vop_lookup_args /* {
131 struct vnode *a_dvp;
132 struct vnode **a_vpp;
133 struct componentname *a_cnp;
134 } */ *ap = v;
135
136 *ap->a_vpp = NULL;
137 return (ENOTDIR);
138 }
139
140 /*
141 * Open called to set up a new instance of a fifo or
142 * to find an active instance of a fifo.
143 */
144 /* ARGSUSED */
145 int
146 fifo_open(void *v)
147 {
148 struct vop_open_args /* {
149 struct vnode *a_vp;
150 int a_mode;
151 struct ucred *a_cred;
152 struct proc *a_p;
153 } */ *ap = v;
154 struct vnode *vp;
155 struct fifoinfo *fip;
156 struct proc *p;
157 struct socket *rso, *wso;
158 int error;
159 static const char openstr[] = "fifo";
160
161 vp = ap->a_vp;
162 p = ap->a_p;
163
164 if ((fip = vp->v_fifoinfo) == NULL) {
165 MALLOC(fip, struct fifoinfo *, sizeof(*fip), M_VNODE, M_WAITOK);
166 vp->v_fifoinfo = fip;
167 if ((error = socreate(AF_LOCAL, &rso, SOCK_STREAM, 0)) != 0) {
168 free(fip, M_VNODE);
169 vp->v_fifoinfo = NULL;
170 return (error);
171 }
172 fip->fi_readsock = rso;
173 if ((error = socreate(AF_LOCAL, &wso, SOCK_STREAM, 0)) != 0) {
174 (void)soclose(rso);
175 free(fip, M_VNODE);
176 vp->v_fifoinfo = NULL;
177 return (error);
178 }
179 fip->fi_writesock = wso;
180 if ((error = unp_connect2(wso, rso)) != 0) {
181 (void)soclose(wso);
182 (void)soclose(rso);
183 free(fip, M_VNODE);
184 vp->v_fifoinfo = NULL;
185 return (error);
186 }
187 fip->fi_readers = fip->fi_writers = 0;
188 fip->fi_opencount = 0;
189 wso->so_state |= SS_CANTRCVMORE;
190 rso->so_state |= SS_CANTSENDMORE;
191 }
192 fip->fi_opencount++;
193 if (ap->a_mode & FREAD) {
194 if (fip->fi_readers++ == 0) {
195 fip->fi_writesock->so_state &= ~SS_CANTSENDMORE;
196 if (fip->fi_writers > 0)
197 wakeup((caddr_t)&fip->fi_writers);
198 }
199 }
200 if (ap->a_mode & FWRITE) {
201 if (fip->fi_writers++ == 0) {
202 fip->fi_readsock->so_state &= ~SS_CANTRCVMORE;
203 if (fip->fi_readers > 0)
204 wakeup((caddr_t)&fip->fi_readers);
205 }
206 }
207 if (ap->a_mode & FREAD) {
208 if (ap->a_mode & O_NONBLOCK) {
209 } else {
210 while (fip->fi_writers == 0) {
211 VOP_UNLOCK(vp, 0);
212 error = tsleep((caddr_t)&fip->fi_readers,
213 PCATCH | PSOCK, openstr, 0);
214 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
215 if (error)
216 goto bad;
217 }
218 }
219 }
220 if (ap->a_mode & FWRITE) {
221 if (ap->a_mode & O_NONBLOCK) {
222 if (fip->fi_readers == 0) {
223 error = ENXIO;
224 goto bad;
225 }
226 } else {
227 while (fip->fi_readers == 0) {
228 VOP_UNLOCK(vp, 0);
229 error = tsleep((caddr_t)&fip->fi_writers,
230 PCATCH | PSOCK, openstr, 0);
231 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
232 if (error)
233 goto bad;
234 }
235 }
236 }
237 return (0);
238 bad:
239 VOP_CLOSE(vp, ap->a_mode, ap->a_cred, p);
240 return (error);
241 }
242
243 /*
244 * Vnode op for read
245 */
246 /* ARGSUSED */
247 int
248 fifo_read(void *v)
249 {
250 struct vop_read_args /* {
251 struct vnode *a_vp;
252 struct uio *a_uio;
253 int a_ioflag;
254 struct ucred *a_cred;
255 } */ *ap = v;
256 struct uio *uio;
257 struct socket *rso;
258 int error, startresid;
259
260 uio = ap->a_uio;
261 rso = ap->a_vp->v_fifoinfo->fi_readsock;
262 #ifdef DIAGNOSTIC
263 if (uio->uio_rw != UIO_READ)
264 panic("fifo_read mode");
265 #endif
266 if (uio->uio_resid == 0)
267 return (0);
268 if (ap->a_ioflag & IO_NDELAY)
269 rso->so_state |= SS_NBIO;
270 startresid = uio->uio_resid;
271 VOP_UNLOCK(ap->a_vp, 0);
272 error = (*rso->so_receive)(rso, (struct mbuf **)0, uio,
273 (struct mbuf **)0, (struct mbuf **)0, (int *)0);
274 vn_lock(ap->a_vp, LK_EXCLUSIVE | LK_RETRY);
275 /*
276 * Clear EOF indication after first such return.
277 */
278 if (uio->uio_resid == startresid)
279 rso->so_state &= ~SS_CANTRCVMORE;
280 if (ap->a_ioflag & IO_NDELAY) {
281 rso->so_state &= ~SS_NBIO;
282 if (error == EWOULDBLOCK &&
283 ap->a_vp->v_fifoinfo->fi_writers == 0)
284 error = 0;
285 }
286 return (error);
287 }
288
289 /*
290 * Vnode op for write
291 */
292 /* ARGSUSED */
293 int
294 fifo_write(void *v)
295 {
296 struct vop_write_args /* {
297 struct vnode *a_vp;
298 struct uio *a_uio;
299 int a_ioflag;
300 struct ucred *a_cred;
301 } */ *ap = v;
302 struct socket *wso;
303 int error;
304
305 wso = ap->a_vp->v_fifoinfo->fi_writesock;
306 #ifdef DIAGNOSTIC
307 if (ap->a_uio->uio_rw != UIO_WRITE)
308 panic("fifo_write mode");
309 #endif
310 if (ap->a_ioflag & IO_NDELAY)
311 wso->so_state |= SS_NBIO;
312 VOP_UNLOCK(ap->a_vp, 0);
313 error = (*wso->so_send)(wso, (struct mbuf *)0, ap->a_uio, 0,
314 (struct mbuf *)0, 0);
315 vn_lock(ap->a_vp, LK_EXCLUSIVE | LK_RETRY);
316 if (ap->a_ioflag & IO_NDELAY)
317 wso->so_state &= ~SS_NBIO;
318 return (error);
319 }
320
321 /*
322 * Device ioctl operation.
323 */
324 /* ARGSUSED */
325 int
326 fifo_ioctl(void *v)
327 {
328 struct vop_ioctl_args /* {
329 struct vnode *a_vp;
330 u_long a_command;
331 caddr_t a_data;
332 int a_fflag;
333 struct ucred *a_cred;
334 struct proc *a_p;
335 } */ *ap = v;
336 struct file filetmp;
337 int error;
338
339 if (ap->a_command == FIONBIO)
340 return (0);
341 if (ap->a_fflag & FREAD) {
342 filetmp.f_data = (caddr_t)ap->a_vp->v_fifoinfo->fi_readsock;
343 error = soo_ioctl(&filetmp, ap->a_command, ap->a_data, ap->a_p);
344 if (error)
345 return (error);
346 }
347 if (ap->a_fflag & FWRITE) {
348 filetmp.f_data = (caddr_t)ap->a_vp->v_fifoinfo->fi_writesock;
349 error = soo_ioctl(&filetmp, ap->a_command, ap->a_data, ap->a_p);
350 if (error)
351 return (error);
352 }
353 return (0);
354 }
355
356 /* ARGSUSED */
357 int
358 fifo_poll(void *v)
359 {
360 struct vop_poll_args /* {
361 struct vnode *a_vp;
362 int a_events;
363 struct proc *a_p;
364 } */ *ap = v;
365 struct file filetmp;
366 int revents;
367
368 revents = 0;
369 if (ap->a_events & (POLLIN | POLLPRI | POLLRDNORM | POLLRDBAND)) {
370 filetmp.f_data = (caddr_t)ap->a_vp->v_fifoinfo->fi_readsock;
371 if (filetmp.f_data)
372 revents |= soo_poll(&filetmp, ap->a_events, ap->a_p);
373 }
374 if (ap->a_events & (POLLOUT | POLLWRNORM | POLLWRBAND)) {
375 filetmp.f_data = (caddr_t)ap->a_vp->v_fifoinfo->fi_writesock;
376 if (filetmp.f_data)
377 revents |= soo_poll(&filetmp, ap->a_events, ap->a_p);
378 }
379
380 return (revents);
381 }
382
383 int
384 fifo_inactive(void *v)
385 {
386 struct vop_inactive_args /* {
387 struct vnode *a_vp;
388 struct proc *a_p;
389 } */ *ap = v;
390
391 VOP_UNLOCK(ap->a_vp, 0);
392 return (0);
393 }
394
395 /*
396 * This is a noop, simply returning what one has been given.
397 */
398 int
399 fifo_bmap(void *v)
400 {
401 struct vop_bmap_args /* {
402 struct vnode *a_vp;
403 daddr_t a_bn;
404 struct vnode **a_vpp;
405 daddr_t *a_bnp;
406 int *a_runp;
407 } */ *ap = v;
408
409 if (ap->a_vpp != NULL)
410 *ap->a_vpp = ap->a_vp;
411 if (ap->a_bnp != NULL)
412 *ap->a_bnp = ap->a_bn;
413 if (ap->a_runp != NULL)
414 *ap->a_runp = 0;
415 return (0);
416 }
417
418 /*
419 * Device close routine
420 */
421 /* ARGSUSED */
422 int
423 fifo_close(void *v)
424 {
425 struct vop_close_args /* {
426 struct vnode *a_vp;
427 int a_fflag;
428 struct ucred *a_cred;
429 struct proc *a_p;
430 } */ *ap = v;
431 struct vnode *vp;
432 struct fifoinfo *fip;
433
434 vp = ap->a_vp;
435 fip = vp->v_fifoinfo;
436 if (ap->a_fflag & FREAD) {
437 if (--fip->fi_readers == 0)
438 socantsendmore(fip->fi_writesock);
439 }
440 if (ap->a_fflag & FWRITE) {
441 if (--fip->fi_writers == 0)
442 socantrcvmore(fip->fi_readsock);
443 }
444 if (--fip->fi_opencount == 0) {
445 (void) soclose(fip->fi_readsock);
446 (void) soclose(fip->fi_writesock);
447 FREE(fip, M_VNODE);
448 vp->v_fifoinfo = NULL;
449 }
450 return (0);
451 }
452
453 /*
454 * Print out the contents of a fifo vnode.
455 */
456 int
457 fifo_print(void *v)
458 {
459 struct vop_print_args /* {
460 struct vnode *a_vp;
461 } */ *ap = v;
462
463 printf("tag VT_NON");
464 fifo_printinfo(ap->a_vp);
465 printf("\n");
466 return 0;
467 }
468
469 /*
470 * Print out internal contents of a fifo vnode.
471 */
472 void
473 fifo_printinfo(struct vnode *vp)
474 {
475 struct fifoinfo *fip;
476
477 fip = vp->v_fifoinfo;
478 printf(", fifo with %ld readers and %ld writers",
479 fip->fi_readers, fip->fi_writers);
480 }
481
482 /*
483 * Return POSIX pathconf information applicable to fifo's.
484 */
485 int
486 fifo_pathconf(void *v)
487 {
488 struct vop_pathconf_args /* {
489 struct vnode *a_vp;
490 int a_name;
491 register_t *a_retval;
492 } */ *ap = v;
493
494 switch (ap->a_name) {
495 case _PC_LINK_MAX:
496 *ap->a_retval = LINK_MAX;
497 return (0);
498 case _PC_PIPE_BUF:
499 *ap->a_retval = PIPE_BUF;
500 return (0);
501 case _PC_CHOWN_RESTRICTED:
502 *ap->a_retval = 1;
503 return (0);
504 case _PC_SYNC_IO:
505 *ap->a_retval = 1;
506 return (0);
507 default:
508 return (EINVAL);
509 }
510 /* NOTREACHED */
511 }
512