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