2 * Copyright (c) 2000-2002 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
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
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.
23 * @APPLE_LICENSE_HEADER_END@
25 /* Copyright (c) 1995 NeXT Computer, Inc. All Rights Reserved */
27 * Copyright (c) 1990, 1993, 1995
28 * The Regents of the University of California. All rights reserved.
30 * Redistribution and use in source and binary forms, with or without
31 * modification, are permitted provided that the following conditions
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.
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
58 * @(#)fifo_vnops.c 8.4 (Berkeley) 8/10/94
61 #include <sys/param.h>
64 #include <sys/namei.h>
65 #include <sys/vnode.h>
66 #include <sys/socket.h>
67 #include <sys/socketvar.h>
69 #include <sys/systm.h>
70 #include <sys/ioctl.h>
72 #include <sys/errno.h>
73 #include <sys/malloc.h>
74 #include <miscfs/fifofs/fifo.h>
75 #include <vfs/vfs_support.h>
78 * This structure is associated with the FIFO vnode and stores
79 * the state associated with the FIFO.
82 struct socket
*fi_readsock
;
83 struct socket
*fi_writesock
;
88 #define VOPFUNC int (*)(void *)
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
}
143 struct vnodeopv_desc fifo_vnodeop_opv_desc
=
144 { &fifo_vnodeop_p
, fifo_vnodeop_entries
};
147 * Trivial lookup routine that always fails.
151 struct vop_lookup_args
/* {
152 struct vnode * a_dvp;
153 struct vnode ** a_vpp;
154 struct componentname * a_cnp;
163 * Open called to set up a new instance of a fifo or
164 * to find an active instance of a fifo.
168 struct vop_open_args
/* {
171 struct ucred *a_cred;
175 struct vnode
*vp
= ap
->a_vp
;
176 struct fifoinfo
*fip
;
177 struct proc
*p
= ap
->a_p
;
178 struct socket
*rso
, *wso
;
181 if ((fip
= vp
->v_fifoinfo
) == NULL
) {
182 MALLOC(fip
, struct fifoinfo
*,
183 sizeof(*fip
), M_TEMP
, 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 vp
->v_fifoinfo
= NULL
;
192 fip
->fi_readsock
= rso
;
193 if (error
= socreate(AF_LOCAL
, &wso
, SOCK_STREAM
, 0)) {
195 thread_funnel_switch(NETWORK_FUNNEL
, KERNEL_FUNNEL
);
196 vp
->v_fifoinfo
= NULL
;
200 fip
->fi_writesock
= wso
;
201 if (error
= unp_connect2(wso
, rso
)) {
204 thread_funnel_switch(NETWORK_FUNNEL
, KERNEL_FUNNEL
);
205 vp
->v_fifoinfo
= NULL
;
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;
215 if (ap
->a_mode
& FREAD
) {
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
);
223 if (ap
->a_mode
& FWRITE
) {
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
);
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
);
239 if (fip
->fi_readers
== 1) {
240 if (fip
->fi_writers
> 0)
241 wakeup((caddr_t
)&fip
->fi_writers
);
245 if (ap
->a_mode
& FWRITE
) {
246 if (ap
->a_mode
& O_NONBLOCK
) {
247 if (fip
->fi_readers
== 0) {
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
);
259 if (fip
->fi_writers
== 1) {
260 if (fip
->fi_readers
> 0)
261 wakeup((caddr_t
)&fip
->fi_readers
);
269 VOP_CLOSE(vp
, ap
->a_mode
, ap
->a_cred
, p
);
278 struct vop_read_args
/* {
282 struct ucred *a_cred;
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
;
291 if (uio
->uio_rw
!= UIO_READ
)
292 panic("fifo_read mode");
294 if (uio
->uio_resid
== 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
);
306 * Clear EOF indication after first such return.
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
;
320 struct vop_write_args
/* {
324 struct ucred *a_cred;
327 struct socket
*wso
= ap
->a_vp
->v_fifoinfo
->fi_writesock
;
328 struct proc
*p
= ap
->a_uio
->uio_procp
;
332 if (ap
->a_uio
->uio_rw
!= UIO_WRITE
)
333 panic("fifo_write mode");
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
;
348 * Device ioctl operation.
352 struct vop_ioctl_args
/* {
357 struct ucred *a_cred;
364 if (ap
->a_command
== FIONBIO
)
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
);
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
);
383 struct vop_select_args
/* {
387 struct ucred *a_cred;
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
);
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
);
412 struct vop_inactive_args
/* {
418 VOP_UNLOCK(ap
->a_vp
, 0, ap
->a_p
);
423 * This is a noop, simply returning what one has been given.
426 struct vop_bmap_args
/* {
429 struct vnode **a_vpp;
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
)
445 * Device close routine
449 struct vop_close_args
/* {
452 struct ucred *a_cred;
456 register struct vnode
*vp
= ap
->a_vp
;
457 register struct fifoinfo
*fip
= vp
->v_fifoinfo
;
460 if (ap
->a_fflag
& FREAD
) {
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
);
468 if (ap
->a_fflag
& FWRITE
) {
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
);
476 if (vp
->v_usecount
> 1)
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 vp
->v_fifoinfo
= NULL
;
490 * Print out the contents of a fifo vnode.
493 struct vop_print_args
/* {
498 printf("tag VT_NON");
499 fifo_printinfo(ap
->a_vp
);
504 * Print out internal contents of a fifo vnode.
509 register struct fifoinfo
*fip
= vp
->v_fifoinfo
;
511 printf(", fifo with %d readers and %d writers",
512 fip
->fi_readers
, fip
->fi_writers
);
516 * Return POSIX pathconf information applicable to fifo's.
519 struct vop_pathconf_args
/* {
526 switch (ap
->a_name
) {
528 *ap
->a_retval
= LINK_MAX
;
531 *ap
->a_retval
= PIPE_BUF
;
533 case _PC_CHOWN_RESTRICTED
:
543 * Fifo failed operation
552 * Fifo advisory byte-level locks.
556 struct vop_advlock_args
/* {