2 * Copyright (c) 2000-2008 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 static int vn_closefile(struct fileglob
*fp
, vfs_context_t ctx
);
116 static int vn_ioctl(struct fileproc
*fp
, u_long com
, caddr_t data
,
118 static int vn_read(struct fileproc
*fp
, struct uio
*uio
, int flags
,
120 static int vn_write(struct fileproc
*fp
, struct uio
*uio
, int flags
,
122 static int vn_select( struct fileproc
*fp
, int which
, void * wql
,
124 static int vn_kqfilt_add(struct fileproc
*fp
, struct knote
*kn
,
126 static void filt_vndetach(struct knote
*kn
);
127 static int filt_vnode(struct knote
*kn
, long hint
);
129 static int vn_kqfilt_remove(struct vnode
*vp
, uintptr_t ident
,
133 struct fileops vnops
=
134 { vn_read
, vn_write
, vn_ioctl
, vn_select
, vn_closefile
, vn_kqfilt_add
, NULL
};
136 struct filterops vnode_filtops
= {
139 .f_detach
= filt_vndetach
,
140 .f_event
= filt_vnode
144 * Common code for vnode open operations.
145 * Check permissions, and call the VNOP_OPEN or VNOP_CREATE routine.
147 * XXX the profusion of interfaces here is probably a bad thing.
150 vn_open(struct nameidata
*ndp
, int fmode
, int cmode
)
152 return(vn_open_modflags(ndp
, &fmode
, cmode
));
156 vn_open_modflags(struct nameidata
*ndp
, int *fmodep
, int cmode
)
158 struct vnode_attr va
;
161 VATTR_SET(&va
, va_mode
, cmode
);
163 return(vn_open_auth(ndp
, fmodep
, &va
));
167 * Open a file with authorization, updating the contents of the structures
168 * pointed to by ndp, fmodep, and vap as necessary to perform the requested
169 * operation. This function is used for both opens of existing files, and
170 * creation of new files.
172 * Parameters: ndp The nami data pointer describing the
174 * fmodep A pointer to an int containg the mode
175 * information to be used for the open
176 * vap A pointer to the vnode attribute
177 * descriptor to be used for the open
179 * Indirect: * Contents of the data structures pointed
180 * to by the parameters are modified as
181 * necessary to the requested operation.
186 * Notes: The kauth_filesec_t in 'vap', if any, is in host byte order.
188 * The contents of '*ndp' will be modified, based on the other
189 * arguments to this function, and to return file and directory
190 * data necessary to satisfy the requested operation.
192 * If the file does not exist and we are creating it, then the
193 * O_TRUNC flag will be cleared in '*fmodep' to indicate to the
194 * caller that the file was not truncated.
196 * If the file exists and the O_EXCL flag was not specified, then
197 * the O_CREAT flag will be cleared in '*fmodep' to indicate to
198 * the caller that the existing file was merely opened rather
201 * The contents of '*vap' will be modified as necessary to
202 * complete the operation, including setting of supported
203 * attribute, clearing of fields containing unsupported attributes
204 * in the request, if the request proceeds without them, etc..
206 * XXX: This function is too complicated in actings on its arguments
208 * XXX: We should enummerate the possible errno values here, and where
209 * in the code they originated.
212 vn_open_auth(struct nameidata
*ndp
, int *fmodep
, struct vnode_attr
*vap
)
216 vfs_context_t ctx
= ndp
->ni_cnd
.cn_context
;
219 uint32_t origcnflags
;
220 kauth_action_t action
;
226 origcnflags
= ndp
->ni_cnd
.cn_flags
;
227 if (fmode
& O_CREAT
) {
228 if ( (fmode
& O_DIRECTORY
) ) {
232 ndp
->ni_cnd
.cn_nameiop
= CREATE
;
233 /* Inherit USEDVP, vnode_open() supported flags only */
234 ndp
->ni_cnd
.cn_flags
&= (USEDVP
| NOCROSSMOUNT
| DOWHITEOUT
);
235 ndp
->ni_cnd
.cn_flags
|= LOCKPARENT
| LOCKLEAF
| AUDITVNPATH1
;
237 /* open calls are allowed for resource forks. */
238 ndp
->ni_cnd
.cn_flags
|= CN_ALLOWRSRCFORK
;
240 if ((fmode
& O_EXCL
) == 0 && (fmode
& O_NOFOLLOW
) == 0 && (origcnflags
& FOLLOW
) != 0)
241 ndp
->ni_cnd
.cn_flags
|= FOLLOW
;
242 if ( (error
= namei(ndp
)) )
247 /* not found, create */
249 /* must have attributes for a new file */
255 VATTR_SET(vap
, va_type
, VREG
);
257 error
= mac_vnode_check_create(ctx
,
258 dvp
, &ndp
->ni_cnd
, vap
);
263 /* authorize before creating */
264 if ((error
= vnode_authorize(dvp
, NULL
, KAUTH_VNODE_ADD_FILE
, ctx
)) != 0)
268 vap
->va_vaflags
|= VA_EXCLUSIVE
;
270 if (ndp
->ni_cnd
.cn_flags
& CN_WANTSRSRCFORK
) {
271 if ((error
= vnode_makenamedstream(dvp
, &ndp
->ni_vp
, XATTR_RESOURCEFORK_NAME
, 0, ctx
)) != 0)
275 if ((error
= vn_create(dvp
, &ndp
->ni_vp
, &ndp
->ni_cnd
, vap
, 0, ctx
)) != 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 if (need_fsevent(FSE_CREATE_FILE
, vp
)) {
296 add_fsevent(FSE_CREATE_FILE
, ctx
,
304 * nameidone has to happen before we vnode_put(dvp)
305 * and clear the ni_dvp field, since it may need
306 * to release the fs_nodelock on the dvp
318 * Check for a creation or unlink race.
320 if (((error
== EEXIST
) && !(fmode
& O_EXCL
)) ||
321 ((error
== ENOENT
) && (fmode
& O_CREAT
))){
332 if (fmode
& O_EXCL
) {
339 ndp
->ni_cnd
.cn_nameiop
= LOOKUP
;
340 /* Inherit USEDVP, vnode_open() supported flags only */
341 ndp
->ni_cnd
.cn_flags
&= (USEDVP
| NOCROSSMOUNT
| DOWHITEOUT
);
342 ndp
->ni_cnd
.cn_flags
|= FOLLOW
| LOCKLEAF
| AUDITVNPATH1
;
344 /* open calls are allowed for resource forks. */
345 ndp
->ni_cnd
.cn_flags
|= CN_ALLOWRSRCFORK
;
347 /* preserve NOFOLLOW from vnode_open() */
348 if (fmode
& O_NOFOLLOW
|| fmode
& O_SYMLINK
|| (origcnflags
& FOLLOW
) == 0) {
349 ndp
->ni_cnd
.cn_flags
&= ~FOLLOW
;
352 if ( (error
= namei(ndp
)) )
358 if ( (fmode
& O_DIRECTORY
) && vp
->v_type
!= VDIR
) {
364 if (vp
->v_type
== VSOCK
&& vp
->v_tag
!= VT_FDESC
) {
365 error
= EOPNOTSUPP
; /* Operation not supported on socket */
369 if (vp
->v_type
== VLNK
&& (fmode
& O_NOFOLLOW
) != 0) {
370 error
= ELOOP
; /* O_NOFOLLOW was specified and the target is a symbolic link */
374 /* authorize open of an existing file */
375 if ((fmode
& O_CREAT
) == 0) {
377 /* disallow write operations on directories */
378 if (vnode_isdir(vp
) && (fmode
& (FWRITE
| O_TRUNC
))) {
384 error
= mac_vnode_check_open(ctx
, vp
, fmode
);
389 /* compute action to be authorized */
392 action
|= KAUTH_VNODE_READ_DATA
;
394 if (fmode
& (FWRITE
| O_TRUNC
)) {
396 * If we are writing, appending, and not truncating,
397 * indicate that we are appending so that if the
398 * UF_APPEND or SF_APPEND bits are set, we do not deny
401 if ((fmode
& O_APPEND
) && !(fmode
& O_TRUNC
)) {
402 action
|= KAUTH_VNODE_APPEND_DATA
;
404 action
|= KAUTH_VNODE_WRITE_DATA
;
407 if ((error
= vnode_authorize(vp
, NULL
, action
, ctx
)) != 0)
412 // if the vnode is tagged VOPENEVT and the current process
413 // has the P_CHECKOPENEVT flag set, then we or in the O_EVTONLY
414 // flag to the open mode so that this open won't count against
415 // the vnode when carbon delete() does a vnode_isinuse() to see
416 // if a file is currently in use. this allows spotlight
417 // importers to not interfere with carbon apps that depend on
418 // the no-delete-if-busy semantics of carbon delete().
420 if ((vp
->v_flag
& VOPENEVT
) && (current_proc()->p_flag
& P_CHECKOPENEVT
)) {
426 if ( (error
= VNOP_OPEN(vp
, fmode
, ctx
)) ) {
429 if ( (error
= vnode_ref_ext(vp
, fmode
)) ) {
433 /* call out to allow 3rd party notification of open.
434 * Ignore result of kauth_authorize_fileop call.
436 kauth_authorize_fileop(vfs_context_ucred(ctx
), KAUTH_FILEOP_OPEN
,
442 VNOP_CLOSE(vp
, fmode
, ctx
);
447 /* Aggressively recycle shadow files if we error'd out during open() */
448 if ((vnode_isnamedstream(vp
)) &&
449 (vp
->v_parent
!= NULLVP
) &&
450 (vnode_isshadow(vp
))) {
456 * Check for a race against unlink. We had a vnode
457 * but according to vnode_authorize or VNOP_OPEN it
460 * EREDRIVEOPEN: means that we were hit by the tty allocation race.
462 if (((error
== ENOENT
) && (*fmodep
& O_CREAT
)) || (error
== EREDRIVEOPEN
)) {
470 #if vn_access_DEPRECATED
472 * Authorize an action against a vnode. This has been the canonical way to
473 * ensure that the credential/process/etc. referenced by a vfs_context
474 * is granted the rights called out in 'mode' against the vnode 'vp'.
476 * Unfortunately, the use of VREAD/VWRITE/VEXEC makes it very difficult
477 * to add support for more rights. As such, this interface will be deprecated
478 * and callers will use vnode_authorize instead.
481 vn_access(vnode_t vp
, int mode
, vfs_context_t context
)
483 kauth_action_t action
;
487 action
|= KAUTH_VNODE_READ_DATA
;
489 action
|= KAUTH_VNODE_WRITE_DATA
;
491 action
|= KAUTH_VNODE_EXECUTE
;
493 return(vnode_authorize(vp
, NULL
, action
, context
));
495 #endif /* vn_access_DEPRECATED */
501 vn_close(struct vnode
*vp
, int flags
, vfs_context_t ctx
)
506 if (flags
& FWASWRITTEN
) {
507 if (need_fsevent(FSE_CONTENT_MODIFIED
, vp
)) {
508 add_fsevent(FSE_CONTENT_MODIFIED
, ctx
,
516 /* Sync data from resource fork shadow file if needed. */
517 if ((vp
->v_flag
& VISNAMEDSTREAM
) &&
518 (vp
->v_parent
!= NULLVP
) &&
519 vnode_isshadow(vp
)) {
520 if (flags
& FWASWRITTEN
) {
521 (void) vnode_flushnamedstream(vp
->v_parent
, vp
, ctx
);
526 /* work around for foxhound */
527 if (vnode_isspec(vp
))
528 (void)vnode_rele_ext(vp
, flags
, 0);
530 error
= VNOP_CLOSE(vp
, flags
, ctx
);
532 if (!vnode_isspec(vp
))
533 (void)vnode_rele_ext(vp
, flags
, 0);
544 off_t swap_count
, this_count
;
545 off_t file_end
, read_end
;
550 * Reading from a swap file will get you zeroes.
555 swap_count
= uio_resid(uio
);
557 file_end
= ubc_getsize(vp
);
558 read_end
= uio
->uio_offset
+ uio_resid(uio
);
559 if (uio
->uio_offset
>= file_end
) {
560 /* uio starts after end of file: nothing to read */
562 } else if (read_end
> file_end
) {
563 /* uio extends beyond end of file: stop before that */
564 swap_count
-= (read_end
- file_end
);
567 while (swap_count
> 0) {
568 if (my_swap_page
== NULL
) {
569 MALLOC(my_swap_page
, char *, PAGE_SIZE
,
571 memset(my_swap_page
, '\0', PAGE_SIZE
);
572 /* add an end-of-line to keep line counters happy */
573 my_swap_page
[PAGE_SIZE
-1] = '\n';
575 this_count
= swap_count
;
576 if (this_count
> PAGE_SIZE
) {
577 this_count
= PAGE_SIZE
;
580 prev_resid
= uio_resid(uio
);
581 error
= uiomove((caddr_t
) my_swap_page
,
587 swap_count
-= (prev_resid
- uio_resid(uio
));
589 if (my_swap_page
!= NULL
) {
590 FREE(my_swap_page
, M_TEMP
);
597 * Package up an I/O request on a vnode into a uio and do it.
615 result
= vn_rdwr_64(rw
,
617 (uint64_t)(uintptr_t)base
,
626 /* "resid" should be bounded above by "len," which is an int */
627 if (aresid
!= NULL
) {
650 struct vfs_context context
;
652 char uio_buf
[ UIO_SIZEOF(1) ];
654 context
.vc_thread
= current_thread();
655 context
.vc_ucred
= cred
;
657 if (UIO_SEG_IS_USER_SPACE(segflg
)) {
658 spacetype
= proc_is64bit(p
) ? UIO_USERSPACE64
: UIO_USERSPACE32
;
661 spacetype
= UIO_SYSSPACE
;
663 auio
= uio_createwithbuffer(1, offset
, spacetype
, rw
,
664 &uio_buf
[0], sizeof(uio_buf
));
665 uio_addiov(auio
, base
, len
);
669 * IO_NOAUTH should be re-examined.
670 * Likely that mediation should be performed in caller.
672 if ((ioflg
& IO_NOAUTH
) == 0) {
673 /* passed cred is fp->f_cred */
675 error
= mac_vnode_check_read(&context
, cred
, vp
);
677 error
= mac_vnode_check_write(&context
, cred
, vp
);
682 if (rw
== UIO_READ
) {
683 if (vnode_isswap(vp
)) {
684 error
= vn_read_swapfile(vp
, auio
);
686 error
= VNOP_READ(vp
, auio
, ioflg
, &context
);
689 error
= VNOP_WRITE(vp
, auio
, ioflg
, &context
);
694 *aresid
= uio_resid(auio
);
696 if (uio_resid(auio
) && error
== 0)
702 * File table vnode read routine.
705 vn_read(struct fileproc
*fp
, struct uio
*uio
, int flags
, vfs_context_t ctx
)
711 vp
= (struct vnode
*)fp
->f_fglob
->fg_data
;
712 if ( (error
= vnode_getwithref(vp
)) ) {
717 error
= mac_vnode_check_read(ctx
, vfs_context_ucred(ctx
), vp
);
725 if (fp
->f_fglob
->fg_flag
& FNONBLOCK
)
727 if ((fp
->f_fglob
->fg_flag
& FNOCACHE
) || vnode_isnocache(vp
))
728 ioflag
|= IO_NOCACHE
;
729 if (fp
->f_fglob
->fg_flag
& FNORDAHEAD
)
732 if ((flags
& FOF_OFFSET
) == 0)
733 uio
->uio_offset
= fp
->f_fglob
->fg_offset
;
734 count
= uio_resid(uio
);
736 if (vnode_isswap(vp
)) {
737 /* special case for swap files */
738 error
= vn_read_swapfile(vp
, uio
);
740 error
= VNOP_READ(vp
, uio
, ioflag
, ctx
);
742 if ((flags
& FOF_OFFSET
) == 0)
743 fp
->f_fglob
->fg_offset
+= count
- uio_resid(uio
);
751 * File table vnode write routine.
754 vn_write(struct fileproc
*fp
, struct uio
*uio
, int flags
, vfs_context_t ctx
)
761 int residcount
, oldcount
;
762 proc_t p
= vfs_context_proc(ctx
);
765 vp
= (struct vnode
*)fp
->f_fglob
->fg_data
;
766 if ( (error
= vnode_getwithref(vp
)) ) {
771 error
= mac_vnode_check_write(ctx
, vfs_context_ucred(ctx
), vp
);
779 if (vp
->v_type
== VREG
&& (fp
->f_fglob
->fg_flag
& O_APPEND
))
781 if (fp
->f_fglob
->fg_flag
& FNONBLOCK
)
783 if ((fp
->f_fglob
->fg_flag
& FNOCACHE
) || vnode_isnocache(vp
))
784 ioflag
|= IO_NOCACHE
;
786 * Treat synchronous mounts and O_FSYNC on the fd as equivalent.
788 * XXX We treat O_DSYNC as O_FSYNC for now, since we can not delay
789 * XXX the non-essential metadata without some additional VFS work;
790 * XXX the intent at this point is to plumb the interface for it.
792 if ((fp
->f_fglob
->fg_flag
& (O_FSYNC
|O_DSYNC
)) ||
793 (vp
->v_mount
&& (vp
->v_mount
->mnt_flag
& MNT_SYNCHRONOUS
))) {
797 if ((flags
& FOF_OFFSET
) == 0) {
798 uio
->uio_offset
= fp
->f_fglob
->fg_offset
;
799 count
= uio_resid(uio
);
801 if (((flags
& FOF_OFFSET
) == 0) &&
802 vfs_context_proc(ctx
) && (vp
->v_type
== VREG
) &&
803 (((rlim_t
)(uio
->uio_offset
+ uio_resid(uio
)) > p
->p_rlimit
[RLIMIT_FSIZE
].rlim_cur
) ||
804 ((rlim_t
)uio_resid(uio
) > (p
->p_rlimit
[RLIMIT_FSIZE
].rlim_cur
- uio
->uio_offset
)))) {
806 * If the requested residual would cause us to go past the
807 * administrative limit, then we need to adjust the residual
808 * down to cause fewer bytes than requested to be written. If
809 * we can't do that (e.g. the residual is already 1 byte),
810 * then we fail the write with EFBIG.
812 residcount
= uio_resid(uio
);
813 if ((rlim_t
)(uio
->uio_offset
+ uio_resid(uio
)) > p
->p_rlimit
[RLIMIT_FSIZE
].rlim_cur
) {
814 clippedsize
= (uio
->uio_offset
+ uio_resid(uio
)) - p
->p_rlimit
[RLIMIT_FSIZE
].rlim_cur
;
815 } else if ((rlim_t
)uio_resid(uio
) > (p
->p_rlimit
[RLIMIT_FSIZE
].rlim_cur
- uio
->uio_offset
)) {
816 clippedsize
= (p
->p_rlimit
[RLIMIT_FSIZE
].rlim_cur
- uio
->uio_offset
);
818 if (clippedsize
>= residcount
) {
824 uio_setresid(uio
, residcount
-clippedsize
);
826 if ((flags
& FOF_OFFSET
) != 0) {
827 /* for pwrite, append should be ignored */
828 ioflag
&= ~IO_APPEND
;
829 if (p
&& (vp
->v_type
== VREG
) &&
830 ((rlim_t
)uio
->uio_offset
>= p
->p_rlimit
[RLIMIT_FSIZE
].rlim_cur
)) {
835 if (p
&& (vp
->v_type
== VREG
) &&
836 ((rlim_t
)(uio
->uio_offset
+ uio_resid(uio
)) > p
->p_rlimit
[RLIMIT_FSIZE
].rlim_cur
)) {
837 //Debugger("vn_bwrite:overstepping the bounds");
838 residcount
= uio_resid(uio
);
839 clippedsize
= (uio
->uio_offset
+ uio_resid(uio
)) - p
->p_rlimit
[RLIMIT_FSIZE
].rlim_cur
;
841 uio_setresid(uio
, residcount
-clippedsize
);
845 error
= VNOP_WRITE(vp
, uio
, ioflag
, ctx
);
848 oldcount
= uio_resid(uio
);
849 uio_setresid(uio
, oldcount
+ clippedsize
);
852 if ((flags
& FOF_OFFSET
) == 0) {
853 if (ioflag
& IO_APPEND
)
854 fp
->f_fglob
->fg_offset
= uio
->uio_offset
;
856 fp
->f_fglob
->fg_offset
+= count
- uio_resid(uio
);
860 * Set the credentials on successful writes
862 if ((error
== 0) && (vp
->v_tag
== VT_NFS
) && (UBCINFOEXISTS(vp
))) {
864 * When called from aio subsystem, we only have the proc from
865 * which to get the credential, at this point, so use that
866 * instead. This means aio functions are incompatible with
867 * per-thread credentials (aio operations are proxied). We
868 * can't easily correct the aio vs. settid race in this case
869 * anyway, so we disallow it.
871 if ((flags
& FOF_PCRED
) == 0) {
872 ubc_setthreadcred(vp
, p
, current_thread());
882 * File table vnode stat routine.
890 vn_stat_noauth(struct vnode
*vp
, void *sbptr
, kauth_filesec_t
*xsec
, int isstat64
, vfs_context_t ctx
)
892 struct vnode_attr va
;
895 kauth_filesec_t fsec
;
896 struct stat
*sb
= (struct stat
*)0; /* warning avoidance ; protected by isstat64 */
897 struct stat64
* sb64
= (struct stat64
*)0; /* warning avoidance ; protected by isstat64 */
900 sb64
= (struct stat64
*)sbptr
;
902 sb
= (struct stat
*)sbptr
;
903 memset(&va
, 0, sizeof(va
));
905 VATTR_WANTED(&va
, va_fsid
);
906 VATTR_WANTED(&va
, va_fileid
);
907 VATTR_WANTED(&va
, va_mode
);
908 VATTR_WANTED(&va
, va_type
);
909 VATTR_WANTED(&va
, va_nlink
);
910 VATTR_WANTED(&va
, va_uid
);
911 VATTR_WANTED(&va
, va_gid
);
912 VATTR_WANTED(&va
, va_rdev
);
913 VATTR_WANTED(&va
, va_data_size
);
914 VATTR_WANTED(&va
, va_access_time
);
915 VATTR_WANTED(&va
, va_modify_time
);
916 VATTR_WANTED(&va
, va_change_time
);
917 VATTR_WANTED(&va
, va_create_time
);
918 VATTR_WANTED(&va
, va_flags
);
919 VATTR_WANTED(&va
, va_gen
);
920 VATTR_WANTED(&va
, va_iosize
);
921 /* lower layers will synthesise va_total_alloc from va_data_size if required */
922 VATTR_WANTED(&va
, va_total_alloc
);
924 VATTR_WANTED(&va
, va_uuuid
);
925 VATTR_WANTED(&va
, va_guuid
);
926 VATTR_WANTED(&va
, va_acl
);
928 error
= vnode_getattr(vp
, &va
, ctx
);
932 * Copy from vattr table
935 sb64
->st_dev
= va
.va_fsid
;
936 sb64
->st_ino
= (ino64_t
)va
.va_fileid
;
939 sb
->st_dev
= va
.va_fsid
;
940 sb
->st_ino
= (ino_t
)va
.va_fileid
;
943 switch (vp
->v_type
) {
970 sb64
->st_mode
= mode
;
971 sb64
->st_nlink
= VATTR_IS_SUPPORTED(&va
, va_nlink
) ? (u_int16_t
)va
.va_nlink
: 1;
972 sb64
->st_uid
= va
.va_uid
;
973 sb64
->st_gid
= va
.va_gid
;
974 sb64
->st_rdev
= va
.va_rdev
;
975 sb64
->st_size
= va
.va_data_size
;
976 sb64
->st_atimespec
= va
.va_access_time
;
977 sb64
->st_mtimespec
= va
.va_modify_time
;
978 sb64
->st_ctimespec
= va
.va_change_time
;
979 sb64
->st_birthtimespec
=
980 VATTR_IS_SUPPORTED(&va
, va_create_time
) ? va
.va_create_time
: va
.va_change_time
;
981 sb64
->st_blksize
= va
.va_iosize
;
982 sb64
->st_flags
= va
.va_flags
;
983 sb64
->st_blocks
= roundup(va
.va_total_alloc
, 512) / 512;
986 sb
->st_nlink
= VATTR_IS_SUPPORTED(&va
, va_nlink
) ? (u_int16_t
)va
.va_nlink
: 1;
987 sb
->st_uid
= va
.va_uid
;
988 sb
->st_gid
= va
.va_gid
;
989 sb
->st_rdev
= va
.va_rdev
;
990 sb
->st_size
= va
.va_data_size
;
991 sb
->st_atimespec
= va
.va_access_time
;
992 sb
->st_mtimespec
= va
.va_modify_time
;
993 sb
->st_ctimespec
= va
.va_change_time
;
994 sb
->st_blksize
= va
.va_iosize
;
995 sb
->st_flags
= va
.va_flags
;
996 sb
->st_blocks
= roundup(va
.va_total_alloc
, 512) / 512;
999 /* if we're interested in exended security data and we got an ACL */
1001 if (!VATTR_IS_SUPPORTED(&va
, va_acl
) &&
1002 !VATTR_IS_SUPPORTED(&va
, va_uuuid
) &&
1003 !VATTR_IS_SUPPORTED(&va
, va_guuid
)) {
1004 *xsec
= KAUTH_FILESEC_NONE
;
1007 if (VATTR_IS_SUPPORTED(&va
, va_acl
) && (va
.va_acl
!= NULL
)) {
1008 fsec
= kauth_filesec_alloc(va
.va_acl
->acl_entrycount
);
1010 fsec
= kauth_filesec_alloc(0);
1016 fsec
->fsec_magic
= KAUTH_FILESEC_MAGIC
;
1017 if (VATTR_IS_SUPPORTED(&va
, va_uuuid
)) {
1018 fsec
->fsec_owner
= va
.va_uuuid
;
1020 fsec
->fsec_owner
= kauth_null_guid
;
1022 if (VATTR_IS_SUPPORTED(&va
, va_guuid
)) {
1023 fsec
->fsec_group
= va
.va_guuid
;
1025 fsec
->fsec_group
= kauth_null_guid
;
1027 if (VATTR_IS_SUPPORTED(&va
, va_acl
) && (va
.va_acl
!= NULL
)) {
1028 bcopy(va
.va_acl
, &(fsec
->fsec_acl
), KAUTH_ACL_COPYSIZE(va
.va_acl
));
1030 fsec
->fsec_acl
.acl_entrycount
= KAUTH_FILESEC_NOACL
;
1036 /* Do not give the generation number out to unpriviledged users */
1037 if (va
.va_gen
&& !vfs_context_issuser(ctx
)) {
1044 sb64
->st_gen
= va
.va_gen
;
1046 sb
->st_gen
= va
.va_gen
;
1051 if (VATTR_IS_SUPPORTED(&va
, va_acl
) && va
.va_acl
!= NULL
)
1052 kauth_acl_free(va
.va_acl
);
1057 vn_stat(struct vnode
*vp
, void *sb
, kauth_filesec_t
*xsec
, int isstat64
, vfs_context_t ctx
)
1062 error
= mac_vnode_check_stat(ctx
, NOCRED
, vp
);
1068 if ((error
= vnode_authorize(vp
, NULL
, KAUTH_VNODE_READ_ATTRIBUTES
| KAUTH_VNODE_READ_SECURITY
, ctx
)) != 0)
1072 return(vn_stat_noauth(vp
, sb
, xsec
, isstat64
, ctx
));
1077 * File table vnode ioctl routine.
1080 vn_ioctl(struct fileproc
*fp
, u_long com
, caddr_t data
, vfs_context_t ctx
)
1082 struct vnode
*vp
= ((struct vnode
*)fp
->f_fglob
->fg_data
);
1085 struct vnode
*ttyvp
;
1087 struct session
* sessp
;
1089 if ( (error
= vnode_getwithref(vp
)) ) {
1094 error
= mac_vnode_check_ioctl(ctx
, vp
, com
);
1099 switch (vp
->v_type
) {
1102 if (com
== FIONREAD
) {
1103 if ((error
= vnode_size(vp
, &file_size
, ctx
)) != 0)
1105 *(int *)data
= file_size
- fp
->f_fglob
->fg_offset
;
1108 if (com
== FIONBIO
|| com
== FIOASYNC
) { /* XXX */
1121 /* Should not be able to set block size from user space */
1122 if (com
== DKIOCSETBLOCKSIZE
) {
1127 if (com
== FIODTYPE
) {
1128 if (vp
->v_type
== VBLK
) {
1129 if (major(vp
->v_rdev
) >= nblkdev
) {
1133 *(int *)data
= bdevsw
[major(vp
->v_rdev
)].d_type
;
1135 } else if (vp
->v_type
== VCHR
) {
1136 if (major(vp
->v_rdev
) >= nchrdev
) {
1140 *(int *)data
= cdevsw
[major(vp
->v_rdev
)].d_type
;
1147 error
= VNOP_IOCTL(vp
, com
, data
, fp
->f_fglob
->fg_flag
, ctx
);
1149 if (error
== 0 && com
== TIOCSCTTY
) {
1152 funnel_state
= thread_funnel_set(kernel_flock
, TRUE
);
1153 sessp
= proc_session(vfs_context_proc(ctx
));
1155 session_lock(sessp
);
1156 ttyvp
= sessp
->s_ttyvp
;
1157 sessp
->s_ttyvp
= vp
;
1158 sessp
->s_ttyvid
= vnode_vid(vp
);
1159 session_unlock(sessp
);
1160 session_rele(sessp
);
1161 thread_funnel_set(kernel_flock
, funnel_state
);
1168 (void)vnode_put(vp
);
1173 * File table vnode select routine.
1176 vn_select(struct fileproc
*fp
, int which
, void *wql
, __unused vfs_context_t ctx
)
1179 struct vnode
* vp
= (struct vnode
*)fp
->f_fglob
->fg_data
;
1180 struct vfs_context context
;
1182 if ( (error
= vnode_getwithref(vp
)) == 0 ) {
1183 context
.vc_thread
= current_thread();
1184 context
.vc_ucred
= fp
->f_fglob
->fg_cred
;
1188 * XXX We should use a per thread credential here; minimally,
1189 * XXX the process credential should have a persistent
1190 * XXX reference on it before being passed in here.
1192 error
= mac_vnode_check_select(ctx
, vp
, which
);
1195 error
= VNOP_SELECT(vp
, which
, fp
->f_fglob
->fg_flag
, wql
, ctx
);
1197 (void)vnode_put(vp
);
1204 * File table vnode close routine.
1207 vn_closefile(struct fileglob
*fg
, vfs_context_t ctx
)
1209 struct vnode
*vp
= (struct vnode
*)fg
->fg_data
;
1213 if ( (error
= vnode_getwithref(vp
)) == 0 ) {
1215 if ((fg
->fg_flag
& FHASLOCK
) && fg
->fg_type
== DTYPE_VNODE
) {
1216 lf
.l_whence
= SEEK_SET
;
1219 lf
.l_type
= F_UNLCK
;
1221 (void)VNOP_ADVLOCK(vp
, (caddr_t
)fg
, F_UNLCK
, &lf
, F_FLOCK
, ctx
);
1223 error
= vn_close(vp
, fg
->fg_flag
, ctx
);
1225 (void)vnode_put(vp
);
1231 * Returns: 0 Success
1235 vn_pathconf(vnode_t vp
, int name
, int32_t *retval
, vfs_context_t ctx
)
1240 case _PC_EXTENDED_SECURITY_NP
:
1241 *retval
= vfs_extendedsecurity(vnode_mount(vp
)) ? 1 : 0;
1243 case _PC_AUTH_OPAQUE_NP
:
1244 *retval
= vfs_authopaque(vnode_mount(vp
));
1246 case _PC_2_SYMLINKS
:
1247 *retval
= 1; /* XXX NOTSUP on MSDOS, etc. */
1249 case _PC_ALLOC_SIZE_MIN
:
1250 *retval
= 1; /* XXX lie: 1 byte */
1252 case _PC_ASYNC_IO
: /* unistd.h: _POSIX_ASYNCHRONUS_IO */
1253 *retval
= 1; /* [AIO] option is supported */
1255 case _PC_PRIO_IO
: /* unistd.h: _POSIX_PRIORITIZED_IO */
1256 *retval
= 0; /* [PIO] option is not supported */
1258 case _PC_REC_INCR_XFER_SIZE
:
1259 *retval
= 4096; /* XXX go from MIN to MAX 4K at a time */
1261 case _PC_REC_MIN_XFER_SIZE
:
1262 *retval
= 4096; /* XXX recommend 4K minimum reads/writes */
1264 case _PC_REC_MAX_XFER_SIZE
:
1265 *retval
= 65536; /* XXX recommend 64K maximum reads/writes */
1267 case _PC_REC_XFER_ALIGN
:
1268 *retval
= 4096; /* XXX recommend page aligned buffers */
1270 case _PC_SYMLINK_MAX
:
1271 *retval
= 255; /* Minimum acceptable POSIX value */
1273 case _PC_SYNC_IO
: /* unistd.h: _POSIX_SYNCHRONIZED_IO */
1274 *retval
= 0; /* [SIO] option is not supported */
1277 error
= VNOP_PATHCONF(vp
, name
, retval
, ctx
);
1285 vn_kqfilt_add(struct fileproc
*fp
, struct knote
*kn
, vfs_context_t ctx
)
1290 vp
= (struct vnode
*)fp
->f_fglob
->fg_data
;
1293 * Don't attach a knote to a dead vnode.
1295 if ((error
= vget_internal(vp
, 0, VNODE_NODEAD
)) == 0) {
1296 switch (kn
->kn_filter
) {
1299 if (vnode_isfifo(vp
)) {
1300 /* We'll only watch FIFOs that use our fifofs */
1301 if (!(vp
->v_fifoinfo
&& vp
->v_fifoinfo
->fi_readsock
)) {
1305 } else if (!vnode_isreg(vp
)) {
1306 if (vnode_isspec(vp
) &&
1307 (error
= spec_kqfilter(vp
, kn
)) == 0) {
1308 /* claimed by a special device */
1328 error
= mac_vnode_check_kqfilter(ctx
, fp
->f_fglob
->fg_cred
, kn
, vp
);
1335 kn
->kn_hook
= (void*)vp
;
1336 kn
->kn_hookid
= vnode_vid(vp
);
1337 kn
->kn_fop
= &vnode_filtops
;
1340 KNOTE_ATTACH(&vp
->v_knotes
, kn
);
1343 /* Ask the filesystem to provide remove notifications, but ignore failure */
1344 VNOP_MONITOR(vp
, 0, VNODE_MONITOR_BEGIN
, (void*) kn
, ctx
);
1353 filt_vndetach(struct knote
*kn
)
1355 vfs_context_t ctx
= vfs_context_current();
1357 vp
= (struct vnode
*)kn
->kn_hook
;
1358 if (vnode_getwithvid(vp
, kn
->kn_hookid
))
1362 KNOTE_DETACH(&vp
->v_knotes
, kn
);
1366 * Tell a (generally networked) filesystem that we're no longer watching
1367 * If the FS wants to track contexts, it should still be using the one from
1368 * the VNODE_MONITOR_BEGIN.
1370 VNOP_MONITOR(vp
, 0, VNODE_MONITOR_END
, (void*)kn
, ctx
);
1376 * Used for EVFILT_READ
1378 * Takes only VFIFO or VREG. vnode is locked. We handle the "poll" case
1379 * differently than the regular case for VREG files. If not in poll(),
1380 * then we need to know current fileproc offset for VREG.
1383 vnode_readable_data_count(vnode_t vp
, off_t current_offset
, int ispoll
)
1385 if (vnode_isfifo(vp
)) {
1387 int err
= fifo_charcount(vp
, &cnt
);
1389 return (intptr_t)cnt
;
1393 } else if (vnode_isreg(vp
)) {
1399 amount
= vp
->v_un
.vu_ubcinfo
->ui_size
- current_offset
;
1400 if (amount
> (off_t
)INTPTR_MAX
) {
1402 } else if (amount
< (off_t
)INTPTR_MIN
) {
1405 return (intptr_t)amount
;
1408 panic("Should never have an EVFILT_READ except for reg or fifo.");
1414 * Used for EVFILT_WRITE.
1416 * For regular vnodes, we can always write (1). For named pipes,
1417 * see how much space there is in the buffer. Nothing else is covered.
1420 vnode_writable_space_count(vnode_t vp
)
1422 if (vnode_isfifo(vp
)) {
1424 int err
= fifo_freespace(vp
, &spc
);
1426 return (intptr_t)spc
;
1430 } else if (vnode_isreg(vp
)) {
1433 panic("Should never have an EVFILT_READ except for reg or fifo.");
1439 * Determine whether this knote should be active
1441 * This is kind of subtle.
1442 * --First, notice if the vnode has been revoked: in so, override hint
1443 * --EVFILT_READ knotes are checked no matter what the hint is
1444 * --Other knotes activate based on hint.
1445 * --If hint is revoke, set special flags and activate
1448 filt_vnode(struct knote
*kn
, long hint
)
1450 struct vnode
*vp
= (struct vnode
*)kn
->kn_hook
;
1454 if ((vnode_getwithvid(vp
, kn
->kn_hookid
) != 0)) {
1461 /* NOTE_REVOKE is special, as it is only sent during vnode reclaim */
1462 if (NOTE_REVOKE
== hint
) {
1463 kn
->kn_flags
|= (EV_EOF
| EV_ONESHOT
);
1466 if ((kn
->kn_filter
== EVFILT_VNODE
) && (kn
->kn_sfflags
& NOTE_REVOKE
)) {
1467 kn
->kn_fflags
|= NOTE_REVOKE
;
1470 switch(kn
->kn_filter
) {
1472 kn
->kn_data
= vnode_readable_data_count(vp
, kn
->kn_fp
->f_fglob
->fg_offset
, (kn
->kn_flags
& EV_POLL
));
1474 if (kn
->kn_data
!= 0) {
1479 kn
->kn_data
= vnode_writable_space_count(vp
);
1481 if (kn
->kn_data
!= 0) {
1486 /* Check events this note matches against the hint */
1487 if (kn
->kn_sfflags
& hint
) {
1488 kn
->kn_fflags
|= hint
; /* Set which event occurred */
1490 if (kn
->kn_fflags
!= 0) {
1495 panic("Invalid knote filter on a vnode!\n");