2 * Copyright (c) 2000-2012 Apple Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
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.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
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.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
28 /* Copyright (c) 1995 NeXT Computer, Inc. All Rights Reserved */
30 * Copyright (c) 1982, 1986, 1989, 1993
31 * The Regents of the University of California. All rights reserved.
32 * (c) UNIX System Laboratories, Inc.
33 * All or some portions of this file are derived from material licensed
34 * to the University of California by American Telephone and Telegraph
35 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
36 * the permission of UNIX System Laboratories, Inc.
38 * Redistribution and use in source and binary forms, with or without
39 * modification, are permitted provided that the following conditions
41 * 1. Redistributions of source code must retain the above copyright
42 * notice, this list of conditions and the following disclaimer.
43 * 2. Redistributions in binary form must reproduce the above copyright
44 * notice, this list of conditions and the following disclaimer in the
45 * documentation and/or other materials provided with the distribution.
46 * 3. All advertising materials mentioning features or use of this software
47 * must display the following acknowledgement:
48 * This product includes software developed by the University of
49 * California, Berkeley and its contributors.
50 * 4. Neither the name of the University nor the names of its contributors
51 * may be used to endorse or promote products derived from this software
52 * without specific prior written permission.
54 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
55 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
56 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
57 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
58 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
59 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
60 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
61 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
62 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
63 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
66 * @(#)vfs_vnops.c 8.14 (Berkeley) 6/15/95
70 * NOTICE: This file was modified by SPARTA, Inc. in 2005 to introduce
71 * support for mandatory and extensible security protections. This notice
72 * is included in support of clause 2.2 (b) of the Apple Public License,
76 #include <sys/param.h>
77 #include <sys/types.h>
78 #include <sys/systm.h>
79 #include <sys/kernel.h>
80 #include <sys/file_internal.h>
82 #include <sys/proc_internal.h>
83 #include <sys/kauth.h>
84 #include <sys/mount_internal.h>
85 #include <sys/namei.h>
86 #include <sys/vnode_internal.h>
87 #include <sys/ioctl.h>
89 /* Temporary workaround for ubc.h until <rdar://4714366 is resolved */
90 #define ubc_setcred ubc_setcred_deprecated
93 int ubc_setcred(struct vnode
*, struct proc
*);
96 #include <sys/fsevents.h>
97 #include <sys/kdebug.h>
98 #include <sys/xattr.h>
99 #include <sys/ubc_internal.h>
100 #include <sys/uio_internal.h>
101 #include <sys/resourcevar.h>
102 #include <sys/signalvar.h>
104 #include <vm/vm_kern.h>
105 #include <vm/vm_map.h>
107 #include <miscfs/specfs/specdev.h>
108 #include <miscfs/fifofs/fifo.h>
111 #include <security/mac_framework.h>
115 #include <sys/cprotect.h>
118 extern void sigpup_attach_vnode(vnode_t
); /* XXX */
120 static int vn_closefile(struct fileglob
*fp
, vfs_context_t ctx
);
121 static int vn_ioctl(struct fileproc
*fp
, u_long com
, caddr_t data
,
123 static int vn_read(struct fileproc
*fp
, struct uio
*uio
, int flags
,
125 static int vn_write(struct fileproc
*fp
, struct uio
*uio
, int flags
,
127 static int vn_select( struct fileproc
*fp
, int which
, void * wql
,
129 static int vn_kqfilt_add(struct fileproc
*fp
, struct knote
*kn
,
131 static void filt_vndetach(struct knote
*kn
);
132 static int filt_vnode(struct knote
*kn
, long hint
);
133 static int vn_open_auth_finish(vnode_t vp
, int fmode
, vfs_context_t ctx
);
135 static int vn_kqfilt_remove(struct vnode
*vp
, uintptr_t ident
,
139 const struct fileops vnops
= {
150 struct filterops vnode_filtops
= {
153 .f_detach
= filt_vndetach
,
154 .f_event
= filt_vnode
158 * Common code for vnode open operations.
159 * Check permissions, and call the VNOP_OPEN or VNOP_CREATE routine.
161 * XXX the profusion of interfaces here is probably a bad thing.
164 vn_open(struct nameidata
*ndp
, int fmode
, int cmode
)
166 return(vn_open_modflags(ndp
, &fmode
, cmode
));
170 vn_open_modflags(struct nameidata
*ndp
, int *fmodep
, int cmode
)
172 struct vnode_attr va
;
175 VATTR_SET(&va
, va_mode
, cmode
);
177 return(vn_open_auth(ndp
, fmodep
, &va
));
181 vn_open_auth_finish(vnode_t vp
, int fmode
, vfs_context_t ctx
)
185 if ((error
= vnode_ref_ext(vp
, fmode
, 0)) != 0) {
189 /* Call out to allow 3rd party notification of open.
190 * Ignore result of kauth_authorize_fileop call.
193 mac_vnode_notify_open(ctx
, vp
, fmode
);
195 kauth_authorize_fileop(vfs_context_ucred(ctx
), KAUTH_FILEOP_OPEN
,
198 sigpup_attach_vnode(vp
);
208 * May do nameidone() to allow safely adding an FSEvent. Cue off of ni_dvp to
209 * determine whether that has happened.
212 vn_open_auth_do_create(struct nameidata
*ndp
, struct vnode_attr
*vap
, int fmode
, boolean_t
*did_create
, boolean_t
*did_open
, vfs_context_t ctx
)
215 vnode_t dvp
= ndp
->ni_dvp
;
220 batched
= vnode_compound_open_available(ndp
->ni_dvp
);
223 VATTR_SET(vap
, va_type
, VREG
);
225 vap
->va_vaflags
|= VA_EXCLUSIVE
;
228 if (ndp
->ni_cnd
.cn_flags
& CN_WANTSRSRCFORK
) {
229 if ((error
= vn_authorize_create(dvp
, &ndp
->ni_cnd
, vap
, ctx
, NULL
)) != 0)
231 if ((error
= vnode_makenamedstream(dvp
, &ndp
->ni_vp
, XATTR_RESOURCEFORK_NAME
, 0, ctx
)) != 0)
237 if ((error
= vn_authorize_create(dvp
, &ndp
->ni_cnd
, vap
, ctx
, NULL
)) != 0)
241 error
= vn_create(dvp
, &ndp
->ni_vp
, ndp
, vap
, VN_CREATE_DOOPEN
, fmode
, &status
, ctx
);
244 *did_create
= (status
& COMPOUND_OPEN_STATUS_DID_CREATE
) ? TRUE
: FALSE
;
249 if (error
== EKEEPLOOKING
) {
251 panic("EKEEPLOOKING, but we did a create?");
254 panic("EKEEPLOOKING from filesystem that doesn't support compound vnops?");
256 if ((ndp
->ni_flag
& NAMEI_CONTLOOKUP
) == 0) {
257 panic("EKEEPLOOKING, but continue flag not set?");
261 * Do NOT drop the dvp: we need everything to continue the lookup.
267 *did_create
= (status
& COMPOUND_OPEN_STATUS_DID_CREATE
) ? 1 : 0;
280 int update_flags
= 0;
282 // Make sure the name & parent pointers are hooked up
283 if (vp
->v_name
== NULL
)
284 update_flags
|= VNODE_UPDATE_NAME
;
285 if (vp
->v_parent
== NULLVP
)
286 update_flags
|= VNODE_UPDATE_PARENT
;
289 vnode_update_identity(vp
, dvp
, ndp
->ni_cnd
.cn_nameptr
, ndp
->ni_cnd
.cn_namelen
, ndp
->ni_cnd
.cn_hash
, update_flags
);
292 ndp
->ni_dvp
= NULLVP
;
295 if (need_fsevent(FSE_CREATE_FILE
, vp
)) {
296 add_fsevent(FSE_CREATE_FILE
, ctx
,
303 if (ndp
->ni_dvp
!= NULLVP
) {
305 ndp
->ni_dvp
= NULLVP
;
312 * Open a file with authorization, updating the contents of the structures
313 * pointed to by ndp, fmodep, and vap as necessary to perform the requested
314 * operation. This function is used for both opens of existing files, and
315 * creation of new files.
317 * Parameters: ndp The nami data pointer describing the
319 * fmodep A pointer to an int containg the mode
320 * information to be used for the open
321 * vap A pointer to the vnode attribute
322 * descriptor to be used for the open
324 * Indirect: * Contents of the data structures pointed
325 * to by the parameters are modified as
326 * necessary to the requested operation.
331 * Notes: The kauth_filesec_t in 'vap', if any, is in host byte order.
333 * The contents of '*ndp' will be modified, based on the other
334 * arguments to this function, and to return file and directory
335 * data necessary to satisfy the requested operation.
337 * If the file does not exist and we are creating it, then the
338 * O_TRUNC flag will be cleared in '*fmodep' to indicate to the
339 * caller that the file was not truncated.
341 * If the file exists and the O_EXCL flag was not specified, then
342 * the O_CREAT flag will be cleared in '*fmodep' to indicate to
343 * the caller that the existing file was merely opened rather
346 * The contents of '*vap' will be modified as necessary to
347 * complete the operation, including setting of supported
348 * attribute, clearing of fields containing unsupported attributes
349 * in the request, if the request proceeds without them, etc..
351 * XXX: This function is too complicated in actings on its arguments
353 * XXX: We should enummerate the possible errno values here, and where
354 * in the code they originated.
357 vn_open_auth(struct nameidata
*ndp
, int *fmodep
, struct vnode_attr
*vap
)
361 vfs_context_t ctx
= ndp
->ni_cnd
.cn_context
;
364 uint32_t origcnflags
;
365 boolean_t did_create
;
367 boolean_t need_vnop_open
;
369 boolean_t ref_failed
;
376 need_vnop_open
= TRUE
;
379 origcnflags
= ndp
->ni_cnd
.cn_flags
;
384 if (fmode
& O_CREAT
) {
385 if ( (fmode
& O_DIRECTORY
) ) {
389 ndp
->ni_cnd
.cn_nameiop
= CREATE
;
391 ndp
->ni_op
= OP_LINK
;
393 /* Inherit USEDVP, vnode_open() supported flags only */
394 ndp
->ni_cnd
.cn_flags
&= (USEDVP
| NOCROSSMOUNT
| DOWHITEOUT
);
395 ndp
->ni_cnd
.cn_flags
|= LOCKPARENT
| LOCKLEAF
| AUDITVNPATH1
;
396 ndp
->ni_flag
= NAMEI_COMPOUNDOPEN
;
398 /* open calls are allowed for resource forks. */
399 ndp
->ni_cnd
.cn_flags
|= CN_ALLOWRSRCFORK
;
401 if ((fmode
& O_EXCL
) == 0 && (fmode
& O_NOFOLLOW
) == 0 && (origcnflags
& FOLLOW
) != 0)
402 ndp
->ni_cnd
.cn_flags
|= FOLLOW
;
404 continue_create_lookup
:
405 if ( (error
= namei(ndp
)) )
411 batched
= vnode_compound_open_available(dvp
);
413 /* not found, create */
415 /* must have attributes for a new file */
421 * Attempt a create. For a system supporting compound VNOPs, we may
422 * find an existing file or create one; in either case, we will already
423 * have the file open and no VNOP_OPEN() will be needed.
425 error
= vn_open_auth_do_create(ndp
, vap
, fmode
, &did_create
, &did_open
, ctx
);
431 * Detected a node that the filesystem couldn't handle. Don't call
432 * nameidone() yet, because we need that path buffer.
434 if (error
== EKEEPLOOKING
) {
436 panic("EKEEPLOOKING from a filesystem that doesn't support compound VNOPs?");
438 goto continue_create_lookup
;
443 panic("Shouldn't have a dvp here.");
448 * Check for a creation or unlink race.
450 if (((error
== EEXIST
) && !(fmode
& O_EXCL
)) ||
451 ((error
== ENOENT
) && (fmode
& O_CREAT
))){
459 need_vnop_open
= !did_open
;
466 * We have a vnode. Use compound open if available
467 * or else fall through to "traditional" path. Note: can't
468 * do a compound open for root, because the parent belongs
471 if (error
== 0 && batched
&& (vnode_mount(dvp
) == vnode_mount(vp
))) {
472 error
= VNOP_COMPOUND_OPEN(dvp
, &ndp
->ni_vp
, ndp
, 0, fmode
, NULL
, NULL
, ctx
);
476 need_vnop_open
= FALSE
;
477 } else if (error
== EKEEPLOOKING
) {
478 if ((ndp
->ni_flag
& NAMEI_CONTLOOKUP
) == 0) {
479 panic("EKEEPLOOKING, but continue flag not set?");
481 goto continue_create_lookup
;
486 ndp
->ni_dvp
= NULLVP
;
501 ndp
->ni_cnd
.cn_nameiop
= LOOKUP
;
502 /* Inherit USEDVP, vnode_open() supported flags only */
503 ndp
->ni_cnd
.cn_flags
&= (USEDVP
| NOCROSSMOUNT
| DOWHITEOUT
);
504 ndp
->ni_cnd
.cn_flags
|= FOLLOW
| LOCKLEAF
| AUDITVNPATH1
| WANTPARENT
;
506 /* open calls are allowed for resource forks. */
507 ndp
->ni_cnd
.cn_flags
|= CN_ALLOWRSRCFORK
;
509 ndp
->ni_flag
= NAMEI_COMPOUNDOPEN
;
511 /* preserve NOFOLLOW from vnode_open() */
512 if (fmode
& O_NOFOLLOW
|| fmode
& O_SYMLINK
|| (origcnflags
& FOLLOW
) == 0) {
513 ndp
->ni_cnd
.cn_flags
&= ~FOLLOW
;
516 /* Do a lookup, possibly going directly to filesystem for compound operation */
518 if ( (error
= namei(ndp
)) )
523 /* Check for batched lookup-open */
524 batched
= vnode_compound_open_available(dvp
);
525 if (batched
&& ((vp
== NULLVP
) || (vnode_mount(dvp
) == vnode_mount(vp
)))) {
526 error
= VNOP_COMPOUND_OPEN(dvp
, &ndp
->ni_vp
, ndp
, 0, fmode
, NULL
, NULL
, ctx
);
529 need_vnop_open
= FALSE
;
530 } else if (error
== EKEEPLOOKING
) {
531 if ((ndp
->ni_flag
& NAMEI_CONTLOOKUP
) == 0) {
532 panic("EKEEPLOOKING, but continue flag not set?");
536 } while (error
== EKEEPLOOKING
);
540 ndp
->ni_dvp
= NULLVP
;
548 * By this point, nameidone() is called, dvp iocount is dropped,
549 * and dvp pointer is cleared.
551 if (ndp
->ni_dvp
!= NULLVP
) {
552 panic("Haven't cleaned up adequately in vn_open_auth()");
556 * Expect to use this code for filesystems without compound VNOPs, for the root
557 * of a filesystem, which can't be "looked up" in the sense of VNOP_LOOKUP(),
558 * and for shadow files, which do not live on the same filesystems as their "parents."
560 if (need_vnop_open
) {
561 if (batched
&& !vnode_isvroot(vp
) && !vnode_isnamedstream(vp
)) {
562 panic("Why am I trying to use VNOP_OPEN() on anything other than the root or a named stream?");
566 error
= vn_authorize_open_existing(vp
, &ndp
->ni_cnd
, fmode
, ctx
, NULL
);
574 * Perform any content protection access checks prior to calling
575 * into the filesystem, if the raw encrypted mode was not
578 * If the va_dataprotect_flags are NOT active, or if they are,
579 * but they do not have the VA_DP_RAWENCRYPTED bit set, then we need
580 * to perform the checks.
582 if (!(VATTR_IS_ACTIVE (vap
, va_dataprotect_flags
)) ||
583 ((vap
->va_dataprotect_flags
& VA_DP_RAWENCRYPTED
) == 0)) {
584 error
= cp_handle_open (vp
, fmode
);
591 error
= VNOP_OPEN(vp
, fmode
, ctx
);
595 need_vnop_open
= FALSE
;
598 // if the vnode is tagged VOPENEVT and the current process
599 // has the P_CHECKOPENEVT flag set, then we or in the O_EVTONLY
600 // flag to the open mode so that this open won't count against
601 // the vnode when carbon delete() does a vnode_isinuse() to see
602 // if a file is currently in use. this allows spotlight
603 // importers to not interfere with carbon apps that depend on
604 // the no-delete-if-busy semantics of carbon delete().
606 if (!did_create
&& (vp
->v_flag
& VOPENEVT
) && (current_proc()->p_flag
& P_CHECKOPENEVT
)) {
611 * Grab reference, etc.
613 error
= vn_open_auth_finish(vp
, fmode
, ctx
);
619 /* Compound VNOP open is responsible for doing the truncate */
620 if (batched
|| did_create
)
627 /* Opened either explicitly or by a batched create */
628 if (!need_vnop_open
) {
629 VNOP_CLOSE(vp
, fmode
, ctx
);
635 /* Aggressively recycle shadow files if we error'd out during open() */
636 if ((vnode_isnamedstream(vp
)) &&
637 (vp
->v_parent
!= NULLVP
) &&
638 (vnode_isshadow(vp
))) {
644 * Check for a race against unlink. We had a vnode
645 * but according to vnode_authorize or VNOP_OPEN it
648 * EREDRIVEOPEN: means that we were hit by the tty allocation race.
650 if (((error
== ENOENT
) && (*fmodep
& O_CREAT
)) || (error
== EREDRIVEOPEN
) || ref_failed
) {
659 #if vn_access_DEPRECATED
661 * Authorize an action against a vnode. This has been the canonical way to
662 * ensure that the credential/process/etc. referenced by a vfs_context
663 * is granted the rights called out in 'mode' against the vnode 'vp'.
665 * Unfortunately, the use of VREAD/VWRITE/VEXEC makes it very difficult
666 * to add support for more rights. As such, this interface will be deprecated
667 * and callers will use vnode_authorize instead.
670 vn_access(vnode_t vp
, int mode
, vfs_context_t context
)
672 kauth_action_t action
;
676 action
|= KAUTH_VNODE_READ_DATA
;
678 action
|= KAUTH_VNODE_WRITE_DATA
;
680 action
|= KAUTH_VNODE_EXECUTE
;
682 return(vnode_authorize(vp
, NULL
, action
, context
));
684 #endif /* vn_access_DEPRECATED */
690 vn_close(struct vnode
*vp
, int flags
, vfs_context_t ctx
)
696 /* Sync data from resource fork shadow file if needed. */
697 if ((vp
->v_flag
& VISNAMEDSTREAM
) &&
698 (vp
->v_parent
!= NULLVP
) &&
699 vnode_isshadow(vp
)) {
700 if (flags
& FWASWRITTEN
) {
701 flusherror
= vnode_flushnamedstream(vp
->v_parent
, vp
, ctx
);
706 /* work around for foxhound */
707 if (vnode_isspec(vp
))
708 (void)vnode_rele_ext(vp
, flags
, 0);
710 error
= VNOP_CLOSE(vp
, flags
, ctx
);
713 if (flags
& FWASWRITTEN
) {
714 if (need_fsevent(FSE_CONTENT_MODIFIED
, vp
)) {
715 add_fsevent(FSE_CONTENT_MODIFIED
, ctx
,
722 if (!vnode_isspec(vp
))
723 (void)vnode_rele_ext(vp
, flags
, 0);
737 off_t swap_count
, this_count
;
738 off_t file_end
, read_end
;
743 * Reading from a swap file will get you zeroes.
748 swap_count
= uio_resid(uio
);
750 file_end
= ubc_getsize(vp
);
751 read_end
= uio
->uio_offset
+ uio_resid(uio
);
752 if (uio
->uio_offset
>= file_end
) {
753 /* uio starts after end of file: nothing to read */
755 } else if (read_end
> file_end
) {
756 /* uio extends beyond end of file: stop before that */
757 swap_count
-= (read_end
- file_end
);
760 while (swap_count
> 0) {
761 if (my_swap_page
== NULL
) {
762 MALLOC(my_swap_page
, char *, PAGE_SIZE
,
764 memset(my_swap_page
, '\0', PAGE_SIZE
);
765 /* add an end-of-line to keep line counters happy */
766 my_swap_page
[PAGE_SIZE
-1] = '\n';
768 this_count
= swap_count
;
769 if (this_count
> PAGE_SIZE
) {
770 this_count
= PAGE_SIZE
;
773 prev_resid
= uio_resid(uio
);
774 error
= uiomove((caddr_t
) my_swap_page
,
780 swap_count
-= (prev_resid
- uio_resid(uio
));
782 if (my_swap_page
!= NULL
) {
783 FREE(my_swap_page
, M_TEMP
);
790 * Package up an I/O request on a vnode into a uio and do it.
808 result
= vn_rdwr_64(rw
,
810 (uint64_t)(uintptr_t)base
,
819 /* "resid" should be bounded above by "len," which is an int */
820 if (aresid
!= NULL
) {
843 struct vfs_context context
;
845 char uio_buf
[ UIO_SIZEOF(1) ];
847 context
.vc_thread
= current_thread();
848 context
.vc_ucred
= cred
;
850 if (UIO_SEG_IS_USER_SPACE(segflg
)) {
851 spacetype
= proc_is64bit(p
) ? UIO_USERSPACE64
: UIO_USERSPACE32
;
854 spacetype
= UIO_SYSSPACE
;
856 auio
= uio_createwithbuffer(1, offset
, spacetype
, rw
,
857 &uio_buf
[0], sizeof(uio_buf
));
858 uio_addiov(auio
, base
, len
);
862 * IO_NOAUTH should be re-examined.
863 * Likely that mediation should be performed in caller.
865 if ((ioflg
& IO_NOAUTH
) == 0) {
866 /* passed cred is fp->f_cred */
868 error
= mac_vnode_check_read(&context
, cred
, vp
);
870 error
= mac_vnode_check_write(&context
, cred
, vp
);
875 if (rw
== UIO_READ
) {
876 if (vnode_isswap(vp
) && ((ioflg
& IO_SWAP_DISPATCH
) == 0)) {
877 error
= vn_read_swapfile(vp
, auio
);
879 error
= VNOP_READ(vp
, auio
, ioflg
, &context
);
882 error
= VNOP_WRITE(vp
, auio
, ioflg
, &context
);
887 *aresid
= uio_resid(auio
);
889 if (uio_resid(auio
) && error
== 0)
895 * File table vnode read routine.
898 vn_read(struct fileproc
*fp
, struct uio
*uio
, int flags
, vfs_context_t ctx
)
905 vp
= (struct vnode
*)fp
->f_fglob
->fg_data
;
906 if ( (error
= vnode_getwithref(vp
)) ) {
911 error
= mac_vnode_check_read(ctx
, vfs_context_ucred(ctx
), vp
);
918 /* This signals to VNOP handlers that this read came from a file table read */
919 ioflag
= IO_SYSCALL_DISPATCH
;
921 if (fp
->f_fglob
->fg_flag
& FNONBLOCK
)
923 if ((fp
->f_fglob
->fg_flag
& FNOCACHE
) || vnode_isnocache(vp
))
924 ioflag
|= IO_NOCACHE
;
925 if (fp
->f_fglob
->fg_flag
& FENCRYPTED
) {
926 ioflag
|= IO_ENCRYPTED
;
928 if (fp
->f_fglob
->fg_flag
& FNORDAHEAD
)
931 if ((flags
& FOF_OFFSET
) == 0)
932 uio
->uio_offset
= fp
->f_fglob
->fg_offset
;
933 count
= uio_resid(uio
);
935 if (vnode_isswap(vp
)) {
936 /* special case for swap files */
937 error
= vn_read_swapfile(vp
, uio
);
939 error
= VNOP_READ(vp
, uio
, ioflag
, ctx
);
941 if ((flags
& FOF_OFFSET
) == 0)
942 fp
->f_fglob
->fg_offset
+= count
- uio_resid(uio
);
950 * File table vnode write routine.
953 vn_write(struct fileproc
*fp
, struct uio
*uio
, int flags
, vfs_context_t ctx
)
960 int residcount
, oldcount
;
961 proc_t p
= vfs_context_proc(ctx
);
964 vp
= (struct vnode
*)fp
->f_fglob
->fg_data
;
965 if ( (error
= vnode_getwithref(vp
)) ) {
970 error
= mac_vnode_check_write(ctx
, vfs_context_ucred(ctx
), vp
);
978 * IO_SYSCALL_DISPATCH signals to VNOP handlers that this write came from
981 ioflag
= (IO_UNIT
| IO_SYSCALL_DISPATCH
);
983 if (vp
->v_type
== VREG
&& (fp
->f_fglob
->fg_flag
& O_APPEND
))
985 if (fp
->f_fglob
->fg_flag
& FNONBLOCK
)
987 if ((fp
->f_fglob
->fg_flag
& FNOCACHE
) || vnode_isnocache(vp
))
988 ioflag
|= IO_NOCACHE
;
989 if (fp
->f_fglob
->fg_flag
& FNODIRECT
)
990 ioflag
|= IO_NODIRECT
;
991 if (fp
->f_fglob
->fg_flag
& FSINGLE_WRITER
)
992 ioflag
|= IO_SINGLE_WRITER
;
995 * Treat synchronous mounts and O_FSYNC on the fd as equivalent.
997 * XXX We treat O_DSYNC as O_FSYNC for now, since we can not delay
998 * XXX the non-essential metadata without some additional VFS work;
999 * XXX the intent at this point is to plumb the interface for it.
1001 if ((fp
->f_fglob
->fg_flag
& (O_FSYNC
|O_DSYNC
)) ||
1002 (vp
->v_mount
&& (vp
->v_mount
->mnt_flag
& MNT_SYNCHRONOUS
))) {
1006 if ((flags
& FOF_OFFSET
) == 0) {
1007 uio
->uio_offset
= fp
->f_fglob
->fg_offset
;
1008 count
= uio_resid(uio
);
1010 if (((flags
& FOF_OFFSET
) == 0) &&
1011 vfs_context_proc(ctx
) && (vp
->v_type
== VREG
) &&
1012 (((rlim_t
)(uio
->uio_offset
+ uio_resid(uio
)) > p
->p_rlimit
[RLIMIT_FSIZE
].rlim_cur
) ||
1013 ((rlim_t
)uio_resid(uio
) > (p
->p_rlimit
[RLIMIT_FSIZE
].rlim_cur
- uio
->uio_offset
)))) {
1015 * If the requested residual would cause us to go past the
1016 * administrative limit, then we need to adjust the residual
1017 * down to cause fewer bytes than requested to be written. If
1018 * we can't do that (e.g. the residual is already 1 byte),
1019 * then we fail the write with EFBIG.
1021 residcount
= uio_resid(uio
);
1022 if ((rlim_t
)(uio
->uio_offset
+ uio_resid(uio
)) > p
->p_rlimit
[RLIMIT_FSIZE
].rlim_cur
) {
1023 clippedsize
= (uio
->uio_offset
+ uio_resid(uio
)) - p
->p_rlimit
[RLIMIT_FSIZE
].rlim_cur
;
1024 } else if ((rlim_t
)uio_resid(uio
) > (p
->p_rlimit
[RLIMIT_FSIZE
].rlim_cur
- uio
->uio_offset
)) {
1025 clippedsize
= (p
->p_rlimit
[RLIMIT_FSIZE
].rlim_cur
- uio
->uio_offset
);
1027 if (clippedsize
>= residcount
) {
1028 psignal(p
, SIGXFSZ
);
1033 uio_setresid(uio
, residcount
-clippedsize
);
1035 if ((flags
& FOF_OFFSET
) != 0) {
1036 /* for pwrite, append should be ignored */
1037 ioflag
&= ~IO_APPEND
;
1038 if (p
&& (vp
->v_type
== VREG
) &&
1039 ((rlim_t
)uio
->uio_offset
>= p
->p_rlimit
[RLIMIT_FSIZE
].rlim_cur
)) {
1040 psignal(p
, SIGXFSZ
);
1044 if (p
&& (vp
->v_type
== VREG
) &&
1045 ((rlim_t
)(uio
->uio_offset
+ uio_resid(uio
)) > p
->p_rlimit
[RLIMIT_FSIZE
].rlim_cur
)) {
1046 //Debugger("vn_bwrite:overstepping the bounds");
1047 residcount
= uio_resid(uio
);
1048 clippedsize
= (uio
->uio_offset
+ uio_resid(uio
)) - p
->p_rlimit
[RLIMIT_FSIZE
].rlim_cur
;
1050 uio_setresid(uio
, residcount
-clippedsize
);
1054 error
= VNOP_WRITE(vp
, uio
, ioflag
, ctx
);
1057 oldcount
= uio_resid(uio
);
1058 uio_setresid(uio
, oldcount
+ clippedsize
);
1061 if ((flags
& FOF_OFFSET
) == 0) {
1062 if (ioflag
& IO_APPEND
)
1063 fp
->f_fglob
->fg_offset
= uio
->uio_offset
;
1065 fp
->f_fglob
->fg_offset
+= count
- uio_resid(uio
);
1069 * Set the credentials on successful writes
1071 if ((error
== 0) && (vp
->v_tag
== VT_NFS
) && (UBCINFOEXISTS(vp
))) {
1073 * When called from aio subsystem, we only have the proc from
1074 * which to get the credential, at this point, so use that
1075 * instead. This means aio functions are incompatible with
1076 * per-thread credentials (aio operations are proxied). We
1077 * can't easily correct the aio vs. settid race in this case
1078 * anyway, so we disallow it.
1080 if ((flags
& FOF_PCRED
) == 0) {
1081 ubc_setthreadcred(vp
, p
, current_thread());
1086 (void)vnode_put(vp
);
1091 * File table vnode stat routine.
1093 * Returns: 0 Success
1099 vn_stat_noauth(struct vnode
*vp
, void *sbptr
, kauth_filesec_t
*xsec
, int isstat64
, vfs_context_t ctx
)
1101 struct vnode_attr va
;
1104 kauth_filesec_t fsec
;
1105 struct stat
*sb
= (struct stat
*)0; /* warning avoidance ; protected by isstat64 */
1106 struct stat64
* sb64
= (struct stat64
*)0; /* warning avoidance ; protected by isstat64 */
1109 sb64
= (struct stat64
*)sbptr
;
1111 sb
= (struct stat
*)sbptr
;
1112 memset(&va
, 0, sizeof(va
));
1114 VATTR_WANTED(&va
, va_fsid
);
1115 VATTR_WANTED(&va
, va_fileid
);
1116 VATTR_WANTED(&va
, va_mode
);
1117 VATTR_WANTED(&va
, va_type
);
1118 VATTR_WANTED(&va
, va_nlink
);
1119 VATTR_WANTED(&va
, va_uid
);
1120 VATTR_WANTED(&va
, va_gid
);
1121 VATTR_WANTED(&va
, va_rdev
);
1122 VATTR_WANTED(&va
, va_data_size
);
1123 VATTR_WANTED(&va
, va_access_time
);
1124 VATTR_WANTED(&va
, va_modify_time
);
1125 VATTR_WANTED(&va
, va_change_time
);
1126 VATTR_WANTED(&va
, va_create_time
);
1127 VATTR_WANTED(&va
, va_flags
);
1128 VATTR_WANTED(&va
, va_gen
);
1129 VATTR_WANTED(&va
, va_iosize
);
1130 /* lower layers will synthesise va_total_alloc from va_data_size if required */
1131 VATTR_WANTED(&va
, va_total_alloc
);
1133 VATTR_WANTED(&va
, va_uuuid
);
1134 VATTR_WANTED(&va
, va_guuid
);
1135 VATTR_WANTED(&va
, va_acl
);
1137 error
= vnode_getattr(vp
, &va
, ctx
);
1141 * Copy from vattr table
1143 if (isstat64
!= 0) {
1144 sb64
->st_dev
= va
.va_fsid
;
1145 sb64
->st_ino
= (ino64_t
)va
.va_fileid
;
1148 sb
->st_dev
= va
.va_fsid
;
1149 sb
->st_ino
= (ino_t
)va
.va_fileid
;
1152 switch (vp
->v_type
) {
1178 if (isstat64
!= 0) {
1179 sb64
->st_mode
= mode
;
1180 sb64
->st_nlink
= VATTR_IS_SUPPORTED(&va
, va_nlink
) ? (u_int16_t
)va
.va_nlink
: 1;
1181 sb64
->st_uid
= va
.va_uid
;
1182 sb64
->st_gid
= va
.va_gid
;
1183 sb64
->st_rdev
= va
.va_rdev
;
1184 sb64
->st_size
= va
.va_data_size
;
1185 sb64
->st_atimespec
= va
.va_access_time
;
1186 sb64
->st_mtimespec
= va
.va_modify_time
;
1187 sb64
->st_ctimespec
= va
.va_change_time
;
1188 sb64
->st_birthtimespec
=
1189 VATTR_IS_SUPPORTED(&va
, va_create_time
) ? va
.va_create_time
: va
.va_change_time
;
1190 sb64
->st_blksize
= va
.va_iosize
;
1191 sb64
->st_flags
= va
.va_flags
;
1192 sb64
->st_blocks
= roundup(va
.va_total_alloc
, 512) / 512;
1195 sb
->st_nlink
= VATTR_IS_SUPPORTED(&va
, va_nlink
) ? (u_int16_t
)va
.va_nlink
: 1;
1196 sb
->st_uid
= va
.va_uid
;
1197 sb
->st_gid
= va
.va_gid
;
1198 sb
->st_rdev
= va
.va_rdev
;
1199 sb
->st_size
= va
.va_data_size
;
1200 sb
->st_atimespec
= va
.va_access_time
;
1201 sb
->st_mtimespec
= va
.va_modify_time
;
1202 sb
->st_ctimespec
= va
.va_change_time
;
1203 sb
->st_blksize
= va
.va_iosize
;
1204 sb
->st_flags
= va
.va_flags
;
1205 sb
->st_blocks
= roundup(va
.va_total_alloc
, 512) / 512;
1208 /* if we're interested in extended security data and we got an ACL */
1210 if (!VATTR_IS_SUPPORTED(&va
, va_acl
) &&
1211 !VATTR_IS_SUPPORTED(&va
, va_uuuid
) &&
1212 !VATTR_IS_SUPPORTED(&va
, va_guuid
)) {
1213 *xsec
= KAUTH_FILESEC_NONE
;
1216 if (VATTR_IS_SUPPORTED(&va
, va_acl
) && (va
.va_acl
!= NULL
)) {
1217 fsec
= kauth_filesec_alloc(va
.va_acl
->acl_entrycount
);
1219 fsec
= kauth_filesec_alloc(0);
1225 fsec
->fsec_magic
= KAUTH_FILESEC_MAGIC
;
1226 if (VATTR_IS_SUPPORTED(&va
, va_uuuid
)) {
1227 fsec
->fsec_owner
= va
.va_uuuid
;
1229 fsec
->fsec_owner
= kauth_null_guid
;
1231 if (VATTR_IS_SUPPORTED(&va
, va_guuid
)) {
1232 fsec
->fsec_group
= va
.va_guuid
;
1234 fsec
->fsec_group
= kauth_null_guid
;
1236 if (VATTR_IS_SUPPORTED(&va
, va_acl
) && (va
.va_acl
!= NULL
)) {
1237 bcopy(va
.va_acl
, &(fsec
->fsec_acl
), KAUTH_ACL_COPYSIZE(va
.va_acl
));
1239 fsec
->fsec_acl
.acl_entrycount
= KAUTH_FILESEC_NOACL
;
1245 /* Do not give the generation number out to unpriviledged users */
1246 if (va
.va_gen
&& !vfs_context_issuser(ctx
)) {
1253 sb64
->st_gen
= va
.va_gen
;
1255 sb
->st_gen
= va
.va_gen
;
1260 if (VATTR_IS_SUPPORTED(&va
, va_acl
) && va
.va_acl
!= NULL
)
1261 kauth_acl_free(va
.va_acl
);
1266 vn_stat(struct vnode
*vp
, void *sb
, kauth_filesec_t
*xsec
, int isstat64
, vfs_context_t ctx
)
1271 error
= mac_vnode_check_stat(ctx
, NOCRED
, vp
);
1277 if ((error
= vnode_authorize(vp
, NULL
, KAUTH_VNODE_READ_ATTRIBUTES
| KAUTH_VNODE_READ_SECURITY
, ctx
)) != 0)
1281 return(vn_stat_noauth(vp
, sb
, xsec
, isstat64
, ctx
));
1286 * File table vnode ioctl routine.
1289 vn_ioctl(struct fileproc
*fp
, u_long com
, caddr_t data
, vfs_context_t ctx
)
1291 struct vnode
*vp
= ((struct vnode
*)fp
->f_fglob
->fg_data
);
1294 struct vnode
*ttyvp
;
1296 struct session
* sessp
;
1298 if ( (error
= vnode_getwithref(vp
)) ) {
1303 error
= mac_vnode_check_ioctl(ctx
, vp
, com
);
1308 switch (vp
->v_type
) {
1311 if (com
== FIONREAD
) {
1312 if ((error
= vnode_size(vp
, &file_size
, ctx
)) != 0)
1314 *(int *)data
= file_size
- fp
->f_fglob
->fg_offset
;
1317 if (com
== FIONBIO
|| com
== FIOASYNC
) { /* XXX */
1330 /* Should not be able to set block size from user space */
1331 if (com
== DKIOCSETBLOCKSIZE
) {
1336 if (com
== FIODTYPE
) {
1337 if (vp
->v_type
== VBLK
) {
1338 if (major(vp
->v_rdev
) >= nblkdev
) {
1342 *(int *)data
= bdevsw
[major(vp
->v_rdev
)].d_type
;
1344 } else if (vp
->v_type
== VCHR
) {
1345 if (major(vp
->v_rdev
) >= nchrdev
) {
1349 *(int *)data
= cdevsw
[major(vp
->v_rdev
)].d_type
;
1356 error
= VNOP_IOCTL(vp
, com
, data
, fp
->f_fglob
->fg_flag
, ctx
);
1358 if (error
== 0 && com
== TIOCSCTTY
) {
1359 error
= vnode_ref_ext(vp
, 0, VNODE_REF_FORCE
);
1361 panic("vnode_ref_ext() failed despite VNODE_REF_FORCE?!");
1364 funnel_state
= thread_funnel_set(kernel_flock
, TRUE
);
1365 sessp
= proc_session(vfs_context_proc(ctx
));
1367 session_lock(sessp
);
1368 ttyvp
= sessp
->s_ttyvp
;
1369 sessp
->s_ttyvp
= vp
;
1370 sessp
->s_ttyvid
= vnode_vid(vp
);
1371 session_unlock(sessp
);
1372 session_rele(sessp
);
1373 thread_funnel_set(kernel_flock
, funnel_state
);
1380 (void)vnode_put(vp
);
1385 * File table vnode select routine.
1388 vn_select(struct fileproc
*fp
, int which
, void *wql
, __unused vfs_context_t ctx
)
1391 struct vnode
* vp
= (struct vnode
*)fp
->f_fglob
->fg_data
;
1392 struct vfs_context context
;
1394 if ( (error
= vnode_getwithref(vp
)) == 0 ) {
1395 context
.vc_thread
= current_thread();
1396 context
.vc_ucred
= fp
->f_fglob
->fg_cred
;
1400 * XXX We should use a per thread credential here; minimally,
1401 * XXX the process credential should have a persistent
1402 * XXX reference on it before being passed in here.
1404 error
= mac_vnode_check_select(ctx
, vp
, which
);
1407 error
= VNOP_SELECT(vp
, which
, fp
->f_fglob
->fg_flag
, wql
, ctx
);
1409 (void)vnode_put(vp
);
1416 * File table vnode close routine.
1419 vn_closefile(struct fileglob
*fg
, vfs_context_t ctx
)
1421 struct vnode
*vp
= (struct vnode
*)fg
->fg_data
;
1425 if ( (error
= vnode_getwithref(vp
)) == 0 ) {
1427 if ((fg
->fg_flag
& FHASLOCK
) &&
1428 FILEGLOB_DTYPE(fg
) == DTYPE_VNODE
) {
1429 lf
.l_whence
= SEEK_SET
;
1432 lf
.l_type
= F_UNLCK
;
1434 (void)VNOP_ADVLOCK(vp
, (caddr_t
)fg
, F_UNLCK
, &lf
, F_FLOCK
, ctx
, NULL
);
1436 error
= vn_close(vp
, fg
->fg_flag
, ctx
);
1438 (void)vnode_put(vp
);
1444 * Returns: 0 Success
1448 vn_pathconf(vnode_t vp
, int name
, int32_t *retval
, vfs_context_t ctx
)
1451 struct vfs_attr vfa
;
1454 case _PC_EXTENDED_SECURITY_NP
:
1455 *retval
= vfs_extendedsecurity(vnode_mount(vp
)) ? 1 : 0;
1457 case _PC_AUTH_OPAQUE_NP
:
1458 *retval
= vfs_authopaque(vnode_mount(vp
));
1460 case _PC_2_SYMLINKS
:
1461 *retval
= 1; /* XXX NOTSUP on MSDOS, etc. */
1463 case _PC_ALLOC_SIZE_MIN
:
1464 *retval
= 1; /* XXX lie: 1 byte */
1466 case _PC_ASYNC_IO
: /* unistd.h: _POSIX_ASYNCHRONUS_IO */
1467 *retval
= 1; /* [AIO] option is supported */
1469 case _PC_PRIO_IO
: /* unistd.h: _POSIX_PRIORITIZED_IO */
1470 *retval
= 0; /* [PIO] option is not supported */
1472 case _PC_REC_INCR_XFER_SIZE
:
1473 *retval
= 4096; /* XXX go from MIN to MAX 4K at a time */
1475 case _PC_REC_MIN_XFER_SIZE
:
1476 *retval
= 4096; /* XXX recommend 4K minimum reads/writes */
1478 case _PC_REC_MAX_XFER_SIZE
:
1479 *retval
= 65536; /* XXX recommend 64K maximum reads/writes */
1481 case _PC_REC_XFER_ALIGN
:
1482 *retval
= 4096; /* XXX recommend page aligned buffers */
1484 case _PC_SYMLINK_MAX
:
1485 *retval
= 255; /* Minimum acceptable POSIX value */
1487 case _PC_SYNC_IO
: /* unistd.h: _POSIX_SYNCHRONIZED_IO */
1488 *retval
= 0; /* [SIO] option is not supported */
1490 case _PC_XATTR_SIZE_BITS
:
1491 /* The number of bits used to store maximum extended
1492 * attribute size in bytes. For example, if the maximum
1493 * attribute size supported by a file system is 128K, the
1494 * value returned will be 18. However a value 18 can mean
1495 * that the maximum attribute size can be anywhere from
1496 * (256KB - 1) to 128KB. As a special case, the resource
1497 * fork can have much larger size, and some file system
1498 * specific extended attributes can have smaller and preset
1499 * size; for example, Finder Info is always 32 bytes.
1501 memset(&vfa
, 0, sizeof(vfa
));
1503 VFSATTR_WANTED(&vfa
, f_capabilities
);
1504 if (vfs_getattr(vnode_mount(vp
), &vfa
, ctx
) == 0 &&
1505 (VFSATTR_IS_SUPPORTED(&vfa
, f_capabilities
)) &&
1506 (vfa
.f_capabilities
.capabilities
[VOL_CAPABILITIES_INTERFACES
] & VOL_CAP_INT_EXTENDED_ATTR
) &&
1507 (vfa
.f_capabilities
.valid
[VOL_CAPABILITIES_INTERFACES
] & VOL_CAP_INT_EXTENDED_ATTR
)) {
1508 /* Supports native extended attributes */
1509 error
= VNOP_PATHCONF(vp
, name
, retval
, ctx
);
1511 /* Number of bits used to represent the maximum size of
1512 * extended attribute stored in an Apple Double file.
1514 *retval
= AD_XATTR_SIZE_BITS
;
1518 error
= VNOP_PATHCONF(vp
, name
, retval
, ctx
);
1526 vn_kqfilt_add(struct fileproc
*fp
, struct knote
*kn
, vfs_context_t ctx
)
1531 vp
= (struct vnode
*)fp
->f_fglob
->fg_data
;
1534 * Don't attach a knote to a dead vnode.
1536 if ((error
= vget_internal(vp
, 0, VNODE_NODEAD
)) == 0) {
1537 switch (kn
->kn_filter
) {
1540 if (vnode_isfifo(vp
)) {
1541 /* We'll only watch FIFOs that use our fifofs */
1542 if (!(vp
->v_fifoinfo
&& vp
->v_fifoinfo
->fi_readsock
)) {
1546 } else if (!vnode_isreg(vp
)) {
1547 if (vnode_ischr(vp
) &&
1548 (error
= spec_kqfilter(vp
, kn
)) == 0) {
1549 /* claimed by a special device */
1569 error
= mac_vnode_check_kqfilter(ctx
, fp
->f_fglob
->fg_cred
, kn
, vp
);
1576 kn
->kn_hook
= (void*)vp
;
1577 kn
->kn_hookid
= vnode_vid(vp
);
1578 kn
->kn_fop
= &vnode_filtops
;
1581 KNOTE_ATTACH(&vp
->v_knotes
, kn
);
1584 /* Ask the filesystem to provide remove notifications, but ignore failure */
1585 VNOP_MONITOR(vp
, 0, VNODE_MONITOR_BEGIN
, (void*) kn
, ctx
);
1594 filt_vndetach(struct knote
*kn
)
1596 vfs_context_t ctx
= vfs_context_current();
1598 vp
= (struct vnode
*)kn
->kn_hook
;
1599 if (vnode_getwithvid(vp
, kn
->kn_hookid
))
1603 KNOTE_DETACH(&vp
->v_knotes
, kn
);
1607 * Tell a (generally networked) filesystem that we're no longer watching
1608 * If the FS wants to track contexts, it should still be using the one from
1609 * the VNODE_MONITOR_BEGIN.
1611 VNOP_MONITOR(vp
, 0, VNODE_MONITOR_END
, (void*)kn
, ctx
);
1617 * Used for EVFILT_READ
1619 * Takes only VFIFO or VREG. vnode is locked. We handle the "poll" case
1620 * differently than the regular case for VREG files. If not in poll(),
1621 * then we need to know current fileproc offset for VREG.
1624 vnode_readable_data_count(vnode_t vp
, off_t current_offset
, int ispoll
)
1626 if (vnode_isfifo(vp
)) {
1629 int err
= fifo_charcount(vp
, &cnt
);
1631 return (intptr_t)cnt
;
1637 } else if (vnode_isreg(vp
)) {
1643 amount
= vp
->v_un
.vu_ubcinfo
->ui_size
- current_offset
;
1644 if (amount
> (off_t
)INTPTR_MAX
) {
1646 } else if (amount
< (off_t
)INTPTR_MIN
) {
1649 return (intptr_t)amount
;
1652 panic("Should never have an EVFILT_READ except for reg or fifo.");
1658 * Used for EVFILT_WRITE.
1660 * For regular vnodes, we can always write (1). For named pipes,
1661 * see how much space there is in the buffer. Nothing else is covered.
1664 vnode_writable_space_count(vnode_t vp
)
1666 if (vnode_isfifo(vp
)) {
1669 int err
= fifo_freespace(vp
, &spc
);
1671 return (intptr_t)spc
;
1677 } else if (vnode_isreg(vp
)) {
1680 panic("Should never have an EVFILT_READ except for reg or fifo.");
1686 * Determine whether this knote should be active
1688 * This is kind of subtle.
1689 * --First, notice if the vnode has been revoked: in so, override hint
1690 * --EVFILT_READ knotes are checked no matter what the hint is
1691 * --Other knotes activate based on hint.
1692 * --If hint is revoke, set special flags and activate
1695 filt_vnode(struct knote
*kn
, long hint
)
1697 vnode_t vp
= (struct vnode
*)kn
->kn_hook
;
1699 long orig_hint
= hint
;
1704 if (vnode_getiocount(vp
, kn
->kn_hookid
, VNODE_NODEAD
| VNODE_WITHID
) != 0) {
1709 lck_mtx_assert(&vp
->v_lock
, LCK_MTX_ASSERT_OWNED
);
1712 /* Special handling for vnodes that are in recycle or already gone */
1713 if (NOTE_REVOKE
== hint
) {
1714 kn
->kn_flags
|= (EV_EOF
| EV_ONESHOT
);
1717 if ((kn
->kn_filter
== EVFILT_VNODE
) && (kn
->kn_sfflags
& NOTE_REVOKE
)) {
1718 kn
->kn_fflags
|= NOTE_REVOKE
;
1721 switch(kn
->kn_filter
) {
1723 kn
->kn_data
= vnode_readable_data_count(vp
, kn
->kn_fp
->f_fglob
->fg_offset
, (kn
->kn_flags
& EV_POLL
));
1725 if (kn
->kn_data
!= 0) {
1730 kn
->kn_data
= vnode_writable_space_count(vp
);
1732 if (kn
->kn_data
!= 0) {
1737 /* Check events this note matches against the hint */
1738 if (kn
->kn_sfflags
& hint
) {
1739 kn
->kn_fflags
|= hint
; /* Set which event occurred */
1741 if (kn
->kn_fflags
!= 0) {
1746 panic("Invalid knote filter on a vnode!\n");
1750 if (orig_hint
== 0) {
1752 * Definitely need to unlock, may need to put
1755 vnode_put_locked(vp
);