2 * Copyright (c) 2000-2014 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>
114 #include <IOKit/IOBSD.h>
115 #include <libkern/section_keywords.h>
117 static int vn_closefile(struct fileglob
*fp
, vfs_context_t ctx
);
118 static int vn_ioctl(struct fileproc
*fp
, u_long com
, caddr_t data
,
120 static int vn_read(struct fileproc
*fp
, struct uio
*uio
, int flags
,
122 static int vn_write(struct fileproc
*fp
, struct uio
*uio
, int flags
,
124 static int vn_select( struct fileproc
*fp
, int which
, void * wql
,
126 static int vn_kqfilt_add(struct fileproc
*fp
, struct knote
*kn
,
127 struct kevent_internal_s
*kev
, vfs_context_t ctx
);
128 static void filt_vndetach(struct knote
*kn
);
129 static int filt_vnode(struct knote
*kn
, long hint
);
130 static int filt_vnode_common(struct knote
*kn
, vnode_t vp
, long hint
);
131 static int vn_open_auth_finish(vnode_t vp
, int fmode
, vfs_context_t ctx
);
133 static int vn_kqfilt_remove(struct vnode
*vp
, uintptr_t ident
,
137 const struct fileops vnops
= {
138 .fo_type
= DTYPE_VNODE
,
140 .fo_write
= vn_write
,
141 .fo_ioctl
= vn_ioctl
,
142 .fo_select
= vn_select
,
143 .fo_close
= vn_closefile
,
144 .fo_kqfilter
= vn_kqfilt_add
,
148 static int filt_vntouch(struct knote
*kn
, struct kevent_internal_s
*kev
);
149 static int filt_vnprocess(struct knote
*kn
, struct filt_process_s
*data
, struct kevent_internal_s
*kev
);
151 SECURITY_READ_ONLY_EARLY(struct filterops
) vnode_filtops
= {
154 .f_detach
= filt_vndetach
,
155 .f_event
= filt_vnode
,
156 .f_touch
= filt_vntouch
,
157 .f_process
= filt_vnprocess
,
161 * Common code for vnode open operations.
162 * Check permissions, and call the VNOP_OPEN or VNOP_CREATE routine.
164 * XXX the profusion of interfaces here is probably a bad thing.
167 vn_open(struct nameidata
*ndp
, int fmode
, int cmode
)
169 return(vn_open_modflags(ndp
, &fmode
, cmode
));
173 vn_open_modflags(struct nameidata
*ndp
, int *fmodep
, int cmode
)
175 struct vnode_attr va
;
178 VATTR_SET(&va
, va_mode
, cmode
);
180 return(vn_open_auth(ndp
, fmodep
, &va
));
184 vn_open_auth_finish(vnode_t vp
, int fmode
, vfs_context_t ctx
)
188 if ((error
= vnode_ref_ext(vp
, fmode
, 0)) != 0) {
192 /* Call out to allow 3rd party notification of open.
193 * Ignore result of kauth_authorize_fileop call.
196 mac_vnode_notify_open(ctx
, vp
, fmode
);
198 kauth_authorize_fileop(vfs_context_ucred(ctx
), KAUTH_FILEOP_OPEN
,
209 * May do nameidone() to allow safely adding an FSEvent. Cue off of ni_dvp to
210 * determine whether that has happened.
213 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
)
216 vnode_t dvp
= ndp
->ni_dvp
;
221 batched
= vnode_compound_open_available(ndp
->ni_dvp
);
224 VATTR_SET(vap
, va_type
, VREG
);
226 vap
->va_vaflags
|= VA_EXCLUSIVE
;
229 if (ndp
->ni_cnd
.cn_flags
& CN_WANTSRSRCFORK
) {
230 if ((error
= vn_authorize_create(dvp
, &ndp
->ni_cnd
, vap
, ctx
, NULL
)) != 0)
232 if ((error
= vnode_makenamedstream(dvp
, &ndp
->ni_vp
, XATTR_RESOURCEFORK_NAME
, 0, ctx
)) != 0)
238 if ((error
= vn_authorize_create(dvp
, &ndp
->ni_cnd
, vap
, ctx
, NULL
)) != 0)
242 error
= vn_create(dvp
, &ndp
->ni_vp
, ndp
, vap
, VN_CREATE_DOOPEN
, fmode
, &status
, ctx
);
245 *did_create
= (status
& COMPOUND_OPEN_STATUS_DID_CREATE
) ? TRUE
: FALSE
;
250 if (error
== EKEEPLOOKING
) {
252 panic("EKEEPLOOKING, but we did a create?");
255 panic("EKEEPLOOKING from filesystem that doesn't support compound vnops?");
257 if ((ndp
->ni_flag
& NAMEI_CONTLOOKUP
) == 0) {
258 panic("EKEEPLOOKING, but continue flag not set?");
262 * Do NOT drop the dvp: we need everything to continue the lookup.
268 *did_create
= (status
& COMPOUND_OPEN_STATUS_DID_CREATE
) ? 1 : 0;
281 int update_flags
= 0;
283 // Make sure the name & parent pointers are hooked up
284 if (vp
->v_name
== NULL
)
285 update_flags
|= VNODE_UPDATE_NAME
;
286 if (vp
->v_parent
== NULLVP
)
287 update_flags
|= VNODE_UPDATE_PARENT
;
290 vnode_update_identity(vp
, dvp
, ndp
->ni_cnd
.cn_nameptr
, ndp
->ni_cnd
.cn_namelen
, ndp
->ni_cnd
.cn_hash
, update_flags
);
293 ndp
->ni_dvp
= NULLVP
;
296 if (need_fsevent(FSE_CREATE_FILE
, vp
)) {
297 add_fsevent(FSE_CREATE_FILE
, ctx
,
304 if (ndp
->ni_dvp
!= NULLVP
) {
306 ndp
->ni_dvp
= NULLVP
;
313 * This is the number of times we'll loop in vn_open_auth without explicitly
314 * yielding the CPU when we determine we have to retry.
316 #define RETRY_NO_YIELD_COUNT 5
319 * Open a file with authorization, updating the contents of the structures
320 * pointed to by ndp, fmodep, and vap as necessary to perform the requested
321 * operation. This function is used for both opens of existing files, and
322 * creation of new files.
324 * Parameters: ndp The nami data pointer describing the
326 * fmodep A pointer to an int containg the mode
327 * information to be used for the open
328 * vap A pointer to the vnode attribute
329 * descriptor to be used for the open
331 * Indirect: * Contents of the data structures pointed
332 * to by the parameters are modified as
333 * necessary to the requested operation.
338 * Notes: The kauth_filesec_t in 'vap', if any, is in host byte order.
340 * The contents of '*ndp' will be modified, based on the other
341 * arguments to this function, and to return file and directory
342 * data necessary to satisfy the requested operation.
344 * If the file does not exist and we are creating it, then the
345 * O_TRUNC flag will be cleared in '*fmodep' to indicate to the
346 * caller that the file was not truncated.
348 * If the file exists and the O_EXCL flag was not specified, then
349 * the O_CREAT flag will be cleared in '*fmodep' to indicate to
350 * the caller that the existing file was merely opened rather
353 * The contents of '*vap' will be modified as necessary to
354 * complete the operation, including setting of supported
355 * attribute, clearing of fields containing unsupported attributes
356 * in the request, if the request proceeds without them, etc..
358 * XXX: This function is too complicated in actings on its arguments
360 * XXX: We should enummerate the possible errno values here, and where
361 * in the code they originated.
364 vn_open_auth(struct nameidata
*ndp
, int *fmodep
, struct vnode_attr
*vap
)
368 vfs_context_t ctx
= ndp
->ni_cnd
.cn_context
;
371 uint32_t origcnflags
;
372 boolean_t did_create
;
374 boolean_t need_vnop_open
;
376 boolean_t ref_failed
;
384 need_vnop_open
= TRUE
;
387 origcnflags
= ndp
->ni_cnd
.cn_flags
;
389 // If raw encrypted mode is requested, handle that here
390 if (VATTR_IS_ACTIVE (vap
, va_dataprotect_flags
)
391 && ISSET(vap
->va_dataprotect_flags
, VA_DP_RAWENCRYPTED
)) {
398 if (fmode
& O_CREAT
) {
399 if ( (fmode
& O_DIRECTORY
) ) {
403 ndp
->ni_cnd
.cn_nameiop
= CREATE
;
405 ndp
->ni_op
= OP_LINK
;
407 /* Inherit USEDVP, vnode_open() supported flags only */
408 ndp
->ni_cnd
.cn_flags
&= (USEDVP
| NOCROSSMOUNT
);
409 ndp
->ni_cnd
.cn_flags
|= LOCKPARENT
| LOCKLEAF
| AUDITVNPATH1
;
410 ndp
->ni_flag
= NAMEI_COMPOUNDOPEN
;
412 /* open calls are allowed for resource forks. */
413 ndp
->ni_cnd
.cn_flags
|= CN_ALLOWRSRCFORK
;
415 if ((fmode
& O_EXCL
) == 0 && (fmode
& O_NOFOLLOW
) == 0 && (origcnflags
& FOLLOW
) != 0)
416 ndp
->ni_cnd
.cn_flags
|= FOLLOW
;
418 continue_create_lookup
:
419 if ( (error
= namei(ndp
)) )
425 batched
= vnode_compound_open_available(dvp
);
427 /* not found, create */
429 /* must have attributes for a new file */
436 * Attempt a create. For a system supporting compound VNOPs, we may
437 * find an existing file or create one; in either case, we will already
438 * have the file open and no VNOP_OPEN() will be needed.
440 error
= vn_open_auth_do_create(ndp
, vap
, fmode
, &did_create
, &did_open
, ctx
);
446 * Detected a node that the filesystem couldn't handle. Don't call
447 * nameidone() yet, because we need that path buffer.
449 if (error
== EKEEPLOOKING
) {
451 panic("EKEEPLOOKING from a filesystem that doesn't support compound VNOPs?");
453 goto continue_create_lookup
;
458 panic("Shouldn't have a dvp here.");
463 * Check for a create race.
465 if ((error
== EEXIST
) && !(fmode
& O_EXCL
)){
473 need_vnop_open
= !did_open
;
480 * We have a vnode. Use compound open if available
481 * or else fall through to "traditional" path. Note: can't
482 * do a compound open for root, because the parent belongs
485 if (error
== 0 && batched
&& (vnode_mount(dvp
) == vnode_mount(vp
))) {
486 error
= VNOP_COMPOUND_OPEN(dvp
, &ndp
->ni_vp
, ndp
, 0, fmode
, NULL
, NULL
, ctx
);
490 need_vnop_open
= FALSE
;
491 } else if (error
== EKEEPLOOKING
) {
492 if ((ndp
->ni_flag
& NAMEI_CONTLOOKUP
) == 0) {
493 panic("EKEEPLOOKING, but continue flag not set?");
495 goto continue_create_lookup
;
500 ndp
->ni_dvp
= NULLVP
;
515 ndp
->ni_cnd
.cn_nameiop
= LOOKUP
;
516 /* Inherit USEDVP, vnode_open() supported flags only */
517 ndp
->ni_cnd
.cn_flags
&= (USEDVP
| NOCROSSMOUNT
);
518 ndp
->ni_cnd
.cn_flags
|= FOLLOW
| LOCKLEAF
| AUDITVNPATH1
| WANTPARENT
;
520 /* open calls are allowed for resource forks. */
521 ndp
->ni_cnd
.cn_flags
|= CN_ALLOWRSRCFORK
;
523 if (fmode
& FENCRYPTED
)
524 ndp
->ni_cnd
.cn_flags
|= CN_RAW_ENCRYPTED
| CN_SKIPNAMECACHE
;
525 ndp
->ni_flag
= NAMEI_COMPOUNDOPEN
;
527 /* preserve NOFOLLOW from vnode_open() */
528 if (fmode
& O_NOFOLLOW
|| fmode
& O_SYMLINK
|| (origcnflags
& FOLLOW
) == 0) {
529 ndp
->ni_cnd
.cn_flags
&= ~FOLLOW
;
532 /* Do a lookup, possibly going directly to filesystem for compound operation */
534 if ( (error
= namei(ndp
)) )
539 /* Check for batched lookup-open */
540 batched
= vnode_compound_open_available(dvp
);
541 if (batched
&& ((vp
== NULLVP
) || (vnode_mount(dvp
) == vnode_mount(vp
)))) {
542 error
= VNOP_COMPOUND_OPEN(dvp
, &ndp
->ni_vp
, ndp
, 0, fmode
, NULL
, NULL
, ctx
);
545 need_vnop_open
= FALSE
;
546 } else if (error
== EKEEPLOOKING
) {
547 if ((ndp
->ni_flag
& NAMEI_CONTLOOKUP
) == 0) {
548 panic("EKEEPLOOKING, but continue flag not set?");
552 } while (error
== EKEEPLOOKING
);
556 ndp
->ni_dvp
= NULLVP
;
564 * By this point, nameidone() is called, dvp iocount is dropped,
565 * and dvp pointer is cleared.
567 if (ndp
->ni_dvp
!= NULLVP
) {
568 panic("Haven't cleaned up adequately in vn_open_auth()");
571 #if DEVELOPMENT || DEBUG
573 * XXX VSWAP: Check for entitlements or special flag here
574 * so we can restrict access appropriately.
576 #else /* DEVELOPMENT || DEBUG */
578 if (vnode_isswap(vp
) && (fmode
& (FWRITE
| O_TRUNC
)) && (ctx
!= vfs_context_kernel())) {
582 #endif /* DEVELOPMENT || DEBUG */
585 * Expect to use this code for filesystems without compound VNOPs, for the root
586 * of a filesystem, which can't be "looked up" in the sense of VNOP_LOOKUP(),
587 * and for shadow files, which do not live on the same filesystems as their "parents."
589 if (need_vnop_open
) {
590 if (batched
&& !vnode_isvroot(vp
) && !vnode_isnamedstream(vp
)) {
591 panic("Why am I trying to use VNOP_OPEN() on anything other than the root or a named stream?");
595 error
= vn_authorize_open_existing(vp
, &ndp
->ni_cnd
, fmode
, ctx
, NULL
);
601 if (VATTR_IS_ACTIVE (vap
, va_dataprotect_flags
)
602 && ISSET(vap
->va_dataprotect_flags
, VA_DP_RAWUNENCRYPTED
)) {
603 /* Don't allow unencrypted io request from user space unless entitled */
604 boolean_t entitled
= FALSE
;
606 entitled
= IOTaskHasEntitlement(current_task(), "com.apple.private.security.file-unencrypt-access");
612 fmode
|= FUNENCRYPTED
;
615 error
= VNOP_OPEN(vp
, fmode
, ctx
);
619 need_vnop_open
= FALSE
;
622 // if the vnode is tagged VOPENEVT and the current process
623 // has the P_CHECKOPENEVT flag set, then we or in the O_EVTONLY
624 // flag to the open mode so that this open won't count against
625 // the vnode when carbon delete() does a vnode_isinuse() to see
626 // if a file is currently in use. this allows spotlight
627 // importers to not interfere with carbon apps that depend on
628 // the no-delete-if-busy semantics of carbon delete().
630 if (!did_create
&& (vp
->v_flag
& VOPENEVT
) && (current_proc()->p_flag
& P_CHECKOPENEVT
)) {
635 * Grab reference, etc.
637 error
= vn_open_auth_finish(vp
, fmode
, ctx
);
643 /* Compound VNOP open is responsible for doing the truncate */
644 if (batched
|| did_create
)
651 /* Opened either explicitly or by a batched create */
652 if (!need_vnop_open
) {
653 VNOP_CLOSE(vp
, fmode
, ctx
);
659 /* Aggressively recycle shadow files if we error'd out during open() */
660 if ((vnode_isnamedstream(vp
)) &&
661 (vp
->v_parent
!= NULLVP
) &&
662 (vnode_isshadow(vp
))) {
668 * Check for a race against unlink. We had a vnode
669 * but according to vnode_authorize or VNOP_OPEN it
672 * EREDRIVEOPEN: means that we were hit by the tty allocation race.
674 if (((error
== ENOENT
) && (*fmodep
& O_CREAT
)) || (error
== EREDRIVEOPEN
) || ref_failed
) {
676 * We'll retry here but it may be possible that we get
677 * into a retry "spin" inside the kernel and not allow
678 * threads, which need to run in order for the retry
679 * loop to end, to run. An example is an open of a
680 * terminal which is getting revoked and we spin here
681 * without yielding becasue namei and VNOP_OPEN are
682 * successful but vnode_ref fails. The revoke needs
683 * threads with an iocount to run but if spin here we
684 * may possibly be blcoking other threads from running.
686 * We start yielding the CPU after some number of
687 * retries for increasing durations. Note that this is
688 * still a loop without an exit condition.
691 if (nretries
> RETRY_NO_YIELD_COUNT
) {
692 /* Every hz/100 secs is 10 msecs ... */
693 tsleep(&nretries
, PVFS
, "vn_open_auth_retry",
694 MIN((nretries
* (hz
/100)), hz
));
704 #if vn_access_DEPRECATED
706 * Authorize an action against a vnode. This has been the canonical way to
707 * ensure that the credential/process/etc. referenced by a vfs_context
708 * is granted the rights called out in 'mode' against the vnode 'vp'.
710 * Unfortunately, the use of VREAD/VWRITE/VEXEC makes it very difficult
711 * to add support for more rights. As such, this interface will be deprecated
712 * and callers will use vnode_authorize instead.
715 vn_access(vnode_t vp
, int mode
, vfs_context_t context
)
717 kauth_action_t action
;
721 action
|= KAUTH_VNODE_READ_DATA
;
723 action
|= KAUTH_VNODE_WRITE_DATA
;
725 action
|= KAUTH_VNODE_EXECUTE
;
727 return(vnode_authorize(vp
, NULL
, action
, context
));
729 #endif /* vn_access_DEPRECATED */
735 vn_close(struct vnode
*vp
, int flags
, vfs_context_t ctx
)
741 /* Sync data from resource fork shadow file if needed. */
742 if ((vp
->v_flag
& VISNAMEDSTREAM
) &&
743 (vp
->v_parent
!= NULLVP
) &&
744 vnode_isshadow(vp
)) {
745 if (flags
& FWASWRITTEN
) {
746 flusherror
= vnode_flushnamedstream(vp
->v_parent
, vp
, ctx
);
751 /* work around for foxhound */
752 if (vnode_isspec(vp
))
753 (void)vnode_rele_ext(vp
, flags
, 0);
756 * On HFS, we flush when the last writer closes. We do this
757 * because resource fork vnodes hold a reference on data fork
758 * vnodes and that will prevent them from getting VNOP_INACTIVE
759 * which will delay when we flush cached data. In future, we
760 * might find it beneficial to do this for all file systems.
761 * Note that it's OK to access v_writecount without the lock
764 if (vp
->v_tag
== VT_HFS
&& (flags
& FWRITE
) && vp
->v_writecount
== 1)
765 VNOP_FSYNC(vp
, MNT_NOWAIT
, ctx
);
767 error
= VNOP_CLOSE(vp
, flags
, ctx
);
770 if (flags
& FWASWRITTEN
) {
771 if (need_fsevent(FSE_CONTENT_MODIFIED
, vp
)) {
772 add_fsevent(FSE_CONTENT_MODIFIED
, ctx
,
779 if (!vnode_isspec(vp
))
780 (void)vnode_rele_ext(vp
, flags
, 0);
794 off_t swap_count
, this_count
;
795 off_t file_end
, read_end
;
800 * Reading from a swap file will get you zeroes.
805 swap_count
= uio_resid(uio
);
807 file_end
= ubc_getsize(vp
);
808 read_end
= uio
->uio_offset
+ uio_resid(uio
);
809 if (uio
->uio_offset
>= file_end
) {
810 /* uio starts after end of file: nothing to read */
812 } else if (read_end
> file_end
) {
813 /* uio extends beyond end of file: stop before that */
814 swap_count
-= (read_end
- file_end
);
817 while (swap_count
> 0) {
818 if (my_swap_page
== NULL
) {
819 MALLOC(my_swap_page
, char *, PAGE_SIZE
,
821 memset(my_swap_page
, '\0', PAGE_SIZE
);
822 /* add an end-of-line to keep line counters happy */
823 my_swap_page
[PAGE_SIZE
-1] = '\n';
825 this_count
= swap_count
;
826 if (this_count
> PAGE_SIZE
) {
827 this_count
= PAGE_SIZE
;
830 prev_resid
= uio_resid(uio
);
831 error
= uiomove((caddr_t
) my_swap_page
,
837 swap_count
-= (prev_resid
- uio_resid(uio
));
839 if (my_swap_page
!= NULL
) {
840 FREE(my_swap_page
, M_TEMP
);
847 * Package up an I/O request on a vnode into a uio and do it.
865 result
= vn_rdwr_64(rw
,
867 (uint64_t)(uintptr_t)base
,
876 /* "resid" should be bounded above by "len," which is an int */
877 if (aresid
!= NULL
) {
900 struct vfs_context context
;
902 char uio_buf
[ UIO_SIZEOF(1) ];
904 context
.vc_thread
= current_thread();
905 context
.vc_ucred
= cred
;
907 if (UIO_SEG_IS_USER_SPACE(segflg
)) {
908 spacetype
= proc_is64bit(p
) ? UIO_USERSPACE64
: UIO_USERSPACE32
;
911 spacetype
= UIO_SYSSPACE
;
913 auio
= uio_createwithbuffer(1, offset
, spacetype
, rw
,
914 &uio_buf
[0], sizeof(uio_buf
));
915 uio_addiov(auio
, base
, len
);
919 * IO_NOAUTH should be re-examined.
920 * Likely that mediation should be performed in caller.
922 if ((ioflg
& IO_NOAUTH
) == 0) {
923 /* passed cred is fp->f_cred */
925 error
= mac_vnode_check_read(&context
, cred
, vp
);
927 error
= mac_vnode_check_write(&context
, cred
, vp
);
932 if (rw
== UIO_READ
) {
933 if (vnode_isswap(vp
) && ((ioflg
& IO_SWAP_DISPATCH
) == 0)) {
934 error
= vn_read_swapfile(vp
, auio
);
936 error
= VNOP_READ(vp
, auio
, ioflg
, &context
);
940 #if DEVELOPMENT || DEBUG
942 * XXX VSWAP: Check for entitlements or special flag here
943 * so we can restrict access appropriately.
945 error
= VNOP_WRITE(vp
, auio
, ioflg
, &context
);
946 #else /* DEVELOPMENT || DEBUG */
948 if (vnode_isswap(vp
) && ((ioflg
& (IO_SWAP_DISPATCH
| IO_SKIP_ENCRYPTION
)) == 0)) {
951 error
= VNOP_WRITE(vp
, auio
, ioflg
, &context
);
953 #endif /* DEVELOPMENT || DEBUG */
958 *aresid
= uio_resid(auio
);
960 if (uio_resid(auio
) && error
== 0)
966 vn_offset_lock(struct fileglob
*fg
)
968 lck_mtx_lock_spin(&fg
->fg_lock
);
969 while (fg
->fg_lflags
& FG_OFF_LOCKED
) {
970 fg
->fg_lflags
|= FG_OFF_LOCKWANT
;
971 msleep(&fg
->fg_lflags
, &fg
->fg_lock
, PVFS
| PSPIN
,
972 "fg_offset_lock_wait", 0);
974 fg
->fg_lflags
|= FG_OFF_LOCKED
;
975 lck_mtx_unlock(&fg
->fg_lock
);
979 vn_offset_unlock(struct fileglob
*fg
)
983 lck_mtx_lock_spin(&fg
->fg_lock
);
984 if (fg
->fg_lflags
& FG_OFF_LOCKWANT
) {
987 fg
->fg_lflags
&= ~(FG_OFF_LOCKED
| FG_OFF_LOCKWANT
);
988 lck_mtx_unlock(&fg
->fg_lock
);
990 wakeup(&fg
->fg_lflags
);
995 * File table vnode read routine.
998 vn_read(struct fileproc
*fp
, struct uio
*uio
, int flags
, vfs_context_t ctx
)
1004 int offset_locked
= 0;
1006 vp
= (struct vnode
*)fp
->f_fglob
->fg_data
;
1007 if ( (error
= vnode_getwithref(vp
)) ) {
1012 error
= mac_vnode_check_read(ctx
, vfs_context_ucred(ctx
), vp
);
1014 (void)vnode_put(vp
);
1019 /* This signals to VNOP handlers that this read came from a file table read */
1020 ioflag
= IO_SYSCALL_DISPATCH
;
1022 if (fp
->f_fglob
->fg_flag
& FNONBLOCK
)
1023 ioflag
|= IO_NDELAY
;
1024 if ((fp
->f_fglob
->fg_flag
& FNOCACHE
) || vnode_isnocache(vp
))
1025 ioflag
|= IO_NOCACHE
;
1026 if (fp
->f_fglob
->fg_flag
& FENCRYPTED
) {
1027 ioflag
|= IO_ENCRYPTED
;
1029 if (fp
->f_fglob
->fg_flag
& FUNENCRYPTED
) {
1030 ioflag
|= IO_SKIP_ENCRYPTION
;
1032 if (fp
->f_fglob
->fg_flag
& O_EVTONLY
) {
1033 ioflag
|= IO_EVTONLY
;
1035 if (fp
->f_fglob
->fg_flag
& FNORDAHEAD
)
1038 if ((flags
& FOF_OFFSET
) == 0) {
1039 if ((vnode_vtype(vp
) == VREG
) && !vnode_isswap(vp
)) {
1040 vn_offset_lock(fp
->f_fglob
);
1043 uio
->uio_offset
= fp
->f_fglob
->fg_offset
;
1045 count
= uio_resid(uio
);
1047 if (vnode_isswap(vp
) && !(IO_SKIP_ENCRYPTION
& ioflag
)) {
1049 /* special case for swap files */
1050 error
= vn_read_swapfile(vp
, uio
);
1052 error
= VNOP_READ(vp
, uio
, ioflag
, ctx
);
1055 if ((flags
& FOF_OFFSET
) == 0) {
1056 fp
->f_fglob
->fg_offset
+= count
- uio_resid(uio
);
1057 if (offset_locked
) {
1058 vn_offset_unlock(fp
->f_fglob
);
1063 (void)vnode_put(vp
);
1069 * File table vnode write routine.
1072 vn_write(struct fileproc
*fp
, struct uio
*uio
, int flags
, vfs_context_t ctx
)
1077 int clippedsize
= 0;
1079 int residcount
, oldcount
;
1080 int offset_locked
= 0;
1081 proc_t p
= vfs_context_proc(ctx
);
1084 vp
= (struct vnode
*)fp
->f_fglob
->fg_data
;
1085 if ( (error
= vnode_getwithref(vp
)) ) {
1089 #if DEVELOPMENT || DEBUG
1091 * XXX VSWAP: Check for entitlements or special flag here
1092 * so we can restrict access appropriately.
1094 #else /* DEVELOPMENT || DEBUG */
1096 if (vnode_isswap(vp
)) {
1097 (void)vnode_put(vp
);
1101 #endif /* DEVELOPMENT || DEBUG */
1105 error
= mac_vnode_check_write(ctx
, vfs_context_ucred(ctx
), vp
);
1107 (void)vnode_put(vp
);
1113 * IO_SYSCALL_DISPATCH signals to VNOP handlers that this write came from
1114 * a file table write
1116 ioflag
= (IO_UNIT
| IO_SYSCALL_DISPATCH
);
1118 if (vp
->v_type
== VREG
&& (fp
->f_fglob
->fg_flag
& O_APPEND
))
1119 ioflag
|= IO_APPEND
;
1120 if (fp
->f_fglob
->fg_flag
& FNONBLOCK
)
1121 ioflag
|= IO_NDELAY
;
1122 if ((fp
->f_fglob
->fg_flag
& FNOCACHE
) || vnode_isnocache(vp
))
1123 ioflag
|= IO_NOCACHE
;
1124 if (fp
->f_fglob
->fg_flag
& FNODIRECT
)
1125 ioflag
|= IO_NODIRECT
;
1126 if (fp
->f_fglob
->fg_flag
& FSINGLE_WRITER
)
1127 ioflag
|= IO_SINGLE_WRITER
;
1128 if (fp
->f_fglob
->fg_flag
& O_EVTONLY
)
1129 ioflag
|= IO_EVTONLY
;
1132 * Treat synchronous mounts and O_FSYNC on the fd as equivalent.
1134 * XXX We treat O_DSYNC as O_FSYNC for now, since we can not delay
1135 * XXX the non-essential metadata without some additional VFS work;
1136 * XXX the intent at this point is to plumb the interface for it.
1138 if ((fp
->f_fglob
->fg_flag
& (O_FSYNC
|O_DSYNC
)) ||
1139 (vp
->v_mount
&& (vp
->v_mount
->mnt_flag
& MNT_SYNCHRONOUS
))) {
1143 if ((flags
& FOF_OFFSET
) == 0) {
1144 if ((vnode_vtype(vp
) == VREG
) && !vnode_isswap(vp
)) {
1145 vn_offset_lock(fp
->f_fglob
);
1148 uio
->uio_offset
= fp
->f_fglob
->fg_offset
;
1149 count
= uio_resid(uio
);
1151 if (((flags
& FOF_OFFSET
) == 0) &&
1152 vfs_context_proc(ctx
) && (vp
->v_type
== VREG
) &&
1153 (((rlim_t
)(uio
->uio_offset
+ uio_resid(uio
)) > p
->p_rlimit
[RLIMIT_FSIZE
].rlim_cur
) ||
1154 ((rlim_t
)uio_resid(uio
) > (p
->p_rlimit
[RLIMIT_FSIZE
].rlim_cur
- uio
->uio_offset
)))) {
1156 * If the requested residual would cause us to go past the
1157 * administrative limit, then we need to adjust the residual
1158 * down to cause fewer bytes than requested to be written. If
1159 * we can't do that (e.g. the residual is already 1 byte),
1160 * then we fail the write with EFBIG.
1162 residcount
= uio_resid(uio
);
1163 if ((rlim_t
)(uio
->uio_offset
+ uio_resid(uio
)) > p
->p_rlimit
[RLIMIT_FSIZE
].rlim_cur
) {
1164 clippedsize
= (uio
->uio_offset
+ uio_resid(uio
)) - p
->p_rlimit
[RLIMIT_FSIZE
].rlim_cur
;
1165 } else if ((rlim_t
)uio_resid(uio
) > (p
->p_rlimit
[RLIMIT_FSIZE
].rlim_cur
- uio
->uio_offset
)) {
1166 clippedsize
= (p
->p_rlimit
[RLIMIT_FSIZE
].rlim_cur
- uio
->uio_offset
);
1168 if (clippedsize
>= residcount
) {
1169 psignal(p
, SIGXFSZ
);
1174 uio_setresid(uio
, residcount
-clippedsize
);
1176 if ((flags
& FOF_OFFSET
) != 0) {
1177 /* for pwrite, append should be ignored */
1178 ioflag
&= ~IO_APPEND
;
1179 if (p
&& (vp
->v_type
== VREG
) &&
1180 ((rlim_t
)uio
->uio_offset
>= p
->p_rlimit
[RLIMIT_FSIZE
].rlim_cur
)) {
1181 psignal(p
, SIGXFSZ
);
1185 if (p
&& (vp
->v_type
== VREG
) &&
1186 ((rlim_t
)(uio
->uio_offset
+ uio_resid(uio
)) > p
->p_rlimit
[RLIMIT_FSIZE
].rlim_cur
)) {
1187 //Debugger("vn_bwrite:overstepping the bounds");
1188 residcount
= uio_resid(uio
);
1189 clippedsize
= (uio
->uio_offset
+ uio_resid(uio
)) - p
->p_rlimit
[RLIMIT_FSIZE
].rlim_cur
;
1191 uio_setresid(uio
, residcount
-clippedsize
);
1195 error
= VNOP_WRITE(vp
, uio
, ioflag
, ctx
);
1198 oldcount
= uio_resid(uio
);
1199 uio_setresid(uio
, oldcount
+ clippedsize
);
1202 if ((flags
& FOF_OFFSET
) == 0) {
1203 if (ioflag
& IO_APPEND
)
1204 fp
->f_fglob
->fg_offset
= uio
->uio_offset
;
1206 fp
->f_fglob
->fg_offset
+= count
- uio_resid(uio
);
1207 if (offset_locked
) {
1208 vn_offset_unlock(fp
->f_fglob
);
1214 * Set the credentials on successful writes
1216 if ((error
== 0) && (vp
->v_tag
== VT_NFS
) && (UBCINFOEXISTS(vp
))) {
1218 * When called from aio subsystem, we only have the proc from
1219 * which to get the credential, at this point, so use that
1220 * instead. This means aio functions are incompatible with
1221 * per-thread credentials (aio operations are proxied). We
1222 * can't easily correct the aio vs. settid race in this case
1223 * anyway, so we disallow it.
1225 if ((flags
& FOF_PCRED
) == 0) {
1226 ubc_setthreadcred(vp
, p
, current_thread());
1231 (void)vnode_put(vp
);
1235 if (offset_locked
) {
1236 vn_offset_unlock(fp
->f_fglob
);
1238 (void)vnode_put(vp
);
1243 * File table vnode stat routine.
1245 * Returns: 0 Success
1251 vn_stat_noauth(struct vnode
*vp
, void *sbptr
, kauth_filesec_t
*xsec
, int isstat64
,
1252 vfs_context_t ctx
, struct ucred
*file_cred
)
1254 struct vnode_attr va
;
1257 kauth_filesec_t fsec
;
1258 struct stat
*sb
= (struct stat
*)0; /* warning avoidance ; protected by isstat64 */
1259 struct stat64
* sb64
= (struct stat64
*)0; /* warning avoidance ; protected by isstat64 */
1262 sb64
= (struct stat64
*)sbptr
;
1264 sb
= (struct stat
*)sbptr
;
1265 memset(&va
, 0, sizeof(va
));
1267 VATTR_WANTED(&va
, va_fsid
);
1268 VATTR_WANTED(&va
, va_fileid
);
1269 VATTR_WANTED(&va
, va_mode
);
1270 VATTR_WANTED(&va
, va_type
);
1271 VATTR_WANTED(&va
, va_nlink
);
1272 VATTR_WANTED(&va
, va_uid
);
1273 VATTR_WANTED(&va
, va_gid
);
1274 VATTR_WANTED(&va
, va_rdev
);
1275 VATTR_WANTED(&va
, va_data_size
);
1276 VATTR_WANTED(&va
, va_access_time
);
1277 VATTR_WANTED(&va
, va_modify_time
);
1278 VATTR_WANTED(&va
, va_change_time
);
1279 VATTR_WANTED(&va
, va_create_time
);
1280 VATTR_WANTED(&va
, va_flags
);
1281 VATTR_WANTED(&va
, va_gen
);
1282 VATTR_WANTED(&va
, va_iosize
);
1283 /* lower layers will synthesise va_total_alloc from va_data_size if required */
1284 VATTR_WANTED(&va
, va_total_alloc
);
1286 VATTR_WANTED(&va
, va_uuuid
);
1287 VATTR_WANTED(&va
, va_guuid
);
1288 VATTR_WANTED(&va
, va_acl
);
1290 error
= vnode_getattr(vp
, &va
, ctx
);
1295 * Give MAC polices a chance to reject or filter the attributes
1296 * returned by the filesystem. Note that MAC policies are consulted
1297 * *after* calling the filesystem because filesystems can return more
1298 * attributes than were requested so policies wouldn't be authoritative
1299 * is consulted beforehand. This also gives policies an opportunity
1300 * to change the values of attributes retrieved.
1302 error
= mac_vnode_check_getattr(ctx
, file_cred
, vp
, &va
);
1307 * Copy from vattr table
1309 if (isstat64
!= 0) {
1310 sb64
->st_dev
= va
.va_fsid
;
1311 sb64
->st_ino
= (ino64_t
)va
.va_fileid
;
1314 sb
->st_dev
= va
.va_fsid
;
1315 sb
->st_ino
= (ino_t
)va
.va_fileid
;
1318 switch (vp
->v_type
) {
1344 if (isstat64
!= 0) {
1345 sb64
->st_mode
= mode
;
1346 sb64
->st_nlink
= VATTR_IS_SUPPORTED(&va
, va_nlink
) ? va
.va_nlink
> UINT16_MAX
? UINT16_MAX
: (u_int16_t
)va
.va_nlink
: 1;
1347 sb64
->st_uid
= va
.va_uid
;
1348 sb64
->st_gid
= va
.va_gid
;
1349 sb64
->st_rdev
= va
.va_rdev
;
1350 sb64
->st_size
= va
.va_data_size
;
1351 sb64
->st_atimespec
= va
.va_access_time
;
1352 sb64
->st_mtimespec
= va
.va_modify_time
;
1353 sb64
->st_ctimespec
= va
.va_change_time
;
1354 if (VATTR_IS_SUPPORTED(&va
, va_create_time
)) {
1355 sb64
->st_birthtimespec
= va
.va_create_time
;
1357 sb64
->st_birthtimespec
.tv_sec
= sb64
->st_birthtimespec
.tv_nsec
= 0;
1359 sb64
->st_blksize
= va
.va_iosize
;
1360 sb64
->st_flags
= va
.va_flags
;
1361 sb64
->st_blocks
= roundup(va
.va_total_alloc
, 512) / 512;
1364 sb
->st_nlink
= VATTR_IS_SUPPORTED(&va
, va_nlink
) ? va
.va_nlink
> UINT16_MAX
? UINT16_MAX
: (u_int16_t
)va
.va_nlink
: 1;
1365 sb
->st_uid
= va
.va_uid
;
1366 sb
->st_gid
= va
.va_gid
;
1367 sb
->st_rdev
= va
.va_rdev
;
1368 sb
->st_size
= va
.va_data_size
;
1369 sb
->st_atimespec
= va
.va_access_time
;
1370 sb
->st_mtimespec
= va
.va_modify_time
;
1371 sb
->st_ctimespec
= va
.va_change_time
;
1372 sb
->st_blksize
= va
.va_iosize
;
1373 sb
->st_flags
= va
.va_flags
;
1374 sb
->st_blocks
= roundup(va
.va_total_alloc
, 512) / 512;
1377 /* if we're interested in extended security data and we got an ACL */
1379 if (!VATTR_IS_SUPPORTED(&va
, va_acl
) &&
1380 !VATTR_IS_SUPPORTED(&va
, va_uuuid
) &&
1381 !VATTR_IS_SUPPORTED(&va
, va_guuid
)) {
1382 *xsec
= KAUTH_FILESEC_NONE
;
1385 if (VATTR_IS_SUPPORTED(&va
, va_acl
) && (va
.va_acl
!= NULL
)) {
1386 fsec
= kauth_filesec_alloc(va
.va_acl
->acl_entrycount
);
1388 fsec
= kauth_filesec_alloc(0);
1394 fsec
->fsec_magic
= KAUTH_FILESEC_MAGIC
;
1395 if (VATTR_IS_SUPPORTED(&va
, va_uuuid
)) {
1396 fsec
->fsec_owner
= va
.va_uuuid
;
1398 fsec
->fsec_owner
= kauth_null_guid
;
1400 if (VATTR_IS_SUPPORTED(&va
, va_guuid
)) {
1401 fsec
->fsec_group
= va
.va_guuid
;
1403 fsec
->fsec_group
= kauth_null_guid
;
1405 if (VATTR_IS_SUPPORTED(&va
, va_acl
) && (va
.va_acl
!= NULL
)) {
1406 bcopy(va
.va_acl
, &(fsec
->fsec_acl
), KAUTH_ACL_COPYSIZE(va
.va_acl
));
1408 fsec
->fsec_acl
.acl_entrycount
= KAUTH_FILESEC_NOACL
;
1414 /* Do not give the generation number out to unpriviledged users */
1415 if (va
.va_gen
&& !vfs_context_issuser(ctx
)) {
1422 sb64
->st_gen
= va
.va_gen
;
1424 sb
->st_gen
= va
.va_gen
;
1429 if (VATTR_IS_SUPPORTED(&va
, va_acl
) && va
.va_acl
!= NULL
)
1430 kauth_acl_free(va
.va_acl
);
1435 vn_stat(struct vnode
*vp
, void *sb
, kauth_filesec_t
*xsec
, int isstat64
, vfs_context_t ctx
)
1440 error
= mac_vnode_check_stat(ctx
, NOCRED
, vp
);
1446 if ((error
= vnode_authorize(vp
, NULL
, KAUTH_VNODE_READ_ATTRIBUTES
| KAUTH_VNODE_READ_SECURITY
, ctx
)) != 0)
1450 return(vn_stat_noauth(vp
, sb
, xsec
, isstat64
, ctx
, NOCRED
));
1455 * File table vnode ioctl routine.
1458 vn_ioctl(struct fileproc
*fp
, u_long com
, caddr_t data
, vfs_context_t ctx
)
1460 struct vnode
*vp
= ((struct vnode
*)fp
->f_fglob
->fg_data
);
1463 struct vnode
*ttyvp
;
1464 struct session
* sessp
;
1466 if ( (error
= vnode_getwithref(vp
)) ) {
1471 error
= mac_vnode_check_ioctl(ctx
, vp
, com
);
1476 switch (vp
->v_type
) {
1479 if (com
== FIONREAD
) {
1480 if ((error
= vnode_size(vp
, &file_size
, ctx
)) != 0)
1482 *(int *)data
= file_size
- fp
->f_fglob
->fg_offset
;
1485 if (com
== FIONBIO
|| com
== FIOASYNC
) { /* XXX */
1498 /* Should not be able to set block size from user space */
1499 if (com
== DKIOCSETBLOCKSIZE
) {
1504 if (com
== FIODTYPE
) {
1505 if (vp
->v_type
== VBLK
) {
1506 if (major(vp
->v_rdev
) >= nblkdev
) {
1510 *(int *)data
= bdevsw
[major(vp
->v_rdev
)].d_type
;
1512 } else if (vp
->v_type
== VCHR
) {
1513 if (major(vp
->v_rdev
) >= nchrdev
) {
1517 *(int *)data
= cdevsw
[major(vp
->v_rdev
)].d_type
;
1524 error
= VNOP_IOCTL(vp
, com
, data
, fp
->f_fglob
->fg_flag
, ctx
);
1526 if (error
== 0 && com
== TIOCSCTTY
) {
1527 sessp
= proc_session(vfs_context_proc(ctx
));
1529 session_lock(sessp
);
1530 ttyvp
= sessp
->s_ttyvp
;
1531 sessp
->s_ttyvp
= vp
;
1532 sessp
->s_ttyvid
= vnode_vid(vp
);
1533 session_unlock(sessp
);
1534 session_rele(sessp
);
1538 (void)vnode_put(vp
);
1543 * File table vnode select routine.
1546 vn_select(struct fileproc
*fp
, int which
, void *wql
, __unused vfs_context_t ctx
)
1549 struct vnode
* vp
= (struct vnode
*)fp
->f_fglob
->fg_data
;
1550 struct vfs_context context
;
1552 if ( (error
= vnode_getwithref(vp
)) == 0 ) {
1553 context
.vc_thread
= current_thread();
1554 context
.vc_ucred
= fp
->f_fglob
->fg_cred
;
1558 * XXX We should use a per thread credential here; minimally,
1559 * XXX the process credential should have a persistent
1560 * XXX reference on it before being passed in here.
1562 error
= mac_vnode_check_select(ctx
, vp
, which
);
1565 error
= VNOP_SELECT(vp
, which
, fp
->f_fglob
->fg_flag
, wql
, ctx
);
1567 (void)vnode_put(vp
);
1574 * File table vnode close routine.
1577 vn_closefile(struct fileglob
*fg
, vfs_context_t ctx
)
1579 struct vnode
*vp
= fg
->fg_data
;
1582 if ( (error
= vnode_getwithref(vp
)) == 0 ) {
1583 if (FILEGLOB_DTYPE(fg
) == DTYPE_VNODE
&&
1584 ((fg
->fg_flag
& FHASLOCK
) != 0 ||
1585 (fg
->fg_lflags
& FG_HAS_OFDLOCK
) != 0)) {
1587 .l_whence
= SEEK_SET
,
1593 if ((fg
->fg_flag
& FHASLOCK
) != 0)
1594 (void) VNOP_ADVLOCK(vp
, (caddr_t
)fg
,
1595 F_UNLCK
, &lf
, F_FLOCK
, ctx
, NULL
);
1597 if ((fg
->fg_lflags
& FG_HAS_OFDLOCK
) != 0)
1598 (void) VNOP_ADVLOCK(vp
, (caddr_t
)fg
,
1599 F_UNLCK
, &lf
, F_OFD_LOCK
, ctx
, NULL
);
1601 error
= vn_close(vp
, fg
->fg_flag
, ctx
);
1602 (void) vnode_put(vp
);
1608 * Returns: 0 Success
1612 vn_pathconf(vnode_t vp
, int name
, int32_t *retval
, vfs_context_t ctx
)
1615 struct vfs_attr vfa
;
1618 case _PC_EXTENDED_SECURITY_NP
:
1619 *retval
= vfs_extendedsecurity(vnode_mount(vp
)) ? 1 : 0;
1621 case _PC_AUTH_OPAQUE_NP
:
1622 *retval
= vfs_authopaque(vnode_mount(vp
));
1624 case _PC_2_SYMLINKS
:
1625 *retval
= 1; /* XXX NOTSUP on MSDOS, etc. */
1627 case _PC_ALLOC_SIZE_MIN
:
1628 *retval
= 1; /* XXX lie: 1 byte */
1630 case _PC_ASYNC_IO
: /* unistd.h: _POSIX_ASYNCHRONUS_IO */
1631 *retval
= 1; /* [AIO] option is supported */
1633 case _PC_PRIO_IO
: /* unistd.h: _POSIX_PRIORITIZED_IO */
1634 *retval
= 0; /* [PIO] option is not supported */
1636 case _PC_REC_INCR_XFER_SIZE
:
1637 *retval
= 4096; /* XXX go from MIN to MAX 4K at a time */
1639 case _PC_REC_MIN_XFER_SIZE
:
1640 *retval
= 4096; /* XXX recommend 4K minimum reads/writes */
1642 case _PC_REC_MAX_XFER_SIZE
:
1643 *retval
= 65536; /* XXX recommend 64K maximum reads/writes */
1645 case _PC_REC_XFER_ALIGN
:
1646 *retval
= 4096; /* XXX recommend page aligned buffers */
1648 case _PC_SYMLINK_MAX
:
1649 *retval
= 255; /* Minimum acceptable POSIX value */
1651 case _PC_SYNC_IO
: /* unistd.h: _POSIX_SYNCHRONIZED_IO */
1652 *retval
= 0; /* [SIO] option is not supported */
1654 case _PC_XATTR_SIZE_BITS
:
1655 /* The number of bits used to store maximum extended
1656 * attribute size in bytes. For example, if the maximum
1657 * attribute size supported by a file system is 128K, the
1658 * value returned will be 18. However a value 18 can mean
1659 * that the maximum attribute size can be anywhere from
1660 * (256KB - 1) to 128KB. As a special case, the resource
1661 * fork can have much larger size, and some file system
1662 * specific extended attributes can have smaller and preset
1663 * size; for example, Finder Info is always 32 bytes.
1665 memset(&vfa
, 0, sizeof(vfa
));
1667 VFSATTR_WANTED(&vfa
, f_capabilities
);
1668 if (vfs_getattr(vnode_mount(vp
), &vfa
, ctx
) == 0 &&
1669 (VFSATTR_IS_SUPPORTED(&vfa
, f_capabilities
)) &&
1670 (vfa
.f_capabilities
.capabilities
[VOL_CAPABILITIES_INTERFACES
] & VOL_CAP_INT_EXTENDED_ATTR
) &&
1671 (vfa
.f_capabilities
.valid
[VOL_CAPABILITIES_INTERFACES
] & VOL_CAP_INT_EXTENDED_ATTR
)) {
1672 /* Supports native extended attributes */
1673 error
= VNOP_PATHCONF(vp
, name
, retval
, ctx
);
1675 /* Number of bits used to represent the maximum size of
1676 * extended attribute stored in an Apple Double file.
1678 *retval
= AD_XATTR_SIZE_BITS
;
1682 error
= VNOP_PATHCONF(vp
, name
, retval
, ctx
);
1690 vn_kqfilt_add(struct fileproc
*fp
, struct knote
*kn
,
1691 struct kevent_internal_s
*kev
, vfs_context_t ctx
)
1697 vp
= (struct vnode
*)fp
->f_fglob
->fg_data
;
1700 * Don't attach a knote to a dead vnode.
1702 if ((error
= vget_internal(vp
, 0, VNODE_NODEAD
)) == 0) {
1703 switch (kn
->kn_filter
) {
1706 if (vnode_isfifo(vp
)) {
1707 /* We'll only watch FIFOs that use our fifofs */
1708 if (!(vp
->v_fifoinfo
&& vp
->v_fifoinfo
->fi_readsock
)) {
1712 } else if (!vnode_isreg(vp
)) {
1713 if (vnode_ischr(vp
)) {
1714 result
= spec_kqfilter(vp
, kn
, kev
);
1715 if ((kn
->kn_flags
& EV_ERROR
) == 0) {
1716 /* claimed by a special device */
1733 error
= mac_vnode_check_kqfilter(ctx
, fp
->f_fglob
->fg_cred
, kn
, vp
);
1740 kn
->kn_hook
= (void*)vp
;
1741 kn
->kn_hookid
= vnode_vid(vp
);
1742 kn
->kn_filtid
= EVFILTID_VN
;
1745 KNOTE_ATTACH(&vp
->v_knotes
, kn
);
1746 result
= filt_vnode_common(kn
, vp
, 0);
1750 * Ask the filesystem to provide remove notifications,
1751 * but ignore failure
1753 VNOP_MONITOR(vp
, 0, VNODE_MONITOR_BEGIN
, (void*) kn
, ctx
);
1761 kn
->kn_flags
= EV_ERROR
;
1762 kn
->kn_data
= error
;
1769 filt_vndetach(struct knote
*kn
)
1771 vfs_context_t ctx
= vfs_context_current();
1773 vp
= (struct vnode
*)kn
->kn_hook
;
1774 if (vnode_getwithvid(vp
, kn
->kn_hookid
))
1778 KNOTE_DETACH(&vp
->v_knotes
, kn
);
1782 * Tell a (generally networked) filesystem that we're no longer watching
1783 * If the FS wants to track contexts, it should still be using the one from
1784 * the VNODE_MONITOR_BEGIN.
1786 VNOP_MONITOR(vp
, 0, VNODE_MONITOR_END
, (void*)kn
, ctx
);
1792 * Used for EVFILT_READ
1794 * Takes only VFIFO or VREG. vnode is locked. We handle the "poll" case
1795 * differently than the regular case for VREG files. If not in poll(),
1796 * then we need to know current fileproc offset for VREG.
1799 vnode_readable_data_count(vnode_t vp
, off_t current_offset
, int ispoll
)
1801 if (vnode_isfifo(vp
)) {
1804 int err
= fifo_charcount(vp
, &cnt
);
1806 return (int64_t)cnt
;
1812 } else if (vnode_isreg(vp
)) {
1818 amount
= vp
->v_un
.vu_ubcinfo
->ui_size
- current_offset
;
1819 if (amount
> INT64_MAX
) {
1821 } else if (amount
< INT64_MIN
) {
1824 return (int64_t)amount
;
1827 panic("Should never have an EVFILT_READ except for reg or fifo.");
1833 * Used for EVFILT_WRITE.
1835 * For regular vnodes, we can always write (1). For named pipes,
1836 * see how much space there is in the buffer. Nothing else is covered.
1839 vnode_writable_space_count(vnode_t vp
)
1841 if (vnode_isfifo(vp
)) {
1844 int err
= fifo_freespace(vp
, &spc
);
1846 return (intptr_t)spc
;
1852 } else if (vnode_isreg(vp
)) {
1855 panic("Should never have an EVFILT_READ except for reg or fifo.");
1861 * Determine whether this knote should be active
1863 * This is kind of subtle.
1864 * --First, notice if the vnode has been revoked: in so, override hint
1865 * --EVFILT_READ knotes are checked no matter what the hint is
1866 * --Other knotes activate based on hint.
1867 * --If hint is revoke, set special flags and activate
1870 filt_vnode_common(struct knote
*kn
, vnode_t vp
, long hint
)
1874 lck_mtx_assert(&vp
->v_lock
, LCK_MTX_ASSERT_OWNED
);
1876 /* Special handling for vnodes that are in recycle or already gone */
1877 if (NOTE_REVOKE
== hint
) {
1878 kn
->kn_flags
|= (EV_EOF
| EV_ONESHOT
);
1881 if ((kn
->kn_filter
== EVFILT_VNODE
) && (kn
->kn_sfflags
& NOTE_REVOKE
)) {
1882 kn
->kn_fflags
|= NOTE_REVOKE
;
1885 switch(kn
->kn_filter
) {
1887 kn
->kn_data
= vnode_readable_data_count(vp
, kn
->kn_fp
->f_fglob
->fg_offset
, (kn
->kn_flags
& EV_POLL
));
1889 if (kn
->kn_data
!= 0) {
1894 kn
->kn_data
= vnode_writable_space_count(vp
);
1896 if (kn
->kn_data
!= 0) {
1901 /* Check events this note matches against the hint */
1902 if (kn
->kn_sfflags
& hint
) {
1903 kn
->kn_fflags
|= hint
; /* Set which event occurred */
1905 if (kn
->kn_fflags
!= 0) {
1910 panic("Invalid knote filter on a vnode!\n");
1917 filt_vnode(struct knote
*kn
, long hint
)
1919 vnode_t vp
= (struct vnode
*)kn
->kn_hook
;
1921 return filt_vnode_common(kn
, vp
, hint
);
1925 filt_vntouch(struct knote
*kn
, struct kevent_internal_s
*kev
)
1927 vnode_t vp
= (struct vnode
*)kn
->kn_hook
;
1932 if (vnode_getiocount(vp
, kn
->kn_hookid
, VNODE_NODEAD
| VNODE_WITHID
) != 0) {
1937 /* accept new input fflags mask */
1938 kn
->kn_sfflags
= kev
->fflags
;
1940 activate
= filt_vnode_common(kn
, vp
, hint
);
1943 vnode_put_locked(vp
);
1950 filt_vnprocess(struct knote
*kn
, struct filt_process_s
*data
, struct kevent_internal_s
*kev
)
1952 #pragma unused(data)
1953 vnode_t vp
= (struct vnode
*)kn
->kn_hook
;
1958 if (vnode_getiocount(vp
, kn
->kn_hookid
, VNODE_NODEAD
| VNODE_WITHID
) != 0) {
1962 activate
= filt_vnode_common(kn
, vp
, hint
);
1964 *kev
= kn
->kn_kevent
;
1965 if (kn
->kn_flags
& EV_CLEAR
) {
1971 /* Definitely need to unlock, may need to put */
1973 vnode_put_locked(vp
);