]> git.saurik.com Git - apple/xnu.git/blob - bsd/miscfs/fifofs/fifo_vnops.c
c1af000e6ed148be0345eb6a0397c5b13d9c44c7
[apple/xnu.git] / bsd / miscfs / fifofs / fifo_vnops.c
1 /*
2 * Copyright (c) 2000-2006 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, vfs_context_t ctx);
83 extern int soo_select(struct fileproc *fp, int which, void * wql, vfs_context_t ctx);
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(struct vnop_lookup_args *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(struct vnop_open_args *ap)
147 {
148 struct vnode *vp = ap->a_vp;
149 struct fifoinfo *fip;
150 struct socket *rso, *wso;
151 int error;
152
153 vnode_lock(vp);
154
155 retry:
156
157 fip = vp->v_fifoinfo;
158
159 if (fip == (struct fifoinfo *)0)
160 panic("fifo_open with no fifoinfo");
161
162 if ((fip->fi_flags & FIFO_CREATED) == 0) {
163 if (fip->fi_flags & FIFO_INCREATE) {
164 fip->fi_flags |= FIFO_CREATEWAIT;
165 error = msleep(&fip->fi_flags, &vp->v_lock, PRIBIO | PCATCH, "fifocreatewait", NULL);
166 if (error) {
167 vnode_unlock(vp);
168 return(error);
169 }
170 goto retry;
171 } else {
172 fip->fi_flags |= FIFO_INCREATE;
173 vnode_unlock(vp);
174 if ( (error = socreate(AF_LOCAL, &rso, SOCK_STREAM, 0)) ) {
175 goto bad1;
176 }
177
178 if ( (error = socreate(AF_LOCAL, &wso, SOCK_STREAM, 0)) ) {
179 (void)soclose(rso);
180 goto bad1;
181 }
182
183 if ( (error = soconnect2(wso, rso)) ) {
184 (void)soclose(wso);
185 (void)soclose(rso);
186 goto bad1;
187 }
188 fip->fi_readers = fip->fi_writers = 0;
189
190 /* Lock ordering between wso and rso does not matter here
191 * because they are just created and no one has a reference to them
192 */
193 socket_lock(wso, 1);
194 wso->so_state |= SS_CANTRCVMORE;
195 wso->so_snd.sb_lowat = PIPE_BUF;
196 socket_unlock(wso, 1);
197
198 socket_lock(rso, 1);
199 rso->so_state |= SS_CANTSENDMORE;
200 socket_unlock(rso, 1);
201
202 vnode_lock(vp);
203 fip->fi_readsock = rso;
204 fip->fi_writesock = wso;
205
206 fip->fi_flags |= FIFO_CREATED;
207 fip->fi_flags &= ~FIFO_INCREATE;
208
209 if ((fip->fi_flags & FIFO_CREATEWAIT)) {
210 fip->fi_flags &= ~FIFO_CREATEWAIT;
211 wakeup(&fip->fi_flags);
212 }
213 /* vnode lock is held to process further */
214 }
215 }
216
217 /* vnode is locked at this point */
218 /* fifo in created already */
219 if (ap->a_mode & FREAD) {
220 fip->fi_readers++;
221 if (fip->fi_readers == 1) {
222 socket_lock(fip->fi_writesock, 1);
223 fip->fi_writesock->so_state &= ~SS_CANTSENDMORE;
224 socket_unlock(fip->fi_writesock, 1);
225
226 if (fip->fi_writers > 0)
227 wakeup((caddr_t)&fip->fi_writers);
228 }
229 }
230 if (ap->a_mode & FWRITE) {
231 fip->fi_writers++;
232 if (fip->fi_writers == 1) {
233 socket_lock(fip->fi_readsock, 1);
234 fip->fi_readsock->so_state &= ~SS_CANTRCVMORE;
235 socket_unlock(fip->fi_readsock, 1);
236
237 if (fip->fi_readers > 0)
238 wakeup((caddr_t)&fip->fi_readers);
239 }
240 }
241 if ((ap->a_mode & FREAD) && (ap->a_mode & O_NONBLOCK) == 0) {
242 if (fip->fi_writers == 0) {
243 error = msleep((caddr_t)&fip->fi_readers, &vp->v_lock,
244 PCATCH | PSOCK, "fifoor", NULL);
245 if (error)
246 goto bad;
247 if (fip->fi_readers == 1) {
248 if (fip->fi_writers > 0)
249 wakeup((caddr_t)&fip->fi_writers);
250 }
251 }
252 }
253 if (ap->a_mode & FWRITE) {
254 if (ap->a_mode & O_NONBLOCK) {
255 if (fip->fi_readers == 0) {
256 error = ENXIO;
257 goto bad;
258 }
259 } else {
260 if (fip->fi_readers == 0) {
261 error = msleep((caddr_t)&fip->fi_writers,&vp->v_lock,
262 PCATCH | PSOCK, "fifoow", NULL);
263 if (error)
264 goto bad;
265 if (fip->fi_writers == 1) {
266 if (fip->fi_readers > 0)
267 wakeup((caddr_t)&fip->fi_readers);
268 }
269 }
270 }
271 }
272
273 vnode_unlock(vp);
274 return (0);
275 bad:
276 fifo_close_internal(vp, ap->a_mode, ap->a_context, 1);
277
278 vnode_unlock(vp);
279 return (error);
280 bad1:
281 vnode_lock(vp);
282
283 fip->fi_flags &= ~FIFO_INCREATE;
284
285 if ((fip->fi_flags & FIFO_CREATEWAIT)) {
286 fip->fi_flags &= ~FIFO_CREATEWAIT;
287 wakeup(&fip->fi_flags);
288 }
289 vnode_unlock(vp);
290
291 return (error);
292 }
293
294 /*
295 * Vnode op for read
296 */
297 int
298 fifo_read(struct vnop_read_args *ap)
299 {
300 struct uio *uio = ap->a_uio;
301 struct socket *rso = ap->a_vp->v_fifoinfo->fi_readsock;
302 user_ssize_t startresid;
303 int error;
304 int rflags;
305
306 #if DIAGNOSTIC
307 if (uio->uio_rw != UIO_READ)
308 panic("fifo_read mode");
309 #endif
310 if (uio_resid(uio) == 0)
311 return (0);
312
313 rflags = (ap->a_ioflag & IO_NDELAY) ? MSG_NBIO : 0;
314
315 startresid = uio_resid(uio);
316
317 /* fifo conformance - if we have a reader open on the fifo but no
318 * writers then we need to make sure we do not block. We do that by
319 * checking the receive buffer and if empty set error to EWOULDBLOCK.
320 * If error is set to EWOULDBLOCK we skip the call into soreceive
321 */
322 error = 0;
323 if (ap->a_vp->v_fifoinfo->fi_writers < 1) {
324 socket_lock(rso, 1);
325 error = (rso->so_rcv.sb_cc == 0) ? EWOULDBLOCK : 0;
326 socket_unlock(rso, 1);
327 }
328
329 /* skip soreceive to avoid blocking when we have no writers */
330 if (error != EWOULDBLOCK) {
331 error = soreceive(rso, (struct sockaddr **)0, uio, (struct mbuf **)0,
332 (struct mbuf **)0, &rflags);
333 }
334 else {
335 /* clear EWOULDBLOCK and return EOF (zero) */
336 error = 0;
337 }
338 /*
339 * Clear EOF indication after first such return.
340 */
341 if (uio_resid(uio) == startresid) {
342 socket_lock(rso, 1);
343 rso->so_state &= ~SS_CANTRCVMORE;
344 socket_unlock(rso, 1);
345 }
346 return (error);
347 }
348
349 /*
350 * Vnode op for write
351 */
352 int
353 fifo_write(struct vnop_write_args *ap)
354 {
355 struct socket *wso = ap->a_vp->v_fifoinfo->fi_writesock;
356 int error;
357
358 #if DIAGNOSTIC
359 if (ap->a_uio->uio_rw != UIO_WRITE)
360 panic("fifo_write mode");
361 #endif
362 error = sosend(wso, (struct sockaddr *)0, ap->a_uio, NULL,
363 (struct mbuf *)0, (ap->a_ioflag & IO_NDELAY) ? MSG_NBIO : 0);
364
365 return (error);
366 }
367
368 /*
369 * Device ioctl operation.
370 */
371 int
372 fifo_ioctl(struct vnop_ioctl_args *ap)
373 {
374 struct fileproc filetmp;
375 struct fileglob filefg;
376 int error;
377
378 if (ap->a_command == FIONBIO)
379 return (0);
380 bzero(&filetmp, sizeof(struct fileproc));
381 filetmp.f_fglob = &filefg;
382 if (ap->a_fflag & FREAD) {
383 filetmp.f_fglob->fg_data = (caddr_t)ap->a_vp->v_fifoinfo->fi_readsock;
384 error = soo_ioctl(&filetmp, ap->a_command, ap->a_data, ap->a_context);
385 if (error)
386 return (error);
387 }
388 if (ap->a_fflag & FWRITE) {
389 filetmp.f_fglob->fg_data = (caddr_t)ap->a_vp->v_fifoinfo->fi_writesock;
390 error = soo_ioctl(&filetmp, ap->a_command, ap->a_data, ap->a_context);
391 if (error)
392 return (error);
393 }
394 return (0);
395 }
396
397 int
398 fifo_select(struct vnop_select_args *ap)
399 {
400 struct fileproc filetmp;
401 struct fileglob filefg;
402 int ready;
403
404 bzero(&filetmp, sizeof(struct fileproc));
405 filetmp.f_fglob = &filefg;
406 if (ap->a_which & FREAD) {
407 filetmp.f_fglob->fg_data = (caddr_t)ap->a_vp->v_fifoinfo->fi_readsock;
408 ready = soo_select(&filetmp, ap->a_which, ap->a_wql, ap->a_context);
409 if (ready)
410 return (ready);
411 }
412 if (ap->a_which & FWRITE) {
413 filetmp.f_fglob->fg_data = (caddr_t)ap->a_vp->v_fifoinfo->fi_writesock;
414 ready = soo_select(&filetmp, ap->a_which, ap->a_wql, ap->a_context);
415 if (ready)
416 return (ready);
417 }
418 return (0);
419 }
420
421 int
422 fifo_inactive(__unused struct vnop_inactive_args *ap)
423 {
424 return (0);
425 }
426
427
428 /*
429 * Device close routine
430 */
431 int
432 fifo_close(struct vnop_close_args *ap)
433 {
434 return fifo_close_internal(ap->a_vp, ap->a_fflag, ap->a_context, 0);
435 }
436
437 int
438 fifo_close_internal(vnode_t vp, int fflag, __unused vfs_context_t context, int locked)
439 {
440 struct fifoinfo *fip = vp->v_fifoinfo;
441 int error1, error2;
442 struct socket *rso;
443 struct socket *wso;
444
445 if (!locked)
446 vnode_lock(vp);
447
448 if ((fip->fi_flags & FIFO_CREATED) == 0) {
449 if (!locked)
450 vnode_unlock(vp);
451 return(0);
452
453 }
454
455 if (fflag & FREAD) {
456 fip->fi_readers--;
457 if (fip->fi_readers == 0){
458 socket_lock(fip->fi_writesock, 1);
459 socantsendmore(fip->fi_writesock);
460 socket_unlock(fip->fi_writesock, 1);
461 }
462 }
463
464 if (fflag & FWRITE) {
465 fip->fi_writers--;
466 if (fip->fi_writers == 0) {
467 socket_lock(fip->fi_readsock, 1);
468 socantrcvmore(fip->fi_readsock);
469 socket_unlock(fip->fi_readsock, 1);
470 }
471 }
472 #if 0
473 if (vnode_isinuse_locked(vp, 0, 1)) {
474 if (!locked)
475 vnode_unlock(vp);
476 return (0);
477 }
478 #endif
479
480 if (fip->fi_writers || fip->fi_readers) {
481 if (!locked)
482 vnode_unlock(vp);
483 return (0);
484 }
485
486 wso = fip->fi_writesock;
487 rso = fip->fi_readsock;
488 fip->fi_readsock = NULL;
489 fip->fi_writesock = NULL;
490 fip->fi_flags &= ~FIFO_CREATED;
491 if (!locked)
492 vnode_unlock(vp);
493 error1 = soclose(rso);
494 error2 = soclose(wso);
495
496 if (error1)
497 return (error1);
498 return (error2);
499 }
500
501 /*
502 * Print out internal contents of a fifo vnode.
503 */
504 void
505 fifo_printinfo(struct vnode *vp)
506 {
507 struct fifoinfo *fip = vp->v_fifoinfo;
508
509 printf(", fifo with %ld readers and %ld writers",
510 fip->fi_readers, fip->fi_writers);
511 }
512
513 /*
514 * Return POSIX pathconf information applicable to fifo's.
515 */
516 int
517 fifo_pathconf(struct vnop_pathconf_args *ap)
518 {
519 switch (ap->a_name) {
520 case _PC_LINK_MAX:
521 *ap->a_retval = LINK_MAX;
522 return (0);
523 case _PC_PIPE_BUF:
524 *ap->a_retval = PIPE_BUF;
525 return (0);
526 case _PC_CHOWN_RESTRICTED:
527 *ap->a_retval = 200112; /* _POSIX_CHOWN_RESTRICTED */
528 return (0);
529 default:
530 return (EINVAL);
531 }
532 /* NOTREACHED */
533 }
534
535 /*
536 * Fifo failed operation
537 */
538 int
539 fifo_ebadf(__unused void *dummy)
540 {
541
542 return (EBADF);
543 }
544
545 /*
546 * Fifo advisory byte-level locks.
547 */
548 int
549 fifo_advlock(__unused struct vnop_advlock_args *ap)
550 {
551
552 return (ENOTSUP);
553 }
554
555
556 /* You'd certainly better have an iocount on the vnode! */
557 int
558 fifo_freespace(struct vnode *vp, long *count)
559 {
560 struct socket *rsock;
561 rsock = vp->v_fifoinfo->fi_readsock;
562 socket_lock(rsock, 1);
563 *count = sbspace(&rsock->so_rcv);
564 socket_unlock(rsock, 1);
565 return 0;
566 }
567
568 int
569 fifo_charcount(struct vnode *vp, int *count)
570 {
571 int mcount;
572 int err = sock_ioctl(vp->v_fifoinfo->fi_readsock, FIONREAD, (void*)&mcount);
573 if (err == 0) {
574 *count = mcount;
575 }
576 return err;
577 }