2 * Copyright (c) 2000-2004 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
20 * @APPLE_LICENSE_HEADER_END@
22 /* Copyright (c) 1995 NeXT Computer, Inc. All Rights Reserved */
24 * Copyright (c) 1990, 1993, 1995
25 * The Regents of the University of California. All rights reserved.
27 * Redistribution and use in source and binary forms, with or without
28 * modification, are permitted provided that the following conditions
30 * 1. Redistributions of source code must retain the above copyright
31 * notice, this list of conditions and the following disclaimer.
32 * 2. Redistributions in binary form must reproduce the above copyright
33 * notice, this list of conditions and the following disclaimer in the
34 * documentation and/or other materials provided with the distribution.
35 * 3. All advertising materials mentioning features or use of this software
36 * must display the following acknowledgement:
37 * This product includes software developed by the University of
38 * California, Berkeley and its contributors.
39 * 4. Neither the name of the University nor the names of its contributors
40 * may be used to endorse or promote products derived from this software
41 * without specific prior written permission.
43 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
44 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
45 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
46 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
47 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
48 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
49 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
50 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
51 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
52 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
55 * @(#)fifo_vnops.c 8.4 (Berkeley) 8/10/94
58 #include <sys/param.h>
61 #include <sys/namei.h>
62 #include <sys/vnode_internal.h>
63 #include <sys/socket.h>
64 #include <sys/socketvar.h>
66 #include <sys/systm.h>
67 #include <sys/ioctl.h>
68 #include <sys/file_internal.h>
69 #include <sys/errno.h>
70 #include <sys/malloc.h>
71 #include <miscfs/fifofs/fifo.h>
72 #include <vfs/vfs_support.h>
74 #define VOPFUNC int (*)(void *)
76 extern int soo_ioctl(struct fileproc
*fp
, u_long cmd
, caddr_t data
, struct proc
*p
);
77 extern int soo_select(struct fileproc
*fp
, int which
, void * wql
, struct proc
*p
);
79 int (**fifo_vnodeop_p
)(void *);
80 struct vnodeopv_entry_desc fifo_vnodeop_entries
[] = {
81 { &vnop_default_desc
, (VOPFUNC
)vn_default_error
},
82 { &vnop_lookup_desc
, (VOPFUNC
)fifo_lookup
}, /* lookup */
83 { &vnop_create_desc
, (VOPFUNC
)err_create
}, /* create */
84 { &vnop_mknod_desc
, (VOPFUNC
)err_mknod
}, /* mknod */
85 { &vnop_open_desc
, (VOPFUNC
)fifo_open
}, /* open */
86 { &vnop_close_desc
, (VOPFUNC
)fifo_close
}, /* close */
87 { &vnop_access_desc
, (VOPFUNC
)fifo_access
}, /* access */
88 { &vnop_getattr_desc
, (VOPFUNC
)fifo_getattr
}, /* getattr */
89 { &vnop_setattr_desc
, (VOPFUNC
)fifo_setattr
}, /* setattr */
90 { &vnop_read_desc
, (VOPFUNC
)fifo_read
}, /* read */
91 { &vnop_write_desc
, (VOPFUNC
)fifo_write
}, /* write */
92 { &vnop_ioctl_desc
, (VOPFUNC
)fifo_ioctl
}, /* ioctl */
93 { &vnop_select_desc
, (VOPFUNC
)fifo_select
}, /* select */
94 { &vnop_revoke_desc
, (VOPFUNC
)fifo_revoke
}, /* revoke */
95 { &vnop_mmap_desc
, (VOPFUNC
)err_mmap
}, /* mmap */
96 { &vnop_fsync_desc
, (VOPFUNC
)fifo_fsync
}, /* fsync */
97 { &vnop_remove_desc
, (VOPFUNC
)err_remove
}, /* remove */
98 { &vnop_link_desc
, (VOPFUNC
)err_link
}, /* link */
99 { &vnop_rename_desc
, (VOPFUNC
)err_rename
}, /* rename */
100 { &vnop_mkdir_desc
, (VOPFUNC
)err_mkdir
}, /* mkdir */
101 { &vnop_rmdir_desc
, (VOPFUNC
)err_rmdir
}, /* rmdir */
102 { &vnop_symlink_desc
, (VOPFUNC
)err_symlink
}, /* symlink */
103 { &vnop_readdir_desc
, (VOPFUNC
)err_readdir
}, /* readdir */
104 { &vnop_readlink_desc
, (VOPFUNC
)err_readlink
}, /* readlink */
105 { &vnop_inactive_desc
, (VOPFUNC
)fifo_inactive
}, /* inactive */
106 { &vnop_reclaim_desc
, (VOPFUNC
)fifo_reclaim
}, /* reclaim */
107 { &vnop_strategy_desc
, (VOPFUNC
)err_strategy
}, /* strategy */
108 { &vnop_pathconf_desc
, (VOPFUNC
)fifo_pathconf
}, /* pathconf */
109 { &vnop_advlock_desc
, (VOPFUNC
)fifo_advlock
}, /* advlock */
110 { &vnop_bwrite_desc
, (VOPFUNC
)fifo_bwrite
}, /* bwrite */
111 { &vnop_pagein_desc
, (VOPFUNC
)err_pagein
}, /* Pagein */
112 { &vnop_pageout_desc
, (VOPFUNC
)err_pageout
}, /* Pageout */
113 { &vnop_copyfile_desc
, (VOPFUNC
)err_copyfile
}, /* Copyfile */
114 { &vnop_blktooff_desc
, (VOPFUNC
)err_blktooff
}, /* blktooff */
115 { &vnop_offtoblk_desc
, (VOPFUNC
)err_offtoblk
}, /* offtoblk */
116 { &vnop_blockmap_desc
, (VOPFUNC
)err_blockmap
}, /* blockmap */
117 { (struct vnodeop_desc
*)NULL
, (int(*)())NULL
}
119 struct vnodeopv_desc fifo_vnodeop_opv_desc
=
120 { &fifo_vnodeop_p
, fifo_vnodeop_entries
};
123 * Trivial lookup routine that always fails.
128 struct vnop_lookup_args
/* {
129 struct vnode * a_dvp;
130 struct vnode ** a_vpp;
131 struct componentname * a_cnp;
132 vfs_context_t a_context;
141 * Open called to set up a new instance of a fifo or
142 * to find an active instance of a fifo.
147 struct vnop_open_args
/* {
150 vfs_context_t a_context;
153 struct vnode
*vp
= ap
->a_vp
;
154 struct fifoinfo
*fip
;
155 struct socket
*rso
, *wso
;
162 fip
= vp
->v_fifoinfo
;
164 if (fip
== (struct fifoinfo
*)0)
165 panic("fifo_open with no fifoinfo");
167 if ((fip
->fi_flags
& FIFO_CREATED
) == 0) {
168 if (fip
->fi_flags
& FIFO_INCREATE
) {
169 fip
->fi_flags
|= FIFO_CREATEWAIT
;
170 error
= msleep(&fip
->fi_flags
, &vp
->v_lock
, PRIBIO
| PCATCH
, "fifocreatewait", 0);
177 fip
->fi_flags
|= FIFO_INCREATE
;
179 if ( (error
= socreate(AF_LOCAL
, &rso
, SOCK_STREAM
, 0)) ) {
182 fip
->fi_readsock
= rso
;
184 if ( (error
= socreate(AF_LOCAL
, &wso
, SOCK_STREAM
, 0)) ) {
188 fip
->fi_writesock
= wso
;
190 if ( (error
= soconnect2(wso
, rso
)) ) {
195 fip
->fi_readers
= fip
->fi_writers
= 0;
198 wso
->so_state
|= SS_CANTRCVMORE
;
199 wso
->so_snd
.sb_lowat
= PIPE_BUF
;
201 /* Because all the unp is protected by single mutex
202 * doing it in two step may actually cause problems
203 * as it opens up window between the drop and acquire
205 socket_unlock(wso
, 1);
209 rso
->so_state
|= SS_CANTSENDMORE
;
210 socket_unlock(wso
, 1);
213 fip
->fi_flags
|= FIFO_CREATED
;
214 fip
->fi_flags
&= ~FIFO_INCREATE
;
216 if ((fip
->fi_flags
& FIFO_CREATEWAIT
)) {
217 fip
->fi_flags
&= ~FIFO_CREATEWAIT
;
218 wakeup(&fip
->fi_flags
);
220 /* vnode lock is held to process further */
224 /* vnode is locked at this point */
225 /* fifo in created already */
226 if (ap
->a_mode
& FREAD
) {
228 if (fip
->fi_readers
== 1) {
229 socket_lock(fip
->fi_writesock
, 1);
230 fip
->fi_writesock
->so_state
&= ~SS_CANTSENDMORE
;
231 socket_unlock(fip
->fi_writesock
, 1);
233 if (fip
->fi_writers
> 0)
234 wakeup((caddr_t
)&fip
->fi_writers
);
237 if (ap
->a_mode
& FWRITE
) {
239 if (fip
->fi_writers
== 1) {
240 socket_lock(fip
->fi_readsock
, 1);
241 fip
->fi_readsock
->so_state
&= ~SS_CANTRCVMORE
;
242 socket_unlock(fip
->fi_readsock
, 1);
244 if (fip
->fi_readers
> 0)
245 wakeup((caddr_t
)&fip
->fi_readers
);
248 if ((ap
->a_mode
& FREAD
) && (ap
->a_mode
& O_NONBLOCK
) == 0) {
249 if (fip
->fi_writers
== 0) {
250 error
= msleep((caddr_t
)&fip
->fi_readers
, &vp
->v_lock
,
251 PCATCH
| PSOCK
, "fifoor", 0);
254 if (fip
->fi_readers
== 1) {
255 if (fip
->fi_writers
> 0)
256 wakeup((caddr_t
)&fip
->fi_writers
);
260 if (ap
->a_mode
& FWRITE
) {
261 if (ap
->a_mode
& O_NONBLOCK
) {
262 if (fip
->fi_readers
== 0) {
267 if (fip
->fi_readers
== 0) {
268 error
= msleep((caddr_t
)&fip
->fi_writers
,&vp
->v_lock
,
269 PCATCH
| PSOCK
, "fifoow", 0);
272 if (fip
->fi_writers
== 1) {
273 if (fip
->fi_readers
> 0)
274 wakeup((caddr_t
)&fip
->fi_readers
);
283 fifo_close_internal(vp
, ap
->a_mode
, ap
->a_context
, 1);
290 fip
->fi_flags
&= ~FIFO_INCREATE
;
292 if ((fip
->fi_flags
& FIFO_CREATEWAIT
)) {
293 fip
->fi_flags
&= ~FIFO_CREATEWAIT
;
294 wakeup(&fip
->fi_flags
);
306 struct vnop_read_args
/* {
310 vfs_context_t a_context;
313 struct uio
*uio
= ap
->a_uio
;
314 struct socket
*rso
= ap
->a_vp
->v_fifoinfo
->fi_readsock
;
315 int error
, startresid
;
319 if (uio
->uio_rw
!= UIO_READ
)
320 panic("fifo_read mode");
322 if (uio_resid(uio
) == 0)
325 rflags
= (ap
->a_ioflag
& IO_NDELAY
) ? MSG_NBIO
: 0;
327 // LP64todo - fix this!
328 startresid
= uio_resid(uio
);
330 /* fifo conformance - if we have a reader open on the fifo but no
331 * writers then we need to make sure we do not block. We do that by
332 * checking the receive buffer and if empty set error to EWOULDBLOCK.
333 * If error is set to EWOULDBLOCK we skip the call into soreceive
336 if (ap
->a_vp
->v_fifoinfo
->fi_writers
< 1) {
338 error
= (rso
->so_rcv
.sb_cc
== 0) ? EWOULDBLOCK
: 0;
339 socket_unlock(rso
, 1);
342 /* skip soreceive to avoid blocking when we have no writers */
343 if (error
!= EWOULDBLOCK
) {
344 error
= soreceive(rso
, (struct sockaddr
**)0, uio
, (struct mbuf
**)0,
345 (struct mbuf
**)0, &rflags
);
348 /* clear EWOULDBLOCK and return EOF (zero) */
352 * Clear EOF indication after first such return.
354 if (uio_resid(uio
) == startresid
) {
356 rso
->so_state
&= ~SS_CANTRCVMORE
;
357 socket_unlock(rso
, 1);
367 struct vnop_write_args
/* {
371 vfs_context_t a_context;
374 struct socket
*wso
= ap
->a_vp
->v_fifoinfo
->fi_writesock
;
378 if (ap
->a_uio
->uio_rw
!= UIO_WRITE
)
379 panic("fifo_write mode");
381 error
= sosend(wso
, (struct sockaddr
*)0, ap
->a_uio
, 0,
382 (struct mbuf
*)0, (ap
->a_ioflag
& IO_NDELAY
) ? MSG_NBIO
: 0);
388 * Device ioctl operation.
392 struct vnop_ioctl_args
/* {
397 vfs_context_t a_context;
400 struct proc
*p
= vfs_context_proc(ap
->a_context
);
401 struct fileproc filetmp
;
402 struct fileglob filefg
;
405 if (ap
->a_command
== FIONBIO
)
407 bzero(&filetmp
, sizeof(struct fileproc
));
408 filetmp
.f_fglob
= &filefg
;
409 if (ap
->a_fflag
& FREAD
) {
410 filetmp
.f_fglob
->fg_data
= (caddr_t
)ap
->a_vp
->v_fifoinfo
->fi_readsock
;
411 error
= soo_ioctl(&filetmp
, ap
->a_command
, ap
->a_data
, p
);
415 if (ap
->a_fflag
& FWRITE
) {
416 filetmp
.f_fglob
->fg_data
= (caddr_t
)ap
->a_vp
->v_fifoinfo
->fi_writesock
;
417 error
= soo_ioctl(&filetmp
, ap
->a_command
, ap
->a_data
, p
);
426 struct vnop_select_args
/* {
431 vfs_context_t a_context;
434 struct proc
*p
= vfs_context_proc(ap
->a_context
);
435 struct fileproc filetmp
;
436 struct fileglob filefg
;
439 bzero(&filetmp
, sizeof(struct fileproc
));
440 filetmp
.f_fglob
= &filefg
;
441 if (ap
->a_which
& FREAD
) {
442 filetmp
.f_fglob
->fg_data
= (caddr_t
)ap
->a_vp
->v_fifoinfo
->fi_readsock
;
443 ready
= soo_select(&filetmp
, ap
->a_which
, ap
->a_wql
, p
);
447 if (ap
->a_which
& FWRITE
) {
448 filetmp
.f_fglob
->fg_data
= (caddr_t
)ap
->a_vp
->v_fifoinfo
->fi_writesock
;
449 ready
= soo_select(&filetmp
, ap
->a_which
, ap
->a_wql
, p
);
457 fifo_inactive(__unused
struct vnop_inactive_args
*ap
)
464 * Device close routine
468 struct vnop_close_args
/* {
471 vfs_context_t a_context;
474 return fifo_close_internal(ap
->a_vp
, ap
->a_fflag
, ap
->a_context
, 0);
478 fifo_close_internal(vnode_t vp
, int fflag
, __unused vfs_context_t context
, int locked
)
480 register struct fifoinfo
*fip
= vp
->v_fifoinfo
;
488 if ((fip
->fi_flags
& FIFO_CREATED
) == 0) {
497 if (fip
->fi_readers
== 0){
498 socket_lock(fip
->fi_writesock
, 1);
499 socantsendmore(fip
->fi_writesock
);
500 socket_unlock(fip
->fi_writesock
, 1);
504 if (fflag
& FWRITE
) {
506 if (fip
->fi_writers
== 0) {
507 socket_lock(fip
->fi_readsock
, 1);
508 socantrcvmore(fip
->fi_readsock
);
509 socket_unlock(fip
->fi_readsock
, 1);
513 if (vnode_isinuse_locked(vp
, 0, 1)) {
520 if (fip
->fi_writers
|| fip
->fi_readers
) {
526 wso
= fip
->fi_writesock
;
527 rso
= fip
->fi_readsock
;
528 fip
->fi_readsock
= 0;
529 fip
->fi_writesock
= 0;
530 fip
->fi_flags
&= ~FIFO_CREATED
;
533 error1
= soclose(rso
);
534 error2
= soclose(wso
);
543 * Print out internal contents of a fifo vnode.
549 register struct fifoinfo
*fip
= vp
->v_fifoinfo
;
551 printf(", fifo with %d readers and %d writers",
552 fip
->fi_readers
, fip
->fi_writers
);
556 * Return POSIX pathconf information applicable to fifo's.
560 struct vnop_pathconf_args
/* {
564 vfs_context_t a_context;
568 switch (ap
->a_name
) {
570 *ap
->a_retval
= LINK_MAX
;
573 *ap
->a_retval
= PIPE_BUF
;
575 case _PC_CHOWN_RESTRICTED
:
585 * Fifo failed operation
588 fifo_ebadf(__unused
void *dummy
)
595 * Fifo advisory byte-level locks.
598 fifo_advlock(__unused
struct vnop_advlock_args
*ap
)