]> git.saurik.com Git - apple/xnu.git/blob - bsd/miscfs/fifofs/fifo_vnops.c
xnu-792.17.14.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_OSREFERENCE_LICENSE_HEADER_START@
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
14 *
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
17 *
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
25 *
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27 */
28 /* Copyright (c) 1995 NeXT Computer, Inc. All Rights Reserved */
29 /*
30 * Copyright (c) 1990, 1993, 1995
31 * The Regents of the University of California. All rights reserved.
32 *
33 * Redistribution and use in source and binary forms, with or without
34 * modification, are permitted provided that the following conditions
35 * are met:
36 * 1. Redistributions of source code must retain the above copyright
37 * notice, this list of conditions and the following disclaimer.
38 * 2. Redistributions in binary form must reproduce the above copyright
39 * notice, this list of conditions and the following disclaimer in the
40 * documentation and/or other materials provided with the distribution.
41 * 3. All advertising materials mentioning features or use of this software
42 * must display the following acknowledgement:
43 * This product includes software developed by the University of
44 * California, Berkeley and its contributors.
45 * 4. Neither the name of the University nor the names of its contributors
46 * may be used to endorse or promote products derived from this software
47 * without specific prior written permission.
48 *
49 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
50 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
51 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
52 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
53 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
54 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
55 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
56 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
57 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
58 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
59 * SUCH DAMAGE.
60 *
61 * @(#)fifo_vnops.c 8.4 (Berkeley) 8/10/94
62 */
63
64 #include <sys/param.h>
65 #include <sys/proc.h>
66 #include <sys/time.h>
67 #include <sys/namei.h>
68 #include <sys/vnode_internal.h>
69 #include <sys/socket.h>
70 #include <sys/socketvar.h>
71 #include <sys/stat.h>
72 #include <sys/systm.h>
73 #include <sys/ioctl.h>
74 #include <sys/file_internal.h>
75 #include <sys/errno.h>
76 #include <sys/malloc.h>
77 #include <miscfs/fifofs/fifo.h>
78 #include <vfs/vfs_support.h>
79
80 #define VOPFUNC int (*)(void *)
81
82 extern int soo_ioctl(struct fileproc *fp, u_long cmd, caddr_t data, struct proc *p);
83 extern int soo_select(struct fileproc *fp, int which, void * wql, struct proc *p);
84
85 int (**fifo_vnodeop_p)(void *);
86 struct vnodeopv_entry_desc fifo_vnodeop_entries[] = {
87 { &vnop_default_desc, (VOPFUNC)vn_default_error },
88 { &vnop_lookup_desc, (VOPFUNC)fifo_lookup }, /* lookup */
89 { &vnop_create_desc, (VOPFUNC)err_create }, /* create */
90 { &vnop_mknod_desc, (VOPFUNC)err_mknod }, /* mknod */
91 { &vnop_open_desc, (VOPFUNC)fifo_open }, /* open */
92 { &vnop_close_desc, (VOPFUNC)fifo_close }, /* close */
93 { &vnop_access_desc, (VOPFUNC)fifo_access }, /* access */
94 { &vnop_getattr_desc, (VOPFUNC)fifo_getattr }, /* getattr */
95 { &vnop_setattr_desc, (VOPFUNC)fifo_setattr }, /* setattr */
96 { &vnop_read_desc, (VOPFUNC)fifo_read }, /* read */
97 { &vnop_write_desc, (VOPFUNC)fifo_write }, /* write */
98 { &vnop_ioctl_desc, (VOPFUNC)fifo_ioctl }, /* ioctl */
99 { &vnop_select_desc, (VOPFUNC)fifo_select }, /* select */
100 { &vnop_revoke_desc, (VOPFUNC)fifo_revoke }, /* revoke */
101 { &vnop_mmap_desc, (VOPFUNC)err_mmap }, /* mmap */
102 { &vnop_fsync_desc, (VOPFUNC)fifo_fsync }, /* fsync */
103 { &vnop_remove_desc, (VOPFUNC)err_remove }, /* remove */
104 { &vnop_link_desc, (VOPFUNC)err_link }, /* link */
105 { &vnop_rename_desc, (VOPFUNC)err_rename }, /* rename */
106 { &vnop_mkdir_desc, (VOPFUNC)err_mkdir }, /* mkdir */
107 { &vnop_rmdir_desc, (VOPFUNC)err_rmdir }, /* rmdir */
108 { &vnop_symlink_desc, (VOPFUNC)err_symlink }, /* symlink */
109 { &vnop_readdir_desc, (VOPFUNC)err_readdir }, /* readdir */
110 { &vnop_readlink_desc, (VOPFUNC)err_readlink }, /* readlink */
111 { &vnop_inactive_desc, (VOPFUNC)fifo_inactive }, /* inactive */
112 { &vnop_reclaim_desc, (VOPFUNC)fifo_reclaim }, /* reclaim */
113 { &vnop_strategy_desc, (VOPFUNC)err_strategy }, /* strategy */
114 { &vnop_pathconf_desc, (VOPFUNC)fifo_pathconf }, /* pathconf */
115 { &vnop_advlock_desc, (VOPFUNC)fifo_advlock }, /* advlock */
116 { &vnop_bwrite_desc, (VOPFUNC)fifo_bwrite }, /* bwrite */
117 { &vnop_pagein_desc, (VOPFUNC)err_pagein }, /* Pagein */
118 { &vnop_pageout_desc, (VOPFUNC)err_pageout }, /* Pageout */
119 { &vnop_copyfile_desc, (VOPFUNC)err_copyfile }, /* Copyfile */
120 { &vnop_blktooff_desc, (VOPFUNC)err_blktooff }, /* blktooff */
121 { &vnop_offtoblk_desc, (VOPFUNC)err_offtoblk }, /* offtoblk */
122 { &vnop_blockmap_desc, (VOPFUNC)err_blockmap }, /* blockmap */
123 { (struct vnodeop_desc*)NULL, (int(*)())NULL }
124 };
125 struct vnodeopv_desc fifo_vnodeop_opv_desc =
126 { &fifo_vnodeop_p, fifo_vnodeop_entries };
127
128 /*
129 * Trivial lookup routine that always fails.
130 */
131 /* ARGSUSED */
132 int
133 fifo_lookup(ap)
134 struct vnop_lookup_args /* {
135 struct vnode * a_dvp;
136 struct vnode ** a_vpp;
137 struct componentname * a_cnp;
138 vfs_context_t a_context;
139 } */ *ap;
140 {
141
142 *ap->a_vpp = NULL;
143 return (ENOTDIR);
144 }
145
146 /*
147 * Open called to set up a new instance of a fifo or
148 * to find an active instance of a fifo.
149 */
150 /* ARGSUSED */
151 int
152 fifo_open(ap)
153 struct vnop_open_args /* {
154 struct vnode *a_vp;
155 int a_mode;
156 vfs_context_t a_context;
157 } */ *ap;
158 {
159 struct vnode *vp = ap->a_vp;
160 struct fifoinfo *fip;
161 struct socket *rso, *wso;
162 int error;
163
164 vnode_lock(vp);
165
166 retry:
167
168 fip = vp->v_fifoinfo;
169
170 if (fip == (struct fifoinfo *)0)
171 panic("fifo_open with no fifoinfo");
172
173 if ((fip->fi_flags & FIFO_CREATED) == 0) {
174 if (fip->fi_flags & FIFO_INCREATE) {
175 fip->fi_flags |= FIFO_CREATEWAIT;
176 error = msleep(&fip->fi_flags, &vp->v_lock, PRIBIO | PCATCH, "fifocreatewait", 0);
177 if (error) {
178 vnode_unlock(vp);
179 return(error);
180 }
181 goto retry;
182 } else {
183 fip->fi_flags |= FIFO_INCREATE;
184 vnode_unlock(vp);
185 if ( (error = socreate(AF_LOCAL, &rso, SOCK_STREAM, 0)) ) {
186 goto bad1;
187 }
188 fip->fi_readsock = rso;
189
190 if ( (error = socreate(AF_LOCAL, &wso, SOCK_STREAM, 0)) ) {
191 (void)soclose(rso);
192 goto bad1;
193 }
194 fip->fi_writesock = wso;
195
196 if ( (error = soconnect2(wso, rso)) ) {
197 (void)soclose(wso);
198 (void)soclose(rso);
199 goto bad1;
200 }
201 fip->fi_readers = fip->fi_writers = 0;
202
203 socket_lock(wso, 1);
204 wso->so_state |= SS_CANTRCVMORE;
205 wso->so_snd.sb_lowat = PIPE_BUF;
206 #if 0
207 /* Because all the unp is protected by single mutex
208 * doing it in two step may actually cause problems
209 * as it opens up window between the drop and acquire
210 */
211 socket_unlock(wso, 1);
212
213 socket_lock(rso, 1);
214 #endif
215 rso->so_state |= SS_CANTSENDMORE;
216 socket_unlock(wso, 1);
217
218 vnode_lock(vp);
219 fip->fi_flags |= FIFO_CREATED;
220 fip->fi_flags &= ~FIFO_INCREATE;
221
222 if ((fip->fi_flags & FIFO_CREATEWAIT)) {
223 fip->fi_flags &= ~FIFO_CREATEWAIT;
224 wakeup(&fip->fi_flags);
225 }
226 /* vnode lock is held to process further */
227 }
228 }
229
230 /* vnode is locked at this point */
231 /* fifo in created already */
232 if (ap->a_mode & FREAD) {
233 fip->fi_readers++;
234 if (fip->fi_readers == 1) {
235 socket_lock(fip->fi_writesock, 1);
236 fip->fi_writesock->so_state &= ~SS_CANTSENDMORE;
237 socket_unlock(fip->fi_writesock, 1);
238
239 if (fip->fi_writers > 0)
240 wakeup((caddr_t)&fip->fi_writers);
241 }
242 }
243 if (ap->a_mode & FWRITE) {
244 fip->fi_writers++;
245 if (fip->fi_writers == 1) {
246 socket_lock(fip->fi_readsock, 1);
247 fip->fi_readsock->so_state &= ~SS_CANTRCVMORE;
248 socket_unlock(fip->fi_readsock, 1);
249
250 if (fip->fi_readers > 0)
251 wakeup((caddr_t)&fip->fi_readers);
252 }
253 }
254 if ((ap->a_mode & FREAD) && (ap->a_mode & O_NONBLOCK) == 0) {
255 if (fip->fi_writers == 0) {
256 error = msleep((caddr_t)&fip->fi_readers, &vp->v_lock,
257 PCATCH | PSOCK, "fifoor", 0);
258 if (error)
259 goto bad;
260 if (fip->fi_readers == 1) {
261 if (fip->fi_writers > 0)
262 wakeup((caddr_t)&fip->fi_writers);
263 }
264 }
265 }
266 if (ap->a_mode & FWRITE) {
267 if (ap->a_mode & O_NONBLOCK) {
268 if (fip->fi_readers == 0) {
269 error = ENXIO;
270 goto bad;
271 }
272 } else {
273 if (fip->fi_readers == 0) {
274 error = msleep((caddr_t)&fip->fi_writers,&vp->v_lock,
275 PCATCH | PSOCK, "fifoow", 0);
276 if (error)
277 goto bad;
278 if (fip->fi_writers == 1) {
279 if (fip->fi_readers > 0)
280 wakeup((caddr_t)&fip->fi_readers);
281 }
282 }
283 }
284 }
285
286 vnode_unlock(vp);
287 return (0);
288 bad:
289 fifo_close_internal(vp, ap->a_mode, ap->a_context, 1);
290
291 vnode_unlock(vp);
292 return (error);
293 bad1:
294 vnode_lock(vp);
295
296 fip->fi_flags &= ~FIFO_INCREATE;
297
298 if ((fip->fi_flags & FIFO_CREATEWAIT)) {
299 fip->fi_flags &= ~FIFO_CREATEWAIT;
300 wakeup(&fip->fi_flags);
301 }
302 vnode_unlock(vp);
303
304 return (error);
305 }
306
307 /*
308 * Vnode op for read
309 */
310 int
311 fifo_read(ap)
312 struct vnop_read_args /* {
313 struct vnode *a_vp;
314 struct uio *a_uio;
315 int a_ioflag;
316 vfs_context_t a_context;
317 } */ *ap;
318 {
319 struct uio *uio = ap->a_uio;
320 struct socket *rso = ap->a_vp->v_fifoinfo->fi_readsock;
321 int error, startresid;
322 int rflags;
323
324 #if DIAGNOSTIC
325 if (uio->uio_rw != UIO_READ)
326 panic("fifo_read mode");
327 #endif
328 if (uio_resid(uio) == 0)
329 return (0);
330
331 rflags = (ap->a_ioflag & IO_NDELAY) ? MSG_NBIO : 0;
332
333 // LP64todo - fix this!
334 startresid = uio_resid(uio);
335
336 /* fifo conformance - if we have a reader open on the fifo but no
337 * writers then we need to make sure we do not block. We do that by
338 * checking the receive buffer and if empty set error to EWOULDBLOCK.
339 * If error is set to EWOULDBLOCK we skip the call into soreceive
340 */
341 error = 0;
342 if (ap->a_vp->v_fifoinfo->fi_writers < 1) {
343 socket_lock(rso, 1);
344 error = (rso->so_rcv.sb_cc == 0) ? EWOULDBLOCK : 0;
345 socket_unlock(rso, 1);
346 }
347
348 /* skip soreceive to avoid blocking when we have no writers */
349 if (error != EWOULDBLOCK) {
350 error = soreceive(rso, (struct sockaddr **)0, uio, (struct mbuf **)0,
351 (struct mbuf **)0, &rflags);
352 }
353 else {
354 /* clear EWOULDBLOCK and return EOF (zero) */
355 error = 0;
356 }
357 /*
358 * Clear EOF indication after first such return.
359 */
360 if (uio_resid(uio) == startresid) {
361 socket_lock(rso, 1);
362 rso->so_state &= ~SS_CANTRCVMORE;
363 socket_unlock(rso, 1);
364 }
365 return (error);
366 }
367
368 /*
369 * Vnode op for write
370 */
371 int
372 fifo_write(ap)
373 struct vnop_write_args /* {
374 struct vnode *a_vp;
375 struct uio *a_uio;
376 int a_ioflag;
377 vfs_context_t a_context;
378 } */ *ap;
379 {
380 struct socket *wso = ap->a_vp->v_fifoinfo->fi_writesock;
381 int error;
382
383 #if DIAGNOSTIC
384 if (ap->a_uio->uio_rw != UIO_WRITE)
385 panic("fifo_write mode");
386 #endif
387 error = sosend(wso, (struct sockaddr *)0, ap->a_uio, 0,
388 (struct mbuf *)0, (ap->a_ioflag & IO_NDELAY) ? MSG_NBIO : 0);
389
390 return (error);
391 }
392
393 /*
394 * Device ioctl operation.
395 */
396 int
397 fifo_ioctl(ap)
398 struct vnop_ioctl_args /* {
399 struct vnode *a_vp;
400 int a_command;
401 caddr_t a_data;
402 int a_fflag;
403 vfs_context_t a_context;
404 } */ *ap;
405 {
406 struct proc *p = vfs_context_proc(ap->a_context);
407 struct fileproc filetmp;
408 struct fileglob filefg;
409 int error;
410
411 if (ap->a_command == FIONBIO)
412 return (0);
413 bzero(&filetmp, sizeof(struct fileproc));
414 filetmp.f_fglob = &filefg;
415 if (ap->a_fflag & FREAD) {
416 filetmp.f_fglob->fg_data = (caddr_t)ap->a_vp->v_fifoinfo->fi_readsock;
417 error = soo_ioctl(&filetmp, ap->a_command, ap->a_data, p);
418 if (error)
419 return (error);
420 }
421 if (ap->a_fflag & FWRITE) {
422 filetmp.f_fglob->fg_data = (caddr_t)ap->a_vp->v_fifoinfo->fi_writesock;
423 error = soo_ioctl(&filetmp, ap->a_command, ap->a_data, p);
424 if (error)
425 return (error);
426 }
427 return (0);
428 }
429
430 int
431 fifo_select(ap)
432 struct vnop_select_args /* {
433 struct vnode *a_vp;
434 int a_which;
435 int a_fflags;
436 void * a_wql;
437 vfs_context_t a_context;
438 } */ *ap;
439 {
440 struct proc *p = vfs_context_proc(ap->a_context);
441 struct fileproc filetmp;
442 struct fileglob filefg;
443 int ready;
444
445 bzero(&filetmp, sizeof(struct fileproc));
446 filetmp.f_fglob = &filefg;
447 if (ap->a_which & FREAD) {
448 filetmp.f_fglob->fg_data = (caddr_t)ap->a_vp->v_fifoinfo->fi_readsock;
449 ready = soo_select(&filetmp, ap->a_which, ap->a_wql, p);
450 if (ready)
451 return (ready);
452 }
453 if (ap->a_which & FWRITE) {
454 filetmp.f_fglob->fg_data = (caddr_t)ap->a_vp->v_fifoinfo->fi_writesock;
455 ready = soo_select(&filetmp, ap->a_which, ap->a_wql, p);
456 if (ready)
457 return (ready);
458 }
459 return (0);
460 }
461
462 int
463 fifo_inactive(__unused struct vnop_inactive_args *ap)
464 {
465 return (0);
466 }
467
468
469 /*
470 * Device close routine
471 */
472 int
473 fifo_close(ap)
474 struct vnop_close_args /* {
475 struct vnode *a_vp;
476 int a_fflag;
477 vfs_context_t a_context;
478 } */ *ap;
479 {
480 return fifo_close_internal(ap->a_vp, ap->a_fflag, ap->a_context, 0);
481 }
482
483 int
484 fifo_close_internal(vnode_t vp, int fflag, __unused vfs_context_t context, int locked)
485 {
486 register struct fifoinfo *fip = vp->v_fifoinfo;
487 int error1, error2;
488 struct socket *rso;
489 struct socket *wso;
490
491 if (!locked)
492 vnode_lock(vp);
493
494 if ((fip->fi_flags & FIFO_CREATED) == 0) {
495 if (!locked)
496 vnode_unlock(vp);
497 return(0);
498
499 }
500
501 if (fflag & FREAD) {
502 fip->fi_readers--;
503 if (fip->fi_readers == 0){
504 socket_lock(fip->fi_writesock, 1);
505 socantsendmore(fip->fi_writesock);
506 socket_unlock(fip->fi_writesock, 1);
507 }
508 }
509
510 if (fflag & FWRITE) {
511 fip->fi_writers--;
512 if (fip->fi_writers == 0) {
513 socket_lock(fip->fi_readsock, 1);
514 socantrcvmore(fip->fi_readsock);
515 socket_unlock(fip->fi_readsock, 1);
516 }
517 }
518 #if 0
519 if (vnode_isinuse_locked(vp, 0, 1)) {
520 if (!locked)
521 vnode_unlock(vp);
522 return (0);
523 }
524 #endif
525
526 if (fip->fi_writers || fip->fi_readers) {
527 if (!locked)
528 vnode_unlock(vp);
529 return (0);
530 }
531
532 wso = fip->fi_writesock;
533 rso = fip->fi_readsock;
534 fip->fi_readsock = 0;
535 fip->fi_writesock = 0;
536 fip->fi_flags &= ~FIFO_CREATED;
537 if (!locked)
538 vnode_unlock(vp);
539 error1 = soclose(rso);
540 error2 = soclose(wso);
541
542 if (error1)
543 return (error1);
544 return (error2);
545 }
546
547
548 /*
549 * Print out internal contents of a fifo vnode.
550 */
551 void
552 fifo_printinfo(vp)
553 struct vnode *vp;
554 {
555 register struct fifoinfo *fip = vp->v_fifoinfo;
556
557 printf(", fifo with %d readers and %d writers",
558 fip->fi_readers, fip->fi_writers);
559 }
560
561 /*
562 * Return POSIX pathconf information applicable to fifo's.
563 */
564 int
565 fifo_pathconf(ap)
566 struct vnop_pathconf_args /* {
567 struct vnode *a_vp;
568 int a_name;
569 int *a_retval;
570 vfs_context_t a_context;
571 } */ *ap;
572 {
573
574 switch (ap->a_name) {
575 case _PC_LINK_MAX:
576 *ap->a_retval = LINK_MAX;
577 return (0);
578 case _PC_PIPE_BUF:
579 *ap->a_retval = PIPE_BUF;
580 return (0);
581 case _PC_CHOWN_RESTRICTED:
582 *ap->a_retval = 1;
583 return (0);
584 default:
585 return (EINVAL);
586 }
587 /* NOTREACHED */
588 }
589
590 /*
591 * Fifo failed operation
592 */
593 int
594 fifo_ebadf(__unused void *dummy)
595 {
596
597 return (EBADF);
598 }
599
600 /*
601 * Fifo advisory byte-level locks.
602 */
603 int
604 fifo_advlock(__unused struct vnop_advlock_args *ap)
605 {
606
607 return (ENOTSUP);
608 }
609