2 * Copyright (c) 2000-2005 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
21 * @APPLE_LICENSE_HEADER_END@
23 /* Copyright (c) 1995 NeXT Computer, Inc. All Rights Reserved */
25 * Copyright (c) 1989, 1993
26 * The Regents of the University of California. All rights reserved.
27 * (c) UNIX System Laboratories, Inc.
28 * All or some portions of this file are derived from material licensed
29 * to the University of California by American Telephone and Telegraph
30 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
31 * the permission of UNIX System Laboratories, Inc.
33 * Redistribution and use in source and binary forms, with or without
34 * modification, are permitted provided that the following conditions
36 * 1. Redistributions of source code must retain the above copyright
37 * notice, this list of conditions and the following disclaimer.
38 * 2. Redistributions in binary form must reproduce the above copyright
39 * notice, this list of conditions and the following disclaimer in the
40 * documentation and/or other materials provided with the distribution.
41 * 3. All advertising materials mentioning features or use of this software
42 * must display the following acknowledgement:
43 * This product includes software developed by the University of
44 * California, Berkeley and its contributors.
45 * 4. Neither the name of the University nor the names of its contributors
46 * may be used to endorse or promote products derived from this software
47 * without specific prior written permission.
49 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
50 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
51 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
52 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
53 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
54 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
55 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
56 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
57 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
58 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
61 * @(#)vfs_subr.c 8.31 (Berkeley) 5/26/95
65 * External virtual filesystem routines
71 #include <sys/param.h>
72 #include <sys/systm.h>
73 #include <sys/proc_internal.h>
74 #include <sys/kauth.h>
75 #include <sys/mount_internal.h>
78 #include <sys/vnode_internal.h>
80 #include <sys/namei.h>
81 #include <sys/ucred.h>
82 #include <sys/buf_internal.h>
83 #include <sys/errno.h>
84 #include <sys/malloc.h>
85 #include <sys/domain.h>
87 #include <sys/syslog.h>
88 #include <sys/ubc_internal.h>
90 #include <sys/sysctl.h>
91 #include <sys/filedesc.h>
92 #include <sys/event.h>
93 #include <sys/kdebug.h>
94 #include <sys/kauth.h>
96 #include <miscfs/fifofs/fifo.h>
99 #include <machine/spl.h>
102 #include <kern/assert.h>
104 #include <miscfs/specfs/specdev.h>
106 #include <mach/mach_types.h>
107 #include <mach/memory_object_types.h>
109 extern lck_grp_t
*vnode_lck_grp
;
110 extern lck_attr_t
*vnode_lck_attr
;
113 extern lck_mtx_t
* mnt_list_mtx_lock
;
115 enum vtype iftovt_tab
[16] = {
116 VNON
, VFIFO
, VCHR
, VNON
, VDIR
, VNON
, VBLK
, VNON
,
117 VREG
, VNON
, VLNK
, VNON
, VSOCK
, VNON
, VNON
, VBAD
,
119 int vttoif_tab
[9] = {
120 0, S_IFREG
, S_IFDIR
, S_IFBLK
, S_IFCHR
, S_IFLNK
,
121 S_IFSOCK
, S_IFIFO
, S_IFMT
,
124 extern int ubc_isinuse_locked(vnode_t
, int, int);
125 extern kern_return_t
adjust_vm_object_cache(vm_size_t oval
, vm_size_t nval
);
127 static void vnode_list_add(vnode_t
);
128 static void vnode_list_remove(vnode_t
);
130 static errno_t
vnode_drain(vnode_t
);
131 static void vgone(vnode_t
);
132 static void vclean(vnode_t vp
, int flag
, proc_t p
);
133 static void vnode_reclaim_internal(vnode_t
, int, int);
135 static void vnode_dropiocount (vnode_t
, int);
136 static errno_t
vnode_getiocount(vnode_t vp
, int locked
, int vid
, int vflags
);
137 static int vget_internal(vnode_t
, int, int);
139 static vnode_t
checkalias(vnode_t vp
, dev_t nvp_rdev
);
140 static int vnode_reload(vnode_t
);
141 static int vnode_isinuse_locked(vnode_t
, int, int);
143 static void insmntque(vnode_t vp
, mount_t mp
);
144 mount_t
mount_list_lookupby_fsid(fsid_t
*, int, int);
145 static int mount_getvfscnt(void);
146 static int mount_fillfsids(fsid_t
*, int );
147 static void vnode_iterate_setup(mount_t
);
148 static int vnode_umount_preflight(mount_t
, vnode_t
, int);
149 static int vnode_iterate_prepare(mount_t
);
150 static int vnode_iterate_reloadq(mount_t
);
151 static void vnode_iterate_clear(mount_t
);
153 TAILQ_HEAD(freelst
, vnode
) vnode_free_list
; /* vnode free list */
154 TAILQ_HEAD(inactivelst
, vnode
) vnode_inactive_list
; /* vnode inactive list */
155 struct mntlist mountlist
; /* mounted filesystem list */
156 static int nummounts
= 0;
159 #define VLISTCHECK(fun, vp, list) \
160 if ((vp)->v_freelist.tqe_prev == (struct vnode **)0xdeadb) \
161 panic("%s: %s vnode not on %slist", (fun), (list), (list));
163 #define VINACTIVECHECK(fun, vp, expected) \
165 int __is_inactive = ISSET((vp)->v_flag, VUINACTIVE); \
166 if (__is_inactive ^ expected) \
167 panic("%s: %sinactive vnode, expected %s", (fun), \
168 __is_inactive? "" : "not ", \
169 expected? "inactive": "not inactive"); \
172 #define VLISTCHECK(fun, vp, list)
173 #define VINACTIVECHECK(fun, vp, expected)
174 #endif /* DIAGNOSTIC */
176 #define VLISTNONE(vp) \
178 (vp)->v_freelist.tqe_next = (struct vnode *)0; \
179 (vp)->v_freelist.tqe_prev = (struct vnode **)0xdeadb; \
182 #define VONLIST(vp) \
183 ((vp)->v_freelist.tqe_prev != (struct vnode **)0xdeadb)
185 /* remove a vnode from free vnode list */
186 #define VREMFREE(fun, vp) \
188 VLISTCHECK((fun), (vp), "free"); \
189 TAILQ_REMOVE(&vnode_free_list, (vp), v_freelist); \
194 /* remove a vnode from inactive vnode list */
195 #define VREMINACTIVE(fun, vp) \
197 VLISTCHECK((fun), (vp), "inactive"); \
198 VINACTIVECHECK((fun), (vp), VUINACTIVE); \
199 TAILQ_REMOVE(&vnode_inactive_list, (vp), v_freelist); \
200 CLR((vp)->v_flag, VUINACTIVE); \
206 * Have to declare first two locks as actual data even if !MACH_SLOCKS, since
207 * a pointers to them get passed around.
209 void * mntvnode_slock
;
211 void * spechash_slock
;
214 * vnodetarget is the amount of vnodes we expect to get back
215 * from the the inactive vnode list and VM object cache.
216 * As vnreclaim() is a mainly cpu bound operation for faster
217 * processers this number could be higher.
218 * Having this number too high introduces longer delays in
219 * the execution of new_vnode().
221 unsigned long vnodetarget
; /* target for vnreclaim() */
222 #define VNODE_FREE_TARGET 20 /* Default value for vnodetarget */
225 * We need quite a few vnodes on the free list to sustain the
226 * rapid stat() the compilation process does, and still benefit from the name
227 * cache. Having too few vnodes on the free list causes serious disk
228 * thrashing as we cycle through them.
230 #define VNODE_FREE_MIN 300 /* freelist should have at least these many */
233 * We need to get vnodes back from the VM object cache when a certain #
234 * of vnodes are reused from the freelist. This is essential for the
235 * caching to be effective in the namecache and the buffer cache [for the
238 #define VNODE_TOOMANY_REUSED (VNODE_FREE_MIN/4)
241 * If we have enough vnodes on the freelist we do not want to reclaim
242 * the vnodes from the VM object cache.
244 #define VNODE_FREE_ENOUGH (VNODE_FREE_MIN + (VNODE_FREE_MIN/2))
247 * Initialize the vnode management data structures.
249 __private_extern__
void
252 TAILQ_INIT(&vnode_free_list
);
253 TAILQ_INIT(&vnode_inactive_list
);
254 TAILQ_INIT(&mountlist
);
257 vnodetarget
= VNODE_FREE_TARGET
;
260 * Scale the vm_object_cache to accomodate the vnodes
263 (void) adjust_vm_object_cache(0, desiredvnodes
- VNODE_FREE_MIN
);
266 /* Reset the VM Object Cache with the values passed in */
267 __private_extern__ kern_return_t
268 reset_vmobjectcache(unsigned int val1
, unsigned int val2
)
270 vm_size_t oval
= val1
- VNODE_FREE_MIN
;
273 if(val2
< VNODE_FREE_MIN
)
276 nval
= val2
- VNODE_FREE_MIN
;
278 return(adjust_vm_object_cache(oval
, nval
));
282 /* the timeout is in 10 msecs */
284 vnode_waitforwrites(vnode_t vp
, int output_target
, int slpflag
, int slptimeout
, char *msg
) {
288 KERNEL_DEBUG(0x3010280 | DBG_FUNC_START
, (int)vp
, output_target
, vp
->v_numoutput
, 0, 0);
290 if (vp
->v_numoutput
> output_target
) {
296 while ((vp
->v_numoutput
> output_target
) && error
== 0) {
298 vp
->v_flag
|= VTHROTTLED
;
300 vp
->v_flag
|= VBWAIT
;
301 ts
.tv_sec
= (slptimeout
/100);
302 ts
.tv_nsec
= (slptimeout
% 1000) * 10 * NSEC_PER_USEC
* 1000 ;
303 error
= msleep((caddr_t
)&vp
->v_numoutput
, &vp
->v_lock
, (slpflag
| (PRIBIO
+ 1)), msg
, &ts
);
307 KERNEL_DEBUG(0x3010280 | DBG_FUNC_END
, (int)vp
, output_target
, vp
->v_numoutput
, error
, 0);
314 vnode_startwrite(vnode_t vp
) {
316 OSAddAtomic(1, &vp
->v_numoutput
);
321 vnode_writedone(vnode_t vp
)
326 OSAddAtomic(-1, &vp
->v_numoutput
);
330 if (vp
->v_numoutput
< 0)
331 panic("vnode_writedone: numoutput < 0");
333 if ((vp
->v_flag
& VTHROTTLED
) && (vp
->v_numoutput
< (VNODE_ASYNC_THROTTLE
/ 3))) {
334 vp
->v_flag
&= ~VTHROTTLED
;
337 if ((vp
->v_flag
& VBWAIT
) && (vp
->v_numoutput
== 0)) {
338 vp
->v_flag
&= ~VBWAIT
;
344 wakeup((caddr_t
)&vp
->v_numoutput
);
351 vnode_hasdirtyblks(vnode_t vp
)
353 struct cl_writebehind
*wbp
;
356 * Not taking the buf_mtxp as there is little
357 * point doing it. Even if the lock is taken the
358 * state can change right after that. If their
359 * needs to be a synchronization, it must be driven
362 if (vp
->v_dirtyblkhd
.lh_first
)
365 if (!UBCINFOEXISTS(vp
))
368 wbp
= vp
->v_ubcinfo
->cl_wbehind
;
370 if (wbp
&& (wbp
->cl_number
|| wbp
->cl_scmap
))
377 vnode_hascleanblks(vnode_t vp
)
380 * Not taking the buf_mtxp as there is little
381 * point doing it. Even if the lock is taken the
382 * state can change right after that. If their
383 * needs to be a synchronization, it must be driven
386 if (vp
->v_cleanblkhd
.lh_first
)
392 vnode_iterate_setup(mount_t mp
)
394 while (mp
->mnt_lflag
& MNT_LITER
) {
395 mp
->mnt_lflag
|= MNT_LITERWAIT
;
396 msleep((caddr_t
)mp
, &mp
->mnt_mlock
, PVFS
, "vnode_iterate_setup", 0);
399 mp
->mnt_lflag
|= MNT_LITER
;
404 vnode_umount_preflight(mount_t mp
, vnode_t skipvp
, int flags
)
408 TAILQ_FOREACH(vp
, &mp
->mnt_vnodelist
, v_mntvnodes
) {
409 if (vp
->v_type
== VDIR
)
413 if ((flags
& SKIPSYSTEM
) && ((vp
->v_flag
& VSYSTEM
) ||
414 (vp
->v_flag
& VNOFLUSH
)))
416 if ((flags
& SKIPSWAP
) && (vp
->v_flag
& VSWAP
))
418 if ((flags
& WRITECLOSE
) &&
419 (vp
->v_writecount
== 0 || vp
->v_type
!= VREG
))
421 /* Look for busy vnode */
422 if (((vp
->v_usecount
!= 0) &&
423 ((vp
->v_usecount
- vp
->v_kusecount
) != 0)))
431 * This routine prepares iteration by moving all the vnodes to worker queue
432 * called with mount lock held
435 vnode_iterate_prepare(mount_t mp
)
439 if (TAILQ_EMPTY(&mp
->mnt_vnodelist
)) {
444 vp
= TAILQ_FIRST(&mp
->mnt_vnodelist
);
445 vp
->v_mntvnodes
.tqe_prev
= &(mp
->mnt_workerqueue
.tqh_first
);
446 mp
->mnt_workerqueue
.tqh_first
= mp
->mnt_vnodelist
.tqh_first
;
447 mp
->mnt_workerqueue
.tqh_last
= mp
->mnt_vnodelist
.tqh_last
;
449 TAILQ_INIT(&mp
->mnt_vnodelist
);
450 if (mp
->mnt_newvnodes
.tqh_first
!= NULL
)
451 panic("vnode_iterate_prepare: newvnode when entering vnode");
452 TAILQ_INIT(&mp
->mnt_newvnodes
);
458 /* called with mount lock held */
460 vnode_iterate_reloadq(mount_t mp
)
464 /* add the remaining entries in workerq to the end of mount vnode list */
465 if (!TAILQ_EMPTY(&mp
->mnt_workerqueue
)) {
467 mvp
= TAILQ_LAST(&mp
->mnt_vnodelist
, vnodelst
);
469 /* Joining the workerque entities to mount vnode list */
471 mvp
->v_mntvnodes
.tqe_next
= mp
->mnt_workerqueue
.tqh_first
;
473 mp
->mnt_vnodelist
.tqh_first
= mp
->mnt_workerqueue
.tqh_first
;
474 mp
->mnt_workerqueue
.tqh_first
->v_mntvnodes
.tqe_prev
= mp
->mnt_vnodelist
.tqh_last
;
475 mp
->mnt_vnodelist
.tqh_last
= mp
->mnt_workerqueue
.tqh_last
;
476 TAILQ_INIT(&mp
->mnt_workerqueue
);
479 /* add the newvnodes to the head of mount vnode list */
480 if (!TAILQ_EMPTY(&mp
->mnt_newvnodes
)) {
482 nlvp
= TAILQ_LAST(&mp
->mnt_newvnodes
, vnodelst
);
484 mp
->mnt_newvnodes
.tqh_first
->v_mntvnodes
.tqe_prev
= &mp
->mnt_vnodelist
.tqh_first
;
485 nlvp
->v_mntvnodes
.tqe_next
= mp
->mnt_vnodelist
.tqh_first
;
486 if(mp
->mnt_vnodelist
.tqh_first
)
487 mp
->mnt_vnodelist
.tqh_first
->v_mntvnodes
.tqe_prev
= &nlvp
->v_mntvnodes
.tqe_next
;
489 mp
->mnt_vnodelist
.tqh_last
= mp
->mnt_newvnodes
.tqh_last
;
490 mp
->mnt_vnodelist
.tqh_first
= mp
->mnt_newvnodes
.tqh_first
;
491 TAILQ_INIT(&mp
->mnt_newvnodes
);
500 vnode_iterate_clear(mount_t mp
)
502 mp
->mnt_lflag
&= ~MNT_LITER
;
503 if (mp
->mnt_lflag
& MNT_LITERWAIT
) {
504 mp
->mnt_lflag
&= ~MNT_LITERWAIT
;
511 vnode_iterate(mp
, flags
, callout
, arg
)
514 int (*callout
)(struct vnode
*, void *);
523 vnode_iterate_setup(mp
);
525 /* it is returns 0 then there is nothing to do */
526 retval
= vnode_iterate_prepare(mp
);
529 vnode_iterate_clear(mp
);
534 /* iterate over all the vnodes */
535 while (!TAILQ_EMPTY(&mp
->mnt_workerqueue
)) {
536 vp
= TAILQ_FIRST(&mp
->mnt_workerqueue
);
537 TAILQ_REMOVE(&mp
->mnt_workerqueue
, vp
, v_mntvnodes
);
538 TAILQ_INSERT_TAIL(&mp
->mnt_vnodelist
, vp
, v_mntvnodes
);
540 if ((vp
->v_data
== NULL
) || (vp
->v_type
== VNON
) || (vp
->v_mount
!= mp
)) {
545 if ( vget_internal(vp
, vid
, (flags
| VNODE_NODEAD
| VNODE_WITHID
| VNODE_NOSUSPEND
))) {
549 if (flags
& VNODE_RELOAD
) {
551 * we're reloading the filesystem
552 * cast out any inactive vnodes...
554 if (vnode_reload(vp
)) {
555 /* vnode will be recycled on the refcount drop */
562 retval
= callout(vp
, arg
);
566 case VNODE_RETURNED_DONE
:
568 if (retval
== VNODE_RETURNED_DONE
) {
575 case VNODE_CLAIMED_DONE
:
587 (void)vnode_iterate_reloadq(mp
);
588 vnode_iterate_clear(mp
);
594 mount_lock_renames(mount_t mp
)
596 lck_mtx_lock(&mp
->mnt_renamelock
);
600 mount_unlock_renames(mount_t mp
)
602 lck_mtx_unlock(&mp
->mnt_renamelock
);
606 mount_lock(mount_t mp
)
608 lck_mtx_lock(&mp
->mnt_mlock
);
612 mount_unlock(mount_t mp
)
614 lck_mtx_unlock(&mp
->mnt_mlock
);
619 mount_ref(mount_t mp
, int locked
)
632 mount_drop(mount_t mp
, int locked
)
639 if (mp
->mnt_count
== 0 && (mp
->mnt_lflag
& MNT_LDRAIN
))
640 wakeup(&mp
->mnt_lflag
);
648 mount_iterref(mount_t mp
, int locked
)
654 if (mp
->mnt_iterref
< 0) {
665 mount_isdrained(mount_t mp
, int locked
)
671 if (mp
->mnt_iterref
< 0)
681 mount_iterdrop(mount_t mp
)
685 wakeup(&mp
->mnt_iterref
);
690 mount_iterdrain(mount_t mp
)
693 while (mp
->mnt_iterref
)
694 msleep((caddr_t
)&mp
->mnt_iterref
, mnt_list_mtx_lock
, PVFS
, "mount_iterdrain", 0 );
695 /* mount iterations drained */
696 mp
->mnt_iterref
= -1;
700 mount_iterreset(mount_t mp
)
703 if (mp
->mnt_iterref
== -1)
708 /* always called with mount lock held */
710 mount_refdrain(mount_t mp
)
712 if (mp
->mnt_lflag
& MNT_LDRAIN
)
713 panic("already in drain");
714 mp
->mnt_lflag
|= MNT_LDRAIN
;
716 while (mp
->mnt_count
)
717 msleep((caddr_t
)&mp
->mnt_lflag
, &mp
->mnt_mlock
, PVFS
, "mount_drain", 0 );
719 if (mp
->mnt_vnodelist
.tqh_first
!= NULL
)
720 panic("mount_refdrain: dangling vnode");
722 mp
->mnt_lflag
&= ~MNT_LDRAIN
;
729 * Mark a mount point as busy. Used to synchronize access and to delay
733 vfs_busy(mount_t mp
, int flags
)
737 if (mp
->mnt_lflag
& MNT_LDEAD
)
740 if (mp
->mnt_lflag
& MNT_LUNMOUNT
) {
741 if (flags
& LK_NOWAIT
)
746 if (mp
->mnt_lflag
& MNT_LDEAD
) {
750 if (mp
->mnt_lflag
& MNT_LUNMOUNT
) {
751 mp
->mnt_lflag
|= MNT_LWAIT
;
753 * Since all busy locks are shared except the exclusive
754 * lock granted when unmounting, the only place that a
755 * wakeup needs to be done is at the release of the
756 * exclusive lock at the end of dounmount.
758 msleep((caddr_t
)mp
, &mp
->mnt_mlock
, (PVFS
| PDROP
), "vfsbusy", 0 );
764 lck_rw_lock_shared(&mp
->mnt_rwlock
);
767 * until we are granted the rwlock, it's possible for the mount point to
768 * change state, so reevaluate before granting the vfs_busy
770 if (mp
->mnt_lflag
& (MNT_LDEAD
| MNT_LUNMOUNT
)) {
771 lck_rw_done(&mp
->mnt_rwlock
);
778 * Free a busy filesystem.
782 vfs_unbusy(mount_t mp
)
784 lck_rw_done(&mp
->mnt_rwlock
);
790 vfs_rootmountfailed(mount_t mp
) {
793 mp
->mnt_vtable
->vfc_refcount
--;
798 mount_lock_destroy(mp
);
800 FREE_ZONE(mp
, sizeof(struct mount
), M_MOUNT
);
804 * Lookup a filesystem type, and if found allocate and initialize
805 * a mount structure for it.
807 * Devname is usually updated by mount(8) after booting.
810 vfs_rootmountalloc_internal(struct vfstable
*vfsp
, const char *devname
)
814 mp
= _MALLOC_ZONE((u_long
)sizeof(struct mount
), M_MOUNT
, M_WAITOK
);
815 bzero((char *)mp
, (u_long
)sizeof(struct mount
));
817 /* Initialize the default IO constraints */
818 mp
->mnt_maxreadcnt
= mp
->mnt_maxwritecnt
= MAXPHYS
;
819 mp
->mnt_segreadcnt
= mp
->mnt_segwritecnt
= 32;
820 mp
->mnt_maxsegreadsize
= mp
->mnt_maxreadcnt
;
821 mp
->mnt_maxsegwritesize
= mp
->mnt_maxwritecnt
;
822 mp
->mnt_devblocksize
= DEV_BSIZE
;
825 (void)vfs_busy(mp
, LK_NOWAIT
);
827 TAILQ_INIT(&mp
->mnt_vnodelist
);
828 TAILQ_INIT(&mp
->mnt_workerqueue
);
829 TAILQ_INIT(&mp
->mnt_newvnodes
);
831 mp
->mnt_vtable
= vfsp
;
832 mp
->mnt_op
= vfsp
->vfc_vfsops
;
833 mp
->mnt_flag
= MNT_RDONLY
| MNT_ROOTFS
;
834 mp
->mnt_vnodecovered
= NULLVP
;
835 //mp->mnt_stat.f_type = vfsp->vfc_typenum;
836 mp
->mnt_flag
|= vfsp
->vfc_flags
& MNT_VISFLAGMASK
;
839 vfsp
->vfc_refcount
++;
842 strncpy(mp
->mnt_vfsstat
.f_fstypename
, vfsp
->vfc_name
, MFSTYPENAMELEN
);
843 mp
->mnt_vfsstat
.f_mntonname
[0] = '/';
844 (void) copystr((char *)devname
, mp
->mnt_vfsstat
.f_mntfromname
, MAXPATHLEN
- 1, 0);
850 vfs_rootmountalloc(const char *fstypename
, const char *devname
, mount_t
*mpp
)
852 struct vfstable
*vfsp
;
854 for (vfsp
= vfsconf
; vfsp
; vfsp
= vfsp
->vfc_next
)
855 if (!strcmp(vfsp
->vfc_name
, fstypename
))
860 *mpp
= vfs_rootmountalloc_internal(vfsp
, devname
);
870 * Find an appropriate filesystem to use for the root. If a filesystem
871 * has not been preselected, walk through the list of known filesystems
872 * trying those that have mountroot routines, and try them until one
873 * works or we have tried them all.
875 extern int (*mountroot
)(void);
880 struct vfstable
*vfsp
;
881 struct vfs_context context
;
885 if (mountroot
!= NULL
) {
887 * used for netboot which follows a different set of rules
889 error
= (*mountroot
)();
892 if ((error
= bdevvp(rootdev
, &rootvp
))) {
893 printf("vfs_mountroot: can't setup bdevvp\n");
896 context
.vc_proc
= current_proc();
897 context
.vc_ucred
= kauth_cred_get();
899 for (vfsp
= vfsconf
; vfsp
; vfsp
= vfsp
->vfc_next
) {
900 if (vfsp
->vfc_mountroot
== NULL
)
903 mp
= vfs_rootmountalloc_internal(vfsp
, "root_device");
904 mp
->mnt_devvp
= rootvp
;
906 if ((error
= (*vfsp
->vfc_mountroot
)(mp
, rootvp
, &context
)) == 0) {
907 mp
->mnt_devvp
->v_specflags
|= SI_MOUNTEDON
;
914 * cache the IO attributes for the underlying physical media...
915 * an error return indicates the underlying driver doesn't
916 * support all the queries necessary... however, reasonable
917 * defaults will have been set, so no reason to bail or care
919 vfs_init_io_attributes(rootvp
, mp
);
921 * get rid of iocount reference returned
922 * by bdevvp... it will have also taken
923 * a usecount reference which we want to keep
929 vfs_rootmountfailed(mp
);
932 printf("%s_mountroot failed: %d\n", vfsp
->vfc_name
, error
);
938 * Lookup a mount point by filesystem identifier.
940 extern mount_t
vfs_getvfs_locked(fsid_t
*);
946 return (mount_list_lookupby_fsid(fsid
, 0, 0));
950 vfs_getvfs_locked(fsid
)
953 return(mount_list_lookupby_fsid(fsid
, 1, 0));
957 vfs_getvfs_by_mntonname(u_char
*path
)
959 mount_t retmp
= (mount_t
)0;
963 TAILQ_FOREACH(mp
, &mountlist
, mnt_list
) {
964 if (!strcmp(mp
->mnt_vfsstat
.f_mntonname
, path
)) {
974 /* generation number for creation of new fsids */
975 u_short mntid_gen
= 0;
977 * Get a new unique fsid
990 /* generate a new fsid */
991 mtype
= mp
->mnt_vtable
->vfc_typenum
;
992 if (++mntid_gen
== 0)
994 tfsid
.val
[0] = makedev(nblkdev
+ mtype
, mntid_gen
);
995 tfsid
.val
[1] = mtype
;
997 TAILQ_FOREACH(nmp
, &mountlist
, mnt_list
) {
998 while (vfs_getvfs_locked(&tfsid
)) {
999 if (++mntid_gen
== 0)
1001 tfsid
.val
[0] = makedev(nblkdev
+ mtype
, mntid_gen
);
1004 mp
->mnt_vfsstat
.f_fsid
.val
[0] = tfsid
.val
[0];
1005 mp
->mnt_vfsstat
.f_fsid
.val
[1] = tfsid
.val
[1];
1006 mount_list_unlock();
1010 * Routines having to do with the management of the vnode table.
1012 extern int (**dead_vnodeop_p
)(void *);
1013 long numvnodes
, freevnodes
;
1014 long inactivevnodes
;
1018 * Move a vnode from one mount queue to another.
1021 insmntque(vnode_t vp
, mount_t mp
)
1025 * Delete from old mount point vnode list, if on one.
1027 if ( (lmp
= vp
->v_mount
) != NULL
&& lmp
!= dead_mountp
) {
1028 if ((vp
->v_lflag
& VNAMED_MOUNT
) == 0)
1029 panic("insmntque: vp not in mount vnode list");
1030 vp
->v_lflag
&= ~VNAMED_MOUNT
;
1036 if (vp
->v_mntvnodes
.tqe_next
== NULL
) {
1037 if (TAILQ_LAST(&lmp
->mnt_vnodelist
, vnodelst
) == vp
)
1038 TAILQ_REMOVE(&lmp
->mnt_vnodelist
, vp
, v_mntvnodes
);
1039 else if (TAILQ_LAST(&lmp
->mnt_newvnodes
, vnodelst
) == vp
)
1040 TAILQ_REMOVE(&lmp
->mnt_newvnodes
, vp
, v_mntvnodes
);
1041 else if (TAILQ_LAST(&lmp
->mnt_workerqueue
, vnodelst
) == vp
)
1042 TAILQ_REMOVE(&lmp
->mnt_workerqueue
, vp
, v_mntvnodes
);
1044 vp
->v_mntvnodes
.tqe_next
->v_mntvnodes
.tqe_prev
= vp
->v_mntvnodes
.tqe_prev
;
1045 *vp
->v_mntvnodes
.tqe_prev
= vp
->v_mntvnodes
.tqe_next
;
1047 vp
->v_mntvnodes
.tqe_next
= 0;
1048 vp
->v_mntvnodes
.tqe_prev
= 0;
1054 * Insert into list of vnodes for the new mount point, if available.
1056 if ((vp
->v_mount
= mp
) != NULL
) {
1058 if ((vp
->v_mntvnodes
.tqe_next
!= 0) && (vp
->v_mntvnodes
.tqe_prev
!= 0))
1059 panic("vp already in mount list");
1060 if (mp
->mnt_lflag
& MNT_LITER
)
1061 TAILQ_INSERT_HEAD(&mp
->mnt_newvnodes
, vp
, v_mntvnodes
);
1063 TAILQ_INSERT_HEAD(&mp
->mnt_vnodelist
, vp
, v_mntvnodes
);
1064 if (vp
->v_lflag
& VNAMED_MOUNT
)
1065 panic("insmntque: vp already in mount vnode list");
1066 if ((vp
->v_freelist
.tqe_prev
!= (struct vnode
**)0xdeadb))
1067 panic("insmntque: vp on the free list\n");
1068 vp
->v_lflag
|= VNAMED_MOUNT
;
1076 * Create a vnode for a block device.
1077 * Used for root filesystem, argdev, and swap areas.
1078 * Also used for memory file system special devices.
1081 bdevvp(dev_t dev
, vnode_t
*vpp
)
1085 struct vnode_fsparam vfsp
;
1086 struct vfs_context context
;
1093 context
.vc_proc
= current_proc();
1094 context
.vc_ucred
= FSCRED
;
1096 vfsp
.vnfs_mp
= (struct mount
*)0;
1097 vfsp
.vnfs_vtype
= VBLK
;
1098 vfsp
.vnfs_str
= "bdevvp";
1100 vfsp
.vnfs_fsnode
= 0;
1102 vfsp
.vnfs_vops
= spec_vnodeop_p
;
1103 vfsp
.vnfs_rdev
= dev
;
1104 vfsp
.vnfs_filesize
= 0;
1106 vfsp
.vnfs_flags
= VNFS_NOCACHE
| VNFS_CANTCACHE
;
1108 vfsp
.vnfs_marksystem
= 0;
1109 vfsp
.vnfs_markroot
= 0;
1111 if ( (error
= vnode_create(VNCREATE_FLAVOR
, VCREATESIZE
, &vfsp
, &nvp
)) ) {
1115 if ( (error
= vnode_ref(nvp
)) ) {
1116 panic("bdevvp failed: vnode_ref");
1119 if ( (error
= VNOP_FSYNC(nvp
, MNT_WAIT
, &context
)) ) {
1120 panic("bdevvp failed: fsync");
1123 if ( (error
= buf_invalidateblks(nvp
, BUF_WRITE_DATA
, 0, 0)) ) {
1124 panic("bdevvp failed: invalidateblks");
1127 if ( (error
= VNOP_OPEN(nvp
, FREAD
, &context
)) ) {
1128 panic("bdevvp failed: open");
1137 * Check to see if the new vnode represents a special device
1138 * for which we already have a vnode (either because of
1139 * bdevvp() or because of a different vnode representing
1140 * the same block device). If such an alias exists, deallocate
1141 * the existing contents and return the aliased vnode. The
1142 * caller is responsible for filling it with its new contents.
1145 checkalias(nvp
, nvp_rdev
)
1146 register struct vnode
*nvp
;
1153 vpp
= &speclisth
[SPECHASH(nvp_rdev
)];
1157 for (vp
= *vpp
; vp
; vp
= vp
->v_specnext
) {
1158 if (nvp_rdev
== vp
->v_rdev
&& nvp
->v_type
== vp
->v_type
) {
1166 if (vnode_getwithvid(vp
,vid
)) {
1170 * Termination state is checked in vnode_getwithvid
1175 * Alias, but not in use, so flush it out.
1177 if ((vp
->v_iocount
== 1) && (vp
->v_usecount
== 0)) {
1178 vnode_reclaim_internal(vp
, 1, 0);
1184 if (vp
== NULL
|| vp
->v_tag
!= VT_NON
) {
1185 MALLOC_ZONE(nvp
->v_specinfo
, struct specinfo
*, sizeof(struct specinfo
),
1186 M_SPECINFO
, M_WAITOK
);
1187 bzero(nvp
->v_specinfo
, sizeof(struct specinfo
));
1188 nvp
->v_rdev
= nvp_rdev
;
1189 nvp
->v_specflags
= 0;
1190 nvp
->v_speclastr
= -1;
1193 nvp
->v_hashchain
= vpp
;
1194 nvp
->v_specnext
= *vpp
;
1199 nvp
->v_flag
|= VALIASED
;
1200 vp
->v_flag
|= VALIASED
;
1211 * Get a reference on a particular vnode and lock it if requested.
1212 * If the vnode was on the inactive list, remove it from the list.
1213 * If the vnode was on the free list, remove it from the list and
1214 * move it to inactive list as needed.
1215 * The vnode lock bit is set if the vnode is being eliminated in
1216 * vgone. The process is awakened when the transition is completed,
1217 * and an error returned to indicate that the vnode is no longer
1218 * usable (possibly having been changed to a new file system type).
1221 vget_internal(vnode_t vp
, int vid
, int vflags
)
1228 if (vflags
& VNODE_WITHID
)
1231 vpid
= vp
->v_id
; // save off the original v_id
1233 if ((vflags
& VNODE_WRITEABLE
) && (vp
->v_writecount
== 0))
1235 * vnode to be returned only if it has writers opened
1239 error
= vnode_getiocount(vp
, 1, vpid
, vflags
);
1247 vnode_ref(vnode_t vp
)
1250 return (vnode_ref_ext(vp
, 0));
1254 vnode_ref_ext(vnode_t vp
, int fmode
)
1261 * once all the current call sites have been fixed to insure they have
1262 * taken an iocount, we can toughen this assert up and insist that the
1263 * iocount is non-zero... a non-zero usecount doesn't insure correctness
1265 if (vp
->v_iocount
<= 0 && vp
->v_usecount
<= 0)
1266 panic("vnode_ref_ext: vp %x has no valid reference %d, %d", vp
, vp
->v_iocount
, vp
->v_usecount
);
1269 * if you are the owner of drain/termination, can acquire usecount
1271 if ((vp
->v_lflag
& (VL_DRAIN
| VL_TERMINATE
| VL_DEAD
))) {
1272 if (vp
->v_owner
!= current_thread()) {
1279 if (fmode
& FWRITE
) {
1280 if (++vp
->v_writecount
<= 0)
1281 panic("vnode_ref_ext: v_writecount");
1283 if (fmode
& O_EVTONLY
) {
1284 if (++vp
->v_kusecount
<= 0)
1285 panic("vnode_ref_ext: v_kusecount");
1295 * put the vnode on appropriate free list.
1296 * called with vnode LOCKED
1299 vnode_list_add(vnode_t vp
)
1303 * if it is already on a list or non zero references return
1305 if (VONLIST(vp
) || (vp
->v_usecount
!= 0) || (vp
->v_iocount
!= 0))
1310 * insert at tail of LRU list or at head if VAGE or VL_DEAD is set
1312 if ((vp
->v_flag
& VAGE
) || (vp
->v_lflag
& VL_DEAD
)) {
1313 TAILQ_INSERT_HEAD(&vnode_free_list
, vp
, v_freelist
);
1314 vp
->v_flag
&= ~VAGE
;
1316 TAILQ_INSERT_TAIL(&vnode_free_list
, vp
, v_freelist
);
1320 vnode_list_unlock();
1324 * remove the vnode from appropriate free list.
1327 vnode_list_remove(vnode_t vp
)
1330 * we want to avoid taking the list lock
1331 * in the case where we're not on the free
1332 * list... this will be true for most
1333 * directories and any currently in use files
1335 * we're guaranteed that we can't go from
1336 * the not-on-list state to the on-list
1337 * state since we hold the vnode lock...
1338 * all calls to vnode_list_add are done
1339 * under the vnode lock... so we can
1340 * check for that condition (the prevelant one)
1341 * without taking the list lock
1346 * however, we're not guaranteed that
1347 * we won't go from the on-list state
1348 * to the non-on-list state until we
1349 * hold the vnode_list_lock... this
1350 * is due to new_vnode removing vnodes
1351 * from the free list uder the list_lock
1352 * w/o the vnode lock... so we need to
1353 * check again whether we're currently
1357 VREMFREE("vnode_list_remove", vp
);
1360 vnode_list_unlock();
1366 vnode_rele(vnode_t vp
)
1368 vnode_rele_internal(vp
, 0, 0, 0);
1373 vnode_rele_ext(vnode_t vp
, int fmode
, int dont_reenter
)
1375 vnode_rele_internal(vp
, fmode
, dont_reenter
, 0);
1380 vnode_rele_internal(vnode_t vp
, int fmode
, int dont_reenter
, int locked
)
1382 struct vfs_context context
;
1387 if (--vp
->v_usecount
< 0)
1388 panic("vnode_rele_ext: vp %x usecount -ve : %d", vp
, vp
->v_usecount
);
1390 if (fmode
& FWRITE
) {
1391 if (--vp
->v_writecount
< 0)
1392 panic("vnode_rele_ext: vp %x writecount -ve : %d", vp
, vp
->v_writecount
);
1394 if (fmode
& O_EVTONLY
) {
1395 if (--vp
->v_kusecount
< 0)
1396 panic("vnode_rele_ext: vp %x kusecount -ve : %d", vp
, vp
->v_kusecount
);
1398 if ((vp
->v_iocount
> 0) || (vp
->v_usecount
> 0)) {
1400 * vnode is still busy... if we're the last
1401 * usecount, mark for a future call to VNOP_INACTIVE
1402 * when the iocount finally drops to 0
1404 if (vp
->v_usecount
== 0) {
1405 vp
->v_lflag
|= VL_NEEDINACTIVE
;
1406 vp
->v_flag
&= ~(VNOCACHE_DATA
| VRAOFF
);
1412 vp
->v_flag
&= ~(VNOCACHE_DATA
| VRAOFF
);
1414 if ( (vp
->v_lflag
& (VL_TERMINATE
| VL_DEAD
)) || dont_reenter
) {
1416 * vnode is being cleaned, or
1417 * we've requested that we don't reenter
1418 * the filesystem on this release... in
1419 * this case, we'll mark the vnode aged
1420 * if it's been marked for termination
1423 if ( !(vp
->v_lflag
& (VL_TERMINATE
| VL_DEAD
| VL_MARKTERM
)) )
1424 vp
->v_lflag
|= VL_NEEDINACTIVE
;
1433 * at this point both the iocount and usecount
1435 * pick up an iocount so that we can call
1436 * VNOP_INACTIVE with the vnode lock unheld
1442 vp
->v_lflag
&= ~VL_NEEDINACTIVE
;
1445 context
.vc_proc
= current_proc();
1446 context
.vc_ucred
= kauth_cred_get();
1447 VNOP_INACTIVE(vp
, &context
);
1451 * because we dropped the vnode lock to call VNOP_INACTIVE
1452 * the state of the vnode may have changed... we may have
1453 * picked up an iocount, usecount or the MARKTERM may have
1454 * been set... we need to reevaluate the reference counts
1455 * to determine if we can call vnode_reclaim_internal at
1456 * this point... if the reference counts are up, we'll pick
1457 * up the MARKTERM state when they get subsequently dropped
1459 if ( (vp
->v_iocount
== 1) && (vp
->v_usecount
== 0) &&
1460 ((vp
->v_lflag
& (VL_MARKTERM
| VL_TERMINATE
| VL_DEAD
)) == VL_MARKTERM
)) {
1463 ut
= get_bsdthread_info(current_thread());
1465 if (ut
->uu_defer_reclaims
) {
1466 vp
->v_defer_reclaimlist
= ut
->uu_vreclaims
;
1467 ut
->uu_vreclaims
= vp
;
1470 vnode_reclaim_internal(vp
, 1, 0);
1472 vnode_dropiocount(vp
, 1);
1481 * Remove any vnodes in the vnode table belonging to mount point mp.
1483 * If MNT_NOFORCE is specified, there should not be any active ones,
1484 * return error if any are found (nb: this is a user error, not a
1485 * system error). If MNT_FORCE is specified, detach any active vnodes
1489 int busyprt
= 0; /* print out busy vnodes */
1491 struct ctldebug debug1
= { "busyprt", &busyprt
};
1496 vflush(mp
, skipvp
, flags
)
1498 struct vnode
*skipvp
;
1501 struct proc
*p
= current_proc();
1508 vnode_iterate_setup(mp
);
1510 * On regular unmounts(not forced) do a
1511 * quick check for vnodes to be in use. This
1512 * preserves the caching of vnodes. automounter
1513 * tries unmounting every so often to see whether
1514 * it is still busy or not.
1516 if ((flags
& FORCECLOSE
)==0) {
1517 if (vnode_umount_preflight(mp
, skipvp
, flags
)) {
1518 vnode_iterate_clear(mp
);
1524 /* it is returns 0 then there is nothing to do */
1525 retval
= vnode_iterate_prepare(mp
);
1528 vnode_iterate_clear(mp
);
1533 /* iterate over all the vnodes */
1534 while (!TAILQ_EMPTY(&mp
->mnt_workerqueue
)) {
1535 vp
= TAILQ_FIRST(&mp
->mnt_workerqueue
);
1536 TAILQ_REMOVE(&mp
->mnt_workerqueue
, vp
, v_mntvnodes
);
1537 TAILQ_INSERT_TAIL(&mp
->mnt_vnodelist
, vp
, v_mntvnodes
);
1538 if ( (vp
->v_mount
!= mp
) || (vp
== skipvp
)) {
1545 if ((vp
->v_id
!= vid
) || ((vp
->v_lflag
& (VL_DEAD
| VL_TERMINATE
)))) {
1552 * If requested, skip over vnodes marked VSYSTEM.
1553 * Skip over all vnodes marked VNOFLUSH.
1555 if ((flags
& SKIPSYSTEM
) && ((vp
->v_flag
& VSYSTEM
) ||
1556 (vp
->v_flag
& VNOFLUSH
))) {
1562 * If requested, skip over vnodes marked VSWAP.
1564 if ((flags
& SKIPSWAP
) && (vp
->v_flag
& VSWAP
)) {
1570 * If requested, skip over vnodes marked VSWAP.
1572 if ((flags
& SKIPROOT
) && (vp
->v_flag
& VROOT
)) {
1578 * If WRITECLOSE is set, only flush out regular file
1579 * vnodes open for writing.
1581 if ((flags
& WRITECLOSE
) &&
1582 (vp
->v_writecount
== 0 || vp
->v_type
!= VREG
)) {
1588 * If the real usecount is 0, all we need to do is clear
1589 * out the vnode data structures and we are done.
1591 if (((vp
->v_usecount
== 0) ||
1592 ((vp
->v_usecount
- vp
->v_kusecount
) == 0))) {
1593 vp
->v_iocount
++; /* so that drain waits for * other iocounts */
1597 vnode_reclaim_internal(vp
, 1, 0);
1598 vnode_dropiocount(vp
, 1);
1607 * If FORCECLOSE is set, forcibly close the vnode.
1608 * For block or character devices, revert to an
1609 * anonymous device. For all other files, just kill them.
1611 if (flags
& FORCECLOSE
) {
1612 if (vp
->v_type
!= VBLK
&& vp
->v_type
!= VCHR
) {
1613 vp
->v_iocount
++; /* so that drain waits * for other iocounts */
1617 vnode_reclaim_internal(vp
, 1, 0);
1618 vnode_dropiocount(vp
, 1);
1623 vp
->v_lflag
&= ~VL_DEAD
;
1624 vp
->v_op
= spec_vnodeop_p
;
1632 vprint("vflush: busy vnode", vp
);
1639 /* At this point the worker queue is completed */
1640 if (busy
&& ((flags
& FORCECLOSE
)==0) && reclaimed
) {
1643 (void)vnode_iterate_reloadq(mp
);
1644 /* returned with mount lock held */
1648 /* if new vnodes were created in between retry the reclaim */
1649 if ( vnode_iterate_reloadq(mp
) != 0) {
1650 if (!(busy
&& ((flags
& FORCECLOSE
)==0)))
1653 vnode_iterate_clear(mp
);
1656 if (busy
&& ((flags
& FORCECLOSE
)==0))
1661 int num_recycledvnodes
=0;
1663 * Disassociate the underlying file system from a vnode.
1664 * The vnode lock is held on entry.
1667 vclean(vnode_t vp
, int flags
, proc_t p
)
1669 struct vfs_context context
;
1672 int already_terminating
;
1673 kauth_cred_t ucred
= NULL
;
1675 context
.vc_proc
= p
;
1676 context
.vc_ucred
= kauth_cred_get();
1679 * Check to see if the vnode is in use.
1680 * If so we have to reference it before we clean it out
1681 * so that its count cannot fall to zero and generate a
1682 * race against ourselves to recycle it.
1684 active
= vp
->v_usecount
;
1687 * just in case we missed sending a needed
1688 * VNOP_INACTIVE, we'll do it now
1690 need_inactive
= (vp
->v_lflag
& VL_NEEDINACTIVE
);
1692 vp
->v_lflag
&= ~VL_NEEDINACTIVE
;
1695 * Prevent the vnode from being recycled or
1696 * brought into use while we clean it out.
1698 already_terminating
= (vp
->v_lflag
& VL_TERMINATE
);
1700 vp
->v_lflag
|= VL_TERMINATE
;
1703 * remove the vnode from any mount list
1706 insmntque(vp
, (struct mount
*)0);
1714 kauth_cred_rele(ucred
);
1716 OSAddAtomic(1, &num_recycledvnodes
);
1718 * purge from the name cache as early as possible...
1722 if (active
&& (flags
& DOCLOSE
))
1723 VNOP_CLOSE(vp
, IO_NDELAY
, &context
);
1726 * Clean out any buffers associated with the vnode.
1728 if (flags
& DOCLOSE
) {
1730 if (vp
->v_tag
== VT_NFS
)
1731 nfs_vinvalbuf(vp
, V_SAVE
, NOCRED
, p
, 0);
1735 VNOP_FSYNC(vp
, MNT_WAIT
, &context
);
1736 buf_invalidateblks(vp
, BUF_WRITE_DATA
, 0, 0);
1738 if (UBCINFOEXISTS(vp
))
1740 * Clean the pages in VM.
1742 (void)ubc_sync_range(vp
, (off_t
)0, ubc_getsize(vp
), UBC_PUSHALL
);
1744 if (UBCINFOEXISTS(vp
))
1745 cluster_release(vp
->v_ubcinfo
);
1747 if (active
|| need_inactive
)
1748 VNOP_INACTIVE(vp
, &context
);
1750 /* Destroy ubc named reference */
1751 ubc_destroy_named(vp
);
1754 * Reclaim the vnode.
1756 if (VNOP_RECLAIM(vp
, &context
))
1757 panic("vclean: cannot reclaim");
1759 // make sure the name & parent ptrs get cleaned out!
1760 vnode_update_identity(vp
, NULLVP
, NULL
, 0, 0, VNODE_UPDATE_PARENT
| VNODE_UPDATE_NAME
);
1764 vp
->v_mount
= dead_mountp
;
1765 vp
->v_op
= dead_vnodeop_p
;
1769 vp
->v_lflag
|= VL_DEAD
;
1771 if (already_terminating
== 0) {
1772 vp
->v_lflag
&= ~VL_TERMINATE
;
1774 * Done with purge, notify sleepers of the grim news.
1776 if (vp
->v_lflag
& VL_TERMWANT
) {
1777 vp
->v_lflag
&= ~VL_TERMWANT
;
1778 wakeup(&vp
->v_lflag
);
1784 * Eliminate all activity associated with the requested vnode
1785 * and with all vnodes aliased to the requested vnode.
1788 vn_revoke(vnode_t vp
, int flags
, __unused vfs_context_t a_context
)
1794 if ((flags
& REVOKEALL
) == 0)
1795 panic("vnop_revoke");
1798 if (vp
->v_flag
& VALIASED
) {
1800 * If a vgone (or vclean) is already in progress,
1801 * wait until it is done and return.
1804 if (vp
->v_lflag
& VL_TERMINATE
) {
1810 * Ensure that vp will not be vgone'd while we
1811 * are eliminating its aliases.
1814 while (vp
->v_flag
& VALIASED
) {
1815 for (vq
= *vp
->v_hashchain
; vq
; vq
= vq
->v_specnext
) {
1816 if (vq
->v_rdev
!= vp
->v_rdev
||
1817 vq
->v_type
!= vp
->v_type
|| vp
== vq
)
1821 if (vnode_getwithvid(vq
,vid
)){
1825 vnode_reclaim_internal(vq
, 0, 0);
1833 vnode_reclaim_internal(vp
, 0, 0);
1839 * Recycle an unused vnode to the front of the free list.
1840 * Release the passed interlock if the vnode will be recycled.
1848 if (vp
->v_iocount
|| vp
->v_usecount
) {
1849 vp
->v_lflag
|= VL_MARKTERM
;
1853 vnode_reclaim_internal(vp
, 1, 0);
1860 vnode_reload(vnode_t vp
)
1864 if ((vp
->v_iocount
> 1) || vp
->v_usecount
) {
1868 if (vp
->v_iocount
<= 0)
1869 panic("vnode_reload with no iocount %d", vp
->v_iocount
);
1871 /* mark for release when iocount is dopped */
1872 vp
->v_lflag
|= VL_MARKTERM
;
1886 * Clean out the filesystem specific data.
1887 * vclean also takes care of removing the
1888 * vnode from any mount list it might be on
1890 vclean(vp
, DOCLOSE
, current_proc());
1893 * If special device, remove it from special device alias list
1896 if ((vp
->v_type
== VBLK
|| vp
->v_type
== VCHR
) && vp
->v_specinfo
!= 0) {
1898 if (*vp
->v_hashchain
== vp
) {
1899 *vp
->v_hashchain
= vp
->v_specnext
;
1901 for (vq
= *vp
->v_hashchain
; vq
; vq
= vq
->v_specnext
) {
1902 if (vq
->v_specnext
!= vp
)
1904 vq
->v_specnext
= vp
->v_specnext
;
1908 panic("missing bdev");
1910 if (vp
->v_flag
& VALIASED
) {
1912 for (vq
= *vp
->v_hashchain
; vq
; vq
= vq
->v_specnext
) {
1913 if (vq
->v_rdev
!= vp
->v_rdev
||
1914 vq
->v_type
!= vp
->v_type
)
1921 panic("missing alias");
1923 vx
->v_flag
&= ~VALIASED
;
1924 vp
->v_flag
&= ~VALIASED
;
1928 struct specinfo
*tmp
= vp
->v_specinfo
;
1929 vp
->v_specinfo
= NULL
;
1930 FREE_ZONE((void *)tmp
, sizeof(struct specinfo
), M_SPECINFO
);
1936 * Lookup a vnode by device number.
1939 check_mountedon(dev_t dev
, enum vtype type
, int *errorp
)
1947 for (vp
= speclisth
[SPECHASH(dev
)]; vp
; vp
= vp
->v_specnext
) {
1948 if (dev
!= vp
->v_rdev
|| type
!= vp
->v_type
)
1952 if (vnode_getwithvid(vp
,vid
))
1955 if ((vp
->v_usecount
> 0) || (vp
->v_iocount
> 1)) {
1957 if ((*errorp
= vfs_mountedon(vp
)) != 0)
1969 * Calculate the total number of references to a special device.
1979 if ((vp
->v_flag
& VALIASED
) == 0)
1980 return (vp
->v_usecount
- vp
->v_kusecount
);
1983 for (count
= 0, vq
= *vp
->v_hashchain
; vq
; vq
= vnext
) {
1984 vnext
= vq
->v_specnext
;
1985 if (vq
->v_rdev
!= vp
->v_rdev
|| vq
->v_type
!= vp
->v_type
)
1990 if (vnode_getwithvid(vq
, vid
)) {
1994 * Alias, but not in use, so flush it out.
1997 if ((vq
->v_usecount
== 0) && (vq
->v_iocount
== 1) && vq
!= vp
) {
1998 vnode_reclaim_internal(vq
, 1, 0);
2003 count
+= (vq
->v_usecount
- vq
->v_kusecount
);
2014 int prtactive
= 0; /* 1 => print out reclaim of active vnodes */
2017 * Print out a description of a vnode.
2019 static char *typename
[] =
2020 { "VNON", "VREG", "VDIR", "VBLK", "VCHR", "VLNK", "VSOCK", "VFIFO", "VBAD" };
2023 vprint(const char *label
, struct vnode
*vp
)
2028 printf("%s: ", label
);
2029 printf("type %s, usecount %d, writecount %d",
2030 typename
[vp
->v_type
], vp
->v_usecount
, vp
->v_writecount
);
2032 if (vp
->v_flag
& VROOT
)
2033 strcat(sbuf
, "|VROOT");
2034 if (vp
->v_flag
& VTEXT
)
2035 strcat(sbuf
, "|VTEXT");
2036 if (vp
->v_flag
& VSYSTEM
)
2037 strcat(sbuf
, "|VSYSTEM");
2038 if (vp
->v_flag
& VNOFLUSH
)
2039 strcat(sbuf
, "|VNOFLUSH");
2040 if (vp
->v_flag
& VBWAIT
)
2041 strcat(sbuf
, "|VBWAIT");
2042 if (vp
->v_flag
& VALIASED
)
2043 strcat(sbuf
, "|VALIASED");
2044 if (sbuf
[0] != '\0')
2045 printf(" flags (%s)", &sbuf
[1]);
2050 vn_getpath(struct vnode
*vp
, char *pathbuf
, int *len
)
2052 return build_path(vp
, pathbuf
, *len
, len
);
2056 static char *extension_table
=NULL
;
2058 static int max_ext_width
;
2061 extension_cmp(void *a
, void *b
)
2063 return (strlen((char *)a
) - strlen((char *)b
));
2068 // This is the api LaunchServices uses to inform the kernel
2069 // the list of package extensions to ignore.
2071 // Internally we keep the list sorted by the length of the
2072 // the extension (from longest to shortest). We sort the
2073 // list of extensions so that we can speed up our searches
2074 // when comparing file names -- we only compare extensions
2075 // that could possibly fit into the file name, not all of
2076 // them (i.e. a short 8 character name can't have an 8
2077 // character extension).
2079 __private_extern__
int
2080 set_package_extensions_table(void *data
, int nentries
, int maxwidth
)
2082 char *new_exts
, *ptr
;
2085 if (nentries
<= 0 || nentries
> 1024 || maxwidth
<= 0 || maxwidth
> 255) {
2089 MALLOC(new_exts
, char *, nentries
* maxwidth
, M_TEMP
, M_WAITOK
);
2091 error
= copyin(CAST_USER_ADDR_T(data
), new_exts
, nentries
* maxwidth
);
2093 FREE(new_exts
, M_TEMP
);
2097 if (extension_table
) {
2098 FREE(extension_table
, M_TEMP
);
2100 extension_table
= new_exts
;
2102 max_ext_width
= maxwidth
;
2104 qsort(extension_table
, nexts
, maxwidth
, extension_cmp
);
2110 __private_extern__
int
2111 is_package_name(char *name
, int len
)
2114 char *ptr
, *name_ext
;
2121 for(ptr
=name
; *ptr
!= '\0'; ptr
++) {
2127 // if there is no "." extension, it can't match
2128 if (name_ext
== NULL
) {
2132 // advance over the "."
2135 // now iterate over all the extensions to see if any match
2136 ptr
= &extension_table
[0];
2137 for(i
=0; i
< nexts
; i
++, ptr
+=max_ext_width
) {
2138 extlen
= strlen(ptr
);
2139 if (strncasecmp(name_ext
, ptr
, extlen
) == 0 && name_ext
[extlen
] == '\0') {
2145 // if we get here, no extension matched
2150 vn_path_package_check(__unused vnode_t vp
, char *path
, int pathlen
, int *component
)
2161 while(end
< path
+ pathlen
&& *end
!= '\0') {
2162 while(end
< path
+ pathlen
&& *end
== '/' && *end
!= '\0') {
2168 while(end
< path
+ pathlen
&& *end
!= '/' && *end
!= '\0') {
2172 if (end
> path
+ pathlen
) {
2173 // hmm, string wasn't null terminated
2178 if (is_package_name(ptr
, end
- ptr
)) {
2192 * Top level filesystem related information gathering.
2194 extern unsigned int vfs_nummntops
;
2197 vfs_sysctl(int *name
, u_int namelen
, user_addr_t oldp
, size_t *oldlenp
,
2198 user_addr_t newp
, size_t newlen
, struct proc
*p
)
2200 struct vfstable
*vfsp
;
2204 struct vfsconf
*vfsc
;
2207 * The VFS_NUMMNTOPS shouldn't be at name[0] since
2208 * is a VFS generic variable. So now we must check
2209 * namelen so we don't end up covering any UFS
2210 * variables (sinc UFS vfc_typenum is 1).
2212 * It should have been:
2213 * name[0]: VFS_GENERIC
2214 * name[1]: VFS_NUMMNTOPS
2216 if (namelen
== 1 && name
[0] == VFS_NUMMNTOPS
) {
2217 return (sysctl_rdint(oldp
, oldlenp
, newp
, vfs_nummntops
));
2220 /* all sysctl names at this level are at least name and field */
2222 return (EISDIR
); /* overloaded */
2223 if (name
[0] != VFS_GENERIC
) {
2224 struct vfs_context context
;
2226 for (vfsp
= vfsconf
; vfsp
; vfsp
= vfsp
->vfc_next
)
2227 if (vfsp
->vfc_typenum
== name
[0])
2231 context
.vc_proc
= p
;
2232 context
.vc_ucred
= kauth_cred_get();
2234 return ((*vfsp
->vfc_vfsops
->vfs_sysctl
)(&name
[1], namelen
- 1,
2235 oldp
, oldlenp
, newp
, newlen
, &context
));
2238 case VFS_MAXTYPENUM
:
2239 return (sysctl_rdint(oldp
, oldlenp
, newp
, maxvfsconf
));
2242 return (ENOTDIR
); /* overloaded */
2243 for (vfsp
= vfsconf
; vfsp
; vfsp
= vfsp
->vfc_next
)
2244 if (vfsp
->vfc_typenum
== name
[2])
2248 vfsc
= (struct vfsconf
*)vfsp
;
2249 if (proc_is64bit(p
)) {
2250 struct user_vfsconf usr_vfsc
;
2251 usr_vfsc
.vfc_vfsops
= CAST_USER_ADDR_T(vfsc
->vfc_vfsops
);
2252 bcopy(vfsc
->vfc_name
, usr_vfsc
.vfc_name
, sizeof(usr_vfsc
.vfc_name
));
2253 usr_vfsc
.vfc_typenum
= vfsc
->vfc_typenum
;
2254 usr_vfsc
.vfc_refcount
= vfsc
->vfc_refcount
;
2255 usr_vfsc
.vfc_flags
= vfsc
->vfc_flags
;
2256 usr_vfsc
.vfc_mountroot
= CAST_USER_ADDR_T(vfsc
->vfc_mountroot
);
2257 usr_vfsc
.vfc_next
= CAST_USER_ADDR_T(vfsc
->vfc_next
);
2258 return (sysctl_rdstruct(oldp
, oldlenp
, newp
, &usr_vfsc
,
2262 return (sysctl_rdstruct(oldp
, oldlenp
, newp
, vfsc
,
2263 sizeof(struct vfsconf
)));
2266 case VFS_SET_PACKAGE_EXTS
:
2267 return set_package_extensions_table((void *)name
[1], name
[2], name
[3]);
2270 * We need to get back into the general MIB, so we need to re-prepend
2271 * CTL_VFS to our name and try userland_sysctl().
2273 usernamelen
= namelen
+ 1;
2274 MALLOC(username
, int *, usernamelen
* sizeof(*username
),
2276 bcopy(name
, username
+ 1, namelen
* sizeof(*name
));
2277 username
[0] = CTL_VFS
;
2278 error
= userland_sysctl(p
, username
, usernamelen
, oldp
,
2279 oldlenp
, 1, newp
, newlen
, oldlenp
);
2280 FREE(username
, M_TEMP
);
2284 int kinfo_vdebug
= 1;
2285 #define KINFO_VNODESLOP 10
2287 * Dump vnode list (via sysctl).
2288 * Copyout address of vnode followed by vnode.
2292 sysctl_vnode(__unused user_addr_t where
, __unused
size_t *sizep
)
2295 struct mount
*mp
, *nmp
;
2296 struct vnode
*nvp
, *vp
;
2297 char *bp
= where
, *savebp
;
2301 #define VPTRSZ sizeof (struct vnode *)
2302 #define VNODESZ sizeof (struct vnode)
2303 if (where
== NULL
) {
2304 *sizep
= (numvnodes
+ KINFO_VNODESLOP
) * (VPTRSZ
+ VNODESZ
);
2307 ewhere
= where
+ *sizep
;
2309 for (mp
= mountlist
.cqh_first
; mp
!= (void *)&mountlist
; mp
= nmp
) {
2310 if (vfs_busy(mp
, LK_NOWAIT
)) {
2311 nmp
= mp
->mnt_list
.cqe_next
;
2316 TAILQ_FOREACH(vp
, &mp
->mnt_vnodelist
, v_mntvnodes
) {
2318 * Check that the vp is still associated with
2319 * this filesystem. RACE: could have been
2320 * recycled onto the same filesystem.
2322 if (vp
->v_mount
!= mp
) {
2324 printf("kinfo: vp changed\n");
2328 if (bp
+ VPTRSZ
+ VNODESZ
> ewhere
) {
2330 *sizep
= bp
- where
;
2333 if ((error
= copyout((caddr_t
)&vp
, bp
, VPTRSZ
)) ||
2334 (error
= copyout((caddr_t
)vp
, bp
+ VPTRSZ
, VNODESZ
))) {
2338 bp
+= VPTRSZ
+ VNODESZ
;
2340 nmp
= mp
->mnt_list
.cqe_next
;
2344 *sizep
= bp
- where
;
2352 * Check to see if a filesystem is mounted on a block device.
2362 if (vp
->v_specflags
& SI_MOUNTEDON
) {
2366 if (vp
->v_flag
& VALIASED
) {
2367 for (vq
= *vp
->v_hashchain
; vq
; vq
= vq
->v_specnext
) {
2368 if (vq
->v_rdev
!= vp
->v_rdev
||
2369 vq
->v_type
!= vp
->v_type
)
2371 if (vq
->v_specflags
& SI_MOUNTEDON
) {
2383 * Unmount all filesystems. The list is traversed in reverse order
2384 * of mounting to avoid dependencies.
2386 __private_extern__
void
2390 struct proc
*p
= current_proc();
2394 * Since this only runs when rebooting, it is not interlocked.
2397 while(!TAILQ_EMPTY(&mountlist
)) {
2398 mp
= TAILQ_LAST(&mountlist
, mntlist
);
2399 mount_list_unlock();
2400 error
= dounmount(mp
, MNT_FORCE
, p
);
2403 TAILQ_REMOVE(&mountlist
, mp
, mnt_list
);
2404 printf("unmount of %s failed (", mp
->mnt_vfsstat
.f_mntonname
);
2408 printf("%d)\n", error
);
2413 mount_list_unlock();
2418 * This routine is called from vnode_pager_no_senders()
2419 * which in turn can be called with vnode locked by vnode_uncache()
2420 * But it could also get called as a result of vm_object_cache_trim().
2421 * In that case lock state is unknown.
2422 * AGE the vnode so that it gets recycled quickly.
2424 __private_extern__
void
2425 vnode_pager_vrele(struct vnode
*vp
)
2429 if (!ISSET(vp
->v_lflag
, VL_TERMINATE
))
2430 panic("vnode_pager_vrele: vp not in termination");
2431 vp
->v_lflag
&= ~VNAMED_UBC
;
2433 if (UBCINFOEXISTS(vp
)) {
2434 struct ubc_info
*uip
= vp
->v_ubcinfo
;
2436 if (ISSET(uip
->ui_flags
, UI_WASMAPPED
))
2437 SET(vp
->v_flag
, VWASMAPPED
);
2438 vp
->v_ubcinfo
= UBC_INFO_NULL
;
2440 ubc_info_deallocate(uip
);
2442 panic("NO ubcinfo in vnode_pager_vrele");
2446 wakeup(&vp
->v_lflag
);
2450 #include <sys/disk.h>
2453 vfs_init_io_attributes(vnode_t devvp
, mount_t mp
)
2457 off_t writeblockcnt
;
2466 struct vfs_context context
;
2468 proc_t p
= current_proc();
2470 context
.vc_proc
= p
;
2471 context
.vc_ucred
= kauth_cred_get();
2475 * determine if this mount point exists on the same device as the root
2476 * partition... if so, then it comes under the hard throttle control
2479 static int rootunit
= -1;
2481 if (rootunit
== -1) {
2482 if (VNOP_IOCTL(rootvp
, DKIOCGETBSDUNIT
, (caddr_t
)&rootunit
, 0, &context
))
2484 else if (rootvp
== devvp
)
2485 mp
->mnt_kern_flag
|= MNTK_ROOTDEV
;
2487 if (devvp
!= rootvp
&& rootunit
!= -1) {
2488 if (VNOP_IOCTL(devvp
, DKIOCGETBSDUNIT
, (caddr_t
)&thisunit
, 0, &context
) == 0) {
2489 if (thisunit
== rootunit
)
2490 mp
->mnt_kern_flag
|= MNTK_ROOTDEV
;
2494 * force the spec device to re-cache
2495 * the underlying block size in case
2496 * the filesystem overrode the initial value
2498 set_fsblocksize(devvp
);
2501 if ((error
= VNOP_IOCTL(devvp
, DKIOCGETBLOCKSIZE
,
2502 (caddr_t
)&blksize
, 0, &context
)))
2505 mp
->mnt_devblocksize
= blksize
;
2507 if (VNOP_IOCTL(devvp
, DKIOCISVIRTUAL
, (caddr_t
)&isvirtual
, 0, &context
) == 0) {
2509 mp
->mnt_kern_flag
|= MNTK_VIRTUALDEV
;
2512 if ((error
= VNOP_IOCTL(devvp
, DKIOCGETMAXBLOCKCOUNTREAD
,
2513 (caddr_t
)&readblockcnt
, 0, &context
)))
2516 if ((error
= VNOP_IOCTL(devvp
, DKIOCGETMAXBLOCKCOUNTWRITE
,
2517 (caddr_t
)&writeblockcnt
, 0, &context
)))
2520 if ((error
= VNOP_IOCTL(devvp
, DKIOCGETMAXBYTECOUNTREAD
,
2521 (caddr_t
)&readmaxcnt
, 0, &context
)))
2524 if ((error
= VNOP_IOCTL(devvp
, DKIOCGETMAXBYTECOUNTWRITE
,
2525 (caddr_t
)&writemaxcnt
, 0, &context
)))
2528 if ((error
= VNOP_IOCTL(devvp
, DKIOCGETMAXSEGMENTCOUNTREAD
,
2529 (caddr_t
)&readsegcnt
, 0, &context
)))
2532 if ((error
= VNOP_IOCTL(devvp
, DKIOCGETMAXSEGMENTCOUNTWRITE
,
2533 (caddr_t
)&writesegcnt
, 0, &context
)))
2536 if ((error
= VNOP_IOCTL(devvp
, DKIOCGETMAXSEGMENTBYTECOUNTREAD
,
2537 (caddr_t
)&readsegsize
, 0, &context
)))
2540 if ((error
= VNOP_IOCTL(devvp
, DKIOCGETMAXSEGMENTBYTECOUNTWRITE
,
2541 (caddr_t
)&writesegsize
, 0, &context
)))
2545 temp
= (readmaxcnt
> UINT32_MAX
) ? UINT32_MAX
: readmaxcnt
;
2548 temp
= readblockcnt
* blksize
;
2549 temp
= (temp
> UINT32_MAX
) ? UINT32_MAX
: temp
;
2553 mp
->mnt_maxreadcnt
= (u_int32_t
)temp
;
2556 temp
= (writemaxcnt
> UINT32_MAX
) ? UINT32_MAX
: writemaxcnt
;
2558 if (writeblockcnt
) {
2559 temp
= writeblockcnt
* blksize
;
2560 temp
= (temp
> UINT32_MAX
) ? UINT32_MAX
: temp
;
2564 mp
->mnt_maxwritecnt
= (u_int32_t
)temp
;
2567 temp
= (readsegcnt
> UINT16_MAX
) ? UINT16_MAX
: readsegcnt
;
2568 mp
->mnt_segreadcnt
= (u_int16_t
)temp
;
2571 temp
= (writesegcnt
> UINT16_MAX
) ? UINT16_MAX
: writesegcnt
;
2572 mp
->mnt_segwritecnt
= (u_int16_t
)temp
;
2575 temp
= (readsegsize
> UINT32_MAX
) ? UINT32_MAX
: readsegsize
;
2577 temp
= mp
->mnt_maxreadcnt
;
2578 mp
->mnt_maxsegreadsize
= (u_int32_t
)temp
;
2581 temp
= (writesegsize
> UINT32_MAX
) ? UINT32_MAX
: writesegsize
;
2583 temp
= mp
->mnt_maxwritecnt
;
2584 mp
->mnt_maxsegwritesize
= (u_int32_t
)temp
;
2589 static struct klist fs_klist
;
2592 vfs_event_init(void)
2595 klist_init(&fs_klist
);
2599 vfs_event_signal(__unused fsid_t
*fsid
, u_int32_t event
, __unused
intptr_t data
)
2602 KNOTE(&fs_klist
, event
);
2606 * return the number of mounted filesystems.
2609 sysctl_vfs_getvfscnt(void)
2611 return(mount_getvfscnt());
2616 mount_getvfscnt(void)
2622 mount_list_unlock();
2630 mount_fillfsids(fsid_t
*fsidlst
, int count
)
2637 TAILQ_FOREACH(mp
, &mountlist
, mnt_list
) {
2638 if (actual
<= count
) {
2639 fsidlst
[actual
] = mp
->mnt_vfsstat
.f_fsid
;
2643 mount_list_unlock();
2649 * fill in the array of fsid_t's up to a max of 'count', the actual
2650 * number filled in will be set in '*actual'. If there are more fsid_t's
2651 * than room in fsidlst then ENOMEM will be returned and '*actual' will
2652 * have the actual count.
2653 * having *actual filled out even in the error case is depended upon.
2656 sysctl_vfs_getvfslist(fsid_t
*fsidlst
, int count
, int *actual
)
2662 TAILQ_FOREACH(mp
, &mountlist
, mnt_list
) {
2664 if (*actual
<= count
)
2665 fsidlst
[(*actual
) - 1] = mp
->mnt_vfsstat
.f_fsid
;
2667 mount_list_unlock();
2668 return (*actual
<= count
? 0 : ENOMEM
);
2672 sysctl_vfs_vfslist SYSCTL_HANDLER_ARGS
2678 /* This is a readonly node. */
2679 if (req
->newptr
!= USER_ADDR_NULL
)
2682 /* they are querying us so just return the space required. */
2683 if (req
->oldptr
== USER_ADDR_NULL
) {
2684 req
->oldidx
= sysctl_vfs_getvfscnt() * sizeof(fsid_t
);
2689 * Retrieve an accurate count of the amount of space required to copy
2690 * out all the fsids in the system.
2692 space
= req
->oldlen
;
2693 req
->oldlen
= sysctl_vfs_getvfscnt() * sizeof(fsid_t
);
2695 /* they didn't give us enough space. */
2696 if (space
< req
->oldlen
)
2699 MALLOC(fsidlst
, fsid_t
*, req
->oldlen
, M_TEMP
, M_WAITOK
);
2700 error
= sysctl_vfs_getvfslist(fsidlst
, req
->oldlen
/ sizeof(fsid_t
),
2703 * If we get back ENOMEM, then another mount has been added while we
2704 * slept in malloc above. If this is the case then try again.
2706 if (error
== ENOMEM
) {
2707 FREE(fsidlst
, M_TEMP
);
2708 req
->oldlen
= space
;
2712 error
= SYSCTL_OUT(req
, fsidlst
, actual
* sizeof(fsid_t
));
2714 FREE(fsidlst
, M_TEMP
);
2719 * Do a sysctl by fsid.
2722 sysctl_vfs_ctlbyfsid SYSCTL_HANDLER_ARGS
2725 struct user_vfsidctl user_vc
;
2727 struct vfsstatfs
*sp
;
2730 int error
, flags
, namelen
;
2731 struct vfs_context context
;
2732 boolean_t is_64_bit
;
2737 context
.vc_proc
= p
;
2738 context
.vc_ucred
= kauth_cred_get();
2739 is_64_bit
= proc_is64bit(p
);
2742 error
= SYSCTL_IN(req
, &user_vc
, sizeof(user_vc
));
2745 if (user_vc
.vc_vers
!= VFS_CTL_VERS1
)
2747 mp
= mount_list_lookupby_fsid(&user_vc
.vc_fsid
, 0, 0);
2750 error
= SYSCTL_IN(req
, &vc
, sizeof(vc
));
2753 if (vc
.vc_vers
!= VFS_CTL_VERS1
)
2755 mp
= mount_list_lookupby_fsid(&vc
.vc_fsid
, 0, 0);
2759 /* reset so that the fs specific code can fetch it. */
2762 * Note if this is a VFS_CTL then we pass the actual sysctl req
2763 * in for "oldp" so that the lower layer can DTRT and use the
2764 * SYSCTL_IN/OUT routines.
2766 if (mp
->mnt_op
->vfs_sysctl
!= NULL
) {
2768 if (vfs_64bitready(mp
)) {
2769 error
= mp
->mnt_op
->vfs_sysctl(name
, namelen
,
2770 CAST_USER_ADDR_T(req
),
2771 NULL
, USER_ADDR_NULL
, 0,
2779 error
= mp
->mnt_op
->vfs_sysctl(name
, namelen
,
2780 CAST_USER_ADDR_T(req
),
2781 NULL
, USER_ADDR_NULL
, 0,
2784 if (error
!= ENOTSUP
)
2788 case VFS_CTL_UMOUNT
:
2791 req
->newptr
= user_vc
.vc_ptr
;
2792 req
->newlen
= (size_t)user_vc
.vc_len
;
2795 req
->newptr
= CAST_USER_ADDR_T(vc
.vc_ptr
);
2796 req
->newlen
= vc
.vc_len
;
2798 error
= SYSCTL_IN(req
, &flags
, sizeof(flags
));
2801 error
= safedounmount(mp
, flags
, p
);
2803 case VFS_CTL_STATFS
:
2806 req
->newptr
= user_vc
.vc_ptr
;
2807 req
->newlen
= (size_t)user_vc
.vc_len
;
2810 req
->newptr
= CAST_USER_ADDR_T(vc
.vc_ptr
);
2811 req
->newlen
= vc
.vc_len
;
2813 error
= SYSCTL_IN(req
, &flags
, sizeof(flags
));
2816 sp
= &mp
->mnt_vfsstat
;
2817 if (((flags
& MNT_NOWAIT
) == 0 || (flags
& MNT_WAIT
)) &&
2818 (error
= vfs_update_vfsstat(mp
, &context
)))
2821 struct user_statfs sfs
;
2822 bzero(&sfs
, sizeof(sfs
));
2823 sfs
.f_flags
= mp
->mnt_flag
& MNT_VISFLAGMASK
;
2824 sfs
.f_type
= mp
->mnt_vtable
->vfc_typenum
;
2825 sfs
.f_bsize
= (user_long_t
)sp
->f_bsize
;
2826 sfs
.f_iosize
= (user_long_t
)sp
->f_iosize
;
2827 sfs
.f_blocks
= (user_long_t
)sp
->f_blocks
;
2828 sfs
.f_bfree
= (user_long_t
)sp
->f_bfree
;
2829 sfs
.f_bavail
= (user_long_t
)sp
->f_bavail
;
2830 sfs
.f_files
= (user_long_t
)sp
->f_files
;
2831 sfs
.f_ffree
= (user_long_t
)sp
->f_ffree
;
2832 sfs
.f_fsid
= sp
->f_fsid
;
2833 sfs
.f_owner
= sp
->f_owner
;
2835 strncpy(&sfs
.f_fstypename
, &sp
->f_fstypename
, MFSNAMELEN
-1);
2836 strncpy(&sfs
.f_mntonname
, &sp
->f_mntonname
, MNAMELEN
-1);
2837 strncpy(&sfs
.f_mntfromname
, &sp
->f_mntfromname
, MNAMELEN
-1);
2839 error
= SYSCTL_OUT(req
, &sfs
, sizeof(sfs
));
2843 bzero(&sfs
, sizeof(struct statfs
));
2844 sfs
.f_flags
= mp
->mnt_flag
& MNT_VISFLAGMASK
;
2845 sfs
.f_type
= mp
->mnt_vtable
->vfc_typenum
;
2848 * It's possible for there to be more than 2^^31 blocks in the filesystem, so we
2849 * have to fudge the numbers here in that case. We inflate the blocksize in order
2850 * to reflect the filesystem size as best we can.
2852 if (sp
->f_blocks
> LONG_MAX
) {
2856 * Work out how far we have to shift the block count down to make it fit.
2857 * Note that it's possible to have to shift so far that the resulting
2858 * blocksize would be unreportably large. At that point, we will clip
2859 * any values that don't fit.
2861 * For safety's sake, we also ensure that f_iosize is never reported as
2862 * being smaller than f_bsize.
2864 for (shift
= 0; shift
< 32; shift
++) {
2865 if ((sp
->f_blocks
>> shift
) <= LONG_MAX
)
2867 if ((sp
->f_bsize
<< (shift
+ 1)) > LONG_MAX
)
2870 #define __SHIFT_OR_CLIP(x, s) ((((x) >> (s)) > LONG_MAX) ? LONG_MAX : ((x) >> (s)))
2871 sfs
.f_blocks
= (long)__SHIFT_OR_CLIP(sp
->f_blocks
, shift
);
2872 sfs
.f_bfree
= (long)__SHIFT_OR_CLIP(sp
->f_bfree
, shift
);
2873 sfs
.f_bavail
= (long)__SHIFT_OR_CLIP(sp
->f_bavail
, shift
);
2874 #undef __SHIFT_OR_CLIP
2875 sfs
.f_bsize
= (long)(sp
->f_bsize
<< shift
);
2876 sfs
.f_iosize
= lmax(sp
->f_iosize
, sp
->f_bsize
);
2878 sfs
.f_bsize
= (long)sp
->f_bsize
;
2879 sfs
.f_iosize
= (long)sp
->f_iosize
;
2880 sfs
.f_blocks
= (long)sp
->f_blocks
;
2881 sfs
.f_bfree
= (long)sp
->f_bfree
;
2882 sfs
.f_bavail
= (long)sp
->f_bavail
;
2884 sfs
.f_files
= (long)sp
->f_files
;
2885 sfs
.f_ffree
= (long)sp
->f_ffree
;
2886 sfs
.f_fsid
= sp
->f_fsid
;
2887 sfs
.f_owner
= sp
->f_owner
;
2889 strncpy(&sfs
.f_fstypename
, &sp
->f_fstypename
, MFSNAMELEN
-1);
2890 strncpy(&sfs
.f_mntonname
, &sp
->f_mntonname
, MNAMELEN
-1);
2891 strncpy(&sfs
.f_mntfromname
, &sp
->f_mntfromname
, MNAMELEN
-1);
2893 error
= SYSCTL_OUT(req
, &sfs
, sizeof(sfs
));
2902 static int filt_fsattach(struct knote
*kn
);
2903 static void filt_fsdetach(struct knote
*kn
);
2904 static int filt_fsevent(struct knote
*kn
, long hint
);
2906 struct filterops fs_filtops
=
2907 { 0, filt_fsattach
, filt_fsdetach
, filt_fsevent
};
2910 filt_fsattach(struct knote
*kn
)
2913 kn
->kn_flags
|= EV_CLEAR
;
2914 KNOTE_ATTACH(&fs_klist
, kn
);
2919 filt_fsdetach(struct knote
*kn
)
2922 KNOTE_DETACH(&fs_klist
, kn
);
2926 filt_fsevent(struct knote
*kn
, long hint
)
2929 kn
->kn_fflags
|= hint
;
2930 return (kn
->kn_fflags
!= 0);
2934 sysctl_vfs_noremotehang SYSCTL_HANDLER_ARGS
2941 /* We need a pid. */
2942 if (req
->newptr
== USER_ADDR_NULL
)
2945 error
= SYSCTL_IN(req
, &pid
, sizeof(pid
));
2949 p
= pfind(pid
< 0 ? -pid
: pid
);
2954 * Fetching the value is ok, but we only fetch if the old
2957 if (req
->oldptr
!= USER_ADDR_NULL
) {
2958 out
= !((p
->p_flag
& P_NOREMOTEHANG
) == 0);
2959 error
= SYSCTL_OUT(req
, &out
, sizeof(out
));
2963 /* XXX req->p->p_ucred -> kauth_cred_get() ??? */
2964 /* cansignal offers us enough security. */
2965 if (p
!= req
->p
&& suser(req
->p
->p_ucred
, &req
->p
->p_acflag
) != 0)
2969 p
->p_flag
&= ~P_NOREMOTEHANG
;
2971 p
->p_flag
|= P_NOREMOTEHANG
;
2975 /* the vfs.generic. branch. */
2976 SYSCTL_NODE(_vfs
, VFS_GENERIC
, generic
, CTLFLAG_RW
, 0, "vfs generic hinge");
2977 /* retreive a list of mounted filesystem fsid_t */
2978 SYSCTL_PROC(_vfs_generic
, OID_AUTO
, vfsidlist
, CTLFLAG_RD
,
2979 0, 0, sysctl_vfs_vfslist
, "S,fsid", "List of mounted filesystem ids");
2980 /* perform operations on filesystem via fsid_t */
2981 SYSCTL_NODE(_vfs_generic
, OID_AUTO
, ctlbyfsid
, CTLFLAG_RW
,
2982 sysctl_vfs_ctlbyfsid
, "ctlbyfsid");
2983 SYSCTL_PROC(_vfs_generic
, OID_AUTO
, noremotehang
, CTLFLAG_RW
,
2984 0, 0, sysctl_vfs_noremotehang
, "I", "noremotehang");
2987 int num_reusedvnodes
=0;
2990 new_vnode(vnode_t
*vpp
)
2993 int retries
= 0; /* retry incase of tablefull */
3000 if ( !TAILQ_EMPTY(&vnode_free_list
)) {
3002 * Pick the first vp for possible reuse
3004 vp
= TAILQ_FIRST(&vnode_free_list
);
3006 if (vp
->v_lflag
& VL_DEAD
)
3012 * we're either empty, or the next guy on the
3013 * list is a valid vnode... if we're under the
3014 * limit, we'll create a new vnode
3016 if (numvnodes
< desiredvnodes
) {
3018 vnode_list_unlock();
3019 MALLOC_ZONE(vp
, struct vnode
*, sizeof *vp
, M_VNODE
, M_WAITOK
);
3020 bzero((char *)vp
, sizeof *vp
);
3021 VLISTNONE(vp
); /* avoid double queue removal */
3022 lck_mtx_init(&vp
->v_lock
, vnode_lck_grp
, vnode_lck_attr
);
3025 vp
->v_id
= ts
.tv_nsec
;
3026 vp
->v_flag
= VSTANDARD
;
3032 * we've reached the system imposed maximum number of vnodes
3033 * but there isn't a single one available
3034 * wait a bit and then retry... if we can't get a vnode
3035 * after 100 retries, than log a complaint
3037 if (++retries
<= 100) {
3038 vnode_list_unlock();
3043 vnode_list_unlock();
3045 log(LOG_EMERG
, "%d desired, %d numvnodes, "
3046 "%d free, %d inactive\n",
3047 desiredvnodes
, numvnodes
, freevnodes
, inactivevnodes
);
3054 VREMFREE("new_vnode", vp
);
3057 vnode_list_unlock();
3061 * We could wait for the vnode_lock after removing the vp from the freelist
3062 * and the vid is bumped only at the very end of reclaim. So it is possible
3063 * that we are looking at a vnode that is being terminated. If so skip it.
3065 if ((vpid
!= vp
->v_id
) || (vp
->v_usecount
!= 0) || (vp
->v_iocount
!= 0) ||
3066 VONLIST(vp
) || (vp
->v_lflag
& VL_TERMINATE
)) {
3068 * we lost the race between dropping the list lock
3069 * and picking up the vnode_lock... someone else
3070 * used this vnode and it is now in a new state
3071 * so we need to go back and try again
3076 if ( (vp
->v_lflag
& (VL_NEEDINACTIVE
| VL_MARKTERM
)) == VL_NEEDINACTIVE
) {
3078 * we did a vnode_rele_ext that asked for
3079 * us not to reenter the filesystem during
3080 * the release even though VL_NEEDINACTIVE was
3081 * set... we'll do it here by doing a
3082 * vnode_get/vnode_put
3084 * pick up an iocount so that we can call
3085 * vnode_put and drive the VNOP_INACTIVE...
3086 * vnode_put will either leave us off
3087 * the freelist if a new ref comes in,
3088 * or put us back on the end of the freelist
3089 * or recycle us if we were marked for termination...
3090 * so we'll just go grab a new candidate
3096 vnode_put_locked(vp
);
3100 OSAddAtomic(1, &num_reusedvnodes
);
3102 /* Checks for anyone racing us for recycle */
3103 if (vp
->v_type
!= VBAD
) {
3104 if (vp
->v_lflag
& VL_DEAD
)
3105 panic("new_vnode: the vnode is VL_DEAD but not VBAD");
3107 (void)vnode_reclaim_internal(vp
, 1, 1);
3110 panic("new_vnode: vp on list ");
3111 if (vp
->v_usecount
|| vp
->v_iocount
|| vp
->v_kusecount
||
3112 (vp
->v_lflag
& (VNAMED_UBC
| VNAMED_MOUNT
| VNAMED_FSHASH
)))
3113 panic("new_vnode: free vnode still referenced\n");
3114 if ((vp
->v_mntvnodes
.tqe_prev
!= 0) && (vp
->v_mntvnodes
.tqe_next
!= 0))
3115 panic("new_vnode: vnode seems to be on mount list ");
3116 if ( !LIST_EMPTY(&vp
->v_nclinks
) || !LIST_EMPTY(&vp
->v_ncchildren
))
3117 panic("new_vnode: vnode still hooked into the name cache");
3119 if (vp
->v_unsafefs
) {
3120 lck_mtx_destroy(&vp
->v_unsafefs
->fsnodelock
, vnode_lck_grp
);
3121 FREE_ZONE((void *)vp
->v_unsafefs
, sizeof(struct unsafe_fsnode
), M_UNSAFEFS
);
3122 vp
->v_unsafefs
= (struct unsafe_fsnode
*)NULL
;
3125 vp
->v_writecount
= 0;
3126 vp
->v_references
= 0;
3127 vp
->v_iterblkflags
= 0;
3128 vp
->v_flag
= VSTANDARD
;
3129 /* vbad vnodes can point to dead_mountp */
3131 vp
->v_defer_reclaimlist
= (vnode_t
)0;
3141 vnode_lock(vnode_t vp
)
3143 lck_mtx_lock(&vp
->v_lock
);
3147 vnode_unlock(vnode_t vp
)
3149 lck_mtx_unlock(&vp
->v_lock
);
3155 vnode_get(struct vnode
*vp
)
3159 if ( (vp
->v_iocount
== 0) && (vp
->v_lflag
& (VL_TERMINATE
| VL_DEAD
)) ) {
3173 vnode_getwithvid(vnode_t vp
, int vid
)
3175 return(vget_internal(vp
, vid
, ( VNODE_NODEAD
| VNODE_WITHID
)));
3179 vnode_getwithref(vnode_t vp
)
3181 return(vget_internal(vp
, 0, 0));
3186 vnode_put(vnode_t vp
)
3191 retval
= vnode_put_locked(vp
);
3198 vnode_put_locked(vnode_t vp
)
3200 struct vfs_context context
;
3203 if (vp
->v_iocount
< 1)
3204 panic("vnode_put(%x): iocount < 1", vp
);
3206 if ((vp
->v_usecount
> 0) || (vp
->v_iocount
> 1)) {
3207 vnode_dropiocount(vp
, 1);
3210 if ((vp
->v_lflag
& (VL_MARKTERM
| VL_TERMINATE
| VL_DEAD
| VL_NEEDINACTIVE
)) == VL_NEEDINACTIVE
) {
3212 vp
->v_lflag
&= ~VL_NEEDINACTIVE
;
3215 context
.vc_proc
= current_proc();
3216 context
.vc_ucred
= kauth_cred_get();
3217 VNOP_INACTIVE(vp
, &context
);
3221 * because we had to drop the vnode lock before calling
3222 * VNOP_INACTIVE, the state of this vnode may have changed...
3223 * we may pick up both VL_MARTERM and either
3224 * an iocount or a usecount while in the VNOP_INACTIVE call
3225 * we don't want to call vnode_reclaim_internal on a vnode
3226 * that has active references on it... so loop back around
3227 * and reevaluate the state
3231 vp
->v_lflag
&= ~VL_NEEDINACTIVE
;
3233 if ((vp
->v_lflag
& (VL_MARKTERM
| VL_TERMINATE
| VL_DEAD
)) == VL_MARKTERM
)
3234 vnode_reclaim_internal(vp
, 1, 0);
3236 vnode_dropiocount(vp
, 1);
3242 /* is vnode_t in use by others? */
3244 vnode_isinuse(vnode_t vp
, int refcnt
)
3246 return(vnode_isinuse_locked(vp
, refcnt
, 0));
3251 vnode_isinuse_locked(vnode_t vp
, int refcnt
, int locked
)
3257 if ((vp
->v_type
!= VREG
) && (vp
->v_usecount
> refcnt
)) {
3261 if (vp
->v_type
== VREG
) {
3262 retval
= ubc_isinuse_locked(vp
, refcnt
, 1);
3272 /* resume vnode_t */
3274 vnode_resume(vnode_t vp
)
3279 if (vp
->v_owner
== current_thread()) {
3280 vp
->v_lflag
&= ~VL_SUSPENDED
;
3283 wakeup(&vp
->v_iocount
);
3291 vnode_drain(vnode_t vp
)
3294 if (vp
->v_lflag
& VL_DRAIN
) {
3295 panic("vnode_drain: recursuve drain");
3298 vp
->v_lflag
|= VL_DRAIN
;
3299 vp
->v_owner
= current_thread();
3301 while (vp
->v_iocount
> 1)
3302 msleep(&vp
->v_iocount
, &vp
->v_lock
, PVFS
, "vnode_drain", 0);
3308 * if the number of recent references via vnode_getwithvid or vnode_getwithref
3309 * exceeds this threshhold, than 'UN-AGE' the vnode by removing it from
3310 * the LRU list if it's currently on it... once the iocount and usecount both drop
3311 * to 0, it will get put back on the end of the list, effectively making it younger
3312 * this allows us to keep actively referenced vnodes in the list without having
3313 * to constantly remove and add to the list each time a vnode w/o a usecount is
3314 * referenced which costs us taking and dropping a global lock twice.
3316 #define UNAGE_THRESHHOLD 10
3319 vnode_getiocount(vnode_t vp
, int locked
, int vid
, int vflags
)
3321 int nodead
= vflags
& VNODE_NODEAD
;
3322 int nosusp
= vflags
& VNODE_NOSUSPEND
;
3329 * if it is a dead vnode with deadfs
3331 if (nodead
&& (vp
->v_lflag
& VL_DEAD
) && ((vp
->v_type
== VBAD
) || (vp
->v_data
== 0))) {
3337 * will return VL_DEAD ones
3339 if ((vp
->v_lflag
& (VL_SUSPENDED
| VL_DRAIN
| VL_TERMINATE
)) == 0 ) {
3343 * if suspended vnodes are to be failed
3345 if (nosusp
&& (vp
->v_lflag
& VL_SUSPENDED
)) {
3351 * if you are the owner of drain/suspend/termination , can acquire iocount
3352 * check for VL_TERMINATE; it does not set owner
3354 if ((vp
->v_lflag
& (VL_DRAIN
| VL_SUSPENDED
| VL_TERMINATE
)) &&
3355 (vp
->v_owner
== current_thread())) {
3358 if (vp
->v_lflag
& VL_TERMINATE
) {
3359 vp
->v_lflag
|= VL_TERMWANT
;
3361 msleep(&vp
->v_lflag
, &vp
->v_lock
, PVFS
, "vnode getiocount", 0);
3363 msleep(&vp
->v_iocount
, &vp
->v_lock
, PVFS
, "vnode_getiocount", 0);
3365 if (vid
!= vp
->v_id
) {
3370 if (++vp
->v_references
>= UNAGE_THRESHHOLD
) {
3371 vp
->v_references
= 0;
3372 vnode_list_remove(vp
);
3384 vnode_dropiocount (vnode_t vp
, int locked
)
3388 if (vp
->v_iocount
< 1)
3389 panic("vnode_dropiocount(%x): v_iocount < 1", vp
);
3395 if ((vp
->v_lflag
& (VL_DRAIN
| VL_SUSPENDED
)) && (vp
->v_iocount
<= 1))
3396 wakeup(&vp
->v_iocount
);
3404 vnode_reclaim(struct vnode
* vp
)
3406 vnode_reclaim_internal(vp
, 0, 0);
3411 vnode_reclaim_internal(struct vnode
* vp
, int locked
, int reuse
)
3418 if (vp
->v_lflag
& VL_TERMINATE
) {
3419 panic("vnode reclaim in progress");
3421 vp
->v_lflag
|= VL_TERMINATE
;
3423 if (vnode_drain(vp
)) {
3424 panic("vnode drain failed");
3428 isfifo
= (vp
->v_type
== VFIFO
);
3430 if (vp
->v_type
!= VBAD
)
3431 vgone(vp
); /* clean and reclaim the vnode */
3434 * give the vnode a new identity so
3435 * that vnode_getwithvid will fail
3436 * on any stale cache accesses
3440 struct fifoinfo
* fip
;
3442 fip
= vp
->v_fifoinfo
;
3443 vp
->v_fifoinfo
= NULL
;
3450 panic("vnode_reclaim_internal: cleaned vnode isn't");
3451 if (vp
->v_numoutput
)
3452 panic("vnode_reclaim_internal: Clean vnode has pending I/O's");
3453 if (UBCINFOEXISTS(vp
))
3454 panic("vnode_reclaim_internal: ubcinfo not cleaned");
3456 panic("vnode_reclaim_internal: vparent not removed");
3458 panic("vnode_reclaim_internal: vname not removed");
3462 vp
->v_lflag
&= ~VL_TERMINATE
;
3463 vp
->v_lflag
&= ~VL_DRAIN
;
3466 if (vp
->v_lflag
& VL_TERMWANT
) {
3467 vp
->v_lflag
&= ~VL_TERMWANT
;
3468 wakeup(&vp
->v_lflag
);
3470 if (!reuse
&& vp
->v_usecount
== 0)
3477 * The following api creates a vnode and associates all the parameter specified in vnode_fsparam
3478 * structure and returns a vnode handle with a reference. device aliasing is handled here so checkalias
3479 * is obsoleted by this.
3480 * vnode_create(int flavor, size_t size, void * param, vnode_t *vp)
3483 vnode_create(int flavor
, size_t size
, void *data
, vnode_t
*vpp
)
3490 struct componentname
*cnp
;
3491 struct vnode_fsparam
*param
= (struct vnode_fsparam
*)data
;
3493 if (flavor
== VNCREATE_FLAVOR
&& (size
== VCREATESIZE
) && param
) {
3494 if ( (error
= new_vnode(&vp
)) ) {
3497 dvp
= param
->vnfs_dvp
;
3498 cnp
= param
->vnfs_cnp
;
3500 vp
->v_op
= param
->vnfs_vops
;
3501 vp
->v_type
= param
->vnfs_vtype
;
3502 vp
->v_data
= param
->vnfs_fsnode
;
3505 if (param
->vnfs_markroot
)
3506 vp
->v_flag
|= VROOT
;
3507 if (param
->vnfs_marksystem
)
3508 vp
->v_flag
|= VSYSTEM
;
3509 else if (vp
->v_type
== VREG
) {
3511 * only non SYSTEM vp
3513 error
= ubc_info_init_withsize(vp
, param
->vnfs_filesize
);
3519 vp
->v_op
= dead_vnodeop_p
;
3523 vp
->v_lflag
|= VL_DEAD
;
3532 if (vp
->v_type
== VCHR
|| vp
->v_type
== VBLK
) {
3534 if ( (nvp
= checkalias(vp
, param
->vnfs_rdev
)) ) {
3536 * if checkalias returns a vnode, it will be locked
3538 * first get rid of the unneeded vnode we acquired
3541 vp
->v_op
= spec_vnodeop_p
;
3543 vp
->v_lflag
= VL_DEAD
;
3549 * switch to aliased vnode and finish
3554 vclean(vp
, 0, current_proc());
3555 vp
->v_op
= param
->vnfs_vops
;
3556 vp
->v_type
= param
->vnfs_vtype
;
3557 vp
->v_data
= param
->vnfs_fsnode
;
3560 insmntque(vp
, param
->vnfs_mp
);
3566 if (vp
->v_type
== VFIFO
) {
3567 struct fifoinfo
*fip
;
3569 MALLOC(fip
, struct fifoinfo
*,
3570 sizeof(*fip
), M_TEMP
, M_WAITOK
);
3571 bzero(fip
, sizeof(struct fifoinfo
));
3572 vp
->v_fifoinfo
= fip
;
3574 /* The file systems usually pass the address of the location where
3575 * where there store the vnode pointer. When we add the vnode in mount
3576 * point and name cache they are discoverable. So the file system node
3577 * will have the connection to vnode setup by then
3581 if (param
->vnfs_mp
) {
3582 if (param
->vnfs_mp
->mnt_kern_flag
& MNTK_LOCK_LOCAL
)
3583 vp
->v_flag
|= VLOCKLOCAL
;
3586 * enter in mount vnode list
3588 insmntque(vp
, param
->vnfs_mp
);
3590 #ifdef INTERIM_FSNODE_LOCK
3591 if (param
->vnfs_mp
->mnt_vtable
->vfc_threadsafe
== 0) {
3592 MALLOC_ZONE(vp
->v_unsafefs
, struct unsafe_fsnode
*,
3593 sizeof(struct unsafe_fsnode
), M_UNSAFEFS
, M_WAITOK
);
3594 vp
->v_unsafefs
->fsnode_count
= 0;
3595 vp
->v_unsafefs
->fsnodeowner
= (void *)NULL
;
3596 lck_mtx_init(&vp
->v_unsafefs
->fsnodelock
, vnode_lck_grp
, vnode_lck_attr
);
3598 #endif /* INTERIM_FSNODE_LOCK */
3600 if (dvp
&& vnode_ref(dvp
) == 0) {
3604 if (dvp
&& ((param
->vnfs_flags
& (VNFS_NOCACHE
| VNFS_CANTCACHE
)) == 0)) {
3606 * enter into name cache
3607 * we've got the info to enter it into the name cache now
3609 cache_enter(dvp
, vp
, cnp
);
3611 vp
->v_name
= vfs_addname(cnp
->cn_nameptr
, cnp
->cn_namelen
, cnp
->cn_hash
, 0);
3613 if ((param
->vnfs_flags
& VNFS_CANTCACHE
) == 0) {
3615 * this vnode is being created as cacheable in the name cache
3616 * this allows us to re-enter it in the cache
3618 vp
->v_flag
|= VNCACHEABLE
;
3620 if ((vp
->v_flag
& VSYSTEM
) && (vp
->v_type
!= VREG
))
3621 panic("incorrect vnode setup");
3630 vnode_addfsref(vnode_t vp
)
3633 if (vp
->v_lflag
& VNAMED_FSHASH
)
3634 panic("add_fsref: vp already has named reference");
3635 if ((vp
->v_freelist
.tqe_prev
!= (struct vnode
**)0xdeadb))
3636 panic("addfsref: vp on the free list\n");
3637 vp
->v_lflag
|= VNAMED_FSHASH
;
3643 vnode_removefsref(vnode_t vp
)
3646 if ((vp
->v_lflag
& VNAMED_FSHASH
) == 0)
3647 panic("remove_fsref: no named reference");
3648 vp
->v_lflag
&= ~VNAMED_FSHASH
;
3656 vfs_iterate(__unused
int flags
, int (*callout
)(mount_t
, void *), void *arg
)
3661 int count
, actualcount
, i
;
3664 count
= mount_getvfscnt();
3667 fsid_list
= (fsid_t
*)kalloc(count
* sizeof(fsid_t
));
3668 allocmem
= (void *)fsid_list
;
3670 actualcount
= mount_fillfsids(fsid_list
, count
);
3672 for (i
=0; i
< actualcount
; i
++) {
3674 /* obtain the mount point with iteration reference */
3675 mp
= mount_list_lookupby_fsid(&fsid_list
[i
], 0, 1);
3677 if(mp
== (struct mount
*)0)
3680 if (mp
->mnt_lflag
& (MNT_LDEAD
| MNT_LUNMOUNT
)) {
3688 /* iterate over all the vnodes */
3689 ret
= callout(mp
, arg
);
3695 case VFS_RETURNED_DONE
:
3696 if (ret
== VFS_RETURNED_DONE
) {
3702 case VFS_CLAIMED_DONE
:
3713 kfree(allocmem
, (count
* sizeof(fsid_t
)));
3718 * Update the vfsstatfs structure in the mountpoint.
3721 vfs_update_vfsstat(mount_t mp
, vfs_context_t ctx
)
3727 * Request the attributes we want to propagate into
3728 * the per-mount vfsstat structure.
3731 VFSATTR_WANTED(&va
, f_iosize
);
3732 VFSATTR_WANTED(&va
, f_blocks
);
3733 VFSATTR_WANTED(&va
, f_bfree
);
3734 VFSATTR_WANTED(&va
, f_bavail
);
3735 VFSATTR_WANTED(&va
, f_bused
);
3736 VFSATTR_WANTED(&va
, f_files
);
3737 VFSATTR_WANTED(&va
, f_ffree
);
3738 VFSATTR_WANTED(&va
, f_bsize
);
3739 VFSATTR_WANTED(&va
, f_fssubtype
);
3740 if ((error
= vfs_getattr(mp
, &va
, ctx
)) != 0) {
3741 KAUTH_DEBUG("STAT - filesystem returned error %d", error
);
3746 * Unpack into the per-mount structure.
3748 * We only overwrite these fields, which are likely to change:
3756 * And these which are not, but which the FS has no other way
3757 * of providing to us:
3763 if (VFSATTR_IS_SUPPORTED(&va
, f_bsize
)) {
3764 mp
->mnt_vfsstat
.f_bsize
= va
.f_bsize
;
3766 mp
->mnt_vfsstat
.f_bsize
= mp
->mnt_devblocksize
; /* default from the device block size */
3768 if (VFSATTR_IS_SUPPORTED(&va
, f_iosize
)) {
3769 mp
->mnt_vfsstat
.f_iosize
= va
.f_iosize
;
3771 mp
->mnt_vfsstat
.f_iosize
= 1024 * 1024; /* 1MB sensible I/O size */
3773 if (VFSATTR_IS_SUPPORTED(&va
, f_blocks
))
3774 mp
->mnt_vfsstat
.f_blocks
= va
.f_blocks
;
3775 if (VFSATTR_IS_SUPPORTED(&va
, f_bfree
))
3776 mp
->mnt_vfsstat
.f_bfree
= va
.f_bfree
;
3777 if (VFSATTR_IS_SUPPORTED(&va
, f_bavail
))
3778 mp
->mnt_vfsstat
.f_bavail
= va
.f_bavail
;
3779 if (VFSATTR_IS_SUPPORTED(&va
, f_bused
))
3780 mp
->mnt_vfsstat
.f_bused
= va
.f_bused
;
3781 if (VFSATTR_IS_SUPPORTED(&va
, f_files
))
3782 mp
->mnt_vfsstat
.f_files
= va
.f_files
;
3783 if (VFSATTR_IS_SUPPORTED(&va
, f_ffree
))
3784 mp
->mnt_vfsstat
.f_ffree
= va
.f_ffree
;
3786 /* this is unlikely to change, but has to be queried for */
3787 if (VFSATTR_IS_SUPPORTED(&va
, f_fssubtype
))
3788 mp
->mnt_vfsstat
.f_fssubtype
= va
.f_fssubtype
;
3794 mount_list_add(mount_t mp
)
3797 TAILQ_INSERT_TAIL(&mountlist
, mp
, mnt_list
);
3799 mount_list_unlock();
3803 mount_list_remove(mount_t mp
)
3806 TAILQ_REMOVE(&mountlist
, mp
, mnt_list
);
3808 mp
->mnt_list
.tqe_next
= 0;
3809 mp
->mnt_list
.tqe_prev
= 0;
3810 mount_list_unlock();
3814 mount_lookupby_volfsid(int volfs_id
, int withref
)
3816 mount_t cur_mount
= (mount_t
)0;
3820 TAILQ_FOREACH(mp
, &mountlist
, mnt_list
) {
3821 if (validfsnode(mp
) && mp
->mnt_vfsstat
.f_fsid
.val
[0] == volfs_id
) {
3824 if (mount_iterref(cur_mount
, 1)) {
3825 cur_mount
= (mount_t
)0;
3826 mount_list_unlock();
3833 mount_list_unlock();
3834 if (withref
&& (cur_mount
!= (mount_t
)0)) {
3836 if (vfs_busy(mp
, LK_NOWAIT
) != 0) {
3837 cur_mount
= (mount_t
)0;
3847 mount_list_lookupby_fsid(fsid
, locked
, withref
)
3852 mount_t retmp
= (mount_t
)0;
3857 TAILQ_FOREACH(mp
, &mountlist
, mnt_list
)
3858 if (mp
->mnt_vfsstat
.f_fsid
.val
[0] == fsid
->val
[0] &&
3859 mp
->mnt_vfsstat
.f_fsid
.val
[1] == fsid
->val
[1]) {
3862 if (mount_iterref(retmp
, 1))
3869 mount_list_unlock();
3874 vnode_lookup(const char *path
, int flags
, vnode_t
*vpp
, vfs_context_t context
)
3876 struct nameidata nd
;
3878 struct vfs_context context2
;
3879 vfs_context_t ctx
= context
;
3882 if (context
== NULL
) { /* XXX technically an error */
3883 context2
.vc_proc
= current_proc();
3884 context2
.vc_ucred
= kauth_cred_get();
3888 if (flags
& VNODE_LOOKUP_NOFOLLOW
)
3893 if (flags
& VNODE_LOOKUP_NOCROSSMOUNT
)
3894 ndflags
|= NOCROSSMOUNT
;
3895 if (flags
& VNODE_LOOKUP_DOWHITEOUT
)
3896 ndflags
|= DOWHITEOUT
;
3898 /* XXX AUDITVNPATH1 needed ? */
3899 NDINIT(&nd
, LOOKUP
, ndflags
, UIO_SYSSPACE
, CAST_USER_ADDR_T(path
), ctx
);
3901 if ((error
= namei(&nd
)))
3910 vnode_open(const char *path
, int fmode
, int cmode
, int flags
, vnode_t
*vpp
, vfs_context_t context
)
3912 struct nameidata nd
;
3914 struct vfs_context context2
;
3915 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 (fmode
& O_NOFOLLOW
)
3926 lflags
|= VNODE_LOOKUP_NOFOLLOW
;
3928 if (lflags
& VNODE_LOOKUP_NOFOLLOW
)
3933 if (lflags
& VNODE_LOOKUP_NOCROSSMOUNT
)
3934 ndflags
|= NOCROSSMOUNT
;
3935 if (lflags
& VNODE_LOOKUP_DOWHITEOUT
)
3936 ndflags
|= DOWHITEOUT
;
3938 /* XXX AUDITVNPATH1 needed ? */
3939 NDINIT(&nd
, LOOKUP
, ndflags
, UIO_SYSSPACE
, CAST_USER_ADDR_T(path
), ctx
);
3941 if ((error
= vn_open(&nd
, fmode
, cmode
)))
3950 vnode_close(vnode_t vp
, int flags
, vfs_context_t context
)
3957 p
= context
->vc_proc
;
3958 cred
= context
->vc_ucred
;
3961 cred
= kauth_cred_get();
3964 error
= vn_close(vp
, flags
, cred
, p
);
3970 vnode_size(vnode_t vp
, off_t
*sizep
, vfs_context_t ctx
)
3972 struct vnode_attr va
;
3976 VATTR_WANTED(&va
, va_data_size
);
3977 error
= vnode_getattr(vp
, &va
, ctx
);
3979 *sizep
= va
.va_data_size
;
3984 vnode_setsize(vnode_t vp
, off_t size
, int ioflag
, vfs_context_t ctx
)
3986 struct vnode_attr va
;
3989 VATTR_SET(&va
, va_data_size
, size
);
3990 va
.va_vaflags
= ioflag
& 0xffff;
3991 return(vnode_setattr(vp
, &va
, ctx
));
3995 vn_create(vnode_t dvp
, vnode_t
*vpp
, struct componentname
*cnp
, struct vnode_attr
*vap
, int flags
, vfs_context_t ctx
)
3997 kauth_acl_t oacl
, nacl
;
4000 vnode_t vp
= (vnode_t
)0;
4006 KAUTH_DEBUG("%p CREATE - '%s'", dvp
, cnp
->cn_nameptr
);
4009 * Handle ACL inheritance.
4011 if (!(flags
& VN_CREATE_NOINHERIT
) && vfs_extendedsecurity(dvp
->v_mount
)) {
4012 /* save the original filesec */
4013 if (VATTR_IS_ACTIVE(vap
, va_acl
)) {
4019 if ((error
= kauth_acl_inherit(dvp
,
4022 vap
->va_type
== VDIR
,
4024 KAUTH_DEBUG("%p CREATE - error %d processing inheritance", dvp
, error
);
4029 * If the generated ACL is NULL, then we can save ourselves some effort
4030 * by clearing the active bit.
4033 VATTR_CLEAR_ACTIVE(vap
, va_acl
);
4035 VATTR_SET(vap
, va_acl
, nacl
);
4040 * Check and default new attributes.
4041 * This will set va_uid, va_gid, va_mode and va_create_time at least, if the caller
4042 * hasn't supplied them.
4044 if ((error
= vnode_authattr_new(dvp
, vap
, flags
& VN_CREATE_NOAUTH
, ctx
)) != 0) {
4045 KAUTH_DEBUG("%p CREATE - error %d handing/defaulting attributes", dvp
, error
);
4051 * Create the requested node.
4053 switch(vap
->va_type
) {
4055 error
= VNOP_CREATE(dvp
, vpp
, cnp
, vap
, ctx
);
4058 error
= VNOP_MKDIR(dvp
, vpp
, cnp
, vap
, ctx
);
4064 error
= VNOP_MKNOD(dvp
, vpp
, cnp
, vap
, ctx
);
4067 panic("vnode_create: unknown vtype %d", vap
->va_type
);
4070 KAUTH_DEBUG("%p CREATE - error %d returned by filesystem", dvp
, error
);
4076 * If some of the requested attributes weren't handled by the VNOP,
4077 * use our fallback code.
4079 if (!VATTR_ALL_SUPPORTED(vap
) && *vpp
) {
4080 KAUTH_DEBUG(" CREATE - doing fallback with ACL %p", vap
->va_acl
);
4081 error
= vnode_setattr_fallback(*vpp
, vap
, ctx
);
4083 if ((error
!= 0 ) && (vp
!= (vnode_t
)0)) {
4090 * If the caller supplied a filesec in vap, it has been replaced
4091 * now by the post-inheritance copy. We need to put the original back
4092 * and free the inherited product.
4095 VATTR_SET(vap
, va_acl
, oacl
);
4097 VATTR_CLEAR_ACTIVE(vap
, va_acl
);
4100 kauth_acl_free(nacl
);
4105 static kauth_scope_t vnode_scope
;
4106 static int vnode_authorize_callback(kauth_cred_t credential
, __unused
void *idata
, kauth_action_t action
,
4107 uintptr_t arg0
, uintptr_t arg1
, uintptr_t arg2
, uintptr_t arg3
);
4109 typedef struct _vnode_authorize_context
{
4111 struct vnode_attr
*vap
;
4113 struct vnode_attr
*dvap
;
4117 #define _VAC_IS_OWNER (1<<0)
4118 #define _VAC_IN_GROUP (1<<1)
4119 #define _VAC_IS_DIR_OWNER (1<<2)
4120 #define _VAC_IN_DIR_GROUP (1<<3)
4124 vnode_authorize_init(void)
4126 vnode_scope
= kauth_register_scope(KAUTH_SCOPE_VNODE
, vnode_authorize_callback
, NULL
);
4130 * Authorize an operation on a vnode.
4132 * This is KPI, but here because it needs vnode_scope.
4135 vnode_authorize(vnode_t vp
, vnode_t dvp
, kauth_action_t action
, vfs_context_t context
)
4140 * We can't authorize against a dead vnode; allow all operations through so that
4141 * the correct error can be returned.
4143 if (vp
->v_type
== VBAD
)
4147 result
= kauth_authorize_action(vnode_scope
, vfs_context_ucred(context
), action
,
4148 (uintptr_t)context
, (uintptr_t)vp
, (uintptr_t)dvp
, (uintptr_t)&error
);
4149 if (result
== EPERM
) /* traditional behaviour */
4151 /* did the lower layers give a better error return? */
4152 if ((result
!= 0) && (error
!= 0))
4158 * Test for vnode immutability.
4160 * The 'append' flag is set when the authorization request is constrained
4161 * to operations which only request the right to append to a file.
4163 * The 'ignore' flag is set when an operation modifying the immutability flags
4164 * is being authorized. We check the system securelevel to determine which
4165 * immutability flags we can ignore.
4168 vnode_immutable(struct vnode_attr
*vap
, int append
, int ignore
)
4172 /* start with all bits precluding the operation */
4173 mask
= IMMUTABLE
| APPEND
;
4175 /* if appending only, remove the append-only bits */
4179 /* ignore only set when authorizing flags changes */
4181 if (securelevel
<= 0) {
4182 /* in insecure state, flags do not inhibit changes */
4185 /* in secure state, user flags don't inhibit */
4186 mask
&= ~(UF_IMMUTABLE
| UF_APPEND
);
4189 KAUTH_DEBUG("IMMUTABLE - file flags 0x%x mask 0x%x append = %d ignore = %d", vap
->va_flags
, mask
, append
, ignore
);
4190 if ((vap
->va_flags
& mask
) != 0)
4196 vauth_node_owner(struct vnode_attr
*vap
, kauth_cred_t cred
)
4200 /* default assumption is not-owner */
4204 * If the filesystem has given us a UID, we treat this as authoritative.
4206 if (vap
&& VATTR_IS_SUPPORTED(vap
, va_uid
)) {
4207 result
= (vap
->va_uid
== kauth_cred_getuid(cred
)) ? 1 : 0;
4209 /* we could test the owner UUID here if we had a policy for it */
4215 vauth_node_group(struct vnode_attr
*vap
, kauth_cred_t cred
, int *ismember
)
4223 /* the caller is expected to have asked the filesystem for a group at some point */
4224 if (vap
&& VATTR_IS_SUPPORTED(vap
, va_gid
)) {
4225 error
= kauth_cred_ismember_gid(cred
, vap
->va_gid
, &result
);
4227 /* we could test the group UUID here if we had a policy for it */
4235 vauth_file_owner(vauth_ctx vcp
)
4239 if (vcp
->flags_valid
& _VAC_IS_OWNER
) {
4240 result
= (vcp
->flags
& _VAC_IS_OWNER
) ? 1 : 0;
4242 result
= vauth_node_owner(vcp
->vap
, vcp
->ctx
->vc_ucred
);
4244 /* cache our result */
4245 vcp
->flags_valid
|= _VAC_IS_OWNER
;
4247 vcp
->flags
|= _VAC_IS_OWNER
;
4249 vcp
->flags
&= ~_VAC_IS_OWNER
;
4256 vauth_file_ingroup(vauth_ctx vcp
, int *ismember
)
4260 if (vcp
->flags_valid
& _VAC_IN_GROUP
) {
4261 *ismember
= (vcp
->flags
& _VAC_IN_GROUP
) ? 1 : 0;
4264 error
= vauth_node_group(vcp
->vap
, vcp
->ctx
->vc_ucred
, ismember
);
4267 /* cache our result */
4268 vcp
->flags_valid
|= _VAC_IN_GROUP
;
4270 vcp
->flags
|= _VAC_IN_GROUP
;
4272 vcp
->flags
&= ~_VAC_IN_GROUP
;
4281 vauth_dir_owner(vauth_ctx vcp
)
4285 if (vcp
->flags_valid
& _VAC_IS_DIR_OWNER
) {
4286 result
= (vcp
->flags
& _VAC_IS_DIR_OWNER
) ? 1 : 0;
4288 result
= vauth_node_owner(vcp
->dvap
, vcp
->ctx
->vc_ucred
);
4290 /* cache our result */
4291 vcp
->flags_valid
|= _VAC_IS_DIR_OWNER
;
4293 vcp
->flags
|= _VAC_IS_DIR_OWNER
;
4295 vcp
->flags
&= ~_VAC_IS_DIR_OWNER
;
4302 vauth_dir_ingroup(vauth_ctx vcp
, int *ismember
)
4306 if (vcp
->flags_valid
& _VAC_IN_DIR_GROUP
) {
4307 *ismember
= (vcp
->flags
& _VAC_IN_DIR_GROUP
) ? 1 : 0;
4310 error
= vauth_node_group(vcp
->dvap
, vcp
->ctx
->vc_ucred
, ismember
);
4313 /* cache our result */
4314 vcp
->flags_valid
|= _VAC_IN_DIR_GROUP
;
4316 vcp
->flags
|= _VAC_IN_DIR_GROUP
;
4318 vcp
->flags
&= ~_VAC_IN_DIR_GROUP
;
4326 * Test the posix permissions in (vap) to determine whether (credential)
4327 * may perform (action)
4330 vnode_authorize_posix(vauth_ctx vcp
, int action
, int on_dir
)
4332 struct vnode_attr
*vap
;
4333 int needed
, error
, owner_ok
, group_ok
, world_ok
, ismember
;
4334 #ifdef KAUTH_DEBUG_ENABLE
4336 # define _SETWHERE(c) where = c;
4338 # define _SETWHERE(c)
4341 /* checking file or directory? */
4351 * We want to do as little work here as possible. So first we check
4352 * which sets of permissions grant us the access we need, and avoid checking
4353 * whether specific permissions grant access when more generic ones would.
4356 /* owner permissions */
4360 if (action
& VWRITE
)
4364 owner_ok
= (needed
& vap
->va_mode
) == needed
;
4366 /* group permissions */
4370 if (action
& VWRITE
)
4374 group_ok
= (needed
& vap
->va_mode
) == needed
;
4376 /* world permissions */
4380 if (action
& VWRITE
)
4384 world_ok
= (needed
& vap
->va_mode
) == needed
;
4386 /* If granted/denied by all three, we're done */
4387 if (owner_ok
&& group_ok
&& world_ok
) {
4391 if (!owner_ok
&& !group_ok
&& !world_ok
) {
4397 /* Check ownership (relatively cheap) */
4398 if ((on_dir
&& vauth_dir_owner(vcp
)) ||
4399 (!on_dir
&& vauth_file_owner(vcp
))) {
4406 /* Not owner; if group and world both grant it we're done */
4407 if (group_ok
&& world_ok
) {
4408 _SETWHERE("group/world");
4411 if (!group_ok
&& !world_ok
) {
4412 _SETWHERE("group/world");
4417 /* Check group membership (most expensive) */
4420 error
= vauth_dir_ingroup(vcp
, &ismember
);
4422 error
= vauth_file_ingroup(vcp
, &ismember
);
4433 /* Not owner, not in group, use world result */
4441 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",
4442 vcp
->vp
, (error
== 0) ? "ALLOWED" : "DENIED", where
,
4443 (action
& VREAD
) ? "r" : "-",
4444 (action
& VWRITE
) ? "w" : "-",
4445 (action
& VEXEC
) ? "x" : "-",
4447 (vap
->va_mode
& S_IRUSR
) ? "r" : "-",
4448 (vap
->va_mode
& S_IWUSR
) ? "w" : "-",
4449 (vap
->va_mode
& S_IXUSR
) ? "x" : "-",
4450 (vap
->va_mode
& S_IRGRP
) ? "r" : "-",
4451 (vap
->va_mode
& S_IWGRP
) ? "w" : "-",
4452 (vap
->va_mode
& S_IXGRP
) ? "x" : "-",
4453 (vap
->va_mode
& S_IROTH
) ? "r" : "-",
4454 (vap
->va_mode
& S_IWOTH
) ? "w" : "-",
4455 (vap
->va_mode
& S_IXOTH
) ? "x" : "-",
4456 kauth_cred_getuid(vcp
->ctx
->vc_ucred
),
4457 on_dir
? vcp
->dvap
->va_uid
: vcp
->vap
->va_uid
,
4458 on_dir
? vcp
->dvap
->va_gid
: vcp
->vap
->va_gid
);
4463 * Authorize the deletion of the node vp from the directory dvp.
4466 * - Neither the node nor the directory are immutable.
4467 * - The user is not the superuser.
4469 * Deletion is not permitted if the directory is sticky and the caller is not owner of the
4470 * node or directory.
4472 * If either the node grants DELETE, or the directory grants DELETE_CHILD, the node may be
4473 * deleted. If neither denies the permission, and the caller has Posix write access to the
4474 * directory, then the node may be deleted.
4477 vnode_authorize_delete(vauth_ctx vcp
)
4479 struct vnode_attr
*vap
= vcp
->vap
;
4480 struct vnode_attr
*dvap
= vcp
->dvap
;
4481 kauth_cred_t cred
= vcp
->ctx
->vc_ucred
;
4482 struct kauth_acl_eval eval
;
4483 int error
, delete_denied
, delete_child_denied
, ismember
;
4485 /* check the ACL on the directory */
4486 delete_child_denied
= 0;
4487 if (VATTR_IS_NOT(dvap
, va_acl
, NULL
)) {
4488 eval
.ae_requested
= KAUTH_VNODE_DELETE_CHILD
;
4489 eval
.ae_acl
= &dvap
->va_acl
->acl_ace
[0];
4490 eval
.ae_count
= dvap
->va_acl
->acl_entrycount
;
4491 eval
.ae_options
= 0;
4492 if (vauth_dir_owner(vcp
))
4493 eval
.ae_options
|= KAUTH_AEVAL_IS_OWNER
;
4494 if ((error
= vauth_dir_ingroup(vcp
, &ismember
)) != 0)
4497 eval
.ae_options
|= KAUTH_AEVAL_IN_GROUP
;
4498 eval
.ae_exp_gall
= KAUTH_VNODE_GENERIC_ALL_BITS
;
4499 eval
.ae_exp_gread
= KAUTH_VNODE_GENERIC_READ_BITS
;
4500 eval
.ae_exp_gwrite
= KAUTH_VNODE_GENERIC_WRITE_BITS
;
4501 eval
.ae_exp_gexec
= KAUTH_VNODE_GENERIC_EXECUTE_BITS
;
4503 error
= kauth_acl_evaluate(cred
, &eval
);
4506 KAUTH_DEBUG("%p ERROR during ACL processing - %d", vcp
->vp
, error
);
4509 if (eval
.ae_result
== KAUTH_RESULT_DENY
)
4510 delete_child_denied
= 1;
4511 if (eval
.ae_result
== KAUTH_RESULT_ALLOW
) {
4512 KAUTH_DEBUG("%p ALLOWED - granted by directory ACL", vcp
->vp
);
4517 /* check the ACL on the node */
4519 if (VATTR_IS_NOT(vap
, va_acl
, NULL
)) {
4520 eval
.ae_requested
= KAUTH_VNODE_DELETE
;
4521 eval
.ae_acl
= &vap
->va_acl
->acl_ace
[0];
4522 eval
.ae_count
= vap
->va_acl
->acl_entrycount
;
4523 eval
.ae_options
= 0;
4524 if (vauth_file_owner(vcp
))
4525 eval
.ae_options
|= KAUTH_AEVAL_IS_OWNER
;
4526 if ((error
= vauth_file_ingroup(vcp
, &ismember
)) != 0)
4529 eval
.ae_options
|= KAUTH_AEVAL_IN_GROUP
;
4530 eval
.ae_exp_gall
= KAUTH_VNODE_GENERIC_ALL_BITS
;
4531 eval
.ae_exp_gread
= KAUTH_VNODE_GENERIC_READ_BITS
;
4532 eval
.ae_exp_gwrite
= KAUTH_VNODE_GENERIC_WRITE_BITS
;
4533 eval
.ae_exp_gexec
= KAUTH_VNODE_GENERIC_EXECUTE_BITS
;
4535 if ((error
= kauth_acl_evaluate(cred
, &eval
)) != 0) {
4536 KAUTH_DEBUG("%p ERROR during ACL processing - %d", vcp
->vp
, error
);
4539 if (eval
.ae_result
== KAUTH_RESULT_DENY
)
4541 if (eval
.ae_result
== KAUTH_RESULT_ALLOW
) {
4542 KAUTH_DEBUG("%p ALLOWED - granted by file ACL", vcp
->vp
);
4547 /* if denied by ACL on directory or node, return denial */
4548 if (delete_denied
|| delete_child_denied
) {
4549 KAUTH_DEBUG("%p ALLOWED - denied by ACL", vcp
->vp
);
4553 /* enforce sticky bit behaviour */
4554 if ((dvap
->va_mode
& S_ISTXT
) && !vauth_file_owner(vcp
) && !vauth_dir_owner(vcp
)) {
4555 KAUTH_DEBUG("%p DENIED - sticky bit rules (user %d file %d dir %d)",
4556 vcp
->vp
, cred
->cr_uid
, vap
->va_uid
, dvap
->va_uid
);
4560 /* check the directory */
4561 if ((error
= vnode_authorize_posix(vcp
, VWRITE
, 1 /* on_dir */)) != 0) {
4562 KAUTH_DEBUG("%p ALLOWED - granted by posix permisssions", vcp
->vp
);
4566 /* not denied, must be OK */
4572 * Authorize an operation based on the node's attributes.
4575 vnode_authorize_simple(vauth_ctx vcp
, kauth_ace_rights_t acl_rights
, kauth_ace_rights_t preauth_rights
)
4577 struct vnode_attr
*vap
= vcp
->vap
;
4578 kauth_cred_t cred
= vcp
->ctx
->vc_ucred
;
4579 struct kauth_acl_eval eval
;
4580 int error
, ismember
;
4581 mode_t posix_action
;
4584 * If we are the file owner, we automatically have some rights.
4586 * Do we need to expand this to support group ownership?
4588 if (vauth_file_owner(vcp
))
4589 acl_rights
&= ~(KAUTH_VNODE_WRITE_SECURITY
);
4592 * If we are checking both TAKE_OWNERSHIP and WRITE_SECURITY, we can
4593 * mask the latter. If TAKE_OWNERSHIP is requested the caller is about to
4594 * change ownership to themselves, and WRITE_SECURITY is implicitly
4595 * granted to the owner. We need to do this because at this point
4596 * WRITE_SECURITY may not be granted as the caller is not currently
4599 if ((acl_rights
& KAUTH_VNODE_TAKE_OWNERSHIP
) &&
4600 (acl_rights
& KAUTH_VNODE_WRITE_SECURITY
))
4601 acl_rights
&= ~KAUTH_VNODE_WRITE_SECURITY
;
4603 if (acl_rights
== 0) {
4604 KAUTH_DEBUG("%p ALLOWED - implicit or no rights required", vcp
->vp
);
4608 /* if we have an ACL, evaluate it */
4609 if (VATTR_IS_NOT(vap
, va_acl
, NULL
)) {
4610 eval
.ae_requested
= acl_rights
;
4611 eval
.ae_acl
= &vap
->va_acl
->acl_ace
[0];
4612 eval
.ae_count
= vap
->va_acl
->acl_entrycount
;
4613 eval
.ae_options
= 0;
4614 if (vauth_file_owner(vcp
))
4615 eval
.ae_options
|= KAUTH_AEVAL_IS_OWNER
;
4616 if ((error
= vauth_file_ingroup(vcp
, &ismember
)) != 0)
4619 eval
.ae_options
|= KAUTH_AEVAL_IN_GROUP
;
4620 eval
.ae_exp_gall
= KAUTH_VNODE_GENERIC_ALL_BITS
;
4621 eval
.ae_exp_gread
= KAUTH_VNODE_GENERIC_READ_BITS
;
4622 eval
.ae_exp_gwrite
= KAUTH_VNODE_GENERIC_WRITE_BITS
;
4623 eval
.ae_exp_gexec
= KAUTH_VNODE_GENERIC_EXECUTE_BITS
;
4625 if ((error
= kauth_acl_evaluate(cred
, &eval
)) != 0) {
4626 KAUTH_DEBUG("%p ERROR during ACL processing - %d", vcp
->vp
, error
);
4630 if (eval
.ae_result
== KAUTH_RESULT_DENY
) {
4631 KAUTH_DEBUG("%p DENIED - by ACL", vcp
->vp
);
4632 return(EACCES
); /* deny, deny, counter-allege */
4634 if (eval
.ae_result
== KAUTH_RESULT_ALLOW
) {
4635 KAUTH_DEBUG("%p ALLOWED - all rights granted by ACL", vcp
->vp
);
4638 /* fall through and evaluate residual rights */
4640 /* no ACL, everything is residual */
4641 eval
.ae_residual
= acl_rights
;
4645 * Grant residual rights that have been pre-authorized.
4647 eval
.ae_residual
&= ~preauth_rights
;
4650 * We grant WRITE_ATTRIBUTES to the owner if it hasn't been denied.
4652 if (vauth_file_owner(vcp
))
4653 eval
.ae_residual
&= ~KAUTH_VNODE_WRITE_ATTRIBUTES
;
4655 if (eval
.ae_residual
== 0) {
4656 KAUTH_DEBUG("%p ALLOWED - rights already authorized", vcp
->vp
);
4661 * Bail if we have residual rights that can't be granted by posix permissions,
4662 * or aren't presumed granted at this point.
4664 * XXX these can be collapsed for performance
4666 if (eval
.ae_residual
& KAUTH_VNODE_CHANGE_OWNER
) {
4667 KAUTH_DEBUG("%p DENIED - CHANGE_OWNER not permitted", vcp
->vp
);
4670 if (eval
.ae_residual
& KAUTH_VNODE_WRITE_SECURITY
) {
4671 KAUTH_DEBUG("%p DENIED - WRITE_SECURITY not permitted", vcp
->vp
);
4676 if (eval
.ae_residual
& KAUTH_VNODE_DELETE
)
4677 panic("vnode_authorize: can't be checking delete permission here");
4681 * Compute the fallback posix permissions that will satisfy the remaining
4685 if (eval
.ae_residual
& (KAUTH_VNODE_READ_DATA
|
4686 KAUTH_VNODE_LIST_DIRECTORY
|
4687 KAUTH_VNODE_READ_EXTATTRIBUTES
))
4688 posix_action
|= VREAD
;
4689 if (eval
.ae_residual
& (KAUTH_VNODE_WRITE_DATA
|
4690 KAUTH_VNODE_ADD_FILE
|
4691 KAUTH_VNODE_ADD_SUBDIRECTORY
|
4692 KAUTH_VNODE_DELETE_CHILD
|
4693 KAUTH_VNODE_WRITE_ATTRIBUTES
|
4694 KAUTH_VNODE_WRITE_EXTATTRIBUTES
))
4695 posix_action
|= VWRITE
;
4696 if (eval
.ae_residual
& (KAUTH_VNODE_EXECUTE
|
4697 KAUTH_VNODE_SEARCH
))
4698 posix_action
|= VEXEC
;
4700 if (posix_action
!= 0) {
4701 return(vnode_authorize_posix(vcp
, posix_action
, 0 /* !on_dir */));
4703 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",
4705 (eval
.ae_residual
& KAUTH_VNODE_READ_DATA
)
4706 ? vnode_isdir(vcp
->vp
) ? " LIST_DIRECTORY" : " READ_DATA" : "",
4707 (eval
.ae_residual
& KAUTH_VNODE_WRITE_DATA
)
4708 ? vnode_isdir(vcp
->vp
) ? " ADD_FILE" : " WRITE_DATA" : "",
4709 (eval
.ae_residual
& KAUTH_VNODE_EXECUTE
)
4710 ? vnode_isdir(vcp
->vp
) ? " SEARCH" : " EXECUTE" : "",
4711 (eval
.ae_residual
& KAUTH_VNODE_DELETE
)
4713 (eval
.ae_residual
& KAUTH_VNODE_APPEND_DATA
)
4714 ? vnode_isdir(vcp
->vp
) ? " ADD_SUBDIRECTORY" : " APPEND_DATA" : "",
4715 (eval
.ae_residual
& KAUTH_VNODE_DELETE_CHILD
)
4716 ? " DELETE_CHILD" : "",
4717 (eval
.ae_residual
& KAUTH_VNODE_READ_ATTRIBUTES
)
4718 ? " READ_ATTRIBUTES" : "",
4719 (eval
.ae_residual
& KAUTH_VNODE_WRITE_ATTRIBUTES
)
4720 ? " WRITE_ATTRIBUTES" : "",
4721 (eval
.ae_residual
& KAUTH_VNODE_READ_EXTATTRIBUTES
)
4722 ? " READ_EXTATTRIBUTES" : "",
4723 (eval
.ae_residual
& KAUTH_VNODE_WRITE_EXTATTRIBUTES
)
4724 ? " WRITE_EXTATTRIBUTES" : "",
4725 (eval
.ae_residual
& KAUTH_VNODE_READ_SECURITY
)
4726 ? " READ_SECURITY" : "",
4727 (eval
.ae_residual
& KAUTH_VNODE_WRITE_SECURITY
)
4728 ? " WRITE_SECURITY" : "",
4729 (eval
.ae_residual
& KAUTH_VNODE_CHECKIMMUTABLE
)
4730 ? " CHECKIMMUTABLE" : "",
4731 (eval
.ae_residual
& KAUTH_VNODE_CHANGE_OWNER
)
4732 ? " CHANGE_OWNER" : "");
4736 * Lack of required Posix permissions implies no reason to deny access.
4742 * Check for file immutability.
4745 vnode_authorize_checkimmutable(vnode_t vp
, struct vnode_attr
*vap
, int rights
, int ignore
)
4752 * Perform immutability checks for operations that change data.
4754 * Sockets, fifos and devices require special handling.
4756 switch(vp
->v_type
) {
4762 * Writing to these nodes does not change the filesystem data,
4763 * so forget that it's being tried.
4765 rights
&= ~KAUTH_VNODE_WRITE_DATA
;
4772 if (rights
& KAUTH_VNODE_WRITE_RIGHTS
) {
4774 /* check per-filesystem options if possible */
4775 mp
= vnode_mount(vp
);
4778 /* check for no-EA filesystems */
4779 if ((rights
& KAUTH_VNODE_WRITE_EXTATTRIBUTES
) &&
4780 (vfs_flags(mp
) & MNT_NOUSERXATTR
)) {
4781 KAUTH_DEBUG("%p DENIED - filesystem disallowed extended attributes", vp
);
4782 error
= EACCES
; /* User attributes disabled */
4787 /* check for file immutability */
4789 if (vp
->v_type
== VDIR
) {
4790 if ((rights
& (KAUTH_VNODE_ADD_FILE
| KAUTH_VNODE_ADD_SUBDIRECTORY
)) == rights
)
4793 if ((rights
& KAUTH_VNODE_APPEND_DATA
) == rights
)
4796 if ((error
= vnode_immutable(vap
, append
, ignore
)) != 0) {
4797 KAUTH_DEBUG("%p DENIED - file is immutable", vp
);
4806 * Handle authorization actions for filesystems that advertise that the server will
4810 vnode_authorize_opaque(vnode_t vp
, int *resultp
, kauth_action_t action
, vfs_context_t ctx
)
4815 * If the vp is a device node, socket or FIFO it actually represents a local
4816 * endpoint, so we need to handle it locally.
4818 switch(vp
->v_type
) {
4829 * In the advisory request case, if the filesystem doesn't think it's reliable
4830 * we will attempt to formulate a result ourselves based on VNOP_GETATTR data.
4832 if ((action
& KAUTH_VNODE_ACCESS
) && !vfs_authopaqueaccess(vnode_mount(vp
)))
4836 * Let the filesystem have a say in the matter. It's OK for it to not implemnent
4837 * VNOP_ACCESS, as most will authorise inline with the actual request.
4839 if ((error
= VNOP_ACCESS(vp
, action
, ctx
)) != ENOTSUP
) {
4841 KAUTH_DEBUG("%p DENIED - opaque filesystem VNOP_ACCESS denied access", vp
);
4846 * Typically opaque filesystems do authorisation in-line, but exec is a special case. In
4847 * order to be reasonably sure that exec will be permitted, we try a bit harder here.
4849 if ((action
& KAUTH_VNODE_EXECUTE
) && vnode_isreg(vp
)) {
4850 /* try a VNOP_OPEN for readonly access */
4851 if ((error
= VNOP_OPEN(vp
, FREAD
, ctx
)) != 0) {
4853 KAUTH_DEBUG("%p DENIED - EXECUTE denied because file could not be opened readonly", vp
);
4856 VNOP_CLOSE(vp
, FREAD
, ctx
);
4860 * We don't have any reason to believe that the request has to be denied at this point,
4861 * so go ahead and allow it.
4864 KAUTH_DEBUG("%p ALLOWED - bypassing access check for non-local filesystem", vp
);
4869 vnode_authorize_callback(__unused kauth_cred_t unused_cred
, __unused
void *idata
, kauth_action_t action
,
4870 uintptr_t arg0
, uintptr_t arg1
, uintptr_t arg2
, uintptr_t arg3
)
4872 struct _vnode_authorize_context auth_context
;
4877 kauth_ace_rights_t rights
;
4878 struct vnode_attr va
, dva
;
4883 vcp
= &auth_context
;
4884 ctx
= vcp
->ctx
= (vfs_context_t
)arg0
;
4885 vp
= vcp
->vp
= (vnode_t
)arg1
;
4886 dvp
= vcp
->dvp
= (vnode_t
)arg2
;
4887 errorp
= (int *)arg3
;
4888 /* note that we authorize against the context, not the passed cred (the same thing anyway) */
4889 cred
= ctx
->vc_ucred
;
4896 vcp
->flags
= vcp
->flags_valid
= 0;
4899 if ((ctx
== NULL
) || (vp
== NULL
) || (cred
== NULL
))
4900 panic("vnode_authorize: bad arguments (context %p vp %p cred %p)", ctx
, vp
, cred
);
4903 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)",
4904 vp
, vfs_context_proc(ctx
)->p_comm
,
4905 (action
& KAUTH_VNODE_ACCESS
) ? "access" : "auth",
4906 (action
& KAUTH_VNODE_READ_DATA
) ? vnode_isdir(vp
) ? " LIST_DIRECTORY" : " READ_DATA" : "",
4907 (action
& KAUTH_VNODE_WRITE_DATA
) ? vnode_isdir(vp
) ? " ADD_FILE" : " WRITE_DATA" : "",
4908 (action
& KAUTH_VNODE_EXECUTE
) ? vnode_isdir(vp
) ? " SEARCH" : " EXECUTE" : "",
4909 (action
& KAUTH_VNODE_DELETE
) ? " DELETE" : "",
4910 (action
& KAUTH_VNODE_APPEND_DATA
) ? vnode_isdir(vp
) ? " ADD_SUBDIRECTORY" : " APPEND_DATA" : "",
4911 (action
& KAUTH_VNODE_DELETE_CHILD
) ? " DELETE_CHILD" : "",
4912 (action
& KAUTH_VNODE_READ_ATTRIBUTES
) ? " READ_ATTRIBUTES" : "",
4913 (action
& KAUTH_VNODE_WRITE_ATTRIBUTES
) ? " WRITE_ATTRIBUTES" : "",
4914 (action
& KAUTH_VNODE_READ_EXTATTRIBUTES
) ? " READ_EXTATTRIBUTES" : "",
4915 (action
& KAUTH_VNODE_WRITE_EXTATTRIBUTES
) ? " WRITE_EXTATTRIBUTES" : "",
4916 (action
& KAUTH_VNODE_READ_SECURITY
) ? " READ_SECURITY" : "",
4917 (action
& KAUTH_VNODE_WRITE_SECURITY
) ? " WRITE_SECURITY" : "",
4918 (action
& KAUTH_VNODE_CHANGE_OWNER
) ? " CHANGE_OWNER" : "",
4919 (action
& KAUTH_VNODE_NOIMMUTABLE
) ? " (noimmutable)" : "",
4920 vnode_isdir(vp
) ? "directory" : "file",
4921 vp
->v_name
? vp
->v_name
: "<NULL>", action
, vp
, dvp
);
4924 * Extract the control bits from the action, everything else is
4927 noimmutable
= (action
& KAUTH_VNODE_NOIMMUTABLE
) ? 1 : 0;
4928 rights
= action
& ~(KAUTH_VNODE_ACCESS
| KAUTH_VNODE_NOIMMUTABLE
);
4930 if (rights
& KAUTH_VNODE_DELETE
) {
4933 panic("vnode_authorize: KAUTH_VNODE_DELETE test requires a directory");
4940 * Check for read-only filesystems.
4942 if ((rights
& KAUTH_VNODE_WRITE_RIGHTS
) &&
4943 (vp
->v_mount
->mnt_flag
& MNT_RDONLY
) &&
4944 ((vp
->v_type
== VREG
) || (vp
->v_type
== VDIR
) ||
4945 (vp
->v_type
== VLNK
) || (vp
->v_type
== VCPLX
) ||
4946 (rights
& KAUTH_VNODE_DELETE
) || (rights
& KAUTH_VNODE_DELETE_CHILD
))) {
4952 * Check for noexec filesystems.
4954 if ((rights
& KAUTH_VNODE_EXECUTE
) && vnode_isreg(vp
) && (vp
->v_mount
->mnt_flag
& MNT_NOEXEC
)) {
4960 * Handle cases related to filesystems with non-local enforcement.
4961 * This call can return 0, in which case we will fall through to perform a
4962 * check based on VNOP_GETATTR data. Otherwise it returns 1 and sets
4963 * an appropriate result, at which point we can return immediately.
4965 if (vfs_authopaque(vp
->v_mount
) && vnode_authorize_opaque(vp
, &result
, action
, ctx
))
4969 * Get vnode attributes and extended security information for the vnode
4970 * and directory if required.
4972 VATTR_WANTED(&va
, va_mode
);
4973 VATTR_WANTED(&va
, va_uid
);
4974 VATTR_WANTED(&va
, va_gid
);
4975 VATTR_WANTED(&va
, va_flags
);
4976 VATTR_WANTED(&va
, va_acl
);
4977 if ((result
= vnode_getattr(vp
, &va
, ctx
)) != 0) {
4978 KAUTH_DEBUG("%p ERROR - failed to get vnode attributes - %d", vp
, result
);
4982 VATTR_WANTED(&dva
, va_mode
);
4983 VATTR_WANTED(&dva
, va_uid
);
4984 VATTR_WANTED(&dva
, va_gid
);
4985 VATTR_WANTED(&dva
, va_flags
);
4986 VATTR_WANTED(&dva
, va_acl
);
4987 if ((result
= vnode_getattr(dvp
, &dva
, ctx
)) != 0) {
4988 KAUTH_DEBUG("%p ERROR - failed to get directory vnode attributes - %d", vp
, result
);
4994 * If the vnode is an extended attribute data vnode (eg. a resource fork), *_DATA becomes
4997 if (S_ISXATTR(va
.va_mode
)) {
4998 if (rights
& KAUTH_VNODE_READ_DATA
) {
4999 rights
&= ~KAUTH_VNODE_READ_DATA
;
5000 rights
|= KAUTH_VNODE_READ_EXTATTRIBUTES
;
5002 if (rights
& KAUTH_VNODE_WRITE_DATA
) {
5003 rights
&= ~KAUTH_VNODE_WRITE_DATA
;
5004 rights
|= KAUTH_VNODE_WRITE_EXTATTRIBUTES
;
5009 * Check for immutability.
5011 * In the deletion case, parent directory immutability vetoes specific
5014 if ((result
= vnode_authorize_checkimmutable(vp
, &va
, rights
, noimmutable
)) != 0)
5016 if ((rights
& KAUTH_VNODE_DELETE
) &&
5017 ((result
= vnode_authorize_checkimmutable(dvp
, &dva
, KAUTH_VNODE_DELETE_CHILD
, 0)) != 0))
5021 * Clear rights that have been authorized by reaching this point, bail if nothing left to
5024 rights
&= ~(KAUTH_VNODE_LINKTARGET
| KAUTH_VNODE_CHECKIMMUTABLE
);
5029 * If we're not the superuser, authorize based on file properties.
5031 if (!vfs_context_issuser(ctx
)) {
5032 /* process delete rights */
5033 if ((rights
& KAUTH_VNODE_DELETE
) &&
5034 ((result
= vnode_authorize_delete(vcp
)) != 0))
5037 /* process remaining rights */
5038 if ((rights
& ~KAUTH_VNODE_DELETE
) &&
5039 ((result
= vnode_authorize_simple(vcp
, rights
, rights
& KAUTH_VNODE_DELETE
)) != 0))
5044 * Execute is only granted to root if one of the x bits is set. This check only
5045 * makes sense if the posix mode bits are actually supported.
5047 if ((rights
& KAUTH_VNODE_EXECUTE
) &&
5048 (vp
->v_type
== VREG
) &&
5049 VATTR_IS_SUPPORTED(&va
, va_mode
) &&
5050 !(va
.va_mode
& (S_IXUSR
| S_IXGRP
| S_IXOTH
))) {
5052 KAUTH_DEBUG("%p DENIED - root execute requires at least one x bit in 0x%x", vp
, va
.va_mode
);
5056 KAUTH_DEBUG("%p ALLOWED - caller is superuser", vp
);
5060 if (VATTR_IS_SUPPORTED(&va
, va_acl
) && (va
.va_acl
!= NULL
))
5061 kauth_acl_free(va
.va_acl
);
5062 if (VATTR_IS_SUPPORTED(&dva
, va_acl
) && (dva
.va_acl
!= NULL
))
5063 kauth_acl_free(dva
.va_acl
);
5066 KAUTH_DEBUG("%p DENIED - auth denied", vp
);
5067 return(KAUTH_RESULT_DENY
);
5071 * Note that this implies that we will allow requests for no rights, as well as
5072 * for rights that we do not recognise. There should be none of these.
5074 KAUTH_DEBUG("%p ALLOWED - auth granted", vp
);
5075 return(KAUTH_RESULT_ALLOW
);
5079 * Check that the attribute information in vattr can be legally applied to
5080 * a new file by the context.
5083 vnode_authattr_new(vnode_t dvp
, struct vnode_attr
*vap
, int noauth
, vfs_context_t ctx
)
5086 int is_suser
, ismember
, defaulted_owner
, defaulted_group
, defaulted_mode
;
5092 defaulted_owner
= defaulted_group
= defaulted_mode
= 0;
5095 * Require that the filesystem support extended security to apply any.
5097 if (!vfs_extendedsecurity(dvp
->v_mount
) &&
5098 (VATTR_IS_ACTIVE(vap
, va_acl
) || VATTR_IS_ACTIVE(vap
, va_uuuid
) || VATTR_IS_ACTIVE(vap
, va_guuid
))) {
5104 * Default some fields.
5109 * If the filesystem is mounted IGNORE_OWNERSHIP and an explicit owner is set, that
5110 * owner takes ownership of all new files.
5112 if ((dmp
->mnt_flag
& MNT_IGNORE_OWNERSHIP
) && (dmp
->mnt_fsowner
!= KAUTH_UID_NONE
)) {
5113 VATTR_SET(vap
, va_uid
, dmp
->mnt_fsowner
);
5114 defaulted_owner
= 1;
5116 if (!VATTR_IS_ACTIVE(vap
, va_uid
)) {
5117 /* default owner is current user */
5118 VATTR_SET(vap
, va_uid
, kauth_cred_getuid(vfs_context_ucred(ctx
)));
5119 defaulted_owner
= 1;
5124 * If the filesystem is mounted IGNORE_OWNERSHIP and an explicit grouo is set, that
5125 * group takes ownership of all new files.
5127 if ((dmp
->mnt_flag
& MNT_IGNORE_OWNERSHIP
) && (dmp
->mnt_fsgroup
!= KAUTH_GID_NONE
)) {
5128 VATTR_SET(vap
, va_gid
, dmp
->mnt_fsgroup
);
5129 defaulted_group
= 1;
5131 if (!VATTR_IS_ACTIVE(vap
, va_gid
)) {
5132 /* default group comes from parent object, fallback to current user */
5133 struct vnode_attr dva
;
5135 VATTR_WANTED(&dva
, va_gid
);
5136 if ((error
= vnode_getattr(dvp
, &dva
, ctx
)) != 0)
5138 if (VATTR_IS_SUPPORTED(&dva
, va_gid
)) {
5139 VATTR_SET(vap
, va_gid
, dva
.va_gid
);
5141 VATTR_SET(vap
, va_gid
, kauth_cred_getgid(vfs_context_ucred(ctx
)));
5143 defaulted_group
= 1;
5147 if (!VATTR_IS_ACTIVE(vap
, va_flags
))
5148 VATTR_SET(vap
, va_flags
, 0);
5150 /* default mode is everything, masked with current umask */
5151 if (!VATTR_IS_ACTIVE(vap
, va_mode
)) {
5152 VATTR_SET(vap
, va_mode
, ACCESSPERMS
& ~vfs_context_proc(ctx
)->p_fd
->fd_cmask
);
5153 KAUTH_DEBUG("ATTR - defaulting new file mode to %o from umask %o", vap
->va_mode
, vfs_context_proc(ctx
)->p_fd
->fd_cmask
);
5156 /* set timestamps to now */
5157 if (!VATTR_IS_ACTIVE(vap
, va_create_time
)) {
5158 nanotime(&vap
->va_create_time
);
5159 VATTR_SET_ACTIVE(vap
, va_create_time
);
5163 * Check for attempts to set nonsensical fields.
5165 if (vap
->va_active
& ~VNODE_ATTR_NEWOBJ
) {
5167 KAUTH_DEBUG("ATTR - ERROR - attempt to set unsupported new-file attributes %llx",
5168 vap
->va_active
& ~VNODE_ATTR_NEWOBJ
);
5173 * Quickly check for the applicability of any enforcement here.
5174 * Tests below maintain the integrity of the local security model.
5176 if (vfs_authopaque(vnode_mount(dvp
)))
5180 * We need to know if the caller is the superuser, or if the work is
5181 * otherwise already authorised.
5183 cred
= vfs_context_ucred(ctx
);
5185 /* doing work for the kernel */
5188 is_suser
= vfs_context_issuser(ctx
);
5192 if (VATTR_IS_ACTIVE(vap
, va_flags
)) {
5194 if ((vap
->va_flags
& (UF_SETTABLE
| SF_SETTABLE
)) != vap
->va_flags
) {
5196 KAUTH_DEBUG(" DENIED - superuser attempt to set illegal flag(s)");
5200 if ((vap
->va_flags
& UF_SETTABLE
) != vap
->va_flags
) {
5202 KAUTH_DEBUG(" DENIED - user attempt to set illegal flag(s)");
5208 /* if not superuser, validate legality of new-item attributes */
5210 if (!defaulted_mode
&& VATTR_IS_ACTIVE(vap
, va_mode
)) {
5212 if (vap
->va_mode
& S_ISGID
) {
5213 if ((error
= kauth_cred_ismember_gid(cred
, vap
->va_gid
, &ismember
)) != 0) {
5214 KAUTH_DEBUG("ATTR - ERROR: got %d checking for membership in %d", error
, vap
->va_gid
);
5218 KAUTH_DEBUG(" DENIED - can't set SGID bit, not a member of %d", vap
->va_gid
);
5225 if ((vap
->va_mode
& S_ISUID
) && (vap
->va_uid
!= kauth_cred_getuid(cred
))) {
5226 KAUTH_DEBUG("ATTR - ERROR: illegal attempt to set the setuid bit");
5231 if (!defaulted_owner
&& (vap
->va_uid
!= kauth_cred_getuid(cred
))) {
5232 KAUTH_DEBUG(" DENIED - cannot create new item owned by %d", vap
->va_uid
);
5236 if (!defaulted_group
) {
5237 if ((error
= kauth_cred_ismember_gid(cred
, vap
->va_gid
, &ismember
)) != 0) {
5238 KAUTH_DEBUG(" ERROR - got %d checking for membership in %d", error
, vap
->va_gid
);
5242 KAUTH_DEBUG(" DENIED - cannot create new item with group %d - not a member", vap
->va_gid
);
5248 /* initialising owner/group UUID */
5249 if (VATTR_IS_ACTIVE(vap
, va_uuuid
)) {
5250 if ((error
= kauth_cred_getguid(cred
, &changer
)) != 0) {
5251 KAUTH_DEBUG(" ERROR - got %d trying to get caller UUID", error
);
5252 /* XXX ENOENT here - no GUID - should perhaps become EPERM */
5255 if (!kauth_guid_equal(&vap
->va_uuuid
, &changer
)) {
5256 KAUTH_DEBUG(" ERROR - cannot create item with supplied owner UUID - not us");
5261 if (VATTR_IS_ACTIVE(vap
, va_guuid
)) {
5262 if ((error
= kauth_cred_ismember_guid(cred
, &vap
->va_guuid
, &ismember
)) != 0) {
5263 KAUTH_DEBUG(" ERROR - got %d trying to check group membership", error
);
5267 KAUTH_DEBUG(" ERROR - cannot create item with supplied group UUID - not a member");
5278 * Check that the attribute information in vap can be legally written by the context.
5280 * Call this when you're not sure about the vnode_attr; either its contents have come
5281 * from an unknown source, or when they are variable.
5283 * Returns errno, or zero and sets *actionp to the KAUTH_VNODE_* actions that
5284 * must be authorized to be permitted to write the vattr.
5287 vnode_authattr(vnode_t vp
, struct vnode_attr
*vap
, kauth_action_t
*actionp
, vfs_context_t ctx
)
5289 struct vnode_attr ova
;
5290 kauth_action_t required_action
;
5291 int error
, is_suser
, ismember
, chowner
, chgroup
;
5300 required_action
= 0;
5304 * Quickly check for enforcement applicability.
5306 if (vfs_authopaque(vnode_mount(vp
)))
5310 * Check for attempts to set nonsensical fields.
5312 if (vap
->va_active
& VNODE_ATTR_RDONLY
) {
5313 KAUTH_DEBUG("ATTR - ERROR: attempt to set readonly attribute(s)");
5319 * We need to know if the caller is the superuser.
5321 cred
= vfs_context_ucred(ctx
);
5322 is_suser
= kauth_cred_issuser(cred
);
5325 * If any of the following are changing, we need information from the old file:
5332 if (VATTR_IS_ACTIVE(vap
, va_uid
) ||
5333 VATTR_IS_ACTIVE(vap
, va_gid
) ||
5334 VATTR_IS_ACTIVE(vap
, va_mode
) ||
5335 VATTR_IS_ACTIVE(vap
, va_uuuid
) ||
5336 VATTR_IS_ACTIVE(vap
, va_guuid
)) {
5337 VATTR_WANTED(&ova
, va_mode
);
5338 VATTR_WANTED(&ova
, va_uid
);
5339 VATTR_WANTED(&ova
, va_gid
);
5340 VATTR_WANTED(&ova
, va_uuuid
);
5341 VATTR_WANTED(&ova
, va_guuid
);
5342 KAUTH_DEBUG("ATTR - security information changing, fetching existing attributes");
5346 * If timestamps are being changed, we need to know who the file is owned
5349 if (VATTR_IS_ACTIVE(vap
, va_create_time
) ||
5350 VATTR_IS_ACTIVE(vap
, va_change_time
) ||
5351 VATTR_IS_ACTIVE(vap
, va_modify_time
) ||
5352 VATTR_IS_ACTIVE(vap
, va_access_time
) ||
5353 VATTR_IS_ACTIVE(vap
, va_backup_time
)) {
5355 VATTR_WANTED(&ova
, va_uid
);
5356 #if 0 /* enable this when we support UUIDs as official owners */
5357 VATTR_WANTED(&ova
, va_uuuid
);
5359 KAUTH_DEBUG("ATTR - timestamps changing, fetching uid and GUID");
5363 * If flags are being changed, we need the old flags.
5365 if (VATTR_IS_ACTIVE(vap
, va_flags
)) {
5366 KAUTH_DEBUG("ATTR - flags changing, fetching old flags");
5367 VATTR_WANTED(&ova
, va_flags
);
5371 * If the size is being set, make sure it's not a directory.
5373 if (VATTR_IS_ACTIVE(vap
, va_data_size
)) {
5374 /* size is meaningless on a directory, don't permit this */
5375 if (vnode_isdir(vp
)) {
5376 KAUTH_DEBUG("ATTR - ERROR: size change requested on a directory");
5385 KAUTH_DEBUG("ATTR - fetching old attributes %016llx", ova
.va_active
);
5386 if ((error
= vnode_getattr(vp
, &ova
, ctx
)) != 0) {
5387 KAUTH_DEBUG(" ERROR - got %d trying to get attributes", error
);
5392 * Size changes require write access to the file data.
5394 if (VATTR_IS_ACTIVE(vap
, va_data_size
)) {
5395 /* if we can't get the size, or it's different, we need write access */
5396 KAUTH_DEBUG("ATTR - size change, requiring WRITE_DATA");
5397 required_action
|= KAUTH_VNODE_WRITE_DATA
;
5401 * Changing timestamps?
5403 * Note that we are only called to authorize user-requested time changes;
5404 * side-effect time changes are not authorized. Authorisation is only
5405 * required for existing files.
5407 * Non-owners are not permitted to change the time on an existing
5408 * file to anything other than the current time.
5410 if (VATTR_IS_ACTIVE(vap
, va_create_time
) ||
5411 VATTR_IS_ACTIVE(vap
, va_change_time
) ||
5412 VATTR_IS_ACTIVE(vap
, va_modify_time
) ||
5413 VATTR_IS_ACTIVE(vap
, va_access_time
) ||
5414 VATTR_IS_ACTIVE(vap
, va_backup_time
)) {
5416 * The owner and root may set any timestamps they like,
5417 * provided that the file is not immutable. The owner still needs
5418 * WRITE_ATTRIBUTES (implied by ownership but still deniable).
5420 if (is_suser
|| vauth_node_owner(&ova
, cred
)) {
5421 KAUTH_DEBUG("ATTR - root or owner changing timestamps");
5422 required_action
|= KAUTH_VNODE_CHECKIMMUTABLE
| KAUTH_VNODE_WRITE_ATTRIBUTES
;
5424 /* just setting the current time? */
5425 if (vap
->va_vaflags
& VA_UTIMES_NULL
) {
5426 KAUTH_DEBUG("ATTR - non-root/owner changing timestamps, requiring WRITE_ATTRIBUTES");
5427 required_action
|= KAUTH_VNODE_WRITE_ATTRIBUTES
;
5429 KAUTH_DEBUG("ATTR - ERROR: illegal timestamp modification attempted");
5437 * Changing file mode?
5439 if (VATTR_IS_ACTIVE(vap
, va_mode
) && VATTR_IS_SUPPORTED(&ova
, va_mode
) && (ova
.va_mode
!= vap
->va_mode
)) {
5440 KAUTH_DEBUG("ATTR - mode change from %06o to %06o", ova
.va_mode
, vap
->va_mode
);
5443 * Mode changes always have the same basic auth requirements.
5446 KAUTH_DEBUG("ATTR - superuser mode change, requiring immutability check");
5447 required_action
|= KAUTH_VNODE_CHECKIMMUTABLE
;
5449 /* need WRITE_SECURITY */
5450 KAUTH_DEBUG("ATTR - non-superuser mode change, requiring WRITE_SECURITY");
5451 required_action
|= KAUTH_VNODE_WRITE_SECURITY
;
5455 * Can't set the setgid bit if you're not in the group and not root. Have to have
5456 * existing group information in the case we're not setting it right now.
5458 if (vap
->va_mode
& S_ISGID
) {
5459 required_action
|= KAUTH_VNODE_CHECKIMMUTABLE
; /* always required */
5461 if (VATTR_IS_ACTIVE(vap
, va_gid
)) {
5462 group
= vap
->va_gid
;
5463 } else if (VATTR_IS_SUPPORTED(&ova
, va_gid
)) {
5466 KAUTH_DEBUG("ATTR - ERROR: setgid but no gid available");
5471 * This might be too restrictive; WRITE_SECURITY might be implied by
5472 * membership in this case, rather than being an additional requirement.
5474 if ((error
= kauth_cred_ismember_gid(cred
, group
, &ismember
)) != 0) {
5475 KAUTH_DEBUG("ATTR - ERROR: got %d checking for membership in %d", error
, vap
->va_gid
);
5479 KAUTH_DEBUG(" DENIED - can't set SGID bit, not a member of %d", group
);
5487 * Can't set the setuid bit unless you're root or the file's owner.
5489 if (vap
->va_mode
& S_ISUID
) {
5490 required_action
|= KAUTH_VNODE_CHECKIMMUTABLE
; /* always required */
5492 if (VATTR_IS_ACTIVE(vap
, va_uid
)) {
5493 owner
= vap
->va_uid
;
5494 } else if (VATTR_IS_SUPPORTED(&ova
, va_uid
)) {
5497 KAUTH_DEBUG("ATTR - ERROR: setuid but no uid available");
5501 if (owner
!= kauth_cred_getuid(cred
)) {
5503 * We could allow this if WRITE_SECURITY is permitted, perhaps.
5505 KAUTH_DEBUG("ATTR - ERROR: illegal attempt to set the setuid bit");
5514 * Validate/mask flags changes. This checks that only the flags in
5515 * the UF_SETTABLE mask are being set, and preserves the flags in
5516 * the SF_SETTABLE case.
5518 * Since flags changes may be made in conjunction with other changes,
5519 * we will ask the auth code to ignore immutability in the case that
5520 * the SF_* flags are not set and we are only manipulating the file flags.
5523 if (VATTR_IS_ACTIVE(vap
, va_flags
)) {
5524 /* compute changing flags bits */
5525 if (VATTR_IS_SUPPORTED(&ova
, va_flags
)) {
5526 fdelta
= vap
->va_flags
^ ova
.va_flags
;
5528 fdelta
= vap
->va_flags
;
5532 KAUTH_DEBUG("ATTR - flags changing, requiring WRITE_SECURITY");
5533 required_action
|= KAUTH_VNODE_WRITE_SECURITY
;
5535 /* check that changing bits are legal */
5538 * The immutability check will prevent us from clearing the SF_*
5539 * flags unless the system securelevel permits it, so just check
5540 * for legal flags here.
5542 if (fdelta
& ~(UF_SETTABLE
| SF_SETTABLE
)) {
5544 KAUTH_DEBUG(" DENIED - superuser attempt to set illegal flag(s)");
5548 if (fdelta
& ~UF_SETTABLE
) {
5550 KAUTH_DEBUG(" DENIED - user attempt to set illegal flag(s)");
5555 * If the caller has the ability to manipulate file flags,
5556 * security is not reduced by ignoring them for this operation.
5558 * A more complete test here would consider the 'after' states of the flags
5559 * to determine whether it would permit the operation, but this becomes
5562 * Ignoring immutability is conditional on securelevel; this does not bypass
5563 * the SF_* flags if securelevel > 0.
5565 required_action
|= KAUTH_VNODE_NOIMMUTABLE
;
5570 * Validate ownership information.
5577 * Note that if the filesystem didn't give us a UID, we expect that it doesn't
5578 * support them in general, and will ignore it if/when we try to set it.
5579 * We might want to clear the uid out of vap completely here.
5581 if (VATTR_IS_ACTIVE(vap
, va_uid
) && VATTR_IS_SUPPORTED(&ova
, va_uid
) && (vap
->va_uid
!= ova
.va_uid
)) {
5582 if (!is_suser
&& (kauth_cred_getuid(cred
) != vap
->va_uid
)) {
5583 KAUTH_DEBUG(" DENIED - non-superuser cannot change ownershipt to a third party");
5592 * Note that if the filesystem didn't give us a GID, we expect that it doesn't
5593 * support them in general, and will ignore it if/when we try to set it.
5594 * We might want to clear the gid out of vap completely here.
5596 if (VATTR_IS_ACTIVE(vap
, va_gid
) && VATTR_IS_SUPPORTED(&ova
, va_gid
) && (vap
->va_gid
!= ova
.va_gid
)) {
5598 if ((error
= kauth_cred_ismember_gid(cred
, vap
->va_gid
, &ismember
)) != 0) {
5599 KAUTH_DEBUG(" ERROR - got %d checking for membership in %d", error
, vap
->va_gid
);
5603 KAUTH_DEBUG(" DENIED - group change from %d to %d but not a member of target group",
5604 ova
.va_gid
, vap
->va_gid
);
5613 * Owner UUID being set or changed.
5615 if (VATTR_IS_ACTIVE(vap
, va_uuuid
)) {
5616 /* if the owner UUID is not actually changing ... */
5617 if (VATTR_IS_SUPPORTED(&ova
, va_uuuid
) && kauth_guid_equal(&vap
->va_uuuid
, &ova
.va_uuuid
))
5618 goto no_uuuid_change
;
5621 * The owner UUID cannot be set by a non-superuser to anything other than
5625 if ((error
= kauth_cred_getguid(cred
, &changer
)) != 0) {
5626 KAUTH_DEBUG(" ERROR - got %d trying to get caller UUID", error
);
5627 /* XXX ENOENT here - no UUID - should perhaps become EPERM */
5630 if (!kauth_guid_equal(&vap
->va_uuuid
, &changer
)) {
5631 KAUTH_DEBUG(" ERROR - cannot set supplied owner UUID - not us");
5640 * Group UUID being set or changed.
5642 if (VATTR_IS_ACTIVE(vap
, va_guuid
)) {
5643 /* if the group UUID is not actually changing ... */
5644 if (VATTR_IS_SUPPORTED(&ova
, va_guuid
) && kauth_guid_equal(&vap
->va_guuid
, &ova
.va_guuid
))
5645 goto no_guuid_change
;
5648 * The group UUID cannot be set by a non-superuser to anything other than
5649 * one of which they are a member.
5652 if ((error
= kauth_cred_ismember_guid(cred
, &vap
->va_guuid
, &ismember
)) != 0) {
5653 KAUTH_DEBUG(" ERROR - got %d trying to check group membership", error
);
5657 KAUTH_DEBUG(" ERROR - cannot create item with supplied group UUID - not a member");
5667 * Compute authorisation for group/ownership changes.
5669 if (chowner
|| chgroup
) {
5671 KAUTH_DEBUG("ATTR - superuser changing file owner/group, requiring immutability check");
5672 required_action
|= KAUTH_VNODE_CHECKIMMUTABLE
;
5675 KAUTH_DEBUG("ATTR - ownership change, requiring TAKE_OWNERSHIP");
5676 required_action
|= KAUTH_VNODE_TAKE_OWNERSHIP
;
5678 if (chgroup
&& !chowner
) {
5679 KAUTH_DEBUG("ATTR - group change, requiring WRITE_SECURITY");
5680 required_action
|= KAUTH_VNODE_WRITE_SECURITY
;
5683 /* clear set-uid and set-gid bits as required by Posix */
5684 if (VATTR_IS_ACTIVE(vap
, va_mode
)) {
5685 newmode
= vap
->va_mode
;
5686 } else if (VATTR_IS_SUPPORTED(&ova
, va_mode
)) {
5687 newmode
= ova
.va_mode
;
5689 KAUTH_DEBUG("CHOWN - trying to change owner but cannot get mode from filesystem to mask setugid bits");
5692 if (newmode
& (S_ISUID
| S_ISGID
)) {
5693 VATTR_SET(vap
, va_mode
, newmode
& ~(S_ISUID
| S_ISGID
));
5694 KAUTH_DEBUG("CHOWN - masking setugid bits from mode %o to %o", newmode
, vap
->va_mode
);
5700 * Authorise changes in the ACL.
5702 if (VATTR_IS_ACTIVE(vap
, va_acl
)) {
5704 /* no existing ACL */
5705 if (!VATTR_IS_ACTIVE(&ova
, va_acl
) || (ova
.va_acl
== NULL
)) {
5708 if (vap
->va_acl
!= NULL
) {
5709 required_action
|= KAUTH_VNODE_WRITE_SECURITY
;
5710 KAUTH_DEBUG("CHMOD - adding ACL");
5713 /* removing an existing ACL */
5714 } else if (vap
->va_acl
== NULL
) {
5715 required_action
|= KAUTH_VNODE_WRITE_SECURITY
;
5716 KAUTH_DEBUG("CHMOD - removing ACL");
5718 /* updating an existing ACL */
5720 if (vap
->va_acl
->acl_entrycount
!= ova
.va_acl
->acl_entrycount
) {
5721 /* entry count changed, must be different */
5722 required_action
|= KAUTH_VNODE_WRITE_SECURITY
;
5723 KAUTH_DEBUG("CHMOD - adding/removing ACL entries");
5724 } else if (vap
->va_acl
->acl_entrycount
> 0) {
5725 /* both ACLs have the same ACE count, said count is 1 or more, bitwise compare ACLs */
5726 if (!memcmp(&vap
->va_acl
->acl_ace
[0], &ova
.va_acl
->acl_ace
[0],
5727 sizeof(struct kauth_ace
) * vap
->va_acl
->acl_entrycount
)) {
5728 required_action
|= KAUTH_VNODE_WRITE_SECURITY
;
5729 KAUTH_DEBUG("CHMOD - changing ACL entries");
5736 * Other attributes that require authorisation.
5738 if (VATTR_IS_ACTIVE(vap
, va_encoding
))
5739 required_action
|= KAUTH_VNODE_WRITE_ATTRIBUTES
;
5742 if (VATTR_IS_SUPPORTED(&ova
, va_acl
) && (ova
.va_acl
!= NULL
))
5743 kauth_acl_free(ova
.va_acl
);
5745 *actionp
= required_action
;
5751 vfs_setlocklocal(mount_t mp
)
5756 mp
->mnt_kern_flag
|= MNTK_LOCK_LOCAL
;
5759 * We do not expect anyone to be using any vnodes at the
5760 * time this routine is called. So no need for vnode locking
5762 TAILQ_FOREACH(vp
, &mp
->mnt_vnodelist
, v_mntvnodes
) {
5763 vp
->v_flag
|= VLOCKLOCAL
;
5765 TAILQ_FOREACH(vp
, &mp
->mnt_workerqueue
, v_mntvnodes
) {
5766 vp
->v_flag
|= VLOCKLOCAL
;
5768 TAILQ_FOREACH(vp
, &mp
->mnt_newvnodes
, v_mntvnodes
) {
5769 vp
->v_flag
|= VLOCKLOCAL
;
5777 record_vp(vnode_t vp
, int count
) {
5781 if ((vp
->v_flag
& VSYSTEM
))
5784 ut
= get_bsdthread_info(current_thread());
5785 ut
->uu_iocount
+= count
;
5787 if (ut
->uu_vpindex
< 32) {
5788 for (i
= 0; i
< ut
->uu_vpindex
; i
++) {
5789 if (ut
->uu_vps
[i
] == vp
)
5792 ut
->uu_vps
[ut
->uu_vpindex
] = vp
;