fifo_vnops.c revision 1.32 1 /* $NetBSD: fifo_vnops.c,v 1.32 2001/11/10 13:33:41 lukem 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.32 2001/11/10 13:33:41 lukem 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_readers;
68 long fi_writers;
69 };
70
71 int (**fifo_vnodeop_p) __P((void *));
72 const struct vnodeopv_entry_desc fifo_vnodeop_entries[] = {
73 { &vop_default_desc, vn_default_error },
74 { &vop_lookup_desc, fifo_lookup }, /* lookup */
75 { &vop_create_desc, fifo_create }, /* create */
76 { &vop_mknod_desc, fifo_mknod }, /* mknod */
77 { &vop_open_desc, fifo_open }, /* open */
78 { &vop_close_desc, fifo_close }, /* close */
79 { &vop_access_desc, fifo_access }, /* access */
80 { &vop_getattr_desc, fifo_getattr }, /* getattr */
81 { &vop_setattr_desc, fifo_setattr }, /* setattr */
82 { &vop_read_desc, fifo_read }, /* read */
83 { &vop_write_desc, fifo_write }, /* write */
84 { &vop_lease_desc, fifo_lease_check }, /* lease */
85 { &vop_ioctl_desc, fifo_ioctl }, /* ioctl */
86 { &vop_poll_desc, fifo_poll }, /* poll */
87 { &vop_revoke_desc, fifo_revoke }, /* revoke */
88 { &vop_mmap_desc, fifo_mmap }, /* mmap */
89 { &vop_fsync_desc, fifo_fsync }, /* fsync */
90 { &vop_seek_desc, fifo_seek }, /* seek */
91 { &vop_remove_desc, fifo_remove }, /* remove */
92 { &vop_link_desc, fifo_link }, /* link */
93 { &vop_rename_desc, fifo_rename }, /* rename */
94 { &vop_mkdir_desc, fifo_mkdir }, /* mkdir */
95 { &vop_rmdir_desc, fifo_rmdir }, /* rmdir */
96 { &vop_symlink_desc, fifo_symlink }, /* symlink */
97 { &vop_readdir_desc, fifo_readdir }, /* readdir */
98 { &vop_readlink_desc, fifo_readlink }, /* readlink */
99 { &vop_abortop_desc, fifo_abortop }, /* abortop */
100 { &vop_inactive_desc, fifo_inactive }, /* inactive */
101 { &vop_reclaim_desc, fifo_reclaim }, /* reclaim */
102 { &vop_lock_desc, fifo_lock }, /* lock */
103 { &vop_unlock_desc, fifo_unlock }, /* unlock */
104 { &vop_bmap_desc, fifo_bmap }, /* bmap */
105 { &vop_strategy_desc, fifo_strategy }, /* strategy */
106 { &vop_print_desc, fifo_print }, /* print */
107 { &vop_islocked_desc, fifo_islocked }, /* islocked */
108 { &vop_pathconf_desc, fifo_pathconf }, /* pathconf */
109 { &vop_advlock_desc, fifo_advlock }, /* advlock */
110 { &vop_blkatoff_desc, fifo_blkatoff }, /* blkatoff */
111 { &vop_valloc_desc, fifo_valloc }, /* valloc */
112 { &vop_vfree_desc, fifo_vfree }, /* vfree */
113 { &vop_truncate_desc, fifo_truncate }, /* truncate */
114 { &vop_update_desc, fifo_update }, /* update */
115 { &vop_bwrite_desc, fifo_bwrite }, /* bwrite */
116 { &vop_putpages_desc, fifo_putpages }, /* putpages */
117 { (struct vnodeop_desc*)NULL, (int(*) __P((void *)))NULL }
118 };
119 const struct vnodeopv_desc fifo_vnodeop_opv_desc =
120 { &fifo_vnodeop_p, fifo_vnodeop_entries };
121
122 /*
123 * Trivial lookup routine that always fails.
124 */
125 /* ARGSUSED */
126 int
127 fifo_lookup(void *v)
128 {
129 struct vop_lookup_args /* {
130 struct vnode *a_dvp;
131 struct vnode **a_vpp;
132 struct componentname *a_cnp;
133 } */ *ap = v;
134
135 *ap->a_vpp = NULL;
136 return (ENOTDIR);
137 }
138
139 /*
140 * Open called to set up a new instance of a fifo or
141 * to find an active instance of a fifo.
142 */
143 /* ARGSUSED */
144 int
145 fifo_open(void *v)
146 {
147 struct vop_open_args /* {
148 struct vnode *a_vp;
149 int a_mode;
150 struct ucred *a_cred;
151 struct proc *a_p;
152 } */ *ap = v;
153 struct vnode *vp;
154 struct fifoinfo *fip;
155 struct proc *p;
156 struct socket *rso, *wso;
157 int error;
158 static const char openstr[] = "fifo";
159
160 vp = ap->a_vp;
161 p = ap->a_p;
162
163 if ((fip = vp->v_fifoinfo) == NULL) {
164 MALLOC(fip, struct fifoinfo *, sizeof(*fip), M_VNODE, M_WAITOK);
165 vp->v_fifoinfo = fip;
166 if ((error = socreate(AF_LOCAL, &rso, SOCK_STREAM, 0)) != 0) {
167 free(fip, M_VNODE);
168 vp->v_fifoinfo = NULL;
169 return (error);
170 }
171 fip->fi_readsock = rso;
172 if ((error = socreate(AF_LOCAL, &wso, SOCK_STREAM, 0)) != 0) {
173 (void)soclose(rso);
174 free(fip, M_VNODE);
175 vp->v_fifoinfo = NULL;
176 return (error);
177 }
178 fip->fi_writesock = wso;
179 if ((error = unp_connect2(wso, rso)) != 0) {
180 (void)soclose(wso);
181 (void)soclose(rso);
182 free(fip, M_VNODE);
183 vp->v_fifoinfo = NULL;
184 return (error);
185 }
186 fip->fi_readers = fip->fi_writers = 0;
187 wso->so_state |= SS_CANTRCVMORE;
188 rso->so_state |= SS_CANTSENDMORE;
189 }
190 if (ap->a_mode & FREAD) {
191 if (fip->fi_readers++ == 0) {
192 fip->fi_writesock->so_state &= ~SS_CANTSENDMORE;
193 if (fip->fi_writers > 0)
194 wakeup((caddr_t)&fip->fi_writers);
195 }
196 }
197 if (ap->a_mode & FWRITE) {
198 if (fip->fi_writers++ == 0) {
199 fip->fi_readsock->so_state &= ~SS_CANTRCVMORE;
200 if (fip->fi_readers > 0)
201 wakeup((caddr_t)&fip->fi_readers);
202 }
203 }
204 if (ap->a_mode & FREAD) {
205 if (ap->a_mode & O_NONBLOCK) {
206 } else {
207 while (fip->fi_writers == 0) {
208 VOP_UNLOCK(vp, 0);
209 error = tsleep((caddr_t)&fip->fi_readers,
210 PCATCH | PSOCK, openstr, 0);
211 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
212 if (error)
213 goto bad;
214 }
215 }
216 }
217 if (ap->a_mode & FWRITE) {
218 if (ap->a_mode & O_NONBLOCK) {
219 if (fip->fi_readers == 0) {
220 error = ENXIO;
221 goto bad;
222 }
223 } else {
224 while (fip->fi_readers == 0) {
225 VOP_UNLOCK(vp, 0);
226 error = tsleep((caddr_t)&fip->fi_writers,
227 PCATCH | PSOCK, openstr, 0);
228 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
229 if (error)
230 goto bad;
231 }
232 }
233 }
234 return (0);
235 bad:
236 VOP_CLOSE(vp, ap->a_mode, ap->a_cred, p);
237 return (error);
238 }
239
240 /*
241 * Vnode op for read
242 */
243 /* ARGSUSED */
244 int
245 fifo_read(void *v)
246 {
247 struct vop_read_args /* {
248 struct vnode *a_vp;
249 struct uio *a_uio;
250 int a_ioflag;
251 struct ucred *a_cred;
252 } */ *ap = v;
253 struct uio *uio;
254 struct socket *rso;
255 int error, startresid;
256
257 uio = ap->a_uio;
258 rso = ap->a_vp->v_fifoinfo->fi_readsock;
259 #ifdef DIAGNOSTIC
260 if (uio->uio_rw != UIO_READ)
261 panic("fifo_read mode");
262 #endif
263 if (uio->uio_resid == 0)
264 return (0);
265 if (ap->a_ioflag & IO_NDELAY)
266 rso->so_state |= SS_NBIO;
267 startresid = uio->uio_resid;
268 VOP_UNLOCK(ap->a_vp, 0);
269 error = (*rso->so_receive)(rso, (struct mbuf **)0, uio,
270 (struct mbuf **)0, (struct mbuf **)0, (int *)0);
271 vn_lock(ap->a_vp, LK_EXCLUSIVE | LK_RETRY);
272 /*
273 * Clear EOF indication after first such return.
274 */
275 if (uio->uio_resid == startresid)
276 rso->so_state &= ~SS_CANTRCVMORE;
277 if (ap->a_ioflag & IO_NDELAY) {
278 rso->so_state &= ~SS_NBIO;
279 if (error == EWOULDBLOCK &&
280 ap->a_vp->v_fifoinfo->fi_writers == 0)
281 error = 0;
282 }
283 return (error);
284 }
285
286 /*
287 * Vnode op for write
288 */
289 /* ARGSUSED */
290 int
291 fifo_write(void *v)
292 {
293 struct vop_write_args /* {
294 struct vnode *a_vp;
295 struct uio *a_uio;
296 int a_ioflag;
297 struct ucred *a_cred;
298 } */ *ap = v;
299 struct socket *wso;
300 int error;
301
302 wso = ap->a_vp->v_fifoinfo->fi_writesock;
303 #ifdef DIAGNOSTIC
304 if (ap->a_uio->uio_rw != UIO_WRITE)
305 panic("fifo_write mode");
306 #endif
307 if (ap->a_ioflag & IO_NDELAY)
308 wso->so_state |= SS_NBIO;
309 VOP_UNLOCK(ap->a_vp, 0);
310 error = (*wso->so_send)(wso, (struct mbuf *)0, ap->a_uio, 0,
311 (struct mbuf *)0, 0);
312 vn_lock(ap->a_vp, LK_EXCLUSIVE | LK_RETRY);
313 if (ap->a_ioflag & IO_NDELAY)
314 wso->so_state &= ~SS_NBIO;
315 return (error);
316 }
317
318 /*
319 * Device ioctl operation.
320 */
321 /* ARGSUSED */
322 int
323 fifo_ioctl(void *v)
324 {
325 struct vop_ioctl_args /* {
326 struct vnode *a_vp;
327 u_long a_command;
328 caddr_t a_data;
329 int a_fflag;
330 struct ucred *a_cred;
331 struct proc *a_p;
332 } */ *ap = v;
333 struct file filetmp;
334 int error;
335
336 if (ap->a_command == FIONBIO)
337 return (0);
338 if (ap->a_fflag & FREAD) {
339 filetmp.f_data = (caddr_t)ap->a_vp->v_fifoinfo->fi_readsock;
340 error = soo_ioctl(&filetmp, ap->a_command, ap->a_data, ap->a_p);
341 if (error)
342 return (error);
343 }
344 if (ap->a_fflag & FWRITE) {
345 filetmp.f_data = (caddr_t)ap->a_vp->v_fifoinfo->fi_writesock;
346 error = soo_ioctl(&filetmp, ap->a_command, ap->a_data, ap->a_p);
347 if (error)
348 return (error);
349 }
350 return (0);
351 }
352
353 /* ARGSUSED */
354 int
355 fifo_poll(void *v)
356 {
357 struct vop_poll_args /* {
358 struct vnode *a_vp;
359 int a_events;
360 struct proc *a_p;
361 } */ *ap = v;
362 struct file filetmp;
363 int revents;
364
365 revents = 0;
366 if (ap->a_events & (POLLIN | POLLPRI | POLLRDNORM | POLLRDBAND)) {
367 filetmp.f_data = (caddr_t)ap->a_vp->v_fifoinfo->fi_readsock;
368 if (filetmp.f_data)
369 revents |= soo_poll(&filetmp, ap->a_events, ap->a_p);
370 }
371 if (ap->a_events & (POLLOUT | POLLWRNORM | POLLWRBAND)) {
372 filetmp.f_data = (caddr_t)ap->a_vp->v_fifoinfo->fi_writesock;
373 if (filetmp.f_data)
374 revents |= soo_poll(&filetmp, ap->a_events, ap->a_p);
375 }
376
377 return (revents);
378 }
379
380 int
381 fifo_inactive(void *v)
382 {
383 struct vop_inactive_args /* {
384 struct vnode *a_vp;
385 struct proc *a_p;
386 } */ *ap = v;
387
388 VOP_UNLOCK(ap->a_vp, 0);
389 return (0);
390 }
391
392 /*
393 * This is a noop, simply returning what one has been given.
394 */
395 int
396 fifo_bmap(void *v)
397 {
398 struct vop_bmap_args /* {
399 struct vnode *a_vp;
400 daddr_t a_bn;
401 struct vnode **a_vpp;
402 daddr_t *a_bnp;
403 int *a_runp;
404 } */ *ap = v;
405
406 if (ap->a_vpp != NULL)
407 *ap->a_vpp = ap->a_vp;
408 if (ap->a_bnp != NULL)
409 *ap->a_bnp = ap->a_bn;
410 if (ap->a_runp != NULL)
411 *ap->a_runp = 0;
412 return (0);
413 }
414
415 /*
416 * Device close routine
417 */
418 /* ARGSUSED */
419 int
420 fifo_close(void *v)
421 {
422 struct vop_close_args /* {
423 struct vnode *a_vp;
424 int a_fflag;
425 struct ucred *a_cred;
426 struct proc *a_p;
427 } */ *ap = v;
428 struct vnode *vp;
429 struct fifoinfo *fip;
430 int error1, error2;
431
432 vp = ap->a_vp;
433 fip = vp->v_fifoinfo;
434 if (ap->a_fflag & FREAD) {
435 if (--fip->fi_readers == 0)
436 socantsendmore(fip->fi_writesock);
437 }
438 if (ap->a_fflag & FWRITE) {
439 if (--fip->fi_writers == 0)
440 socantrcvmore(fip->fi_readsock);
441 }
442 if (vp->v_usecount > 1)
443 return (0);
444 error1 = soclose(fip->fi_readsock);
445 error2 = soclose(fip->fi_writesock);
446 FREE(fip, M_VNODE);
447 vp->v_fifoinfo = NULL;
448 if (error1)
449 return (error1);
450 return (error2);
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
513 /*
514 * Dummy putpages routine.
515 */
516
517 int
518 fifo_putpages(void *v)
519 {
520 struct vop_putpages_args /* {
521 struct vnode *a_vp;
522 voff_t a_offlo;
523 voff_t a_offhi;
524 int a_flags;
525 } */ *ap = v;
526 struct vnode *vp = ap->a_vp;
527 struct uvm_object *uobj = &vp->v_uobj;
528
529 KASSERT(uobj->uo_npages == 0);
530 simple_unlock(&uobj->vmobjlock);
531 return (0);
532 }
533