]> git.saurik.com Git - apple/xnu.git/blob - bsd/miscfs/fifofs/fifo_vnops.c
xnu-792.10.96.tar.gz
[apple/xnu.git] / bsd / miscfs / fifofs / fifo_vnops.c
1 /*
2 * Copyright (c) 2000-2004 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
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.
11 *
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
18 * under the License.
19 *
20 * @APPLE_LICENSE_HEADER_END@
21 */
22 /* Copyright (c) 1995 NeXT Computer, Inc. All Rights Reserved */
23 /*
24 * Copyright (c) 1990, 1993, 1995
25 * The Regents of the University of California. All rights reserved.
26 *
27 * Redistribution and use in source and binary forms, with or without
28 * modification, are permitted provided that the following conditions
29 * are met:
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.
42 *
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
53 * SUCH DAMAGE.
54 *
55 * @(#)fifo_vnops.c 8.4 (Berkeley) 8/10/94
56 */
57
58 #include <sys/param.h>
59 #include <sys/proc.h>
60 #include <sys/time.h>
61 #include <sys/namei.h>
62 #include <sys/vnode_internal.h>
63 #include <sys/socket.h>
64 #include <sys/socketvar.h>
65 #include <sys/stat.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>
73
74 #define VOPFUNC int (*)(void *)
75
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);
78
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 }
118 };
119 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(ap)
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;
133 } */ *ap;
134 {
135
136 *ap->a_vpp = NULL;
137 return (ENOTDIR);
138 }
139
140 /*
141 * Open called to set up a new instance of a fifo or
142 * to find an active instance of a fifo.
143 */
144 /* ARGSUSED */
145 int
146 fifo_open(ap)
147 struct vnop_open_args /* {
148 struct vnode *a_vp;
149 int a_mode;
150 vfs_context_t a_context;
151 } */ *ap;
152 {
153 struct vnode *vp = ap->a_vp;
154 struct fifoinfo *fip;
155 struct socket *rso, *wso;
156 int error;
157
158 vnode_lock(vp);
159
160 retry:
161
162 fip = vp->v_fifoinfo;
163
164 if (fip == (struct fifoinfo *)0)
165 panic("fifo_open with no fifoinfo");
166
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);
171 if (error) {
172 vnode_unlock(vp);
173 return(error);
174 }
175 goto retry;
176 } else {
177 fip->fi_flags |= FIFO_INCREATE;
178 vnode_unlock(vp);
179 if ( (error = socreate(AF_LOCAL, &rso, SOCK_STREAM, 0)) ) {
180 goto bad1;
181 }
182 fip->fi_readsock = rso;
183
184 if ( (error = socreate(AF_LOCAL, &wso, SOCK_STREAM, 0)) ) {
185 (void)soclose(rso);
186 goto bad1;
187 }
188 fip->fi_writesock = wso;
189
190 if ( (error = soconnect2(wso, rso)) ) {
191 (void)soclose(wso);
192 (void)soclose(rso);
193 goto bad1;
194 }
195 fip->fi_readers = fip->fi_writers = 0;
196
197 socket_lock(wso, 1);
198 wso->so_state |= SS_CANTRCVMORE;
199 wso->so_snd.sb_lowat = PIPE_BUF;
200 #if 0
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
204 */
205 socket_unlock(wso, 1);
206
207 socket_lock(rso, 1);
208 #endif
209 rso->so_state |= SS_CANTSENDMORE;
210 socket_unlock(wso, 1);
211
212 vnode_lock(vp);
213 fip->fi_flags |= FIFO_CREATED;
214 fip->fi_flags &= ~FIFO_INCREATE;
215
216 if ((fip->fi_flags & FIFO_CREATEWAIT)) {
217 fip->fi_flags &= ~FIFO_CREATEWAIT;
218 wakeup(&fip->fi_flags);
219 }
220 /* vnode lock is held to process further */
221 }
222 }
223
224 /* vnode is locked at this point */
225 /* fifo in created already */
226 if (ap->a_mode & FREAD) {
227 fip->fi_readers++;
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);
232
233 if (fip->fi_writers > 0)
234 wakeup((caddr_t)&fip->fi_writers);
235 }
236 }
237 if (ap->a_mode & FWRITE) {
238 fip->fi_writers++;
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);
243
244 if (fip->fi_readers > 0)
245 wakeup((caddr_t)&fip->fi_readers);
246 }
247 }
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);
252 if (error)
253 goto bad;
254 if (fip->fi_readers == 1) {
255 if (fip->fi_writers > 0)
256 wakeup((caddr_t)&fip->fi_writers);
257 }
258 }
259 }
260 if (ap->a_mode & FWRITE) {
261 if (ap->a_mode & O_NONBLOCK) {
262 if (fip->fi_readers == 0) {
263 error = ENXIO;
264 goto bad;
265 }
266 } else {
267 if (fip->fi_readers == 0) {
268 error = msleep((caddr_t)&fip->fi_writers,&vp->v_lock,
269 PCATCH | PSOCK, "fifoow", 0);
270 if (error)
271 goto bad;
272 if (fip->fi_writers == 1) {
273 if (fip->fi_readers > 0)
274 wakeup((caddr_t)&fip->fi_readers);
275 }
276 }
277 }
278 }
279
280 vnode_unlock(vp);
281 return (0);
282 bad:
283 fifo_close_internal(vp, ap->a_mode, ap->a_context, 1);
284
285 vnode_unlock(vp);
286 return (error);
287 bad1:
288 vnode_lock(vp);
289
290 fip->fi_flags &= ~FIFO_INCREATE;
291
292 if ((fip->fi_flags & FIFO_CREATEWAIT)) {
293 fip->fi_flags &= ~FIFO_CREATEWAIT;
294 wakeup(&fip->fi_flags);
295 }
296 vnode_unlock(vp);
297
298 return (error);
299 }
300
301 /*
302 * Vnode op for read
303 */
304 int
305 fifo_read(ap)
306 struct vnop_read_args /* {
307 struct vnode *a_vp;
308 struct uio *a_uio;
309 int a_ioflag;
310 vfs_context_t a_context;
311 } */ *ap;
312 {
313 struct uio *uio = ap->a_uio;
314 struct socket *rso = ap->a_vp->v_fifoinfo->fi_readsock;
315 int error, startresid;
316 int rflags;
317
318 #if DIAGNOSTIC
319 if (uio->uio_rw != UIO_READ)
320 panic("fifo_read mode");
321 #endif
322 if (uio_resid(uio) == 0)
323 return (0);
324
325 rflags = (ap->a_ioflag & IO_NDELAY) ? MSG_NBIO : 0;
326
327 // LP64todo - fix this!
328 startresid = uio_resid(uio);
329
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
334 */
335 error = 0;
336 if (ap->a_vp->v_fifoinfo->fi_writers < 1) {
337 socket_lock(rso, 1);
338 error = (rso->so_rcv.sb_cc == 0) ? EWOULDBLOCK : 0;
339 socket_unlock(rso, 1);
340 }
341
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);
346 }
347 else {
348 /* clear EWOULDBLOCK and return EOF (zero) */
349 error = 0;
350 }
351 /*
352 * Clear EOF indication after first such return.
353 */
354 if (uio_resid(uio) == startresid) {
355 socket_lock(rso, 1);
356 rso->so_state &= ~SS_CANTRCVMORE;
357 socket_unlock(rso, 1);
358 }
359 return (error);
360 }
361
362 /*
363 * Vnode op for write
364 */
365 int
366 fifo_write(ap)
367 struct vnop_write_args /* {
368 struct vnode *a_vp;
369 struct uio *a_uio;
370 int a_ioflag;
371 vfs_context_t a_context;
372 } */ *ap;
373 {
374 struct socket *wso = ap->a_vp->v_fifoinfo->fi_writesock;
375 int error;
376
377 #if DIAGNOSTIC
378 if (ap->a_uio->uio_rw != UIO_WRITE)
379 panic("fifo_write mode");
380 #endif
381 error = sosend(wso, (struct sockaddr *)0, ap->a_uio, 0,
382 (struct mbuf *)0, (ap->a_ioflag & IO_NDELAY) ? MSG_NBIO : 0);
383
384 return (error);
385 }
386
387 /*
388 * Device ioctl operation.
389 */
390 int
391 fifo_ioctl(ap)
392 struct vnop_ioctl_args /* {
393 struct vnode *a_vp;
394 int a_command;
395 caddr_t a_data;
396 int a_fflag;
397 vfs_context_t a_context;
398 } */ *ap;
399 {
400 struct proc *p = vfs_context_proc(ap->a_context);
401 struct fileproc filetmp;
402 struct fileglob filefg;
403 int error;
404
405 if (ap->a_command == FIONBIO)
406 return (0);
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);
412 if (error)
413 return (error);
414 }
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);
418 if (error)
419 return (error);
420 }
421 return (0);
422 }
423
424 int
425 fifo_select(ap)
426 struct vnop_select_args /* {
427 struct vnode *a_vp;
428 int a_which;
429 int a_fflags;
430 void * a_wql;
431 vfs_context_t a_context;
432 } */ *ap;
433 {
434 struct proc *p = vfs_context_proc(ap->a_context);
435 struct fileproc filetmp;
436 struct fileglob filefg;
437 int ready;
438
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);
444 if (ready)
445 return (ready);
446 }
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);
450 if (ready)
451 return (ready);
452 }
453 return (0);
454 }
455
456 int
457 fifo_inactive(__unused struct vnop_inactive_args *ap)
458 {
459 return (0);
460 }
461
462
463 /*
464 * Device close routine
465 */
466 int
467 fifo_close(ap)
468 struct vnop_close_args /* {
469 struct vnode *a_vp;
470 int a_fflag;
471 vfs_context_t a_context;
472 } */ *ap;
473 {
474 return fifo_close_internal(ap->a_vp, ap->a_fflag, ap->a_context, 0);
475 }
476
477 int
478 fifo_close_internal(vnode_t vp, int fflag, __unused vfs_context_t context, int locked)
479 {
480 register struct fifoinfo *fip = vp->v_fifoinfo;
481 int error1, error2;
482 struct socket *rso;
483 struct socket *wso;
484
485 if (!locked)
486 vnode_lock(vp);
487
488 if ((fip->fi_flags & FIFO_CREATED) == 0) {
489 if (!locked)
490 vnode_unlock(vp);
491 return(0);
492
493 }
494
495 if (fflag & FREAD) {
496 fip->fi_readers--;
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);
501 }
502 }
503
504 if (fflag & FWRITE) {
505 fip->fi_writers--;
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);
510 }
511 }
512 #if 0
513 if (vnode_isinuse_locked(vp, 0, 1)) {
514 if (!locked)
515 vnode_unlock(vp);
516 return (0);
517 }
518 #endif
519
520 if (fip->fi_writers || fip->fi_readers) {
521 if (!locked)
522 vnode_unlock(vp);
523 return (0);
524 }
525
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;
531 if (!locked)
532 vnode_unlock(vp);
533 error1 = soclose(rso);
534 error2 = soclose(wso);
535
536 if (error1)
537 return (error1);
538 return (error2);
539 }
540
541
542 /*
543 * Print out internal contents of a fifo vnode.
544 */
545 void
546 fifo_printinfo(vp)
547 struct vnode *vp;
548 {
549 register struct fifoinfo *fip = vp->v_fifoinfo;
550
551 printf(", fifo with %d readers and %d writers",
552 fip->fi_readers, fip->fi_writers);
553 }
554
555 /*
556 * Return POSIX pathconf information applicable to fifo's.
557 */
558 int
559 fifo_pathconf(ap)
560 struct vnop_pathconf_args /* {
561 struct vnode *a_vp;
562 int a_name;
563 int *a_retval;
564 vfs_context_t a_context;
565 } */ *ap;
566 {
567
568 switch (ap->a_name) {
569 case _PC_LINK_MAX:
570 *ap->a_retval = LINK_MAX;
571 return (0);
572 case _PC_PIPE_BUF:
573 *ap->a_retval = PIPE_BUF;
574 return (0);
575 case _PC_CHOWN_RESTRICTED:
576 *ap->a_retval = 1;
577 return (0);
578 default:
579 return (EINVAL);
580 }
581 /* NOTREACHED */
582 }
583
584 /*
585 * Fifo failed operation
586 */
587 int
588 fifo_ebadf(__unused void *dummy)
589 {
590
591 return (EBADF);
592 }
593
594 /*
595 * Fifo advisory byte-level locks.
596 */
597 int
598 fifo_advlock(__unused struct vnop_advlock_args *ap)
599 {
600
601 return (ENOTSUP);
602 }
603