2 * Copyright (c) 2000-2005 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_OSREFERENCE_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
10 * License may not be used to create, or enable the creation or
11 * redistribution of, unlawful or unlicensed copies of an Apple operating
12 * system, or to circumvent, violate, or enable the circumvention or
13 * violation of, any terms of an Apple operating system software license
16 * Please obtain a copy of the License at
17 * http://www.opensource.apple.com/apsl/ and read it before using this
20 * The Original Code and all software distributed under the License are
21 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
22 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
23 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
24 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
25 * Please see the License for the specific language governing rights and
26 * limitations under the License.
28 * @APPLE_LICENSE_OSREFERENCE_HEADER_END@
30 /* Copyright (c) 1995 NeXT Computer, Inc. All Rights Reserved */
32 * Copyright (c) 1989, 1993
33 * The Regents of the University of California. All rights reserved.
34 * (c) UNIX System Laboratories, Inc.
35 * All or some portions of this file are derived from material licensed
36 * to the University of California by American Telephone and Telegraph
37 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
38 * the permission of UNIX System Laboratories, Inc.
40 * Redistribution and use in source and binary forms, with or without
41 * modification, are permitted provided that the following conditions
43 * 1. Redistributions of source code must retain the above copyright
44 * notice, this list of conditions and the following disclaimer.
45 * 2. Redistributions in binary form must reproduce the above copyright
46 * notice, this list of conditions and the following disclaimer in the
47 * documentation and/or other materials provided with the distribution.
48 * 3. All advertising materials mentioning features or use of this software
49 * must display the following acknowledgement:
50 * This product includes software developed by the University of
51 * California, Berkeley and its contributors.
52 * 4. Neither the name of the University nor the names of its contributors
53 * may be used to endorse or promote products derived from this software
54 * without specific prior written permission.
56 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
57 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
58 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
59 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
60 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
61 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
62 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
63 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
64 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
65 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
68 * @(#)vfs_subr.c 8.31 (Berkeley) 5/26/95
72 * External virtual filesystem routines
78 #include <sys/param.h>
79 #include <sys/systm.h>
80 #include <sys/proc_internal.h>
81 #include <sys/kauth.h>
82 #include <sys/mount_internal.h>
85 #include <sys/vnode_internal.h>
87 #include <sys/namei.h>
88 #include <sys/ucred.h>
89 #include <sys/buf_internal.h>
90 #include <sys/errno.h>
91 #include <sys/malloc.h>
92 #include <sys/domain.h>
94 #include <sys/syslog.h>
95 #include <sys/ubc_internal.h>
97 #include <sys/sysctl.h>
98 #include <sys/filedesc.h>
99 #include <sys/event.h>
100 #include <sys/kdebug.h>
101 #include <sys/kauth.h>
102 #include <sys/user.h>
103 #include <miscfs/fifofs/fifo.h>
106 #include <machine/spl.h>
109 #include <kern/assert.h>
111 #include <miscfs/specfs/specdev.h>
113 #include <mach/mach_types.h>
114 #include <mach/memory_object_types.h>
116 extern lck_grp_t
*vnode_lck_grp
;
117 extern lck_attr_t
*vnode_lck_attr
;
120 extern lck_mtx_t
* mnt_list_mtx_lock
;
122 enum vtype iftovt_tab
[16] = {
123 VNON
, VFIFO
, VCHR
, VNON
, VDIR
, VNON
, VBLK
, VNON
,
124 VREG
, VNON
, VLNK
, VNON
, VSOCK
, VNON
, VNON
, VBAD
,
126 int vttoif_tab
[9] = {
127 0, S_IFREG
, S_IFDIR
, S_IFBLK
, S_IFCHR
, S_IFLNK
,
128 S_IFSOCK
, S_IFIFO
, S_IFMT
,
131 extern int ubc_isinuse_locked(vnode_t
, int, int);
132 extern kern_return_t
adjust_vm_object_cache(vm_size_t oval
, vm_size_t nval
);
134 static void vnode_list_add(vnode_t
);
135 static void vnode_list_remove(vnode_t
);
137 static errno_t
vnode_drain(vnode_t
);
138 static void vgone(vnode_t
);
139 static void vclean(vnode_t vp
, int flag
, proc_t p
);
140 static void vnode_reclaim_internal(vnode_t
, int, int);
142 static void vnode_dropiocount (vnode_t
, int);
143 static errno_t
vnode_getiocount(vnode_t vp
, int locked
, int vid
, int vflags
);
144 static int vget_internal(vnode_t
, int, int);
146 static vnode_t
checkalias(vnode_t vp
, dev_t nvp_rdev
);
147 static int vnode_reload(vnode_t
);
148 static int vnode_isinuse_locked(vnode_t
, int, int);
150 static void insmntque(vnode_t vp
, mount_t mp
);
151 mount_t
mount_list_lookupby_fsid(fsid_t
*, int, int);
152 static int mount_getvfscnt(void);
153 static int mount_fillfsids(fsid_t
*, int );
154 static void vnode_iterate_setup(mount_t
);
155 static int vnode_umount_preflight(mount_t
, vnode_t
, int);
156 static int vnode_iterate_prepare(mount_t
);
157 static int vnode_iterate_reloadq(mount_t
);
158 static void vnode_iterate_clear(mount_t
);
160 TAILQ_HEAD(freelst
, vnode
) vnode_free_list
; /* vnode free list */
161 TAILQ_HEAD(inactivelst
, vnode
) vnode_inactive_list
; /* vnode inactive list */
162 struct mntlist mountlist
; /* mounted filesystem list */
163 static int nummounts
= 0;
166 #define VLISTCHECK(fun, vp, list) \
167 if ((vp)->v_freelist.tqe_prev == (struct vnode **)0xdeadb) \
168 panic("%s: %s vnode not on %slist", (fun), (list), (list));
170 #define VINACTIVECHECK(fun, vp, expected) \
172 int __is_inactive = ISSET((vp)->v_flag, VUINACTIVE); \
173 if (__is_inactive ^ expected) \
174 panic("%s: %sinactive vnode, expected %s", (fun), \
175 __is_inactive? "" : "not ", \
176 expected? "inactive": "not inactive"); \
179 #define VLISTCHECK(fun, vp, list)
180 #define VINACTIVECHECK(fun, vp, expected)
181 #endif /* DIAGNOSTIC */
183 #define VLISTNONE(vp) \
185 (vp)->v_freelist.tqe_next = (struct vnode *)0; \
186 (vp)->v_freelist.tqe_prev = (struct vnode **)0xdeadb; \
189 #define VONLIST(vp) \
190 ((vp)->v_freelist.tqe_prev != (struct vnode **)0xdeadb)
192 /* remove a vnode from free vnode list */
193 #define VREMFREE(fun, vp) \
195 VLISTCHECK((fun), (vp), "free"); \
196 TAILQ_REMOVE(&vnode_free_list, (vp), v_freelist); \
201 /* remove a vnode from inactive vnode list */
202 #define VREMINACTIVE(fun, vp) \
204 VLISTCHECK((fun), (vp), "inactive"); \
205 VINACTIVECHECK((fun), (vp), VUINACTIVE); \
206 TAILQ_REMOVE(&vnode_inactive_list, (vp), v_freelist); \
207 CLR((vp)->v_flag, VUINACTIVE); \
213 * Have to declare first two locks as actual data even if !MACH_SLOCKS, since
214 * a pointers to them get passed around.
216 void * mntvnode_slock
;
218 void * spechash_slock
;
221 * vnodetarget is the amount of vnodes we expect to get back
222 * from the the inactive vnode list and VM object cache.
223 * As vnreclaim() is a mainly cpu bound operation for faster
224 * processers this number could be higher.
225 * Having this number too high introduces longer delays in
226 * the execution of new_vnode().
228 unsigned long vnodetarget
; /* target for vnreclaim() */
229 #define VNODE_FREE_TARGET 20 /* Default value for vnodetarget */
232 * We need quite a few vnodes on the free list to sustain the
233 * rapid stat() the compilation process does, and still benefit from the name
234 * cache. Having too few vnodes on the free list causes serious disk
235 * thrashing as we cycle through them.
237 #define VNODE_FREE_MIN 300 /* freelist should have at least these many */
240 * We need to get vnodes back from the VM object cache when a certain #
241 * of vnodes are reused from the freelist. This is essential for the
242 * caching to be effective in the namecache and the buffer cache [for the
245 #define VNODE_TOOMANY_REUSED (VNODE_FREE_MIN/4)
248 * If we have enough vnodes on the freelist we do not want to reclaim
249 * the vnodes from the VM object cache.
251 #define VNODE_FREE_ENOUGH (VNODE_FREE_MIN + (VNODE_FREE_MIN/2))
254 * Initialize the vnode management data structures.
256 __private_extern__
void
259 TAILQ_INIT(&vnode_free_list
);
260 TAILQ_INIT(&vnode_inactive_list
);
261 TAILQ_INIT(&mountlist
);
264 vnodetarget
= VNODE_FREE_TARGET
;
267 * Scale the vm_object_cache to accomodate the vnodes
270 (void) adjust_vm_object_cache(0, desiredvnodes
- VNODE_FREE_MIN
);
273 /* Reset the VM Object Cache with the values passed in */
274 __private_extern__ kern_return_t
275 reset_vmobjectcache(unsigned int val1
, unsigned int val2
)
277 vm_size_t oval
= val1
- VNODE_FREE_MIN
;
280 if(val2
< VNODE_FREE_MIN
)
283 nval
= val2
- VNODE_FREE_MIN
;
285 return(adjust_vm_object_cache(oval
, nval
));
289 /* the timeout is in 10 msecs */
291 vnode_waitforwrites(vnode_t vp
, int output_target
, int slpflag
, int slptimeout
, char *msg
) {
295 KERNEL_DEBUG(0x3010280 | DBG_FUNC_START
, (int)vp
, output_target
, vp
->v_numoutput
, 0, 0);
297 if (vp
->v_numoutput
> output_target
) {
303 while ((vp
->v_numoutput
> output_target
) && error
== 0) {
305 vp
->v_flag
|= VTHROTTLED
;
307 vp
->v_flag
|= VBWAIT
;
308 ts
.tv_sec
= (slptimeout
/100);
309 ts
.tv_nsec
= (slptimeout
% 1000) * 10 * NSEC_PER_USEC
* 1000 ;
310 error
= msleep((caddr_t
)&vp
->v_numoutput
, &vp
->v_lock
, (slpflag
| (PRIBIO
+ 1)), msg
, &ts
);
314 KERNEL_DEBUG(0x3010280 | DBG_FUNC_END
, (int)vp
, output_target
, vp
->v_numoutput
, error
, 0);
321 vnode_startwrite(vnode_t vp
) {
323 OSAddAtomic(1, &vp
->v_numoutput
);
328 vnode_writedone(vnode_t vp
)
333 OSAddAtomic(-1, &vp
->v_numoutput
);
337 if (vp
->v_numoutput
< 0)
338 panic("vnode_writedone: numoutput < 0");
340 if ((vp
->v_flag
& VTHROTTLED
) && (vp
->v_numoutput
< (VNODE_ASYNC_THROTTLE
/ 3))) {
341 vp
->v_flag
&= ~VTHROTTLED
;
344 if ((vp
->v_flag
& VBWAIT
) && (vp
->v_numoutput
== 0)) {
345 vp
->v_flag
&= ~VBWAIT
;
351 wakeup((caddr_t
)&vp
->v_numoutput
);
358 vnode_hasdirtyblks(vnode_t vp
)
360 struct cl_writebehind
*wbp
;
363 * Not taking the buf_mtxp as there is little
364 * point doing it. Even if the lock is taken the
365 * state can change right after that. If their
366 * needs to be a synchronization, it must be driven
369 if (vp
->v_dirtyblkhd
.lh_first
)
372 if (!UBCINFOEXISTS(vp
))
375 wbp
= vp
->v_ubcinfo
->cl_wbehind
;
377 if (wbp
&& (wbp
->cl_number
|| wbp
->cl_scmap
))
384 vnode_hascleanblks(vnode_t vp
)
387 * Not taking the buf_mtxp as there is little
388 * point doing it. Even if the lock is taken the
389 * state can change right after that. If their
390 * needs to be a synchronization, it must be driven
393 if (vp
->v_cleanblkhd
.lh_first
)
399 vnode_iterate_setup(mount_t mp
)
401 while (mp
->mnt_lflag
& MNT_LITER
) {
402 mp
->mnt_lflag
|= MNT_LITERWAIT
;
403 msleep((caddr_t
)mp
, &mp
->mnt_mlock
, PVFS
, "vnode_iterate_setup", 0);
406 mp
->mnt_lflag
|= MNT_LITER
;
411 vnode_umount_preflight(mount_t mp
, vnode_t skipvp
, int flags
)
415 TAILQ_FOREACH(vp
, &mp
->mnt_vnodelist
, v_mntvnodes
) {
416 if (vp
->v_type
== VDIR
)
420 if ((flags
& SKIPSYSTEM
) && ((vp
->v_flag
& VSYSTEM
) ||
421 (vp
->v_flag
& VNOFLUSH
)))
423 if ((flags
& SKIPSWAP
) && (vp
->v_flag
& VSWAP
))
425 if ((flags
& WRITECLOSE
) &&
426 (vp
->v_writecount
== 0 || vp
->v_type
!= VREG
))
428 /* Look for busy vnode */
429 if (((vp
->v_usecount
!= 0) &&
430 ((vp
->v_usecount
- vp
->v_kusecount
) != 0)))
438 * This routine prepares iteration by moving all the vnodes to worker queue
439 * called with mount lock held
442 vnode_iterate_prepare(mount_t mp
)
446 if (TAILQ_EMPTY(&mp
->mnt_vnodelist
)) {
451 vp
= TAILQ_FIRST(&mp
->mnt_vnodelist
);
452 vp
->v_mntvnodes
.tqe_prev
= &(mp
->mnt_workerqueue
.tqh_first
);
453 mp
->mnt_workerqueue
.tqh_first
= mp
->mnt_vnodelist
.tqh_first
;
454 mp
->mnt_workerqueue
.tqh_last
= mp
->mnt_vnodelist
.tqh_last
;
456 TAILQ_INIT(&mp
->mnt_vnodelist
);
457 if (mp
->mnt_newvnodes
.tqh_first
!= NULL
)
458 panic("vnode_iterate_prepare: newvnode when entering vnode");
459 TAILQ_INIT(&mp
->mnt_newvnodes
);
465 /* called with mount lock held */
467 vnode_iterate_reloadq(mount_t mp
)
471 /* add the remaining entries in workerq to the end of mount vnode list */
472 if (!TAILQ_EMPTY(&mp
->mnt_workerqueue
)) {
474 mvp
= TAILQ_LAST(&mp
->mnt_vnodelist
, vnodelst
);
476 /* Joining the workerque entities to mount vnode list */
478 mvp
->v_mntvnodes
.tqe_next
= mp
->mnt_workerqueue
.tqh_first
;
480 mp
->mnt_vnodelist
.tqh_first
= mp
->mnt_workerqueue
.tqh_first
;
481 mp
->mnt_workerqueue
.tqh_first
->v_mntvnodes
.tqe_prev
= mp
->mnt_vnodelist
.tqh_last
;
482 mp
->mnt_vnodelist
.tqh_last
= mp
->mnt_workerqueue
.tqh_last
;
483 TAILQ_INIT(&mp
->mnt_workerqueue
);
486 /* add the newvnodes to the head of mount vnode list */
487 if (!TAILQ_EMPTY(&mp
->mnt_newvnodes
)) {
489 nlvp
= TAILQ_LAST(&mp
->mnt_newvnodes
, vnodelst
);
491 mp
->mnt_newvnodes
.tqh_first
->v_mntvnodes
.tqe_prev
= &mp
->mnt_vnodelist
.tqh_first
;
492 nlvp
->v_mntvnodes
.tqe_next
= mp
->mnt_vnodelist
.tqh_first
;
493 if(mp
->mnt_vnodelist
.tqh_first
)
494 mp
->mnt_vnodelist
.tqh_first
->v_mntvnodes
.tqe_prev
= &nlvp
->v_mntvnodes
.tqe_next
;
496 mp
->mnt_vnodelist
.tqh_last
= mp
->mnt_newvnodes
.tqh_last
;
497 mp
->mnt_vnodelist
.tqh_first
= mp
->mnt_newvnodes
.tqh_first
;
498 TAILQ_INIT(&mp
->mnt_newvnodes
);
507 vnode_iterate_clear(mount_t mp
)
509 mp
->mnt_lflag
&= ~MNT_LITER
;
510 if (mp
->mnt_lflag
& MNT_LITERWAIT
) {
511 mp
->mnt_lflag
&= ~MNT_LITERWAIT
;
518 vnode_iterate(mp
, flags
, callout
, arg
)
521 int (*callout
)(struct vnode
*, void *);
530 vnode_iterate_setup(mp
);
532 /* it is returns 0 then there is nothing to do */
533 retval
= vnode_iterate_prepare(mp
);
536 vnode_iterate_clear(mp
);
541 /* iterate over all the vnodes */
542 while (!TAILQ_EMPTY(&mp
->mnt_workerqueue
)) {
543 vp
= TAILQ_FIRST(&mp
->mnt_workerqueue
);
544 TAILQ_REMOVE(&mp
->mnt_workerqueue
, vp
, v_mntvnodes
);
545 TAILQ_INSERT_TAIL(&mp
->mnt_vnodelist
, vp
, v_mntvnodes
);
547 if ((vp
->v_data
== NULL
) || (vp
->v_type
== VNON
) || (vp
->v_mount
!= mp
)) {
552 if ( vget_internal(vp
, vid
, (flags
| VNODE_NODEAD
| VNODE_WITHID
| VNODE_NOSUSPEND
))) {
556 if (flags
& VNODE_RELOAD
) {
558 * we're reloading the filesystem
559 * cast out any inactive vnodes...
561 if (vnode_reload(vp
)) {
562 /* vnode will be recycled on the refcount drop */
569 retval
= callout(vp
, arg
);
573 case VNODE_RETURNED_DONE
:
575 if (retval
== VNODE_RETURNED_DONE
) {
582 case VNODE_CLAIMED_DONE
:
594 (void)vnode_iterate_reloadq(mp
);
595 vnode_iterate_clear(mp
);
601 mount_lock_renames(mount_t mp
)
603 lck_mtx_lock(&mp
->mnt_renamelock
);
607 mount_unlock_renames(mount_t mp
)
609 lck_mtx_unlock(&mp
->mnt_renamelock
);
613 mount_lock(mount_t mp
)
615 lck_mtx_lock(&mp
->mnt_mlock
);
619 mount_unlock(mount_t mp
)
621 lck_mtx_unlock(&mp
->mnt_mlock
);
626 mount_ref(mount_t mp
, int locked
)
639 mount_drop(mount_t mp
, int locked
)
646 if (mp
->mnt_count
== 0 && (mp
->mnt_lflag
& MNT_LDRAIN
))
647 wakeup(&mp
->mnt_lflag
);
655 mount_iterref(mount_t mp
, int locked
)
661 if (mp
->mnt_iterref
< 0) {
672 mount_isdrained(mount_t mp
, int locked
)
678 if (mp
->mnt_iterref
< 0)
688 mount_iterdrop(mount_t mp
)
692 wakeup(&mp
->mnt_iterref
);
697 mount_iterdrain(mount_t mp
)
700 while (mp
->mnt_iterref
)
701 msleep((caddr_t
)&mp
->mnt_iterref
, mnt_list_mtx_lock
, PVFS
, "mount_iterdrain", 0 );
702 /* mount iterations drained */
703 mp
->mnt_iterref
= -1;
707 mount_iterreset(mount_t mp
)
710 if (mp
->mnt_iterref
== -1)
715 /* always called with mount lock held */
717 mount_refdrain(mount_t mp
)
719 if (mp
->mnt_lflag
& MNT_LDRAIN
)
720 panic("already in drain");
721 mp
->mnt_lflag
|= MNT_LDRAIN
;
723 while (mp
->mnt_count
)
724 msleep((caddr_t
)&mp
->mnt_lflag
, &mp
->mnt_mlock
, PVFS
, "mount_drain", 0 );
726 if (mp
->mnt_vnodelist
.tqh_first
!= NULL
)
727 panic("mount_refdrain: dangling vnode");
729 mp
->mnt_lflag
&= ~MNT_LDRAIN
;
736 * Mark a mount point as busy. Used to synchronize access and to delay
740 vfs_busy(mount_t mp
, int flags
)
744 if (mp
->mnt_lflag
& MNT_LDEAD
)
747 if (mp
->mnt_lflag
& MNT_LUNMOUNT
) {
748 if (flags
& LK_NOWAIT
)
753 if (mp
->mnt_lflag
& MNT_LDEAD
) {
757 if (mp
->mnt_lflag
& MNT_LUNMOUNT
) {
758 mp
->mnt_lflag
|= MNT_LWAIT
;
760 * Since all busy locks are shared except the exclusive
761 * lock granted when unmounting, the only place that a
762 * wakeup needs to be done is at the release of the
763 * exclusive lock at the end of dounmount.
765 msleep((caddr_t
)mp
, &mp
->mnt_mlock
, (PVFS
| PDROP
), "vfsbusy", 0 );
771 lck_rw_lock_shared(&mp
->mnt_rwlock
);
774 * until we are granted the rwlock, it's possible for the mount point to
775 * change state, so reevaluate before granting the vfs_busy
777 if (mp
->mnt_lflag
& (MNT_LDEAD
| MNT_LUNMOUNT
)) {
778 lck_rw_done(&mp
->mnt_rwlock
);
785 * Free a busy filesystem.
789 vfs_unbusy(mount_t mp
)
791 lck_rw_done(&mp
->mnt_rwlock
);
797 vfs_rootmountfailed(mount_t mp
) {
800 mp
->mnt_vtable
->vfc_refcount
--;
805 mount_lock_destroy(mp
);
807 FREE_ZONE(mp
, sizeof(struct mount
), M_MOUNT
);
811 * Lookup a filesystem type, and if found allocate and initialize
812 * a mount structure for it.
814 * Devname is usually updated by mount(8) after booting.
817 vfs_rootmountalloc_internal(struct vfstable
*vfsp
, const char *devname
)
821 mp
= _MALLOC_ZONE((u_long
)sizeof(struct mount
), M_MOUNT
, M_WAITOK
);
822 bzero((char *)mp
, (u_long
)sizeof(struct mount
));
824 /* Initialize the default IO constraints */
825 mp
->mnt_maxreadcnt
= mp
->mnt_maxwritecnt
= MAXPHYS
;
826 mp
->mnt_segreadcnt
= mp
->mnt_segwritecnt
= 32;
827 mp
->mnt_maxsegreadsize
= mp
->mnt_maxreadcnt
;
828 mp
->mnt_maxsegwritesize
= mp
->mnt_maxwritecnt
;
829 mp
->mnt_devblocksize
= DEV_BSIZE
;
832 (void)vfs_busy(mp
, LK_NOWAIT
);
834 TAILQ_INIT(&mp
->mnt_vnodelist
);
835 TAILQ_INIT(&mp
->mnt_workerqueue
);
836 TAILQ_INIT(&mp
->mnt_newvnodes
);
838 mp
->mnt_vtable
= vfsp
;
839 mp
->mnt_op
= vfsp
->vfc_vfsops
;
840 mp
->mnt_flag
= MNT_RDONLY
| MNT_ROOTFS
;
841 mp
->mnt_vnodecovered
= NULLVP
;
842 //mp->mnt_stat.f_type = vfsp->vfc_typenum;
843 mp
->mnt_flag
|= vfsp
->vfc_flags
& MNT_VISFLAGMASK
;
846 vfsp
->vfc_refcount
++;
849 strncpy(mp
->mnt_vfsstat
.f_fstypename
, vfsp
->vfc_name
, MFSTYPENAMELEN
);
850 mp
->mnt_vfsstat
.f_mntonname
[0] = '/';
851 (void) copystr((char *)devname
, mp
->mnt_vfsstat
.f_mntfromname
, MAXPATHLEN
- 1, 0);
857 vfs_rootmountalloc(const char *fstypename
, const char *devname
, mount_t
*mpp
)
859 struct vfstable
*vfsp
;
861 for (vfsp
= vfsconf
; vfsp
; vfsp
= vfsp
->vfc_next
)
862 if (!strcmp(vfsp
->vfc_name
, fstypename
))
867 *mpp
= vfs_rootmountalloc_internal(vfsp
, devname
);
877 * Find an appropriate filesystem to use for the root. If a filesystem
878 * has not been preselected, walk through the list of known filesystems
879 * trying those that have mountroot routines, and try them until one
880 * works or we have tried them all.
882 extern int (*mountroot
)(void);
887 struct vfstable
*vfsp
;
888 struct vfs_context context
;
892 if (mountroot
!= NULL
) {
894 * used for netboot which follows a different set of rules
896 error
= (*mountroot
)();
899 if ((error
= bdevvp(rootdev
, &rootvp
))) {
900 printf("vfs_mountroot: can't setup bdevvp\n");
903 context
.vc_proc
= current_proc();
904 context
.vc_ucred
= kauth_cred_get();
906 for (vfsp
= vfsconf
; vfsp
; vfsp
= vfsp
->vfc_next
) {
907 if (vfsp
->vfc_mountroot
== NULL
)
910 mp
= vfs_rootmountalloc_internal(vfsp
, "root_device");
911 mp
->mnt_devvp
= rootvp
;
913 if ((error
= (*vfsp
->vfc_mountroot
)(mp
, rootvp
, &context
)) == 0) {
914 mp
->mnt_devvp
->v_specflags
|= SI_MOUNTEDON
;
921 * cache the IO attributes for the underlying physical media...
922 * an error return indicates the underlying driver doesn't
923 * support all the queries necessary... however, reasonable
924 * defaults will have been set, so no reason to bail or care
926 vfs_init_io_attributes(rootvp
, mp
);
928 * get rid of iocount reference returned
929 * by bdevvp... it will have also taken
930 * a usecount reference which we want to keep
936 vfs_rootmountfailed(mp
);
939 printf("%s_mountroot failed: %d\n", vfsp
->vfc_name
, error
);
945 * Lookup a mount point by filesystem identifier.
947 extern mount_t
vfs_getvfs_locked(fsid_t
*);
953 return (mount_list_lookupby_fsid(fsid
, 0, 0));
957 vfs_getvfs_locked(fsid
)
960 return(mount_list_lookupby_fsid(fsid
, 1, 0));
964 vfs_getvfs_by_mntonname(u_char
*path
)
966 mount_t retmp
= (mount_t
)0;
970 TAILQ_FOREACH(mp
, &mountlist
, mnt_list
) {
971 if (!strcmp(mp
->mnt_vfsstat
.f_mntonname
, path
)) {
981 /* generation number for creation of new fsids */
982 u_short mntid_gen
= 0;
984 * Get a new unique fsid
997 /* generate a new fsid */
998 mtype
= mp
->mnt_vtable
->vfc_typenum
;
999 if (++mntid_gen
== 0)
1001 tfsid
.val
[0] = makedev(nblkdev
+ mtype
, mntid_gen
);
1002 tfsid
.val
[1] = mtype
;
1004 TAILQ_FOREACH(nmp
, &mountlist
, mnt_list
) {
1005 while (vfs_getvfs_locked(&tfsid
)) {
1006 if (++mntid_gen
== 0)
1008 tfsid
.val
[0] = makedev(nblkdev
+ mtype
, mntid_gen
);
1011 mp
->mnt_vfsstat
.f_fsid
.val
[0] = tfsid
.val
[0];
1012 mp
->mnt_vfsstat
.f_fsid
.val
[1] = tfsid
.val
[1];
1013 mount_list_unlock();
1017 * Routines having to do with the management of the vnode table.
1019 extern int (**dead_vnodeop_p
)(void *);
1020 long numvnodes
, freevnodes
;
1021 long inactivevnodes
;
1025 * Move a vnode from one mount queue to another.
1028 insmntque(vnode_t vp
, mount_t mp
)
1032 * Delete from old mount point vnode list, if on one.
1034 if ( (lmp
= vp
->v_mount
) != NULL
&& lmp
!= dead_mountp
) {
1035 if ((vp
->v_lflag
& VNAMED_MOUNT
) == 0)
1036 panic("insmntque: vp not in mount vnode list");
1037 vp
->v_lflag
&= ~VNAMED_MOUNT
;
1043 if (vp
->v_mntvnodes
.tqe_next
== NULL
) {
1044 if (TAILQ_LAST(&lmp
->mnt_vnodelist
, vnodelst
) == vp
)
1045 TAILQ_REMOVE(&lmp
->mnt_vnodelist
, vp
, v_mntvnodes
);
1046 else if (TAILQ_LAST(&lmp
->mnt_newvnodes
, vnodelst
) == vp
)
1047 TAILQ_REMOVE(&lmp
->mnt_newvnodes
, vp
, v_mntvnodes
);
1048 else if (TAILQ_LAST(&lmp
->mnt_workerqueue
, vnodelst
) == vp
)
1049 TAILQ_REMOVE(&lmp
->mnt_workerqueue
, vp
, v_mntvnodes
);
1051 vp
->v_mntvnodes
.tqe_next
->v_mntvnodes
.tqe_prev
= vp
->v_mntvnodes
.tqe_prev
;
1052 *vp
->v_mntvnodes
.tqe_prev
= vp
->v_mntvnodes
.tqe_next
;
1054 vp
->v_mntvnodes
.tqe_next
= 0;
1055 vp
->v_mntvnodes
.tqe_prev
= 0;
1061 * Insert into list of vnodes for the new mount point, if available.
1063 if ((vp
->v_mount
= mp
) != NULL
) {
1065 if ((vp
->v_mntvnodes
.tqe_next
!= 0) && (vp
->v_mntvnodes
.tqe_prev
!= 0))
1066 panic("vp already in mount list");
1067 if (mp
->mnt_lflag
& MNT_LITER
)
1068 TAILQ_INSERT_HEAD(&mp
->mnt_newvnodes
, vp
, v_mntvnodes
);
1070 TAILQ_INSERT_HEAD(&mp
->mnt_vnodelist
, vp
, v_mntvnodes
);
1071 if (vp
->v_lflag
& VNAMED_MOUNT
)
1072 panic("insmntque: vp already in mount vnode list");
1073 if ((vp
->v_freelist
.tqe_prev
!= (struct vnode
**)0xdeadb))
1074 panic("insmntque: vp on the free list\n");
1075 vp
->v_lflag
|= VNAMED_MOUNT
;
1083 * Create a vnode for a block device.
1084 * Used for root filesystem, argdev, and swap areas.
1085 * Also used for memory file system special devices.
1088 bdevvp(dev_t dev
, vnode_t
*vpp
)
1092 struct vnode_fsparam vfsp
;
1093 struct vfs_context context
;
1100 context
.vc_proc
= current_proc();
1101 context
.vc_ucred
= FSCRED
;
1103 vfsp
.vnfs_mp
= (struct mount
*)0;
1104 vfsp
.vnfs_vtype
= VBLK
;
1105 vfsp
.vnfs_str
= "bdevvp";
1107 vfsp
.vnfs_fsnode
= 0;
1109 vfsp
.vnfs_vops
= spec_vnodeop_p
;
1110 vfsp
.vnfs_rdev
= dev
;
1111 vfsp
.vnfs_filesize
= 0;
1113 vfsp
.vnfs_flags
= VNFS_NOCACHE
| VNFS_CANTCACHE
;
1115 vfsp
.vnfs_marksystem
= 0;
1116 vfsp
.vnfs_markroot
= 0;
1118 if ( (error
= vnode_create(VNCREATE_FLAVOR
, VCREATESIZE
, &vfsp
, &nvp
)) ) {
1122 if ( (error
= vnode_ref(nvp
)) ) {
1123 panic("bdevvp failed: vnode_ref");
1126 if ( (error
= VNOP_FSYNC(nvp
, MNT_WAIT
, &context
)) ) {
1127 panic("bdevvp failed: fsync");
1130 if ( (error
= buf_invalidateblks(nvp
, BUF_WRITE_DATA
, 0, 0)) ) {
1131 panic("bdevvp failed: invalidateblks");
1134 if ( (error
= VNOP_OPEN(nvp
, FREAD
, &context
)) ) {
1135 panic("bdevvp failed: open");
1144 * Check to see if the new vnode represents a special device
1145 * for which we already have a vnode (either because of
1146 * bdevvp() or because of a different vnode representing
1147 * the same block device). If such an alias exists, deallocate
1148 * the existing contents and return the aliased vnode. The
1149 * caller is responsible for filling it with its new contents.
1152 checkalias(nvp
, nvp_rdev
)
1153 register struct vnode
*nvp
;
1160 vpp
= &speclisth
[SPECHASH(nvp_rdev
)];
1164 for (vp
= *vpp
; vp
; vp
= vp
->v_specnext
) {
1165 if (nvp_rdev
== vp
->v_rdev
&& nvp
->v_type
== vp
->v_type
) {
1173 if (vnode_getwithvid(vp
,vid
)) {
1177 * Termination state is checked in vnode_getwithvid
1182 * Alias, but not in use, so flush it out.
1184 if ((vp
->v_iocount
== 1) && (vp
->v_usecount
== 0)) {
1185 vnode_reclaim_internal(vp
, 1, 0);
1191 if (vp
== NULL
|| vp
->v_tag
!= VT_NON
) {
1192 MALLOC_ZONE(nvp
->v_specinfo
, struct specinfo
*, sizeof(struct specinfo
),
1193 M_SPECINFO
, M_WAITOK
);
1194 bzero(nvp
->v_specinfo
, sizeof(struct specinfo
));
1195 nvp
->v_rdev
= nvp_rdev
;
1196 nvp
->v_specflags
= 0;
1197 nvp
->v_speclastr
= -1;
1200 nvp
->v_hashchain
= vpp
;
1201 nvp
->v_specnext
= *vpp
;
1206 nvp
->v_flag
|= VALIASED
;
1207 vp
->v_flag
|= VALIASED
;
1218 * Get a reference on a particular vnode and lock it if requested.
1219 * If the vnode was on the inactive list, remove it from the list.
1220 * If the vnode was on the free list, remove it from the list and
1221 * move it to inactive list as needed.
1222 * The vnode lock bit is set if the vnode is being eliminated in
1223 * vgone. The process is awakened when the transition is completed,
1224 * and an error returned to indicate that the vnode is no longer
1225 * usable (possibly having been changed to a new file system type).
1228 vget_internal(vnode_t vp
, int vid
, int vflags
)
1235 if (vflags
& VNODE_WITHID
)
1238 vpid
= vp
->v_id
; // save off the original v_id
1240 if ((vflags
& VNODE_WRITEABLE
) && (vp
->v_writecount
== 0))
1242 * vnode to be returned only if it has writers opened
1246 error
= vnode_getiocount(vp
, 1, vpid
, vflags
);
1254 vnode_ref(vnode_t vp
)
1257 return (vnode_ref_ext(vp
, 0));
1261 vnode_ref_ext(vnode_t vp
, int fmode
)
1268 * once all the current call sites have been fixed to insure they have
1269 * taken an iocount, we can toughen this assert up and insist that the
1270 * iocount is non-zero... a non-zero usecount doesn't insure correctness
1272 if (vp
->v_iocount
<= 0 && vp
->v_usecount
<= 0)
1273 panic("vnode_ref_ext: vp %x has no valid reference %d, %d", vp
, vp
->v_iocount
, vp
->v_usecount
);
1276 * if you are the owner of drain/termination, can acquire usecount
1278 if ((vp
->v_lflag
& (VL_DRAIN
| VL_TERMINATE
| VL_DEAD
))) {
1279 if (vp
->v_owner
!= current_thread()) {
1286 if (fmode
& FWRITE
) {
1287 if (++vp
->v_writecount
<= 0)
1288 panic("vnode_ref_ext: v_writecount");
1290 if (fmode
& O_EVTONLY
) {
1291 if (++vp
->v_kusecount
<= 0)
1292 panic("vnode_ref_ext: v_kusecount");
1302 * put the vnode on appropriate free list.
1303 * called with vnode LOCKED
1306 vnode_list_add(vnode_t vp
)
1310 * if it is already on a list or non zero references return
1312 if (VONLIST(vp
) || (vp
->v_usecount
!= 0) || (vp
->v_iocount
!= 0))
1317 * insert at tail of LRU list or at head if VAGE or VL_DEAD is set
1319 if ((vp
->v_flag
& VAGE
) || (vp
->v_lflag
& VL_DEAD
)) {
1320 TAILQ_INSERT_HEAD(&vnode_free_list
, vp
, v_freelist
);
1321 vp
->v_flag
&= ~VAGE
;
1323 TAILQ_INSERT_TAIL(&vnode_free_list
, vp
, v_freelist
);
1327 vnode_list_unlock();
1331 * remove the vnode from appropriate free list.
1334 vnode_list_remove(vnode_t vp
)
1337 * we want to avoid taking the list lock
1338 * in the case where we're not on the free
1339 * list... this will be true for most
1340 * directories and any currently in use files
1342 * we're guaranteed that we can't go from
1343 * the not-on-list state to the on-list
1344 * state since we hold the vnode lock...
1345 * all calls to vnode_list_add are done
1346 * under the vnode lock... so we can
1347 * check for that condition (the prevelant one)
1348 * without taking the list lock
1353 * however, we're not guaranteed that
1354 * we won't go from the on-list state
1355 * to the non-on-list state until we
1356 * hold the vnode_list_lock... this
1357 * is due to new_vnode removing vnodes
1358 * from the free list uder the list_lock
1359 * w/o the vnode lock... so we need to
1360 * check again whether we're currently
1364 VREMFREE("vnode_list_remove", vp
);
1367 vnode_list_unlock();
1373 vnode_rele(vnode_t vp
)
1375 vnode_rele_internal(vp
, 0, 0, 0);
1380 vnode_rele_ext(vnode_t vp
, int fmode
, int dont_reenter
)
1382 vnode_rele_internal(vp
, fmode
, dont_reenter
, 0);
1387 vnode_rele_internal(vnode_t vp
, int fmode
, int dont_reenter
, int locked
)
1389 struct vfs_context context
;
1394 if (--vp
->v_usecount
< 0)
1395 panic("vnode_rele_ext: vp %x usecount -ve : %d", vp
, vp
->v_usecount
);
1397 if (fmode
& FWRITE
) {
1398 if (--vp
->v_writecount
< 0)
1399 panic("vnode_rele_ext: vp %x writecount -ve : %d", vp
, vp
->v_writecount
);
1401 if (fmode
& O_EVTONLY
) {
1402 if (--vp
->v_kusecount
< 0)
1403 panic("vnode_rele_ext: vp %x kusecount -ve : %d", vp
, vp
->v_kusecount
);
1405 if ((vp
->v_iocount
> 0) || (vp
->v_usecount
> 0)) {
1407 * vnode is still busy... if we're the last
1408 * usecount, mark for a future call to VNOP_INACTIVE
1409 * when the iocount finally drops to 0
1411 if (vp
->v_usecount
== 0) {
1412 vp
->v_lflag
|= VL_NEEDINACTIVE
;
1413 vp
->v_flag
&= ~(VNOCACHE_DATA
| VRAOFF
);
1419 vp
->v_flag
&= ~(VNOCACHE_DATA
| VRAOFF
);
1421 if ( (vp
->v_lflag
& (VL_TERMINATE
| VL_DEAD
)) || dont_reenter
) {
1423 * vnode is being cleaned, or
1424 * we've requested that we don't reenter
1425 * the filesystem on this release... in
1426 * this case, we'll mark the vnode aged
1427 * if it's been marked for termination
1430 if ( !(vp
->v_lflag
& (VL_TERMINATE
| VL_DEAD
| VL_MARKTERM
)) )
1431 vp
->v_lflag
|= VL_NEEDINACTIVE
;
1440 * at this point both the iocount and usecount
1442 * pick up an iocount so that we can call
1443 * VNOP_INACTIVE with the vnode lock unheld
1449 vp
->v_lflag
&= ~VL_NEEDINACTIVE
;
1452 context
.vc_proc
= current_proc();
1453 context
.vc_ucred
= kauth_cred_get();
1454 VNOP_INACTIVE(vp
, &context
);
1458 * because we dropped the vnode lock to call VNOP_INACTIVE
1459 * the state of the vnode may have changed... we may have
1460 * picked up an iocount, usecount or the MARKTERM may have
1461 * been set... we need to reevaluate the reference counts
1462 * to determine if we can call vnode_reclaim_internal at
1463 * this point... if the reference counts are up, we'll pick
1464 * up the MARKTERM state when they get subsequently dropped
1466 if ( (vp
->v_iocount
== 1) && (vp
->v_usecount
== 0) &&
1467 ((vp
->v_lflag
& (VL_MARKTERM
| VL_TERMINATE
| VL_DEAD
)) == VL_MARKTERM
)) {
1470 ut
= get_bsdthread_info(current_thread());
1472 if (ut
->uu_defer_reclaims
) {
1473 vp
->v_defer_reclaimlist
= ut
->uu_vreclaims
;
1474 ut
->uu_vreclaims
= vp
;
1477 vnode_reclaim_internal(vp
, 1, 0);
1479 vnode_dropiocount(vp
, 1);
1488 * Remove any vnodes in the vnode table belonging to mount point mp.
1490 * If MNT_NOFORCE is specified, there should not be any active ones,
1491 * return error if any are found (nb: this is a user error, not a
1492 * system error). If MNT_FORCE is specified, detach any active vnodes
1496 int busyprt
= 0; /* print out busy vnodes */
1498 struct ctldebug debug1
= { "busyprt", &busyprt
};
1503 vflush(mp
, skipvp
, flags
)
1505 struct vnode
*skipvp
;
1508 struct proc
*p
= current_proc();
1515 vnode_iterate_setup(mp
);
1517 * On regular unmounts(not forced) do a
1518 * quick check for vnodes to be in use. This
1519 * preserves the caching of vnodes. automounter
1520 * tries unmounting every so often to see whether
1521 * it is still busy or not.
1523 if ((flags
& FORCECLOSE
)==0) {
1524 if (vnode_umount_preflight(mp
, skipvp
, flags
)) {
1525 vnode_iterate_clear(mp
);
1531 /* it is returns 0 then there is nothing to do */
1532 retval
= vnode_iterate_prepare(mp
);
1535 vnode_iterate_clear(mp
);
1540 /* iterate over all the vnodes */
1541 while (!TAILQ_EMPTY(&mp
->mnt_workerqueue
)) {
1542 vp
= TAILQ_FIRST(&mp
->mnt_workerqueue
);
1543 TAILQ_REMOVE(&mp
->mnt_workerqueue
, vp
, v_mntvnodes
);
1544 TAILQ_INSERT_TAIL(&mp
->mnt_vnodelist
, vp
, v_mntvnodes
);
1545 if ( (vp
->v_mount
!= mp
) || (vp
== skipvp
)) {
1552 if ((vp
->v_id
!= vid
) || ((vp
->v_lflag
& (VL_DEAD
| VL_TERMINATE
)))) {
1559 * If requested, skip over vnodes marked VSYSTEM.
1560 * Skip over all vnodes marked VNOFLUSH.
1562 if ((flags
& SKIPSYSTEM
) && ((vp
->v_flag
& VSYSTEM
) ||
1563 (vp
->v_flag
& VNOFLUSH
))) {
1569 * If requested, skip over vnodes marked VSWAP.
1571 if ((flags
& SKIPSWAP
) && (vp
->v_flag
& VSWAP
)) {
1577 * If requested, skip over vnodes marked VSWAP.
1579 if ((flags
& SKIPROOT
) && (vp
->v_flag
& VROOT
)) {
1585 * If WRITECLOSE is set, only flush out regular file
1586 * vnodes open for writing.
1588 if ((flags
& WRITECLOSE
) &&
1589 (vp
->v_writecount
== 0 || vp
->v_type
!= VREG
)) {
1595 * If the real usecount is 0, all we need to do is clear
1596 * out the vnode data structures and we are done.
1598 if (((vp
->v_usecount
== 0) ||
1599 ((vp
->v_usecount
- vp
->v_kusecount
) == 0))) {
1600 vp
->v_iocount
++; /* so that drain waits for * other iocounts */
1604 vnode_reclaim_internal(vp
, 1, 0);
1605 vnode_dropiocount(vp
, 1);
1614 * If FORCECLOSE is set, forcibly close the vnode.
1615 * For block or character devices, revert to an
1616 * anonymous device. For all other files, just kill them.
1618 if (flags
& FORCECLOSE
) {
1619 if (vp
->v_type
!= VBLK
&& vp
->v_type
!= VCHR
) {
1620 vp
->v_iocount
++; /* so that drain waits * for other iocounts */
1624 vnode_reclaim_internal(vp
, 1, 0);
1625 vnode_dropiocount(vp
, 1);
1630 vp
->v_lflag
&= ~VL_DEAD
;
1631 vp
->v_op
= spec_vnodeop_p
;
1639 vprint("vflush: busy vnode", vp
);
1646 /* At this point the worker queue is completed */
1647 if (busy
&& ((flags
& FORCECLOSE
)==0) && reclaimed
) {
1650 (void)vnode_iterate_reloadq(mp
);
1651 /* returned with mount lock held */
1655 /* if new vnodes were created in between retry the reclaim */
1656 if ( vnode_iterate_reloadq(mp
) != 0) {
1657 if (!(busy
&& ((flags
& FORCECLOSE
)==0)))
1660 vnode_iterate_clear(mp
);
1663 if (busy
&& ((flags
& FORCECLOSE
)==0))
1668 int num_recycledvnodes
=0;
1670 * Disassociate the underlying file system from a vnode.
1671 * The vnode lock is held on entry.
1674 vclean(vnode_t vp
, int flags
, proc_t p
)
1676 struct vfs_context context
;
1679 int already_terminating
;
1680 kauth_cred_t ucred
= NULL
;
1682 context
.vc_proc
= p
;
1683 context
.vc_ucred
= kauth_cred_get();
1686 * Check to see if the vnode is in use.
1687 * If so we have to reference it before we clean it out
1688 * so that its count cannot fall to zero and generate a
1689 * race against ourselves to recycle it.
1691 active
= vp
->v_usecount
;
1694 * just in case we missed sending a needed
1695 * VNOP_INACTIVE, we'll do it now
1697 need_inactive
= (vp
->v_lflag
& VL_NEEDINACTIVE
);
1699 vp
->v_lflag
&= ~VL_NEEDINACTIVE
;
1702 * Prevent the vnode from being recycled or
1703 * brought into use while we clean it out.
1705 already_terminating
= (vp
->v_lflag
& VL_TERMINATE
);
1707 vp
->v_lflag
|= VL_TERMINATE
;
1710 * remove the vnode from any mount list
1713 insmntque(vp
, (struct mount
*)0);
1721 kauth_cred_rele(ucred
);
1723 OSAddAtomic(1, &num_recycledvnodes
);
1725 * purge from the name cache as early as possible...
1729 if (active
&& (flags
& DOCLOSE
))
1730 VNOP_CLOSE(vp
, IO_NDELAY
, &context
);
1733 * Clean out any buffers associated with the vnode.
1735 if (flags
& DOCLOSE
) {
1737 if (vp
->v_tag
== VT_NFS
)
1738 nfs_vinvalbuf(vp
, V_SAVE
, NOCRED
, p
, 0);
1742 VNOP_FSYNC(vp
, MNT_WAIT
, &context
);
1743 buf_invalidateblks(vp
, BUF_WRITE_DATA
, 0, 0);
1745 if (UBCINFOEXISTS(vp
))
1747 * Clean the pages in VM.
1749 (void)ubc_sync_range(vp
, (off_t
)0, ubc_getsize(vp
), UBC_PUSHALL
);
1751 if (UBCINFOEXISTS(vp
))
1752 cluster_release(vp
->v_ubcinfo
);
1754 if (active
|| need_inactive
)
1755 VNOP_INACTIVE(vp
, &context
);
1757 /* Destroy ubc named reference */
1758 ubc_destroy_named(vp
);
1761 * Reclaim the vnode.
1763 if (VNOP_RECLAIM(vp
, &context
))
1764 panic("vclean: cannot reclaim");
1766 // make sure the name & parent ptrs get cleaned out!
1767 vnode_update_identity(vp
, NULLVP
, NULL
, 0, 0, VNODE_UPDATE_PARENT
| VNODE_UPDATE_NAME
);
1771 vp
->v_mount
= dead_mountp
;
1772 vp
->v_op
= dead_vnodeop_p
;
1776 vp
->v_lflag
|= VL_DEAD
;
1778 if (already_terminating
== 0) {
1779 vp
->v_lflag
&= ~VL_TERMINATE
;
1781 * Done with purge, notify sleepers of the grim news.
1783 if (vp
->v_lflag
& VL_TERMWANT
) {
1784 vp
->v_lflag
&= ~VL_TERMWANT
;
1785 wakeup(&vp
->v_lflag
);
1791 * Eliminate all activity associated with the requested vnode
1792 * and with all vnodes aliased to the requested vnode.
1795 vn_revoke(vnode_t vp
, int flags
, __unused vfs_context_t a_context
)
1801 if ((flags
& REVOKEALL
) == 0)
1802 panic("vnop_revoke");
1805 if (vp
->v_flag
& VALIASED
) {
1807 * If a vgone (or vclean) is already in progress,
1808 * wait until it is done and return.
1811 if (vp
->v_lflag
& VL_TERMINATE
) {
1817 * Ensure that vp will not be vgone'd while we
1818 * are eliminating its aliases.
1821 while (vp
->v_flag
& VALIASED
) {
1822 for (vq
= *vp
->v_hashchain
; vq
; vq
= vq
->v_specnext
) {
1823 if (vq
->v_rdev
!= vp
->v_rdev
||
1824 vq
->v_type
!= vp
->v_type
|| vp
== vq
)
1828 if (vnode_getwithvid(vq
,vid
)){
1832 vnode_reclaim_internal(vq
, 0, 0);
1840 vnode_reclaim_internal(vp
, 0, 0);
1846 * Recycle an unused vnode to the front of the free list.
1847 * Release the passed interlock if the vnode will be recycled.
1855 if (vp
->v_iocount
|| vp
->v_usecount
) {
1856 vp
->v_lflag
|= VL_MARKTERM
;
1860 vnode_reclaim_internal(vp
, 1, 0);
1867 vnode_reload(vnode_t vp
)
1871 if ((vp
->v_iocount
> 1) || vp
->v_usecount
) {
1875 if (vp
->v_iocount
<= 0)
1876 panic("vnode_reload with no iocount %d", vp
->v_iocount
);
1878 /* mark for release when iocount is dopped */
1879 vp
->v_lflag
|= VL_MARKTERM
;
1893 * Clean out the filesystem specific data.
1894 * vclean also takes care of removing the
1895 * vnode from any mount list it might be on
1897 vclean(vp
, DOCLOSE
, current_proc());
1900 * If special device, remove it from special device alias list
1903 if ((vp
->v_type
== VBLK
|| vp
->v_type
== VCHR
) && vp
->v_specinfo
!= 0) {
1905 if (*vp
->v_hashchain
== vp
) {
1906 *vp
->v_hashchain
= vp
->v_specnext
;
1908 for (vq
= *vp
->v_hashchain
; vq
; vq
= vq
->v_specnext
) {
1909 if (vq
->v_specnext
!= vp
)
1911 vq
->v_specnext
= vp
->v_specnext
;
1915 panic("missing bdev");
1917 if (vp
->v_flag
& VALIASED
) {
1919 for (vq
= *vp
->v_hashchain
; vq
; vq
= vq
->v_specnext
) {
1920 if (vq
->v_rdev
!= vp
->v_rdev
||
1921 vq
->v_type
!= vp
->v_type
)
1928 panic("missing alias");
1930 vx
->v_flag
&= ~VALIASED
;
1931 vp
->v_flag
&= ~VALIASED
;
1935 struct specinfo
*tmp
= vp
->v_specinfo
;
1936 vp
->v_specinfo
= NULL
;
1937 FREE_ZONE((void *)tmp
, sizeof(struct specinfo
), M_SPECINFO
);
1943 * Lookup a vnode by device number.
1946 check_mountedon(dev_t dev
, enum vtype type
, int *errorp
)
1954 for (vp
= speclisth
[SPECHASH(dev
)]; vp
; vp
= vp
->v_specnext
) {
1955 if (dev
!= vp
->v_rdev
|| type
!= vp
->v_type
)
1959 if (vnode_getwithvid(vp
,vid
))
1962 if ((vp
->v_usecount
> 0) || (vp
->v_iocount
> 1)) {
1964 if ((*errorp
= vfs_mountedon(vp
)) != 0)
1976 * Calculate the total number of references to a special device.
1986 if ((vp
->v_flag
& VALIASED
) == 0)
1987 return (vp
->v_usecount
- vp
->v_kusecount
);
1990 for (count
= 0, vq
= *vp
->v_hashchain
; vq
; vq
= vnext
) {
1991 vnext
= vq
->v_specnext
;
1992 if (vq
->v_rdev
!= vp
->v_rdev
|| vq
->v_type
!= vp
->v_type
)
1997 if (vnode_getwithvid(vq
, vid
)) {
2001 * Alias, but not in use, so flush it out.
2004 if ((vq
->v_usecount
== 0) && (vq
->v_iocount
== 1) && vq
!= vp
) {
2005 vnode_reclaim_internal(vq
, 1, 0);
2010 count
+= (vq
->v_usecount
- vq
->v_kusecount
);
2021 int prtactive
= 0; /* 1 => print out reclaim of active vnodes */
2024 * Print out a description of a vnode.
2026 static char *typename
[] =
2027 { "VNON", "VREG", "VDIR", "VBLK", "VCHR", "VLNK", "VSOCK", "VFIFO", "VBAD" };
2030 vprint(const char *label
, struct vnode
*vp
)
2035 printf("%s: ", label
);
2036 printf("type %s, usecount %d, writecount %d",
2037 typename
[vp
->v_type
], vp
->v_usecount
, vp
->v_writecount
);
2039 if (vp
->v_flag
& VROOT
)
2040 strcat(sbuf
, "|VROOT");
2041 if (vp
->v_flag
& VTEXT
)
2042 strcat(sbuf
, "|VTEXT");
2043 if (vp
->v_flag
& VSYSTEM
)
2044 strcat(sbuf
, "|VSYSTEM");
2045 if (vp
->v_flag
& VNOFLUSH
)
2046 strcat(sbuf
, "|VNOFLUSH");
2047 if (vp
->v_flag
& VBWAIT
)
2048 strcat(sbuf
, "|VBWAIT");
2049 if (vp
->v_flag
& VALIASED
)
2050 strcat(sbuf
, "|VALIASED");
2051 if (sbuf
[0] != '\0')
2052 printf(" flags (%s)", &sbuf
[1]);
2057 vn_getpath(struct vnode
*vp
, char *pathbuf
, int *len
)
2059 return build_path(vp
, pathbuf
, *len
, len
);
2063 static char *extension_table
=NULL
;
2065 static int max_ext_width
;
2068 extension_cmp(void *a
, void *b
)
2070 return (strlen((char *)a
) - strlen((char *)b
));
2075 // This is the api LaunchServices uses to inform the kernel
2076 // the list of package extensions to ignore.
2078 // Internally we keep the list sorted by the length of the
2079 // the extension (from longest to shortest). We sort the
2080 // list of extensions so that we can speed up our searches
2081 // when comparing file names -- we only compare extensions
2082 // that could possibly fit into the file name, not all of
2083 // them (i.e. a short 8 character name can't have an 8
2084 // character extension).
2086 __private_extern__
int
2087 set_package_extensions_table(void *data
, int nentries
, int maxwidth
)
2089 char *new_exts
, *ptr
;
2092 if (nentries
<= 0 || nentries
> 1024 || maxwidth
<= 0 || maxwidth
> 255) {
2096 MALLOC(new_exts
, char *, nentries
* maxwidth
, M_TEMP
, M_WAITOK
);
2098 error
= copyin(CAST_USER_ADDR_T(data
), new_exts
, nentries
* maxwidth
);
2100 FREE(new_exts
, M_TEMP
);
2104 if (extension_table
) {
2105 FREE(extension_table
, M_TEMP
);
2107 extension_table
= new_exts
;
2109 max_ext_width
= maxwidth
;
2111 qsort(extension_table
, nexts
, maxwidth
, extension_cmp
);
2117 __private_extern__
int
2118 is_package_name(char *name
, int len
)
2121 char *ptr
, *name_ext
;
2128 for(ptr
=name
; *ptr
!= '\0'; ptr
++) {
2134 // if there is no "." extension, it can't match
2135 if (name_ext
== NULL
) {
2139 // advance over the "."
2142 // now iterate over all the extensions to see if any match
2143 ptr
= &extension_table
[0];
2144 for(i
=0; i
< nexts
; i
++, ptr
+=max_ext_width
) {
2145 extlen
= strlen(ptr
);
2146 if (strncasecmp(name_ext
, ptr
, extlen
) == 0 && name_ext
[extlen
] == '\0') {
2152 // if we get here, no extension matched
2157 vn_path_package_check(__unused vnode_t vp
, char *path
, int pathlen
, int *component
)
2168 while(end
< path
+ pathlen
&& *end
!= '\0') {
2169 while(end
< path
+ pathlen
&& *end
== '/' && *end
!= '\0') {
2175 while(end
< path
+ pathlen
&& *end
!= '/' && *end
!= '\0') {
2179 if (end
> path
+ pathlen
) {
2180 // hmm, string wasn't null terminated
2185 if (is_package_name(ptr
, end
- ptr
)) {
2199 * Top level filesystem related information gathering.
2201 extern unsigned int vfs_nummntops
;
2204 vfs_sysctl(int *name
, u_int namelen
, user_addr_t oldp
, size_t *oldlenp
,
2205 user_addr_t newp
, size_t newlen
, struct proc
*p
)
2207 struct vfstable
*vfsp
;
2211 struct vfsconf
*vfsc
;
2214 * The VFS_NUMMNTOPS shouldn't be at name[0] since
2215 * is a VFS generic variable. So now we must check
2216 * namelen so we don't end up covering any UFS
2217 * variables (sinc UFS vfc_typenum is 1).
2219 * It should have been:
2220 * name[0]: VFS_GENERIC
2221 * name[1]: VFS_NUMMNTOPS
2223 if (namelen
== 1 && name
[0] == VFS_NUMMNTOPS
) {
2224 return (sysctl_rdint(oldp
, oldlenp
, newp
, vfs_nummntops
));
2227 /* all sysctl names at this level are at least name and field */
2229 return (EISDIR
); /* overloaded */
2230 if (name
[0] != VFS_GENERIC
) {
2231 struct vfs_context context
;
2233 for (vfsp
= vfsconf
; vfsp
; vfsp
= vfsp
->vfc_next
)
2234 if (vfsp
->vfc_typenum
== name
[0])
2238 context
.vc_proc
= p
;
2239 context
.vc_ucred
= kauth_cred_get();
2241 return ((*vfsp
->vfc_vfsops
->vfs_sysctl
)(&name
[1], namelen
- 1,
2242 oldp
, oldlenp
, newp
, newlen
, &context
));
2245 case VFS_MAXTYPENUM
:
2246 return (sysctl_rdint(oldp
, oldlenp
, newp
, maxvfsconf
));
2249 return (ENOTDIR
); /* overloaded */
2250 for (vfsp
= vfsconf
; vfsp
; vfsp
= vfsp
->vfc_next
)
2251 if (vfsp
->vfc_typenum
== name
[2])
2255 vfsc
= (struct vfsconf
*)vfsp
;
2256 if (proc_is64bit(p
)) {
2257 struct user_vfsconf usr_vfsc
;
2258 usr_vfsc
.vfc_vfsops
= CAST_USER_ADDR_T(vfsc
->vfc_vfsops
);
2259 bcopy(vfsc
->vfc_name
, usr_vfsc
.vfc_name
, sizeof(usr_vfsc
.vfc_name
));
2260 usr_vfsc
.vfc_typenum
= vfsc
->vfc_typenum
;
2261 usr_vfsc
.vfc_refcount
= vfsc
->vfc_refcount
;
2262 usr_vfsc
.vfc_flags
= vfsc
->vfc_flags
;
2263 usr_vfsc
.vfc_mountroot
= CAST_USER_ADDR_T(vfsc
->vfc_mountroot
);
2264 usr_vfsc
.vfc_next
= CAST_USER_ADDR_T(vfsc
->vfc_next
);
2265 return (sysctl_rdstruct(oldp
, oldlenp
, newp
, &usr_vfsc
,
2269 return (sysctl_rdstruct(oldp
, oldlenp
, newp
, vfsc
,
2270 sizeof(struct vfsconf
)));
2273 case VFS_SET_PACKAGE_EXTS
:
2274 return set_package_extensions_table((void *)name
[1], name
[2], name
[3]);
2277 * We need to get back into the general MIB, so we need to re-prepend
2278 * CTL_VFS to our name and try userland_sysctl().
2280 usernamelen
= namelen
+ 1;
2281 MALLOC(username
, int *, usernamelen
* sizeof(*username
),
2283 bcopy(name
, username
+ 1, namelen
* sizeof(*name
));
2284 username
[0] = CTL_VFS
;
2285 error
= userland_sysctl(p
, username
, usernamelen
, oldp
,
2286 oldlenp
, 1, newp
, newlen
, oldlenp
);
2287 FREE(username
, M_TEMP
);
2291 int kinfo_vdebug
= 1;
2292 #define KINFO_VNODESLOP 10
2294 * Dump vnode list (via sysctl).
2295 * Copyout address of vnode followed by vnode.
2299 sysctl_vnode(__unused user_addr_t where
, __unused
size_t *sizep
)
2302 struct mount
*mp
, *nmp
;
2303 struct vnode
*nvp
, *vp
;
2304 char *bp
= where
, *savebp
;
2308 #define VPTRSZ sizeof (struct vnode *)
2309 #define VNODESZ sizeof (struct vnode)
2310 if (where
== NULL
) {
2311 *sizep
= (numvnodes
+ KINFO_VNODESLOP
) * (VPTRSZ
+ VNODESZ
);
2314 ewhere
= where
+ *sizep
;
2316 for (mp
= mountlist
.cqh_first
; mp
!= (void *)&mountlist
; mp
= nmp
) {
2317 if (vfs_busy(mp
, LK_NOWAIT
)) {
2318 nmp
= mp
->mnt_list
.cqe_next
;
2323 TAILQ_FOREACH(vp
, &mp
->mnt_vnodelist
, v_mntvnodes
) {
2325 * Check that the vp is still associated with
2326 * this filesystem. RACE: could have been
2327 * recycled onto the same filesystem.
2329 if (vp
->v_mount
!= mp
) {
2331 printf("kinfo: vp changed\n");
2335 if (bp
+ VPTRSZ
+ VNODESZ
> ewhere
) {
2337 *sizep
= bp
- where
;
2340 if ((error
= copyout((caddr_t
)&vp
, bp
, VPTRSZ
)) ||
2341 (error
= copyout((caddr_t
)vp
, bp
+ VPTRSZ
, VNODESZ
))) {
2345 bp
+= VPTRSZ
+ VNODESZ
;
2347 nmp
= mp
->mnt_list
.cqe_next
;
2351 *sizep
= bp
- where
;
2359 * Check to see if a filesystem is mounted on a block device.
2369 if (vp
->v_specflags
& SI_MOUNTEDON
) {
2373 if (vp
->v_flag
& VALIASED
) {
2374 for (vq
= *vp
->v_hashchain
; vq
; vq
= vq
->v_specnext
) {
2375 if (vq
->v_rdev
!= vp
->v_rdev
||
2376 vq
->v_type
!= vp
->v_type
)
2378 if (vq
->v_specflags
& SI_MOUNTEDON
) {
2390 * Unmount all filesystems. The list is traversed in reverse order
2391 * of mounting to avoid dependencies.
2393 __private_extern__
void
2397 struct proc
*p
= current_proc();
2399 int skip_listremove
;
2402 * Since this only runs when rebooting, it is not interlocked.
2405 while(!TAILQ_EMPTY(&mountlist
)) {
2406 mp
= TAILQ_LAST(&mountlist
, mntlist
);
2407 mount_list_unlock();
2408 skip_listremove
= 0;
2409 error
= dounmount(mp
, MNT_FORCE
, &skip_listremove
, p
);
2412 if (skip_listremove
== 0) {
2413 TAILQ_REMOVE(&mountlist
, mp
, mnt_list
);
2414 printf("unmount of %s failed (", mp
->mnt_vfsstat
.f_mntonname
);
2420 printf("%d)\n", error
);
2425 mount_list_unlock();
2430 * This routine is called from vnode_pager_no_senders()
2431 * which in turn can be called with vnode locked by vnode_uncache()
2432 * But it could also get called as a result of vm_object_cache_trim().
2433 * In that case lock state is unknown.
2434 * AGE the vnode so that it gets recycled quickly.
2436 __private_extern__
void
2437 vnode_pager_vrele(struct vnode
*vp
)
2441 if (!ISSET(vp
->v_lflag
, VL_TERMINATE
))
2442 panic("vnode_pager_vrele: vp not in termination");
2443 vp
->v_lflag
&= ~VNAMED_UBC
;
2445 if (UBCINFOEXISTS(vp
)) {
2446 struct ubc_info
*uip
= vp
->v_ubcinfo
;
2448 if (ISSET(uip
->ui_flags
, UI_WASMAPPED
))
2449 SET(vp
->v_flag
, VWASMAPPED
);
2450 vp
->v_ubcinfo
= UBC_INFO_NULL
;
2452 ubc_info_deallocate(uip
);
2454 panic("NO ubcinfo in vnode_pager_vrele");
2458 wakeup(&vp
->v_lflag
);
2462 #include <sys/disk.h>
2465 vfs_init_io_attributes(vnode_t devvp
, mount_t mp
)
2469 off_t writeblockcnt
;
2478 struct vfs_context context
;
2480 proc_t p
= current_proc();
2482 context
.vc_proc
= p
;
2483 context
.vc_ucred
= kauth_cred_get();
2487 * determine if this mount point exists on the same device as the root
2488 * partition... if so, then it comes under the hard throttle control
2491 static int rootunit
= -1;
2493 if (rootunit
== -1) {
2494 if (VNOP_IOCTL(rootvp
, DKIOCGETBSDUNIT
, (caddr_t
)&rootunit
, 0, &context
))
2496 else if (rootvp
== devvp
)
2497 mp
->mnt_kern_flag
|= MNTK_ROOTDEV
;
2499 if (devvp
!= rootvp
&& rootunit
!= -1) {
2500 if (VNOP_IOCTL(devvp
, DKIOCGETBSDUNIT
, (caddr_t
)&thisunit
, 0, &context
) == 0) {
2501 if (thisunit
== rootunit
)
2502 mp
->mnt_kern_flag
|= MNTK_ROOTDEV
;
2506 * force the spec device to re-cache
2507 * the underlying block size in case
2508 * the filesystem overrode the initial value
2510 set_fsblocksize(devvp
);
2513 if ((error
= VNOP_IOCTL(devvp
, DKIOCGETBLOCKSIZE
,
2514 (caddr_t
)&blksize
, 0, &context
)))
2517 mp
->mnt_devblocksize
= blksize
;
2519 if (VNOP_IOCTL(devvp
, DKIOCISVIRTUAL
, (caddr_t
)&isvirtual
, 0, &context
) == 0) {
2521 mp
->mnt_kern_flag
|= MNTK_VIRTUALDEV
;
2524 if ((error
= VNOP_IOCTL(devvp
, DKIOCGETMAXBLOCKCOUNTREAD
,
2525 (caddr_t
)&readblockcnt
, 0, &context
)))
2528 if ((error
= VNOP_IOCTL(devvp
, DKIOCGETMAXBLOCKCOUNTWRITE
,
2529 (caddr_t
)&writeblockcnt
, 0, &context
)))
2532 if ((error
= VNOP_IOCTL(devvp
, DKIOCGETMAXBYTECOUNTREAD
,
2533 (caddr_t
)&readmaxcnt
, 0, &context
)))
2536 if ((error
= VNOP_IOCTL(devvp
, DKIOCGETMAXBYTECOUNTWRITE
,
2537 (caddr_t
)&writemaxcnt
, 0, &context
)))
2540 if ((error
= VNOP_IOCTL(devvp
, DKIOCGETMAXSEGMENTCOUNTREAD
,
2541 (caddr_t
)&readsegcnt
, 0, &context
)))
2544 if ((error
= VNOP_IOCTL(devvp
, DKIOCGETMAXSEGMENTCOUNTWRITE
,
2545 (caddr_t
)&writesegcnt
, 0, &context
)))
2548 if ((error
= VNOP_IOCTL(devvp
, DKIOCGETMAXSEGMENTBYTECOUNTREAD
,
2549 (caddr_t
)&readsegsize
, 0, &context
)))
2552 if ((error
= VNOP_IOCTL(devvp
, DKIOCGETMAXSEGMENTBYTECOUNTWRITE
,
2553 (caddr_t
)&writesegsize
, 0, &context
)))
2557 temp
= (readmaxcnt
> UINT32_MAX
) ? UINT32_MAX
: readmaxcnt
;
2560 temp
= readblockcnt
* blksize
;
2561 temp
= (temp
> UINT32_MAX
) ? UINT32_MAX
: temp
;
2565 mp
->mnt_maxreadcnt
= (u_int32_t
)temp
;
2568 temp
= (writemaxcnt
> UINT32_MAX
) ? UINT32_MAX
: writemaxcnt
;
2570 if (writeblockcnt
) {
2571 temp
= writeblockcnt
* blksize
;
2572 temp
= (temp
> UINT32_MAX
) ? UINT32_MAX
: temp
;
2576 mp
->mnt_maxwritecnt
= (u_int32_t
)temp
;
2579 temp
= (readsegcnt
> UINT16_MAX
) ? UINT16_MAX
: readsegcnt
;
2580 mp
->mnt_segreadcnt
= (u_int16_t
)temp
;
2583 temp
= (writesegcnt
> UINT16_MAX
) ? UINT16_MAX
: writesegcnt
;
2584 mp
->mnt_segwritecnt
= (u_int16_t
)temp
;
2587 temp
= (readsegsize
> UINT32_MAX
) ? UINT32_MAX
: readsegsize
;
2589 temp
= mp
->mnt_maxreadcnt
;
2590 mp
->mnt_maxsegreadsize
= (u_int32_t
)temp
;
2593 temp
= (writesegsize
> UINT32_MAX
) ? UINT32_MAX
: writesegsize
;
2595 temp
= mp
->mnt_maxwritecnt
;
2596 mp
->mnt_maxsegwritesize
= (u_int32_t
)temp
;
2601 static struct klist fs_klist
;
2604 vfs_event_init(void)
2607 klist_init(&fs_klist
);
2611 vfs_event_signal(__unused fsid_t
*fsid
, u_int32_t event
, __unused
intptr_t data
)
2614 KNOTE(&fs_klist
, event
);
2618 * return the number of mounted filesystems.
2621 sysctl_vfs_getvfscnt(void)
2623 return(mount_getvfscnt());
2628 mount_getvfscnt(void)
2634 mount_list_unlock();
2642 mount_fillfsids(fsid_t
*fsidlst
, int count
)
2649 TAILQ_FOREACH(mp
, &mountlist
, mnt_list
) {
2650 if (actual
<= count
) {
2651 fsidlst
[actual
] = mp
->mnt_vfsstat
.f_fsid
;
2655 mount_list_unlock();
2661 * fill in the array of fsid_t's up to a max of 'count', the actual
2662 * number filled in will be set in '*actual'. If there are more fsid_t's
2663 * than room in fsidlst then ENOMEM will be returned and '*actual' will
2664 * have the actual count.
2665 * having *actual filled out even in the error case is depended upon.
2668 sysctl_vfs_getvfslist(fsid_t
*fsidlst
, int count
, int *actual
)
2674 TAILQ_FOREACH(mp
, &mountlist
, mnt_list
) {
2676 if (*actual
<= count
)
2677 fsidlst
[(*actual
) - 1] = mp
->mnt_vfsstat
.f_fsid
;
2679 mount_list_unlock();
2680 return (*actual
<= count
? 0 : ENOMEM
);
2684 sysctl_vfs_vfslist SYSCTL_HANDLER_ARGS
2690 /* This is a readonly node. */
2691 if (req
->newptr
!= USER_ADDR_NULL
)
2694 /* they are querying us so just return the space required. */
2695 if (req
->oldptr
== USER_ADDR_NULL
) {
2696 req
->oldidx
= sysctl_vfs_getvfscnt() * sizeof(fsid_t
);
2701 * Retrieve an accurate count of the amount of space required to copy
2702 * out all the fsids in the system.
2704 space
= req
->oldlen
;
2705 req
->oldlen
= sysctl_vfs_getvfscnt() * sizeof(fsid_t
);
2707 /* they didn't give us enough space. */
2708 if (space
< req
->oldlen
)
2711 MALLOC(fsidlst
, fsid_t
*, req
->oldlen
, M_TEMP
, M_WAITOK
);
2712 error
= sysctl_vfs_getvfslist(fsidlst
, req
->oldlen
/ sizeof(fsid_t
),
2715 * If we get back ENOMEM, then another mount has been added while we
2716 * slept in malloc above. If this is the case then try again.
2718 if (error
== ENOMEM
) {
2719 FREE(fsidlst
, M_TEMP
);
2720 req
->oldlen
= space
;
2724 error
= SYSCTL_OUT(req
, fsidlst
, actual
* sizeof(fsid_t
));
2726 FREE(fsidlst
, M_TEMP
);
2731 * Do a sysctl by fsid.
2734 sysctl_vfs_ctlbyfsid SYSCTL_HANDLER_ARGS
2737 struct user_vfsidctl user_vc
;
2739 struct vfsstatfs
*sp
;
2742 int error
, flags
, namelen
;
2743 struct vfs_context context
;
2744 boolean_t is_64_bit
;
2749 context
.vc_proc
= p
;
2750 context
.vc_ucred
= kauth_cred_get();
2751 is_64_bit
= proc_is64bit(p
);
2754 error
= SYSCTL_IN(req
, &user_vc
, sizeof(user_vc
));
2757 if (user_vc
.vc_vers
!= VFS_CTL_VERS1
)
2759 mp
= mount_list_lookupby_fsid(&user_vc
.vc_fsid
, 0, 0);
2762 error
= SYSCTL_IN(req
, &vc
, sizeof(vc
));
2765 if (vc
.vc_vers
!= VFS_CTL_VERS1
)
2767 mp
= mount_list_lookupby_fsid(&vc
.vc_fsid
, 0, 0);
2771 /* reset so that the fs specific code can fetch it. */
2774 * Note if this is a VFS_CTL then we pass the actual sysctl req
2775 * in for "oldp" so that the lower layer can DTRT and use the
2776 * SYSCTL_IN/OUT routines.
2778 if (mp
->mnt_op
->vfs_sysctl
!= NULL
) {
2780 if (vfs_64bitready(mp
)) {
2781 error
= mp
->mnt_op
->vfs_sysctl(name
, namelen
,
2782 CAST_USER_ADDR_T(req
),
2783 NULL
, USER_ADDR_NULL
, 0,
2791 error
= mp
->mnt_op
->vfs_sysctl(name
, namelen
,
2792 CAST_USER_ADDR_T(req
),
2793 NULL
, USER_ADDR_NULL
, 0,
2796 if (error
!= ENOTSUP
)
2800 case VFS_CTL_UMOUNT
:
2803 req
->newptr
= user_vc
.vc_ptr
;
2804 req
->newlen
= (size_t)user_vc
.vc_len
;
2807 req
->newptr
= CAST_USER_ADDR_T(vc
.vc_ptr
);
2808 req
->newlen
= vc
.vc_len
;
2810 error
= SYSCTL_IN(req
, &flags
, sizeof(flags
));
2813 error
= safedounmount(mp
, flags
, p
);
2815 case VFS_CTL_STATFS
:
2818 req
->newptr
= user_vc
.vc_ptr
;
2819 req
->newlen
= (size_t)user_vc
.vc_len
;
2822 req
->newptr
= CAST_USER_ADDR_T(vc
.vc_ptr
);
2823 req
->newlen
= vc
.vc_len
;
2825 error
= SYSCTL_IN(req
, &flags
, sizeof(flags
));
2828 sp
= &mp
->mnt_vfsstat
;
2829 if (((flags
& MNT_NOWAIT
) == 0 || (flags
& MNT_WAIT
)) &&
2830 (error
= vfs_update_vfsstat(mp
, &context
)))
2833 struct user_statfs sfs
;
2834 bzero(&sfs
, sizeof(sfs
));
2835 sfs
.f_flags
= mp
->mnt_flag
& MNT_VISFLAGMASK
;
2836 sfs
.f_type
= mp
->mnt_vtable
->vfc_typenum
;
2837 sfs
.f_bsize
= (user_long_t
)sp
->f_bsize
;
2838 sfs
.f_iosize
= (user_long_t
)sp
->f_iosize
;
2839 sfs
.f_blocks
= (user_long_t
)sp
->f_blocks
;
2840 sfs
.f_bfree
= (user_long_t
)sp
->f_bfree
;
2841 sfs
.f_bavail
= (user_long_t
)sp
->f_bavail
;
2842 sfs
.f_files
= (user_long_t
)sp
->f_files
;
2843 sfs
.f_ffree
= (user_long_t
)sp
->f_ffree
;
2844 sfs
.f_fsid
= sp
->f_fsid
;
2845 sfs
.f_owner
= sp
->f_owner
;
2847 strncpy(&sfs
.f_fstypename
, &sp
->f_fstypename
, MFSNAMELEN
-1);
2848 strncpy(&sfs
.f_mntonname
, &sp
->f_mntonname
, MNAMELEN
-1);
2849 strncpy(&sfs
.f_mntfromname
, &sp
->f_mntfromname
, MNAMELEN
-1);
2851 error
= SYSCTL_OUT(req
, &sfs
, sizeof(sfs
));
2855 bzero(&sfs
, sizeof(struct statfs
));
2856 sfs
.f_flags
= mp
->mnt_flag
& MNT_VISFLAGMASK
;
2857 sfs
.f_type
= mp
->mnt_vtable
->vfc_typenum
;
2860 * It's possible for there to be more than 2^^31 blocks in the filesystem, so we
2861 * have to fudge the numbers here in that case. We inflate the blocksize in order
2862 * to reflect the filesystem size as best we can.
2864 if (sp
->f_blocks
> LONG_MAX
) {
2868 * Work out how far we have to shift the block count down to make it fit.
2869 * Note that it's possible to have to shift so far that the resulting
2870 * blocksize would be unreportably large. At that point, we will clip
2871 * any values that don't fit.
2873 * For safety's sake, we also ensure that f_iosize is never reported as
2874 * being smaller than f_bsize.
2876 for (shift
= 0; shift
< 32; shift
++) {
2877 if ((sp
->f_blocks
>> shift
) <= LONG_MAX
)
2879 if ((sp
->f_bsize
<< (shift
+ 1)) > LONG_MAX
)
2882 #define __SHIFT_OR_CLIP(x, s) ((((x) >> (s)) > LONG_MAX) ? LONG_MAX : ((x) >> (s)))
2883 sfs
.f_blocks
= (long)__SHIFT_OR_CLIP(sp
->f_blocks
, shift
);
2884 sfs
.f_bfree
= (long)__SHIFT_OR_CLIP(sp
->f_bfree
, shift
);
2885 sfs
.f_bavail
= (long)__SHIFT_OR_CLIP(sp
->f_bavail
, shift
);
2886 #undef __SHIFT_OR_CLIP
2887 sfs
.f_bsize
= (long)(sp
->f_bsize
<< shift
);
2888 sfs
.f_iosize
= lmax(sp
->f_iosize
, sp
->f_bsize
);
2890 sfs
.f_bsize
= (long)sp
->f_bsize
;
2891 sfs
.f_iosize
= (long)sp
->f_iosize
;
2892 sfs
.f_blocks
= (long)sp
->f_blocks
;
2893 sfs
.f_bfree
= (long)sp
->f_bfree
;
2894 sfs
.f_bavail
= (long)sp
->f_bavail
;
2896 sfs
.f_files
= (long)sp
->f_files
;
2897 sfs
.f_ffree
= (long)sp
->f_ffree
;
2898 sfs
.f_fsid
= sp
->f_fsid
;
2899 sfs
.f_owner
= sp
->f_owner
;
2901 strncpy(&sfs
.f_fstypename
, &sp
->f_fstypename
, MFSNAMELEN
-1);
2902 strncpy(&sfs
.f_mntonname
, &sp
->f_mntonname
, MNAMELEN
-1);
2903 strncpy(&sfs
.f_mntfromname
, &sp
->f_mntfromname
, MNAMELEN
-1);
2905 error
= SYSCTL_OUT(req
, &sfs
, sizeof(sfs
));
2914 static int filt_fsattach(struct knote
*kn
);
2915 static void filt_fsdetach(struct knote
*kn
);
2916 static int filt_fsevent(struct knote
*kn
, long hint
);
2918 struct filterops fs_filtops
=
2919 { 0, filt_fsattach
, filt_fsdetach
, filt_fsevent
};
2922 filt_fsattach(struct knote
*kn
)
2925 kn
->kn_flags
|= EV_CLEAR
;
2926 KNOTE_ATTACH(&fs_klist
, kn
);
2931 filt_fsdetach(struct knote
*kn
)
2934 KNOTE_DETACH(&fs_klist
, kn
);
2938 filt_fsevent(struct knote
*kn
, long hint
)
2941 kn
->kn_fflags
|= hint
;
2942 return (kn
->kn_fflags
!= 0);
2946 sysctl_vfs_noremotehang SYSCTL_HANDLER_ARGS
2953 /* We need a pid. */
2954 if (req
->newptr
== USER_ADDR_NULL
)
2957 error
= SYSCTL_IN(req
, &pid
, sizeof(pid
));
2961 p
= pfind(pid
< 0 ? -pid
: pid
);
2966 * Fetching the value is ok, but we only fetch if the old
2969 if (req
->oldptr
!= USER_ADDR_NULL
) {
2970 out
= !((p
->p_flag
& P_NOREMOTEHANG
) == 0);
2971 error
= SYSCTL_OUT(req
, &out
, sizeof(out
));
2975 /* XXX req->p->p_ucred -> kauth_cred_get() ??? */
2976 /* cansignal offers us enough security. */
2977 if (p
!= req
->p
&& suser(req
->p
->p_ucred
, &req
->p
->p_acflag
) != 0)
2981 p
->p_flag
&= ~P_NOREMOTEHANG
;
2983 p
->p_flag
|= P_NOREMOTEHANG
;
2987 /* the vfs.generic. branch. */
2988 SYSCTL_NODE(_vfs
, VFS_GENERIC
, generic
, CTLFLAG_RW
, 0, "vfs generic hinge");
2989 /* retreive a list of mounted filesystem fsid_t */
2990 SYSCTL_PROC(_vfs_generic
, OID_AUTO
, vfsidlist
, CTLFLAG_RD
,
2991 0, 0, sysctl_vfs_vfslist
, "S,fsid", "List of mounted filesystem ids");
2992 /* perform operations on filesystem via fsid_t */
2993 SYSCTL_NODE(_vfs_generic
, OID_AUTO
, ctlbyfsid
, CTLFLAG_RW
,
2994 sysctl_vfs_ctlbyfsid
, "ctlbyfsid");
2995 SYSCTL_PROC(_vfs_generic
, OID_AUTO
, noremotehang
, CTLFLAG_RW
,
2996 0, 0, sysctl_vfs_noremotehang
, "I", "noremotehang");
2999 int num_reusedvnodes
=0;
3002 new_vnode(vnode_t
*vpp
)
3005 int retries
= 0; /* retry incase of tablefull */
3012 if ( !TAILQ_EMPTY(&vnode_free_list
)) {
3014 * Pick the first vp for possible reuse
3016 vp
= TAILQ_FIRST(&vnode_free_list
);
3018 if (vp
->v_lflag
& VL_DEAD
)
3024 * we're either empty, or the next guy on the
3025 * list is a valid vnode... if we're under the
3026 * limit, we'll create a new vnode
3028 if (numvnodes
< desiredvnodes
) {
3030 vnode_list_unlock();
3031 MALLOC_ZONE(vp
, struct vnode
*, sizeof *vp
, M_VNODE
, M_WAITOK
);
3032 bzero((char *)vp
, sizeof *vp
);
3033 VLISTNONE(vp
); /* avoid double queue removal */
3034 lck_mtx_init(&vp
->v_lock
, vnode_lck_grp
, vnode_lck_attr
);
3037 vp
->v_id
= ts
.tv_nsec
;
3038 vp
->v_flag
= VSTANDARD
;
3044 * we've reached the system imposed maximum number of vnodes
3045 * but there isn't a single one available
3046 * wait a bit and then retry... if we can't get a vnode
3047 * after 100 retries, than log a complaint
3049 if (++retries
<= 100) {
3050 vnode_list_unlock();
3055 vnode_list_unlock();
3057 log(LOG_EMERG
, "%d desired, %d numvnodes, "
3058 "%d free, %d inactive\n",
3059 desiredvnodes
, numvnodes
, freevnodes
, inactivevnodes
);
3066 VREMFREE("new_vnode", vp
);
3069 vnode_list_unlock();
3073 * We could wait for the vnode_lock after removing the vp from the freelist
3074 * and the vid is bumped only at the very end of reclaim. So it is possible
3075 * that we are looking at a vnode that is being terminated. If so skip it.
3077 if ((vpid
!= vp
->v_id
) || (vp
->v_usecount
!= 0) || (vp
->v_iocount
!= 0) ||
3078 VONLIST(vp
) || (vp
->v_lflag
& VL_TERMINATE
)) {
3080 * we lost the race between dropping the list lock
3081 * and picking up the vnode_lock... someone else
3082 * used this vnode and it is now in a new state
3083 * so we need to go back and try again
3088 if ( (vp
->v_lflag
& (VL_NEEDINACTIVE
| VL_MARKTERM
)) == VL_NEEDINACTIVE
) {
3090 * we did a vnode_rele_ext that asked for
3091 * us not to reenter the filesystem during
3092 * the release even though VL_NEEDINACTIVE was
3093 * set... we'll do it here by doing a
3094 * vnode_get/vnode_put
3096 * pick up an iocount so that we can call
3097 * vnode_put and drive the VNOP_INACTIVE...
3098 * vnode_put will either leave us off
3099 * the freelist if a new ref comes in,
3100 * or put us back on the end of the freelist
3101 * or recycle us if we were marked for termination...
3102 * so we'll just go grab a new candidate
3108 vnode_put_locked(vp
);
3112 OSAddAtomic(1, &num_reusedvnodes
);
3114 /* Checks for anyone racing us for recycle */
3115 if (vp
->v_type
!= VBAD
) {
3116 if (vp
->v_lflag
& VL_DEAD
)
3117 panic("new_vnode: the vnode is VL_DEAD but not VBAD");
3119 (void)vnode_reclaim_internal(vp
, 1, 1);
3122 panic("new_vnode: vp on list ");
3123 if (vp
->v_usecount
|| vp
->v_iocount
|| vp
->v_kusecount
||
3124 (vp
->v_lflag
& (VNAMED_UBC
| VNAMED_MOUNT
| VNAMED_FSHASH
)))
3125 panic("new_vnode: free vnode still referenced\n");
3126 if ((vp
->v_mntvnodes
.tqe_prev
!= 0) && (vp
->v_mntvnodes
.tqe_next
!= 0))
3127 panic("new_vnode: vnode seems to be on mount list ");
3128 if ( !LIST_EMPTY(&vp
->v_nclinks
) || !LIST_EMPTY(&vp
->v_ncchildren
))
3129 panic("new_vnode: vnode still hooked into the name cache");
3131 if (vp
->v_unsafefs
) {
3132 lck_mtx_destroy(&vp
->v_unsafefs
->fsnodelock
, vnode_lck_grp
);
3133 FREE_ZONE((void *)vp
->v_unsafefs
, sizeof(struct unsafe_fsnode
), M_UNSAFEFS
);
3134 vp
->v_unsafefs
= (struct unsafe_fsnode
*)NULL
;
3137 vp
->v_writecount
= 0;
3138 vp
->v_references
= 0;
3139 vp
->v_iterblkflags
= 0;
3140 vp
->v_flag
= VSTANDARD
;
3141 /* vbad vnodes can point to dead_mountp */
3143 vp
->v_defer_reclaimlist
= (vnode_t
)0;
3153 vnode_lock(vnode_t vp
)
3155 lck_mtx_lock(&vp
->v_lock
);
3159 vnode_unlock(vnode_t vp
)
3161 lck_mtx_unlock(&vp
->v_lock
);
3167 vnode_get(struct vnode
*vp
)
3171 if ( (vp
->v_iocount
== 0) && (vp
->v_lflag
& (VL_TERMINATE
| VL_DEAD
)) ) {
3185 vnode_getwithvid(vnode_t vp
, int vid
)
3187 return(vget_internal(vp
, vid
, ( VNODE_NODEAD
| VNODE_WITHID
)));
3191 vnode_getwithref(vnode_t vp
)
3193 return(vget_internal(vp
, 0, 0));
3198 vnode_put(vnode_t vp
)
3203 retval
= vnode_put_locked(vp
);
3210 vnode_put_locked(vnode_t vp
)
3212 struct vfs_context context
;
3215 if (vp
->v_iocount
< 1)
3216 panic("vnode_put(%x): iocount < 1", vp
);
3218 if ((vp
->v_usecount
> 0) || (vp
->v_iocount
> 1)) {
3219 vnode_dropiocount(vp
, 1);
3222 if ((vp
->v_lflag
& (VL_MARKTERM
| VL_TERMINATE
| VL_DEAD
| VL_NEEDINACTIVE
)) == VL_NEEDINACTIVE
) {
3224 vp
->v_lflag
&= ~VL_NEEDINACTIVE
;
3227 context
.vc_proc
= current_proc();
3228 context
.vc_ucred
= kauth_cred_get();
3229 VNOP_INACTIVE(vp
, &context
);
3233 * because we had to drop the vnode lock before calling
3234 * VNOP_INACTIVE, the state of this vnode may have changed...
3235 * we may pick up both VL_MARTERM and either
3236 * an iocount or a usecount while in the VNOP_INACTIVE call
3237 * we don't want to call vnode_reclaim_internal on a vnode
3238 * that has active references on it... so loop back around
3239 * and reevaluate the state
3243 vp
->v_lflag
&= ~VL_NEEDINACTIVE
;
3245 if ((vp
->v_lflag
& (VL_MARKTERM
| VL_TERMINATE
| VL_DEAD
)) == VL_MARKTERM
)
3246 vnode_reclaim_internal(vp
, 1, 0);
3248 vnode_dropiocount(vp
, 1);
3254 /* is vnode_t in use by others? */
3256 vnode_isinuse(vnode_t vp
, int refcnt
)
3258 return(vnode_isinuse_locked(vp
, refcnt
, 0));
3263 vnode_isinuse_locked(vnode_t vp
, int refcnt
, int locked
)
3269 if ((vp
->v_type
!= VREG
) && (vp
->v_usecount
> refcnt
)) {
3273 if (vp
->v_type
== VREG
) {
3274 retval
= ubc_isinuse_locked(vp
, refcnt
, 1);
3284 /* resume vnode_t */
3286 vnode_resume(vnode_t vp
)
3291 if (vp
->v_owner
== current_thread()) {
3292 vp
->v_lflag
&= ~VL_SUSPENDED
;
3295 wakeup(&vp
->v_iocount
);
3303 vnode_drain(vnode_t vp
)
3306 if (vp
->v_lflag
& VL_DRAIN
) {
3307 panic("vnode_drain: recursuve drain");
3310 vp
->v_lflag
|= VL_DRAIN
;
3311 vp
->v_owner
= current_thread();
3313 while (vp
->v_iocount
> 1)
3314 msleep(&vp
->v_iocount
, &vp
->v_lock
, PVFS
, "vnode_drain", 0);
3320 * if the number of recent references via vnode_getwithvid or vnode_getwithref
3321 * exceeds this threshhold, than 'UN-AGE' the vnode by removing it from
3322 * the LRU list if it's currently on it... once the iocount and usecount both drop
3323 * to 0, it will get put back on the end of the list, effectively making it younger
3324 * this allows us to keep actively referenced vnodes in the list without having
3325 * to constantly remove and add to the list each time a vnode w/o a usecount is
3326 * referenced which costs us taking and dropping a global lock twice.
3328 #define UNAGE_THRESHHOLD 10
3331 vnode_getiocount(vnode_t vp
, int locked
, int vid
, int vflags
)
3333 int nodead
= vflags
& VNODE_NODEAD
;
3334 int nosusp
= vflags
& VNODE_NOSUSPEND
;
3341 * if it is a dead vnode with deadfs
3343 if (nodead
&& (vp
->v_lflag
& VL_DEAD
) && ((vp
->v_type
== VBAD
) || (vp
->v_data
== 0))) {
3349 * will return VL_DEAD ones
3351 if ((vp
->v_lflag
& (VL_SUSPENDED
| VL_DRAIN
| VL_TERMINATE
)) == 0 ) {
3355 * if suspended vnodes are to be failed
3357 if (nosusp
&& (vp
->v_lflag
& VL_SUSPENDED
)) {
3363 * if you are the owner of drain/suspend/termination , can acquire iocount
3364 * check for VL_TERMINATE; it does not set owner
3366 if ((vp
->v_lflag
& (VL_DRAIN
| VL_SUSPENDED
| VL_TERMINATE
)) &&
3367 (vp
->v_owner
== current_thread())) {
3370 if (vp
->v_lflag
& VL_TERMINATE
) {
3371 vp
->v_lflag
|= VL_TERMWANT
;
3373 msleep(&vp
->v_lflag
, &vp
->v_lock
, PVFS
, "vnode getiocount", 0);
3375 msleep(&vp
->v_iocount
, &vp
->v_lock
, PVFS
, "vnode_getiocount", 0);
3377 if (vid
!= vp
->v_id
) {
3382 if (++vp
->v_references
>= UNAGE_THRESHHOLD
) {
3383 vp
->v_references
= 0;
3384 vnode_list_remove(vp
);
3396 vnode_dropiocount (vnode_t vp
, int locked
)
3400 if (vp
->v_iocount
< 1)
3401 panic("vnode_dropiocount(%x): v_iocount < 1", vp
);
3407 if ((vp
->v_lflag
& (VL_DRAIN
| VL_SUSPENDED
)) && (vp
->v_iocount
<= 1))
3408 wakeup(&vp
->v_iocount
);
3416 vnode_reclaim(struct vnode
* vp
)
3418 vnode_reclaim_internal(vp
, 0, 0);
3423 vnode_reclaim_internal(struct vnode
* vp
, int locked
, int reuse
)
3430 if (vp
->v_lflag
& VL_TERMINATE
) {
3431 panic("vnode reclaim in progress");
3433 vp
->v_lflag
|= VL_TERMINATE
;
3435 if (vnode_drain(vp
)) {
3436 panic("vnode drain failed");
3440 isfifo
= (vp
->v_type
== VFIFO
);
3442 if (vp
->v_type
!= VBAD
)
3443 vgone(vp
); /* clean and reclaim the vnode */
3446 * give the vnode a new identity so
3447 * that vnode_getwithvid will fail
3448 * on any stale cache accesses
3452 struct fifoinfo
* fip
;
3454 fip
= vp
->v_fifoinfo
;
3455 vp
->v_fifoinfo
= NULL
;
3462 panic("vnode_reclaim_internal: cleaned vnode isn't");
3463 if (vp
->v_numoutput
)
3464 panic("vnode_reclaim_internal: Clean vnode has pending I/O's");
3465 if (UBCINFOEXISTS(vp
))
3466 panic("vnode_reclaim_internal: ubcinfo not cleaned");
3468 panic("vnode_reclaim_internal: vparent not removed");
3470 panic("vnode_reclaim_internal: vname not removed");
3474 vp
->v_lflag
&= ~VL_TERMINATE
;
3475 vp
->v_lflag
&= ~VL_DRAIN
;
3478 if (vp
->v_lflag
& VL_TERMWANT
) {
3479 vp
->v_lflag
&= ~VL_TERMWANT
;
3480 wakeup(&vp
->v_lflag
);
3482 if (!reuse
&& vp
->v_usecount
== 0)
3489 * The following api creates a vnode and associates all the parameter specified in vnode_fsparam
3490 * structure and returns a vnode handle with a reference. device aliasing is handled here so checkalias
3491 * is obsoleted by this.
3492 * vnode_create(int flavor, size_t size, void * param, vnode_t *vp)
3495 vnode_create(int flavor
, size_t size
, void *data
, vnode_t
*vpp
)
3502 struct componentname
*cnp
;
3503 struct vnode_fsparam
*param
= (struct vnode_fsparam
*)data
;
3505 if (flavor
== VNCREATE_FLAVOR
&& (size
== VCREATESIZE
) && param
) {
3506 if ( (error
= new_vnode(&vp
)) ) {
3509 dvp
= param
->vnfs_dvp
;
3510 cnp
= param
->vnfs_cnp
;
3512 vp
->v_op
= param
->vnfs_vops
;
3513 vp
->v_type
= param
->vnfs_vtype
;
3514 vp
->v_data
= param
->vnfs_fsnode
;
3517 if (param
->vnfs_markroot
)
3518 vp
->v_flag
|= VROOT
;
3519 if (param
->vnfs_marksystem
)
3520 vp
->v_flag
|= VSYSTEM
;
3521 else if (vp
->v_type
== VREG
) {
3523 * only non SYSTEM vp
3525 error
= ubc_info_init_withsize(vp
, param
->vnfs_filesize
);
3531 vp
->v_op
= dead_vnodeop_p
;
3535 vp
->v_lflag
|= VL_DEAD
;
3544 if (vp
->v_type
== VCHR
|| vp
->v_type
== VBLK
) {
3546 if ( (nvp
= checkalias(vp
, param
->vnfs_rdev
)) ) {
3548 * if checkalias returns a vnode, it will be locked
3550 * first get rid of the unneeded vnode we acquired
3553 vp
->v_op
= spec_vnodeop_p
;
3555 vp
->v_lflag
= VL_DEAD
;
3561 * switch to aliased vnode and finish
3566 vclean(vp
, 0, current_proc());
3567 vp
->v_op
= param
->vnfs_vops
;
3568 vp
->v_type
= param
->vnfs_vtype
;
3569 vp
->v_data
= param
->vnfs_fsnode
;
3572 insmntque(vp
, param
->vnfs_mp
);
3578 if (vp
->v_type
== VFIFO
) {
3579 struct fifoinfo
*fip
;
3581 MALLOC(fip
, struct fifoinfo
*,
3582 sizeof(*fip
), M_TEMP
, M_WAITOK
);
3583 bzero(fip
, sizeof(struct fifoinfo
));
3584 vp
->v_fifoinfo
= fip
;
3586 /* The file systems usually pass the address of the location where
3587 * where there store the vnode pointer. When we add the vnode in mount
3588 * point and name cache they are discoverable. So the file system node
3589 * will have the connection to vnode setup by then
3593 if (param
->vnfs_mp
) {
3594 if (param
->vnfs_mp
->mnt_kern_flag
& MNTK_LOCK_LOCAL
)
3595 vp
->v_flag
|= VLOCKLOCAL
;
3598 * enter in mount vnode list
3600 insmntque(vp
, param
->vnfs_mp
);
3602 #ifdef INTERIM_FSNODE_LOCK
3603 if (param
->vnfs_mp
->mnt_vtable
->vfc_threadsafe
== 0) {
3604 MALLOC_ZONE(vp
->v_unsafefs
, struct unsafe_fsnode
*,
3605 sizeof(struct unsafe_fsnode
), M_UNSAFEFS
, M_WAITOK
);
3606 vp
->v_unsafefs
->fsnode_count
= 0;
3607 vp
->v_unsafefs
->fsnodeowner
= (void *)NULL
;
3608 lck_mtx_init(&vp
->v_unsafefs
->fsnodelock
, vnode_lck_grp
, vnode_lck_attr
);
3610 #endif /* INTERIM_FSNODE_LOCK */
3612 if (dvp
&& vnode_ref(dvp
) == 0) {
3616 if (dvp
&& ((param
->vnfs_flags
& (VNFS_NOCACHE
| VNFS_CANTCACHE
)) == 0)) {
3618 * enter into name cache
3619 * we've got the info to enter it into the name cache now
3621 cache_enter(dvp
, vp
, cnp
);
3623 vp
->v_name
= vfs_addname(cnp
->cn_nameptr
, cnp
->cn_namelen
, cnp
->cn_hash
, 0);
3625 if ((param
->vnfs_flags
& VNFS_CANTCACHE
) == 0) {
3627 * this vnode is being created as cacheable in the name cache
3628 * this allows us to re-enter it in the cache
3630 vp
->v_flag
|= VNCACHEABLE
;
3632 if ((vp
->v_flag
& VSYSTEM
) && (vp
->v_type
!= VREG
))
3633 panic("incorrect vnode setup");
3642 vnode_addfsref(vnode_t vp
)
3645 if (vp
->v_lflag
& VNAMED_FSHASH
)
3646 panic("add_fsref: vp already has named reference");
3647 if ((vp
->v_freelist
.tqe_prev
!= (struct vnode
**)0xdeadb))
3648 panic("addfsref: vp on the free list\n");
3649 vp
->v_lflag
|= VNAMED_FSHASH
;
3655 vnode_removefsref(vnode_t vp
)
3658 if ((vp
->v_lflag
& VNAMED_FSHASH
) == 0)
3659 panic("remove_fsref: no named reference");
3660 vp
->v_lflag
&= ~VNAMED_FSHASH
;
3668 vfs_iterate(__unused
int flags
, int (*callout
)(mount_t
, void *), void *arg
)
3673 int count
, actualcount
, i
;
3676 count
= mount_getvfscnt();
3679 fsid_list
= (fsid_t
*)kalloc(count
* sizeof(fsid_t
));
3680 allocmem
= (void *)fsid_list
;
3682 actualcount
= mount_fillfsids(fsid_list
, count
);
3684 for (i
=0; i
< actualcount
; i
++) {
3686 /* obtain the mount point with iteration reference */
3687 mp
= mount_list_lookupby_fsid(&fsid_list
[i
], 0, 1);
3689 if(mp
== (struct mount
*)0)
3692 if (mp
->mnt_lflag
& (MNT_LDEAD
| MNT_LUNMOUNT
)) {
3700 /* iterate over all the vnodes */
3701 ret
= callout(mp
, arg
);
3707 case VFS_RETURNED_DONE
:
3708 if (ret
== VFS_RETURNED_DONE
) {
3714 case VFS_CLAIMED_DONE
:
3725 kfree(allocmem
, (count
* sizeof(fsid_t
)));
3730 * Update the vfsstatfs structure in the mountpoint.
3733 vfs_update_vfsstat(mount_t mp
, vfs_context_t ctx
)
3739 * Request the attributes we want to propagate into
3740 * the per-mount vfsstat structure.
3743 VFSATTR_WANTED(&va
, f_iosize
);
3744 VFSATTR_WANTED(&va
, f_blocks
);
3745 VFSATTR_WANTED(&va
, f_bfree
);
3746 VFSATTR_WANTED(&va
, f_bavail
);
3747 VFSATTR_WANTED(&va
, f_bused
);
3748 VFSATTR_WANTED(&va
, f_files
);
3749 VFSATTR_WANTED(&va
, f_ffree
);
3750 VFSATTR_WANTED(&va
, f_bsize
);
3751 VFSATTR_WANTED(&va
, f_fssubtype
);
3752 if ((error
= vfs_getattr(mp
, &va
, ctx
)) != 0) {
3753 KAUTH_DEBUG("STAT - filesystem returned error %d", error
);
3758 * Unpack into the per-mount structure.
3760 * We only overwrite these fields, which are likely to change:
3768 * And these which are not, but which the FS has no other way
3769 * of providing to us:
3775 if (VFSATTR_IS_SUPPORTED(&va
, f_bsize
)) {
3776 mp
->mnt_vfsstat
.f_bsize
= va
.f_bsize
;
3778 mp
->mnt_vfsstat
.f_bsize
= mp
->mnt_devblocksize
; /* default from the device block size */
3780 if (VFSATTR_IS_SUPPORTED(&va
, f_iosize
)) {
3781 mp
->mnt_vfsstat
.f_iosize
= va
.f_iosize
;
3783 mp
->mnt_vfsstat
.f_iosize
= 1024 * 1024; /* 1MB sensible I/O size */
3785 if (VFSATTR_IS_SUPPORTED(&va
, f_blocks
))
3786 mp
->mnt_vfsstat
.f_blocks
= va
.f_blocks
;
3787 if (VFSATTR_IS_SUPPORTED(&va
, f_bfree
))
3788 mp
->mnt_vfsstat
.f_bfree
= va
.f_bfree
;
3789 if (VFSATTR_IS_SUPPORTED(&va
, f_bavail
))
3790 mp
->mnt_vfsstat
.f_bavail
= va
.f_bavail
;
3791 if (VFSATTR_IS_SUPPORTED(&va
, f_bused
))
3792 mp
->mnt_vfsstat
.f_bused
= va
.f_bused
;
3793 if (VFSATTR_IS_SUPPORTED(&va
, f_files
))
3794 mp
->mnt_vfsstat
.f_files
= va
.f_files
;
3795 if (VFSATTR_IS_SUPPORTED(&va
, f_ffree
))
3796 mp
->mnt_vfsstat
.f_ffree
= va
.f_ffree
;
3798 /* this is unlikely to change, but has to be queried for */
3799 if (VFSATTR_IS_SUPPORTED(&va
, f_fssubtype
))
3800 mp
->mnt_vfsstat
.f_fssubtype
= va
.f_fssubtype
;
3806 mount_list_add(mount_t mp
)
3809 TAILQ_INSERT_TAIL(&mountlist
, mp
, mnt_list
);
3811 mount_list_unlock();
3815 mount_list_remove(mount_t mp
)
3818 TAILQ_REMOVE(&mountlist
, mp
, mnt_list
);
3820 mp
->mnt_list
.tqe_next
= 0;
3821 mp
->mnt_list
.tqe_prev
= 0;
3822 mount_list_unlock();
3826 mount_lookupby_volfsid(int volfs_id
, int withref
)
3828 mount_t cur_mount
= (mount_t
)0;
3832 TAILQ_FOREACH(mp
, &mountlist
, mnt_list
) {
3833 if (validfsnode(mp
) && mp
->mnt_vfsstat
.f_fsid
.val
[0] == volfs_id
) {
3836 if (mount_iterref(cur_mount
, 1)) {
3837 cur_mount
= (mount_t
)0;
3838 mount_list_unlock();
3845 mount_list_unlock();
3846 if (withref
&& (cur_mount
!= (mount_t
)0)) {
3848 if (vfs_busy(mp
, LK_NOWAIT
) != 0) {
3849 cur_mount
= (mount_t
)0;
3859 mount_list_lookupby_fsid(fsid
, locked
, withref
)
3864 mount_t retmp
= (mount_t
)0;
3869 TAILQ_FOREACH(mp
, &mountlist
, mnt_list
)
3870 if (mp
->mnt_vfsstat
.f_fsid
.val
[0] == fsid
->val
[0] &&
3871 mp
->mnt_vfsstat
.f_fsid
.val
[1] == fsid
->val
[1]) {
3874 if (mount_iterref(retmp
, 1))
3881 mount_list_unlock();
3886 vnode_lookup(const char *path
, int flags
, vnode_t
*vpp
, vfs_context_t context
)
3888 struct nameidata nd
;
3890 struct vfs_context context2
;
3891 vfs_context_t ctx
= context
;
3894 if (context
== NULL
) { /* XXX technically an error */
3895 context2
.vc_proc
= current_proc();
3896 context2
.vc_ucred
= kauth_cred_get();
3900 if (flags
& VNODE_LOOKUP_NOFOLLOW
)
3905 if (flags
& VNODE_LOOKUP_NOCROSSMOUNT
)
3906 ndflags
|= NOCROSSMOUNT
;
3907 if (flags
& VNODE_LOOKUP_DOWHITEOUT
)
3908 ndflags
|= DOWHITEOUT
;
3910 /* XXX AUDITVNPATH1 needed ? */
3911 NDINIT(&nd
, LOOKUP
, ndflags
, UIO_SYSSPACE
, CAST_USER_ADDR_T(path
), ctx
);
3913 if ((error
= namei(&nd
)))
3922 vnode_open(const char *path
, int fmode
, int cmode
, int flags
, vnode_t
*vpp
, vfs_context_t context
)
3924 struct nameidata nd
;
3926 struct vfs_context context2
;
3927 vfs_context_t ctx
= context
;
3931 if (context
== NULL
) { /* XXX technically an error */
3932 context2
.vc_proc
= current_proc();
3933 context2
.vc_ucred
= kauth_cred_get();
3937 if (fmode
& O_NOFOLLOW
)
3938 lflags
|= VNODE_LOOKUP_NOFOLLOW
;
3940 if (lflags
& VNODE_LOOKUP_NOFOLLOW
)
3945 if (lflags
& VNODE_LOOKUP_NOCROSSMOUNT
)
3946 ndflags
|= NOCROSSMOUNT
;
3947 if (lflags
& VNODE_LOOKUP_DOWHITEOUT
)
3948 ndflags
|= DOWHITEOUT
;
3950 /* XXX AUDITVNPATH1 needed ? */
3951 NDINIT(&nd
, LOOKUP
, ndflags
, UIO_SYSSPACE
, CAST_USER_ADDR_T(path
), ctx
);
3953 if ((error
= vn_open(&nd
, fmode
, cmode
)))
3962 vnode_close(vnode_t vp
, int flags
, vfs_context_t context
)
3969 p
= context
->vc_proc
;
3970 cred
= context
->vc_ucred
;
3973 cred
= kauth_cred_get();
3976 error
= vn_close(vp
, flags
, cred
, p
);
3982 vnode_size(vnode_t vp
, off_t
*sizep
, vfs_context_t ctx
)
3984 struct vnode_attr va
;
3988 VATTR_WANTED(&va
, va_data_size
);
3989 error
= vnode_getattr(vp
, &va
, ctx
);
3991 *sizep
= va
.va_data_size
;
3996 vnode_setsize(vnode_t vp
, off_t size
, int ioflag
, vfs_context_t ctx
)
3998 struct vnode_attr va
;
4001 VATTR_SET(&va
, va_data_size
, size
);
4002 va
.va_vaflags
= ioflag
& 0xffff;
4003 return(vnode_setattr(vp
, &va
, ctx
));
4007 * Create a filesystem object of arbitrary type with arbitrary attributes in
4008 * the spevied directory with the specified name.
4010 * Parameters: dvp Pointer to the vnode of the directory
4011 * in which to create the object.
4012 * vpp Pointer to the area into which to
4013 * return the vnode of the created object.
4014 * cnp Component name pointer from the namei
4015 * data structure, containing the name to
4016 * use for the create object.
4017 * vap Pointer to the vnode_attr structure
4018 * describing the object to be created,
4019 * including the type of object.
4020 * flags VN_* flags controlling ACL inheritance
4021 * and whether or not authorization is to
4022 * be required for the operation.
4024 * Returns: 0 Success
4027 * Implicit: *vpp Contains the vnode of the object that
4028 * was created, if successful.
4029 * *cnp May be modified by the underlying VFS.
4030 * *vap May be modified by the underlying VFS.
4031 * modified by either ACL inheritance or
4034 * be modified, even if the operation is
4037 * Notes: The kauth_filesec_t in 'vap', if any, is in host byte order.
4039 * Modification of '*cnp' and '*vap' by the underlying VFS is
4040 * strongly discouraged.
4042 * XXX: This function is a 'vn_*' function; it belongs in vfs_vnops.c
4044 * XXX: We should enummerate the possible errno values here, and where
4045 * in the code they originated.
4048 vn_create(vnode_t dvp
, vnode_t
*vpp
, struct componentname
*cnp
, struct vnode_attr
*vap
, int flags
, vfs_context_t ctx
)
4050 kauth_acl_t oacl
, nacl
;
4053 vnode_t vp
= (vnode_t
)0;
4059 KAUTH_DEBUG("%p CREATE - '%s'", dvp
, cnp
->cn_nameptr
);
4062 * Handle ACL inheritance.
4064 if (!(flags
& VN_CREATE_NOINHERIT
) && vfs_extendedsecurity(dvp
->v_mount
)) {
4065 /* save the original filesec */
4066 if (VATTR_IS_ACTIVE(vap
, va_acl
)) {
4072 if ((error
= kauth_acl_inherit(dvp
,
4075 vap
->va_type
== VDIR
,
4077 KAUTH_DEBUG("%p CREATE - error %d processing inheritance", dvp
, error
);
4082 * If the generated ACL is NULL, then we can save ourselves some effort
4083 * by clearing the active bit.
4086 VATTR_CLEAR_ACTIVE(vap
, va_acl
);
4088 VATTR_SET(vap
, va_acl
, nacl
);
4093 * Check and default new attributes.
4094 * This will set va_uid, va_gid, va_mode and va_create_time at least, if the caller
4095 * hasn't supplied them.
4097 if ((error
= vnode_authattr_new(dvp
, vap
, flags
& VN_CREATE_NOAUTH
, ctx
)) != 0) {
4098 KAUTH_DEBUG("%p CREATE - error %d handing/defaulting attributes", dvp
, error
);
4104 * Create the requested node.
4106 switch(vap
->va_type
) {
4108 error
= VNOP_CREATE(dvp
, vpp
, cnp
, vap
, ctx
);
4111 error
= VNOP_MKDIR(dvp
, vpp
, cnp
, vap
, ctx
);
4117 error
= VNOP_MKNOD(dvp
, vpp
, cnp
, vap
, ctx
);
4120 panic("vnode_create: unknown vtype %d", vap
->va_type
);
4123 KAUTH_DEBUG("%p CREATE - error %d returned by filesystem", dvp
, error
);
4129 * If some of the requested attributes weren't handled by the VNOP,
4130 * use our fallback code.
4132 if (!VATTR_ALL_SUPPORTED(vap
) && *vpp
) {
4133 KAUTH_DEBUG(" CREATE - doing fallback with ACL %p", vap
->va_acl
);
4134 error
= vnode_setattr_fallback(*vpp
, vap
, ctx
);
4136 if ((error
!= 0 ) && (vp
!= (vnode_t
)0)) {
4143 * If the caller supplied a filesec in vap, it has been replaced
4144 * now by the post-inheritance copy. We need to put the original back
4145 * and free the inherited product.
4148 VATTR_SET(vap
, va_acl
, oacl
);
4150 VATTR_CLEAR_ACTIVE(vap
, va_acl
);
4153 kauth_acl_free(nacl
);
4158 static kauth_scope_t vnode_scope
;
4159 static int vnode_authorize_callback(kauth_cred_t credential
, __unused
void *idata
, kauth_action_t action
,
4160 uintptr_t arg0
, uintptr_t arg1
, uintptr_t arg2
, uintptr_t arg3
);
4162 typedef struct _vnode_authorize_context
{
4164 struct vnode_attr
*vap
;
4166 struct vnode_attr
*dvap
;
4170 #define _VAC_IS_OWNER (1<<0)
4171 #define _VAC_IN_GROUP (1<<1)
4172 #define _VAC_IS_DIR_OWNER (1<<2)
4173 #define _VAC_IN_DIR_GROUP (1<<3)
4177 vnode_authorize_init(void)
4179 vnode_scope
= kauth_register_scope(KAUTH_SCOPE_VNODE
, vnode_authorize_callback
, NULL
);
4183 * Authorize an operation on a vnode.
4185 * This is KPI, but here because it needs vnode_scope.
4188 vnode_authorize(vnode_t vp
, vnode_t dvp
, kauth_action_t action
, vfs_context_t context
)
4193 * We can't authorize against a dead vnode; allow all operations through so that
4194 * the correct error can be returned.
4196 if (vp
->v_type
== VBAD
)
4200 result
= kauth_authorize_action(vnode_scope
, vfs_context_ucred(context
), action
,
4201 (uintptr_t)context
, (uintptr_t)vp
, (uintptr_t)dvp
, (uintptr_t)&error
);
4202 if (result
== EPERM
) /* traditional behaviour */
4204 /* did the lower layers give a better error return? */
4205 if ((result
!= 0) && (error
!= 0))
4211 * Test for vnode immutability.
4213 * The 'append' flag is set when the authorization request is constrained
4214 * to operations which only request the right to append to a file.
4216 * The 'ignore' flag is set when an operation modifying the immutability flags
4217 * is being authorized. We check the system securelevel to determine which
4218 * immutability flags we can ignore.
4221 vnode_immutable(struct vnode_attr
*vap
, int append
, int ignore
)
4225 /* start with all bits precluding the operation */
4226 mask
= IMMUTABLE
| APPEND
;
4228 /* if appending only, remove the append-only bits */
4232 /* ignore only set when authorizing flags changes */
4234 if (securelevel
<= 0) {
4235 /* in insecure state, flags do not inhibit changes */
4238 /* in secure state, user flags don't inhibit */
4239 mask
&= ~(UF_IMMUTABLE
| UF_APPEND
);
4242 KAUTH_DEBUG("IMMUTABLE - file flags 0x%x mask 0x%x append = %d ignore = %d", vap
->va_flags
, mask
, append
, ignore
);
4243 if ((vap
->va_flags
& mask
) != 0)
4249 vauth_node_owner(struct vnode_attr
*vap
, kauth_cred_t cred
)
4253 /* default assumption is not-owner */
4257 * If the filesystem has given us a UID, we treat this as authoritative.
4259 if (vap
&& VATTR_IS_SUPPORTED(vap
, va_uid
)) {
4260 result
= (vap
->va_uid
== kauth_cred_getuid(cred
)) ? 1 : 0;
4262 /* we could test the owner UUID here if we had a policy for it */
4268 vauth_node_group(struct vnode_attr
*vap
, kauth_cred_t cred
, int *ismember
)
4276 /* the caller is expected to have asked the filesystem for a group at some point */
4277 if (vap
&& VATTR_IS_SUPPORTED(vap
, va_gid
)) {
4278 error
= kauth_cred_ismember_gid(cred
, vap
->va_gid
, &result
);
4280 /* we could test the group UUID here if we had a policy for it */
4288 vauth_file_owner(vauth_ctx vcp
)
4292 if (vcp
->flags_valid
& _VAC_IS_OWNER
) {
4293 result
= (vcp
->flags
& _VAC_IS_OWNER
) ? 1 : 0;
4295 result
= vauth_node_owner(vcp
->vap
, vcp
->ctx
->vc_ucred
);
4297 /* cache our result */
4298 vcp
->flags_valid
|= _VAC_IS_OWNER
;
4300 vcp
->flags
|= _VAC_IS_OWNER
;
4302 vcp
->flags
&= ~_VAC_IS_OWNER
;
4309 vauth_file_ingroup(vauth_ctx vcp
, int *ismember
)
4313 if (vcp
->flags_valid
& _VAC_IN_GROUP
) {
4314 *ismember
= (vcp
->flags
& _VAC_IN_GROUP
) ? 1 : 0;
4317 error
= vauth_node_group(vcp
->vap
, vcp
->ctx
->vc_ucred
, ismember
);
4320 /* cache our result */
4321 vcp
->flags_valid
|= _VAC_IN_GROUP
;
4323 vcp
->flags
|= _VAC_IN_GROUP
;
4325 vcp
->flags
&= ~_VAC_IN_GROUP
;
4334 vauth_dir_owner(vauth_ctx vcp
)
4338 if (vcp
->flags_valid
& _VAC_IS_DIR_OWNER
) {
4339 result
= (vcp
->flags
& _VAC_IS_DIR_OWNER
) ? 1 : 0;
4341 result
= vauth_node_owner(vcp
->dvap
, vcp
->ctx
->vc_ucred
);
4343 /* cache our result */
4344 vcp
->flags_valid
|= _VAC_IS_DIR_OWNER
;
4346 vcp
->flags
|= _VAC_IS_DIR_OWNER
;
4348 vcp
->flags
&= ~_VAC_IS_DIR_OWNER
;
4355 vauth_dir_ingroup(vauth_ctx vcp
, int *ismember
)
4359 if (vcp
->flags_valid
& _VAC_IN_DIR_GROUP
) {
4360 *ismember
= (vcp
->flags
& _VAC_IN_DIR_GROUP
) ? 1 : 0;
4363 error
= vauth_node_group(vcp
->dvap
, vcp
->ctx
->vc_ucred
, ismember
);
4366 /* cache our result */
4367 vcp
->flags_valid
|= _VAC_IN_DIR_GROUP
;
4369 vcp
->flags
|= _VAC_IN_DIR_GROUP
;
4371 vcp
->flags
&= ~_VAC_IN_DIR_GROUP
;
4379 * Test the posix permissions in (vap) to determine whether (credential)
4380 * may perform (action)
4383 vnode_authorize_posix(vauth_ctx vcp
, int action
, int on_dir
)
4385 struct vnode_attr
*vap
;
4386 int needed
, error
, owner_ok
, group_ok
, world_ok
, ismember
;
4387 #ifdef KAUTH_DEBUG_ENABLE
4389 # define _SETWHERE(c) where = c;
4391 # define _SETWHERE(c)
4394 /* checking file or directory? */
4404 * We want to do as little work here as possible. So first we check
4405 * which sets of permissions grant us the access we need, and avoid checking
4406 * whether specific permissions grant access when more generic ones would.
4409 /* owner permissions */
4413 if (action
& VWRITE
)
4417 owner_ok
= (needed
& vap
->va_mode
) == needed
;
4419 /* group permissions */
4423 if (action
& VWRITE
)
4427 group_ok
= (needed
& vap
->va_mode
) == needed
;
4429 /* world permissions */
4433 if (action
& VWRITE
)
4437 world_ok
= (needed
& vap
->va_mode
) == needed
;
4439 /* If granted/denied by all three, we're done */
4440 if (owner_ok
&& group_ok
&& world_ok
) {
4444 if (!owner_ok
&& !group_ok
&& !world_ok
) {
4450 /* Check ownership (relatively cheap) */
4451 if ((on_dir
&& vauth_dir_owner(vcp
)) ||
4452 (!on_dir
&& vauth_file_owner(vcp
))) {
4459 /* Not owner; if group and world both grant it we're done */
4460 if (group_ok
&& world_ok
) {
4461 _SETWHERE("group/world");
4464 if (!group_ok
&& !world_ok
) {
4465 _SETWHERE("group/world");
4470 /* Check group membership (most expensive) */
4473 error
= vauth_dir_ingroup(vcp
, &ismember
);
4475 error
= vauth_file_ingroup(vcp
, &ismember
);
4486 /* Not owner, not in group, use world result */
4494 KAUTH_DEBUG("%p %s - posix %s permissions : need %s%s%s %x have %s%s%s%s%s%s%s%s%s UID = %d file = %d,%d",
4495 vcp
->vp
, (error
== 0) ? "ALLOWED" : "DENIED", where
,
4496 (action
& VREAD
) ? "r" : "-",
4497 (action
& VWRITE
) ? "w" : "-",
4498 (action
& VEXEC
) ? "x" : "-",
4500 (vap
->va_mode
& S_IRUSR
) ? "r" : "-",
4501 (vap
->va_mode
& S_IWUSR
) ? "w" : "-",
4502 (vap
->va_mode
& S_IXUSR
) ? "x" : "-",
4503 (vap
->va_mode
& S_IRGRP
) ? "r" : "-",
4504 (vap
->va_mode
& S_IWGRP
) ? "w" : "-",
4505 (vap
->va_mode
& S_IXGRP
) ? "x" : "-",
4506 (vap
->va_mode
& S_IROTH
) ? "r" : "-",
4507 (vap
->va_mode
& S_IWOTH
) ? "w" : "-",
4508 (vap
->va_mode
& S_IXOTH
) ? "x" : "-",
4509 kauth_cred_getuid(vcp
->ctx
->vc_ucred
),
4510 on_dir
? vcp
->dvap
->va_uid
: vcp
->vap
->va_uid
,
4511 on_dir
? vcp
->dvap
->va_gid
: vcp
->vap
->va_gid
);
4516 * Authorize the deletion of the node vp from the directory dvp.
4519 * - Neither the node nor the directory are immutable.
4520 * - The user is not the superuser.
4522 * Deletion is not permitted if the directory is sticky and the caller is not owner of the
4523 * node or directory.
4525 * If either the node grants DELETE, or the directory grants DELETE_CHILD, the node may be
4526 * deleted. If neither denies the permission, and the caller has Posix write access to the
4527 * directory, then the node may be deleted.
4530 vnode_authorize_delete(vauth_ctx vcp
)
4532 struct vnode_attr
*vap
= vcp
->vap
;
4533 struct vnode_attr
*dvap
= vcp
->dvap
;
4534 kauth_cred_t cred
= vcp
->ctx
->vc_ucred
;
4535 struct kauth_acl_eval eval
;
4536 int error
, delete_denied
, delete_child_denied
, ismember
;
4538 /* check the ACL on the directory */
4539 delete_child_denied
= 0;
4540 if (VATTR_IS_NOT(dvap
, va_acl
, NULL
)) {
4541 eval
.ae_requested
= KAUTH_VNODE_DELETE_CHILD
;
4542 eval
.ae_acl
= &dvap
->va_acl
->acl_ace
[0];
4543 eval
.ae_count
= dvap
->va_acl
->acl_entrycount
;
4544 eval
.ae_options
= 0;
4545 if (vauth_dir_owner(vcp
))
4546 eval
.ae_options
|= KAUTH_AEVAL_IS_OWNER
;
4547 if ((error
= vauth_dir_ingroup(vcp
, &ismember
)) != 0)
4550 eval
.ae_options
|= KAUTH_AEVAL_IN_GROUP
;
4551 eval
.ae_exp_gall
= KAUTH_VNODE_GENERIC_ALL_BITS
;
4552 eval
.ae_exp_gread
= KAUTH_VNODE_GENERIC_READ_BITS
;
4553 eval
.ae_exp_gwrite
= KAUTH_VNODE_GENERIC_WRITE_BITS
;
4554 eval
.ae_exp_gexec
= KAUTH_VNODE_GENERIC_EXECUTE_BITS
;
4556 error
= kauth_acl_evaluate(cred
, &eval
);
4559 KAUTH_DEBUG("%p ERROR during ACL processing - %d", vcp
->vp
, error
);
4562 if (eval
.ae_result
== KAUTH_RESULT_DENY
)
4563 delete_child_denied
= 1;
4564 if (eval
.ae_result
== KAUTH_RESULT_ALLOW
) {
4565 KAUTH_DEBUG("%p ALLOWED - granted by directory ACL", vcp
->vp
);
4570 /* check the ACL on the node */
4572 if (VATTR_IS_NOT(vap
, va_acl
, NULL
)) {
4573 eval
.ae_requested
= KAUTH_VNODE_DELETE
;
4574 eval
.ae_acl
= &vap
->va_acl
->acl_ace
[0];
4575 eval
.ae_count
= vap
->va_acl
->acl_entrycount
;
4576 eval
.ae_options
= 0;
4577 if (vauth_file_owner(vcp
))
4578 eval
.ae_options
|= KAUTH_AEVAL_IS_OWNER
;
4579 if ((error
= vauth_file_ingroup(vcp
, &ismember
)) != 0)
4582 eval
.ae_options
|= KAUTH_AEVAL_IN_GROUP
;
4583 eval
.ae_exp_gall
= KAUTH_VNODE_GENERIC_ALL_BITS
;
4584 eval
.ae_exp_gread
= KAUTH_VNODE_GENERIC_READ_BITS
;
4585 eval
.ae_exp_gwrite
= KAUTH_VNODE_GENERIC_WRITE_BITS
;
4586 eval
.ae_exp_gexec
= KAUTH_VNODE_GENERIC_EXECUTE_BITS
;
4588 if ((error
= kauth_acl_evaluate(cred
, &eval
)) != 0) {
4589 KAUTH_DEBUG("%p ERROR during ACL processing - %d", vcp
->vp
, error
);
4592 if (eval
.ae_result
== KAUTH_RESULT_DENY
)
4594 if (eval
.ae_result
== KAUTH_RESULT_ALLOW
) {
4595 KAUTH_DEBUG("%p ALLOWED - granted by file ACL", vcp
->vp
);
4600 /* if denied by ACL on directory or node, return denial */
4601 if (delete_denied
|| delete_child_denied
) {
4602 KAUTH_DEBUG("%p ALLOWED - denied by ACL", vcp
->vp
);
4606 /* enforce sticky bit behaviour */
4607 if ((dvap
->va_mode
& S_ISTXT
) && !vauth_file_owner(vcp
) && !vauth_dir_owner(vcp
)) {
4608 KAUTH_DEBUG("%p DENIED - sticky bit rules (user %d file %d dir %d)",
4609 vcp
->vp
, cred
->cr_uid
, vap
->va_uid
, dvap
->va_uid
);
4613 /* check the directory */
4614 if ((error
= vnode_authorize_posix(vcp
, VWRITE
, 1 /* on_dir */)) != 0) {
4615 KAUTH_DEBUG("%p ALLOWED - granted by posix permisssions", vcp
->vp
);
4619 /* not denied, must be OK */
4625 * Authorize an operation based on the node's attributes.
4628 vnode_authorize_simple(vauth_ctx vcp
, kauth_ace_rights_t acl_rights
, kauth_ace_rights_t preauth_rights
)
4630 struct vnode_attr
*vap
= vcp
->vap
;
4631 kauth_cred_t cred
= vcp
->ctx
->vc_ucred
;
4632 struct kauth_acl_eval eval
;
4633 int error
, ismember
;
4634 mode_t posix_action
;
4637 * If we are the file owner, we automatically have some rights.
4639 * Do we need to expand this to support group ownership?
4641 if (vauth_file_owner(vcp
))
4642 acl_rights
&= ~(KAUTH_VNODE_WRITE_SECURITY
);
4645 * If we are checking both TAKE_OWNERSHIP and WRITE_SECURITY, we can
4646 * mask the latter. If TAKE_OWNERSHIP is requested the caller is about to
4647 * change ownership to themselves, and WRITE_SECURITY is implicitly
4648 * granted to the owner. We need to do this because at this point
4649 * WRITE_SECURITY may not be granted as the caller is not currently
4652 if ((acl_rights
& KAUTH_VNODE_TAKE_OWNERSHIP
) &&
4653 (acl_rights
& KAUTH_VNODE_WRITE_SECURITY
))
4654 acl_rights
&= ~KAUTH_VNODE_WRITE_SECURITY
;
4656 if (acl_rights
== 0) {
4657 KAUTH_DEBUG("%p ALLOWED - implicit or no rights required", vcp
->vp
);
4661 /* if we have an ACL, evaluate it */
4662 if (VATTR_IS_NOT(vap
, va_acl
, NULL
)) {
4663 eval
.ae_requested
= acl_rights
;
4664 eval
.ae_acl
= &vap
->va_acl
->acl_ace
[0];
4665 eval
.ae_count
= vap
->va_acl
->acl_entrycount
;
4666 eval
.ae_options
= 0;
4667 if (vauth_file_owner(vcp
))
4668 eval
.ae_options
|= KAUTH_AEVAL_IS_OWNER
;
4669 if ((error
= vauth_file_ingroup(vcp
, &ismember
)) != 0)
4672 eval
.ae_options
|= KAUTH_AEVAL_IN_GROUP
;
4673 eval
.ae_exp_gall
= KAUTH_VNODE_GENERIC_ALL_BITS
;
4674 eval
.ae_exp_gread
= KAUTH_VNODE_GENERIC_READ_BITS
;
4675 eval
.ae_exp_gwrite
= KAUTH_VNODE_GENERIC_WRITE_BITS
;
4676 eval
.ae_exp_gexec
= KAUTH_VNODE_GENERIC_EXECUTE_BITS
;
4678 if ((error
= kauth_acl_evaluate(cred
, &eval
)) != 0) {
4679 KAUTH_DEBUG("%p ERROR during ACL processing - %d", vcp
->vp
, error
);
4683 if (eval
.ae_result
== KAUTH_RESULT_DENY
) {
4684 KAUTH_DEBUG("%p DENIED - by ACL", vcp
->vp
);
4685 return(EACCES
); /* deny, deny, counter-allege */
4687 if (eval
.ae_result
== KAUTH_RESULT_ALLOW
) {
4688 KAUTH_DEBUG("%p ALLOWED - all rights granted by ACL", vcp
->vp
);
4691 /* fall through and evaluate residual rights */
4693 /* no ACL, everything is residual */
4694 eval
.ae_residual
= acl_rights
;
4698 * Grant residual rights that have been pre-authorized.
4700 eval
.ae_residual
&= ~preauth_rights
;
4703 * We grant WRITE_ATTRIBUTES to the owner if it hasn't been denied.
4705 if (vauth_file_owner(vcp
))
4706 eval
.ae_residual
&= ~KAUTH_VNODE_WRITE_ATTRIBUTES
;
4708 if (eval
.ae_residual
== 0) {
4709 KAUTH_DEBUG("%p ALLOWED - rights already authorized", vcp
->vp
);
4714 * Bail if we have residual rights that can't be granted by posix permissions,
4715 * or aren't presumed granted at this point.
4717 * XXX these can be collapsed for performance
4719 if (eval
.ae_residual
& KAUTH_VNODE_CHANGE_OWNER
) {
4720 KAUTH_DEBUG("%p DENIED - CHANGE_OWNER not permitted", vcp
->vp
);
4723 if (eval
.ae_residual
& KAUTH_VNODE_WRITE_SECURITY
) {
4724 KAUTH_DEBUG("%p DENIED - WRITE_SECURITY not permitted", vcp
->vp
);
4729 if (eval
.ae_residual
& KAUTH_VNODE_DELETE
)
4730 panic("vnode_authorize: can't be checking delete permission here");
4734 * Compute the fallback posix permissions that will satisfy the remaining
4738 if (eval
.ae_residual
& (KAUTH_VNODE_READ_DATA
|
4739 KAUTH_VNODE_LIST_DIRECTORY
|
4740 KAUTH_VNODE_READ_EXTATTRIBUTES
))
4741 posix_action
|= VREAD
;
4742 if (eval
.ae_residual
& (KAUTH_VNODE_WRITE_DATA
|
4743 KAUTH_VNODE_ADD_FILE
|
4744 KAUTH_VNODE_ADD_SUBDIRECTORY
|
4745 KAUTH_VNODE_DELETE_CHILD
|
4746 KAUTH_VNODE_WRITE_ATTRIBUTES
|
4747 KAUTH_VNODE_WRITE_EXTATTRIBUTES
))
4748 posix_action
|= VWRITE
;
4749 if (eval
.ae_residual
& (KAUTH_VNODE_EXECUTE
|
4750 KAUTH_VNODE_SEARCH
))
4751 posix_action
|= VEXEC
;
4753 if (posix_action
!= 0) {
4754 return(vnode_authorize_posix(vcp
, posix_action
, 0 /* !on_dir */));
4756 KAUTH_DEBUG("%p ALLOWED - residual rights %s%s%s%s%s%s%s%s%s%s%s%s%s%s granted due to no posix mapping",
4758 (eval
.ae_residual
& KAUTH_VNODE_READ_DATA
)
4759 ? vnode_isdir(vcp
->vp
) ? " LIST_DIRECTORY" : " READ_DATA" : "",
4760 (eval
.ae_residual
& KAUTH_VNODE_WRITE_DATA
)
4761 ? vnode_isdir(vcp
->vp
) ? " ADD_FILE" : " WRITE_DATA" : "",
4762 (eval
.ae_residual
& KAUTH_VNODE_EXECUTE
)
4763 ? vnode_isdir(vcp
->vp
) ? " SEARCH" : " EXECUTE" : "",
4764 (eval
.ae_residual
& KAUTH_VNODE_DELETE
)
4766 (eval
.ae_residual
& KAUTH_VNODE_APPEND_DATA
)
4767 ? vnode_isdir(vcp
->vp
) ? " ADD_SUBDIRECTORY" : " APPEND_DATA" : "",
4768 (eval
.ae_residual
& KAUTH_VNODE_DELETE_CHILD
)
4769 ? " DELETE_CHILD" : "",
4770 (eval
.ae_residual
& KAUTH_VNODE_READ_ATTRIBUTES
)
4771 ? " READ_ATTRIBUTES" : "",
4772 (eval
.ae_residual
& KAUTH_VNODE_WRITE_ATTRIBUTES
)
4773 ? " WRITE_ATTRIBUTES" : "",
4774 (eval
.ae_residual
& KAUTH_VNODE_READ_EXTATTRIBUTES
)
4775 ? " READ_EXTATTRIBUTES" : "",
4776 (eval
.ae_residual
& KAUTH_VNODE_WRITE_EXTATTRIBUTES
)
4777 ? " WRITE_EXTATTRIBUTES" : "",
4778 (eval
.ae_residual
& KAUTH_VNODE_READ_SECURITY
)
4779 ? " READ_SECURITY" : "",
4780 (eval
.ae_residual
& KAUTH_VNODE_WRITE_SECURITY
)
4781 ? " WRITE_SECURITY" : "",
4782 (eval
.ae_residual
& KAUTH_VNODE_CHECKIMMUTABLE
)
4783 ? " CHECKIMMUTABLE" : "",
4784 (eval
.ae_residual
& KAUTH_VNODE_CHANGE_OWNER
)
4785 ? " CHANGE_OWNER" : "");
4789 * Lack of required Posix permissions implies no reason to deny access.
4795 * Check for file immutability.
4798 vnode_authorize_checkimmutable(vnode_t vp
, struct vnode_attr
*vap
, int rights
, int ignore
)
4805 * Perform immutability checks for operations that change data.
4807 * Sockets, fifos and devices require special handling.
4809 switch(vp
->v_type
) {
4815 * Writing to these nodes does not change the filesystem data,
4816 * so forget that it's being tried.
4818 rights
&= ~KAUTH_VNODE_WRITE_DATA
;
4825 if (rights
& KAUTH_VNODE_WRITE_RIGHTS
) {
4827 /* check per-filesystem options if possible */
4828 mp
= vnode_mount(vp
);
4831 /* check for no-EA filesystems */
4832 if ((rights
& KAUTH_VNODE_WRITE_EXTATTRIBUTES
) &&
4833 (vfs_flags(mp
) & MNT_NOUSERXATTR
)) {
4834 KAUTH_DEBUG("%p DENIED - filesystem disallowed extended attributes", vp
);
4835 error
= EACCES
; /* User attributes disabled */
4840 /* check for file immutability */
4842 if (vp
->v_type
== VDIR
) {
4843 if ((rights
& (KAUTH_VNODE_ADD_FILE
| KAUTH_VNODE_ADD_SUBDIRECTORY
)) == rights
)
4846 if ((rights
& KAUTH_VNODE_APPEND_DATA
) == rights
)
4849 if ((error
= vnode_immutable(vap
, append
, ignore
)) != 0) {
4850 KAUTH_DEBUG("%p DENIED - file is immutable", vp
);
4859 * Handle authorization actions for filesystems that advertise that the server will
4863 vnode_authorize_opaque(vnode_t vp
, int *resultp
, kauth_action_t action
, vfs_context_t ctx
)
4868 * If the vp is a device node, socket or FIFO it actually represents a local
4869 * endpoint, so we need to handle it locally.
4871 switch(vp
->v_type
) {
4882 * In the advisory request case, if the filesystem doesn't think it's reliable
4883 * we will attempt to formulate a result ourselves based on VNOP_GETATTR data.
4885 if ((action
& KAUTH_VNODE_ACCESS
) && !vfs_authopaqueaccess(vnode_mount(vp
)))
4889 * Let the filesystem have a say in the matter. It's OK for it to not implemnent
4890 * VNOP_ACCESS, as most will authorise inline with the actual request.
4892 if ((error
= VNOP_ACCESS(vp
, action
, ctx
)) != ENOTSUP
) {
4894 KAUTH_DEBUG("%p DENIED - opaque filesystem VNOP_ACCESS denied access", vp
);
4899 * Typically opaque filesystems do authorisation in-line, but exec is a special case. In
4900 * order to be reasonably sure that exec will be permitted, we try a bit harder here.
4902 if ((action
& KAUTH_VNODE_EXECUTE
) && vnode_isreg(vp
)) {
4903 /* try a VNOP_OPEN for readonly access */
4904 if ((error
= VNOP_OPEN(vp
, FREAD
, ctx
)) != 0) {
4906 KAUTH_DEBUG("%p DENIED - EXECUTE denied because file could not be opened readonly", vp
);
4909 VNOP_CLOSE(vp
, FREAD
, ctx
);
4913 * We don't have any reason to believe that the request has to be denied at this point,
4914 * so go ahead and allow it.
4917 KAUTH_DEBUG("%p ALLOWED - bypassing access check for non-local filesystem", vp
);
4922 vnode_authorize_callback(__unused kauth_cred_t unused_cred
, __unused
void *idata
, kauth_action_t action
,
4923 uintptr_t arg0
, uintptr_t arg1
, uintptr_t arg2
, uintptr_t arg3
)
4925 struct _vnode_authorize_context auth_context
;
4930 kauth_ace_rights_t rights
;
4931 struct vnode_attr va
, dva
;
4936 vcp
= &auth_context
;
4937 ctx
= vcp
->ctx
= (vfs_context_t
)arg0
;
4938 vp
= vcp
->vp
= (vnode_t
)arg1
;
4939 dvp
= vcp
->dvp
= (vnode_t
)arg2
;
4940 errorp
= (int *)arg3
;
4941 /* note that we authorize against the context, not the passed cred (the same thing anyway) */
4942 cred
= ctx
->vc_ucred
;
4949 vcp
->flags
= vcp
->flags_valid
= 0;
4952 if ((ctx
== NULL
) || (vp
== NULL
) || (cred
== NULL
))
4953 panic("vnode_authorize: bad arguments (context %p vp %p cred %p)", ctx
, vp
, cred
);
4956 KAUTH_DEBUG("%p AUTH - %s %s%s%s%s%s%s%s%s%s%s%s%s%s%s%s on %s '%s' (0x%x:%p/%p)",
4957 vp
, vfs_context_proc(ctx
)->p_comm
,
4958 (action
& KAUTH_VNODE_ACCESS
) ? "access" : "auth",
4959 (action
& KAUTH_VNODE_READ_DATA
) ? vnode_isdir(vp
) ? " LIST_DIRECTORY" : " READ_DATA" : "",
4960 (action
& KAUTH_VNODE_WRITE_DATA
) ? vnode_isdir(vp
) ? " ADD_FILE" : " WRITE_DATA" : "",
4961 (action
& KAUTH_VNODE_EXECUTE
) ? vnode_isdir(vp
) ? " SEARCH" : " EXECUTE" : "",
4962 (action
& KAUTH_VNODE_DELETE
) ? " DELETE" : "",
4963 (action
& KAUTH_VNODE_APPEND_DATA
) ? vnode_isdir(vp
) ? " ADD_SUBDIRECTORY" : " APPEND_DATA" : "",
4964 (action
& KAUTH_VNODE_DELETE_CHILD
) ? " DELETE_CHILD" : "",
4965 (action
& KAUTH_VNODE_READ_ATTRIBUTES
) ? " READ_ATTRIBUTES" : "",
4966 (action
& KAUTH_VNODE_WRITE_ATTRIBUTES
) ? " WRITE_ATTRIBUTES" : "",
4967 (action
& KAUTH_VNODE_READ_EXTATTRIBUTES
) ? " READ_EXTATTRIBUTES" : "",
4968 (action
& KAUTH_VNODE_WRITE_EXTATTRIBUTES
) ? " WRITE_EXTATTRIBUTES" : "",
4969 (action
& KAUTH_VNODE_READ_SECURITY
) ? " READ_SECURITY" : "",
4970 (action
& KAUTH_VNODE_WRITE_SECURITY
) ? " WRITE_SECURITY" : "",
4971 (action
& KAUTH_VNODE_CHANGE_OWNER
) ? " CHANGE_OWNER" : "",
4972 (action
& KAUTH_VNODE_NOIMMUTABLE
) ? " (noimmutable)" : "",
4973 vnode_isdir(vp
) ? "directory" : "file",
4974 vp
->v_name
? vp
->v_name
: "<NULL>", action
, vp
, dvp
);
4977 * Extract the control bits from the action, everything else is
4980 noimmutable
= (action
& KAUTH_VNODE_NOIMMUTABLE
) ? 1 : 0;
4981 rights
= action
& ~(KAUTH_VNODE_ACCESS
| KAUTH_VNODE_NOIMMUTABLE
);
4983 if (rights
& KAUTH_VNODE_DELETE
) {
4986 panic("vnode_authorize: KAUTH_VNODE_DELETE test requires a directory");
4993 * Check for read-only filesystems.
4995 if ((rights
& KAUTH_VNODE_WRITE_RIGHTS
) &&
4996 (vp
->v_mount
->mnt_flag
& MNT_RDONLY
) &&
4997 ((vp
->v_type
== VREG
) || (vp
->v_type
== VDIR
) ||
4998 (vp
->v_type
== VLNK
) || (vp
->v_type
== VCPLX
) ||
4999 (rights
& KAUTH_VNODE_DELETE
) || (rights
& KAUTH_VNODE_DELETE_CHILD
))) {
5005 * Check for noexec filesystems.
5007 if ((rights
& KAUTH_VNODE_EXECUTE
) && vnode_isreg(vp
) && (vp
->v_mount
->mnt_flag
& MNT_NOEXEC
)) {
5013 * Handle cases related to filesystems with non-local enforcement.
5014 * This call can return 0, in which case we will fall through to perform a
5015 * check based on VNOP_GETATTR data. Otherwise it returns 1 and sets
5016 * an appropriate result, at which point we can return immediately.
5018 if (vfs_authopaque(vp
->v_mount
) && vnode_authorize_opaque(vp
, &result
, action
, ctx
))
5022 * Get vnode attributes and extended security information for the vnode
5023 * and directory if required.
5025 VATTR_WANTED(&va
, va_mode
);
5026 VATTR_WANTED(&va
, va_uid
);
5027 VATTR_WANTED(&va
, va_gid
);
5028 VATTR_WANTED(&va
, va_flags
);
5029 VATTR_WANTED(&va
, va_acl
);
5030 if ((result
= vnode_getattr(vp
, &va
, ctx
)) != 0) {
5031 KAUTH_DEBUG("%p ERROR - failed to get vnode attributes - %d", vp
, result
);
5035 VATTR_WANTED(&dva
, va_mode
);
5036 VATTR_WANTED(&dva
, va_uid
);
5037 VATTR_WANTED(&dva
, va_gid
);
5038 VATTR_WANTED(&dva
, va_flags
);
5039 VATTR_WANTED(&dva
, va_acl
);
5040 if ((result
= vnode_getattr(dvp
, &dva
, ctx
)) != 0) {
5041 KAUTH_DEBUG("%p ERROR - failed to get directory vnode attributes - %d", vp
, result
);
5047 * If the vnode is an extended attribute data vnode (eg. a resource fork), *_DATA becomes
5050 if (S_ISXATTR(va
.va_mode
)) {
5051 if (rights
& KAUTH_VNODE_READ_DATA
) {
5052 rights
&= ~KAUTH_VNODE_READ_DATA
;
5053 rights
|= KAUTH_VNODE_READ_EXTATTRIBUTES
;
5055 if (rights
& KAUTH_VNODE_WRITE_DATA
) {
5056 rights
&= ~KAUTH_VNODE_WRITE_DATA
;
5057 rights
|= KAUTH_VNODE_WRITE_EXTATTRIBUTES
;
5062 * Check for immutability.
5064 * In the deletion case, parent directory immutability vetoes specific
5067 if ((result
= vnode_authorize_checkimmutable(vp
, &va
, rights
, noimmutable
)) != 0)
5069 if ((rights
& KAUTH_VNODE_DELETE
) &&
5070 ((result
= vnode_authorize_checkimmutable(dvp
, &dva
, KAUTH_VNODE_DELETE_CHILD
, 0)) != 0))
5074 * Clear rights that have been authorized by reaching this point, bail if nothing left to
5077 rights
&= ~(KAUTH_VNODE_LINKTARGET
| KAUTH_VNODE_CHECKIMMUTABLE
);
5082 * If we're not the superuser, authorize based on file properties.
5084 if (!vfs_context_issuser(ctx
)) {
5085 /* process delete rights */
5086 if ((rights
& KAUTH_VNODE_DELETE
) &&
5087 ((result
= vnode_authorize_delete(vcp
)) != 0))
5090 /* process remaining rights */
5091 if ((rights
& ~KAUTH_VNODE_DELETE
) &&
5092 ((result
= vnode_authorize_simple(vcp
, rights
, rights
& KAUTH_VNODE_DELETE
)) != 0))
5097 * Execute is only granted to root if one of the x bits is set. This check only
5098 * makes sense if the posix mode bits are actually supported.
5100 if ((rights
& KAUTH_VNODE_EXECUTE
) &&
5101 (vp
->v_type
== VREG
) &&
5102 VATTR_IS_SUPPORTED(&va
, va_mode
) &&
5103 !(va
.va_mode
& (S_IXUSR
| S_IXGRP
| S_IXOTH
))) {
5105 KAUTH_DEBUG("%p DENIED - root execute requires at least one x bit in 0x%x", vp
, va
.va_mode
);
5109 KAUTH_DEBUG("%p ALLOWED - caller is superuser", vp
);
5113 if (VATTR_IS_SUPPORTED(&va
, va_acl
) && (va
.va_acl
!= NULL
))
5114 kauth_acl_free(va
.va_acl
);
5115 if (VATTR_IS_SUPPORTED(&dva
, va_acl
) && (dva
.va_acl
!= NULL
))
5116 kauth_acl_free(dva
.va_acl
);
5119 KAUTH_DEBUG("%p DENIED - auth denied", vp
);
5120 return(KAUTH_RESULT_DENY
);
5124 * Note that this implies that we will allow requests for no rights, as well as
5125 * for rights that we do not recognise. There should be none of these.
5127 KAUTH_DEBUG("%p ALLOWED - auth granted", vp
);
5128 return(KAUTH_RESULT_ALLOW
);
5132 * Check that the attribute information in vattr can be legally applied to
5133 * a new file by the context.
5136 vnode_authattr_new(vnode_t dvp
, struct vnode_attr
*vap
, int noauth
, vfs_context_t ctx
)
5139 int is_suser
, ismember
, defaulted_owner
, defaulted_group
, defaulted_mode
;
5145 defaulted_owner
= defaulted_group
= defaulted_mode
= 0;
5148 * Require that the filesystem support extended security to apply any.
5150 if (!vfs_extendedsecurity(dvp
->v_mount
) &&
5151 (VATTR_IS_ACTIVE(vap
, va_acl
) || VATTR_IS_ACTIVE(vap
, va_uuuid
) || VATTR_IS_ACTIVE(vap
, va_guuid
))) {
5157 * Default some fields.
5162 * If the filesystem is mounted IGNORE_OWNERSHIP and an explicit owner is set, that
5163 * owner takes ownership of all new files.
5165 if ((dmp
->mnt_flag
& MNT_IGNORE_OWNERSHIP
) && (dmp
->mnt_fsowner
!= KAUTH_UID_NONE
)) {
5166 VATTR_SET(vap
, va_uid
, dmp
->mnt_fsowner
);
5167 defaulted_owner
= 1;
5169 if (!VATTR_IS_ACTIVE(vap
, va_uid
)) {
5170 /* default owner is current user */
5171 VATTR_SET(vap
, va_uid
, kauth_cred_getuid(vfs_context_ucred(ctx
)));
5172 defaulted_owner
= 1;
5177 * If the filesystem is mounted IGNORE_OWNERSHIP and an explicit grouo is set, that
5178 * group takes ownership of all new files.
5180 if ((dmp
->mnt_flag
& MNT_IGNORE_OWNERSHIP
) && (dmp
->mnt_fsgroup
!= KAUTH_GID_NONE
)) {
5181 VATTR_SET(vap
, va_gid
, dmp
->mnt_fsgroup
);
5182 defaulted_group
= 1;
5184 if (!VATTR_IS_ACTIVE(vap
, va_gid
)) {
5185 /* default group comes from parent object, fallback to current user */
5186 struct vnode_attr dva
;
5188 VATTR_WANTED(&dva
, va_gid
);
5189 if ((error
= vnode_getattr(dvp
, &dva
, ctx
)) != 0)
5191 if (VATTR_IS_SUPPORTED(&dva
, va_gid
)) {
5192 VATTR_SET(vap
, va_gid
, dva
.va_gid
);
5194 VATTR_SET(vap
, va_gid
, kauth_cred_getgid(vfs_context_ucred(ctx
)));
5196 defaulted_group
= 1;
5200 if (!VATTR_IS_ACTIVE(vap
, va_flags
))
5201 VATTR_SET(vap
, va_flags
, 0);
5203 /* default mode is everything, masked with current umask */
5204 if (!VATTR_IS_ACTIVE(vap
, va_mode
)) {
5205 VATTR_SET(vap
, va_mode
, ACCESSPERMS
& ~vfs_context_proc(ctx
)->p_fd
->fd_cmask
);
5206 KAUTH_DEBUG("ATTR - defaulting new file mode to %o from umask %o", vap
->va_mode
, vfs_context_proc(ctx
)->p_fd
->fd_cmask
);
5209 /* set timestamps to now */
5210 if (!VATTR_IS_ACTIVE(vap
, va_create_time
)) {
5211 nanotime(&vap
->va_create_time
);
5212 VATTR_SET_ACTIVE(vap
, va_create_time
);
5216 * Check for attempts to set nonsensical fields.
5218 if (vap
->va_active
& ~VNODE_ATTR_NEWOBJ
) {
5220 KAUTH_DEBUG("ATTR - ERROR - attempt to set unsupported new-file attributes %llx",
5221 vap
->va_active
& ~VNODE_ATTR_NEWOBJ
);
5226 * Quickly check for the applicability of any enforcement here.
5227 * Tests below maintain the integrity of the local security model.
5229 if (vfs_authopaque(vnode_mount(dvp
)))
5233 * We need to know if the caller is the superuser, or if the work is
5234 * otherwise already authorised.
5236 cred
= vfs_context_ucred(ctx
);
5238 /* doing work for the kernel */
5241 is_suser
= vfs_context_issuser(ctx
);
5245 if (VATTR_IS_ACTIVE(vap
, va_flags
)) {
5247 if ((vap
->va_flags
& (UF_SETTABLE
| SF_SETTABLE
)) != vap
->va_flags
) {
5249 KAUTH_DEBUG(" DENIED - superuser attempt to set illegal flag(s)");
5253 if ((vap
->va_flags
& UF_SETTABLE
) != vap
->va_flags
) {
5255 KAUTH_DEBUG(" DENIED - user attempt to set illegal flag(s)");
5261 /* if not superuser, validate legality of new-item attributes */
5263 if (!defaulted_mode
&& VATTR_IS_ACTIVE(vap
, va_mode
)) {
5265 if (vap
->va_mode
& S_ISGID
) {
5266 if ((error
= kauth_cred_ismember_gid(cred
, vap
->va_gid
, &ismember
)) != 0) {
5267 KAUTH_DEBUG("ATTR - ERROR: got %d checking for membership in %d", error
, vap
->va_gid
);
5271 KAUTH_DEBUG(" DENIED - can't set SGID bit, not a member of %d", vap
->va_gid
);
5278 if ((vap
->va_mode
& S_ISUID
) && (vap
->va_uid
!= kauth_cred_getuid(cred
))) {
5279 KAUTH_DEBUG("ATTR - ERROR: illegal attempt to set the setuid bit");
5284 if (!defaulted_owner
&& (vap
->va_uid
!= kauth_cred_getuid(cred
))) {
5285 KAUTH_DEBUG(" DENIED - cannot create new item owned by %d", vap
->va_uid
);
5289 if (!defaulted_group
) {
5290 if ((error
= kauth_cred_ismember_gid(cred
, vap
->va_gid
, &ismember
)) != 0) {
5291 KAUTH_DEBUG(" ERROR - got %d checking for membership in %d", error
, vap
->va_gid
);
5295 KAUTH_DEBUG(" DENIED - cannot create new item with group %d - not a member", vap
->va_gid
);
5301 /* initialising owner/group UUID */
5302 if (VATTR_IS_ACTIVE(vap
, va_uuuid
)) {
5303 if ((error
= kauth_cred_getguid(cred
, &changer
)) != 0) {
5304 KAUTH_DEBUG(" ERROR - got %d trying to get caller UUID", error
);
5305 /* XXX ENOENT here - no GUID - should perhaps become EPERM */
5308 if (!kauth_guid_equal(&vap
->va_uuuid
, &changer
)) {
5309 KAUTH_DEBUG(" ERROR - cannot create item with supplied owner UUID - not us");
5314 if (VATTR_IS_ACTIVE(vap
, va_guuid
)) {
5315 if ((error
= kauth_cred_ismember_guid(cred
, &vap
->va_guuid
, &ismember
)) != 0) {
5316 KAUTH_DEBUG(" ERROR - got %d trying to check group membership", error
);
5320 KAUTH_DEBUG(" ERROR - cannot create item with supplied group UUID - not a member");
5331 * Check that the attribute information in vap can be legally written by the context.
5333 * Call this when you're not sure about the vnode_attr; either its contents have come
5334 * from an unknown source, or when they are variable.
5336 * Returns errno, or zero and sets *actionp to the KAUTH_VNODE_* actions that
5337 * must be authorized to be permitted to write the vattr.
5340 vnode_authattr(vnode_t vp
, struct vnode_attr
*vap
, kauth_action_t
*actionp
, vfs_context_t ctx
)
5342 struct vnode_attr ova
;
5343 kauth_action_t required_action
;
5344 int error
, is_suser
, ismember
, chowner
, chgroup
;
5353 required_action
= 0;
5357 * Quickly check for enforcement applicability.
5359 if (vfs_authopaque(vnode_mount(vp
)))
5363 * Check for attempts to set nonsensical fields.
5365 if (vap
->va_active
& VNODE_ATTR_RDONLY
) {
5366 KAUTH_DEBUG("ATTR - ERROR: attempt to set readonly attribute(s)");
5372 * We need to know if the caller is the superuser.
5374 cred
= vfs_context_ucred(ctx
);
5375 is_suser
= kauth_cred_issuser(cred
);
5378 * If any of the following are changing, we need information from the old file:
5385 if (VATTR_IS_ACTIVE(vap
, va_uid
) ||
5386 VATTR_IS_ACTIVE(vap
, va_gid
) ||
5387 VATTR_IS_ACTIVE(vap
, va_mode
) ||
5388 VATTR_IS_ACTIVE(vap
, va_uuuid
) ||
5389 VATTR_IS_ACTIVE(vap
, va_guuid
)) {
5390 VATTR_WANTED(&ova
, va_mode
);
5391 VATTR_WANTED(&ova
, va_uid
);
5392 VATTR_WANTED(&ova
, va_gid
);
5393 VATTR_WANTED(&ova
, va_uuuid
);
5394 VATTR_WANTED(&ova
, va_guuid
);
5395 KAUTH_DEBUG("ATTR - security information changing, fetching existing attributes");
5399 * If timestamps are being changed, we need to know who the file is owned
5402 if (VATTR_IS_ACTIVE(vap
, va_create_time
) ||
5403 VATTR_IS_ACTIVE(vap
, va_change_time
) ||
5404 VATTR_IS_ACTIVE(vap
, va_modify_time
) ||
5405 VATTR_IS_ACTIVE(vap
, va_access_time
) ||
5406 VATTR_IS_ACTIVE(vap
, va_backup_time
)) {
5408 VATTR_WANTED(&ova
, va_uid
);
5409 #if 0 /* enable this when we support UUIDs as official owners */
5410 VATTR_WANTED(&ova
, va_uuuid
);
5412 KAUTH_DEBUG("ATTR - timestamps changing, fetching uid and GUID");
5416 * If flags are being changed, we need the old flags.
5418 if (VATTR_IS_ACTIVE(vap
, va_flags
)) {
5419 KAUTH_DEBUG("ATTR - flags changing, fetching old flags");
5420 VATTR_WANTED(&ova
, va_flags
);
5424 * If the size is being set, make sure it's not a directory.
5426 if (VATTR_IS_ACTIVE(vap
, va_data_size
)) {
5427 /* size is meaningless on a directory, don't permit this */
5428 if (vnode_isdir(vp
)) {
5429 KAUTH_DEBUG("ATTR - ERROR: size change requested on a directory");
5438 KAUTH_DEBUG("ATTR - fetching old attributes %016llx", ova
.va_active
);
5439 if ((error
= vnode_getattr(vp
, &ova
, ctx
)) != 0) {
5440 KAUTH_DEBUG(" ERROR - got %d trying to get attributes", error
);
5445 * Size changes require write access to the file data.
5447 if (VATTR_IS_ACTIVE(vap
, va_data_size
)) {
5448 /* if we can't get the size, or it's different, we need write access */
5449 KAUTH_DEBUG("ATTR - size change, requiring WRITE_DATA");
5450 required_action
|= KAUTH_VNODE_WRITE_DATA
;
5454 * Changing timestamps?
5456 * Note that we are only called to authorize user-requested time changes;
5457 * side-effect time changes are not authorized. Authorisation is only
5458 * required for existing files.
5460 * Non-owners are not permitted to change the time on an existing
5461 * file to anything other than the current time.
5463 if (VATTR_IS_ACTIVE(vap
, va_create_time
) ||
5464 VATTR_IS_ACTIVE(vap
, va_change_time
) ||
5465 VATTR_IS_ACTIVE(vap
, va_modify_time
) ||
5466 VATTR_IS_ACTIVE(vap
, va_access_time
) ||
5467 VATTR_IS_ACTIVE(vap
, va_backup_time
)) {
5469 * The owner and root may set any timestamps they like,
5470 * provided that the file is not immutable. The owner still needs
5471 * WRITE_ATTRIBUTES (implied by ownership but still deniable).
5473 if (is_suser
|| vauth_node_owner(&ova
, cred
)) {
5474 KAUTH_DEBUG("ATTR - root or owner changing timestamps");
5475 required_action
|= KAUTH_VNODE_CHECKIMMUTABLE
| KAUTH_VNODE_WRITE_ATTRIBUTES
;
5477 /* just setting the current time? */
5478 if (vap
->va_vaflags
& VA_UTIMES_NULL
) {
5479 KAUTH_DEBUG("ATTR - non-root/owner changing timestamps, requiring WRITE_ATTRIBUTES");
5480 required_action
|= KAUTH_VNODE_WRITE_ATTRIBUTES
;
5482 KAUTH_DEBUG("ATTR - ERROR: illegal timestamp modification attempted");
5490 * Changing file mode?
5492 if (VATTR_IS_ACTIVE(vap
, va_mode
) && VATTR_IS_SUPPORTED(&ova
, va_mode
) && (ova
.va_mode
!= vap
->va_mode
)) {
5493 KAUTH_DEBUG("ATTR - mode change from %06o to %06o", ova
.va_mode
, vap
->va_mode
);
5496 * Mode changes always have the same basic auth requirements.
5499 KAUTH_DEBUG("ATTR - superuser mode change, requiring immutability check");
5500 required_action
|= KAUTH_VNODE_CHECKIMMUTABLE
;
5502 /* need WRITE_SECURITY */
5503 KAUTH_DEBUG("ATTR - non-superuser mode change, requiring WRITE_SECURITY");
5504 required_action
|= KAUTH_VNODE_WRITE_SECURITY
;
5508 * Can't set the setgid bit if you're not in the group and not root. Have to have
5509 * existing group information in the case we're not setting it right now.
5511 if (vap
->va_mode
& S_ISGID
) {
5512 required_action
|= KAUTH_VNODE_CHECKIMMUTABLE
; /* always required */
5514 if (VATTR_IS_ACTIVE(vap
, va_gid
)) {
5515 group
= vap
->va_gid
;
5516 } else if (VATTR_IS_SUPPORTED(&ova
, va_gid
)) {
5519 KAUTH_DEBUG("ATTR - ERROR: setgid but no gid available");
5524 * This might be too restrictive; WRITE_SECURITY might be implied by
5525 * membership in this case, rather than being an additional requirement.
5527 if ((error
= kauth_cred_ismember_gid(cred
, group
, &ismember
)) != 0) {
5528 KAUTH_DEBUG("ATTR - ERROR: got %d checking for membership in %d", error
, vap
->va_gid
);
5532 KAUTH_DEBUG(" DENIED - can't set SGID bit, not a member of %d", group
);
5540 * Can't set the setuid bit unless you're root or the file's owner.
5542 if (vap
->va_mode
& S_ISUID
) {
5543 required_action
|= KAUTH_VNODE_CHECKIMMUTABLE
; /* always required */
5545 if (VATTR_IS_ACTIVE(vap
, va_uid
)) {
5546 owner
= vap
->va_uid
;
5547 } else if (VATTR_IS_SUPPORTED(&ova
, va_uid
)) {
5550 KAUTH_DEBUG("ATTR - ERROR: setuid but no uid available");
5554 if (owner
!= kauth_cred_getuid(cred
)) {
5556 * We could allow this if WRITE_SECURITY is permitted, perhaps.
5558 KAUTH_DEBUG("ATTR - ERROR: illegal attempt to set the setuid bit");
5567 * Validate/mask flags changes. This checks that only the flags in
5568 * the UF_SETTABLE mask are being set, and preserves the flags in
5569 * the SF_SETTABLE case.
5571 * Since flags changes may be made in conjunction with other changes,
5572 * we will ask the auth code to ignore immutability in the case that
5573 * the SF_* flags are not set and we are only manipulating the file flags.
5576 if (VATTR_IS_ACTIVE(vap
, va_flags
)) {
5577 /* compute changing flags bits */
5578 if (VATTR_IS_SUPPORTED(&ova
, va_flags
)) {
5579 fdelta
= vap
->va_flags
^ ova
.va_flags
;
5581 fdelta
= vap
->va_flags
;
5585 KAUTH_DEBUG("ATTR - flags changing, requiring WRITE_SECURITY");
5586 required_action
|= KAUTH_VNODE_WRITE_SECURITY
;
5588 /* check that changing bits are legal */
5591 * The immutability check will prevent us from clearing the SF_*
5592 * flags unless the system securelevel permits it, so just check
5593 * for legal flags here.
5595 if (fdelta
& ~(UF_SETTABLE
| SF_SETTABLE
)) {
5597 KAUTH_DEBUG(" DENIED - superuser attempt to set illegal flag(s)");
5601 if (fdelta
& ~UF_SETTABLE
) {
5603 KAUTH_DEBUG(" DENIED - user attempt to set illegal flag(s)");
5608 * If the caller has the ability to manipulate file flags,
5609 * security is not reduced by ignoring them for this operation.
5611 * A more complete test here would consider the 'after' states of the flags
5612 * to determine whether it would permit the operation, but this becomes
5615 * Ignoring immutability is conditional on securelevel; this does not bypass
5616 * the SF_* flags if securelevel > 0.
5618 required_action
|= KAUTH_VNODE_NOIMMUTABLE
;
5623 * Validate ownership information.
5630 * Note that if the filesystem didn't give us a UID, we expect that it doesn't
5631 * support them in general, and will ignore it if/when we try to set it.
5632 * We might want to clear the uid out of vap completely here.
5634 if (VATTR_IS_ACTIVE(vap
, va_uid
) && VATTR_IS_SUPPORTED(&ova
, va_uid
) && (vap
->va_uid
!= ova
.va_uid
)) {
5635 if (!is_suser
&& (kauth_cred_getuid(cred
) != vap
->va_uid
)) {
5636 KAUTH_DEBUG(" DENIED - non-superuser cannot change ownershipt to a third party");
5645 * Note that if the filesystem didn't give us a GID, we expect that it doesn't
5646 * support them in general, and will ignore it if/when we try to set it.
5647 * We might want to clear the gid out of vap completely here.
5649 if (VATTR_IS_ACTIVE(vap
, va_gid
) && VATTR_IS_SUPPORTED(&ova
, va_gid
) && (vap
->va_gid
!= ova
.va_gid
)) {
5651 if ((error
= kauth_cred_ismember_gid(cred
, vap
->va_gid
, &ismember
)) != 0) {
5652 KAUTH_DEBUG(" ERROR - got %d checking for membership in %d", error
, vap
->va_gid
);
5656 KAUTH_DEBUG(" DENIED - group change from %d to %d but not a member of target group",
5657 ova
.va_gid
, vap
->va_gid
);
5666 * Owner UUID being set or changed.
5668 if (VATTR_IS_ACTIVE(vap
, va_uuuid
)) {
5669 /* if the owner UUID is not actually changing ... */
5670 if (VATTR_IS_SUPPORTED(&ova
, va_uuuid
) && kauth_guid_equal(&vap
->va_uuuid
, &ova
.va_uuuid
))
5671 goto no_uuuid_change
;
5674 * The owner UUID cannot be set by a non-superuser to anything other than
5678 if ((error
= kauth_cred_getguid(cred
, &changer
)) != 0) {
5679 KAUTH_DEBUG(" ERROR - got %d trying to get caller UUID", error
);
5680 /* XXX ENOENT here - no UUID - should perhaps become EPERM */
5683 if (!kauth_guid_equal(&vap
->va_uuuid
, &changer
)) {
5684 KAUTH_DEBUG(" ERROR - cannot set supplied owner UUID - not us");
5693 * Group UUID being set or changed.
5695 if (VATTR_IS_ACTIVE(vap
, va_guuid
)) {
5696 /* if the group UUID is not actually changing ... */
5697 if (VATTR_IS_SUPPORTED(&ova
, va_guuid
) && kauth_guid_equal(&vap
->va_guuid
, &ova
.va_guuid
))
5698 goto no_guuid_change
;
5701 * The group UUID cannot be set by a non-superuser to anything other than
5702 * one of which they are a member.
5705 if ((error
= kauth_cred_ismember_guid(cred
, &vap
->va_guuid
, &ismember
)) != 0) {
5706 KAUTH_DEBUG(" ERROR - got %d trying to check group membership", error
);
5710 KAUTH_DEBUG(" ERROR - cannot create item with supplied group UUID - not a member");
5720 * Compute authorisation for group/ownership changes.
5722 if (chowner
|| chgroup
) {
5724 KAUTH_DEBUG("ATTR - superuser changing file owner/group, requiring immutability check");
5725 required_action
|= KAUTH_VNODE_CHECKIMMUTABLE
;
5728 KAUTH_DEBUG("ATTR - ownership change, requiring TAKE_OWNERSHIP");
5729 required_action
|= KAUTH_VNODE_TAKE_OWNERSHIP
;
5731 if (chgroup
&& !chowner
) {
5732 KAUTH_DEBUG("ATTR - group change, requiring WRITE_SECURITY");
5733 required_action
|= KAUTH_VNODE_WRITE_SECURITY
;
5736 /* clear set-uid and set-gid bits as required by Posix */
5737 if (VATTR_IS_ACTIVE(vap
, va_mode
)) {
5738 newmode
= vap
->va_mode
;
5739 } else if (VATTR_IS_SUPPORTED(&ova
, va_mode
)) {
5740 newmode
= ova
.va_mode
;
5742 KAUTH_DEBUG("CHOWN - trying to change owner but cannot get mode from filesystem to mask setugid bits");
5745 if (newmode
& (S_ISUID
| S_ISGID
)) {
5746 VATTR_SET(vap
, va_mode
, newmode
& ~(S_ISUID
| S_ISGID
));
5747 KAUTH_DEBUG("CHOWN - masking setugid bits from mode %o to %o", newmode
, vap
->va_mode
);
5753 * Authorise changes in the ACL.
5755 if (VATTR_IS_ACTIVE(vap
, va_acl
)) {
5757 /* no existing ACL */
5758 if (!VATTR_IS_ACTIVE(&ova
, va_acl
) || (ova
.va_acl
== NULL
)) {
5761 if (vap
->va_acl
!= NULL
) {
5762 required_action
|= KAUTH_VNODE_WRITE_SECURITY
;
5763 KAUTH_DEBUG("CHMOD - adding ACL");
5766 /* removing an existing ACL */
5767 } else if (vap
->va_acl
== NULL
) {
5768 required_action
|= KAUTH_VNODE_WRITE_SECURITY
;
5769 KAUTH_DEBUG("CHMOD - removing ACL");
5771 /* updating an existing ACL */
5773 if (vap
->va_acl
->acl_entrycount
!= ova
.va_acl
->acl_entrycount
) {
5774 /* entry count changed, must be different */
5775 required_action
|= KAUTH_VNODE_WRITE_SECURITY
;
5776 KAUTH_DEBUG("CHMOD - adding/removing ACL entries");
5777 } else if (vap
->va_acl
->acl_entrycount
> 0) {
5778 /* both ACLs have the same ACE count, said count is 1 or more, bitwise compare ACLs */
5779 if (!memcmp(&vap
->va_acl
->acl_ace
[0], &ova
.va_acl
->acl_ace
[0],
5780 sizeof(struct kauth_ace
) * vap
->va_acl
->acl_entrycount
)) {
5781 required_action
|= KAUTH_VNODE_WRITE_SECURITY
;
5782 KAUTH_DEBUG("CHMOD - changing ACL entries");
5789 * Other attributes that require authorisation.
5791 if (VATTR_IS_ACTIVE(vap
, va_encoding
))
5792 required_action
|= KAUTH_VNODE_WRITE_ATTRIBUTES
;
5795 if (VATTR_IS_SUPPORTED(&ova
, va_acl
) && (ova
.va_acl
!= NULL
))
5796 kauth_acl_free(ova
.va_acl
);
5798 *actionp
= required_action
;
5804 vfs_setlocklocal(mount_t mp
)
5809 mp
->mnt_kern_flag
|= MNTK_LOCK_LOCAL
;
5812 * We do not expect anyone to be using any vnodes at the
5813 * time this routine is called. So no need for vnode locking
5815 TAILQ_FOREACH(vp
, &mp
->mnt_vnodelist
, v_mntvnodes
) {
5816 vp
->v_flag
|= VLOCKLOCAL
;
5818 TAILQ_FOREACH(vp
, &mp
->mnt_workerqueue
, v_mntvnodes
) {
5819 vp
->v_flag
|= VLOCKLOCAL
;
5821 TAILQ_FOREACH(vp
, &mp
->mnt_newvnodes
, v_mntvnodes
) {
5822 vp
->v_flag
|= VLOCKLOCAL
;
5830 record_vp(vnode_t vp
, int count
) {
5834 if ((vp
->v_flag
& VSYSTEM
))
5837 ut
= get_bsdthread_info(current_thread());
5838 ut
->uu_iocount
+= count
;
5840 if (ut
->uu_vpindex
< 32) {
5841 for (i
= 0; i
< ut
->uu_vpindex
; i
++) {
5842 if (ut
->uu_vps
[i
] == vp
)
5845 ut
->uu_vps
[ut
->uu_vpindex
] = vp
;