2 * Copyright (c) 2000-2005 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
20 * @APPLE_LICENSE_HEADER_END@
22 /* Copyright (c) 1995 NeXT Computer, Inc. All Rights Reserved */
24 * Copyright (c) 1989, 1993
25 * The Regents of the University of California. All rights reserved.
26 * (c) UNIX System Laboratories, Inc.
27 * All or some portions of this file are derived from material licensed
28 * to the University of California by American Telephone and Telegraph
29 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
30 * the permission of UNIX System Laboratories, Inc.
32 * Redistribution and use in source and binary forms, with or without
33 * modification, are permitted provided that the following conditions
35 * 1. Redistributions of source code must retain the above copyright
36 * notice, this list of conditions and the following disclaimer.
37 * 2. Redistributions in binary form must reproduce the above copyright
38 * notice, this list of conditions and the following disclaimer in the
39 * documentation and/or other materials provided with the distribution.
40 * 3. All advertising materials mentioning features or use of this software
41 * must display the following acknowledgement:
42 * This product includes software developed by the University of
43 * California, Berkeley and its contributors.
44 * 4. Neither the name of the University nor the names of its contributors
45 * may be used to endorse or promote products derived from this software
46 * without specific prior written permission.
48 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
49 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
50 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
51 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
52 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
53 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
54 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
55 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
56 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
57 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
60 * @(#)vfs_subr.c 8.31 (Berkeley) 5/26/95
64 * External virtual filesystem routines
70 #include <sys/param.h>
71 #include <sys/systm.h>
72 #include <sys/proc_internal.h>
73 #include <sys/kauth.h>
74 #include <sys/mount_internal.h>
77 #include <sys/vnode_internal.h>
79 #include <sys/namei.h>
80 #include <sys/ucred.h>
81 #include <sys/buf_internal.h>
82 #include <sys/errno.h>
83 #include <sys/malloc.h>
84 #include <sys/domain.h>
86 #include <sys/syslog.h>
87 #include <sys/ubc_internal.h>
89 #include <sys/sysctl.h>
90 #include <sys/filedesc.h>
91 #include <sys/event.h>
92 #include <sys/kdebug.h>
93 #include <sys/kauth.h>
95 #include <miscfs/fifofs/fifo.h>
98 #include <machine/spl.h>
101 #include <kern/assert.h>
103 #include <miscfs/specfs/specdev.h>
105 #include <mach/mach_types.h>
106 #include <mach/memory_object_types.h>
108 extern lck_grp_t
*vnode_lck_grp
;
109 extern lck_attr_t
*vnode_lck_attr
;
112 extern lck_mtx_t
* mnt_list_mtx_lock
;
114 enum vtype iftovt_tab
[16] = {
115 VNON
, VFIFO
, VCHR
, VNON
, VDIR
, VNON
, VBLK
, VNON
,
116 VREG
, VNON
, VLNK
, VNON
, VSOCK
, VNON
, VNON
, VBAD
,
118 int vttoif_tab
[9] = {
119 0, S_IFREG
, S_IFDIR
, S_IFBLK
, S_IFCHR
, S_IFLNK
,
120 S_IFSOCK
, S_IFIFO
, S_IFMT
,
123 extern int ubc_isinuse_locked(vnode_t
, int, int);
124 extern kern_return_t
adjust_vm_object_cache(vm_size_t oval
, vm_size_t nval
);
126 static void vnode_list_add(vnode_t
);
127 static void vnode_list_remove(vnode_t
);
129 static errno_t
vnode_drain(vnode_t
);
130 static void vgone(vnode_t
);
131 static void vclean(vnode_t vp
, int flag
, proc_t p
);
132 static void vnode_reclaim_internal(vnode_t
, int, int);
134 static void vnode_dropiocount (vnode_t
, int);
135 static errno_t
vnode_getiocount(vnode_t vp
, int locked
, int vid
, int vflags
);
136 static int vget_internal(vnode_t
, int, int);
138 static vnode_t
checkalias(vnode_t vp
, dev_t nvp_rdev
);
139 static int vnode_reload(vnode_t
);
140 static int vnode_isinuse_locked(vnode_t
, int, int);
142 static void insmntque(vnode_t vp
, mount_t mp
);
143 mount_t
mount_list_lookupby_fsid(fsid_t
*, int, int);
144 static int mount_getvfscnt(void);
145 static int mount_fillfsids(fsid_t
*, int );
146 static void vnode_iterate_setup(mount_t
);
147 static int vnode_umount_preflight(mount_t
, vnode_t
, int);
148 static int vnode_iterate_prepare(mount_t
);
149 static int vnode_iterate_reloadq(mount_t
);
150 static void vnode_iterate_clear(mount_t
);
152 TAILQ_HEAD(freelst
, vnode
) vnode_free_list
; /* vnode free list */
153 TAILQ_HEAD(inactivelst
, vnode
) vnode_inactive_list
; /* vnode inactive list */
154 struct mntlist mountlist
; /* mounted filesystem list */
155 static int nummounts
= 0;
158 #define VLISTCHECK(fun, vp, list) \
159 if ((vp)->v_freelist.tqe_prev == (struct vnode **)0xdeadb) \
160 panic("%s: %s vnode not on %slist", (fun), (list), (list));
162 #define VINACTIVECHECK(fun, vp, expected) \
164 int __is_inactive = ISSET((vp)->v_flag, VUINACTIVE); \
165 if (__is_inactive ^ expected) \
166 panic("%s: %sinactive vnode, expected %s", (fun), \
167 __is_inactive? "" : "not ", \
168 expected? "inactive": "not inactive"); \
171 #define VLISTCHECK(fun, vp, list)
172 #define VINACTIVECHECK(fun, vp, expected)
173 #endif /* DIAGNOSTIC */
175 #define VLISTNONE(vp) \
177 (vp)->v_freelist.tqe_next = (struct vnode *)0; \
178 (vp)->v_freelist.tqe_prev = (struct vnode **)0xdeadb; \
181 #define VONLIST(vp) \
182 ((vp)->v_freelist.tqe_prev != (struct vnode **)0xdeadb)
184 /* remove a vnode from free vnode list */
185 #define VREMFREE(fun, vp) \
187 VLISTCHECK((fun), (vp), "free"); \
188 TAILQ_REMOVE(&vnode_free_list, (vp), v_freelist); \
193 /* remove a vnode from inactive vnode list */
194 #define VREMINACTIVE(fun, vp) \
196 VLISTCHECK((fun), (vp), "inactive"); \
197 VINACTIVECHECK((fun), (vp), VUINACTIVE); \
198 TAILQ_REMOVE(&vnode_inactive_list, (vp), v_freelist); \
199 CLR((vp)->v_flag, VUINACTIVE); \
205 * Have to declare first two locks as actual data even if !MACH_SLOCKS, since
206 * a pointers to them get passed around.
208 void * mntvnode_slock
;
210 void * spechash_slock
;
213 * vnodetarget is the amount of vnodes we expect to get back
214 * from the the inactive vnode list and VM object cache.
215 * As vnreclaim() is a mainly cpu bound operation for faster
216 * processers this number could be higher.
217 * Having this number too high introduces longer delays in
218 * the execution of new_vnode().
220 unsigned long vnodetarget
; /* target for vnreclaim() */
221 #define VNODE_FREE_TARGET 20 /* Default value for vnodetarget */
224 * We need quite a few vnodes on the free list to sustain the
225 * rapid stat() the compilation process does, and still benefit from the name
226 * cache. Having too few vnodes on the free list causes serious disk
227 * thrashing as we cycle through them.
229 #define VNODE_FREE_MIN 300 /* freelist should have at least these many */
232 * We need to get vnodes back from the VM object cache when a certain #
233 * of vnodes are reused from the freelist. This is essential for the
234 * caching to be effective in the namecache and the buffer cache [for the
237 #define VNODE_TOOMANY_REUSED (VNODE_FREE_MIN/4)
240 * If we have enough vnodes on the freelist we do not want to reclaim
241 * the vnodes from the VM object cache.
243 #define VNODE_FREE_ENOUGH (VNODE_FREE_MIN + (VNODE_FREE_MIN/2))
246 * Initialize the vnode management data structures.
248 __private_extern__
void
251 TAILQ_INIT(&vnode_free_list
);
252 TAILQ_INIT(&vnode_inactive_list
);
253 TAILQ_INIT(&mountlist
);
256 vnodetarget
= VNODE_FREE_TARGET
;
259 * Scale the vm_object_cache to accomodate the vnodes
262 (void) adjust_vm_object_cache(0, desiredvnodes
- VNODE_FREE_MIN
);
265 /* Reset the VM Object Cache with the values passed in */
266 __private_extern__ kern_return_t
267 reset_vmobjectcache(unsigned int val1
, unsigned int val2
)
269 vm_size_t oval
= val1
- VNODE_FREE_MIN
;
272 if(val2
< VNODE_FREE_MIN
)
275 nval
= val2
- VNODE_FREE_MIN
;
277 return(adjust_vm_object_cache(oval
, nval
));
281 /* the timeout is in 10 msecs */
283 vnode_waitforwrites(vnode_t vp
, int output_target
, int slpflag
, int slptimeout
, char *msg
) {
287 KERNEL_DEBUG(0x3010280 | DBG_FUNC_START
, (int)vp
, output_target
, vp
->v_numoutput
, 0, 0);
289 if (vp
->v_numoutput
> output_target
) {
295 while ((vp
->v_numoutput
> output_target
) && error
== 0) {
297 vp
->v_flag
|= VTHROTTLED
;
299 vp
->v_flag
|= VBWAIT
;
300 ts
.tv_sec
= (slptimeout
/100);
301 ts
.tv_nsec
= (slptimeout
% 1000) * 10 * NSEC_PER_USEC
* 1000 ;
302 error
= msleep((caddr_t
)&vp
->v_numoutput
, &vp
->v_lock
, (slpflag
| (PRIBIO
+ 1)), msg
, &ts
);
306 KERNEL_DEBUG(0x3010280 | DBG_FUNC_END
, (int)vp
, output_target
, vp
->v_numoutput
, error
, 0);
313 vnode_startwrite(vnode_t vp
) {
315 OSAddAtomic(1, &vp
->v_numoutput
);
320 vnode_writedone(vnode_t vp
)
325 OSAddAtomic(-1, &vp
->v_numoutput
);
329 if (vp
->v_numoutput
< 0)
330 panic("vnode_writedone: numoutput < 0");
332 if ((vp
->v_flag
& VTHROTTLED
) && (vp
->v_numoutput
< (VNODE_ASYNC_THROTTLE
/ 3))) {
333 vp
->v_flag
&= ~VTHROTTLED
;
336 if ((vp
->v_flag
& VBWAIT
) && (vp
->v_numoutput
== 0)) {
337 vp
->v_flag
&= ~VBWAIT
;
343 wakeup((caddr_t
)&vp
->v_numoutput
);
350 vnode_hasdirtyblks(vnode_t vp
)
352 struct cl_writebehind
*wbp
;
355 * Not taking the buf_mtxp as there is little
356 * point doing it. Even if the lock is taken the
357 * state can change right after that. If their
358 * needs to be a synchronization, it must be driven
361 if (vp
->v_dirtyblkhd
.lh_first
)
364 if (!UBCINFOEXISTS(vp
))
367 wbp
= vp
->v_ubcinfo
->cl_wbehind
;
369 if (wbp
&& (wbp
->cl_number
|| wbp
->cl_scmap
))
376 vnode_hascleanblks(vnode_t vp
)
379 * Not taking the buf_mtxp as there is little
380 * point doing it. Even if the lock is taken the
381 * state can change right after that. If their
382 * needs to be a synchronization, it must be driven
385 if (vp
->v_cleanblkhd
.lh_first
)
391 vnode_iterate_setup(mount_t mp
)
393 while (mp
->mnt_lflag
& MNT_LITER
) {
394 mp
->mnt_lflag
|= MNT_LITERWAIT
;
395 msleep((caddr_t
)mp
, &mp
->mnt_mlock
, PVFS
, "vnode_iterate_setup", 0);
398 mp
->mnt_lflag
|= MNT_LITER
;
403 vnode_umount_preflight(mount_t mp
, vnode_t skipvp
, int flags
)
407 TAILQ_FOREACH(vp
, &mp
->mnt_vnodelist
, v_mntvnodes
) {
408 if (vp
->v_type
== VDIR
)
412 if ((flags
& SKIPSYSTEM
) && ((vp
->v_flag
& VSYSTEM
) ||
413 (vp
->v_flag
& VNOFLUSH
)))
415 if ((flags
& SKIPSWAP
) && (vp
->v_flag
& VSWAP
))
417 if ((flags
& WRITECLOSE
) &&
418 (vp
->v_writecount
== 0 || vp
->v_type
!= VREG
))
420 /* Look for busy vnode */
421 if (((vp
->v_usecount
!= 0) &&
422 ((vp
->v_usecount
- vp
->v_kusecount
) != 0)))
430 * This routine prepares iteration by moving all the vnodes to worker queue
431 * called with mount lock held
434 vnode_iterate_prepare(mount_t mp
)
438 if (TAILQ_EMPTY(&mp
->mnt_vnodelist
)) {
443 vp
= TAILQ_FIRST(&mp
->mnt_vnodelist
);
444 vp
->v_mntvnodes
.tqe_prev
= &(mp
->mnt_workerqueue
.tqh_first
);
445 mp
->mnt_workerqueue
.tqh_first
= mp
->mnt_vnodelist
.tqh_first
;
446 mp
->mnt_workerqueue
.tqh_last
= mp
->mnt_vnodelist
.tqh_last
;
448 TAILQ_INIT(&mp
->mnt_vnodelist
);
449 if (mp
->mnt_newvnodes
.tqh_first
!= NULL
)
450 panic("vnode_iterate_prepare: newvnode when entering vnode");
451 TAILQ_INIT(&mp
->mnt_newvnodes
);
457 /* called with mount lock held */
459 vnode_iterate_reloadq(mount_t mp
)
463 /* add the remaining entries in workerq to the end of mount vnode list */
464 if (!TAILQ_EMPTY(&mp
->mnt_workerqueue
)) {
466 mvp
= TAILQ_LAST(&mp
->mnt_vnodelist
, vnodelst
);
468 /* Joining the workerque entities to mount vnode list */
470 mvp
->v_mntvnodes
.tqe_next
= mp
->mnt_workerqueue
.tqh_first
;
472 mp
->mnt_vnodelist
.tqh_first
= mp
->mnt_workerqueue
.tqh_first
;
473 mp
->mnt_workerqueue
.tqh_first
->v_mntvnodes
.tqe_prev
= mp
->mnt_vnodelist
.tqh_last
;
474 mp
->mnt_vnodelist
.tqh_last
= mp
->mnt_workerqueue
.tqh_last
;
475 TAILQ_INIT(&mp
->mnt_workerqueue
);
478 /* add the newvnodes to the head of mount vnode list */
479 if (!TAILQ_EMPTY(&mp
->mnt_newvnodes
)) {
481 nlvp
= TAILQ_LAST(&mp
->mnt_newvnodes
, vnodelst
);
483 mp
->mnt_newvnodes
.tqh_first
->v_mntvnodes
.tqe_prev
= &mp
->mnt_vnodelist
.tqh_first
;
484 nlvp
->v_mntvnodes
.tqe_next
= mp
->mnt_vnodelist
.tqh_first
;
485 if(mp
->mnt_vnodelist
.tqh_first
)
486 mp
->mnt_vnodelist
.tqh_first
->v_mntvnodes
.tqe_prev
= &nlvp
->v_mntvnodes
.tqe_next
;
488 mp
->mnt_vnodelist
.tqh_last
= mp
->mnt_newvnodes
.tqh_last
;
489 mp
->mnt_vnodelist
.tqh_first
= mp
->mnt_newvnodes
.tqh_first
;
490 TAILQ_INIT(&mp
->mnt_newvnodes
);
499 vnode_iterate_clear(mount_t mp
)
501 mp
->mnt_lflag
&= ~MNT_LITER
;
502 if (mp
->mnt_lflag
& MNT_LITERWAIT
) {
503 mp
->mnt_lflag
&= ~MNT_LITERWAIT
;
510 vnode_iterate(mp
, flags
, callout
, arg
)
513 int (*callout
)(struct vnode
*, void *);
522 vnode_iterate_setup(mp
);
524 /* it is returns 0 then there is nothing to do */
525 retval
= vnode_iterate_prepare(mp
);
528 vnode_iterate_clear(mp
);
533 /* iterate over all the vnodes */
534 while (!TAILQ_EMPTY(&mp
->mnt_workerqueue
)) {
535 vp
= TAILQ_FIRST(&mp
->mnt_workerqueue
);
536 TAILQ_REMOVE(&mp
->mnt_workerqueue
, vp
, v_mntvnodes
);
537 TAILQ_INSERT_TAIL(&mp
->mnt_vnodelist
, vp
, v_mntvnodes
);
539 if ((vp
->v_data
== NULL
) || (vp
->v_type
== VNON
) || (vp
->v_mount
!= mp
)) {
544 if ( vget_internal(vp
, vid
, (flags
| VNODE_NODEAD
| VNODE_WITHID
| VNODE_NOSUSPEND
))) {
548 if (flags
& VNODE_RELOAD
) {
550 * we're reloading the filesystem
551 * cast out any inactive vnodes...
553 if (vnode_reload(vp
)) {
554 /* vnode will be recycled on the refcount drop */
561 retval
= callout(vp
, arg
);
565 case VNODE_RETURNED_DONE
:
567 if (retval
== VNODE_RETURNED_DONE
) {
574 case VNODE_CLAIMED_DONE
:
586 (void)vnode_iterate_reloadq(mp
);
587 vnode_iterate_clear(mp
);
593 mount_lock_renames(mount_t mp
)
595 lck_mtx_lock(&mp
->mnt_renamelock
);
599 mount_unlock_renames(mount_t mp
)
601 lck_mtx_unlock(&mp
->mnt_renamelock
);
605 mount_lock(mount_t mp
)
607 lck_mtx_lock(&mp
->mnt_mlock
);
611 mount_unlock(mount_t mp
)
613 lck_mtx_unlock(&mp
->mnt_mlock
);
618 mount_ref(mount_t mp
, int locked
)
631 mount_drop(mount_t mp
, int locked
)
638 if (mp
->mnt_count
== 0 && (mp
->mnt_lflag
& MNT_LDRAIN
))
639 wakeup(&mp
->mnt_lflag
);
647 mount_iterref(mount_t mp
, int locked
)
653 if (mp
->mnt_iterref
< 0) {
664 mount_isdrained(mount_t mp
, int locked
)
670 if (mp
->mnt_iterref
< 0)
680 mount_iterdrop(mount_t mp
)
684 wakeup(&mp
->mnt_iterref
);
689 mount_iterdrain(mount_t mp
)
692 while (mp
->mnt_iterref
)
693 msleep((caddr_t
)&mp
->mnt_iterref
, mnt_list_mtx_lock
, PVFS
, "mount_iterdrain", 0 );
694 /* mount iterations drained */
695 mp
->mnt_iterref
= -1;
699 mount_iterreset(mount_t mp
)
702 if (mp
->mnt_iterref
== -1)
707 /* always called with mount lock held */
709 mount_refdrain(mount_t mp
)
711 if (mp
->mnt_lflag
& MNT_LDRAIN
)
712 panic("already in drain");
713 mp
->mnt_lflag
|= MNT_LDRAIN
;
715 while (mp
->mnt_count
)
716 msleep((caddr_t
)&mp
->mnt_lflag
, &mp
->mnt_mlock
, PVFS
, "mount_drain", 0 );
718 if (mp
->mnt_vnodelist
.tqh_first
!= NULL
)
719 panic("mount_refdrain: dangling vnode");
721 mp
->mnt_lflag
&= ~MNT_LDRAIN
;
728 * Mark a mount point as busy. Used to synchronize access and to delay
732 vfs_busy(mount_t mp
, int flags
)
736 if (mp
->mnt_lflag
& MNT_LDEAD
)
739 if (mp
->mnt_lflag
& MNT_LUNMOUNT
) {
740 if (flags
& LK_NOWAIT
)
745 if (mp
->mnt_lflag
& MNT_LDEAD
) {
749 if (mp
->mnt_lflag
& MNT_LUNMOUNT
) {
750 mp
->mnt_lflag
|= MNT_LWAIT
;
752 * Since all busy locks are shared except the exclusive
753 * lock granted when unmounting, the only place that a
754 * wakeup needs to be done is at the release of the
755 * exclusive lock at the end of dounmount.
757 msleep((caddr_t
)mp
, &mp
->mnt_mlock
, (PVFS
| PDROP
), "vfsbusy", 0 );
763 lck_rw_lock_shared(&mp
->mnt_rwlock
);
766 * until we are granted the rwlock, it's possible for the mount point to
767 * change state, so reevaluate before granting the vfs_busy
769 if (mp
->mnt_lflag
& (MNT_LDEAD
| MNT_LUNMOUNT
)) {
770 lck_rw_done(&mp
->mnt_rwlock
);
777 * Free a busy filesystem.
781 vfs_unbusy(mount_t mp
)
783 lck_rw_done(&mp
->mnt_rwlock
);
789 vfs_rootmountfailed(mount_t mp
) {
792 mp
->mnt_vtable
->vfc_refcount
--;
797 mount_lock_destroy(mp
);
799 FREE_ZONE(mp
, sizeof(struct mount
), M_MOUNT
);
803 * Lookup a filesystem type, and if found allocate and initialize
804 * a mount structure for it.
806 * Devname is usually updated by mount(8) after booting.
809 vfs_rootmountalloc_internal(struct vfstable
*vfsp
, const char *devname
)
813 mp
= _MALLOC_ZONE((u_long
)sizeof(struct mount
), M_MOUNT
, M_WAITOK
);
814 bzero((char *)mp
, (u_long
)sizeof(struct mount
));
816 /* Initialize the default IO constraints */
817 mp
->mnt_maxreadcnt
= mp
->mnt_maxwritecnt
= MAXPHYS
;
818 mp
->mnt_segreadcnt
= mp
->mnt_segwritecnt
= 32;
819 mp
->mnt_maxsegreadsize
= mp
->mnt_maxreadcnt
;
820 mp
->mnt_maxsegwritesize
= mp
->mnt_maxwritecnt
;
821 mp
->mnt_devblocksize
= DEV_BSIZE
;
824 (void)vfs_busy(mp
, LK_NOWAIT
);
826 TAILQ_INIT(&mp
->mnt_vnodelist
);
827 TAILQ_INIT(&mp
->mnt_workerqueue
);
828 TAILQ_INIT(&mp
->mnt_newvnodes
);
830 mp
->mnt_vtable
= vfsp
;
831 mp
->mnt_op
= vfsp
->vfc_vfsops
;
832 mp
->mnt_flag
= MNT_RDONLY
| MNT_ROOTFS
;
833 mp
->mnt_vnodecovered
= NULLVP
;
834 //mp->mnt_stat.f_type = vfsp->vfc_typenum;
835 mp
->mnt_flag
|= vfsp
->vfc_flags
& MNT_VISFLAGMASK
;
838 vfsp
->vfc_refcount
++;
841 strncpy(mp
->mnt_vfsstat
.f_fstypename
, vfsp
->vfc_name
, MFSTYPENAMELEN
);
842 mp
->mnt_vfsstat
.f_mntonname
[0] = '/';
843 (void) copystr((char *)devname
, mp
->mnt_vfsstat
.f_mntfromname
, MAXPATHLEN
- 1, 0);
849 vfs_rootmountalloc(const char *fstypename
, const char *devname
, mount_t
*mpp
)
851 struct vfstable
*vfsp
;
853 for (vfsp
= vfsconf
; vfsp
; vfsp
= vfsp
->vfc_next
)
854 if (!strcmp(vfsp
->vfc_name
, fstypename
))
859 *mpp
= vfs_rootmountalloc_internal(vfsp
, devname
);
869 * Find an appropriate filesystem to use for the root. If a filesystem
870 * has not been preselected, walk through the list of known filesystems
871 * trying those that have mountroot routines, and try them until one
872 * works or we have tried them all.
874 extern int (*mountroot
)(void);
879 struct vfstable
*vfsp
;
880 struct vfs_context context
;
884 if (mountroot
!= NULL
) {
886 * used for netboot which follows a different set of rules
888 error
= (*mountroot
)();
891 if ((error
= bdevvp(rootdev
, &rootvp
))) {
892 printf("vfs_mountroot: can't setup bdevvp\n");
895 context
.vc_proc
= current_proc();
896 context
.vc_ucred
= kauth_cred_get();
898 for (vfsp
= vfsconf
; vfsp
; vfsp
= vfsp
->vfc_next
) {
899 if (vfsp
->vfc_mountroot
== NULL
)
902 mp
= vfs_rootmountalloc_internal(vfsp
, "root_device");
903 mp
->mnt_devvp
= rootvp
;
905 if ((error
= (*vfsp
->vfc_mountroot
)(mp
, rootvp
, &context
)) == 0) {
906 mp
->mnt_devvp
->v_specflags
|= SI_MOUNTEDON
;
913 * cache the IO attributes for the underlying physical media...
914 * an error return indicates the underlying driver doesn't
915 * support all the queries necessary... however, reasonable
916 * defaults will have been set, so no reason to bail or care
918 vfs_init_io_attributes(rootvp
, mp
);
920 * get rid of iocount reference returned
921 * by bdevvp... it will have also taken
922 * a usecount reference which we want to keep
928 vfs_rootmountfailed(mp
);
931 printf("%s_mountroot failed: %d\n", vfsp
->vfc_name
, error
);
937 * Lookup a mount point by filesystem identifier.
939 extern mount_t
vfs_getvfs_locked(fsid_t
*);
945 return (mount_list_lookupby_fsid(fsid
, 0, 0));
949 vfs_getvfs_locked(fsid
)
952 return(mount_list_lookupby_fsid(fsid
, 1, 0));
956 vfs_getvfs_by_mntonname(u_char
*path
)
958 mount_t retmp
= (mount_t
)0;
962 TAILQ_FOREACH(mp
, &mountlist
, mnt_list
) {
963 if (!strcmp(mp
->mnt_vfsstat
.f_mntonname
, path
)) {
973 /* generation number for creation of new fsids */
974 u_short mntid_gen
= 0;
976 * Get a new unique fsid
989 /* generate a new fsid */
990 mtype
= mp
->mnt_vtable
->vfc_typenum
;
991 if (++mntid_gen
== 0)
993 tfsid
.val
[0] = makedev(nblkdev
+ mtype
, mntid_gen
);
994 tfsid
.val
[1] = mtype
;
996 TAILQ_FOREACH(nmp
, &mountlist
, mnt_list
) {
997 while (vfs_getvfs_locked(&tfsid
)) {
998 if (++mntid_gen
== 0)
1000 tfsid
.val
[0] = makedev(nblkdev
+ mtype
, mntid_gen
);
1003 mp
->mnt_vfsstat
.f_fsid
.val
[0] = tfsid
.val
[0];
1004 mp
->mnt_vfsstat
.f_fsid
.val
[1] = tfsid
.val
[1];
1005 mount_list_unlock();
1009 * Routines having to do with the management of the vnode table.
1011 extern int (**dead_vnodeop_p
)(void *);
1012 long numvnodes
, freevnodes
;
1013 long inactivevnodes
;
1017 * Move a vnode from one mount queue to another.
1020 insmntque(vnode_t vp
, mount_t mp
)
1024 * Delete from old mount point vnode list, if on one.
1026 if ( (lmp
= vp
->v_mount
) != NULL
) {
1027 if ((vp
->v_lflag
& VNAMED_MOUNT
) == 0)
1028 panic("insmntque: vp not in mount vnode list");
1029 vp
->v_lflag
&= ~VNAMED_MOUNT
;
1035 if (vp
->v_mntvnodes
.tqe_next
== NULL
) {
1036 if (TAILQ_LAST(&lmp
->mnt_vnodelist
, vnodelst
) == vp
)
1037 TAILQ_REMOVE(&lmp
->mnt_vnodelist
, vp
, v_mntvnodes
);
1038 else if (TAILQ_LAST(&lmp
->mnt_newvnodes
, vnodelst
) == vp
)
1039 TAILQ_REMOVE(&lmp
->mnt_newvnodes
, vp
, v_mntvnodes
);
1040 else if (TAILQ_LAST(&lmp
->mnt_workerqueue
, vnodelst
) == vp
)
1041 TAILQ_REMOVE(&lmp
->mnt_workerqueue
, vp
, v_mntvnodes
);
1043 vp
->v_mntvnodes
.tqe_next
->v_mntvnodes
.tqe_prev
= vp
->v_mntvnodes
.tqe_prev
;
1044 *vp
->v_mntvnodes
.tqe_prev
= vp
->v_mntvnodes
.tqe_next
;
1046 vp
->v_mntvnodes
.tqe_next
= 0;
1047 vp
->v_mntvnodes
.tqe_prev
= 0;
1053 * Insert into list of vnodes for the new mount point, if available.
1055 if ((vp
->v_mount
= mp
) != NULL
) {
1057 if ((vp
->v_mntvnodes
.tqe_next
!= 0) && (vp
->v_mntvnodes
.tqe_prev
!= 0))
1058 panic("vp already in mount list");
1059 if (mp
->mnt_lflag
& MNT_LITER
)
1060 TAILQ_INSERT_HEAD(&mp
->mnt_newvnodes
, vp
, v_mntvnodes
);
1062 TAILQ_INSERT_HEAD(&mp
->mnt_vnodelist
, vp
, v_mntvnodes
);
1063 if (vp
->v_lflag
& VNAMED_MOUNT
)
1064 panic("insmntque: vp already in mount vnode list");
1065 if ((vp
->v_freelist
.tqe_prev
!= (struct vnode
**)0xdeadb))
1066 panic("insmntque: vp on the free list\n");
1067 vp
->v_lflag
|= VNAMED_MOUNT
;
1075 * Create a vnode for a block device.
1076 * Used for root filesystem, argdev, and swap areas.
1077 * Also used for memory file system special devices.
1080 bdevvp(dev_t dev
, vnode_t
*vpp
)
1084 struct vnode_fsparam vfsp
;
1085 struct vfs_context context
;
1092 context
.vc_proc
= current_proc();
1093 context
.vc_ucred
= FSCRED
;
1095 vfsp
.vnfs_mp
= (struct mount
*)0;
1096 vfsp
.vnfs_vtype
= VBLK
;
1097 vfsp
.vnfs_str
= "bdevvp";
1099 vfsp
.vnfs_fsnode
= 0;
1101 vfsp
.vnfs_vops
= spec_vnodeop_p
;
1102 vfsp
.vnfs_rdev
= dev
;
1103 vfsp
.vnfs_filesize
= 0;
1105 vfsp
.vnfs_flags
= VNFS_NOCACHE
| VNFS_CANTCACHE
;
1107 vfsp
.vnfs_marksystem
= 0;
1108 vfsp
.vnfs_markroot
= 0;
1110 if ( (error
= vnode_create(VNCREATE_FLAVOR
, VCREATESIZE
, &vfsp
, &nvp
)) ) {
1114 if ( (error
= vnode_ref(nvp
)) ) {
1115 panic("bdevvp failed: vnode_ref");
1118 if ( (error
= VNOP_FSYNC(nvp
, MNT_WAIT
, &context
)) ) {
1119 panic("bdevvp failed: fsync");
1122 if ( (error
= buf_invalidateblks(nvp
, BUF_WRITE_DATA
, 0, 0)) ) {
1123 panic("bdevvp failed: invalidateblks");
1126 if ( (error
= VNOP_OPEN(nvp
, FREAD
, &context
)) ) {
1127 panic("bdevvp failed: open");
1136 * Check to see if the new vnode represents a special device
1137 * for which we already have a vnode (either because of
1138 * bdevvp() or because of a different vnode representing
1139 * the same block device). If such an alias exists, deallocate
1140 * the existing contents and return the aliased vnode. The
1141 * caller is responsible for filling it with its new contents.
1144 checkalias(nvp
, nvp_rdev
)
1145 register struct vnode
*nvp
;
1152 vpp
= &speclisth
[SPECHASH(nvp_rdev
)];
1156 for (vp
= *vpp
; vp
; vp
= vp
->v_specnext
) {
1157 if (nvp_rdev
== vp
->v_rdev
&& nvp
->v_type
== vp
->v_type
) {
1165 if (vnode_getwithvid(vp
,vid
)) {
1169 * Termination state is checked in vnode_getwithvid
1174 * Alias, but not in use, so flush it out.
1176 if ((vp
->v_iocount
== 1) && (vp
->v_usecount
== 0)) {
1177 vnode_reclaim_internal(vp
, 1, 0);
1183 if (vp
== NULL
|| vp
->v_tag
!= VT_NON
) {
1184 MALLOC_ZONE(nvp
->v_specinfo
, struct specinfo
*, sizeof(struct specinfo
),
1185 M_SPECINFO
, M_WAITOK
);
1186 bzero(nvp
->v_specinfo
, sizeof(struct specinfo
));
1187 nvp
->v_rdev
= nvp_rdev
;
1188 nvp
->v_specflags
= 0;
1189 nvp
->v_speclastr
= -1;
1192 nvp
->v_hashchain
= vpp
;
1193 nvp
->v_specnext
= *vpp
;
1198 nvp
->v_flag
|= VALIASED
;
1199 vp
->v_flag
|= VALIASED
;
1210 * Get a reference on a particular vnode and lock it if requested.
1211 * If the vnode was on the inactive list, remove it from the list.
1212 * If the vnode was on the free list, remove it from the list and
1213 * move it to inactive list as needed.
1214 * The vnode lock bit is set if the vnode is being eliminated in
1215 * vgone. The process is awakened when the transition is completed,
1216 * and an error returned to indicate that the vnode is no longer
1217 * usable (possibly having been changed to a new file system type).
1220 vget_internal(vnode_t vp
, int vid
, int vflags
)
1227 if (vflags
& VNODE_WITHID
)
1230 vpid
= vp
->v_id
; // save off the original v_id
1232 if ((vflags
& VNODE_WRITEABLE
) && (vp
->v_writecount
== 0))
1234 * vnode to be returned only if it has writers opened
1238 error
= vnode_getiocount(vp
, 1, vpid
, vflags
);
1246 vnode_ref(vnode_t vp
)
1249 return (vnode_ref_ext(vp
, 0));
1253 vnode_ref_ext(vnode_t vp
, int fmode
)
1260 * once all the current call sites have been fixed to insure they have
1261 * taken an iocount, we can toughen this assert up and insist that the
1262 * iocount is non-zero... a non-zero usecount doesn't insure correctness
1264 if (vp
->v_iocount
<= 0 && vp
->v_usecount
<= 0)
1265 panic("vnode_ref_ext: vp %x has no valid reference %d, %d", vp
, vp
->v_iocount
, vp
->v_usecount
);
1268 * if you are the owner of drain/termination, can acquire usecount
1270 if ((vp
->v_lflag
& (VL_DRAIN
| VL_TERMINATE
| VL_DEAD
))) {
1271 if (vp
->v_owner
!= current_thread()) {
1278 if (fmode
& FWRITE
) {
1279 if (++vp
->v_writecount
<= 0)
1280 panic("vnode_ref_ext: v_writecount");
1282 if (fmode
& O_EVTONLY
) {
1283 if (++vp
->v_kusecount
<= 0)
1284 panic("vnode_ref_ext: v_kusecount");
1294 * put the vnode on appropriate free list.
1295 * called with vnode LOCKED
1298 vnode_list_add(vnode_t vp
)
1302 * if it is already on a list or non zero references return
1304 if (VONLIST(vp
) || (vp
->v_usecount
!= 0) || (vp
->v_iocount
!= 0))
1309 * insert at tail of LRU list or at head if VAGE or VL_DEAD is set
1311 if ((vp
->v_flag
& VAGE
) || (vp
->v_lflag
& VL_DEAD
)) {
1312 TAILQ_INSERT_HEAD(&vnode_free_list
, vp
, v_freelist
);
1313 vp
->v_flag
&= ~VAGE
;
1315 TAILQ_INSERT_TAIL(&vnode_free_list
, vp
, v_freelist
);
1319 vnode_list_unlock();
1323 * remove the vnode from appropriate free list.
1326 vnode_list_remove(vnode_t vp
)
1329 * we want to avoid taking the list lock
1330 * in the case where we're not on the free
1331 * list... this will be true for most
1332 * directories and any currently in use files
1334 * we're guaranteed that we can't go from
1335 * the not-on-list state to the on-list
1336 * state since we hold the vnode lock...
1337 * all calls to vnode_list_add are done
1338 * under the vnode lock... so we can
1339 * check for that condition (the prevelant one)
1340 * without taking the list lock
1345 * however, we're not guaranteed that
1346 * we won't go from the on-list state
1347 * to the non-on-list state until we
1348 * hold the vnode_list_lock... this
1349 * is due to new_vnode removing vnodes
1350 * from the free list uder the list_lock
1351 * w/o the vnode lock... so we need to
1352 * check again whether we're currently
1356 VREMFREE("vnode_list_remove", vp
);
1359 vnode_list_unlock();
1365 vnode_rele(vnode_t vp
)
1367 vnode_rele_internal(vp
, 0, 0, 0);
1372 vnode_rele_ext(vnode_t vp
, int fmode
, int dont_reenter
)
1374 vnode_rele_internal(vp
, fmode
, dont_reenter
, 0);
1379 vnode_rele_internal(vnode_t vp
, int fmode
, int dont_reenter
, int locked
)
1381 struct vfs_context context
;
1386 if (--vp
->v_usecount
< 0)
1387 panic("vnode_rele_ext: vp %x usecount -ve : %d", vp
, vp
->v_usecount
);
1389 if (fmode
& FWRITE
) {
1390 if (--vp
->v_writecount
< 0)
1391 panic("vnode_rele_ext: vp %x writecount -ve : %d", vp
, vp
->v_writecount
);
1393 if (fmode
& O_EVTONLY
) {
1394 if (--vp
->v_kusecount
< 0)
1395 panic("vnode_rele_ext: vp %x kusecount -ve : %d", vp
, vp
->v_kusecount
);
1397 if ((vp
->v_iocount
> 0) || (vp
->v_usecount
> 0)) {
1399 * vnode is still busy... if we're the last
1400 * usecount, mark for a future call to VNOP_INACTIVE
1401 * when the iocount finally drops to 0
1403 if (vp
->v_usecount
== 0) {
1404 vp
->v_lflag
|= VL_NEEDINACTIVE
;
1405 vp
->v_flag
&= ~(VNOCACHE_DATA
| VRAOFF
);
1411 vp
->v_flag
&= ~(VNOCACHE_DATA
| VRAOFF
);
1413 if ( (vp
->v_lflag
& (VL_TERMINATE
| VL_DEAD
)) || dont_reenter
) {
1415 * vnode is being cleaned, or
1416 * we've requested that we don't reenter
1417 * the filesystem on this release... in
1418 * this case, we'll mark the vnode aged
1419 * if it's been marked for termination
1422 if ( !(vp
->v_lflag
& (VL_TERMINATE
| VL_DEAD
| VL_MARKTERM
)) )
1423 vp
->v_lflag
|= VL_NEEDINACTIVE
;
1432 * at this point both the iocount and usecount
1434 * pick up an iocount so that we can call
1435 * VNOP_INACTIVE with the vnode lock unheld
1441 vp
->v_lflag
&= ~VL_NEEDINACTIVE
;
1444 context
.vc_proc
= current_proc();
1445 context
.vc_ucred
= kauth_cred_get();
1446 VNOP_INACTIVE(vp
, &context
);
1450 * because we dropped the vnode lock to call VNOP_INACTIVE
1451 * the state of the vnode may have changed... we may have
1452 * picked up an iocount, usecount or the MARKTERM may have
1453 * been set... we need to reevaluate the reference counts
1454 * to determine if we can call vnode_reclaim_internal at
1455 * this point... if the reference counts are up, we'll pick
1456 * up the MARKTERM state when they get subsequently dropped
1458 if ( (vp
->v_iocount
== 1) && (vp
->v_usecount
== 0) &&
1459 ((vp
->v_lflag
& (VL_MARKTERM
| VL_TERMINATE
| VL_DEAD
)) == VL_MARKTERM
)) {
1462 ut
= get_bsdthread_info(current_thread());
1464 if (ut
->uu_defer_reclaims
) {
1465 vp
->v_defer_reclaimlist
= ut
->uu_vreclaims
;
1466 ut
->uu_vreclaims
= vp
;
1469 vnode_reclaim_internal(vp
, 1, 0);
1471 vnode_dropiocount(vp
, 1);
1480 * Remove any vnodes in the vnode table belonging to mount point mp.
1482 * If MNT_NOFORCE is specified, there should not be any active ones,
1483 * return error if any are found (nb: this is a user error, not a
1484 * system error). If MNT_FORCE is specified, detach any active vnodes
1488 int busyprt
= 0; /* print out busy vnodes */
1490 struct ctldebug debug1
= { "busyprt", &busyprt
};
1495 vflush(mp
, skipvp
, flags
)
1497 struct vnode
*skipvp
;
1500 struct proc
*p
= current_proc();
1507 vnode_iterate_setup(mp
);
1509 * On regular unmounts(not forced) do a
1510 * quick check for vnodes to be in use. This
1511 * preserves the caching of vnodes. automounter
1512 * tries unmounting every so often to see whether
1513 * it is still busy or not.
1515 if ((flags
& FORCECLOSE
)==0) {
1516 if (vnode_umount_preflight(mp
, skipvp
, flags
)) {
1517 vnode_iterate_clear(mp
);
1523 /* it is returns 0 then there is nothing to do */
1524 retval
= vnode_iterate_prepare(mp
);
1527 vnode_iterate_clear(mp
);
1532 /* iterate over all the vnodes */
1533 while (!TAILQ_EMPTY(&mp
->mnt_workerqueue
)) {
1534 vp
= TAILQ_FIRST(&mp
->mnt_workerqueue
);
1535 TAILQ_REMOVE(&mp
->mnt_workerqueue
, vp
, v_mntvnodes
);
1536 TAILQ_INSERT_TAIL(&mp
->mnt_vnodelist
, vp
, v_mntvnodes
);
1537 if ( (vp
->v_mount
!= mp
) || (vp
== skipvp
)) {
1544 if ((vp
->v_id
!= vid
) || ((vp
->v_lflag
& (VL_DEAD
| VL_TERMINATE
)))) {
1551 * If requested, skip over vnodes marked VSYSTEM.
1552 * Skip over all vnodes marked VNOFLUSH.
1554 if ((flags
& SKIPSYSTEM
) && ((vp
->v_flag
& VSYSTEM
) ||
1555 (vp
->v_flag
& VNOFLUSH
))) {
1561 * If requested, skip over vnodes marked VSWAP.
1563 if ((flags
& SKIPSWAP
) && (vp
->v_flag
& VSWAP
)) {
1569 * If requested, skip over vnodes marked VSWAP.
1571 if ((flags
& SKIPROOT
) && (vp
->v_flag
& VROOT
)) {
1577 * If WRITECLOSE is set, only flush out regular file
1578 * vnodes open for writing.
1580 if ((flags
& WRITECLOSE
) &&
1581 (vp
->v_writecount
== 0 || vp
->v_type
!= VREG
)) {
1587 * If the real usecount is 0, all we need to do is clear
1588 * out the vnode data structures and we are done.
1590 if (((vp
->v_usecount
== 0) ||
1591 ((vp
->v_usecount
- vp
->v_kusecount
) == 0))) {
1592 vp
->v_iocount
++; /* so that drain waits for * other iocounts */
1596 vnode_reclaim_internal(vp
, 1, 0);
1597 vnode_dropiocount(vp
, 1);
1606 * If FORCECLOSE is set, forcibly close the vnode.
1607 * For block or character devices, revert to an
1608 * anonymous device. For all other files, just kill them.
1610 if (flags
& FORCECLOSE
) {
1611 if (vp
->v_type
!= VBLK
&& vp
->v_type
!= VCHR
) {
1612 vp
->v_iocount
++; /* so that drain waits * for other iocounts */
1616 vnode_reclaim_internal(vp
, 1, 0);
1617 vnode_dropiocount(vp
, 1);
1622 vp
->v_mount
= 0; /*override any dead_mountp */
1623 vp
->v_lflag
&= ~VL_DEAD
;
1624 vp
->v_op
= spec_vnodeop_p
;
1625 insmntque(vp
, (struct mount
*)0);
1633 vprint("vflush: busy vnode", vp
);
1640 /* At this point the worker queue is completed */
1641 if (busy
&& ((flags
& FORCECLOSE
)==0) && reclaimed
) {
1644 (void)vnode_iterate_reloadq(mp
);
1645 /* returned with mount lock held */
1649 /* if new vnodes were created in between retry the reclaim */
1650 if ( vnode_iterate_reloadq(mp
) != 0) {
1651 if (!(busy
&& ((flags
& FORCECLOSE
)==0)))
1654 vnode_iterate_clear(mp
);
1657 if (busy
&& ((flags
& FORCECLOSE
)==0))
1662 int num_recycledvnodes
=0;
1664 * Disassociate the underlying file system from a vnode.
1665 * The vnode lock is held on entry.
1668 vclean(vnode_t vp
, int flags
, proc_t p
)
1670 struct vfs_context context
;
1673 int already_terminating
;
1674 kauth_cred_t ucred
= NULL
;
1676 context
.vc_proc
= p
;
1677 context
.vc_ucred
= kauth_cred_get();
1680 * Check to see if the vnode is in use.
1681 * If so we have to reference it before we clean it out
1682 * so that its count cannot fall to zero and generate a
1683 * race against ourselves to recycle it.
1685 active
= vp
->v_usecount
;
1688 * just in case we missed sending a needed
1689 * VNOP_INACTIVE, we'll do it now
1691 need_inactive
= (vp
->v_lflag
& VL_NEEDINACTIVE
);
1693 vp
->v_lflag
&= ~VL_NEEDINACTIVE
;
1696 * Prevent the vnode from being recycled or
1697 * brought into use while we clean it out.
1699 already_terminating
= (vp
->v_lflag
& VL_TERMINATE
);
1701 vp
->v_lflag
|= VL_TERMINATE
;
1704 * remove the vnode from any mount list
1707 insmntque(vp
, (struct mount
*)0);
1715 kauth_cred_rele(ucred
);
1717 OSAddAtomic(1, &num_recycledvnodes
);
1719 * purge from the name cache as early as possible...
1723 if (active
&& (flags
& DOCLOSE
))
1724 VNOP_CLOSE(vp
, IO_NDELAY
, &context
);
1727 * Clean out any buffers associated with the vnode.
1729 if (flags
& DOCLOSE
) {
1731 if (vp
->v_tag
== VT_NFS
)
1732 nfs_vinvalbuf(vp
, V_SAVE
, NOCRED
, p
, 0);
1736 VNOP_FSYNC(vp
, MNT_WAIT
, &context
);
1737 buf_invalidateblks(vp
, BUF_WRITE_DATA
, 0, 0);
1739 if (UBCINFOEXISTS(vp
))
1741 * Clean the pages in VM.
1743 (void)ubc_sync_range(vp
, (off_t
)0, ubc_getsize(vp
), UBC_PUSHALL
);
1745 if (UBCINFOEXISTS(vp
))
1746 cluster_release(vp
->v_ubcinfo
);
1748 if (active
|| need_inactive
)
1749 VNOP_INACTIVE(vp
, &context
);
1751 /* Destroy ubc named reference */
1752 ubc_destroy_named(vp
);
1755 * Reclaim the vnode.
1757 if (VNOP_RECLAIM(vp
, &context
))
1758 panic("vclean: cannot reclaim");
1760 // make sure the name & parent ptrs get cleaned out!
1761 vnode_update_identity(vp
, NULLVP
, NULL
, 0, 0, VNODE_UPDATE_PARENT
| VNODE_UPDATE_NAME
);
1765 vp
->v_mount
= dead_mountp
;
1766 vp
->v_op
= dead_vnodeop_p
;
1770 vp
->v_lflag
|= VL_DEAD
;
1772 if (already_terminating
== 0) {
1773 vp
->v_lflag
&= ~VL_TERMINATE
;
1775 * Done with purge, notify sleepers of the grim news.
1777 if (vp
->v_lflag
& VL_TERMWANT
) {
1778 vp
->v_lflag
&= ~VL_TERMWANT
;
1779 wakeup(&vp
->v_lflag
);
1785 * Eliminate all activity associated with the requested vnode
1786 * and with all vnodes aliased to the requested vnode.
1789 vn_revoke(vnode_t vp
, int flags
, __unused vfs_context_t a_context
)
1795 if ((flags
& REVOKEALL
) == 0)
1796 panic("vnop_revoke");
1799 if (vp
->v_flag
& VALIASED
) {
1801 * If a vgone (or vclean) is already in progress,
1802 * wait until it is done and return.
1805 if (vp
->v_lflag
& VL_TERMINATE
) {
1811 * Ensure that vp will not be vgone'd while we
1812 * are eliminating its aliases.
1815 while (vp
->v_flag
& VALIASED
) {
1816 for (vq
= *vp
->v_hashchain
; vq
; vq
= vq
->v_specnext
) {
1817 if (vq
->v_rdev
!= vp
->v_rdev
||
1818 vq
->v_type
!= vp
->v_type
|| vp
== vq
)
1822 if (vnode_getwithvid(vq
,vid
)){
1826 vnode_reclaim_internal(vq
, 0, 0);
1834 vnode_reclaim_internal(vp
, 0, 0);
1840 * Recycle an unused vnode to the front of the free list.
1841 * Release the passed interlock if the vnode will be recycled.
1849 if (vp
->v_iocount
|| vp
->v_usecount
) {
1850 vp
->v_lflag
|= VL_MARKTERM
;
1854 vnode_reclaim_internal(vp
, 1, 0);
1861 vnode_reload(vnode_t vp
)
1865 if ((vp
->v_iocount
> 1) || vp
->v_usecount
) {
1869 if (vp
->v_iocount
<= 0)
1870 panic("vnode_reload with no iocount %d", vp
->v_iocount
);
1872 /* mark for release when iocount is dopped */
1873 vp
->v_lflag
|= VL_MARKTERM
;
1887 * Clean out the filesystem specific data.
1888 * vclean also takes care of removing the
1889 * vnode from any mount list it might be on
1891 vclean(vp
, DOCLOSE
, current_proc());
1894 * If special device, remove it from special device alias list
1897 if ((vp
->v_type
== VBLK
|| vp
->v_type
== VCHR
) && vp
->v_specinfo
!= 0) {
1899 if (*vp
->v_hashchain
== vp
) {
1900 *vp
->v_hashchain
= vp
->v_specnext
;
1902 for (vq
= *vp
->v_hashchain
; vq
; vq
= vq
->v_specnext
) {
1903 if (vq
->v_specnext
!= vp
)
1905 vq
->v_specnext
= vp
->v_specnext
;
1909 panic("missing bdev");
1911 if (vp
->v_flag
& VALIASED
) {
1913 for (vq
= *vp
->v_hashchain
; vq
; vq
= vq
->v_specnext
) {
1914 if (vq
->v_rdev
!= vp
->v_rdev
||
1915 vq
->v_type
!= vp
->v_type
)
1922 panic("missing alias");
1924 vx
->v_flag
&= ~VALIASED
;
1925 vp
->v_flag
&= ~VALIASED
;
1929 struct specinfo
*tmp
= vp
->v_specinfo
;
1930 vp
->v_specinfo
= NULL
;
1931 FREE_ZONE((void *)tmp
, sizeof(struct specinfo
), M_SPECINFO
);
1937 * Lookup a vnode by device number.
1940 check_mountedon(dev_t dev
, enum vtype type
, int *errorp
)
1948 for (vp
= speclisth
[SPECHASH(dev
)]; vp
; vp
= vp
->v_specnext
) {
1949 if (dev
!= vp
->v_rdev
|| type
!= vp
->v_type
)
1953 if (vnode_getwithvid(vp
,vid
))
1956 if ((vp
->v_usecount
> 0) || (vp
->v_iocount
> 1)) {
1958 if ((*errorp
= vfs_mountedon(vp
)) != 0)
1970 * Calculate the total number of references to a special device.
1980 if ((vp
->v_flag
& VALIASED
) == 0)
1981 return (vp
->v_usecount
- vp
->v_kusecount
);
1984 for (count
= 0, vq
= *vp
->v_hashchain
; vq
; vq
= vnext
) {
1985 vnext
= vq
->v_specnext
;
1986 if (vq
->v_rdev
!= vp
->v_rdev
|| vq
->v_type
!= vp
->v_type
)
1991 if (vnode_getwithvid(vq
, vid
)) {
1995 * Alias, but not in use, so flush it out.
1998 if ((vq
->v_usecount
== 0) && (vq
->v_iocount
== 1) && vq
!= vp
) {
1999 vnode_reclaim_internal(vq
, 1, 0);
2004 count
+= (vq
->v_usecount
- vq
->v_kusecount
);
2015 int prtactive
= 0; /* 1 => print out reclaim of active vnodes */
2018 * Print out a description of a vnode.
2020 static char *typename
[] =
2021 { "VNON", "VREG", "VDIR", "VBLK", "VCHR", "VLNK", "VSOCK", "VFIFO", "VBAD" };
2024 vprint(const char *label
, struct vnode
*vp
)
2029 printf("%s: ", label
);
2030 printf("type %s, usecount %d, writecount %d",
2031 typename
[vp
->v_type
], vp
->v_usecount
, vp
->v_writecount
);
2033 if (vp
->v_flag
& VROOT
)
2034 strcat(sbuf
, "|VROOT");
2035 if (vp
->v_flag
& VTEXT
)
2036 strcat(sbuf
, "|VTEXT");
2037 if (vp
->v_flag
& VSYSTEM
)
2038 strcat(sbuf
, "|VSYSTEM");
2039 if (vp
->v_flag
& VNOFLUSH
)
2040 strcat(sbuf
, "|VNOFLUSH");
2041 if (vp
->v_flag
& VBWAIT
)
2042 strcat(sbuf
, "|VBWAIT");
2043 if (vp
->v_flag
& VALIASED
)
2044 strcat(sbuf
, "|VALIASED");
2045 if (sbuf
[0] != '\0')
2046 printf(" flags (%s)", &sbuf
[1]);
2051 vn_getpath(struct vnode
*vp
, char *pathbuf
, int *len
)
2053 return build_path(vp
, pathbuf
, *len
, len
);
2057 static char *extension_table
=NULL
;
2059 static int max_ext_width
;
2062 extension_cmp(void *a
, void *b
)
2064 return (strlen((char *)a
) - strlen((char *)b
));
2069 // This is the api LaunchServices uses to inform the kernel
2070 // the list of package extensions to ignore.
2072 // Internally we keep the list sorted by the length of the
2073 // the extension (from longest to shortest). We sort the
2074 // list of extensions so that we can speed up our searches
2075 // when comparing file names -- we only compare extensions
2076 // that could possibly fit into the file name, not all of
2077 // them (i.e. a short 8 character name can't have an 8
2078 // character extension).
2080 __private_extern__
int
2081 set_package_extensions_table(void *data
, int nentries
, int maxwidth
)
2083 char *new_exts
, *ptr
;
2086 if (nentries
<= 0 || nentries
> 1024 || maxwidth
<= 0 || maxwidth
> 255) {
2090 MALLOC(new_exts
, char *, nentries
* maxwidth
, M_TEMP
, M_WAITOK
);
2092 error
= copyin(CAST_USER_ADDR_T(data
), new_exts
, nentries
* maxwidth
);
2094 FREE(new_exts
, M_TEMP
);
2098 if (extension_table
) {
2099 FREE(extension_table
, M_TEMP
);
2101 extension_table
= new_exts
;
2103 max_ext_width
= maxwidth
;
2105 qsort(extension_table
, nexts
, maxwidth
, extension_cmp
);
2111 __private_extern__
int
2112 is_package_name(char *name
, int len
)
2115 char *ptr
, *name_ext
;
2122 for(ptr
=name
; *ptr
!= '\0'; ptr
++) {
2128 // if there is no "." extension, it can't match
2129 if (name_ext
== NULL
) {
2133 // advance over the "."
2136 // now iterate over all the extensions to see if any match
2137 ptr
= &extension_table
[0];
2138 for(i
=0; i
< nexts
; i
++, ptr
+=max_ext_width
) {
2139 extlen
= strlen(ptr
);
2140 if (strncasecmp(name_ext
, ptr
, extlen
) == 0 && name_ext
[extlen
] == '\0') {
2146 // if we get here, no extension matched
2151 vn_path_package_check(__unused vnode_t vp
, char *path
, int pathlen
, int *component
)
2162 while(end
< path
+ pathlen
&& *end
!= '\0') {
2163 while(end
< path
+ pathlen
&& *end
== '/' && *end
!= '\0') {
2169 while(end
< path
+ pathlen
&& *end
!= '/' && *end
!= '\0') {
2173 if (end
> path
+ pathlen
) {
2174 // hmm, string wasn't null terminated
2179 if (is_package_name(ptr
, end
- ptr
)) {
2193 * Top level filesystem related information gathering.
2195 extern unsigned int vfs_nummntops
;
2198 vfs_sysctl(int *name
, u_int namelen
, user_addr_t oldp
, size_t *oldlenp
,
2199 user_addr_t newp
, size_t newlen
, struct proc
*p
)
2201 struct vfstable
*vfsp
;
2205 struct vfsconf
*vfsc
;
2208 * The VFS_NUMMNTOPS shouldn't be at name[0] since
2209 * is a VFS generic variable. So now we must check
2210 * namelen so we don't end up covering any UFS
2211 * variables (sinc UFS vfc_typenum is 1).
2213 * It should have been:
2214 * name[0]: VFS_GENERIC
2215 * name[1]: VFS_NUMMNTOPS
2217 if (namelen
== 1 && name
[0] == VFS_NUMMNTOPS
) {
2218 return (sysctl_rdint(oldp
, oldlenp
, newp
, vfs_nummntops
));
2221 /* all sysctl names at this level are at least name and field */
2223 return (EISDIR
); /* overloaded */
2224 if (name
[0] != VFS_GENERIC
) {
2225 struct vfs_context context
;
2227 for (vfsp
= vfsconf
; vfsp
; vfsp
= vfsp
->vfc_next
)
2228 if (vfsp
->vfc_typenum
== name
[0])
2232 context
.vc_proc
= p
;
2233 context
.vc_ucred
= kauth_cred_get();
2235 return ((*vfsp
->vfc_vfsops
->vfs_sysctl
)(&name
[1], namelen
- 1,
2236 oldp
, oldlenp
, newp
, newlen
, &context
));
2239 case VFS_MAXTYPENUM
:
2240 return (sysctl_rdint(oldp
, oldlenp
, newp
, maxvfsconf
));
2243 return (ENOTDIR
); /* overloaded */
2244 for (vfsp
= vfsconf
; vfsp
; vfsp
= vfsp
->vfc_next
)
2245 if (vfsp
->vfc_typenum
== name
[2])
2249 vfsc
= (struct vfsconf
*)vfsp
;
2250 if (proc_is64bit(p
)) {
2251 struct user_vfsconf usr_vfsc
;
2252 usr_vfsc
.vfc_vfsops
= CAST_USER_ADDR_T(vfsc
->vfc_vfsops
);
2253 bcopy(vfsc
->vfc_name
, usr_vfsc
.vfc_name
, sizeof(usr_vfsc
.vfc_name
));
2254 usr_vfsc
.vfc_typenum
= vfsc
->vfc_typenum
;
2255 usr_vfsc
.vfc_refcount
= vfsc
->vfc_refcount
;
2256 usr_vfsc
.vfc_flags
= vfsc
->vfc_flags
;
2257 usr_vfsc
.vfc_mountroot
= CAST_USER_ADDR_T(vfsc
->vfc_mountroot
);
2258 usr_vfsc
.vfc_next
= CAST_USER_ADDR_T(vfsc
->vfc_next
);
2259 return (sysctl_rdstruct(oldp
, oldlenp
, newp
, &usr_vfsc
,
2263 return (sysctl_rdstruct(oldp
, oldlenp
, newp
, vfsc
,
2264 sizeof(struct vfsconf
)));
2267 case VFS_SET_PACKAGE_EXTS
:
2268 return set_package_extensions_table((void *)name
[1], name
[2], name
[3]);
2271 * We need to get back into the general MIB, so we need to re-prepend
2272 * CTL_VFS to our name and try userland_sysctl().
2274 usernamelen
= namelen
+ 1;
2275 MALLOC(username
, int *, usernamelen
* sizeof(*username
),
2277 bcopy(name
, username
+ 1, namelen
* sizeof(*name
));
2278 username
[0] = CTL_VFS
;
2279 error
= userland_sysctl(p
, username
, usernamelen
, oldp
,
2280 oldlenp
, 1, newp
, newlen
, oldlenp
);
2281 FREE(username
, M_TEMP
);
2285 int kinfo_vdebug
= 1;
2286 #define KINFO_VNODESLOP 10
2288 * Dump vnode list (via sysctl).
2289 * Copyout address of vnode followed by vnode.
2293 sysctl_vnode(__unused user_addr_t where
, __unused
size_t *sizep
)
2296 struct mount
*mp
, *nmp
;
2297 struct vnode
*nvp
, *vp
;
2298 char *bp
= where
, *savebp
;
2302 #define VPTRSZ sizeof (struct vnode *)
2303 #define VNODESZ sizeof (struct vnode)
2304 if (where
== NULL
) {
2305 *sizep
= (numvnodes
+ KINFO_VNODESLOP
) * (VPTRSZ
+ VNODESZ
);
2308 ewhere
= where
+ *sizep
;
2310 for (mp
= mountlist
.cqh_first
; mp
!= (void *)&mountlist
; mp
= nmp
) {
2311 if (vfs_busy(mp
, LK_NOWAIT
)) {
2312 nmp
= mp
->mnt_list
.cqe_next
;
2317 TAILQ_FOREACH(vp
, &mp
->mnt_vnodelist
, v_mntvnodes
) {
2319 * Check that the vp is still associated with
2320 * this filesystem. RACE: could have been
2321 * recycled onto the same filesystem.
2323 if (vp
->v_mount
!= mp
) {
2325 printf("kinfo: vp changed\n");
2329 if (bp
+ VPTRSZ
+ VNODESZ
> ewhere
) {
2331 *sizep
= bp
- where
;
2334 if ((error
= copyout((caddr_t
)&vp
, bp
, VPTRSZ
)) ||
2335 (error
= copyout((caddr_t
)vp
, bp
+ VPTRSZ
, VNODESZ
))) {
2339 bp
+= VPTRSZ
+ VNODESZ
;
2341 nmp
= mp
->mnt_list
.cqe_next
;
2345 *sizep
= bp
- where
;
2353 * Check to see if a filesystem is mounted on a block device.
2363 if (vp
->v_specflags
& SI_MOUNTEDON
) {
2367 if (vp
->v_flag
& VALIASED
) {
2368 for (vq
= *vp
->v_hashchain
; vq
; vq
= vq
->v_specnext
) {
2369 if (vq
->v_rdev
!= vp
->v_rdev
||
2370 vq
->v_type
!= vp
->v_type
)
2372 if (vq
->v_specflags
& SI_MOUNTEDON
) {
2384 * Unmount all filesystems. The list is traversed in reverse order
2385 * of mounting to avoid dependencies.
2387 __private_extern__
void
2391 struct proc
*p
= current_proc();
2395 * Since this only runs when rebooting, it is not interlocked.
2398 while(!TAILQ_EMPTY(&mountlist
)) {
2399 mp
= TAILQ_LAST(&mountlist
, mntlist
);
2400 mount_list_unlock();
2401 error
= dounmount(mp
, MNT_FORCE
, p
);
2404 TAILQ_REMOVE(&mountlist
, mp
, mnt_list
);
2405 printf("unmount of %s failed (", mp
->mnt_vfsstat
.f_mntonname
);
2409 printf("%d)\n", error
);
2414 mount_list_unlock();
2419 * This routine is called from vnode_pager_no_senders()
2420 * which in turn can be called with vnode locked by vnode_uncache()
2421 * But it could also get called as a result of vm_object_cache_trim().
2422 * In that case lock state is unknown.
2423 * AGE the vnode so that it gets recycled quickly.
2425 __private_extern__
void
2426 vnode_pager_vrele(struct vnode
*vp
)
2430 if (!ISSET(vp
->v_lflag
, VL_TERMINATE
))
2431 panic("vnode_pager_vrele: vp not in termination");
2432 vp
->v_lflag
&= ~VNAMED_UBC
;
2434 if (UBCINFOEXISTS(vp
)) {
2435 struct ubc_info
*uip
= vp
->v_ubcinfo
;
2437 if (ISSET(uip
->ui_flags
, UI_WASMAPPED
))
2438 SET(vp
->v_flag
, VWASMAPPED
);
2439 vp
->v_ubcinfo
= UBC_INFO_NULL
;
2441 ubc_info_deallocate(uip
);
2443 panic("NO ubcinfo in vnode_pager_vrele");
2447 wakeup(&vp
->v_lflag
);
2451 #include <sys/disk.h>
2454 vfs_init_io_attributes(vnode_t devvp
, mount_t mp
)
2458 off_t writeblockcnt
;
2467 struct vfs_context context
;
2469 proc_t p
= current_proc();
2471 context
.vc_proc
= p
;
2472 context
.vc_ucred
= kauth_cred_get();
2476 * determine if this mount point exists on the same device as the root
2477 * partition... if so, then it comes under the hard throttle control
2480 static int rootunit
= -1;
2482 if (rootunit
== -1) {
2483 if (VNOP_IOCTL(rootvp
, DKIOCGETBSDUNIT
, (caddr_t
)&rootunit
, 0, &context
))
2485 else if (rootvp
== devvp
)
2486 mp
->mnt_kern_flag
|= MNTK_ROOTDEV
;
2488 if (devvp
!= rootvp
&& rootunit
!= -1) {
2489 if (VNOP_IOCTL(devvp
, DKIOCGETBSDUNIT
, (caddr_t
)&thisunit
, 0, &context
) == 0) {
2490 if (thisunit
== rootunit
)
2491 mp
->mnt_kern_flag
|= MNTK_ROOTDEV
;
2495 * force the spec device to re-cache
2496 * the underlying block size in case
2497 * the filesystem overrode the initial value
2499 set_fsblocksize(devvp
);
2502 if ((error
= VNOP_IOCTL(devvp
, DKIOCGETBLOCKSIZE
,
2503 (caddr_t
)&blksize
, 0, &context
)))
2506 mp
->mnt_devblocksize
= blksize
;
2508 if (VNOP_IOCTL(devvp
, DKIOCISVIRTUAL
, (caddr_t
)&isvirtual
, 0, &context
) == 0) {
2510 mp
->mnt_kern_flag
|= MNTK_VIRTUALDEV
;
2513 if ((error
= VNOP_IOCTL(devvp
, DKIOCGETMAXBLOCKCOUNTREAD
,
2514 (caddr_t
)&readblockcnt
, 0, &context
)))
2517 if ((error
= VNOP_IOCTL(devvp
, DKIOCGETMAXBLOCKCOUNTWRITE
,
2518 (caddr_t
)&writeblockcnt
, 0, &context
)))
2521 if ((error
= VNOP_IOCTL(devvp
, DKIOCGETMAXBYTECOUNTREAD
,
2522 (caddr_t
)&readmaxcnt
, 0, &context
)))
2525 if ((error
= VNOP_IOCTL(devvp
, DKIOCGETMAXBYTECOUNTWRITE
,
2526 (caddr_t
)&writemaxcnt
, 0, &context
)))
2529 if ((error
= VNOP_IOCTL(devvp
, DKIOCGETMAXSEGMENTCOUNTREAD
,
2530 (caddr_t
)&readsegcnt
, 0, &context
)))
2533 if ((error
= VNOP_IOCTL(devvp
, DKIOCGETMAXSEGMENTCOUNTWRITE
,
2534 (caddr_t
)&writesegcnt
, 0, &context
)))
2537 if ((error
= VNOP_IOCTL(devvp
, DKIOCGETMAXSEGMENTBYTECOUNTREAD
,
2538 (caddr_t
)&readsegsize
, 0, &context
)))
2541 if ((error
= VNOP_IOCTL(devvp
, DKIOCGETMAXSEGMENTBYTECOUNTWRITE
,
2542 (caddr_t
)&writesegsize
, 0, &context
)))
2546 temp
= (readmaxcnt
> UINT32_MAX
) ? UINT32_MAX
: readmaxcnt
;
2549 temp
= readblockcnt
* blksize
;
2550 temp
= (temp
> UINT32_MAX
) ? UINT32_MAX
: temp
;
2554 mp
->mnt_maxreadcnt
= (u_int32_t
)temp
;
2557 temp
= (writemaxcnt
> UINT32_MAX
) ? UINT32_MAX
: writemaxcnt
;
2559 if (writeblockcnt
) {
2560 temp
= writeblockcnt
* blksize
;
2561 temp
= (temp
> UINT32_MAX
) ? UINT32_MAX
: temp
;
2565 mp
->mnt_maxwritecnt
= (u_int32_t
)temp
;
2568 temp
= (readsegcnt
> UINT16_MAX
) ? UINT16_MAX
: readsegcnt
;
2569 mp
->mnt_segreadcnt
= (u_int16_t
)temp
;
2572 temp
= (writesegcnt
> UINT16_MAX
) ? UINT16_MAX
: writesegcnt
;
2573 mp
->mnt_segwritecnt
= (u_int16_t
)temp
;
2576 temp
= (readsegsize
> UINT32_MAX
) ? UINT32_MAX
: readsegsize
;
2578 temp
= mp
->mnt_maxreadcnt
;
2579 mp
->mnt_maxsegreadsize
= (u_int32_t
)temp
;
2582 temp
= (writesegsize
> UINT32_MAX
) ? UINT32_MAX
: writesegsize
;
2584 temp
= mp
->mnt_maxwritecnt
;
2585 mp
->mnt_maxsegwritesize
= (u_int32_t
)temp
;
2590 static struct klist fs_klist
;
2593 vfs_event_init(void)
2596 klist_init(&fs_klist
);
2600 vfs_event_signal(__unused fsid_t
*fsid
, u_int32_t event
, __unused
intptr_t data
)
2603 KNOTE(&fs_klist
, event
);
2607 * return the number of mounted filesystems.
2610 sysctl_vfs_getvfscnt(void)
2612 return(mount_getvfscnt());
2617 mount_getvfscnt(void)
2623 mount_list_unlock();
2631 mount_fillfsids(fsid_t
*fsidlst
, int count
)
2638 TAILQ_FOREACH(mp
, &mountlist
, mnt_list
) {
2639 if (actual
<= count
) {
2640 fsidlst
[actual
] = mp
->mnt_vfsstat
.f_fsid
;
2644 mount_list_unlock();
2650 * fill in the array of fsid_t's up to a max of 'count', the actual
2651 * number filled in will be set in '*actual'. If there are more fsid_t's
2652 * than room in fsidlst then ENOMEM will be returned and '*actual' will
2653 * have the actual count.
2654 * having *actual filled out even in the error case is depended upon.
2657 sysctl_vfs_getvfslist(fsid_t
*fsidlst
, int count
, int *actual
)
2663 TAILQ_FOREACH(mp
, &mountlist
, mnt_list
) {
2665 if (*actual
<= count
)
2666 fsidlst
[(*actual
) - 1] = mp
->mnt_vfsstat
.f_fsid
;
2668 mount_list_unlock();
2669 return (*actual
<= count
? 0 : ENOMEM
);
2673 sysctl_vfs_vfslist SYSCTL_HANDLER_ARGS
2679 /* This is a readonly node. */
2680 if (req
->newptr
!= USER_ADDR_NULL
)
2683 /* they are querying us so just return the space required. */
2684 if (req
->oldptr
== USER_ADDR_NULL
) {
2685 req
->oldidx
= sysctl_vfs_getvfscnt() * sizeof(fsid_t
);
2690 * Retrieve an accurate count of the amount of space required to copy
2691 * out all the fsids in the system.
2693 space
= req
->oldlen
;
2694 req
->oldlen
= sysctl_vfs_getvfscnt() * sizeof(fsid_t
);
2696 /* they didn't give us enough space. */
2697 if (space
< req
->oldlen
)
2700 MALLOC(fsidlst
, fsid_t
*, req
->oldlen
, M_TEMP
, M_WAITOK
);
2701 error
= sysctl_vfs_getvfslist(fsidlst
, req
->oldlen
/ sizeof(fsid_t
),
2704 * If we get back ENOMEM, then another mount has been added while we
2705 * slept in malloc above. If this is the case then try again.
2707 if (error
== ENOMEM
) {
2708 FREE(fsidlst
, M_TEMP
);
2709 req
->oldlen
= space
;
2713 error
= SYSCTL_OUT(req
, fsidlst
, actual
* sizeof(fsid_t
));
2715 FREE(fsidlst
, M_TEMP
);
2720 * Do a sysctl by fsid.
2723 sysctl_vfs_ctlbyfsid SYSCTL_HANDLER_ARGS
2726 struct user_vfsidctl user_vc
;
2728 struct vfsstatfs
*sp
;
2731 int error
, flags
, namelen
;
2732 struct vfs_context context
;
2733 boolean_t is_64_bit
;
2738 context
.vc_proc
= p
;
2739 context
.vc_ucred
= kauth_cred_get();
2740 is_64_bit
= proc_is64bit(p
);
2743 error
= SYSCTL_IN(req
, &user_vc
, sizeof(user_vc
));
2746 if (user_vc
.vc_vers
!= VFS_CTL_VERS1
)
2748 mp
= mount_list_lookupby_fsid(&user_vc
.vc_fsid
, 0, 0);
2751 error
= SYSCTL_IN(req
, &vc
, sizeof(vc
));
2754 if (vc
.vc_vers
!= VFS_CTL_VERS1
)
2756 mp
= mount_list_lookupby_fsid(&vc
.vc_fsid
, 0, 0);
2760 /* reset so that the fs specific code can fetch it. */
2763 * Note if this is a VFS_CTL then we pass the actual sysctl req
2764 * in for "oldp" so that the lower layer can DTRT and use the
2765 * SYSCTL_IN/OUT routines.
2767 if (mp
->mnt_op
->vfs_sysctl
!= NULL
) {
2769 if (vfs_64bitready(mp
)) {
2770 error
= mp
->mnt_op
->vfs_sysctl(name
, namelen
,
2771 CAST_USER_ADDR_T(req
),
2772 NULL
, USER_ADDR_NULL
, 0,
2780 error
= mp
->mnt_op
->vfs_sysctl(name
, namelen
,
2781 CAST_USER_ADDR_T(req
),
2782 NULL
, USER_ADDR_NULL
, 0,
2785 if (error
!= ENOTSUP
)
2789 case VFS_CTL_UMOUNT
:
2792 req
->newptr
= user_vc
.vc_ptr
;
2793 req
->newlen
= (size_t)user_vc
.vc_len
;
2796 req
->newptr
= CAST_USER_ADDR_T(vc
.vc_ptr
);
2797 req
->newlen
= vc
.vc_len
;
2799 error
= SYSCTL_IN(req
, &flags
, sizeof(flags
));
2802 error
= safedounmount(mp
, flags
, p
);
2804 case VFS_CTL_STATFS
:
2807 req
->newptr
= user_vc
.vc_ptr
;
2808 req
->newlen
= (size_t)user_vc
.vc_len
;
2811 req
->newptr
= CAST_USER_ADDR_T(vc
.vc_ptr
);
2812 req
->newlen
= vc
.vc_len
;
2814 error
= SYSCTL_IN(req
, &flags
, sizeof(flags
));
2817 sp
= &mp
->mnt_vfsstat
;
2818 if (((flags
& MNT_NOWAIT
) == 0 || (flags
& MNT_WAIT
)) &&
2819 (error
= vfs_update_vfsstat(mp
, &context
)))
2822 struct user_statfs sfs
;
2823 bzero(&sfs
, sizeof(sfs
));
2824 sfs
.f_flags
= mp
->mnt_flag
& MNT_VISFLAGMASK
;
2825 sfs
.f_type
= mp
->mnt_vtable
->vfc_typenum
;
2826 sfs
.f_bsize
= (user_long_t
)sp
->f_bsize
;
2827 sfs
.f_iosize
= (user_long_t
)sp
->f_iosize
;
2828 sfs
.f_blocks
= (user_long_t
)sp
->f_blocks
;
2829 sfs
.f_bfree
= (user_long_t
)sp
->f_bfree
;
2830 sfs
.f_bavail
= (user_long_t
)sp
->f_bavail
;
2831 sfs
.f_files
= (user_long_t
)sp
->f_files
;
2832 sfs
.f_ffree
= (user_long_t
)sp
->f_ffree
;
2833 sfs
.f_fsid
= sp
->f_fsid
;
2834 sfs
.f_owner
= sp
->f_owner
;
2836 strncpy(&sfs
.f_fstypename
, &sp
->f_fstypename
, MFSNAMELEN
-1);
2837 strncpy(&sfs
.f_mntonname
, &sp
->f_mntonname
, MNAMELEN
-1);
2838 strncpy(&sfs
.f_mntfromname
, &sp
->f_mntfromname
, MNAMELEN
-1);
2840 error
= SYSCTL_OUT(req
, &sfs
, sizeof(sfs
));
2844 bzero(&sfs
, sizeof(struct statfs
));
2845 sfs
.f_flags
= mp
->mnt_flag
& MNT_VISFLAGMASK
;
2846 sfs
.f_type
= mp
->mnt_vtable
->vfc_typenum
;
2849 * It's possible for there to be more than 2^^31 blocks in the filesystem, so we
2850 * have to fudge the numbers here in that case. We inflate the blocksize in order
2851 * to reflect the filesystem size as best we can.
2853 if (sp
->f_blocks
> LONG_MAX
) {
2857 * Work out how far we have to shift the block count down to make it fit.
2858 * Note that it's possible to have to shift so far that the resulting
2859 * blocksize would be unreportably large. At that point, we will clip
2860 * any values that don't fit.
2862 * For safety's sake, we also ensure that f_iosize is never reported as
2863 * being smaller than f_bsize.
2865 for (shift
= 0; shift
< 32; shift
++) {
2866 if ((sp
->f_blocks
>> shift
) <= LONG_MAX
)
2868 if ((sp
->f_bsize
<< (shift
+ 1)) > LONG_MAX
)
2871 #define __SHIFT_OR_CLIP(x, s) ((((x) >> (s)) > LONG_MAX) ? LONG_MAX : ((x) >> (s)))
2872 sfs
.f_blocks
= (long)__SHIFT_OR_CLIP(sp
->f_blocks
, shift
);
2873 sfs
.f_bfree
= (long)__SHIFT_OR_CLIP(sp
->f_bfree
, shift
);
2874 sfs
.f_bavail
= (long)__SHIFT_OR_CLIP(sp
->f_bavail
, shift
);
2875 #undef __SHIFT_OR_CLIP
2876 sfs
.f_bsize
= (long)(sp
->f_bsize
<< shift
);
2877 sfs
.f_iosize
= lmax(sp
->f_iosize
, sp
->f_bsize
);
2879 sfs
.f_bsize
= (long)sp
->f_bsize
;
2880 sfs
.f_iosize
= (long)sp
->f_iosize
;
2881 sfs
.f_blocks
= (long)sp
->f_blocks
;
2882 sfs
.f_bfree
= (long)sp
->f_bfree
;
2883 sfs
.f_bavail
= (long)sp
->f_bavail
;
2885 sfs
.f_files
= (long)sp
->f_files
;
2886 sfs
.f_ffree
= (long)sp
->f_ffree
;
2887 sfs
.f_fsid
= sp
->f_fsid
;
2888 sfs
.f_owner
= sp
->f_owner
;
2890 strncpy(&sfs
.f_fstypename
, &sp
->f_fstypename
, MFSNAMELEN
-1);
2891 strncpy(&sfs
.f_mntonname
, &sp
->f_mntonname
, MNAMELEN
-1);
2892 strncpy(&sfs
.f_mntfromname
, &sp
->f_mntfromname
, MNAMELEN
-1);
2894 error
= SYSCTL_OUT(req
, &sfs
, sizeof(sfs
));
2903 static int filt_fsattach(struct knote
*kn
);
2904 static void filt_fsdetach(struct knote
*kn
);
2905 static int filt_fsevent(struct knote
*kn
, long hint
);
2907 struct filterops fs_filtops
=
2908 { 0, filt_fsattach
, filt_fsdetach
, filt_fsevent
};
2911 filt_fsattach(struct knote
*kn
)
2914 kn
->kn_flags
|= EV_CLEAR
;
2915 KNOTE_ATTACH(&fs_klist
, kn
);
2920 filt_fsdetach(struct knote
*kn
)
2923 KNOTE_DETACH(&fs_klist
, kn
);
2927 filt_fsevent(struct knote
*kn
, long hint
)
2930 kn
->kn_fflags
|= hint
;
2931 return (kn
->kn_fflags
!= 0);
2935 sysctl_vfs_noremotehang SYSCTL_HANDLER_ARGS
2942 /* We need a pid. */
2943 if (req
->newptr
== USER_ADDR_NULL
)
2946 error
= SYSCTL_IN(req
, &pid
, sizeof(pid
));
2950 p
= pfind(pid
< 0 ? -pid
: pid
);
2955 * Fetching the value is ok, but we only fetch if the old
2958 if (req
->oldptr
!= USER_ADDR_NULL
) {
2959 out
= !((p
->p_flag
& P_NOREMOTEHANG
) == 0);
2960 error
= SYSCTL_OUT(req
, &out
, sizeof(out
));
2964 /* XXX req->p->p_ucred -> kauth_cred_get() ??? */
2965 /* cansignal offers us enough security. */
2966 if (p
!= req
->p
&& suser(req
->p
->p_ucred
, &req
->p
->p_acflag
) != 0)
2970 p
->p_flag
&= ~P_NOREMOTEHANG
;
2972 p
->p_flag
|= P_NOREMOTEHANG
;
2976 /* the vfs.generic. branch. */
2977 SYSCTL_NODE(_vfs
, VFS_GENERIC
, generic
, CTLFLAG_RW
, 0, "vfs generic hinge");
2978 /* retreive a list of mounted filesystem fsid_t */
2979 SYSCTL_PROC(_vfs_generic
, OID_AUTO
, vfsidlist
, CTLFLAG_RD
,
2980 0, 0, sysctl_vfs_vfslist
, "S,fsid", "List of mounted filesystem ids");
2981 /* perform operations on filesystem via fsid_t */
2982 SYSCTL_NODE(_vfs_generic
, OID_AUTO
, ctlbyfsid
, CTLFLAG_RW
,
2983 sysctl_vfs_ctlbyfsid
, "ctlbyfsid");
2984 SYSCTL_PROC(_vfs_generic
, OID_AUTO
, noremotehang
, CTLFLAG_RW
,
2985 0, 0, sysctl_vfs_noremotehang
, "I", "noremotehang");
2988 int num_reusedvnodes
=0;
2991 new_vnode(vnode_t
*vpp
)
2994 int retries
= 0; /* retry incase of tablefull */
3001 if ( !TAILQ_EMPTY(&vnode_free_list
)) {
3003 * Pick the first vp for possible reuse
3005 vp
= TAILQ_FIRST(&vnode_free_list
);
3007 if (vp
->v_lflag
& VL_DEAD
)
3013 * we're either empty, or the next guy on the
3014 * list is a valid vnode... if we're under the
3015 * limit, we'll create a new vnode
3017 if (numvnodes
< desiredvnodes
) {
3019 vnode_list_unlock();
3020 MALLOC_ZONE(vp
, struct vnode
*, sizeof *vp
, M_VNODE
, M_WAITOK
);
3021 bzero((char *)vp
, sizeof *vp
);
3022 VLISTNONE(vp
); /* avoid double queue removal */
3023 lck_mtx_init(&vp
->v_lock
, vnode_lck_grp
, vnode_lck_attr
);
3026 vp
->v_id
= ts
.tv_nsec
;
3027 vp
->v_flag
= VSTANDARD
;
3033 * we've reached the system imposed maximum number of vnodes
3034 * but there isn't a single one available
3035 * wait a bit and then retry... if we can't get a vnode
3036 * after 100 retries, than log a complaint
3038 if (++retries
<= 100) {
3039 vnode_list_unlock();
3044 vnode_list_unlock();
3046 log(LOG_EMERG
, "%d desired, %d numvnodes, "
3047 "%d free, %d inactive\n",
3048 desiredvnodes
, numvnodes
, freevnodes
, inactivevnodes
);
3055 VREMFREE("new_vnode", vp
);
3058 vnode_list_unlock();
3062 * We could wait for the vnode_lock after removing the vp from the freelist
3063 * and the vid is bumped only at the very end of reclaim. So it is possible
3064 * that we are looking at a vnode that is being terminated. If so skip it.
3066 if ((vpid
!= vp
->v_id
) || (vp
->v_usecount
!= 0) || (vp
->v_iocount
!= 0) ||
3067 VONLIST(vp
) || (vp
->v_lflag
& VL_TERMINATE
)) {
3069 * we lost the race between dropping the list lock
3070 * and picking up the vnode_lock... someone else
3071 * used this vnode and it is now in a new state
3072 * so we need to go back and try again
3077 if ( (vp
->v_lflag
& (VL_NEEDINACTIVE
| VL_MARKTERM
)) == VL_NEEDINACTIVE
) {
3079 * we did a vnode_rele_ext that asked for
3080 * us not to reenter the filesystem during
3081 * the release even though VL_NEEDINACTIVE was
3082 * set... we'll do it here by doing a
3083 * vnode_get/vnode_put
3085 * pick up an iocount so that we can call
3086 * vnode_put and drive the VNOP_INACTIVE...
3087 * vnode_put will either leave us off
3088 * the freelist if a new ref comes in,
3089 * or put us back on the end of the freelist
3090 * or recycle us if we were marked for termination...
3091 * so we'll just go grab a new candidate
3097 vnode_put_locked(vp
);
3101 OSAddAtomic(1, &num_reusedvnodes
);
3103 /* Checks for anyone racing us for recycle */
3104 if (vp
->v_type
!= VBAD
) {
3105 if (vp
->v_lflag
& VL_DEAD
)
3106 panic("new_vnode: the vnode is VL_DEAD but not VBAD");
3108 (void)vnode_reclaim_internal(vp
, 1, 1);
3111 panic("new_vnode: vp on list ");
3112 if (vp
->v_usecount
|| vp
->v_iocount
|| vp
->v_kusecount
||
3113 (vp
->v_lflag
& (VNAMED_UBC
| VNAMED_MOUNT
| VNAMED_FSHASH
)))
3114 panic("new_vnode: free vnode still referenced\n");
3115 if ((vp
->v_mntvnodes
.tqe_prev
!= 0) && (vp
->v_mntvnodes
.tqe_next
!= 0))
3116 panic("new_vnode: vnode seems to be on mount list ");
3117 if ( !LIST_EMPTY(&vp
->v_nclinks
) || !LIST_EMPTY(&vp
->v_ncchildren
))
3118 panic("new_vnode: vnode still hooked into the name cache");
3120 if (vp
->v_unsafefs
) {
3121 lck_mtx_destroy(&vp
->v_unsafefs
->fsnodelock
, vnode_lck_grp
);
3122 FREE_ZONE((void *)vp
->v_unsafefs
, sizeof(struct unsafe_fsnode
), M_UNSAFEFS
);
3123 vp
->v_unsafefs
= (struct unsafe_fsnode
*)NULL
;
3126 vp
->v_writecount
= 0;
3127 vp
->v_references
= 0;
3128 vp
->v_iterblkflags
= 0;
3129 vp
->v_flag
= VSTANDARD
;
3130 /* vbad vnodes can point to dead_mountp */
3132 vp
->v_defer_reclaimlist
= (vnode_t
)0;
3142 vnode_lock(vnode_t vp
)
3144 lck_mtx_lock(&vp
->v_lock
);
3148 vnode_unlock(vnode_t vp
)
3150 lck_mtx_unlock(&vp
->v_lock
);
3156 vnode_get(struct vnode
*vp
)
3160 if ( (vp
->v_iocount
== 0) && (vp
->v_lflag
& (VL_TERMINATE
| VL_DEAD
)) ) {
3174 vnode_getwithvid(vnode_t vp
, int vid
)
3176 return(vget_internal(vp
, vid
, ( VNODE_NODEAD
| VNODE_WITHID
)));
3180 vnode_getwithref(vnode_t vp
)
3182 return(vget_internal(vp
, 0, 0));
3187 vnode_put(vnode_t vp
)
3192 retval
= vnode_put_locked(vp
);
3199 vnode_put_locked(vnode_t vp
)
3201 struct vfs_context context
;
3204 if (vp
->v_iocount
< 1)
3205 panic("vnode_put(%x): iocount < 1", vp
);
3207 if ((vp
->v_usecount
> 0) || (vp
->v_iocount
> 1)) {
3208 vnode_dropiocount(vp
, 1);
3211 if ((vp
->v_lflag
& (VL_MARKTERM
| VL_TERMINATE
| VL_DEAD
| VL_NEEDINACTIVE
)) == VL_NEEDINACTIVE
) {
3213 vp
->v_lflag
&= ~VL_NEEDINACTIVE
;
3216 context
.vc_proc
= current_proc();
3217 context
.vc_ucred
= kauth_cred_get();
3218 VNOP_INACTIVE(vp
, &context
);
3222 * because we had to drop the vnode lock before calling
3223 * VNOP_INACTIVE, the state of this vnode may have changed...
3224 * we may pick up both VL_MARTERM and either
3225 * an iocount or a usecount while in the VNOP_INACTIVE call
3226 * we don't want to call vnode_reclaim_internal on a vnode
3227 * that has active references on it... so loop back around
3228 * and reevaluate the state
3232 vp
->v_lflag
&= ~VL_NEEDINACTIVE
;
3234 if ((vp
->v_lflag
& (VL_MARKTERM
| VL_TERMINATE
| VL_DEAD
)) == VL_MARKTERM
)
3235 vnode_reclaim_internal(vp
, 1, 0);
3237 vnode_dropiocount(vp
, 1);
3243 /* is vnode_t in use by others? */
3245 vnode_isinuse(vnode_t vp
, int refcnt
)
3247 return(vnode_isinuse_locked(vp
, refcnt
, 0));
3252 vnode_isinuse_locked(vnode_t vp
, int refcnt
, int locked
)
3258 if ((vp
->v_type
!= VREG
) && (vp
->v_usecount
> refcnt
)) {
3262 if (vp
->v_type
== VREG
) {
3263 retval
= ubc_isinuse_locked(vp
, refcnt
, 1);
3273 /* resume vnode_t */
3275 vnode_resume(vnode_t vp
)
3280 if (vp
->v_owner
== current_thread()) {
3281 vp
->v_lflag
&= ~VL_SUSPENDED
;
3284 wakeup(&vp
->v_iocount
);
3292 vnode_drain(vnode_t vp
)
3295 if (vp
->v_lflag
& VL_DRAIN
) {
3296 panic("vnode_drain: recursuve drain");
3299 vp
->v_lflag
|= VL_DRAIN
;
3300 vp
->v_owner
= current_thread();
3302 while (vp
->v_iocount
> 1)
3303 msleep(&vp
->v_iocount
, &vp
->v_lock
, PVFS
, "vnode_drain", 0);
3309 * if the number of recent references via vnode_getwithvid or vnode_getwithref
3310 * exceeds this threshhold, than 'UN-AGE' the vnode by removing it from
3311 * the LRU list if it's currently on it... once the iocount and usecount both drop
3312 * to 0, it will get put back on the end of the list, effectively making it younger
3313 * this allows us to keep actively referenced vnodes in the list without having
3314 * to constantly remove and add to the list each time a vnode w/o a usecount is
3315 * referenced which costs us taking and dropping a global lock twice.
3317 #define UNAGE_THRESHHOLD 10
3320 vnode_getiocount(vnode_t vp
, int locked
, int vid
, int vflags
)
3322 int nodead
= vflags
& VNODE_NODEAD
;
3323 int nosusp
= vflags
& VNODE_NOSUSPEND
;
3330 * if it is a dead vnode with deadfs
3332 if (nodead
&& (vp
->v_lflag
& VL_DEAD
) && ((vp
->v_type
== VBAD
) || (vp
->v_data
== 0))) {
3338 * will return VL_DEAD ones
3340 if ((vp
->v_lflag
& (VL_SUSPENDED
| VL_DRAIN
| VL_TERMINATE
)) == 0 ) {
3344 * if suspended vnodes are to be failed
3346 if (nosusp
&& (vp
->v_lflag
& VL_SUSPENDED
)) {
3352 * if you are the owner of drain/suspend/termination , can acquire iocount
3353 * check for VL_TERMINATE; it does not set owner
3355 if ((vp
->v_lflag
& (VL_DRAIN
| VL_SUSPENDED
| VL_TERMINATE
)) &&
3356 (vp
->v_owner
== current_thread())) {
3359 if (vp
->v_lflag
& VL_TERMINATE
) {
3360 vp
->v_lflag
|= VL_TERMWANT
;
3362 msleep(&vp
->v_lflag
, &vp
->v_lock
, PVFS
, "vnode getiocount", 0);
3364 msleep(&vp
->v_iocount
, &vp
->v_lock
, PVFS
, "vnode_getiocount", 0);
3366 if (vid
!= vp
->v_id
) {
3371 if (++vp
->v_references
>= UNAGE_THRESHHOLD
) {
3372 vp
->v_references
= 0;
3373 vnode_list_remove(vp
);
3385 vnode_dropiocount (vnode_t vp
, int locked
)
3389 if (vp
->v_iocount
< 1)
3390 panic("vnode_dropiocount(%x): v_iocount < 1", vp
);
3396 if ((vp
->v_lflag
& (VL_DRAIN
| VL_SUSPENDED
)) && (vp
->v_iocount
<= 1))
3397 wakeup(&vp
->v_iocount
);
3405 vnode_reclaim(struct vnode
* vp
)
3407 vnode_reclaim_internal(vp
, 0, 0);
3412 vnode_reclaim_internal(struct vnode
* vp
, int locked
, int reuse
)
3419 if (vp
->v_lflag
& VL_TERMINATE
) {
3420 panic("vnode reclaim in progress");
3422 vp
->v_lflag
|= VL_TERMINATE
;
3424 if (vnode_drain(vp
)) {
3425 panic("vnode drain failed");
3429 isfifo
= (vp
->v_type
== VFIFO
);
3431 if (vp
->v_type
!= VBAD
)
3432 vgone(vp
); /* clean and reclaim the vnode */
3435 * give the vnode a new identity so
3436 * that vnode_getwithvid will fail
3437 * on any stale cache accesses
3441 struct fifoinfo
* fip
;
3443 fip
= vp
->v_fifoinfo
;
3444 vp
->v_fifoinfo
= NULL
;
3451 panic("vnode_reclaim_internal: cleaned vnode isn't");
3452 if (vp
->v_numoutput
)
3453 panic("vnode_reclaim_internal: Clean vnode has pending I/O's");
3454 if (UBCINFOEXISTS(vp
))
3455 panic("vnode_reclaim_internal: ubcinfo not cleaned");
3457 panic("vnode_reclaim_internal: vparent not removed");
3459 panic("vnode_reclaim_internal: vname not removed");
3463 vp
->v_lflag
&= ~VL_TERMINATE
;
3464 vp
->v_lflag
&= ~VL_DRAIN
;
3467 if (vp
->v_lflag
& VL_TERMWANT
) {
3468 vp
->v_lflag
&= ~VL_TERMWANT
;
3469 wakeup(&vp
->v_lflag
);
3471 if (!reuse
&& vp
->v_usecount
== 0)
3478 * The following api creates a vnode and associates all the parameter specified in vnode_fsparam
3479 * structure and returns a vnode handle with a reference. device aliasing is handled here so checkalias
3480 * is obsoleted by this.
3481 * vnode_create(int flavor, size_t size, void * param, vnode_t *vp)
3484 vnode_create(int flavor
, size_t size
, void *data
, vnode_t
*vpp
)
3491 struct componentname
*cnp
;
3492 struct vnode_fsparam
*param
= (struct vnode_fsparam
*)data
;
3494 if (flavor
== VNCREATE_FLAVOR
&& (size
== VCREATESIZE
) && param
) {
3495 if ( (error
= new_vnode(&vp
)) ) {
3498 dvp
= param
->vnfs_dvp
;
3499 cnp
= param
->vnfs_cnp
;
3501 vp
->v_op
= param
->vnfs_vops
;
3502 vp
->v_type
= param
->vnfs_vtype
;
3503 vp
->v_data
= param
->vnfs_fsnode
;
3506 if (param
->vnfs_markroot
)
3507 vp
->v_flag
|= VROOT
;
3508 if (param
->vnfs_marksystem
)
3509 vp
->v_flag
|= VSYSTEM
;
3510 else if (vp
->v_type
== VREG
) {
3512 * only non SYSTEM vp
3514 error
= ubc_info_init_withsize(vp
, param
->vnfs_filesize
);
3520 vp
->v_op
= dead_vnodeop_p
;
3524 vp
->v_lflag
|= VL_DEAD
;
3533 if (vp
->v_type
== VCHR
|| vp
->v_type
== VBLK
) {
3535 if ( (nvp
= checkalias(vp
, param
->vnfs_rdev
)) ) {
3537 * if checkalias returns a vnode, it will be locked
3539 * first get rid of the unneeded vnode we acquired
3542 vp
->v_op
= spec_vnodeop_p
;
3544 vp
->v_lflag
= VL_DEAD
;
3550 * switch to aliased vnode and finish
3555 vclean(vp
, 0, current_proc());
3556 vp
->v_op
= param
->vnfs_vops
;
3557 vp
->v_type
= param
->vnfs_vtype
;
3558 vp
->v_data
= param
->vnfs_fsnode
;
3561 insmntque(vp
, param
->vnfs_mp
);
3567 if (vp
->v_type
== VFIFO
) {
3568 struct fifoinfo
*fip
;
3570 MALLOC(fip
, struct fifoinfo
*,
3571 sizeof(*fip
), M_TEMP
, M_WAITOK
);
3572 bzero(fip
, sizeof(struct fifoinfo
));
3573 vp
->v_fifoinfo
= fip
;
3575 /* The file systems usually pass the address of the location where
3576 * where there store the vnode pointer. When we add the vnode in mount
3577 * point and name cache they are discoverable. So the file system node
3578 * will have the connection to vnode setup by then
3582 if (param
->vnfs_mp
) {
3583 if (param
->vnfs_mp
->mnt_kern_flag
& MNTK_LOCK_LOCAL
)
3584 vp
->v_flag
|= VLOCKLOCAL
;
3587 * enter in mount vnode list
3589 insmntque(vp
, param
->vnfs_mp
);
3591 #ifdef INTERIM_FSNODE_LOCK
3592 if (param
->vnfs_mp
->mnt_vtable
->vfc_threadsafe
== 0) {
3593 MALLOC_ZONE(vp
->v_unsafefs
, struct unsafe_fsnode
*,
3594 sizeof(struct unsafe_fsnode
), M_UNSAFEFS
, M_WAITOK
);
3595 vp
->v_unsafefs
->fsnode_count
= 0;
3596 vp
->v_unsafefs
->fsnodeowner
= (void *)NULL
;
3597 lck_mtx_init(&vp
->v_unsafefs
->fsnodelock
, vnode_lck_grp
, vnode_lck_attr
);
3599 #endif /* INTERIM_FSNODE_LOCK */
3601 if (dvp
&& vnode_ref(dvp
) == 0) {
3605 if (dvp
&& ((param
->vnfs_flags
& (VNFS_NOCACHE
| VNFS_CANTCACHE
)) == 0)) {
3607 * enter into name cache
3608 * we've got the info to enter it into the name cache now
3610 cache_enter(dvp
, vp
, cnp
);
3612 vp
->v_name
= vfs_addname(cnp
->cn_nameptr
, cnp
->cn_namelen
, cnp
->cn_hash
, 0);
3614 if ((param
->vnfs_flags
& VNFS_CANTCACHE
) == 0) {
3616 * this vnode is being created as cacheable in the name cache
3617 * this allows us to re-enter it in the cache
3619 vp
->v_flag
|= VNCACHEABLE
;
3621 if ((vp
->v_flag
& VSYSTEM
) && (vp
->v_type
!= VREG
))
3622 panic("incorrect vnode setup");
3631 vnode_addfsref(vnode_t vp
)
3634 if (vp
->v_lflag
& VNAMED_FSHASH
)
3635 panic("add_fsref: vp already has named reference");
3636 if ((vp
->v_freelist
.tqe_prev
!= (struct vnode
**)0xdeadb))
3637 panic("addfsref: vp on the free list\n");
3638 vp
->v_lflag
|= VNAMED_FSHASH
;
3644 vnode_removefsref(vnode_t vp
)
3647 if ((vp
->v_lflag
& VNAMED_FSHASH
) == 0)
3648 panic("remove_fsref: no named reference");
3649 vp
->v_lflag
&= ~VNAMED_FSHASH
;
3657 vfs_iterate(__unused
int flags
, int (*callout
)(mount_t
, void *), void *arg
)
3662 int count
, actualcount
, i
;
3665 count
= mount_getvfscnt();
3668 fsid_list
= (fsid_t
*)kalloc(count
* sizeof(fsid_t
));
3669 allocmem
= (void *)fsid_list
;
3671 actualcount
= mount_fillfsids(fsid_list
, count
);
3673 for (i
=0; i
< actualcount
; i
++) {
3675 /* obtain the mount point with iteration reference */
3676 mp
= mount_list_lookupby_fsid(&fsid_list
[i
], 0, 1);
3678 if(mp
== (struct mount
*)0)
3681 if (mp
->mnt_lflag
& (MNT_LDEAD
| MNT_LUNMOUNT
)) {
3689 /* iterate over all the vnodes */
3690 ret
= callout(mp
, arg
);
3696 case VFS_RETURNED_DONE
:
3697 if (ret
== VFS_RETURNED_DONE
) {
3703 case VFS_CLAIMED_DONE
:
3714 kfree(allocmem
, (count
* sizeof(fsid_t
)));
3719 * Update the vfsstatfs structure in the mountpoint.
3722 vfs_update_vfsstat(mount_t mp
, vfs_context_t ctx
)
3728 * Request the attributes we want to propagate into
3729 * the per-mount vfsstat structure.
3732 VFSATTR_WANTED(&va
, f_iosize
);
3733 VFSATTR_WANTED(&va
, f_blocks
);
3734 VFSATTR_WANTED(&va
, f_bfree
);
3735 VFSATTR_WANTED(&va
, f_bavail
);
3736 VFSATTR_WANTED(&va
, f_bused
);
3737 VFSATTR_WANTED(&va
, f_files
);
3738 VFSATTR_WANTED(&va
, f_ffree
);
3739 VFSATTR_WANTED(&va
, f_bsize
);
3740 VFSATTR_WANTED(&va
, f_fssubtype
);
3741 if ((error
= vfs_getattr(mp
, &va
, ctx
)) != 0) {
3742 KAUTH_DEBUG("STAT - filesystem returned error %d", error
);
3747 * Unpack into the per-mount structure.
3749 * We only overwrite these fields, which are likely to change:
3757 * And these which are not, but which the FS has no other way
3758 * of providing to us:
3764 if (VFSATTR_IS_SUPPORTED(&va
, f_bsize
)) {
3765 mp
->mnt_vfsstat
.f_bsize
= va
.f_bsize
;
3767 mp
->mnt_vfsstat
.f_bsize
= mp
->mnt_devblocksize
; /* default from the device block size */
3769 if (VFSATTR_IS_SUPPORTED(&va
, f_iosize
)) {
3770 mp
->mnt_vfsstat
.f_iosize
= va
.f_iosize
;
3772 mp
->mnt_vfsstat
.f_iosize
= 1024 * 1024; /* 1MB sensible I/O size */
3774 if (VFSATTR_IS_SUPPORTED(&va
, f_blocks
))
3775 mp
->mnt_vfsstat
.f_blocks
= va
.f_blocks
;
3776 if (VFSATTR_IS_SUPPORTED(&va
, f_bfree
))
3777 mp
->mnt_vfsstat
.f_bfree
= va
.f_bfree
;
3778 if (VFSATTR_IS_SUPPORTED(&va
, f_bavail
))
3779 mp
->mnt_vfsstat
.f_bavail
= va
.f_bavail
;
3780 if (VFSATTR_IS_SUPPORTED(&va
, f_bused
))
3781 mp
->mnt_vfsstat
.f_bused
= va
.f_bused
;
3782 if (VFSATTR_IS_SUPPORTED(&va
, f_files
))
3783 mp
->mnt_vfsstat
.f_files
= va
.f_files
;
3784 if (VFSATTR_IS_SUPPORTED(&va
, f_ffree
))
3785 mp
->mnt_vfsstat
.f_ffree
= va
.f_ffree
;
3787 /* this is unlikely to change, but has to be queried for */
3788 if (VFSATTR_IS_SUPPORTED(&va
, f_fssubtype
))
3789 mp
->mnt_vfsstat
.f_fssubtype
= va
.f_fssubtype
;
3795 mount_list_add(mount_t mp
)
3798 TAILQ_INSERT_TAIL(&mountlist
, mp
, mnt_list
);
3800 mount_list_unlock();
3804 mount_list_remove(mount_t mp
)
3807 TAILQ_REMOVE(&mountlist
, mp
, mnt_list
);
3809 mp
->mnt_list
.tqe_next
= 0;
3810 mp
->mnt_list
.tqe_prev
= 0;
3811 mount_list_unlock();
3815 mount_lookupby_volfsid(int volfs_id
, int withref
)
3817 mount_t cur_mount
= (mount_t
)0;
3821 TAILQ_FOREACH(mp
, &mountlist
, mnt_list
) {
3822 if (validfsnode(mp
) && mp
->mnt_vfsstat
.f_fsid
.val
[0] == volfs_id
) {
3825 if (mount_iterref(cur_mount
, 1)) {
3826 cur_mount
= (mount_t
)0;
3827 mount_list_unlock();
3834 mount_list_unlock();
3835 if (withref
&& (cur_mount
!= (mount_t
)0)) {
3837 if (vfs_busy(mp
, LK_NOWAIT
) != 0) {
3838 cur_mount
= (mount_t
)0;
3848 mount_list_lookupby_fsid(fsid
, locked
, withref
)
3853 mount_t retmp
= (mount_t
)0;
3858 TAILQ_FOREACH(mp
, &mountlist
, mnt_list
)
3859 if (mp
->mnt_vfsstat
.f_fsid
.val
[0] == fsid
->val
[0] &&
3860 mp
->mnt_vfsstat
.f_fsid
.val
[1] == fsid
->val
[1]) {
3863 if (mount_iterref(retmp
, 1))
3870 mount_list_unlock();
3875 vnode_lookup(const char *path
, int flags
, vnode_t
*vpp
, vfs_context_t context
)
3877 struct nameidata nd
;
3879 struct vfs_context context2
;
3880 vfs_context_t ctx
= context
;
3883 if (context
== NULL
) { /* XXX technically an error */
3884 context2
.vc_proc
= current_proc();
3885 context2
.vc_ucred
= kauth_cred_get();
3889 if (flags
& VNODE_LOOKUP_NOFOLLOW
)
3894 if (flags
& VNODE_LOOKUP_NOCROSSMOUNT
)
3895 ndflags
|= NOCROSSMOUNT
;
3896 if (flags
& VNODE_LOOKUP_DOWHITEOUT
)
3897 ndflags
|= DOWHITEOUT
;
3899 /* XXX AUDITVNPATH1 needed ? */
3900 NDINIT(&nd
, LOOKUP
, ndflags
, UIO_SYSSPACE
, CAST_USER_ADDR_T(path
), ctx
);
3902 if ((error
= namei(&nd
)))
3911 vnode_open(const char *path
, int fmode
, int cmode
, int flags
, vnode_t
*vpp
, vfs_context_t context
)
3913 struct nameidata nd
;
3915 struct vfs_context context2
;
3916 vfs_context_t ctx
= context
;
3919 if (context
== NULL
) { /* XXX technically an error */
3920 context2
.vc_proc
= current_proc();
3921 context2
.vc_ucred
= kauth_cred_get();
3925 if (flags
& VNODE_LOOKUP_NOFOLLOW
)
3930 if (flags
& VNODE_LOOKUP_NOCROSSMOUNT
)
3931 ndflags
|= NOCROSSMOUNT
;
3932 if (flags
& VNODE_LOOKUP_DOWHITEOUT
)
3933 ndflags
|= DOWHITEOUT
;
3935 /* XXX AUDITVNPATH1 needed ? */
3936 NDINIT(&nd
, LOOKUP
, ndflags
, UIO_SYSSPACE
, CAST_USER_ADDR_T(path
), ctx
);
3938 if ((error
= vn_open(&nd
, fmode
, cmode
)))
3947 vnode_close(vnode_t vp
, int flags
, vfs_context_t context
)
3954 p
= context
->vc_proc
;
3955 cred
= context
->vc_ucred
;
3958 cred
= kauth_cred_get();
3961 error
= vn_close(vp
, flags
, cred
, p
);
3967 vnode_size(vnode_t vp
, off_t
*sizep
, vfs_context_t ctx
)
3969 struct vnode_attr va
;
3973 VATTR_WANTED(&va
, va_data_size
);
3974 error
= vnode_getattr(vp
, &va
, ctx
);
3976 *sizep
= va
.va_data_size
;
3981 vnode_setsize(vnode_t vp
, off_t size
, int ioflag
, vfs_context_t ctx
)
3983 struct vnode_attr va
;
3986 VATTR_SET(&va
, va_data_size
, size
);
3987 va
.va_vaflags
= ioflag
& 0xffff;
3988 return(vnode_setattr(vp
, &va
, ctx
));
3992 vn_create(vnode_t dvp
, vnode_t
*vpp
, struct componentname
*cnp
, struct vnode_attr
*vap
, int flags
, vfs_context_t ctx
)
3994 kauth_acl_t oacl
, nacl
;
3997 vnode_t vp
= (vnode_t
)0;
4003 KAUTH_DEBUG("%p CREATE - '%s'", dvp
, cnp
->cn_nameptr
);
4006 * Handle ACL inheritance.
4008 if (!(flags
& VN_CREATE_NOINHERIT
) && vfs_extendedsecurity(dvp
->v_mount
)) {
4009 /* save the original filesec */
4010 if (VATTR_IS_ACTIVE(vap
, va_acl
)) {
4016 if ((error
= kauth_acl_inherit(dvp
,
4019 vap
->va_type
== VDIR
,
4021 KAUTH_DEBUG("%p CREATE - error %d processing inheritance", dvp
, error
);
4026 * If the generated ACL is NULL, then we can save ourselves some effort
4027 * by clearing the active bit.
4030 VATTR_CLEAR_ACTIVE(vap
, va_acl
);
4032 VATTR_SET(vap
, va_acl
, nacl
);
4037 * Check and default new attributes.
4038 * This will set va_uid, va_gid, va_mode and va_create_time at least, if the caller
4039 * hasn't supplied them.
4041 if ((error
= vnode_authattr_new(dvp
, vap
, flags
& VN_CREATE_NOAUTH
, ctx
)) != 0) {
4042 KAUTH_DEBUG("%p CREATE - error %d handing/defaulting attributes", dvp
, error
);
4048 * Create the requested node.
4050 switch(vap
->va_type
) {
4052 error
= VNOP_CREATE(dvp
, vpp
, cnp
, vap
, ctx
);
4055 error
= VNOP_MKDIR(dvp
, vpp
, cnp
, vap
, ctx
);
4061 error
= VNOP_MKNOD(dvp
, vpp
, cnp
, vap
, ctx
);
4064 panic("vnode_create: unknown vtype %d", vap
->va_type
);
4067 KAUTH_DEBUG("%p CREATE - error %d returned by filesystem", dvp
, error
);
4073 * If some of the requested attributes weren't handled by the VNOP,
4074 * use our fallback code.
4076 if (!VATTR_ALL_SUPPORTED(vap
) && *vpp
) {
4077 KAUTH_DEBUG(" CREATE - doing fallback with ACL %p", vap
->va_acl
);
4078 error
= vnode_setattr_fallback(*vpp
, vap
, ctx
);
4080 if ((error
!= 0 ) && (vp
!= (vnode_t
)0)) {
4087 * If the caller supplied a filesec in vap, it has been replaced
4088 * now by the post-inheritance copy. We need to put the original back
4089 * and free the inherited product.
4092 VATTR_SET(vap
, va_acl
, oacl
);
4094 VATTR_CLEAR_ACTIVE(vap
, va_acl
);
4097 kauth_acl_free(nacl
);
4102 static kauth_scope_t vnode_scope
;
4103 static int vnode_authorize_callback(kauth_cred_t credential
, __unused
void *idata
, kauth_action_t action
,
4104 uintptr_t arg0
, uintptr_t arg1
, uintptr_t arg2
, uintptr_t arg3
);
4106 typedef struct _vnode_authorize_context
{
4108 struct vnode_attr
*vap
;
4110 struct vnode_attr
*dvap
;
4114 #define _VAC_IS_OWNER (1<<0)
4115 #define _VAC_IN_GROUP (1<<1)
4116 #define _VAC_IS_DIR_OWNER (1<<2)
4117 #define _VAC_IN_DIR_GROUP (1<<3)
4121 vnode_authorize_init(void)
4123 vnode_scope
= kauth_register_scope(KAUTH_SCOPE_VNODE
, vnode_authorize_callback
, NULL
);
4127 * Authorize an operation on a vnode.
4129 * This is KPI, but here because it needs vnode_scope.
4132 vnode_authorize(vnode_t vp
, vnode_t dvp
, kauth_action_t action
, vfs_context_t context
)
4137 * We can't authorize against a dead vnode; allow all operations through so that
4138 * the correct error can be returned.
4140 if (vp
->v_type
== VBAD
)
4144 result
= kauth_authorize_action(vnode_scope
, vfs_context_ucred(context
), action
,
4145 (uintptr_t)context
, (uintptr_t)vp
, (uintptr_t)dvp
, (uintptr_t)&error
);
4146 if (result
== EPERM
) /* traditional behaviour */
4148 /* did the lower layers give a better error return? */
4149 if ((result
!= 0) && (error
!= 0))
4155 * Test for vnode immutability.
4157 * The 'append' flag is set when the authorization request is constrained
4158 * to operations which only request the right to append to a file.
4160 * The 'ignore' flag is set when an operation modifying the immutability flags
4161 * is being authorized. We check the system securelevel to determine which
4162 * immutability flags we can ignore.
4165 vnode_immutable(struct vnode_attr
*vap
, int append
, int ignore
)
4169 /* start with all bits precluding the operation */
4170 mask
= IMMUTABLE
| APPEND
;
4172 /* if appending only, remove the append-only bits */
4176 /* ignore only set when authorizing flags changes */
4178 if (securelevel
<= 0) {
4179 /* in insecure state, flags do not inhibit changes */
4182 /* in secure state, user flags don't inhibit */
4183 mask
&= ~(UF_IMMUTABLE
| UF_APPEND
);
4186 KAUTH_DEBUG("IMMUTABLE - file flags 0x%x mask 0x%x append = %d ignore = %d", vap
->va_flags
, mask
, append
, ignore
);
4187 if ((vap
->va_flags
& mask
) != 0)
4193 vauth_node_owner(struct vnode_attr
*vap
, kauth_cred_t cred
)
4197 /* default assumption is not-owner */
4201 * If the filesystem has given us a UID, we treat this as authoritative.
4203 if (vap
&& VATTR_IS_SUPPORTED(vap
, va_uid
)) {
4204 result
= (vap
->va_uid
== kauth_cred_getuid(cred
)) ? 1 : 0;
4206 /* we could test the owner UUID here if we had a policy for it */
4212 vauth_node_group(struct vnode_attr
*vap
, kauth_cred_t cred
, int *ismember
)
4220 /* the caller is expected to have asked the filesystem for a group at some point */
4221 if (vap
&& VATTR_IS_SUPPORTED(vap
, va_gid
)) {
4222 error
= kauth_cred_ismember_gid(cred
, vap
->va_gid
, &result
);
4224 /* we could test the group UUID here if we had a policy for it */
4232 vauth_file_owner(vauth_ctx vcp
)
4236 if (vcp
->flags_valid
& _VAC_IS_OWNER
) {
4237 result
= (vcp
->flags
& _VAC_IS_OWNER
) ? 1 : 0;
4239 result
= vauth_node_owner(vcp
->vap
, vcp
->ctx
->vc_ucred
);
4241 /* cache our result */
4242 vcp
->flags_valid
|= _VAC_IS_OWNER
;
4244 vcp
->flags
|= _VAC_IS_OWNER
;
4246 vcp
->flags
&= ~_VAC_IS_OWNER
;
4253 vauth_file_ingroup(vauth_ctx vcp
, int *ismember
)
4257 if (vcp
->flags_valid
& _VAC_IN_GROUP
) {
4258 *ismember
= (vcp
->flags
& _VAC_IN_GROUP
) ? 1 : 0;
4261 error
= vauth_node_group(vcp
->vap
, vcp
->ctx
->vc_ucred
, ismember
);
4264 /* cache our result */
4265 vcp
->flags_valid
|= _VAC_IN_GROUP
;
4267 vcp
->flags
|= _VAC_IN_GROUP
;
4269 vcp
->flags
&= ~_VAC_IN_GROUP
;
4278 vauth_dir_owner(vauth_ctx vcp
)
4282 if (vcp
->flags_valid
& _VAC_IS_DIR_OWNER
) {
4283 result
= (vcp
->flags
& _VAC_IS_DIR_OWNER
) ? 1 : 0;
4285 result
= vauth_node_owner(vcp
->dvap
, vcp
->ctx
->vc_ucred
);
4287 /* cache our result */
4288 vcp
->flags_valid
|= _VAC_IS_DIR_OWNER
;
4290 vcp
->flags
|= _VAC_IS_DIR_OWNER
;
4292 vcp
->flags
&= ~_VAC_IS_DIR_OWNER
;
4299 vauth_dir_ingroup(vauth_ctx vcp
, int *ismember
)
4303 if (vcp
->flags_valid
& _VAC_IN_DIR_GROUP
) {
4304 *ismember
= (vcp
->flags
& _VAC_IN_DIR_GROUP
) ? 1 : 0;
4307 error
= vauth_node_group(vcp
->dvap
, vcp
->ctx
->vc_ucred
, ismember
);
4310 /* cache our result */
4311 vcp
->flags_valid
|= _VAC_IN_DIR_GROUP
;
4313 vcp
->flags
|= _VAC_IN_DIR_GROUP
;
4315 vcp
->flags
&= ~_VAC_IN_DIR_GROUP
;
4323 * Test the posix permissions in (vap) to determine whether (credential)
4324 * may perform (action)
4327 vnode_authorize_posix(vauth_ctx vcp
, int action
, int on_dir
)
4329 struct vnode_attr
*vap
;
4330 int needed
, error
, owner_ok
, group_ok
, world_ok
, ismember
;
4331 #ifdef KAUTH_DEBUG_ENABLE
4333 # define _SETWHERE(c) where = c;
4335 # define _SETWHERE(c)
4338 /* checking file or directory? */
4348 * We want to do as little work here as possible. So first we check
4349 * which sets of permissions grant us the access we need, and avoid checking
4350 * whether specific permissions grant access when more generic ones would.
4353 /* owner permissions */
4357 if (action
& VWRITE
)
4361 owner_ok
= (needed
& vap
->va_mode
) == needed
;
4363 /* group permissions */
4367 if (action
& VWRITE
)
4371 group_ok
= (needed
& vap
->va_mode
) == needed
;
4373 /* world permissions */
4377 if (action
& VWRITE
)
4381 world_ok
= (needed
& vap
->va_mode
) == needed
;
4383 /* If granted/denied by all three, we're done */
4384 if (owner_ok
&& group_ok
&& world_ok
) {
4388 if (!owner_ok
&& !group_ok
&& !world_ok
) {
4394 /* Check ownership (relatively cheap) */
4395 if ((on_dir
&& vauth_dir_owner(vcp
)) ||
4396 (!on_dir
&& vauth_file_owner(vcp
))) {
4403 /* Not owner; if group and world both grant it we're done */
4404 if (group_ok
&& world_ok
) {
4405 _SETWHERE("group/world");
4408 if (!group_ok
&& !world_ok
) {
4409 _SETWHERE("group/world");
4414 /* Check group membership (most expensive) */
4417 error
= vauth_dir_ingroup(vcp
, &ismember
);
4419 error
= vauth_file_ingroup(vcp
, &ismember
);
4430 /* Not owner, not in group, use world result */
4438 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",
4439 vcp
->vp
, (error
== 0) ? "ALLOWED" : "DENIED", where
,
4440 (action
& VREAD
) ? "r" : "-",
4441 (action
& VWRITE
) ? "w" : "-",
4442 (action
& VEXEC
) ? "x" : "-",
4444 (vap
->va_mode
& S_IRUSR
) ? "r" : "-",
4445 (vap
->va_mode
& S_IWUSR
) ? "w" : "-",
4446 (vap
->va_mode
& S_IXUSR
) ? "x" : "-",
4447 (vap
->va_mode
& S_IRGRP
) ? "r" : "-",
4448 (vap
->va_mode
& S_IWGRP
) ? "w" : "-",
4449 (vap
->va_mode
& S_IXGRP
) ? "x" : "-",
4450 (vap
->va_mode
& S_IROTH
) ? "r" : "-",
4451 (vap
->va_mode
& S_IWOTH
) ? "w" : "-",
4452 (vap
->va_mode
& S_IXOTH
) ? "x" : "-",
4453 kauth_cred_getuid(vcp
->ctx
->vc_ucred
),
4454 on_dir
? vcp
->dvap
->va_uid
: vcp
->vap
->va_uid
,
4455 on_dir
? vcp
->dvap
->va_gid
: vcp
->vap
->va_gid
);
4460 * Authorize the deletion of the node vp from the directory dvp.
4463 * - Neither the node nor the directory are immutable.
4464 * - The user is not the superuser.
4466 * Deletion is not permitted if the directory is sticky and the caller is not owner of the
4467 * node or directory.
4469 * If either the node grants DELETE, or the directory grants DELETE_CHILD, the node may be
4470 * deleted. If neither denies the permission, and the caller has Posix write access to the
4471 * directory, then the node may be deleted.
4474 vnode_authorize_delete(vauth_ctx vcp
)
4476 struct vnode_attr
*vap
= vcp
->vap
;
4477 struct vnode_attr
*dvap
= vcp
->dvap
;
4478 kauth_cred_t cred
= vcp
->ctx
->vc_ucred
;
4479 struct kauth_acl_eval eval
;
4480 int error
, delete_denied
, delete_child_denied
, ismember
;
4482 /* check the ACL on the directory */
4483 delete_child_denied
= 0;
4484 if (VATTR_IS_NOT(dvap
, va_acl
, NULL
)) {
4485 eval
.ae_requested
= KAUTH_VNODE_DELETE_CHILD
;
4486 eval
.ae_acl
= &dvap
->va_acl
->acl_ace
[0];
4487 eval
.ae_count
= dvap
->va_acl
->acl_entrycount
;
4488 eval
.ae_options
= 0;
4489 if (vauth_dir_owner(vcp
))
4490 eval
.ae_options
|= KAUTH_AEVAL_IS_OWNER
;
4491 if ((error
= vauth_dir_ingroup(vcp
, &ismember
)) != 0)
4494 eval
.ae_options
|= KAUTH_AEVAL_IN_GROUP
;
4495 eval
.ae_exp_gall
= KAUTH_VNODE_GENERIC_ALL_BITS
;
4496 eval
.ae_exp_gread
= KAUTH_VNODE_GENERIC_READ_BITS
;
4497 eval
.ae_exp_gwrite
= KAUTH_VNODE_GENERIC_WRITE_BITS
;
4498 eval
.ae_exp_gexec
= KAUTH_VNODE_GENERIC_EXECUTE_BITS
;
4500 error
= kauth_acl_evaluate(cred
, &eval
);
4503 KAUTH_DEBUG("%p ERROR during ACL processing - %d", vcp
->vp
, error
);
4506 if (eval
.ae_result
== KAUTH_RESULT_DENY
)
4507 delete_child_denied
= 1;
4508 if (eval
.ae_result
== KAUTH_RESULT_ALLOW
) {
4509 KAUTH_DEBUG("%p ALLOWED - granted by directory ACL", vcp
->vp
);
4514 /* check the ACL on the node */
4516 if (VATTR_IS_NOT(vap
, va_acl
, NULL
)) {
4517 eval
.ae_requested
= KAUTH_VNODE_DELETE
;
4518 eval
.ae_acl
= &vap
->va_acl
->acl_ace
[0];
4519 eval
.ae_count
= vap
->va_acl
->acl_entrycount
;
4520 eval
.ae_options
= 0;
4521 if (vauth_file_owner(vcp
))
4522 eval
.ae_options
|= KAUTH_AEVAL_IS_OWNER
;
4523 if ((error
= vauth_file_ingroup(vcp
, &ismember
)) != 0)
4526 eval
.ae_options
|= KAUTH_AEVAL_IN_GROUP
;
4527 eval
.ae_exp_gall
= KAUTH_VNODE_GENERIC_ALL_BITS
;
4528 eval
.ae_exp_gread
= KAUTH_VNODE_GENERIC_READ_BITS
;
4529 eval
.ae_exp_gwrite
= KAUTH_VNODE_GENERIC_WRITE_BITS
;
4530 eval
.ae_exp_gexec
= KAUTH_VNODE_GENERIC_EXECUTE_BITS
;
4532 if ((error
= kauth_acl_evaluate(cred
, &eval
)) != 0) {
4533 KAUTH_DEBUG("%p ERROR during ACL processing - %d", vcp
->vp
, error
);
4536 if (eval
.ae_result
== KAUTH_RESULT_DENY
)
4538 if (eval
.ae_result
== KAUTH_RESULT_ALLOW
) {
4539 KAUTH_DEBUG("%p ALLOWED - granted by file ACL", vcp
->vp
);
4544 /* if denied by ACL on directory or node, return denial */
4545 if (delete_denied
|| delete_child_denied
) {
4546 KAUTH_DEBUG("%p ALLOWED - denied by ACL", vcp
->vp
);
4550 /* enforce sticky bit behaviour */
4551 if ((dvap
->va_mode
& S_ISTXT
) && !vauth_file_owner(vcp
) && !vauth_dir_owner(vcp
)) {
4552 KAUTH_DEBUG("%p DENIED - sticky bit rules (user %d file %d dir %d)",
4553 vcp
->vp
, cred
->cr_uid
, vap
->va_uid
, dvap
->va_uid
);
4557 /* check the directory */
4558 if ((error
= vnode_authorize_posix(vcp
, VWRITE
, 1 /* on_dir */)) != 0) {
4559 KAUTH_DEBUG("%p ALLOWED - granted by posix permisssions", vcp
->vp
);
4563 /* not denied, must be OK */
4569 * Authorize an operation based on the node's attributes.
4572 vnode_authorize_simple(vauth_ctx vcp
, kauth_ace_rights_t acl_rights
, kauth_ace_rights_t preauth_rights
)
4574 struct vnode_attr
*vap
= vcp
->vap
;
4575 kauth_cred_t cred
= vcp
->ctx
->vc_ucred
;
4576 struct kauth_acl_eval eval
;
4577 int error
, ismember
;
4578 mode_t posix_action
;
4581 * If we are the file owner, we automatically have some rights.
4583 * Do we need to expand this to support group ownership?
4585 if (vauth_file_owner(vcp
))
4586 acl_rights
&= ~(KAUTH_VNODE_WRITE_SECURITY
);
4589 * If we are checking both TAKE_OWNERSHIP and WRITE_SECURITY, we can
4590 * mask the latter. If TAKE_OWNERSHIP is requested the caller is about to
4591 * change ownership to themselves, and WRITE_SECURITY is implicitly
4592 * granted to the owner. We need to do this because at this point
4593 * WRITE_SECURITY may not be granted as the caller is not currently
4596 if ((acl_rights
& KAUTH_VNODE_TAKE_OWNERSHIP
) &&
4597 (acl_rights
& KAUTH_VNODE_WRITE_SECURITY
))
4598 acl_rights
&= ~KAUTH_VNODE_WRITE_SECURITY
;
4600 if (acl_rights
== 0) {
4601 KAUTH_DEBUG("%p ALLOWED - implicit or no rights required", vcp
->vp
);
4605 /* if we have an ACL, evaluate it */
4606 if (VATTR_IS_NOT(vap
, va_acl
, NULL
)) {
4607 eval
.ae_requested
= acl_rights
;
4608 eval
.ae_acl
= &vap
->va_acl
->acl_ace
[0];
4609 eval
.ae_count
= vap
->va_acl
->acl_entrycount
;
4610 eval
.ae_options
= 0;
4611 if (vauth_file_owner(vcp
))
4612 eval
.ae_options
|= KAUTH_AEVAL_IS_OWNER
;
4613 if ((error
= vauth_file_ingroup(vcp
, &ismember
)) != 0)
4616 eval
.ae_options
|= KAUTH_AEVAL_IN_GROUP
;
4617 eval
.ae_exp_gall
= KAUTH_VNODE_GENERIC_ALL_BITS
;
4618 eval
.ae_exp_gread
= KAUTH_VNODE_GENERIC_READ_BITS
;
4619 eval
.ae_exp_gwrite
= KAUTH_VNODE_GENERIC_WRITE_BITS
;
4620 eval
.ae_exp_gexec
= KAUTH_VNODE_GENERIC_EXECUTE_BITS
;
4622 if ((error
= kauth_acl_evaluate(cred
, &eval
)) != 0) {
4623 KAUTH_DEBUG("%p ERROR during ACL processing - %d", vcp
->vp
, error
);
4627 if (eval
.ae_result
== KAUTH_RESULT_DENY
) {
4628 KAUTH_DEBUG("%p DENIED - by ACL", vcp
->vp
);
4629 return(EACCES
); /* deny, deny, counter-allege */
4631 if (eval
.ae_result
== KAUTH_RESULT_ALLOW
) {
4632 KAUTH_DEBUG("%p ALLOWED - all rights granted by ACL", vcp
->vp
);
4635 /* fall through and evaluate residual rights */
4637 /* no ACL, everything is residual */
4638 eval
.ae_residual
= acl_rights
;
4642 * Grant residual rights that have been pre-authorized.
4644 eval
.ae_residual
&= ~preauth_rights
;
4647 * We grant WRITE_ATTRIBUTES to the owner if it hasn't been denied.
4649 if (vauth_file_owner(vcp
))
4650 eval
.ae_residual
&= ~KAUTH_VNODE_WRITE_ATTRIBUTES
;
4652 if (eval
.ae_residual
== 0) {
4653 KAUTH_DEBUG("%p ALLOWED - rights already authorized", vcp
->vp
);
4658 * Bail if we have residual rights that can't be granted by posix permissions,
4659 * or aren't presumed granted at this point.
4661 * XXX these can be collapsed for performance
4663 if (eval
.ae_residual
& KAUTH_VNODE_CHANGE_OWNER
) {
4664 KAUTH_DEBUG("%p DENIED - CHANGE_OWNER not permitted", vcp
->vp
);
4667 if (eval
.ae_residual
& KAUTH_VNODE_WRITE_SECURITY
) {
4668 KAUTH_DEBUG("%p DENIED - WRITE_SECURITY not permitted", vcp
->vp
);
4673 if (eval
.ae_residual
& KAUTH_VNODE_DELETE
)
4674 panic("vnode_authorize: can't be checking delete permission here");
4678 * Compute the fallback posix permissions that will satisfy the remaining
4682 if (eval
.ae_residual
& (KAUTH_VNODE_READ_DATA
|
4683 KAUTH_VNODE_LIST_DIRECTORY
|
4684 KAUTH_VNODE_READ_EXTATTRIBUTES
))
4685 posix_action
|= VREAD
;
4686 if (eval
.ae_residual
& (KAUTH_VNODE_WRITE_DATA
|
4687 KAUTH_VNODE_ADD_FILE
|
4688 KAUTH_VNODE_ADD_SUBDIRECTORY
|
4689 KAUTH_VNODE_DELETE_CHILD
|
4690 KAUTH_VNODE_WRITE_ATTRIBUTES
|
4691 KAUTH_VNODE_WRITE_EXTATTRIBUTES
))
4692 posix_action
|= VWRITE
;
4693 if (eval
.ae_residual
& (KAUTH_VNODE_EXECUTE
|
4694 KAUTH_VNODE_SEARCH
))
4695 posix_action
|= VEXEC
;
4697 if (posix_action
!= 0) {
4698 return(vnode_authorize_posix(vcp
, posix_action
, 0 /* !on_dir */));
4700 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",
4702 (eval
.ae_residual
& KAUTH_VNODE_READ_DATA
)
4703 ? vnode_isdir(vcp
->vp
) ? " LIST_DIRECTORY" : " READ_DATA" : "",
4704 (eval
.ae_residual
& KAUTH_VNODE_WRITE_DATA
)
4705 ? vnode_isdir(vcp
->vp
) ? " ADD_FILE" : " WRITE_DATA" : "",
4706 (eval
.ae_residual
& KAUTH_VNODE_EXECUTE
)
4707 ? vnode_isdir(vcp
->vp
) ? " SEARCH" : " EXECUTE" : "",
4708 (eval
.ae_residual
& KAUTH_VNODE_DELETE
)
4710 (eval
.ae_residual
& KAUTH_VNODE_APPEND_DATA
)
4711 ? vnode_isdir(vcp
->vp
) ? " ADD_SUBDIRECTORY" : " APPEND_DATA" : "",
4712 (eval
.ae_residual
& KAUTH_VNODE_DELETE_CHILD
)
4713 ? " DELETE_CHILD" : "",
4714 (eval
.ae_residual
& KAUTH_VNODE_READ_ATTRIBUTES
)
4715 ? " READ_ATTRIBUTES" : "",
4716 (eval
.ae_residual
& KAUTH_VNODE_WRITE_ATTRIBUTES
)
4717 ? " WRITE_ATTRIBUTES" : "",
4718 (eval
.ae_residual
& KAUTH_VNODE_READ_EXTATTRIBUTES
)
4719 ? " READ_EXTATTRIBUTES" : "",
4720 (eval
.ae_residual
& KAUTH_VNODE_WRITE_EXTATTRIBUTES
)
4721 ? " WRITE_EXTATTRIBUTES" : "",
4722 (eval
.ae_residual
& KAUTH_VNODE_READ_SECURITY
)
4723 ? " READ_SECURITY" : "",
4724 (eval
.ae_residual
& KAUTH_VNODE_WRITE_SECURITY
)
4725 ? " WRITE_SECURITY" : "",
4726 (eval
.ae_residual
& KAUTH_VNODE_CHECKIMMUTABLE
)
4727 ? " CHECKIMMUTABLE" : "",
4728 (eval
.ae_residual
& KAUTH_VNODE_CHANGE_OWNER
)
4729 ? " CHANGE_OWNER" : "");
4733 * Lack of required Posix permissions implies no reason to deny access.
4739 * Check for file immutability.
4742 vnode_authorize_checkimmutable(vnode_t vp
, struct vnode_attr
*vap
, int rights
, int ignore
)
4749 * Perform immutability checks for operations that change data.
4751 * Sockets, fifos and devices require special handling.
4753 switch(vp
->v_type
) {
4759 * Writing to these nodes does not change the filesystem data,
4760 * so forget that it's being tried.
4762 rights
&= ~KAUTH_VNODE_WRITE_DATA
;
4769 if (rights
& KAUTH_VNODE_WRITE_RIGHTS
) {
4771 /* check per-filesystem options if possible */
4772 mp
= vnode_mount(vp
);
4775 /* check for no-EA filesystems */
4776 if ((rights
& KAUTH_VNODE_WRITE_EXTATTRIBUTES
) &&
4777 (vfs_flags(mp
) & MNT_NOUSERXATTR
)) {
4778 KAUTH_DEBUG("%p DENIED - filesystem disallowed extended attributes", vp
);
4779 error
= EACCES
; /* User attributes disabled */
4784 /* check for file immutability */
4786 if (vp
->v_type
== VDIR
) {
4787 if ((rights
& (KAUTH_VNODE_ADD_FILE
| KAUTH_VNODE_ADD_SUBDIRECTORY
)) == rights
)
4790 if ((rights
& KAUTH_VNODE_APPEND_DATA
) == rights
)
4793 if ((error
= vnode_immutable(vap
, append
, ignore
)) != 0) {
4794 KAUTH_DEBUG("%p DENIED - file is immutable", vp
);
4803 * Handle authorization actions for filesystems that advertise that the server will
4807 vnode_authorize_opaque(vnode_t vp
, int *resultp
, kauth_action_t action
, vfs_context_t ctx
)
4812 * If the vp is a device node, socket or FIFO it actually represents a local
4813 * endpoint, so we need to handle it locally.
4815 switch(vp
->v_type
) {
4826 * In the advisory request case, if the filesystem doesn't think it's reliable
4827 * we will attempt to formulate a result ourselves based on VNOP_GETATTR data.
4829 if ((action
& KAUTH_VNODE_ACCESS
) && !vfs_authopaqueaccess(vnode_mount(vp
)))
4833 * Let the filesystem have a say in the matter. It's OK for it to not implemnent
4834 * VNOP_ACCESS, as most will authorise inline with the actual request.
4836 if ((error
= VNOP_ACCESS(vp
, action
, ctx
)) != ENOTSUP
) {
4838 KAUTH_DEBUG("%p DENIED - opaque filesystem VNOP_ACCESS denied access", vp
);
4843 * Typically opaque filesystems do authorisation in-line, but exec is a special case. In
4844 * order to be reasonably sure that exec will be permitted, we try a bit harder here.
4846 if ((action
& KAUTH_VNODE_EXECUTE
) && vnode_isreg(vp
)) {
4847 /* try a VNOP_OPEN for readonly access */
4848 if ((error
= VNOP_OPEN(vp
, FREAD
, ctx
)) != 0) {
4850 KAUTH_DEBUG("%p DENIED - EXECUTE denied because file could not be opened readonly", vp
);
4853 VNOP_CLOSE(vp
, FREAD
, ctx
);
4857 * We don't have any reason to believe that the request has to be denied at this point,
4858 * so go ahead and allow it.
4861 KAUTH_DEBUG("%p ALLOWED - bypassing access check for non-local filesystem", vp
);
4866 vnode_authorize_callback(__unused kauth_cred_t unused_cred
, __unused
void *idata
, kauth_action_t action
,
4867 uintptr_t arg0
, uintptr_t arg1
, uintptr_t arg2
, uintptr_t arg3
)
4869 struct _vnode_authorize_context auth_context
;
4874 kauth_ace_rights_t rights
;
4875 struct vnode_attr va
, dva
;
4880 vcp
= &auth_context
;
4881 ctx
= vcp
->ctx
= (vfs_context_t
)arg0
;
4882 vp
= vcp
->vp
= (vnode_t
)arg1
;
4883 dvp
= vcp
->dvp
= (vnode_t
)arg2
;
4884 errorp
= (int *)arg3
;
4885 /* note that we authorize against the context, not the passed cred (the same thing anyway) */
4886 cred
= ctx
->vc_ucred
;
4893 vcp
->flags
= vcp
->flags_valid
= 0;
4896 if ((ctx
== NULL
) || (vp
== NULL
) || (cred
== NULL
))
4897 panic("vnode_authorize: bad arguments (context %p vp %p cred %p)", ctx
, vp
, cred
);
4900 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)",
4901 vp
, vfs_context_proc(ctx
)->p_comm
,
4902 (action
& KAUTH_VNODE_ACCESS
) ? "access" : "auth",
4903 (action
& KAUTH_VNODE_READ_DATA
) ? vnode_isdir(vp
) ? " LIST_DIRECTORY" : " READ_DATA" : "",
4904 (action
& KAUTH_VNODE_WRITE_DATA
) ? vnode_isdir(vp
) ? " ADD_FILE" : " WRITE_DATA" : "",
4905 (action
& KAUTH_VNODE_EXECUTE
) ? vnode_isdir(vp
) ? " SEARCH" : " EXECUTE" : "",
4906 (action
& KAUTH_VNODE_DELETE
) ? " DELETE" : "",
4907 (action
& KAUTH_VNODE_APPEND_DATA
) ? vnode_isdir(vp
) ? " ADD_SUBDIRECTORY" : " APPEND_DATA" : "",
4908 (action
& KAUTH_VNODE_DELETE_CHILD
) ? " DELETE_CHILD" : "",
4909 (action
& KAUTH_VNODE_READ_ATTRIBUTES
) ? " READ_ATTRIBUTES" : "",
4910 (action
& KAUTH_VNODE_WRITE_ATTRIBUTES
) ? " WRITE_ATTRIBUTES" : "",
4911 (action
& KAUTH_VNODE_READ_EXTATTRIBUTES
) ? " READ_EXTATTRIBUTES" : "",
4912 (action
& KAUTH_VNODE_WRITE_EXTATTRIBUTES
) ? " WRITE_EXTATTRIBUTES" : "",
4913 (action
& KAUTH_VNODE_READ_SECURITY
) ? " READ_SECURITY" : "",
4914 (action
& KAUTH_VNODE_WRITE_SECURITY
) ? " WRITE_SECURITY" : "",
4915 (action
& KAUTH_VNODE_CHANGE_OWNER
) ? " CHANGE_OWNER" : "",
4916 (action
& KAUTH_VNODE_NOIMMUTABLE
) ? " (noimmutable)" : "",
4917 vnode_isdir(vp
) ? "directory" : "file",
4918 vp
->v_name
? vp
->v_name
: "<NULL>", action
, vp
, dvp
);
4921 * Extract the control bits from the action, everything else is
4924 noimmutable
= (action
& KAUTH_VNODE_NOIMMUTABLE
) ? 1 : 0;
4925 rights
= action
& ~(KAUTH_VNODE_ACCESS
| KAUTH_VNODE_NOIMMUTABLE
);
4927 if (rights
& KAUTH_VNODE_DELETE
) {
4930 panic("vnode_authorize: KAUTH_VNODE_DELETE test requires a directory");
4937 * Check for read-only filesystems.
4939 if ((rights
& KAUTH_VNODE_WRITE_RIGHTS
) &&
4940 (vp
->v_mount
->mnt_flag
& MNT_RDONLY
) &&
4941 ((vp
->v_type
== VREG
) || (vp
->v_type
== VDIR
) ||
4942 (vp
->v_type
== VLNK
) || (vp
->v_type
== VCPLX
) ||
4943 (rights
& KAUTH_VNODE_DELETE
) || (rights
& KAUTH_VNODE_DELETE_CHILD
))) {
4949 * Check for noexec filesystems.
4951 if ((rights
& KAUTH_VNODE_EXECUTE
) && vnode_isreg(vp
) && (vp
->v_mount
->mnt_flag
& MNT_NOEXEC
)) {
4957 * Handle cases related to filesystems with non-local enforcement.
4958 * This call can return 0, in which case we will fall through to perform a
4959 * check based on VNOP_GETATTR data. Otherwise it returns 1 and sets
4960 * an appropriate result, at which point we can return immediately.
4962 if (vfs_authopaque(vp
->v_mount
) && vnode_authorize_opaque(vp
, &result
, action
, ctx
))
4966 * Get vnode attributes and extended security information for the vnode
4967 * and directory if required.
4969 VATTR_WANTED(&va
, va_mode
);
4970 VATTR_WANTED(&va
, va_uid
);
4971 VATTR_WANTED(&va
, va_gid
);
4972 VATTR_WANTED(&va
, va_flags
);
4973 VATTR_WANTED(&va
, va_acl
);
4974 if ((result
= vnode_getattr(vp
, &va
, ctx
)) != 0) {
4975 KAUTH_DEBUG("%p ERROR - failed to get vnode attributes - %d", vp
, result
);
4979 VATTR_WANTED(&dva
, va_mode
);
4980 VATTR_WANTED(&dva
, va_uid
);
4981 VATTR_WANTED(&dva
, va_gid
);
4982 VATTR_WANTED(&dva
, va_flags
);
4983 VATTR_WANTED(&dva
, va_acl
);
4984 if ((result
= vnode_getattr(dvp
, &dva
, ctx
)) != 0) {
4985 KAUTH_DEBUG("%p ERROR - failed to get directory vnode attributes - %d", vp
, result
);
4991 * If the vnode is an extended attribute data vnode (eg. a resource fork), *_DATA becomes
4994 if (S_ISXATTR(va
.va_mode
)) {
4995 if (rights
& KAUTH_VNODE_READ_DATA
) {
4996 rights
&= ~KAUTH_VNODE_READ_DATA
;
4997 rights
|= KAUTH_VNODE_READ_EXTATTRIBUTES
;
4999 if (rights
& KAUTH_VNODE_WRITE_DATA
) {
5000 rights
&= ~KAUTH_VNODE_WRITE_DATA
;
5001 rights
|= KAUTH_VNODE_WRITE_EXTATTRIBUTES
;
5006 * Check for immutability.
5008 * In the deletion case, parent directory immutability vetoes specific
5011 if ((result
= vnode_authorize_checkimmutable(vp
, &va
, rights
, noimmutable
)) != 0)
5013 if ((rights
& KAUTH_VNODE_DELETE
) &&
5014 ((result
= vnode_authorize_checkimmutable(dvp
, &dva
, KAUTH_VNODE_DELETE_CHILD
, 0)) != 0))
5018 * Clear rights that have been authorized by reaching this point, bail if nothing left to
5021 rights
&= ~(KAUTH_VNODE_LINKTARGET
| KAUTH_VNODE_CHECKIMMUTABLE
);
5026 * If we're not the superuser, authorize based on file properties.
5028 if (!vfs_context_issuser(ctx
)) {
5029 /* process delete rights */
5030 if ((rights
& KAUTH_VNODE_DELETE
) &&
5031 ((result
= vnode_authorize_delete(vcp
)) != 0))
5034 /* process remaining rights */
5035 if ((rights
& ~KAUTH_VNODE_DELETE
) &&
5036 ((result
= vnode_authorize_simple(vcp
, rights
, rights
& KAUTH_VNODE_DELETE
)) != 0))
5041 * Execute is only granted to root if one of the x bits is set. This check only
5042 * makes sense if the posix mode bits are actually supported.
5044 if ((rights
& KAUTH_VNODE_EXECUTE
) &&
5045 (vp
->v_type
== VREG
) &&
5046 VATTR_IS_SUPPORTED(&va
, va_mode
) &&
5047 !(va
.va_mode
& (S_IXUSR
| S_IXGRP
| S_IXOTH
))) {
5049 KAUTH_DEBUG("%p DENIED - root execute requires at least one x bit in 0x%x", vp
, va
.va_mode
);
5053 KAUTH_DEBUG("%p ALLOWED - caller is superuser", vp
);
5057 if (VATTR_IS_SUPPORTED(&va
, va_acl
) && (va
.va_acl
!= NULL
))
5058 kauth_acl_free(va
.va_acl
);
5059 if (VATTR_IS_SUPPORTED(&dva
, va_acl
) && (dva
.va_acl
!= NULL
))
5060 kauth_acl_free(dva
.va_acl
);
5063 KAUTH_DEBUG("%p DENIED - auth denied", vp
);
5064 return(KAUTH_RESULT_DENY
);
5068 * Note that this implies that we will allow requests for no rights, as well as
5069 * for rights that we do not recognise. There should be none of these.
5071 KAUTH_DEBUG("%p ALLOWED - auth granted", vp
);
5072 return(KAUTH_RESULT_ALLOW
);
5076 * Check that the attribute information in vattr can be legally applied to
5077 * a new file by the context.
5080 vnode_authattr_new(vnode_t dvp
, struct vnode_attr
*vap
, int noauth
, vfs_context_t ctx
)
5083 int is_suser
, ismember
, defaulted_owner
, defaulted_group
, defaulted_mode
;
5089 defaulted_owner
= defaulted_group
= defaulted_mode
= 0;
5092 * Require that the filesystem support extended security to apply any.
5094 if (!vfs_extendedsecurity(dvp
->v_mount
) &&
5095 (VATTR_IS_ACTIVE(vap
, va_acl
) || VATTR_IS_ACTIVE(vap
, va_uuuid
) || VATTR_IS_ACTIVE(vap
, va_guuid
))) {
5101 * Default some fields.
5106 * If the filesystem is mounted IGNORE_OWNERSHIP and an explicit owner is set, that
5107 * owner takes ownership of all new files.
5109 if ((dmp
->mnt_flag
& MNT_IGNORE_OWNERSHIP
) && (dmp
->mnt_fsowner
!= KAUTH_UID_NONE
)) {
5110 VATTR_SET(vap
, va_uid
, dmp
->mnt_fsowner
);
5111 defaulted_owner
= 1;
5113 if (!VATTR_IS_ACTIVE(vap
, va_uid
)) {
5114 /* default owner is current user */
5115 VATTR_SET(vap
, va_uid
, kauth_cred_getuid(vfs_context_ucred(ctx
)));
5116 defaulted_owner
= 1;
5121 * If the filesystem is mounted IGNORE_OWNERSHIP and an explicit grouo is set, that
5122 * group takes ownership of all new files.
5124 if ((dmp
->mnt_flag
& MNT_IGNORE_OWNERSHIP
) && (dmp
->mnt_fsgroup
!= KAUTH_GID_NONE
)) {
5125 VATTR_SET(vap
, va_gid
, dmp
->mnt_fsgroup
);
5126 defaulted_group
= 1;
5128 if (!VATTR_IS_ACTIVE(vap
, va_gid
)) {
5129 /* default group comes from parent object, fallback to current user */
5130 struct vnode_attr dva
;
5132 VATTR_WANTED(&dva
, va_gid
);
5133 if ((error
= vnode_getattr(dvp
, &dva
, ctx
)) != 0)
5135 if (VATTR_IS_SUPPORTED(&dva
, va_gid
)) {
5136 VATTR_SET(vap
, va_gid
, dva
.va_gid
);
5138 VATTR_SET(vap
, va_gid
, kauth_cred_getgid(vfs_context_ucred(ctx
)));
5140 defaulted_group
= 1;
5144 if (!VATTR_IS_ACTIVE(vap
, va_flags
))
5145 VATTR_SET(vap
, va_flags
, 0);
5147 /* default mode is everything, masked with current umask */
5148 if (!VATTR_IS_ACTIVE(vap
, va_mode
)) {
5149 VATTR_SET(vap
, va_mode
, ACCESSPERMS
& ~vfs_context_proc(ctx
)->p_fd
->fd_cmask
);
5150 KAUTH_DEBUG("ATTR - defaulting new file mode to %o from umask %o", vap
->va_mode
, vfs_context_proc(ctx
)->p_fd
->fd_cmask
);
5153 /* set timestamps to now */
5154 if (!VATTR_IS_ACTIVE(vap
, va_create_time
)) {
5155 nanotime(&vap
->va_create_time
);
5156 VATTR_SET_ACTIVE(vap
, va_create_time
);
5160 * Check for attempts to set nonsensical fields.
5162 if (vap
->va_active
& ~VNODE_ATTR_NEWOBJ
) {
5164 KAUTH_DEBUG("ATTR - ERROR - attempt to set unsupported new-file attributes %llx",
5165 vap
->va_active
& ~VNODE_ATTR_NEWOBJ
);
5170 * Quickly check for the applicability of any enforcement here.
5171 * Tests below maintain the integrity of the local security model.
5173 if (vfs_authopaque(vnode_mount(dvp
)))
5177 * We need to know if the caller is the superuser, or if the work is
5178 * otherwise already authorised.
5180 cred
= vfs_context_ucred(ctx
);
5182 /* doing work for the kernel */
5185 is_suser
= vfs_context_issuser(ctx
);
5189 if (VATTR_IS_ACTIVE(vap
, va_flags
)) {
5191 if ((vap
->va_flags
& (UF_SETTABLE
| SF_SETTABLE
)) != vap
->va_flags
) {
5193 KAUTH_DEBUG(" DENIED - superuser attempt to set illegal flag(s)");
5197 if ((vap
->va_flags
& UF_SETTABLE
) != vap
->va_flags
) {
5199 KAUTH_DEBUG(" DENIED - user attempt to set illegal flag(s)");
5205 /* if not superuser, validate legality of new-item attributes */
5207 if (!defaulted_mode
&& VATTR_IS_ACTIVE(vap
, va_mode
)) {
5209 if (vap
->va_mode
& S_ISGID
) {
5210 if ((error
= kauth_cred_ismember_gid(cred
, vap
->va_gid
, &ismember
)) != 0) {
5211 KAUTH_DEBUG("ATTR - ERROR: got %d checking for membership in %d", error
, vap
->va_gid
);
5215 KAUTH_DEBUG(" DENIED - can't set SGID bit, not a member of %d", vap
->va_gid
);
5222 if ((vap
->va_mode
& S_ISUID
) && (vap
->va_uid
!= kauth_cred_getuid(cred
))) {
5223 KAUTH_DEBUG("ATTR - ERROR: illegal attempt to set the setuid bit");
5228 if (!defaulted_owner
&& (vap
->va_uid
!= kauth_cred_getuid(cred
))) {
5229 KAUTH_DEBUG(" DENIED - cannot create new item owned by %d", vap
->va_uid
);
5233 if (!defaulted_group
) {
5234 if ((error
= kauth_cred_ismember_gid(cred
, vap
->va_gid
, &ismember
)) != 0) {
5235 KAUTH_DEBUG(" ERROR - got %d checking for membership in %d", error
, vap
->va_gid
);
5239 KAUTH_DEBUG(" DENIED - cannot create new item with group %d - not a member", vap
->va_gid
);
5245 /* initialising owner/group UUID */
5246 if (VATTR_IS_ACTIVE(vap
, va_uuuid
)) {
5247 if ((error
= kauth_cred_getguid(cred
, &changer
)) != 0) {
5248 KAUTH_DEBUG(" ERROR - got %d trying to get caller UUID", error
);
5249 /* XXX ENOENT here - no GUID - should perhaps become EPERM */
5252 if (!kauth_guid_equal(&vap
->va_uuuid
, &changer
)) {
5253 KAUTH_DEBUG(" ERROR - cannot create item with supplied owner UUID - not us");
5258 if (VATTR_IS_ACTIVE(vap
, va_guuid
)) {
5259 if ((error
= kauth_cred_ismember_guid(cred
, &vap
->va_guuid
, &ismember
)) != 0) {
5260 KAUTH_DEBUG(" ERROR - got %d trying to check group membership", error
);
5264 KAUTH_DEBUG(" ERROR - cannot create item with supplied group UUID - not a member");
5275 * Check that the attribute information in vap can be legally written by the context.
5277 * Call this when you're not sure about the vnode_attr; either its contents have come
5278 * from an unknown source, or when they are variable.
5280 * Returns errno, or zero and sets *actionp to the KAUTH_VNODE_* actions that
5281 * must be authorized to be permitted to write the vattr.
5284 vnode_authattr(vnode_t vp
, struct vnode_attr
*vap
, kauth_action_t
*actionp
, vfs_context_t ctx
)
5286 struct vnode_attr ova
;
5287 kauth_action_t required_action
;
5288 int error
, is_suser
, ismember
, chowner
, chgroup
;
5297 required_action
= 0;
5301 * Quickly check for enforcement applicability.
5303 if (vfs_authopaque(vnode_mount(vp
)))
5307 * Check for attempts to set nonsensical fields.
5309 if (vap
->va_active
& VNODE_ATTR_RDONLY
) {
5310 KAUTH_DEBUG("ATTR - ERROR: attempt to set readonly attribute(s)");
5316 * We need to know if the caller is the superuser.
5318 cred
= vfs_context_ucred(ctx
);
5319 is_suser
= kauth_cred_issuser(cred
);
5322 * If any of the following are changing, we need information from the old file:
5329 if (VATTR_IS_ACTIVE(vap
, va_uid
) ||
5330 VATTR_IS_ACTIVE(vap
, va_gid
) ||
5331 VATTR_IS_ACTIVE(vap
, va_mode
) ||
5332 VATTR_IS_ACTIVE(vap
, va_uuuid
) ||
5333 VATTR_IS_ACTIVE(vap
, va_guuid
)) {
5334 VATTR_WANTED(&ova
, va_mode
);
5335 VATTR_WANTED(&ova
, va_uid
);
5336 VATTR_WANTED(&ova
, va_gid
);
5337 VATTR_WANTED(&ova
, va_uuuid
);
5338 VATTR_WANTED(&ova
, va_guuid
);
5339 KAUTH_DEBUG("ATTR - security information changing, fetching existing attributes");
5343 * If timestamps are being changed, we need to know who the file is owned
5346 if (VATTR_IS_ACTIVE(vap
, va_create_time
) ||
5347 VATTR_IS_ACTIVE(vap
, va_change_time
) ||
5348 VATTR_IS_ACTIVE(vap
, va_modify_time
) ||
5349 VATTR_IS_ACTIVE(vap
, va_access_time
) ||
5350 VATTR_IS_ACTIVE(vap
, va_backup_time
)) {
5352 VATTR_WANTED(&ova
, va_uid
);
5353 #if 0 /* enable this when we support UUIDs as official owners */
5354 VATTR_WANTED(&ova
, va_uuuid
);
5356 KAUTH_DEBUG("ATTR - timestamps changing, fetching uid and GUID");
5360 * If flags are being changed, we need the old flags.
5362 if (VATTR_IS_ACTIVE(vap
, va_flags
)) {
5363 KAUTH_DEBUG("ATTR - flags changing, fetching old flags");
5364 VATTR_WANTED(&ova
, va_flags
);
5368 * If the size is being set, make sure it's not a directory.
5370 if (VATTR_IS_ACTIVE(vap
, va_data_size
)) {
5371 /* size is meaningless on a directory, don't permit this */
5372 if (vnode_isdir(vp
)) {
5373 KAUTH_DEBUG("ATTR - ERROR: size change requested on a directory");
5382 KAUTH_DEBUG("ATTR - fetching old attributes %016llx", ova
.va_active
);
5383 if ((error
= vnode_getattr(vp
, &ova
, ctx
)) != 0) {
5384 KAUTH_DEBUG(" ERROR - got %d trying to get attributes", error
);
5389 * Size changes require write access to the file data.
5391 if (VATTR_IS_ACTIVE(vap
, va_data_size
)) {
5392 /* if we can't get the size, or it's different, we need write access */
5393 KAUTH_DEBUG("ATTR - size change, requiring WRITE_DATA");
5394 required_action
|= KAUTH_VNODE_WRITE_DATA
;
5398 * Changing timestamps?
5400 * Note that we are only called to authorize user-requested time changes;
5401 * side-effect time changes are not authorized. Authorisation is only
5402 * required for existing files.
5404 * Non-owners are not permitted to change the time on an existing
5405 * file to anything other than the current time.
5407 if (VATTR_IS_ACTIVE(vap
, va_create_time
) ||
5408 VATTR_IS_ACTIVE(vap
, va_change_time
) ||
5409 VATTR_IS_ACTIVE(vap
, va_modify_time
) ||
5410 VATTR_IS_ACTIVE(vap
, va_access_time
) ||
5411 VATTR_IS_ACTIVE(vap
, va_backup_time
)) {
5413 * The owner and root may set any timestamps they like,
5414 * provided that the file is not immutable. The owner still needs
5415 * WRITE_ATTRIBUTES (implied by ownership but still deniable).
5417 if (is_suser
|| vauth_node_owner(&ova
, cred
)) {
5418 KAUTH_DEBUG("ATTR - root or owner changing timestamps");
5419 required_action
|= KAUTH_VNODE_CHECKIMMUTABLE
| KAUTH_VNODE_WRITE_ATTRIBUTES
;
5421 /* just setting the current time? */
5422 if (vap
->va_vaflags
& VA_UTIMES_NULL
) {
5423 KAUTH_DEBUG("ATTR - non-root/owner changing timestamps, requiring WRITE_ATTRIBUTES");
5424 required_action
|= KAUTH_VNODE_WRITE_ATTRIBUTES
;
5426 KAUTH_DEBUG("ATTR - ERROR: illegal timestamp modification attempted");
5434 * Changing file mode?
5436 if (VATTR_IS_ACTIVE(vap
, va_mode
) && VATTR_IS_SUPPORTED(&ova
, va_mode
) && (ova
.va_mode
!= vap
->va_mode
)) {
5437 KAUTH_DEBUG("ATTR - mode change from %06o to %06o", ova
.va_mode
, vap
->va_mode
);
5440 * Mode changes always have the same basic auth requirements.
5443 KAUTH_DEBUG("ATTR - superuser mode change, requiring immutability check");
5444 required_action
|= KAUTH_VNODE_CHECKIMMUTABLE
;
5446 /* need WRITE_SECURITY */
5447 KAUTH_DEBUG("ATTR - non-superuser mode change, requiring WRITE_SECURITY");
5448 required_action
|= KAUTH_VNODE_WRITE_SECURITY
;
5452 * Can't set the setgid bit if you're not in the group and not root. Have to have
5453 * existing group information in the case we're not setting it right now.
5455 if (vap
->va_mode
& S_ISGID
) {
5456 required_action
|= KAUTH_VNODE_CHECKIMMUTABLE
; /* always required */
5458 if (VATTR_IS_ACTIVE(vap
, va_gid
)) {
5459 group
= vap
->va_gid
;
5460 } else if (VATTR_IS_SUPPORTED(&ova
, va_gid
)) {
5463 KAUTH_DEBUG("ATTR - ERROR: setgid but no gid available");
5468 * This might be too restrictive; WRITE_SECURITY might be implied by
5469 * membership in this case, rather than being an additional requirement.
5471 if ((error
= kauth_cred_ismember_gid(cred
, group
, &ismember
)) != 0) {
5472 KAUTH_DEBUG("ATTR - ERROR: got %d checking for membership in %d", error
, vap
->va_gid
);
5476 KAUTH_DEBUG(" DENIED - can't set SGID bit, not a member of %d", group
);
5484 * Can't set the setuid bit unless you're root or the file's owner.
5486 if (vap
->va_mode
& S_ISUID
) {
5487 required_action
|= KAUTH_VNODE_CHECKIMMUTABLE
; /* always required */
5489 if (VATTR_IS_ACTIVE(vap
, va_uid
)) {
5490 owner
= vap
->va_uid
;
5491 } else if (VATTR_IS_SUPPORTED(&ova
, va_uid
)) {
5494 KAUTH_DEBUG("ATTR - ERROR: setuid but no uid available");
5498 if (owner
!= kauth_cred_getuid(cred
)) {
5500 * We could allow this if WRITE_SECURITY is permitted, perhaps.
5502 KAUTH_DEBUG("ATTR - ERROR: illegal attempt to set the setuid bit");
5511 * Validate/mask flags changes. This checks that only the flags in
5512 * the UF_SETTABLE mask are being set, and preserves the flags in
5513 * the SF_SETTABLE case.
5515 * Since flags changes may be made in conjunction with other changes,
5516 * we will ask the auth code to ignore immutability in the case that
5517 * the SF_* flags are not set and we are only manipulating the file flags.
5520 if (VATTR_IS_ACTIVE(vap
, va_flags
)) {
5521 /* compute changing flags bits */
5522 if (VATTR_IS_SUPPORTED(&ova
, va_flags
)) {
5523 fdelta
= vap
->va_flags
^ ova
.va_flags
;
5525 fdelta
= vap
->va_flags
;
5529 KAUTH_DEBUG("ATTR - flags changing, requiring WRITE_SECURITY");
5530 required_action
|= KAUTH_VNODE_WRITE_SECURITY
;
5532 /* check that changing bits are legal */
5535 * The immutability check will prevent us from clearing the SF_*
5536 * flags unless the system securelevel permits it, so just check
5537 * for legal flags here.
5539 if (fdelta
& ~(UF_SETTABLE
| SF_SETTABLE
)) {
5541 KAUTH_DEBUG(" DENIED - superuser attempt to set illegal flag(s)");
5545 if (fdelta
& ~UF_SETTABLE
) {
5547 KAUTH_DEBUG(" DENIED - user attempt to set illegal flag(s)");
5552 * If the caller has the ability to manipulate file flags,
5553 * security is not reduced by ignoring them for this operation.
5555 * A more complete test here would consider the 'after' states of the flags
5556 * to determine whether it would permit the operation, but this becomes
5559 * Ignoring immutability is conditional on securelevel; this does not bypass
5560 * the SF_* flags if securelevel > 0.
5562 required_action
|= KAUTH_VNODE_NOIMMUTABLE
;
5567 * Validate ownership information.
5574 * Note that if the filesystem didn't give us a UID, we expect that it doesn't
5575 * support them in general, and will ignore it if/when we try to set it.
5576 * We might want to clear the uid out of vap completely here.
5578 if (VATTR_IS_ACTIVE(vap
, va_uid
) && VATTR_IS_SUPPORTED(&ova
, va_uid
) && (vap
->va_uid
!= ova
.va_uid
)) {
5579 if (!is_suser
&& (kauth_cred_getuid(cred
) != vap
->va_uid
)) {
5580 KAUTH_DEBUG(" DENIED - non-superuser cannot change ownershipt to a third party");
5589 * Note that if the filesystem didn't give us a GID, we expect that it doesn't
5590 * support them in general, and will ignore it if/when we try to set it.
5591 * We might want to clear the gid out of vap completely here.
5593 if (VATTR_IS_ACTIVE(vap
, va_gid
) && VATTR_IS_SUPPORTED(&ova
, va_gid
) && (vap
->va_gid
!= ova
.va_gid
)) {
5595 if ((error
= kauth_cred_ismember_gid(cred
, vap
->va_gid
, &ismember
)) != 0) {
5596 KAUTH_DEBUG(" ERROR - got %d checking for membership in %d", error
, vap
->va_gid
);
5600 KAUTH_DEBUG(" DENIED - group change from %d to %d but not a member of target group",
5601 ova
.va_gid
, vap
->va_gid
);
5610 * Owner UUID being set or changed.
5612 if (VATTR_IS_ACTIVE(vap
, va_uuuid
)) {
5613 /* if the owner UUID is not actually changing ... */
5614 if (VATTR_IS_SUPPORTED(&ova
, va_uuuid
) && kauth_guid_equal(&vap
->va_uuuid
, &ova
.va_uuuid
))
5615 goto no_uuuid_change
;
5618 * The owner UUID cannot be set by a non-superuser to anything other than
5622 if ((error
= kauth_cred_getguid(cred
, &changer
)) != 0) {
5623 KAUTH_DEBUG(" ERROR - got %d trying to get caller UUID", error
);
5624 /* XXX ENOENT here - no UUID - should perhaps become EPERM */
5627 if (!kauth_guid_equal(&vap
->va_uuuid
, &changer
)) {
5628 KAUTH_DEBUG(" ERROR - cannot set supplied owner UUID - not us");
5637 * Group UUID being set or changed.
5639 if (VATTR_IS_ACTIVE(vap
, va_guuid
)) {
5640 /* if the group UUID is not actually changing ... */
5641 if (VATTR_IS_SUPPORTED(&ova
, va_guuid
) && kauth_guid_equal(&vap
->va_guuid
, &ova
.va_guuid
))
5642 goto no_guuid_change
;
5645 * The group UUID cannot be set by a non-superuser to anything other than
5646 * one of which they are a member.
5649 if ((error
= kauth_cred_ismember_guid(cred
, &vap
->va_guuid
, &ismember
)) != 0) {
5650 KAUTH_DEBUG(" ERROR - got %d trying to check group membership", error
);
5654 KAUTH_DEBUG(" ERROR - cannot create item with supplied group UUID - not a member");
5664 * Compute authorisation for group/ownership changes.
5666 if (chowner
|| chgroup
) {
5668 KAUTH_DEBUG("ATTR - superuser changing file owner/group, requiring immutability check");
5669 required_action
|= KAUTH_VNODE_CHECKIMMUTABLE
;
5672 KAUTH_DEBUG("ATTR - ownership change, requiring TAKE_OWNERSHIP");
5673 required_action
|= KAUTH_VNODE_TAKE_OWNERSHIP
;
5675 if (chgroup
&& !chowner
) {
5676 KAUTH_DEBUG("ATTR - group change, requiring WRITE_SECURITY");
5677 required_action
|= KAUTH_VNODE_WRITE_SECURITY
;
5680 /* clear set-uid and set-gid bits as required by Posix */
5681 if (VATTR_IS_ACTIVE(vap
, va_mode
)) {
5682 newmode
= vap
->va_mode
;
5683 } else if (VATTR_IS_SUPPORTED(&ova
, va_mode
)) {
5684 newmode
= ova
.va_mode
;
5686 KAUTH_DEBUG("CHOWN - trying to change owner but cannot get mode from filesystem to mask setugid bits");
5689 if (newmode
& (S_ISUID
| S_ISGID
)) {
5690 VATTR_SET(vap
, va_mode
, newmode
& ~(S_ISUID
| S_ISGID
));
5691 KAUTH_DEBUG("CHOWN - masking setugid bits from mode %o to %o", newmode
, vap
->va_mode
);
5697 * Authorise changes in the ACL.
5699 if (VATTR_IS_ACTIVE(vap
, va_acl
)) {
5701 /* no existing ACL */
5702 if (!VATTR_IS_ACTIVE(&ova
, va_acl
) || (ova
.va_acl
== NULL
)) {
5705 if (vap
->va_acl
!= NULL
) {
5706 required_action
|= KAUTH_VNODE_WRITE_SECURITY
;
5707 KAUTH_DEBUG("CHMOD - adding ACL");
5710 /* removing an existing ACL */
5711 } else if (vap
->va_acl
== NULL
) {
5712 required_action
|= KAUTH_VNODE_WRITE_SECURITY
;
5713 KAUTH_DEBUG("CHMOD - removing ACL");
5715 /* updating an existing ACL */
5717 if (vap
->va_acl
->acl_entrycount
!= ova
.va_acl
->acl_entrycount
) {
5718 /* entry count changed, must be different */
5719 required_action
|= KAUTH_VNODE_WRITE_SECURITY
;
5720 KAUTH_DEBUG("CHMOD - adding/removing ACL entries");
5721 } else if (vap
->va_acl
->acl_entrycount
> 0) {
5722 /* both ACLs have the same ACE count, said count is 1 or more, bitwise compare ACLs */
5723 if (!memcmp(&vap
->va_acl
->acl_ace
[0], &ova
.va_acl
->acl_ace
[0],
5724 sizeof(struct kauth_ace
) * vap
->va_acl
->acl_entrycount
)) {
5725 required_action
|= KAUTH_VNODE_WRITE_SECURITY
;
5726 KAUTH_DEBUG("CHMOD - changing ACL entries");
5733 * Other attributes that require authorisation.
5735 if (VATTR_IS_ACTIVE(vap
, va_encoding
))
5736 required_action
|= KAUTH_VNODE_WRITE_ATTRIBUTES
;
5739 if (VATTR_IS_SUPPORTED(&ova
, va_acl
) && (ova
.va_acl
!= NULL
))
5740 kauth_acl_free(ova
.va_acl
);
5742 *actionp
= required_action
;
5748 vfs_setlocklocal(mount_t mp
)
5753 mp
->mnt_kern_flag
|= MNTK_LOCK_LOCAL
;
5756 * We do not expect anyone to be using any vnodes at the
5757 * time this routine is called. So no need for vnode locking
5759 TAILQ_FOREACH(vp
, &mp
->mnt_vnodelist
, v_mntvnodes
) {
5760 vp
->v_flag
|= VLOCKLOCAL
;
5762 TAILQ_FOREACH(vp
, &mp
->mnt_workerqueue
, v_mntvnodes
) {
5763 vp
->v_flag
|= VLOCKLOCAL
;
5765 TAILQ_FOREACH(vp
, &mp
->mnt_newvnodes
, v_mntvnodes
) {
5766 vp
->v_flag
|= VLOCKLOCAL
;
5774 record_vp(vnode_t vp
, int count
) {
5778 if ((vp
->v_flag
& VSYSTEM
))
5781 ut
= get_bsdthread_info(current_thread());
5782 ut
->uu_iocount
+= count
;
5784 if (ut
->uu_vpindex
< 32) {
5785 for (i
= 0; i
< ut
->uu_vpindex
; i
++) {
5786 if (ut
->uu_vps
[i
] == vp
)
5789 ut
->uu_vps
[ut
->uu_vpindex
] = vp
;