]> git.saurik.com Git - apple/xnu.git/blob - bsd/miscfs/fifofs/fifo_vnops.c
446c8216b5b3132bc1fae8259f012f44b75bd577
[apple/xnu.git] / bsd / miscfs / fifofs / fifo_vnops.c
1 /*
2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
7 *
8 * This file contains Original Code and/or Modifications of Original Code
9 * as defined in and that are subject to the Apple Public Source License
10 * Version 2.0 (the 'License'). You may not use this file except in
11 * compliance with the License. Please obtain a copy of the License at
12 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * file.
14 *
15 * The Original Code and all software distributed under the License are
16 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
17 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
18 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
20 * Please see the License for the specific language governing rights and
21 * limitations under the License.
22 *
23 * @APPLE_LICENSE_HEADER_END@
24 */
25 /* Copyright (c) 1995 NeXT Computer, Inc. All Rights Reserved */
26 /*
27 * Copyright (c) 1990, 1993, 1995
28 * The Regents of the University of California. All rights reserved.
29 *
30 * Redistribution and use in source and binary forms, with or without
31 * modification, are permitted provided that the following conditions
32 * are met:
33 * 1. Redistributions of source code must retain the above copyright
34 * notice, this list of conditions and the following disclaimer.
35 * 2. Redistributions in binary form must reproduce the above copyright
36 * notice, this list of conditions and the following disclaimer in the
37 * documentation and/or other materials provided with the distribution.
38 * 3. All advertising materials mentioning features or use of this software
39 * must display the following acknowledgement:
40 * This product includes software developed by the University of
41 * California, Berkeley and its contributors.
42 * 4. Neither the name of the University nor the names of its contributors
43 * may be used to endorse or promote products derived from this software
44 * without specific prior written permission.
45 *
46 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
47 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
48 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
49 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
50 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
51 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
52 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
53 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
54 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
55 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
56 * SUCH DAMAGE.
57 *
58 * @(#)fifo_vnops.c 8.4 (Berkeley) 8/10/94
59 */
60
61 #include <sys/param.h>
62 #include <sys/proc.h>
63 #include <sys/time.h>
64 #include <sys/namei.h>
65 #include <sys/vnode.h>
66 #include <sys/socket.h>
67 #include <sys/socketvar.h>
68 #include <sys/stat.h>
69 #include <sys/systm.h>
70 #include <sys/ioctl.h>
71 #include <sys/file.h>
72 #include <sys/errno.h>
73 #include <sys/malloc.h>
74 #include <miscfs/fifofs/fifo.h>
75 #include <vfs/vfs_support.h>
76
77 /*
78 * This structure is associated with the FIFO vnode and stores
79 * the state associated with the FIFO.
80 */
81 struct fifoinfo {
82 struct socket *fi_readsock;
83 struct socket *fi_writesock;
84 long fi_readers;
85 long fi_writers;
86 };
87
88 #define VOPFUNC int (*)(void *)
89
90 int (**fifo_vnodeop_p)(void *);
91 struct vnodeopv_entry_desc fifo_vnodeop_entries[] = {
92 { &vop_default_desc, (VOPFUNC)vn_default_error },
93 { &vop_lookup_desc, (VOPFUNC)fifo_lookup }, /* lookup */
94 { &vop_create_desc, (VOPFUNC)err_create }, /* create */
95 { &vop_mknod_desc, (VOPFUNC)err_mknod }, /* mknod */
96 { &vop_open_desc, (VOPFUNC)fifo_open }, /* open */
97 { &vop_close_desc, (VOPFUNC)fifo_close }, /* close */
98 { &vop_access_desc, (VOPFUNC)fifo_access }, /* access */
99 { &vop_getattr_desc, (VOPFUNC)fifo_getattr }, /* getattr */
100 { &vop_setattr_desc, (VOPFUNC)fifo_setattr }, /* setattr */
101 { &vop_read_desc, (VOPFUNC)fifo_read }, /* read */
102 { &vop_write_desc, (VOPFUNC)fifo_write }, /* write */
103 { &vop_lease_desc, (VOPFUNC)fifo_lease_check }, /* lease */
104 { &vop_ioctl_desc, (VOPFUNC)fifo_ioctl }, /* ioctl */
105 { &vop_select_desc, (VOPFUNC)fifo_select }, /* select */
106 { &vop_revoke_desc, (VOPFUNC)fifo_revoke }, /* revoke */
107 { &vop_mmap_desc, (VOPFUNC)err_mmap }, /* mmap */
108 { &vop_fsync_desc, (VOPFUNC)fifo_fsync }, /* fsync */
109 { &vop_seek_desc, (VOPFUNC)err_seek }, /* seek */
110 { &vop_remove_desc, (VOPFUNC)err_remove }, /* remove */
111 { &vop_link_desc, (VOPFUNC)err_link }, /* link */
112 { &vop_rename_desc, (VOPFUNC)err_rename }, /* rename */
113 { &vop_mkdir_desc, (VOPFUNC)err_mkdir }, /* mkdir */
114 { &vop_rmdir_desc, (VOPFUNC)err_rmdir }, /* rmdir */
115 { &vop_symlink_desc, (VOPFUNC)err_symlink }, /* symlink */
116 { &vop_readdir_desc, (VOPFUNC)err_readdir }, /* readdir */
117 { &vop_readlink_desc, (VOPFUNC)err_readlink }, /* readlink */
118 { &vop_abortop_desc, (VOPFUNC)err_abortop }, /* abortop */
119 { &vop_inactive_desc, (VOPFUNC)fifo_inactive }, /* inactive */
120 { &vop_reclaim_desc, (VOPFUNC)fifo_reclaim }, /* reclaim */
121 { &vop_lock_desc, (VOPFUNC)fifo_lock }, /* lock */
122 { &vop_unlock_desc, (VOPFUNC)fifo_unlock }, /* unlock */
123 { &vop_bmap_desc, (VOPFUNC)fifo_bmap }, /* bmap */
124 { &vop_strategy_desc, (VOPFUNC)err_strategy }, /* strategy */
125 { &vop_print_desc, (VOPFUNC)fifo_print }, /* print */
126 { &vop_islocked_desc, (VOPFUNC)fifo_islocked }, /* islocked */
127 { &vop_pathconf_desc, (VOPFUNC)fifo_pathconf }, /* pathconf */
128 { &vop_advlock_desc, (VOPFUNC)fifo_advlock }, /* advlock */
129 { &vop_blkatoff_desc, (VOPFUNC)err_blkatoff }, /* blkatoff */
130 { &vop_valloc_desc, (VOPFUNC)err_valloc }, /* valloc */
131 { &vop_vfree_desc, (VOPFUNC)err_vfree }, /* vfree */
132 { &vop_truncate_desc, (VOPFUNC)fifo_truncate }, /* truncate */
133 { &vop_update_desc, (VOPFUNC)fifo_update }, /* update */
134 { &vop_bwrite_desc, (VOPFUNC)fifo_bwrite }, /* bwrite */
135 { &vop_pagein_desc, (VOPFUNC)err_pagein }, /* Pagein */
136 { &vop_pageout_desc, (VOPFUNC)err_pageout }, /* Pageout */
137 { &vop_copyfile_desc, (VOPFUNC)err_copyfile }, /* Copyfile */
138 { &vop_blktooff_desc, (VOPFUNC)err_blktooff }, /* blktooff */
139 { &vop_offtoblk_desc, (VOPFUNC)err_offtoblk }, /* offtoblk */
140 { &vop_cmap_desc, (VOPFUNC)err_cmap }, /* cmap */
141 { (struct vnodeop_desc*)NULL, (int(*)())NULL }
142 };
143 struct vnodeopv_desc fifo_vnodeop_opv_desc =
144 { &fifo_vnodeop_p, fifo_vnodeop_entries };
145
146 /*
147 * Trivial lookup routine that always fails.
148 */
149 /* ARGSUSED */
150 fifo_lookup(ap)
151 struct vop_lookup_args /* {
152 struct vnode * a_dvp;
153 struct vnode ** a_vpp;
154 struct componentname * a_cnp;
155 } */ *ap;
156 {
157
158 *ap->a_vpp = NULL;
159 return (ENOTDIR);
160 }
161
162 /*
163 * Open called to set up a new instance of a fifo or
164 * to find an active instance of a fifo.
165 */
166 /* ARGSUSED */
167 fifo_open(ap)
168 struct vop_open_args /* {
169 struct vnode *a_vp;
170 int a_mode;
171 struct ucred *a_cred;
172 struct proc *a_p;
173 } */ *ap;
174 {
175 struct vnode *vp = ap->a_vp;
176 struct fifoinfo *fip;
177 struct proc *p = ap->a_p;
178 struct socket *rso, *wso;
179 int error;
180
181 if ((fip = vp->v_fifoinfo) == NULL) {
182 MALLOC_ZONE(fip, struct fifoinfo *,
183 sizeof(*fip), M_VNODE, M_WAITOK);
184 vp->v_fifoinfo = fip;
185 thread_funnel_switch(KERNEL_FUNNEL, NETWORK_FUNNEL);
186 if (error = socreate(AF_LOCAL, &rso, SOCK_STREAM, 0)) {
187 thread_funnel_switch(NETWORK_FUNNEL, KERNEL_FUNNEL);
188 _FREE_ZONE(fip, sizeof *fip, M_VNODE);
189 vp->v_fifoinfo = NULL;
190 return (error);
191 }
192 fip->fi_readsock = rso;
193 if (error = socreate(AF_LOCAL, &wso, SOCK_STREAM, 0)) {
194 (void)soclose(rso);
195 thread_funnel_switch(NETWORK_FUNNEL, KERNEL_FUNNEL);
196 _FREE_ZONE(fip, sizeof *fip, M_VNODE);
197 vp->v_fifoinfo = NULL;
198 return (error);
199 }
200 fip->fi_writesock = wso;
201 if (error = unp_connect2(wso, rso)) {
202 (void)soclose(wso);
203 (void)soclose(rso);
204 thread_funnel_switch(NETWORK_FUNNEL, KERNEL_FUNNEL);
205 _FREE_ZONE(fip, sizeof *fip, M_VNODE);
206 vp->v_fifoinfo = NULL;
207 return (error);
208 }
209 wso->so_state |= SS_CANTRCVMORE;
210 wso->so_snd.sb_lowat = PIPE_BUF;
211 rso->so_state |= SS_CANTSENDMORE;
212 thread_funnel_switch(NETWORK_FUNNEL, KERNEL_FUNNEL);
213 fip->fi_readers = fip->fi_writers = 0;
214 }
215 if (ap->a_mode & FREAD) {
216 fip->fi_readers++;
217 if (fip->fi_readers == 1) {
218 fip->fi_writesock->so_state &= ~SS_CANTSENDMORE;
219 if (fip->fi_writers > 0)
220 wakeup((caddr_t)&fip->fi_writers);
221 }
222 }
223 if (ap->a_mode & FWRITE) {
224 fip->fi_writers++;
225 if (fip->fi_writers == 1) {
226 fip->fi_readsock->so_state &= ~SS_CANTRCVMORE;
227 if (fip->fi_readers > 0)
228 wakeup((caddr_t)&fip->fi_readers);
229 }
230 }
231 if ((ap->a_mode & FREAD) && (ap->a_mode & O_NONBLOCK) == 0) {
232 if (fip->fi_writers == 0) {
233 VOP_UNLOCK(vp, 0, p);
234 error = tsleep((caddr_t)&fip->fi_readers,
235 PCATCH | PSOCK, "fifoor", 0);
236 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, p);
237 if (error)
238 goto bad;
239 if (fip->fi_readers == 1) {
240 if (fip->fi_writers > 0)
241 wakeup((caddr_t)&fip->fi_writers);
242 }
243 }
244 }
245 if (ap->a_mode & FWRITE) {
246 if (ap->a_mode & O_NONBLOCK) {
247 if (fip->fi_readers == 0) {
248 error = ENXIO;
249 goto bad;
250 }
251 } else {
252 if (fip->fi_readers == 0) {
253 VOP_UNLOCK(vp, 0, p);
254 error = tsleep((caddr_t)&fip->fi_writers,
255 PCATCH | PSOCK, "fifoow", 0);
256 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, p);
257 if (error)
258 goto bad;
259 if (fip->fi_writers == 1) {
260 if (fip->fi_readers > 0)
261 wakeup((caddr_t)&fip->fi_readers);
262 }
263 }
264 }
265 }
266 return (0);
267 bad:
268 if (error)
269 VOP_CLOSE(vp, ap->a_mode, ap->a_cred, p);
270 return (error);
271 }
272
273 /*
274 * Vnode op for read
275 */
276 /* ARGSUSED */
277 fifo_read(ap)
278 struct vop_read_args /* {
279 struct vnode *a_vp;
280 struct uio *a_uio;
281 int a_ioflag;
282 struct ucred *a_cred;
283 } */ *ap;
284 {
285 struct uio *uio = ap->a_uio;
286 struct socket *rso = ap->a_vp->v_fifoinfo->fi_readsock;
287 struct proc *p = uio->uio_procp;
288 int error, startresid;
289
290 #if DIAGNOSTIC
291 if (uio->uio_rw != UIO_READ)
292 panic("fifo_read mode");
293 #endif
294 if (uio->uio_resid == 0)
295 return (0);
296 if (ap->a_ioflag & IO_NDELAY)
297 rso->so_state |= SS_NBIO;
298 startresid = uio->uio_resid;
299 VOP_UNLOCK(ap->a_vp, 0, p);
300 thread_funnel_switch(KERNEL_FUNNEL, NETWORK_FUNNEL);
301 error = soreceive(rso, (struct sockaddr **)0, uio, (struct mbuf **)0,
302 (struct mbuf **)0, (int *)0);
303 thread_funnel_switch(NETWORK_FUNNEL, KERNEL_FUNNEL);
304 vn_lock(ap->a_vp, LK_EXCLUSIVE | LK_RETRY, p);
305 /*
306 * Clear EOF indication after first such return.
307 */
308 if (uio->uio_resid == startresid)
309 rso->so_state &= ~SS_CANTRCVMORE;
310 if (ap->a_ioflag & IO_NDELAY)
311 rso->so_state &= ~SS_NBIO;
312 return (error);
313 }
314
315 /*
316 * Vnode op for write
317 */
318 /* ARGSUSED */
319 fifo_write(ap)
320 struct vop_write_args /* {
321 struct vnode *a_vp;
322 struct uio *a_uio;
323 int a_ioflag;
324 struct ucred *a_cred;
325 } */ *ap;
326 {
327 struct socket *wso = ap->a_vp->v_fifoinfo->fi_writesock;
328 struct proc *p = ap->a_uio->uio_procp;
329 int error;
330
331 #if DIAGNOSTIC
332 if (ap->a_uio->uio_rw != UIO_WRITE)
333 panic("fifo_write mode");
334 #endif
335 if (ap->a_ioflag & IO_NDELAY)
336 wso->so_state |= SS_NBIO;
337 VOP_UNLOCK(ap->a_vp, 0, p);
338 thread_funnel_switch(KERNEL_FUNNEL, NETWORK_FUNNEL);
339 error = sosend(wso, (struct sockaddr *)0, ap->a_uio, 0, (struct mbuf *)0, 0);
340 thread_funnel_switch(NETWORK_FUNNEL, KERNEL_FUNNEL);
341 vn_lock(ap->a_vp, LK_EXCLUSIVE | LK_RETRY, p);
342 if (ap->a_ioflag & IO_NDELAY)
343 wso->so_state &= ~SS_NBIO;
344 return (error);
345 }
346
347 /*
348 * Device ioctl operation.
349 */
350 /* ARGSUSED */
351 fifo_ioctl(ap)
352 struct vop_ioctl_args /* {
353 struct vnode *a_vp;
354 int a_command;
355 caddr_t a_data;
356 int a_fflag;
357 struct ucred *a_cred;
358 struct proc *a_p;
359 } */ *ap;
360 {
361 struct file filetmp;
362 int error;
363
364 if (ap->a_command == FIONBIO)
365 return (0);
366 if (ap->a_fflag & FREAD) {
367 filetmp.f_data = (caddr_t)ap->a_vp->v_fifoinfo->fi_readsock;
368 error = soo_ioctl(&filetmp, ap->a_command, ap->a_data, ap->a_p);
369 if (error)
370 return (error);
371 }
372 if (ap->a_fflag & FWRITE) {
373 filetmp.f_data = (caddr_t)ap->a_vp->v_fifoinfo->fi_writesock;
374 error = soo_ioctl(&filetmp, ap->a_command, ap->a_data, ap->a_p);
375 if (error)
376 return (error);
377 }
378 return (0);
379 }
380
381 /* ARGSUSED */
382 fifo_select(ap)
383 struct vop_select_args /* {
384 struct vnode *a_vp;
385 int a_which;
386 int a_fflags;
387 struct ucred *a_cred;
388 void * a_wql;
389 struct proc *a_p;
390 } */ *ap;
391 {
392 struct file filetmp;
393 int ready;
394
395 if (ap->a_which & FREAD) {
396 filetmp.f_data = (caddr_t)ap->a_vp->v_fifoinfo->fi_readsock;
397 ready = soo_select(&filetmp, ap->a_which, ap->a_wql, ap->a_p);
398 if (ready)
399 return (ready);
400 }
401 if (ap->a_which & FWRITE) {
402 filetmp.f_data = (caddr_t)ap->a_vp->v_fifoinfo->fi_writesock;
403 ready = soo_select(&filetmp, ap->a_which, ap->a_wql, ap->a_p);
404 if (ready)
405 return (ready);
406 }
407 return (0);
408 }
409
410 int
411 fifo_inactive(ap)
412 struct vop_inactive_args /* {
413 struct vnode *a_vp;
414 struct proc *a_p;
415 } */ *ap;
416 {
417
418 VOP_UNLOCK(ap->a_vp, 0, ap->a_p);
419 return (0);
420 }
421
422 /*
423 * This is a noop, simply returning what one has been given.
424 */
425 fifo_bmap(ap)
426 struct vop_bmap_args /* {
427 struct vnode *a_vp;
428 daddr_t a_bn;
429 struct vnode **a_vpp;
430 daddr_t *a_bnp;
431 int *a_runp;
432 } */ *ap;
433 {
434
435 if (ap->a_vpp != NULL)
436 *ap->a_vpp = ap->a_vp;
437 if (ap->a_bnp != NULL)
438 *ap->a_bnp = ap->a_bn;
439 if (ap->a_runp != NULL)
440 *ap->a_runp = 0;
441 return (0);
442 }
443
444 /*
445 * Device close routine
446 */
447 /* ARGSUSED */
448 fifo_close(ap)
449 struct vop_close_args /* {
450 struct vnode *a_vp;
451 int a_fflag;
452 struct ucred *a_cred;
453 struct proc *a_p;
454 } */ *ap;
455 {
456 register struct vnode *vp = ap->a_vp;
457 register struct fifoinfo *fip = vp->v_fifoinfo;
458 int error1, error2;
459
460 if (ap->a_fflag & FREAD) {
461 fip->fi_readers--;
462 if (fip->fi_readers == 0){
463 thread_funnel_switch(KERNEL_FUNNEL, NETWORK_FUNNEL);
464 socantsendmore(fip->fi_writesock);
465 thread_funnel_switch(NETWORK_FUNNEL, KERNEL_FUNNEL);
466 }
467 }
468 if (ap->a_fflag & FWRITE) {
469 fip->fi_writers--;
470 if (fip->fi_writers == 0) {
471 thread_funnel_switch(KERNEL_FUNNEL, NETWORK_FUNNEL);
472 socantrcvmore(fip->fi_readsock);
473 thread_funnel_switch(NETWORK_FUNNEL, KERNEL_FUNNEL);
474 }
475 }
476 if (vp->v_usecount > 1)
477 return (0);
478 thread_funnel_switch(KERNEL_FUNNEL, NETWORK_FUNNEL);
479 error1 = soclose(fip->fi_readsock);
480 error2 = soclose(fip->fi_writesock);
481 thread_funnel_switch(NETWORK_FUNNEL, KERNEL_FUNNEL);
482 FREE_ZONE(fip, sizeof *fip, M_VNODE);
483 vp->v_fifoinfo = NULL;
484 if (error1)
485 return (error1);
486 return (error2);
487 }
488
489 /*
490 * Print out the contents of a fifo vnode.
491 */
492 fifo_print(ap)
493 struct vop_print_args /* {
494 struct vnode *a_vp;
495 } */ *ap;
496 {
497
498 printf("tag VT_NON");
499 fifo_printinfo(ap->a_vp);
500 printf("\n");
501 }
502
503 /*
504 * Print out internal contents of a fifo vnode.
505 */
506 fifo_printinfo(vp)
507 struct vnode *vp;
508 {
509 register struct fifoinfo *fip = vp->v_fifoinfo;
510
511 printf(", fifo with %d readers and %d writers",
512 fip->fi_readers, fip->fi_writers);
513 }
514
515 /*
516 * Return POSIX pathconf information applicable to fifo's.
517 */
518 fifo_pathconf(ap)
519 struct vop_pathconf_args /* {
520 struct vnode *a_vp;
521 int a_name;
522 int *a_retval;
523 } */ *ap;
524 {
525
526 switch (ap->a_name) {
527 case _PC_LINK_MAX:
528 *ap->a_retval = LINK_MAX;
529 return (0);
530 case _PC_PIPE_BUF:
531 *ap->a_retval = PIPE_BUF;
532 return (0);
533 case _PC_CHOWN_RESTRICTED:
534 *ap->a_retval = 1;
535 return (0);
536 default:
537 return (EINVAL);
538 }
539 /* NOTREACHED */
540 }
541
542 /*
543 * Fifo failed operation
544 */
545 fifo_ebadf()
546 {
547
548 return (EBADF);
549 }
550
551 /*
552 * Fifo advisory byte-level locks.
553 */
554 /* ARGSUSED */
555 fifo_advlock(ap)
556 struct vop_advlock_args /* {
557 struct vnode *a_vp;
558 caddr_t a_id;
559 int a_op;
560 struct flock *a_fl;
561 int a_flags;
562 } */ *ap;
563 {
564
565 return (EOPNOTSUPP);
566 }
567