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>
116 static int vn_closefile(struct fileglob
*fp
, vfs_context_t ctx
);
117 static int vn_ioctl(struct fileproc
*fp
, u_long com
, caddr_t data
,
119 static int vn_read(struct fileproc
*fp
, struct uio
*uio
, int flags
,
121 static int vn_write(struct fileproc
*fp
, struct uio
*uio
, int flags
,
123 static int vn_select( struct fileproc
*fp
, int which
, void * wql
,
125 static int vn_kqfilt_add(struct fileproc
*fp
, struct knote
*kn
,
127 static void filt_vndetach(struct knote
*kn
);
128 static int filt_vnode(struct knote
*kn
, long hint
);
129 static int filt_vnode_common(struct knote
*kn
, vnode_t vp
, long hint
);
130 static int vn_open_auth_finish(vnode_t vp
, int fmode
, vfs_context_t ctx
);
132 static int vn_kqfilt_remove(struct vnode
*vp
, uintptr_t ident
,
136 const struct fileops vnops
= {
137 .fo_type
= DTYPE_VNODE
,
139 .fo_write
= vn_write
,
140 .fo_ioctl
= vn_ioctl
,
141 .fo_select
= vn_select
,
142 .fo_close
= vn_closefile
,
143 .fo_kqfilter
= vn_kqfilt_add
,
147 static int filt_vntouch(struct knote
*kn
, struct kevent_internal_s
*kev
);
148 static int filt_vnprocess(struct knote
*kn
, struct filt_process_s
*data
, struct kevent_internal_s
*kev
);
150 struct filterops vnode_filtops
= {
153 .f_detach
= filt_vndetach
,
154 .f_event
= filt_vnode
,
155 .f_touch
= filt_vntouch
,
156 .f_process
= filt_vnprocess
,
160 * Common code for vnode open operations.
161 * Check permissions, and call the VNOP_OPEN or VNOP_CREATE routine.
163 * XXX the profusion of interfaces here is probably a bad thing.
166 vn_open(struct nameidata
*ndp
, int fmode
, int cmode
)
168 return(vn_open_modflags(ndp
, &fmode
, cmode
));
172 vn_open_modflags(struct nameidata
*ndp
, int *fmodep
, int cmode
)
174 struct vnode_attr va
;
177 VATTR_SET(&va
, va_mode
, cmode
);
179 return(vn_open_auth(ndp
, fmodep
, &va
));
183 vn_open_auth_finish(vnode_t vp
, int fmode
, vfs_context_t ctx
)
187 if ((error
= vnode_ref_ext(vp
, fmode
, 0)) != 0) {
191 /* Call out to allow 3rd party notification of open.
192 * Ignore result of kauth_authorize_fileop call.
195 mac_vnode_notify_open(ctx
, vp
, fmode
);
197 kauth_authorize_fileop(vfs_context_ucred(ctx
), KAUTH_FILEOP_OPEN
,
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 * This is the number of times we'll loop in vn_open_auth without explicitly
313 * yielding the CPU when we determine we have to retry.
315 #define RETRY_NO_YIELD_COUNT 5
318 * Open a file with authorization, updating the contents of the structures
319 * pointed to by ndp, fmodep, and vap as necessary to perform the requested
320 * operation. This function is used for both opens of existing files, and
321 * creation of new files.
323 * Parameters: ndp The nami data pointer describing the
325 * fmodep A pointer to an int containg the mode
326 * information to be used for the open
327 * vap A pointer to the vnode attribute
328 * descriptor to be used for the open
330 * Indirect: * Contents of the data structures pointed
331 * to by the parameters are modified as
332 * necessary to the requested operation.
337 * Notes: The kauth_filesec_t in 'vap', if any, is in host byte order.
339 * The contents of '*ndp' will be modified, based on the other
340 * arguments to this function, and to return file and directory
341 * data necessary to satisfy the requested operation.
343 * If the file does not exist and we are creating it, then the
344 * O_TRUNC flag will be cleared in '*fmodep' to indicate to the
345 * caller that the file was not truncated.
347 * If the file exists and the O_EXCL flag was not specified, then
348 * the O_CREAT flag will be cleared in '*fmodep' to indicate to
349 * the caller that the existing file was merely opened rather
352 * The contents of '*vap' will be modified as necessary to
353 * complete the operation, including setting of supported
354 * attribute, clearing of fields containing unsupported attributes
355 * in the request, if the request proceeds without them, etc..
357 * XXX: This function is too complicated in actings on its arguments
359 * XXX: We should enummerate the possible errno values here, and where
360 * in the code they originated.
363 vn_open_auth(struct nameidata
*ndp
, int *fmodep
, struct vnode_attr
*vap
)
367 vfs_context_t ctx
= ndp
->ni_cnd
.cn_context
;
370 uint32_t origcnflags
;
371 boolean_t did_create
;
373 boolean_t need_vnop_open
;
375 boolean_t ref_failed
;
383 need_vnop_open
= TRUE
;
386 origcnflags
= ndp
->ni_cnd
.cn_flags
;
388 // If raw encrypted mode is requested, handle that here
389 if (VATTR_IS_ACTIVE (vap
, va_dataprotect_flags
)
390 && ISSET(vap
->va_dataprotect_flags
, VA_DP_RAWENCRYPTED
)) {
397 if (fmode
& O_CREAT
) {
398 if ( (fmode
& O_DIRECTORY
) ) {
402 ndp
->ni_cnd
.cn_nameiop
= CREATE
;
404 ndp
->ni_op
= OP_LINK
;
406 /* Inherit USEDVP, vnode_open() supported flags only */
407 ndp
->ni_cnd
.cn_flags
&= (USEDVP
| NOCROSSMOUNT
);
408 ndp
->ni_cnd
.cn_flags
|= LOCKPARENT
| LOCKLEAF
| AUDITVNPATH1
;
409 ndp
->ni_flag
= NAMEI_COMPOUNDOPEN
;
411 /* open calls are allowed for resource forks. */
412 ndp
->ni_cnd
.cn_flags
|= CN_ALLOWRSRCFORK
;
414 if ((fmode
& O_EXCL
) == 0 && (fmode
& O_NOFOLLOW
) == 0 && (origcnflags
& FOLLOW
) != 0)
415 ndp
->ni_cnd
.cn_flags
|= FOLLOW
;
417 continue_create_lookup
:
418 if ( (error
= namei(ndp
)) )
424 batched
= vnode_compound_open_available(dvp
);
426 /* not found, create */
428 /* must have attributes for a new file */
435 * Attempt a create. For a system supporting compound VNOPs, we may
436 * find an existing file or create one; in either case, we will already
437 * have the file open and no VNOP_OPEN() will be needed.
439 error
= vn_open_auth_do_create(ndp
, vap
, fmode
, &did_create
, &did_open
, ctx
);
445 * Detected a node that the filesystem couldn't handle. Don't call
446 * nameidone() yet, because we need that path buffer.
448 if (error
== EKEEPLOOKING
) {
450 panic("EKEEPLOOKING from a filesystem that doesn't support compound VNOPs?");
452 goto continue_create_lookup
;
457 panic("Shouldn't have a dvp here.");
462 * Check for a create race.
464 if ((error
== EEXIST
) && !(fmode
& O_EXCL
)){
472 need_vnop_open
= !did_open
;
479 * We have a vnode. Use compound open if available
480 * or else fall through to "traditional" path. Note: can't
481 * do a compound open for root, because the parent belongs
484 if (error
== 0 && batched
&& (vnode_mount(dvp
) == vnode_mount(vp
))) {
485 error
= VNOP_COMPOUND_OPEN(dvp
, &ndp
->ni_vp
, ndp
, 0, fmode
, NULL
, NULL
, ctx
);
489 need_vnop_open
= FALSE
;
490 } else if (error
== EKEEPLOOKING
) {
491 if ((ndp
->ni_flag
& NAMEI_CONTLOOKUP
) == 0) {
492 panic("EKEEPLOOKING, but continue flag not set?");
494 goto continue_create_lookup
;
499 ndp
->ni_dvp
= NULLVP
;
514 ndp
->ni_cnd
.cn_nameiop
= LOOKUP
;
515 /* Inherit USEDVP, vnode_open() supported flags only */
516 ndp
->ni_cnd
.cn_flags
&= (USEDVP
| NOCROSSMOUNT
);
517 ndp
->ni_cnd
.cn_flags
|= FOLLOW
| LOCKLEAF
| AUDITVNPATH1
| WANTPARENT
;
519 /* open calls are allowed for resource forks. */
520 ndp
->ni_cnd
.cn_flags
|= CN_ALLOWRSRCFORK
;
522 if (fmode
& FENCRYPTED
)
523 ndp
->ni_cnd
.cn_flags
|= CN_RAW_ENCRYPTED
| CN_SKIPNAMECACHE
;
524 ndp
->ni_flag
= NAMEI_COMPOUNDOPEN
;
526 /* preserve NOFOLLOW from vnode_open() */
527 if (fmode
& O_NOFOLLOW
|| fmode
& O_SYMLINK
|| (origcnflags
& FOLLOW
) == 0) {
528 ndp
->ni_cnd
.cn_flags
&= ~FOLLOW
;
531 /* Do a lookup, possibly going directly to filesystem for compound operation */
533 if ( (error
= namei(ndp
)) )
538 /* Check for batched lookup-open */
539 batched
= vnode_compound_open_available(dvp
);
540 if (batched
&& ((vp
== NULLVP
) || (vnode_mount(dvp
) == vnode_mount(vp
)))) {
541 error
= VNOP_COMPOUND_OPEN(dvp
, &ndp
->ni_vp
, ndp
, 0, fmode
, NULL
, NULL
, ctx
);
544 need_vnop_open
= FALSE
;
545 } else if (error
== EKEEPLOOKING
) {
546 if ((ndp
->ni_flag
& NAMEI_CONTLOOKUP
) == 0) {
547 panic("EKEEPLOOKING, but continue flag not set?");
551 } while (error
== EKEEPLOOKING
);
555 ndp
->ni_dvp
= NULLVP
;
563 * By this point, nameidone() is called, dvp iocount is dropped,
564 * and dvp pointer is cleared.
566 if (ndp
->ni_dvp
!= NULLVP
) {
567 panic("Haven't cleaned up adequately in vn_open_auth()");
570 #if DEVELOPMENT || DEBUG
572 * XXX VSWAP: Check for entitlements or special flag here
573 * so we can restrict access appropriately.
575 #else /* DEVELOPMENT || DEBUG */
577 if (vnode_isswap(vp
) && (fmode
& (FWRITE
| O_TRUNC
)) && (ctx
!= vfs_context_kernel())) {
581 #endif /* DEVELOPMENT || DEBUG */
584 * Expect to use this code for filesystems without compound VNOPs, for the root
585 * of a filesystem, which can't be "looked up" in the sense of VNOP_LOOKUP(),
586 * and for shadow files, which do not live on the same filesystems as their "parents."
588 if (need_vnop_open
) {
589 if (batched
&& !vnode_isvroot(vp
) && !vnode_isnamedstream(vp
)) {
590 panic("Why am I trying to use VNOP_OPEN() on anything other than the root or a named stream?");
594 error
= vn_authorize_open_existing(vp
, &ndp
->ni_cnd
, fmode
, ctx
, NULL
);
600 if (VATTR_IS_ACTIVE (vap
, va_dataprotect_flags
)
601 && ISSET(vap
->va_dataprotect_flags
, VA_DP_RAWUNENCRYPTED
)) {
602 /* Don't allow unencrypted io request from user space unless entitled */
603 boolean_t entitled
= FALSE
;
605 entitled
= IOTaskHasEntitlement(current_task(), "com.apple.private.security.file-unencrypt-access");
611 fmode
|= FUNENCRYPTED
;
614 error
= VNOP_OPEN(vp
, fmode
, ctx
);
618 need_vnop_open
= FALSE
;
621 // if the vnode is tagged VOPENEVT and the current process
622 // has the P_CHECKOPENEVT flag set, then we or in the O_EVTONLY
623 // flag to the open mode so that this open won't count against
624 // the vnode when carbon delete() does a vnode_isinuse() to see
625 // if a file is currently in use. this allows spotlight
626 // importers to not interfere with carbon apps that depend on
627 // the no-delete-if-busy semantics of carbon delete().
629 if (!did_create
&& (vp
->v_flag
& VOPENEVT
) && (current_proc()->p_flag
& P_CHECKOPENEVT
)) {
634 * Grab reference, etc.
636 error
= vn_open_auth_finish(vp
, fmode
, ctx
);
642 /* Compound VNOP open is responsible for doing the truncate */
643 if (batched
|| did_create
)
650 /* Opened either explicitly or by a batched create */
651 if (!need_vnop_open
) {
652 VNOP_CLOSE(vp
, fmode
, ctx
);
658 /* Aggressively recycle shadow files if we error'd out during open() */
659 if ((vnode_isnamedstream(vp
)) &&
660 (vp
->v_parent
!= NULLVP
) &&
661 (vnode_isshadow(vp
))) {
667 * Check for a race against unlink. We had a vnode
668 * but according to vnode_authorize or VNOP_OPEN it
671 * EREDRIVEOPEN: means that we were hit by the tty allocation race.
673 if (((error
== ENOENT
) && (*fmodep
& O_CREAT
)) || (error
== EREDRIVEOPEN
) || ref_failed
) {
675 * We'll retry here but it may be possible that we get
676 * into a retry "spin" inside the kernel and not allow
677 * threads, which need to run in order for the retry
678 * loop to end, to run. An example is an open of a
679 * terminal which is getting revoked and we spin here
680 * without yielding becasue namei and VNOP_OPEN are
681 * successful but vnode_ref fails. The revoke needs
682 * threads with an iocount to run but if spin here we
683 * may possibly be blcoking other threads from running.
685 * We start yielding the CPU after some number of
686 * retries for increasing durations. Note that this is
687 * still a loop without an exit condition.
690 if (nretries
> RETRY_NO_YIELD_COUNT
) {
691 /* Every hz/100 secs is 10 msecs ... */
692 tsleep(&nretries
, PVFS
, "vn_open_auth_retry",
693 MIN((nretries
* (hz
/100)), hz
));
703 #if vn_access_DEPRECATED
705 * Authorize an action against a vnode. This has been the canonical way to
706 * ensure that the credential/process/etc. referenced by a vfs_context
707 * is granted the rights called out in 'mode' against the vnode 'vp'.
709 * Unfortunately, the use of VREAD/VWRITE/VEXEC makes it very difficult
710 * to add support for more rights. As such, this interface will be deprecated
711 * and callers will use vnode_authorize instead.
714 vn_access(vnode_t vp
, int mode
, vfs_context_t context
)
716 kauth_action_t action
;
720 action
|= KAUTH_VNODE_READ_DATA
;
722 action
|= KAUTH_VNODE_WRITE_DATA
;
724 action
|= KAUTH_VNODE_EXECUTE
;
726 return(vnode_authorize(vp
, NULL
, action
, context
));
728 #endif /* vn_access_DEPRECATED */
734 vn_close(struct vnode
*vp
, int flags
, vfs_context_t ctx
)
740 /* Sync data from resource fork shadow file if needed. */
741 if ((vp
->v_flag
& VISNAMEDSTREAM
) &&
742 (vp
->v_parent
!= NULLVP
) &&
743 vnode_isshadow(vp
)) {
744 if (flags
& FWASWRITTEN
) {
745 flusherror
= vnode_flushnamedstream(vp
->v_parent
, vp
, ctx
);
750 /* work around for foxhound */
751 if (vnode_isspec(vp
))
752 (void)vnode_rele_ext(vp
, flags
, 0);
755 * On HFS, we flush when the last writer closes. We do this
756 * because resource fork vnodes hold a reference on data fork
757 * vnodes and that will prevent them from getting VNOP_INACTIVE
758 * which will delay when we flush cached data. In future, we
759 * might find it beneficial to do this for all file systems.
760 * Note that it's OK to access v_writecount without the lock
763 if (vp
->v_tag
== VT_HFS
&& (flags
& FWRITE
) && vp
->v_writecount
== 1)
764 VNOP_FSYNC(vp
, MNT_NOWAIT
, ctx
);
766 error
= VNOP_CLOSE(vp
, flags
, ctx
);
769 if (flags
& FWASWRITTEN
) {
770 if (need_fsevent(FSE_CONTENT_MODIFIED
, vp
)) {
771 add_fsevent(FSE_CONTENT_MODIFIED
, ctx
,
778 if (!vnode_isspec(vp
))
779 (void)vnode_rele_ext(vp
, flags
, 0);
793 off_t swap_count
, this_count
;
794 off_t file_end
, read_end
;
799 * Reading from a swap file will get you zeroes.
804 swap_count
= uio_resid(uio
);
806 file_end
= ubc_getsize(vp
);
807 read_end
= uio
->uio_offset
+ uio_resid(uio
);
808 if (uio
->uio_offset
>= file_end
) {
809 /* uio starts after end of file: nothing to read */
811 } else if (read_end
> file_end
) {
812 /* uio extends beyond end of file: stop before that */
813 swap_count
-= (read_end
- file_end
);
816 while (swap_count
> 0) {
817 if (my_swap_page
== NULL
) {
818 MALLOC(my_swap_page
, char *, PAGE_SIZE
,
820 memset(my_swap_page
, '\0', PAGE_SIZE
);
821 /* add an end-of-line to keep line counters happy */
822 my_swap_page
[PAGE_SIZE
-1] = '\n';
824 this_count
= swap_count
;
825 if (this_count
> PAGE_SIZE
) {
826 this_count
= PAGE_SIZE
;
829 prev_resid
= uio_resid(uio
);
830 error
= uiomove((caddr_t
) my_swap_page
,
836 swap_count
-= (prev_resid
- uio_resid(uio
));
838 if (my_swap_page
!= NULL
) {
839 FREE(my_swap_page
, M_TEMP
);
846 * Package up an I/O request on a vnode into a uio and do it.
864 result
= vn_rdwr_64(rw
,
866 (uint64_t)(uintptr_t)base
,
875 /* "resid" should be bounded above by "len," which is an int */
876 if (aresid
!= NULL
) {
899 struct vfs_context context
;
901 char uio_buf
[ UIO_SIZEOF(1) ];
903 context
.vc_thread
= current_thread();
904 context
.vc_ucred
= cred
;
906 if (UIO_SEG_IS_USER_SPACE(segflg
)) {
907 spacetype
= proc_is64bit(p
) ? UIO_USERSPACE64
: UIO_USERSPACE32
;
910 spacetype
= UIO_SYSSPACE
;
912 auio
= uio_createwithbuffer(1, offset
, spacetype
, rw
,
913 &uio_buf
[0], sizeof(uio_buf
));
914 uio_addiov(auio
, base
, len
);
918 * IO_NOAUTH should be re-examined.
919 * Likely that mediation should be performed in caller.
921 if ((ioflg
& IO_NOAUTH
) == 0) {
922 /* passed cred is fp->f_cred */
924 error
= mac_vnode_check_read(&context
, cred
, vp
);
926 error
= mac_vnode_check_write(&context
, cred
, vp
);
931 if (rw
== UIO_READ
) {
932 if (vnode_isswap(vp
) && ((ioflg
& IO_SWAP_DISPATCH
) == 0)) {
933 error
= vn_read_swapfile(vp
, auio
);
935 error
= VNOP_READ(vp
, auio
, ioflg
, &context
);
939 #if DEVELOPMENT || DEBUG
941 * XXX VSWAP: Check for entitlements or special flag here
942 * so we can restrict access appropriately.
944 error
= VNOP_WRITE(vp
, auio
, ioflg
, &context
);
945 #else /* DEVELOPMENT || DEBUG */
947 if (vnode_isswap(vp
) && ((ioflg
& (IO_SWAP_DISPATCH
| IO_SKIP_ENCRYPTION
)) == 0)) {
950 error
= VNOP_WRITE(vp
, auio
, ioflg
, &context
);
952 #endif /* DEVELOPMENT || DEBUG */
957 *aresid
= uio_resid(auio
);
959 if (uio_resid(auio
) && error
== 0)
965 vn_offset_lock(struct fileglob
*fg
)
967 lck_mtx_lock_spin(&fg
->fg_lock
);
968 while (fg
->fg_lflags
& FG_OFF_LOCKED
) {
969 fg
->fg_lflags
|= FG_OFF_LOCKWANT
;
970 msleep(&fg
->fg_lflags
, &fg
->fg_lock
, PVFS
| PSPIN
,
971 "fg_offset_lock_wait", 0);
973 fg
->fg_lflags
|= FG_OFF_LOCKED
;
974 lck_mtx_unlock(&fg
->fg_lock
);
978 vn_offset_unlock(struct fileglob
*fg
)
982 lck_mtx_lock_spin(&fg
->fg_lock
);
983 if (fg
->fg_lflags
& FG_OFF_LOCKWANT
) {
986 fg
->fg_lflags
&= ~(FG_OFF_LOCKED
| FG_OFF_LOCKWANT
);
987 lck_mtx_unlock(&fg
->fg_lock
);
989 wakeup(&fg
->fg_lflags
);
994 * File table vnode read routine.
997 vn_read(struct fileproc
*fp
, struct uio
*uio
, int flags
, vfs_context_t ctx
)
1003 int offset_locked
= 0;
1005 vp
= (struct vnode
*)fp
->f_fglob
->fg_data
;
1006 if ( (error
= vnode_getwithref(vp
)) ) {
1011 error
= mac_vnode_check_read(ctx
, vfs_context_ucred(ctx
), vp
);
1013 (void)vnode_put(vp
);
1018 /* This signals to VNOP handlers that this read came from a file table read */
1019 ioflag
= IO_SYSCALL_DISPATCH
;
1021 if (fp
->f_fglob
->fg_flag
& FNONBLOCK
)
1022 ioflag
|= IO_NDELAY
;
1023 if ((fp
->f_fglob
->fg_flag
& FNOCACHE
) || vnode_isnocache(vp
))
1024 ioflag
|= IO_NOCACHE
;
1025 if (fp
->f_fglob
->fg_flag
& FENCRYPTED
) {
1026 ioflag
|= IO_ENCRYPTED
;
1028 if (fp
->f_fglob
->fg_flag
& FUNENCRYPTED
) {
1029 ioflag
|= IO_SKIP_ENCRYPTION
;
1031 if (fp
->f_fglob
->fg_flag
& O_EVTONLY
) {
1032 ioflag
|= IO_EVTONLY
;
1034 if (fp
->f_fglob
->fg_flag
& FNORDAHEAD
)
1037 if ((flags
& FOF_OFFSET
) == 0) {
1038 if ((vnode_vtype(vp
) == VREG
) && !vnode_isswap(vp
)) {
1039 vn_offset_lock(fp
->f_fglob
);
1042 uio
->uio_offset
= fp
->f_fglob
->fg_offset
;
1044 count
= uio_resid(uio
);
1046 if (vnode_isswap(vp
) && !(IO_SKIP_ENCRYPTION
& ioflag
)) {
1048 /* special case for swap files */
1049 error
= vn_read_swapfile(vp
, uio
);
1051 error
= VNOP_READ(vp
, uio
, ioflag
, ctx
);
1054 if ((flags
& FOF_OFFSET
) == 0) {
1055 fp
->f_fglob
->fg_offset
+= count
- uio_resid(uio
);
1056 if (offset_locked
) {
1057 vn_offset_unlock(fp
->f_fglob
);
1062 (void)vnode_put(vp
);
1068 * File table vnode write routine.
1071 vn_write(struct fileproc
*fp
, struct uio
*uio
, int flags
, vfs_context_t ctx
)
1076 int clippedsize
= 0;
1078 int residcount
, oldcount
;
1079 int offset_locked
= 0;
1080 proc_t p
= vfs_context_proc(ctx
);
1083 vp
= (struct vnode
*)fp
->f_fglob
->fg_data
;
1084 if ( (error
= vnode_getwithref(vp
)) ) {
1088 #if DEVELOPMENT || DEBUG
1090 * XXX VSWAP: Check for entitlements or special flag here
1091 * so we can restrict access appropriately.
1093 #else /* DEVELOPMENT || DEBUG */
1095 if (vnode_isswap(vp
)) {
1096 (void)vnode_put(vp
);
1100 #endif /* DEVELOPMENT || DEBUG */
1104 error
= mac_vnode_check_write(ctx
, vfs_context_ucred(ctx
), vp
);
1106 (void)vnode_put(vp
);
1112 * IO_SYSCALL_DISPATCH signals to VNOP handlers that this write came from
1113 * a file table write
1115 ioflag
= (IO_UNIT
| IO_SYSCALL_DISPATCH
);
1117 if (vp
->v_type
== VREG
&& (fp
->f_fglob
->fg_flag
& O_APPEND
))
1118 ioflag
|= IO_APPEND
;
1119 if (fp
->f_fglob
->fg_flag
& FNONBLOCK
)
1120 ioflag
|= IO_NDELAY
;
1121 if ((fp
->f_fglob
->fg_flag
& FNOCACHE
) || vnode_isnocache(vp
))
1122 ioflag
|= IO_NOCACHE
;
1123 if (fp
->f_fglob
->fg_flag
& FNODIRECT
)
1124 ioflag
|= IO_NODIRECT
;
1125 if (fp
->f_fglob
->fg_flag
& FSINGLE_WRITER
)
1126 ioflag
|= IO_SINGLE_WRITER
;
1127 if (fp
->f_fglob
->fg_flag
& O_EVTONLY
)
1128 ioflag
|= IO_EVTONLY
;
1131 * Treat synchronous mounts and O_FSYNC on the fd as equivalent.
1133 * XXX We treat O_DSYNC as O_FSYNC for now, since we can not delay
1134 * XXX the non-essential metadata without some additional VFS work;
1135 * XXX the intent at this point is to plumb the interface for it.
1137 if ((fp
->f_fglob
->fg_flag
& (O_FSYNC
|O_DSYNC
)) ||
1138 (vp
->v_mount
&& (vp
->v_mount
->mnt_flag
& MNT_SYNCHRONOUS
))) {
1142 if ((flags
& FOF_OFFSET
) == 0) {
1143 if ((vnode_vtype(vp
) == VREG
) && !vnode_isswap(vp
)) {
1144 vn_offset_lock(fp
->f_fglob
);
1147 uio
->uio_offset
= fp
->f_fglob
->fg_offset
;
1148 count
= uio_resid(uio
);
1150 if (((flags
& FOF_OFFSET
) == 0) &&
1151 vfs_context_proc(ctx
) && (vp
->v_type
== VREG
) &&
1152 (((rlim_t
)(uio
->uio_offset
+ uio_resid(uio
)) > p
->p_rlimit
[RLIMIT_FSIZE
].rlim_cur
) ||
1153 ((rlim_t
)uio_resid(uio
) > (p
->p_rlimit
[RLIMIT_FSIZE
].rlim_cur
- uio
->uio_offset
)))) {
1155 * If the requested residual would cause us to go past the
1156 * administrative limit, then we need to adjust the residual
1157 * down to cause fewer bytes than requested to be written. If
1158 * we can't do that (e.g. the residual is already 1 byte),
1159 * then we fail the write with EFBIG.
1161 residcount
= uio_resid(uio
);
1162 if ((rlim_t
)(uio
->uio_offset
+ uio_resid(uio
)) > p
->p_rlimit
[RLIMIT_FSIZE
].rlim_cur
) {
1163 clippedsize
= (uio
->uio_offset
+ uio_resid(uio
)) - p
->p_rlimit
[RLIMIT_FSIZE
].rlim_cur
;
1164 } else if ((rlim_t
)uio_resid(uio
) > (p
->p_rlimit
[RLIMIT_FSIZE
].rlim_cur
- uio
->uio_offset
)) {
1165 clippedsize
= (p
->p_rlimit
[RLIMIT_FSIZE
].rlim_cur
- uio
->uio_offset
);
1167 if (clippedsize
>= residcount
) {
1168 psignal(p
, SIGXFSZ
);
1173 uio_setresid(uio
, residcount
-clippedsize
);
1175 if ((flags
& FOF_OFFSET
) != 0) {
1176 /* for pwrite, append should be ignored */
1177 ioflag
&= ~IO_APPEND
;
1178 if (p
&& (vp
->v_type
== VREG
) &&
1179 ((rlim_t
)uio
->uio_offset
>= p
->p_rlimit
[RLIMIT_FSIZE
].rlim_cur
)) {
1180 psignal(p
, SIGXFSZ
);
1184 if (p
&& (vp
->v_type
== VREG
) &&
1185 ((rlim_t
)(uio
->uio_offset
+ uio_resid(uio
)) > p
->p_rlimit
[RLIMIT_FSIZE
].rlim_cur
)) {
1186 //Debugger("vn_bwrite:overstepping the bounds");
1187 residcount
= uio_resid(uio
);
1188 clippedsize
= (uio
->uio_offset
+ uio_resid(uio
)) - p
->p_rlimit
[RLIMIT_FSIZE
].rlim_cur
;
1190 uio_setresid(uio
, residcount
-clippedsize
);
1194 error
= VNOP_WRITE(vp
, uio
, ioflag
, ctx
);
1197 oldcount
= uio_resid(uio
);
1198 uio_setresid(uio
, oldcount
+ clippedsize
);
1201 if ((flags
& FOF_OFFSET
) == 0) {
1202 if (ioflag
& IO_APPEND
)
1203 fp
->f_fglob
->fg_offset
= uio
->uio_offset
;
1205 fp
->f_fglob
->fg_offset
+= count
- uio_resid(uio
);
1206 if (offset_locked
) {
1207 vn_offset_unlock(fp
->f_fglob
);
1213 * Set the credentials on successful writes
1215 if ((error
== 0) && (vp
->v_tag
== VT_NFS
) && (UBCINFOEXISTS(vp
))) {
1217 * When called from aio subsystem, we only have the proc from
1218 * which to get the credential, at this point, so use that
1219 * instead. This means aio functions are incompatible with
1220 * per-thread credentials (aio operations are proxied). We
1221 * can't easily correct the aio vs. settid race in this case
1222 * anyway, so we disallow it.
1224 if ((flags
& FOF_PCRED
) == 0) {
1225 ubc_setthreadcred(vp
, p
, current_thread());
1230 (void)vnode_put(vp
);
1234 if (offset_locked
) {
1235 vn_offset_unlock(fp
->f_fglob
);
1237 (void)vnode_put(vp
);
1242 * File table vnode stat routine.
1244 * Returns: 0 Success
1250 vn_stat_noauth(struct vnode
*vp
, void *sbptr
, kauth_filesec_t
*xsec
, int isstat64
,
1251 vfs_context_t ctx
, struct ucred
*file_cred
)
1253 struct vnode_attr va
;
1256 kauth_filesec_t fsec
;
1257 struct stat
*sb
= (struct stat
*)0; /* warning avoidance ; protected by isstat64 */
1258 struct stat64
* sb64
= (struct stat64
*)0; /* warning avoidance ; protected by isstat64 */
1261 sb64
= (struct stat64
*)sbptr
;
1263 sb
= (struct stat
*)sbptr
;
1264 memset(&va
, 0, sizeof(va
));
1266 VATTR_WANTED(&va
, va_fsid
);
1267 VATTR_WANTED(&va
, va_fileid
);
1268 VATTR_WANTED(&va
, va_mode
);
1269 VATTR_WANTED(&va
, va_type
);
1270 VATTR_WANTED(&va
, va_nlink
);
1271 VATTR_WANTED(&va
, va_uid
);
1272 VATTR_WANTED(&va
, va_gid
);
1273 VATTR_WANTED(&va
, va_rdev
);
1274 VATTR_WANTED(&va
, va_data_size
);
1275 VATTR_WANTED(&va
, va_access_time
);
1276 VATTR_WANTED(&va
, va_modify_time
);
1277 VATTR_WANTED(&va
, va_change_time
);
1278 VATTR_WANTED(&va
, va_create_time
);
1279 VATTR_WANTED(&va
, va_flags
);
1280 VATTR_WANTED(&va
, va_gen
);
1281 VATTR_WANTED(&va
, va_iosize
);
1282 /* lower layers will synthesise va_total_alloc from va_data_size if required */
1283 VATTR_WANTED(&va
, va_total_alloc
);
1285 VATTR_WANTED(&va
, va_uuuid
);
1286 VATTR_WANTED(&va
, va_guuid
);
1287 VATTR_WANTED(&va
, va_acl
);
1289 error
= vnode_getattr(vp
, &va
, ctx
);
1294 * Give MAC polices a chance to reject or filter the attributes
1295 * returned by the filesystem. Note that MAC policies are consulted
1296 * *after* calling the filesystem because filesystems can return more
1297 * attributes than were requested so policies wouldn't be authoritative
1298 * is consulted beforehand. This also gives policies an opportunity
1299 * to change the values of attributes retrieved.
1301 error
= mac_vnode_check_getattr(ctx
, file_cred
, vp
, &va
);
1306 * Copy from vattr table
1308 if (isstat64
!= 0) {
1309 sb64
->st_dev
= va
.va_fsid
;
1310 sb64
->st_ino
= (ino64_t
)va
.va_fileid
;
1313 sb
->st_dev
= va
.va_fsid
;
1314 sb
->st_ino
= (ino_t
)va
.va_fileid
;
1317 switch (vp
->v_type
) {
1343 if (isstat64
!= 0) {
1344 sb64
->st_mode
= mode
;
1345 sb64
->st_nlink
= VATTR_IS_SUPPORTED(&va
, va_nlink
) ? (u_int16_t
)va
.va_nlink
: 1;
1346 sb64
->st_uid
= va
.va_uid
;
1347 sb64
->st_gid
= va
.va_gid
;
1348 sb64
->st_rdev
= va
.va_rdev
;
1349 sb64
->st_size
= va
.va_data_size
;
1350 sb64
->st_atimespec
= va
.va_access_time
;
1351 sb64
->st_mtimespec
= va
.va_modify_time
;
1352 sb64
->st_ctimespec
= va
.va_change_time
;
1353 if (VATTR_IS_SUPPORTED(&va
, va_create_time
)) {
1354 sb64
->st_birthtimespec
= va
.va_create_time
;
1356 sb64
->st_birthtimespec
.tv_sec
= sb64
->st_birthtimespec
.tv_nsec
= 0;
1358 sb64
->st_blksize
= va
.va_iosize
;
1359 sb64
->st_flags
= va
.va_flags
;
1360 sb64
->st_blocks
= roundup(va
.va_total_alloc
, 512) / 512;
1363 sb
->st_nlink
= VATTR_IS_SUPPORTED(&va
, va_nlink
) ? (u_int16_t
)va
.va_nlink
: 1;
1364 sb
->st_uid
= va
.va_uid
;
1365 sb
->st_gid
= va
.va_gid
;
1366 sb
->st_rdev
= va
.va_rdev
;
1367 sb
->st_size
= va
.va_data_size
;
1368 sb
->st_atimespec
= va
.va_access_time
;
1369 sb
->st_mtimespec
= va
.va_modify_time
;
1370 sb
->st_ctimespec
= va
.va_change_time
;
1371 sb
->st_blksize
= va
.va_iosize
;
1372 sb
->st_flags
= va
.va_flags
;
1373 sb
->st_blocks
= roundup(va
.va_total_alloc
, 512) / 512;
1376 /* if we're interested in extended security data and we got an ACL */
1378 if (!VATTR_IS_SUPPORTED(&va
, va_acl
) &&
1379 !VATTR_IS_SUPPORTED(&va
, va_uuuid
) &&
1380 !VATTR_IS_SUPPORTED(&va
, va_guuid
)) {
1381 *xsec
= KAUTH_FILESEC_NONE
;
1384 if (VATTR_IS_SUPPORTED(&va
, va_acl
) && (va
.va_acl
!= NULL
)) {
1385 fsec
= kauth_filesec_alloc(va
.va_acl
->acl_entrycount
);
1387 fsec
= kauth_filesec_alloc(0);
1393 fsec
->fsec_magic
= KAUTH_FILESEC_MAGIC
;
1394 if (VATTR_IS_SUPPORTED(&va
, va_uuuid
)) {
1395 fsec
->fsec_owner
= va
.va_uuuid
;
1397 fsec
->fsec_owner
= kauth_null_guid
;
1399 if (VATTR_IS_SUPPORTED(&va
, va_guuid
)) {
1400 fsec
->fsec_group
= va
.va_guuid
;
1402 fsec
->fsec_group
= kauth_null_guid
;
1404 if (VATTR_IS_SUPPORTED(&va
, va_acl
) && (va
.va_acl
!= NULL
)) {
1405 bcopy(va
.va_acl
, &(fsec
->fsec_acl
), KAUTH_ACL_COPYSIZE(va
.va_acl
));
1407 fsec
->fsec_acl
.acl_entrycount
= KAUTH_FILESEC_NOACL
;
1413 /* Do not give the generation number out to unpriviledged users */
1414 if (va
.va_gen
&& !vfs_context_issuser(ctx
)) {
1421 sb64
->st_gen
= va
.va_gen
;
1423 sb
->st_gen
= va
.va_gen
;
1428 if (VATTR_IS_SUPPORTED(&va
, va_acl
) && va
.va_acl
!= NULL
)
1429 kauth_acl_free(va
.va_acl
);
1434 vn_stat(struct vnode
*vp
, void *sb
, kauth_filesec_t
*xsec
, int isstat64
, vfs_context_t ctx
)
1439 error
= mac_vnode_check_stat(ctx
, NOCRED
, vp
);
1445 if ((error
= vnode_authorize(vp
, NULL
, KAUTH_VNODE_READ_ATTRIBUTES
| KAUTH_VNODE_READ_SECURITY
, ctx
)) != 0)
1449 return(vn_stat_noauth(vp
, sb
, xsec
, isstat64
, ctx
, NOCRED
));
1454 * File table vnode ioctl routine.
1457 vn_ioctl(struct fileproc
*fp
, u_long com
, caddr_t data
, vfs_context_t ctx
)
1459 struct vnode
*vp
= ((struct vnode
*)fp
->f_fglob
->fg_data
);
1462 struct vnode
*ttyvp
;
1463 struct session
* sessp
;
1465 if ( (error
= vnode_getwithref(vp
)) ) {
1470 error
= mac_vnode_check_ioctl(ctx
, vp
, com
);
1475 switch (vp
->v_type
) {
1478 if (com
== FIONREAD
) {
1479 if ((error
= vnode_size(vp
, &file_size
, ctx
)) != 0)
1481 *(int *)data
= file_size
- fp
->f_fglob
->fg_offset
;
1484 if (com
== FIONBIO
|| com
== FIOASYNC
) { /* XXX */
1497 /* Should not be able to set block size from user space */
1498 if (com
== DKIOCSETBLOCKSIZE
) {
1503 if (com
== FIODTYPE
) {
1504 if (vp
->v_type
== VBLK
) {
1505 if (major(vp
->v_rdev
) >= nblkdev
) {
1509 *(int *)data
= bdevsw
[major(vp
->v_rdev
)].d_type
;
1511 } else if (vp
->v_type
== VCHR
) {
1512 if (major(vp
->v_rdev
) >= nchrdev
) {
1516 *(int *)data
= cdevsw
[major(vp
->v_rdev
)].d_type
;
1523 error
= VNOP_IOCTL(vp
, com
, data
, fp
->f_fglob
->fg_flag
, ctx
);
1525 if (error
== 0 && com
== TIOCSCTTY
) {
1526 sessp
= proc_session(vfs_context_proc(ctx
));
1528 session_lock(sessp
);
1529 ttyvp
= sessp
->s_ttyvp
;
1530 sessp
->s_ttyvp
= vp
;
1531 sessp
->s_ttyvid
= vnode_vid(vp
);
1532 session_unlock(sessp
);
1533 session_rele(sessp
);
1537 (void)vnode_put(vp
);
1542 * File table vnode select routine.
1545 vn_select(struct fileproc
*fp
, int which
, void *wql
, __unused vfs_context_t ctx
)
1548 struct vnode
* vp
= (struct vnode
*)fp
->f_fglob
->fg_data
;
1549 struct vfs_context context
;
1551 if ( (error
= vnode_getwithref(vp
)) == 0 ) {
1552 context
.vc_thread
= current_thread();
1553 context
.vc_ucred
= fp
->f_fglob
->fg_cred
;
1557 * XXX We should use a per thread credential here; minimally,
1558 * XXX the process credential should have a persistent
1559 * XXX reference on it before being passed in here.
1561 error
= mac_vnode_check_select(ctx
, vp
, which
);
1564 error
= VNOP_SELECT(vp
, which
, fp
->f_fglob
->fg_flag
, wql
, ctx
);
1566 (void)vnode_put(vp
);
1573 * File table vnode close routine.
1576 vn_closefile(struct fileglob
*fg
, vfs_context_t ctx
)
1578 struct vnode
*vp
= fg
->fg_data
;
1581 if ( (error
= vnode_getwithref(vp
)) == 0 ) {
1582 if (FILEGLOB_DTYPE(fg
) == DTYPE_VNODE
&&
1583 ((fg
->fg_flag
& FHASLOCK
) != 0 ||
1584 (fg
->fg_lflags
& FG_HAS_OFDLOCK
) != 0)) {
1586 .l_whence
= SEEK_SET
,
1592 if ((fg
->fg_flag
& FHASLOCK
) != 0)
1593 (void) VNOP_ADVLOCK(vp
, (caddr_t
)fg
,
1594 F_UNLCK
, &lf
, F_FLOCK
, ctx
, NULL
);
1596 if ((fg
->fg_lflags
& FG_HAS_OFDLOCK
) != 0)
1597 (void) VNOP_ADVLOCK(vp
, (caddr_t
)fg
,
1598 F_UNLCK
, &lf
, F_OFD_LOCK
, ctx
, NULL
);
1600 error
= vn_close(vp
, fg
->fg_flag
, ctx
);
1601 (void) vnode_put(vp
);
1607 * Returns: 0 Success
1611 vn_pathconf(vnode_t vp
, int name
, int32_t *retval
, vfs_context_t ctx
)
1614 struct vfs_attr vfa
;
1617 case _PC_EXTENDED_SECURITY_NP
:
1618 *retval
= vfs_extendedsecurity(vnode_mount(vp
)) ? 1 : 0;
1620 case _PC_AUTH_OPAQUE_NP
:
1621 *retval
= vfs_authopaque(vnode_mount(vp
));
1623 case _PC_2_SYMLINKS
:
1624 *retval
= 1; /* XXX NOTSUP on MSDOS, etc. */
1626 case _PC_ALLOC_SIZE_MIN
:
1627 *retval
= 1; /* XXX lie: 1 byte */
1629 case _PC_ASYNC_IO
: /* unistd.h: _POSIX_ASYNCHRONUS_IO */
1630 *retval
= 1; /* [AIO] option is supported */
1632 case _PC_PRIO_IO
: /* unistd.h: _POSIX_PRIORITIZED_IO */
1633 *retval
= 0; /* [PIO] option is not supported */
1635 case _PC_REC_INCR_XFER_SIZE
:
1636 *retval
= 4096; /* XXX go from MIN to MAX 4K at a time */
1638 case _PC_REC_MIN_XFER_SIZE
:
1639 *retval
= 4096; /* XXX recommend 4K minimum reads/writes */
1641 case _PC_REC_MAX_XFER_SIZE
:
1642 *retval
= 65536; /* XXX recommend 64K maximum reads/writes */
1644 case _PC_REC_XFER_ALIGN
:
1645 *retval
= 4096; /* XXX recommend page aligned buffers */
1647 case _PC_SYMLINK_MAX
:
1648 *retval
= 255; /* Minimum acceptable POSIX value */
1650 case _PC_SYNC_IO
: /* unistd.h: _POSIX_SYNCHRONIZED_IO */
1651 *retval
= 0; /* [SIO] option is not supported */
1653 case _PC_XATTR_SIZE_BITS
:
1654 /* The number of bits used to store maximum extended
1655 * attribute size in bytes. For example, if the maximum
1656 * attribute size supported by a file system is 128K, the
1657 * value returned will be 18. However a value 18 can mean
1658 * that the maximum attribute size can be anywhere from
1659 * (256KB - 1) to 128KB. As a special case, the resource
1660 * fork can have much larger size, and some file system
1661 * specific extended attributes can have smaller and preset
1662 * size; for example, Finder Info is always 32 bytes.
1664 memset(&vfa
, 0, sizeof(vfa
));
1666 VFSATTR_WANTED(&vfa
, f_capabilities
);
1667 if (vfs_getattr(vnode_mount(vp
), &vfa
, ctx
) == 0 &&
1668 (VFSATTR_IS_SUPPORTED(&vfa
, f_capabilities
)) &&
1669 (vfa
.f_capabilities
.capabilities
[VOL_CAPABILITIES_INTERFACES
] & VOL_CAP_INT_EXTENDED_ATTR
) &&
1670 (vfa
.f_capabilities
.valid
[VOL_CAPABILITIES_INTERFACES
] & VOL_CAP_INT_EXTENDED_ATTR
)) {
1671 /* Supports native extended attributes */
1672 error
= VNOP_PATHCONF(vp
, name
, retval
, ctx
);
1674 /* Number of bits used to represent the maximum size of
1675 * extended attribute stored in an Apple Double file.
1677 *retval
= AD_XATTR_SIZE_BITS
;
1681 error
= VNOP_PATHCONF(vp
, name
, retval
, ctx
);
1689 vn_kqfilt_add(struct fileproc
*fp
, struct knote
*kn
, vfs_context_t ctx
)
1695 vp
= (struct vnode
*)fp
->f_fglob
->fg_data
;
1698 * Don't attach a knote to a dead vnode.
1700 if ((error
= vget_internal(vp
, 0, VNODE_NODEAD
)) == 0) {
1701 switch (kn
->kn_filter
) {
1704 if (vnode_isfifo(vp
)) {
1705 /* We'll only watch FIFOs that use our fifofs */
1706 if (!(vp
->v_fifoinfo
&& vp
->v_fifoinfo
->fi_readsock
)) {
1710 } else if (!vnode_isreg(vp
)) {
1711 if (vnode_ischr(vp
)) {
1712 result
= spec_kqfilter(vp
, kn
);
1713 if ((kn
->kn_flags
& EV_ERROR
) == 0) {
1714 /* claimed by a special device */
1731 error
= mac_vnode_check_kqfilter(ctx
, fp
->f_fglob
->fg_cred
, kn
, vp
);
1738 kn
->kn_hook
= (void*)vp
;
1739 kn
->kn_hookid
= vnode_vid(vp
);
1740 kn
->kn_filtid
= EVFILTID_VN
;
1743 KNOTE_ATTACH(&vp
->v_knotes
, kn
);
1744 result
= filt_vnode_common(kn
, vp
, 0);
1748 * Ask the filesystem to provide remove notifications,
1749 * but ignore failure
1751 VNOP_MONITOR(vp
, 0, VNODE_MONITOR_BEGIN
, (void*) kn
, ctx
);
1759 kn
->kn_flags
= EV_ERROR
;
1760 kn
->kn_data
= error
;
1767 filt_vndetach(struct knote
*kn
)
1769 vfs_context_t ctx
= vfs_context_current();
1771 vp
= (struct vnode
*)kn
->kn_hook
;
1772 if (vnode_getwithvid(vp
, kn
->kn_hookid
))
1776 KNOTE_DETACH(&vp
->v_knotes
, kn
);
1780 * Tell a (generally networked) filesystem that we're no longer watching
1781 * If the FS wants to track contexts, it should still be using the one from
1782 * the VNODE_MONITOR_BEGIN.
1784 VNOP_MONITOR(vp
, 0, VNODE_MONITOR_END
, (void*)kn
, ctx
);
1790 * Used for EVFILT_READ
1792 * Takes only VFIFO or VREG. vnode is locked. We handle the "poll" case
1793 * differently than the regular case for VREG files. If not in poll(),
1794 * then we need to know current fileproc offset for VREG.
1797 vnode_readable_data_count(vnode_t vp
, off_t current_offset
, int ispoll
)
1799 if (vnode_isfifo(vp
)) {
1802 int err
= fifo_charcount(vp
, &cnt
);
1804 return (intptr_t)cnt
;
1810 } else if (vnode_isreg(vp
)) {
1816 amount
= vp
->v_un
.vu_ubcinfo
->ui_size
- current_offset
;
1817 if (amount
> (off_t
)INTPTR_MAX
) {
1819 } else if (amount
< (off_t
)INTPTR_MIN
) {
1822 return (intptr_t)amount
;
1825 panic("Should never have an EVFILT_READ except for reg or fifo.");
1831 * Used for EVFILT_WRITE.
1833 * For regular vnodes, we can always write (1). For named pipes,
1834 * see how much space there is in the buffer. Nothing else is covered.
1837 vnode_writable_space_count(vnode_t vp
)
1839 if (vnode_isfifo(vp
)) {
1842 int err
= fifo_freespace(vp
, &spc
);
1844 return (intptr_t)spc
;
1850 } else if (vnode_isreg(vp
)) {
1853 panic("Should never have an EVFILT_READ except for reg or fifo.");
1859 * Determine whether this knote should be active
1861 * This is kind of subtle.
1862 * --First, notice if the vnode has been revoked: in so, override hint
1863 * --EVFILT_READ knotes are checked no matter what the hint is
1864 * --Other knotes activate based on hint.
1865 * --If hint is revoke, set special flags and activate
1868 filt_vnode_common(struct knote
*kn
, vnode_t vp
, long hint
)
1872 lck_mtx_assert(&vp
->v_lock
, LCK_MTX_ASSERT_OWNED
);
1874 /* Special handling for vnodes that are in recycle or already gone */
1875 if (NOTE_REVOKE
== hint
) {
1876 kn
->kn_flags
|= (EV_EOF
| EV_ONESHOT
);
1879 if ((kn
->kn_filter
== EVFILT_VNODE
) && (kn
->kn_sfflags
& NOTE_REVOKE
)) {
1880 kn
->kn_fflags
|= NOTE_REVOKE
;
1883 switch(kn
->kn_filter
) {
1885 kn
->kn_data
= vnode_readable_data_count(vp
, kn
->kn_fp
->f_fglob
->fg_offset
, (kn
->kn_flags
& EV_POLL
));
1887 if (kn
->kn_data
!= 0) {
1892 kn
->kn_data
= vnode_writable_space_count(vp
);
1894 if (kn
->kn_data
!= 0) {
1899 /* Check events this note matches against the hint */
1900 if (kn
->kn_sfflags
& hint
) {
1901 kn
->kn_fflags
|= hint
; /* Set which event occurred */
1903 if (kn
->kn_fflags
!= 0) {
1908 panic("Invalid knote filter on a vnode!\n");
1915 filt_vnode(struct knote
*kn
, long hint
)
1917 vnode_t vp
= (struct vnode
*)kn
->kn_hook
;
1919 return filt_vnode_common(kn
, vp
, hint
);
1923 filt_vntouch(struct knote
*kn
, struct kevent_internal_s
*kev
)
1925 vnode_t vp
= (struct vnode
*)kn
->kn_hook
;
1930 if (vnode_getiocount(vp
, kn
->kn_hookid
, VNODE_NODEAD
| VNODE_WITHID
) != 0) {
1935 /* accept new input fflags mask */
1936 kn
->kn_sfflags
= kev
->fflags
;
1937 if ((kn
->kn_status
& KN_UDATA_SPECIFIC
) == 0)
1938 kn
->kn_udata
= kev
->udata
;
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
);