2 * Copyright (c) 2000-2008 Apple Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
28 /* Copyright (c) 1995 NeXT Computer, Inc. All Rights Reserved */
30 * Copyright (c) 1989, 1993
31 * The Regents of the University of California. All rights reserved.
32 * (c) UNIX System Laboratories, Inc.
33 * All or some portions of this file are derived from material licensed
34 * to the University of California by American Telephone and Telegraph
35 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
36 * the permission of UNIX System Laboratories, Inc.
38 * Redistribution and use in source and binary forms, with or without
39 * modification, are permitted provided that the following conditions
41 * 1. Redistributions of source code must retain the above copyright
42 * notice, this list of conditions and the following disclaimer.
43 * 2. Redistributions in binary form must reproduce the above copyright
44 * notice, this list of conditions and the following disclaimer in the
45 * documentation and/or other materials provided with the distribution.
46 * 3. All advertising materials mentioning features or use of this software
47 * must display the following acknowledgement:
48 * This product includes software developed by the University of
49 * California, Berkeley and its contributors.
50 * 4. Neither the name of the University nor the names of its contributors
51 * may be used to endorse or promote products derived from this software
52 * without specific prior written permission.
54 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
55 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
56 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
57 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
58 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
59 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
60 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
61 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
62 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
63 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
66 * @(#)vfs_subr.c 8.31 (Berkeley) 5/26/95
69 * NOTICE: This file was modified by SPARTA, Inc. in 2005 to introduce
70 * support for mandatory and extensible security protections. This notice
71 * is included in support of clause 2.2 (b) of the Apple Public License,
76 * External virtual filesystem routines
80 #include <sys/param.h>
81 #include <sys/systm.h>
82 #include <sys/proc_internal.h>
83 #include <sys/kauth.h>
84 #include <sys/mount_internal.h>
87 #include <sys/vnode.h>
88 #include <sys/vnode_internal.h>
90 #include <sys/namei.h>
91 #include <sys/ucred.h>
92 #include <sys/buf_internal.h>
93 #include <sys/errno.h>
94 #include <sys/malloc.h>
95 #include <sys/uio_internal.h>
97 #include <sys/domain.h>
99 #include <sys/syslog.h>
100 #include <sys/ubc_internal.h>
102 #include <sys/sysctl.h>
103 #include <sys/filedesc.h>
104 #include <sys/event.h>
105 #include <sys/kdebug.h>
106 #include <sys/kauth.h>
107 #include <sys/user.h>
108 #include <miscfs/fifofs/fifo.h>
111 #include <machine/spl.h>
114 #include <kern/assert.h>
116 #include <miscfs/specfs/specdev.h>
118 #include <mach/mach_types.h>
119 #include <mach/memory_object_types.h>
121 #include <kern/kalloc.h> /* kalloc()/kfree() */
122 #include <kern/clock.h> /* delay_for_interval() */
123 #include <libkern/OSAtomic.h> /* OSAddAtomic() */
126 #include <vm/vm_protos.h> /* vnode_pager_vrele() */
129 #include <security/mac_framework.h>
132 extern lck_grp_t
*vnode_lck_grp
;
133 extern lck_attr_t
*vnode_lck_attr
;
136 extern lck_mtx_t
* mnt_list_mtx_lock
;
138 enum vtype iftovt_tab
[16] = {
139 VNON
, VFIFO
, VCHR
, VNON
, VDIR
, VNON
, VBLK
, VNON
,
140 VREG
, VNON
, VLNK
, VNON
, VSOCK
, VNON
, VNON
, VBAD
,
142 int vttoif_tab
[9] = {
143 0, S_IFREG
, S_IFDIR
, S_IFBLK
, S_IFCHR
, S_IFLNK
,
144 S_IFSOCK
, S_IFIFO
, S_IFMT
,
147 /* XXX next protptype should be from <nfs/nfs.h> */
148 extern int nfs_vinvalbuf(vnode_t
, int, vfs_context_t
, int);
150 /* XXX next prototytype should be from libsa/stdlib.h> but conflicts libkern */
151 __private_extern__
void qsort(
155 int (*)(const void *, const void *));
157 extern kern_return_t
adjust_vm_object_cache(vm_size_t oval
, vm_size_t nval
);
158 __private_extern__
void vntblinit(void);
159 __private_extern__ kern_return_t
reset_vmobjectcache(unsigned int val1
,
161 __private_extern__
int unlink1(vfs_context_t
, struct nameidata
*, int);
163 extern int system_inshutdown
;
165 static void vnode_list_add(vnode_t
);
166 static void vnode_list_remove(vnode_t
);
167 static void vnode_list_remove_locked(vnode_t
);
169 static errno_t
vnode_drain(vnode_t
);
170 static void vgone(vnode_t
, int flags
);
171 static void vclean(vnode_t vp
, int flag
);
172 static void vnode_reclaim_internal(vnode_t
, int, int, int);
174 static void vnode_dropiocount (vnode_t
);
175 static errno_t
vnode_getiocount(vnode_t vp
, unsigned int vid
, int vflags
);
177 static vnode_t
checkalias(vnode_t vp
, dev_t nvp_rdev
);
178 static int vnode_reload(vnode_t
);
179 static int vnode_isinuse_locked(vnode_t
, int, int);
181 static void insmntque(vnode_t vp
, mount_t mp
);
182 static int mount_getvfscnt(void);
183 static int mount_fillfsids(fsid_t
*, int );
184 static void vnode_iterate_setup(mount_t
);
185 static int vnode_umount_preflight(mount_t
, vnode_t
, int);
186 static int vnode_iterate_prepare(mount_t
);
187 static int vnode_iterate_reloadq(mount_t
);
188 static void vnode_iterate_clear(mount_t
);
189 static mount_t
vfs_getvfs_locked(fsid_t
*);
191 errno_t
rmdir_remove_orphaned_appleDouble(vnode_t
, vfs_context_t
, int *);
194 static void record_vp(vnode_t vp
, int count
);
197 TAILQ_HEAD(freelst
, vnode
) vnode_free_list
; /* vnode free list */
198 TAILQ_HEAD(deadlst
, vnode
) vnode_dead_list
; /* vnode dead list */
200 TAILQ_HEAD(ragelst
, vnode
) vnode_rage_list
; /* vnode rapid age list */
201 struct timeval rage_tv
;
205 #define RAGE_LIMIT_MIN 100
206 #define RAGE_TIME_LIMIT 5
208 struct mntlist mountlist
; /* mounted filesystem list */
209 static int nummounts
= 0;
212 #define VLISTCHECK(fun, vp, list) \
213 if ((vp)->v_freelist.tqe_prev == (struct vnode **)0xdeadb) \
214 panic("%s: %s vnode not on %slist", (fun), (list), (list));
216 #define VLISTCHECK(fun, vp, list)
217 #endif /* DIAGNOSTIC */
219 #define VLISTNONE(vp) \
221 (vp)->v_freelist.tqe_next = (struct vnode *)0; \
222 (vp)->v_freelist.tqe_prev = (struct vnode **)0xdeadb; \
225 #define VONLIST(vp) \
226 ((vp)->v_freelist.tqe_prev != (struct vnode **)0xdeadb)
228 /* remove a vnode from free vnode list */
229 #define VREMFREE(fun, vp) \
231 VLISTCHECK((fun), (vp), "free"); \
232 TAILQ_REMOVE(&vnode_free_list, (vp), v_freelist); \
239 /* remove a vnode from dead vnode list */
240 #define VREMDEAD(fun, vp) \
242 VLISTCHECK((fun), (vp), "dead"); \
243 TAILQ_REMOVE(&vnode_dead_list, (vp), v_freelist); \
245 vp->v_listflag &= ~VLIST_DEAD; \
250 /* remove a vnode from rage vnode list */
251 #define VREMRAGE(fun, vp) \
253 if ( !(vp->v_listflag & VLIST_RAGE)) \
254 panic("VREMRAGE: vp not on rage list"); \
255 VLISTCHECK((fun), (vp), "rage"); \
256 TAILQ_REMOVE(&vnode_rage_list, (vp), v_freelist); \
258 vp->v_listflag &= ~VLIST_RAGE; \
264 * vnodetarget hasn't been used in a long time, but
265 * it was exported for some reason... I'm leaving in
266 * place for now... it should be deprecated out of the
267 * exports and removed eventually.
269 u_int32_t vnodetarget
; /* target for vnreclaim() */
270 #define VNODE_FREE_TARGET 20 /* Default value for vnodetarget */
273 * We need quite a few vnodes on the free list to sustain the
274 * rapid stat() the compilation process does, and still benefit from the name
275 * cache. Having too few vnodes on the free list causes serious disk
276 * thrashing as we cycle through them.
278 #define VNODE_FREE_MIN CONFIG_VNODE_FREE_MIN /* freelist should have at least this many */
281 * Initialize the vnode management data structures.
283 __private_extern__
void
286 TAILQ_INIT(&vnode_free_list
);
287 TAILQ_INIT(&vnode_rage_list
);
288 TAILQ_INIT(&vnode_dead_list
);
289 TAILQ_INIT(&mountlist
);
292 vnodetarget
= VNODE_FREE_TARGET
;
294 microuptime(&rage_tv
);
295 rage_limit
= desiredvnodes
/ 100;
297 if (rage_limit
< RAGE_LIMIT_MIN
)
298 rage_limit
= RAGE_LIMIT_MIN
;
301 * Scale the vm_object_cache to accomodate the vnodes
304 (void) adjust_vm_object_cache(0, desiredvnodes
- VNODE_FREE_MIN
);
307 /* Reset the VM Object Cache with the values passed in */
308 __private_extern__ kern_return_t
309 reset_vmobjectcache(unsigned int val1
, unsigned int val2
)
311 vm_size_t oval
= val1
- VNODE_FREE_MIN
;
318 if(val2
< VNODE_FREE_MIN
)
321 nval
= val2
- VNODE_FREE_MIN
;
323 return(adjust_vm_object_cache(oval
, nval
));
327 /* the timeout is in 10 msecs */
329 vnode_waitforwrites(vnode_t vp
, int output_target
, int slpflag
, int slptimeout
, const char *msg
) {
333 KERNEL_DEBUG(0x3010280 | DBG_FUNC_START
, (int)vp
, output_target
, vp
->v_numoutput
, 0, 0);
335 if (vp
->v_numoutput
> output_target
) {
341 while ((vp
->v_numoutput
> output_target
) && error
== 0) {
343 vp
->v_flag
|= VTHROTTLED
;
345 vp
->v_flag
|= VBWAIT
;
347 ts
.tv_sec
= (slptimeout
/100);
348 ts
.tv_nsec
= (slptimeout
% 1000) * 10 * NSEC_PER_USEC
* 1000 ;
349 error
= msleep((caddr_t
)&vp
->v_numoutput
, &vp
->v_lock
, (slpflag
| (PRIBIO
+ 1)), msg
, &ts
);
355 KERNEL_DEBUG(0x3010280 | DBG_FUNC_END
, (int)vp
, output_target
, vp
->v_numoutput
, error
, 0);
362 vnode_startwrite(vnode_t vp
) {
364 OSAddAtomic(1, &vp
->v_numoutput
);
369 vnode_writedone(vnode_t vp
)
372 OSAddAtomic(-1, &vp
->v_numoutput
);
374 if (vp
->v_numoutput
<= 1) {
379 if (vp
->v_numoutput
< 0)
380 panic("vnode_writedone: numoutput < 0");
382 if ((vp
->v_flag
& VTHROTTLED
) && (vp
->v_numoutput
<= 1)) {
383 vp
->v_flag
&= ~VTHROTTLED
;
386 if ((vp
->v_flag
& VBWAIT
) && (vp
->v_numoutput
== 0)) {
387 vp
->v_flag
&= ~VBWAIT
;
393 wakeup((caddr_t
)&vp
->v_numoutput
);
401 vnode_hasdirtyblks(vnode_t vp
)
403 struct cl_writebehind
*wbp
;
406 * Not taking the buf_mtxp as there is little
407 * point doing it. Even if the lock is taken the
408 * state can change right after that. If their
409 * needs to be a synchronization, it must be driven
412 if (vp
->v_dirtyblkhd
.lh_first
)
415 if (!UBCINFOEXISTS(vp
))
418 wbp
= vp
->v_ubcinfo
->cl_wbehind
;
420 if (wbp
&& (wbp
->cl_number
|| wbp
->cl_scmap
))
427 vnode_hascleanblks(vnode_t vp
)
430 * Not taking the buf_mtxp as there is little
431 * point doing it. Even if the lock is taken the
432 * state can change right after that. If their
433 * needs to be a synchronization, it must be driven
436 if (vp
->v_cleanblkhd
.lh_first
)
442 vnode_iterate_setup(mount_t mp
)
444 while (mp
->mnt_lflag
& MNT_LITER
) {
445 mp
->mnt_lflag
|= MNT_LITERWAIT
;
446 msleep((caddr_t
)mp
, &mp
->mnt_mlock
, PVFS
, "vnode_iterate_setup", NULL
);
449 mp
->mnt_lflag
|= MNT_LITER
;
454 vnode_umount_preflight(mount_t mp
, vnode_t skipvp
, int flags
)
458 TAILQ_FOREACH(vp
, &mp
->mnt_vnodelist
, v_mntvnodes
) {
459 /* disable preflight only for udf, a hack to be removed after 4073176 is fixed */
460 if (vp
->v_tag
== VT_UDF
)
462 if (vp
->v_type
== VDIR
)
466 if ((flags
& SKIPSYSTEM
) && ((vp
->v_flag
& VSYSTEM
) ||
467 (vp
->v_flag
& VNOFLUSH
)))
469 if ((flags
& SKIPSWAP
) && (vp
->v_flag
& VSWAP
))
471 if ((flags
& WRITECLOSE
) &&
472 (vp
->v_writecount
== 0 || vp
->v_type
!= VREG
))
474 /* Look for busy vnode */
475 if (((vp
->v_usecount
!= 0) &&
476 ((vp
->v_usecount
- vp
->v_kusecount
) != 0)))
484 * This routine prepares iteration by moving all the vnodes to worker queue
485 * called with mount lock held
488 vnode_iterate_prepare(mount_t mp
)
492 if (TAILQ_EMPTY(&mp
->mnt_vnodelist
)) {
497 vp
= TAILQ_FIRST(&mp
->mnt_vnodelist
);
498 vp
->v_mntvnodes
.tqe_prev
= &(mp
->mnt_workerqueue
.tqh_first
);
499 mp
->mnt_workerqueue
.tqh_first
= mp
->mnt_vnodelist
.tqh_first
;
500 mp
->mnt_workerqueue
.tqh_last
= mp
->mnt_vnodelist
.tqh_last
;
502 TAILQ_INIT(&mp
->mnt_vnodelist
);
503 if (mp
->mnt_newvnodes
.tqh_first
!= NULL
)
504 panic("vnode_iterate_prepare: newvnode when entering vnode");
505 TAILQ_INIT(&mp
->mnt_newvnodes
);
511 /* called with mount lock held */
513 vnode_iterate_reloadq(mount_t mp
)
517 /* add the remaining entries in workerq to the end of mount vnode list */
518 if (!TAILQ_EMPTY(&mp
->mnt_workerqueue
)) {
520 mvp
= TAILQ_LAST(&mp
->mnt_vnodelist
, vnodelst
);
522 /* Joining the workerque entities to mount vnode list */
524 mvp
->v_mntvnodes
.tqe_next
= mp
->mnt_workerqueue
.tqh_first
;
526 mp
->mnt_vnodelist
.tqh_first
= mp
->mnt_workerqueue
.tqh_first
;
527 mp
->mnt_workerqueue
.tqh_first
->v_mntvnodes
.tqe_prev
= mp
->mnt_vnodelist
.tqh_last
;
528 mp
->mnt_vnodelist
.tqh_last
= mp
->mnt_workerqueue
.tqh_last
;
529 TAILQ_INIT(&mp
->mnt_workerqueue
);
532 /* add the newvnodes to the head of mount vnode list */
533 if (!TAILQ_EMPTY(&mp
->mnt_newvnodes
)) {
535 nlvp
= TAILQ_LAST(&mp
->mnt_newvnodes
, vnodelst
);
537 mp
->mnt_newvnodes
.tqh_first
->v_mntvnodes
.tqe_prev
= &mp
->mnt_vnodelist
.tqh_first
;
538 nlvp
->v_mntvnodes
.tqe_next
= mp
->mnt_vnodelist
.tqh_first
;
539 if(mp
->mnt_vnodelist
.tqh_first
)
540 mp
->mnt_vnodelist
.tqh_first
->v_mntvnodes
.tqe_prev
= &nlvp
->v_mntvnodes
.tqe_next
;
542 mp
->mnt_vnodelist
.tqh_last
= mp
->mnt_newvnodes
.tqh_last
;
543 mp
->mnt_vnodelist
.tqh_first
= mp
->mnt_newvnodes
.tqh_first
;
544 TAILQ_INIT(&mp
->mnt_newvnodes
);
553 vnode_iterate_clear(mount_t mp
)
555 mp
->mnt_lflag
&= ~MNT_LITER
;
556 if (mp
->mnt_lflag
& MNT_LITERWAIT
) {
557 mp
->mnt_lflag
&= ~MNT_LITERWAIT
;
564 vnode_iterate(mount_t mp
, int flags
, int (*callout
)(struct vnode
*, void *),
573 vnode_iterate_setup(mp
);
575 /* it is returns 0 then there is nothing to do */
576 retval
= vnode_iterate_prepare(mp
);
579 vnode_iterate_clear(mp
);
584 /* iterate over all the vnodes */
585 while (!TAILQ_EMPTY(&mp
->mnt_workerqueue
)) {
586 vp
= TAILQ_FIRST(&mp
->mnt_workerqueue
);
587 TAILQ_REMOVE(&mp
->mnt_workerqueue
, vp
, v_mntvnodes
);
588 TAILQ_INSERT_TAIL(&mp
->mnt_vnodelist
, vp
, v_mntvnodes
);
590 if ((vp
->v_data
== NULL
) || (vp
->v_type
== VNON
) || (vp
->v_mount
!= mp
)) {
595 if ( vget_internal(vp
, vid
, (flags
| VNODE_NODEAD
| VNODE_WITHID
| VNODE_NOSUSPEND
))) {
599 if (flags
& VNODE_RELOAD
) {
601 * we're reloading the filesystem
602 * cast out any inactive vnodes...
604 if (vnode_reload(vp
)) {
605 /* vnode will be recycled on the refcount drop */
612 retval
= callout(vp
, arg
);
616 case VNODE_RETURNED_DONE
:
618 if (retval
== VNODE_RETURNED_DONE
) {
625 case VNODE_CLAIMED_DONE
:
637 (void)vnode_iterate_reloadq(mp
);
638 vnode_iterate_clear(mp
);
644 mount_lock_renames(mount_t mp
)
646 lck_mtx_lock(&mp
->mnt_renamelock
);
650 mount_unlock_renames(mount_t mp
)
652 lck_mtx_unlock(&mp
->mnt_renamelock
);
656 mount_lock(mount_t mp
)
658 lck_mtx_lock(&mp
->mnt_mlock
);
662 mount_lock_spin(mount_t mp
)
664 lck_mtx_lock_spin(&mp
->mnt_mlock
);
668 mount_unlock(mount_t mp
)
670 lck_mtx_unlock(&mp
->mnt_mlock
);
675 mount_ref(mount_t mp
, int locked
)
688 mount_drop(mount_t mp
, int locked
)
695 if (mp
->mnt_count
== 0 && (mp
->mnt_lflag
& MNT_LDRAIN
))
696 wakeup(&mp
->mnt_lflag
);
704 mount_iterref(mount_t mp
, int locked
)
710 if (mp
->mnt_iterref
< 0) {
721 mount_isdrained(mount_t mp
, int locked
)
727 if (mp
->mnt_iterref
< 0)
737 mount_iterdrop(mount_t mp
)
741 wakeup(&mp
->mnt_iterref
);
746 mount_iterdrain(mount_t mp
)
749 while (mp
->mnt_iterref
)
750 msleep((caddr_t
)&mp
->mnt_iterref
, mnt_list_mtx_lock
, PVFS
, "mount_iterdrain", NULL
);
751 /* mount iterations drained */
752 mp
->mnt_iterref
= -1;
756 mount_iterreset(mount_t mp
)
759 if (mp
->mnt_iterref
== -1)
764 /* always called with mount lock held */
766 mount_refdrain(mount_t mp
)
768 if (mp
->mnt_lflag
& MNT_LDRAIN
)
769 panic("already in drain");
770 mp
->mnt_lflag
|= MNT_LDRAIN
;
772 while (mp
->mnt_count
)
773 msleep((caddr_t
)&mp
->mnt_lflag
, &mp
->mnt_mlock
, PVFS
, "mount_drain", NULL
);
775 if (mp
->mnt_vnodelist
.tqh_first
!= NULL
)
776 panic("mount_refdrain: dangling vnode");
778 mp
->mnt_lflag
&= ~MNT_LDRAIN
;
785 * Mark a mount point as busy. Used to synchronize access and to delay
789 vfs_busy(mount_t mp
, int flags
)
793 if (mp
->mnt_lflag
& MNT_LDEAD
)
796 if (mp
->mnt_lflag
& MNT_LUNMOUNT
) {
797 if (flags
& LK_NOWAIT
)
802 if (mp
->mnt_lflag
& MNT_LDEAD
) {
806 if (mp
->mnt_lflag
& MNT_LUNMOUNT
) {
807 mp
->mnt_lflag
|= MNT_LWAIT
;
809 * Since all busy locks are shared except the exclusive
810 * lock granted when unmounting, the only place that a
811 * wakeup needs to be done is at the release of the
812 * exclusive lock at the end of dounmount.
814 msleep((caddr_t
)mp
, &mp
->mnt_mlock
, (PVFS
| PDROP
), "vfsbusy", NULL
);
820 lck_rw_lock_shared(&mp
->mnt_rwlock
);
823 * until we are granted the rwlock, it's possible for the mount point to
824 * change state, so reevaluate before granting the vfs_busy
826 if (mp
->mnt_lflag
& (MNT_LDEAD
| MNT_LUNMOUNT
)) {
827 lck_rw_done(&mp
->mnt_rwlock
);
834 * Free a busy filesystem.
838 vfs_unbusy(mount_t mp
)
840 lck_rw_done(&mp
->mnt_rwlock
);
846 vfs_rootmountfailed(mount_t mp
) {
849 mp
->mnt_vtable
->vfc_refcount
--;
854 mount_lock_destroy(mp
);
857 mac_mount_label_destroy(mp
);
860 FREE_ZONE(mp
, sizeof(struct mount
), M_MOUNT
);
864 * Lookup a filesystem type, and if found allocate and initialize
865 * a mount structure for it.
867 * Devname is usually updated by mount(8) after booting.
870 vfs_rootmountalloc_internal(struct vfstable
*vfsp
, const char *devname
)
874 mp
= _MALLOC_ZONE(sizeof(struct mount
), M_MOUNT
, M_WAITOK
);
875 bzero((char *)mp
, sizeof(struct mount
));
877 /* Initialize the default IO constraints */
878 mp
->mnt_maxreadcnt
= mp
->mnt_maxwritecnt
= MAXPHYS
;
879 mp
->mnt_segreadcnt
= mp
->mnt_segwritecnt
= 32;
880 mp
->mnt_maxsegreadsize
= mp
->mnt_maxreadcnt
;
881 mp
->mnt_maxsegwritesize
= mp
->mnt_maxwritecnt
;
882 mp
->mnt_devblocksize
= DEV_BSIZE
;
883 mp
->mnt_alignmentmask
= PAGE_MASK
;
884 mp
->mnt_ioqueue_depth
= MNT_DEFAULT_IOQUEUE_DEPTH
;
887 mp
->mnt_realrootvp
= NULLVP
;
888 mp
->mnt_authcache_ttl
= CACHED_LOOKUP_RIGHT_TTL
;
891 (void)vfs_busy(mp
, LK_NOWAIT
);
893 TAILQ_INIT(&mp
->mnt_vnodelist
);
894 TAILQ_INIT(&mp
->mnt_workerqueue
);
895 TAILQ_INIT(&mp
->mnt_newvnodes
);
897 mp
->mnt_vtable
= vfsp
;
898 mp
->mnt_op
= vfsp
->vfc_vfsops
;
899 mp
->mnt_flag
= MNT_RDONLY
| MNT_ROOTFS
;
900 mp
->mnt_vnodecovered
= NULLVP
;
901 //mp->mnt_stat.f_type = vfsp->vfc_typenum;
902 mp
->mnt_flag
|= vfsp
->vfc_flags
& MNT_VISFLAGMASK
;
905 vfsp
->vfc_refcount
++;
908 strncpy(mp
->mnt_vfsstat
.f_fstypename
, vfsp
->vfc_name
, MFSTYPENAMELEN
);
909 mp
->mnt_vfsstat
.f_mntonname
[0] = '/';
910 /* XXX const poisoning layering violation */
911 (void) copystr((const void *)devname
, mp
->mnt_vfsstat
.f_mntfromname
, MAXPATHLEN
- 1, NULL
);
914 mac_mount_label_init(mp
);
915 mac_mount_label_associate(vfs_context_kernel(), mp
);
921 vfs_rootmountalloc(const char *fstypename
, const char *devname
, mount_t
*mpp
)
923 struct vfstable
*vfsp
;
925 for (vfsp
= vfsconf
; vfsp
; vfsp
= vfsp
->vfc_next
)
926 if (!strncmp(vfsp
->vfc_name
, fstypename
,
927 sizeof(vfsp
->vfc_name
)))
932 *mpp
= vfs_rootmountalloc_internal(vfsp
, devname
);
942 * Find an appropriate filesystem to use for the root. If a filesystem
943 * has not been preselected, walk through the list of known filesystems
944 * trying those that have mountroot routines, and try them until one
945 * works or we have tried them all.
947 extern int (*mountroot
)(void);
955 struct vfstable
*vfsp
;
956 vfs_context_t ctx
= vfs_context_kernel();
957 struct vfs_attr vfsattr
;
960 vnode_t bdevvp_rootvp
;
962 if (mountroot
!= NULL
) {
964 * used for netboot which follows a different set of rules
966 error
= (*mountroot
)();
969 if ((error
= bdevvp(rootdev
, &rootvp
))) {
970 printf("vfs_mountroot: can't setup bdevvp\n");
974 * 4951998 - code we call in vfc_mountroot may replace rootvp
975 * so keep a local copy for some house keeping.
977 bdevvp_rootvp
= rootvp
;
979 for (vfsp
= vfsconf
; vfsp
; vfsp
= vfsp
->vfc_next
) {
980 if (vfsp
->vfc_mountroot
== NULL
)
983 mp
= vfs_rootmountalloc_internal(vfsp
, "root_device");
984 mp
->mnt_devvp
= rootvp
;
986 if ((error
= (*vfsp
->vfc_mountroot
)(mp
, rootvp
, ctx
)) == 0) {
987 if ( bdevvp_rootvp
!= rootvp
) {
990 * bump the iocount and fix up mnt_devvp for the
991 * new rootvp (it will already have a usecount taken)...
992 * drop the iocount and the usecount on the orignal
993 * since we are no longer going to use it...
995 vnode_getwithref(rootvp
);
996 mp
->mnt_devvp
= rootvp
;
998 vnode_rele(bdevvp_rootvp
);
999 vnode_put(bdevvp_rootvp
);
1001 mp
->mnt_devvp
->v_specflags
|= SI_MOUNTEDON
;
1008 * cache the IO attributes for the underlying physical media...
1009 * an error return indicates the underlying driver doesn't
1010 * support all the queries necessary... however, reasonable
1011 * defaults will have been set, so no reason to bail or care
1013 vfs_init_io_attributes(rootvp
, mp
);
1016 * Shadow the VFC_VFSNATIVEXATTR flag to MNTK_EXTENDED_ATTRS.
1018 if (mp
->mnt_vtable
->vfc_vfsflags
& VFC_VFSNATIVEXATTR
) {
1019 mp
->mnt_kern_flag
|= MNTK_EXTENDED_ATTRS
;
1021 if (mp
->mnt_vtable
->vfc_vfsflags
& VFC_VFSPREFLIGHT
) {
1022 mp
->mnt_kern_flag
|= MNTK_UNMOUNT_PREFLIGHT
;
1026 * Probe root file system for additional features.
1028 (void)VFS_START(mp
, 0, ctx
);
1030 VFSATTR_INIT(&vfsattr
);
1031 VFSATTR_WANTED(&vfsattr
, f_capabilities
);
1032 if (vfs_getattr(mp
, &vfsattr
, ctx
) == 0 &&
1033 VFSATTR_IS_SUPPORTED(&vfsattr
, f_capabilities
)) {
1034 if ((vfsattr
.f_capabilities
.capabilities
[VOL_CAPABILITIES_INTERFACES
] & VOL_CAP_INT_EXTENDED_ATTR
) &&
1035 (vfsattr
.f_capabilities
.valid
[VOL_CAPABILITIES_INTERFACES
] & VOL_CAP_INT_EXTENDED_ATTR
)) {
1036 mp
->mnt_kern_flag
|= MNTK_EXTENDED_ATTRS
;
1039 if ((vfsattr
.f_capabilities
.capabilities
[VOL_CAPABILITIES_INTERFACES
] & VOL_CAP_INT_NAMEDSTREAMS
) &&
1040 (vfsattr
.f_capabilities
.valid
[VOL_CAPABILITIES_INTERFACES
] & VOL_CAP_INT_NAMEDSTREAMS
)) {
1041 mp
->mnt_kern_flag
|= MNTK_NAMED_STREAMS
;
1044 if ((vfsattr
.f_capabilities
.capabilities
[VOL_CAPABILITIES_FORMAT
] & VOL_CAP_FMT_PATH_FROM_ID
) &&
1045 (vfsattr
.f_capabilities
.valid
[VOL_CAPABILITIES_FORMAT
] & VOL_CAP_FMT_PATH_FROM_ID
)) {
1046 mp
->mnt_kern_flag
|= MNTK_PATH_FROM_ID
;
1051 * get rid of iocount reference returned
1052 * by bdevvp (or picked up by us on the substitued
1053 * rootvp)... it (or we) will have also taken
1054 * a usecount reference which we want to keep
1059 if ((vfs_flags(mp
) & MNT_MULTILABEL
) == 0)
1062 error
= VFS_ROOT(mp
, &vp
, ctx
);
1064 printf("%s() VFS_ROOT() returned %d\n",
1066 dounmount(mp
, MNT_FORCE
, 0, ctx
);
1069 error
= vnode_label(mp
, NULL
, vp
, NULL
, 0, ctx
);
1071 * get rid of reference provided by VFS_ROOT
1076 printf("%s() vnode_label() returned %d\n",
1078 dounmount(mp
, MNT_FORCE
, 0, ctx
);
1087 vfs_rootmountfailed(mp
);
1089 if (error
!= EINVAL
)
1090 printf("%s_mountroot failed: %d\n", vfsp
->vfc_name
, error
);
1096 * Lookup a mount point by filesystem identifier.
1100 vfs_getvfs(fsid_t
*fsid
)
1102 return (mount_list_lookupby_fsid(fsid
, 0, 0));
1105 static struct mount
*
1106 vfs_getvfs_locked(fsid_t
*fsid
)
1108 return(mount_list_lookupby_fsid(fsid
, 1, 0));
1112 vfs_getvfs_by_mntonname(char *path
)
1114 mount_t retmp
= (mount_t
)0;
1118 TAILQ_FOREACH(mp
, &mountlist
, mnt_list
) {
1119 if (!strncmp(mp
->mnt_vfsstat
.f_mntonname
, path
,
1120 sizeof(mp
->mnt_vfsstat
.f_mntonname
))) {
1126 mount_list_unlock();
1130 /* generation number for creation of new fsids */
1131 u_short mntid_gen
= 0;
1133 * Get a new unique fsid
1136 vfs_getnewfsid(struct mount
*mp
)
1145 /* generate a new fsid */
1146 mtype
= mp
->mnt_vtable
->vfc_typenum
;
1147 if (++mntid_gen
== 0)
1149 tfsid
.val
[0] = makedev(nblkdev
+ mtype
, mntid_gen
);
1150 tfsid
.val
[1] = mtype
;
1152 TAILQ_FOREACH(nmp
, &mountlist
, mnt_list
) {
1153 while (vfs_getvfs_locked(&tfsid
)) {
1154 if (++mntid_gen
== 0)
1156 tfsid
.val
[0] = makedev(nblkdev
+ mtype
, mntid_gen
);
1159 mp
->mnt_vfsstat
.f_fsid
.val
[0] = tfsid
.val
[0];
1160 mp
->mnt_vfsstat
.f_fsid
.val
[1] = tfsid
.val
[1];
1161 mount_list_unlock();
1165 * Routines having to do with the management of the vnode table.
1167 extern int (**dead_vnodeop_p
)(void *);
1168 long numvnodes
, freevnodes
, deadvnodes
;
1172 * Move a vnode from one mount queue to another.
1175 insmntque(vnode_t vp
, mount_t mp
)
1179 * Delete from old mount point vnode list, if on one.
1181 if ( (lmp
= vp
->v_mount
) != NULL
&& lmp
!= dead_mountp
) {
1182 if ((vp
->v_lflag
& VNAMED_MOUNT
) == 0)
1183 panic("insmntque: vp not in mount vnode list");
1184 vp
->v_lflag
&= ~VNAMED_MOUNT
;
1186 mount_lock_spin(lmp
);
1190 if (vp
->v_mntvnodes
.tqe_next
== NULL
) {
1191 if (TAILQ_LAST(&lmp
->mnt_vnodelist
, vnodelst
) == vp
)
1192 TAILQ_REMOVE(&lmp
->mnt_vnodelist
, vp
, v_mntvnodes
);
1193 else if (TAILQ_LAST(&lmp
->mnt_newvnodes
, vnodelst
) == vp
)
1194 TAILQ_REMOVE(&lmp
->mnt_newvnodes
, vp
, v_mntvnodes
);
1195 else if (TAILQ_LAST(&lmp
->mnt_workerqueue
, vnodelst
) == vp
)
1196 TAILQ_REMOVE(&lmp
->mnt_workerqueue
, vp
, v_mntvnodes
);
1198 vp
->v_mntvnodes
.tqe_next
->v_mntvnodes
.tqe_prev
= vp
->v_mntvnodes
.tqe_prev
;
1199 *vp
->v_mntvnodes
.tqe_prev
= vp
->v_mntvnodes
.tqe_next
;
1201 vp
->v_mntvnodes
.tqe_next
= NULL
;
1202 vp
->v_mntvnodes
.tqe_prev
= NULL
;
1208 * Insert into list of vnodes for the new mount point, if available.
1210 if ((vp
->v_mount
= mp
) != NULL
) {
1211 mount_lock_spin(mp
);
1212 if ((vp
->v_mntvnodes
.tqe_next
!= 0) && (vp
->v_mntvnodes
.tqe_prev
!= 0))
1213 panic("vp already in mount list");
1214 if (mp
->mnt_lflag
& MNT_LITER
)
1215 TAILQ_INSERT_HEAD(&mp
->mnt_newvnodes
, vp
, v_mntvnodes
);
1217 TAILQ_INSERT_HEAD(&mp
->mnt_vnodelist
, vp
, v_mntvnodes
);
1218 if (vp
->v_lflag
& VNAMED_MOUNT
)
1219 panic("insmntque: vp already in mount vnode list");
1220 vp
->v_lflag
|= VNAMED_MOUNT
;
1228 * Create a vnode for a block device.
1229 * Used for root filesystem, argdev, and swap areas.
1230 * Also used for memory file system special devices.
1233 bdevvp(dev_t dev
, vnode_t
*vpp
)
1237 struct vnode_fsparam vfsp
;
1238 struct vfs_context context
;
1245 context
.vc_thread
= current_thread();
1246 context
.vc_ucred
= FSCRED
;
1248 vfsp
.vnfs_mp
= (struct mount
*)0;
1249 vfsp
.vnfs_vtype
= VBLK
;
1250 vfsp
.vnfs_str
= "bdevvp";
1251 vfsp
.vnfs_dvp
= NULL
;
1252 vfsp
.vnfs_fsnode
= NULL
;
1253 vfsp
.vnfs_cnp
= NULL
;
1254 vfsp
.vnfs_vops
= spec_vnodeop_p
;
1255 vfsp
.vnfs_rdev
= dev
;
1256 vfsp
.vnfs_filesize
= 0;
1258 vfsp
.vnfs_flags
= VNFS_NOCACHE
| VNFS_CANTCACHE
;
1260 vfsp
.vnfs_marksystem
= 0;
1261 vfsp
.vnfs_markroot
= 0;
1263 if ( (error
= vnode_create(VNCREATE_FLAVOR
, VCREATESIZE
, &vfsp
, &nvp
)) ) {
1267 vnode_lock_spin(nvp
);
1268 nvp
->v_flag
|= VBDEVVP
;
1269 nvp
->v_tag
= VT_NON
; /* set this to VT_NON so during aliasing it can be replaced */
1271 if ( (error
= vnode_ref(nvp
)) ) {
1272 panic("bdevvp failed: vnode_ref");
1275 if ( (error
= VNOP_FSYNC(nvp
, MNT_WAIT
, &context
)) ) {
1276 panic("bdevvp failed: fsync");
1279 if ( (error
= buf_invalidateblks(nvp
, BUF_WRITE_DATA
, 0, 0)) ) {
1280 panic("bdevvp failed: invalidateblks");
1286 * XXXMAC: We can't put a MAC check here, the system will
1287 * panic without this vnode.
1291 if ( (error
= VNOP_OPEN(nvp
, FREAD
, &context
)) ) {
1292 panic("bdevvp failed: open");
1301 * Check to see if the new vnode represents a special device
1302 * for which we already have a vnode (either because of
1303 * bdevvp() or because of a different vnode representing
1304 * the same block device). If such an alias exists, deallocate
1305 * the existing contents and return the aliased vnode. The
1306 * caller is responsible for filling it with its new contents.
1309 checkalias(struct vnode
*nvp
, dev_t nvp_rdev
)
1313 struct specinfo
*sin
= NULL
;
1316 vpp
= &speclisth
[SPECHASH(nvp_rdev
)];
1320 for (vp
= *vpp
; vp
; vp
= vp
->v_specnext
) {
1321 if (nvp_rdev
== vp
->v_rdev
&& nvp
->v_type
== vp
->v_type
) {
1330 if (vnode_getwithvid(vp
,vid
)) {
1334 * Termination state is checked in vnode_getwithvid
1339 * Alias, but not in use, so flush it out.
1341 if ((vp
->v_iocount
== 1) && (vp
->v_usecount
== 0)) {
1342 vnode_reclaim_internal(vp
, 1, 1, 0);
1343 vnode_put_locked(vp
);
1349 if (vp
== NULL
|| vp
->v_tag
!= VT_NON
) {
1351 MALLOC_ZONE(sin
, struct specinfo
*, sizeof(struct specinfo
),
1352 M_SPECINFO
, M_WAITOK
);
1355 nvp
->v_specinfo
= sin
;
1356 bzero(nvp
->v_specinfo
, sizeof(struct specinfo
));
1357 nvp
->v_rdev
= nvp_rdev
;
1358 nvp
->v_specflags
= 0;
1359 nvp
->v_speclastr
= -1;
1363 /* We dropped the lock, someone could have added */
1365 for (vp
= *vpp
; vp
; vp
= vp
->v_specnext
) {
1366 if (nvp_rdev
== vp
->v_rdev
&& nvp
->v_type
== vp
->v_type
) {
1374 nvp
->v_hashchain
= vpp
;
1375 nvp
->v_specnext
= *vpp
;
1379 nvp
->v_specflags
|= SI_ALIASED
;
1380 vp
->v_specflags
|= SI_ALIASED
;
1382 vnode_put_locked(vp
);
1392 FREE_ZONE(sin
, sizeof(struct specinfo
), M_SPECINFO
);
1395 if ((vp
->v_flag
& (VBDEVVP
| VDEVFLUSH
)) != 0)
1398 panic("checkalias with VT_NON vp that shouldn't: %p", vp
);
1405 * Get a reference on a particular vnode and lock it if requested.
1406 * If the vnode was on the inactive list, remove it from the list.
1407 * If the vnode was on the free list, remove it from the list and
1408 * move it to inactive list as needed.
1409 * The vnode lock bit is set if the vnode is being eliminated in
1410 * vgone. The process is awakened when the transition is completed,
1411 * and an error returned to indicate that the vnode is no longer
1412 * usable (possibly having been changed to a new file system type).
1415 vget_internal(vnode_t vp
, int vid
, int vflags
)
1420 vnode_lock_spin(vp
);
1422 if (vflags
& VNODE_WITHID
)
1425 vpid
= vp
->v_id
; // save off the original v_id
1427 if ((vflags
& VNODE_WRITEABLE
) && (vp
->v_writecount
== 0))
1429 * vnode to be returned only if it has writers opened
1433 error
= vnode_getiocount(vp
, vpid
, vflags
);
1441 * Returns: 0 Success
1442 * ENOENT No such file or directory [terminating]
1445 vnode_ref(vnode_t vp
)
1448 return (vnode_ref_ext(vp
, 0));
1452 * Returns: 0 Success
1453 * ENOENT No such file or directory [terminating]
1456 vnode_ref_ext(vnode_t vp
, int fmode
)
1460 vnode_lock_spin(vp
);
1463 * once all the current call sites have been fixed to insure they have
1464 * taken an iocount, we can toughen this assert up and insist that the
1465 * iocount is non-zero... a non-zero usecount doesn't insure correctness
1467 if (vp
->v_iocount
<= 0 && vp
->v_usecount
<= 0)
1468 panic("vnode_ref_ext: vp %p has no valid reference %d, %d", vp
, vp
->v_iocount
, vp
->v_usecount
);
1471 * if you are the owner of drain/termination, can acquire usecount
1473 if ((vp
->v_lflag
& (VL_DRAIN
| VL_TERMINATE
| VL_DEAD
))) {
1474 if (vp
->v_owner
!= current_thread()) {
1481 if (fmode
& FWRITE
) {
1482 if (++vp
->v_writecount
<= 0)
1483 panic("vnode_ref_ext: v_writecount");
1485 if (fmode
& O_EVTONLY
) {
1486 if (++vp
->v_kusecount
<= 0)
1487 panic("vnode_ref_ext: v_kusecount");
1489 if (vp
->v_flag
& VRAGE
) {
1492 ut
= get_bsdthread_info(current_thread());
1494 if ( !(current_proc()->p_lflag
& P_LRAGE_VNODES
) &&
1495 !(ut
->uu_flag
& UT_RAGE_VNODES
)) {
1497 * a 'normal' process accessed this vnode
1498 * so make sure its no longer marked
1499 * for rapid aging... also, make sure
1500 * it gets removed from the rage list...
1501 * when v_usecount drops back to 0, it
1502 * will be put back on the real free list
1504 vp
->v_flag
&= ~VRAGE
;
1505 vp
->v_references
= 0;
1506 vnode_list_remove(vp
);
1517 * put the vnode on appropriate free list.
1518 * called with vnode LOCKED
1521 vnode_list_add(vnode_t vp
)
1524 lck_mtx_assert(&vp
->v_lock
, LCK_MTX_ASSERT_OWNED
);
1527 * if it is already on a list or non zero references return
1529 if (VONLIST(vp
) || (vp
->v_usecount
!= 0) || (vp
->v_iocount
!= 0) || (vp
->v_lflag
& VL_TERMINATE
))
1534 if ((vp
->v_flag
& VRAGE
) && !(vp
->v_lflag
& VL_DEAD
)) {
1536 * add the new guy to the appropriate end of the RAGE list
1538 if ((vp
->v_flag
& VAGE
))
1539 TAILQ_INSERT_HEAD(&vnode_rage_list
, vp
, v_freelist
);
1541 TAILQ_INSERT_TAIL(&vnode_rage_list
, vp
, v_freelist
);
1543 vp
->v_listflag
|= VLIST_RAGE
;
1547 * reset the timestamp for the last inserted vp on the RAGE
1548 * queue to let new_vnode know that its not ok to start stealing
1549 * from this list... as long as we're actively adding to this list
1550 * we'll push out the vnodes we want to donate to the real free list
1551 * once we stop pushing, we'll let some time elapse before we start
1552 * stealing them in the new_vnode routine
1554 microuptime(&rage_tv
);
1557 * if VL_DEAD, insert it at head of the dead list
1558 * else insert at tail of LRU list or at head if VAGE is set
1560 if ( (vp
->v_lflag
& VL_DEAD
)) {
1561 TAILQ_INSERT_HEAD(&vnode_dead_list
, vp
, v_freelist
);
1562 vp
->v_listflag
|= VLIST_DEAD
;
1564 } else if ((vp
->v_flag
& VAGE
)) {
1565 TAILQ_INSERT_HEAD(&vnode_free_list
, vp
, v_freelist
);
1566 vp
->v_flag
&= ~VAGE
;
1569 TAILQ_INSERT_TAIL(&vnode_free_list
, vp
, v_freelist
);
1573 vnode_list_unlock();
1578 * remove the vnode from appropriate free list.
1579 * called with vnode LOCKED and
1580 * the list lock held
1583 vnode_list_remove_locked(vnode_t vp
)
1587 * the v_listflag field is
1588 * protected by the vnode_list_lock
1590 if (vp
->v_listflag
& VLIST_RAGE
)
1591 VREMRAGE("vnode_list_remove", vp
);
1592 else if (vp
->v_listflag
& VLIST_DEAD
)
1593 VREMDEAD("vnode_list_remove", vp
);
1595 VREMFREE("vnode_list_remove", vp
);
1601 * remove the vnode from appropriate free list.
1602 * called with vnode LOCKED
1605 vnode_list_remove(vnode_t vp
)
1608 lck_mtx_assert(&vp
->v_lock
, LCK_MTX_ASSERT_OWNED
);
1611 * we want to avoid taking the list lock
1612 * in the case where we're not on the free
1613 * list... this will be true for most
1614 * directories and any currently in use files
1616 * we're guaranteed that we can't go from
1617 * the not-on-list state to the on-list
1618 * state since we hold the vnode lock...
1619 * all calls to vnode_list_add are done
1620 * under the vnode lock... so we can
1621 * check for that condition (the prevelant one)
1622 * without taking the list lock
1627 * however, we're not guaranteed that
1628 * we won't go from the on-list state
1629 * to the not-on-list state until we
1630 * hold the vnode_list_lock... this
1631 * is due to "new_vnode" removing vnodes
1632 * from the free list uder the list_lock
1633 * w/o the vnode lock... so we need to
1634 * check again whether we're currently
1637 vnode_list_remove_locked(vp
);
1639 vnode_list_unlock();
1645 vnode_rele(vnode_t vp
)
1647 vnode_rele_internal(vp
, 0, 0, 0);
1652 vnode_rele_ext(vnode_t vp
, int fmode
, int dont_reenter
)
1654 vnode_rele_internal(vp
, fmode
, dont_reenter
, 0);
1659 vnode_rele_internal(vnode_t vp
, int fmode
, int dont_reenter
, int locked
)
1662 vnode_lock_spin(vp
);
1665 lck_mtx_assert(&vp
->v_lock
, LCK_MTX_ASSERT_OWNED
);
1667 if (--vp
->v_usecount
< 0)
1668 panic("vnode_rele_ext: vp %p usecount -ve : %d. v_tag = %d, v_type = %d, v_flag = %x.", vp
, vp
->v_usecount
, vp
->v_tag
, vp
->v_type
, vp
->v_flag
);
1670 if (fmode
& FWRITE
) {
1671 if (--vp
->v_writecount
< 0)
1672 panic("vnode_rele_ext: vp %p writecount -ve : %d. v_tag = %d, v_type = %d, v_flag = %x.", vp
, vp
->v_writecount
, vp
->v_tag
, vp
->v_type
, vp
->v_flag
);
1674 if (fmode
& O_EVTONLY
) {
1675 if (--vp
->v_kusecount
< 0)
1676 panic("vnode_rele_ext: vp %p kusecount -ve : %d. v_tag = %d, v_type = %d, v_flag = %x.", vp
, vp
->v_kusecount
, vp
->v_tag
, vp
->v_type
, vp
->v_flag
);
1678 if (vp
->v_kusecount
> vp
->v_usecount
)
1679 panic("vnode_rele_ext: vp %p kusecount(%d) out of balance with usecount(%d). v_tag = %d, v_type = %d, v_flag = %x.",vp
, vp
->v_kusecount
, vp
->v_usecount
, vp
->v_tag
, vp
->v_type
, vp
->v_flag
);
1681 if ((vp
->v_iocount
> 0) || (vp
->v_usecount
> 0)) {
1683 * vnode is still busy... if we're the last
1684 * usecount, mark for a future call to VNOP_INACTIVE
1685 * when the iocount finally drops to 0
1687 if (vp
->v_usecount
== 0) {
1688 vp
->v_lflag
|= VL_NEEDINACTIVE
;
1689 vp
->v_flag
&= ~(VNOCACHE_DATA
| VRAOFF
| VOPENEVT
);
1695 vp
->v_flag
&= ~(VNOCACHE_DATA
| VRAOFF
| VOPENEVT
);
1697 if ( (vp
->v_lflag
& (VL_TERMINATE
| VL_DEAD
)) || dont_reenter
) {
1699 * vnode is being cleaned, or
1700 * we've requested that we don't reenter
1701 * the filesystem on this release... in
1702 * this case, we'll mark the vnode aged
1703 * if it's been marked for termination
1706 if ( !(vp
->v_lflag
& (VL_TERMINATE
| VL_DEAD
| VL_MARKTERM
)) )
1707 vp
->v_lflag
|= VL_NEEDINACTIVE
;
1716 * at this point both the iocount and usecount
1718 * pick up an iocount so that we can call
1719 * VNOP_INACTIVE with the vnode lock unheld
1725 vp
->v_lflag
&= ~VL_NEEDINACTIVE
;
1728 VNOP_INACTIVE(vp
, vfs_context_current());
1730 vnode_lock_spin(vp
);
1732 * because we dropped the vnode lock to call VNOP_INACTIVE
1733 * the state of the vnode may have changed... we may have
1734 * picked up an iocount, usecount or the MARKTERM may have
1735 * been set... we need to reevaluate the reference counts
1736 * to determine if we can call vnode_reclaim_internal at
1737 * this point... if the reference counts are up, we'll pick
1738 * up the MARKTERM state when they get subsequently dropped
1740 if ( (vp
->v_iocount
== 1) && (vp
->v_usecount
== 0) &&
1741 ((vp
->v_lflag
& (VL_MARKTERM
| VL_TERMINATE
| VL_DEAD
)) == VL_MARKTERM
)) {
1744 ut
= get_bsdthread_info(current_thread());
1746 if (ut
->uu_defer_reclaims
) {
1747 vp
->v_defer_reclaimlist
= ut
->uu_vreclaims
;
1748 ut
->uu_vreclaims
= vp
;
1751 vnode_lock_convert(vp
);
1752 vnode_reclaim_internal(vp
, 1, 1, 0);
1754 vnode_dropiocount(vp
);
1763 * Remove any vnodes in the vnode table belonging to mount point mp.
1765 * If MNT_NOFORCE is specified, there should not be any active ones,
1766 * return error if any are found (nb: this is a user error, not a
1767 * system error). If MNT_FORCE is specified, detach any active vnodes
1771 int busyprt
= 0; /* print out busy vnodes */
1773 struct ctldebug debug1
= { "busyprt", &busyprt
};
1778 vflush(struct mount
*mp
, struct vnode
*skipvp
, int flags
)
1787 vnode_iterate_setup(mp
);
1789 * On regular unmounts(not forced) do a
1790 * quick check for vnodes to be in use. This
1791 * preserves the caching of vnodes. automounter
1792 * tries unmounting every so often to see whether
1793 * it is still busy or not.
1795 if (((flags
& FORCECLOSE
)==0) && ((mp
->mnt_kern_flag
& MNTK_UNMOUNT_PREFLIGHT
) != 0)) {
1796 if (vnode_umount_preflight(mp
, skipvp
, flags
)) {
1797 vnode_iterate_clear(mp
);
1803 /* it is returns 0 then there is nothing to do */
1804 retval
= vnode_iterate_prepare(mp
);
1807 vnode_iterate_clear(mp
);
1812 /* iterate over all the vnodes */
1813 while (!TAILQ_EMPTY(&mp
->mnt_workerqueue
)) {
1815 vp
= TAILQ_FIRST(&mp
->mnt_workerqueue
);
1816 TAILQ_REMOVE(&mp
->mnt_workerqueue
, vp
, v_mntvnodes
);
1817 TAILQ_INSERT_TAIL(&mp
->mnt_vnodelist
, vp
, v_mntvnodes
);
1819 if ( (vp
->v_mount
!= mp
) || (vp
== skipvp
)) {
1825 vnode_lock_spin(vp
);
1827 if ((vp
->v_id
!= vid
) || ((vp
->v_lflag
& (VL_DEAD
| VL_TERMINATE
)))) {
1834 * If requested, skip over vnodes marked VSYSTEM.
1835 * Skip over all vnodes marked VNOFLUSH.
1837 if ((flags
& SKIPSYSTEM
) && ((vp
->v_flag
& VSYSTEM
) ||
1838 (vp
->v_flag
& VNOFLUSH
))) {
1844 * If requested, skip over vnodes marked VSWAP.
1846 if ((flags
& SKIPSWAP
) && (vp
->v_flag
& VSWAP
)) {
1852 * If requested, skip over vnodes marked VROOT.
1854 if ((flags
& SKIPROOT
) && (vp
->v_flag
& VROOT
)) {
1860 * If WRITECLOSE is set, only flush out regular file
1861 * vnodes open for writing.
1863 if ((flags
& WRITECLOSE
) &&
1864 (vp
->v_writecount
== 0 || vp
->v_type
!= VREG
)) {
1870 * If the real usecount is 0, all we need to do is clear
1871 * out the vnode data structures and we are done.
1873 if (((vp
->v_usecount
== 0) ||
1874 ((vp
->v_usecount
- vp
->v_kusecount
) == 0))) {
1876 vnode_lock_convert(vp
);
1877 vp
->v_iocount
++; /* so that drain waits for * other iocounts */
1881 vnode_reclaim_internal(vp
, 1, 1, 0);
1882 vnode_dropiocount(vp
);
1891 * If FORCECLOSE is set, forcibly close the vnode.
1892 * For block or character devices, revert to an
1893 * anonymous device. For all other files, just kill them.
1895 if (flags
& FORCECLOSE
) {
1896 vnode_lock_convert(vp
);
1898 if (vp
->v_type
!= VBLK
&& vp
->v_type
!= VCHR
) {
1899 vp
->v_iocount
++; /* so that drain waits * for other iocounts */
1903 vnode_reclaim_internal(vp
, 1, 1, 0);
1904 vnode_dropiocount(vp
);
1909 vp
->v_lflag
&= ~VL_DEAD
;
1910 vp
->v_op
= spec_vnodeop_p
;
1911 vp
->v_flag
|= VDEVFLUSH
;
1919 vprint("vflush: busy vnode", vp
);
1926 /* At this point the worker queue is completed */
1927 if (busy
&& ((flags
& FORCECLOSE
)==0) && reclaimed
) {
1930 (void)vnode_iterate_reloadq(mp
);
1931 /* returned with mount lock held */
1935 /* if new vnodes were created in between retry the reclaim */
1936 if ( vnode_iterate_reloadq(mp
) != 0) {
1937 if (!(busy
&& ((flags
& FORCECLOSE
)==0)))
1940 vnode_iterate_clear(mp
);
1943 if (busy
&& ((flags
& FORCECLOSE
)==0))
1948 long num_recycledvnodes
= 0;
1950 * Disassociate the underlying file system from a vnode.
1951 * The vnode lock is held on entry.
1954 vclean(vnode_t vp
, int flags
)
1956 vfs_context_t ctx
= vfs_context_current();
1959 int already_terminating
;
1966 * Check to see if the vnode is in use.
1967 * If so we have to reference it before we clean it out
1968 * so that its count cannot fall to zero and generate a
1969 * race against ourselves to recycle it.
1971 active
= vp
->v_usecount
;
1974 * just in case we missed sending a needed
1975 * VNOP_INACTIVE, we'll do it now
1977 need_inactive
= (vp
->v_lflag
& VL_NEEDINACTIVE
);
1979 vp
->v_lflag
&= ~VL_NEEDINACTIVE
;
1982 * Prevent the vnode from being recycled or
1983 * brought into use while we clean it out.
1985 already_terminating
= (vp
->v_lflag
& VL_TERMINATE
);
1987 vp
->v_lflag
|= VL_TERMINATE
;
1990 * remove the vnode from any mount list
1993 insmntque(vp
, (struct mount
*)0);
1996 is_namedstream
= vnode_isnamedstream(vp
);
2001 OSAddAtomicLong(1, &num_recycledvnodes
);
2003 if (flags
& DOCLOSE
)
2004 clflags
|= IO_NDELAY
;
2005 if (flags
& REVOKEALL
)
2006 clflags
|= IO_REVOKE
;
2008 if (active
&& (flags
& DOCLOSE
))
2009 VNOP_CLOSE(vp
, clflags
, ctx
);
2012 * Clean out any buffers associated with the vnode.
2014 if (flags
& DOCLOSE
) {
2016 if (vp
->v_tag
== VT_NFS
)
2017 nfs_vinvalbuf(vp
, V_SAVE
, ctx
, 0);
2021 VNOP_FSYNC(vp
, MNT_WAIT
, ctx
);
2022 buf_invalidateblks(vp
, BUF_WRITE_DATA
, 0, 0);
2024 if (UBCINFOEXISTS(vp
))
2026 * Clean the pages in VM.
2028 (void)ubc_sync_range(vp
, (off_t
)0, ubc_getsize(vp
), UBC_PUSHALL
);
2030 if (active
|| need_inactive
)
2031 VNOP_INACTIVE(vp
, ctx
);
2034 if ((is_namedstream
!= 0) && (vp
->v_parent
!= NULLVP
)) {
2035 vnode_t pvp
= vp
->v_parent
;
2037 /* Delete the shadow stream file before we reclaim its vnode */
2038 if (vnode_isshadow(vp
)) {
2039 vnode_relenamedstream(pvp
, vp
, ctx
);
2043 * No more streams associated with the parent. We
2044 * have a ref on it, so its identity is stable.
2045 * If the parent is on an opaque volume, then we need to know
2046 * whether it has associated named streams.
2048 if (vfs_authopaque(pvp
->v_mount
)) {
2049 vnode_lock_spin(pvp
);
2050 pvp
->v_lflag
&= ~VL_HASSTREAMS
;
2057 * Destroy ubc named reference
2058 * cluster_release is done on this path
2059 * along with dropping the reference on the ucred
2061 ubc_destroy_named(vp
);
2064 * Reclaim the vnode.
2066 if (VNOP_RECLAIM(vp
, ctx
))
2067 panic("vclean: cannot reclaim");
2069 // make sure the name & parent ptrs get cleaned out!
2070 vnode_update_identity(vp
, NULLVP
, NULL
, 0, 0, VNODE_UPDATE_PARENT
| VNODE_UPDATE_NAME
| VNODE_UPDATE_PURGE
);
2074 vp
->v_mount
= dead_mountp
;
2075 vp
->v_op
= dead_vnodeop_p
;
2079 vp
->v_lflag
|= VL_DEAD
;
2081 if (already_terminating
== 0) {
2082 vp
->v_lflag
&= ~VL_TERMINATE
;
2084 * Done with purge, notify sleepers of the grim news.
2086 if (vp
->v_lflag
& VL_TERMWANT
) {
2087 vp
->v_lflag
&= ~VL_TERMWANT
;
2088 wakeup(&vp
->v_lflag
);
2094 * Eliminate all activity associated with the requested vnode
2095 * and with all vnodes aliased to the requested vnode.
2099 vn_revoke(vnode_t vp
, int flags
, __unused vfs_context_t a_context
)
2101 vn_revoke(vnode_t vp
, __unused
int flags
, __unused vfs_context_t a_context
)
2108 if ((flags
& REVOKEALL
) == 0)
2109 panic("vnop_revoke");
2112 if (vnode_isaliased(vp
)) {
2114 * If a vgone (or vclean) is already in progress,
2115 * return an immediate error
2117 if (vp
->v_lflag
& VL_TERMINATE
)
2121 * Ensure that vp will not be vgone'd while we
2122 * are eliminating its aliases.
2125 while ((vp
->v_specflags
& SI_ALIASED
)) {
2126 for (vq
= *vp
->v_hashchain
; vq
; vq
= vq
->v_specnext
) {
2127 if (vq
->v_rdev
!= vp
->v_rdev
||
2128 vq
->v_type
!= vp
->v_type
|| vp
== vq
)
2132 if (vnode_getwithvid(vq
,vid
)){
2136 vnode_reclaim_internal(vq
, 0, 1, 0);
2144 vnode_reclaim_internal(vp
, 0, 0, REVOKEALL
);
2150 * Recycle an unused vnode to the front of the free list.
2151 * Release the passed interlock if the vnode will be recycled.
2154 vnode_recycle(struct vnode
*vp
)
2156 vnode_lock_spin(vp
);
2158 if (vp
->v_iocount
|| vp
->v_usecount
) {
2159 vp
->v_lflag
|= VL_MARKTERM
;
2163 vnode_lock_convert(vp
);
2164 vnode_reclaim_internal(vp
, 1, 0, 0);
2172 vnode_reload(vnode_t vp
)
2174 vnode_lock_spin(vp
);
2176 if ((vp
->v_iocount
> 1) || vp
->v_usecount
) {
2180 if (vp
->v_iocount
<= 0)
2181 panic("vnode_reload with no iocount %d", vp
->v_iocount
);
2183 /* mark for release when iocount is dopped */
2184 vp
->v_lflag
|= VL_MARKTERM
;
2192 vgone(vnode_t vp
, int flags
)
2198 * Clean out the filesystem specific data.
2199 * vclean also takes care of removing the
2200 * vnode from any mount list it might be on
2202 vclean(vp
, flags
| DOCLOSE
);
2205 * If special device, remove it from special device alias list
2208 if ((vp
->v_type
== VBLK
|| vp
->v_type
== VCHR
) && vp
->v_specinfo
!= 0) {
2210 if (*vp
->v_hashchain
== vp
) {
2211 *vp
->v_hashchain
= vp
->v_specnext
;
2213 for (vq
= *vp
->v_hashchain
; vq
; vq
= vq
->v_specnext
) {
2214 if (vq
->v_specnext
!= vp
)
2216 vq
->v_specnext
= vp
->v_specnext
;
2220 panic("missing bdev");
2222 if (vp
->v_specflags
& SI_ALIASED
) {
2224 for (vq
= *vp
->v_hashchain
; vq
; vq
= vq
->v_specnext
) {
2225 if (vq
->v_rdev
!= vp
->v_rdev
||
2226 vq
->v_type
!= vp
->v_type
)
2233 panic("missing alias");
2235 vx
->v_specflags
&= ~SI_ALIASED
;
2236 vp
->v_specflags
&= ~SI_ALIASED
;
2240 struct specinfo
*tmp
= vp
->v_specinfo
;
2241 vp
->v_specinfo
= NULL
;
2242 FREE_ZONE((void *)tmp
, sizeof(struct specinfo
), M_SPECINFO
);
2248 * Lookup a vnode by device number.
2251 check_mountedon(dev_t dev
, enum vtype type
, int *errorp
)
2259 for (vp
= speclisth
[SPECHASH(dev
)]; vp
; vp
= vp
->v_specnext
) {
2260 if (dev
!= vp
->v_rdev
|| type
!= vp
->v_type
)
2264 if (vnode_getwithvid(vp
,vid
))
2266 vnode_lock_spin(vp
);
2267 if ((vp
->v_usecount
> 0) || (vp
->v_iocount
> 1)) {
2269 if ((*errorp
= vfs_mountedon(vp
)) != 0)
2281 * Calculate the total number of references to a special device.
2291 if (!vnode_isaliased(vp
))
2292 return (vp
->v_usecount
- vp
->v_kusecount
);
2297 * Grab first vnode and its vid.
2299 vq
= *vp
->v_hashchain
;
2300 vid
= vq
? vq
->v_id
: 0;
2306 * Attempt to get the vnode outside the SPECHASH lock.
2308 if (vnode_getwithvid(vq
, vid
)) {
2313 if (vq
->v_rdev
== vp
->v_rdev
&& vq
->v_type
== vp
->v_type
) {
2314 if ((vq
->v_usecount
== 0) && (vq
->v_iocount
== 1) && vq
!= vp
) {
2316 * Alias, but not in use, so flush it out.
2318 vnode_reclaim_internal(vq
, 1, 1, 0);
2319 vnode_put_locked(vq
);
2323 count
+= (vq
->v_usecount
- vq
->v_kusecount
);
2329 * must do this with the reference still held on 'vq'
2330 * so that it can't be destroyed while we're poking
2331 * through v_specnext
2333 vnext
= vq
->v_specnext
;
2334 vid
= vnext
? vnext
->v_id
: 0;
2346 int prtactive
= 0; /* 1 => print out reclaim of active vnodes */
2349 * Print out a description of a vnode.
2351 static const char *typename
[] =
2352 { "VNON", "VREG", "VDIR", "VBLK", "VCHR", "VLNK", "VSOCK", "VFIFO", "VBAD" };
2355 vprint(const char *label
, struct vnode
*vp
)
2360 printf("%s: ", label
);
2361 printf("type %s, usecount %d, writecount %d",
2362 typename
[vp
->v_type
], vp
->v_usecount
, vp
->v_writecount
);
2364 if (vp
->v_flag
& VROOT
)
2365 strlcat(sbuf
, "|VROOT", sizeof(sbuf
));
2366 if (vp
->v_flag
& VTEXT
)
2367 strlcat(sbuf
, "|VTEXT", sizeof(sbuf
));
2368 if (vp
->v_flag
& VSYSTEM
)
2369 strlcat(sbuf
, "|VSYSTEM", sizeof(sbuf
));
2370 if (vp
->v_flag
& VNOFLUSH
)
2371 strlcat(sbuf
, "|VNOFLUSH", sizeof(sbuf
));
2372 if (vp
->v_flag
& VBWAIT
)
2373 strlcat(sbuf
, "|VBWAIT", sizeof(sbuf
));
2374 if (vnode_isaliased(vp
))
2375 strlcat(sbuf
, "|VALIASED", sizeof(sbuf
));
2376 if (sbuf
[0] != '\0')
2377 printf(" flags (%s)", &sbuf
[1]);
2382 vn_getpath(struct vnode
*vp
, char *pathbuf
, int *len
)
2384 return build_path(vp
, pathbuf
, *len
, len
, BUILDPATH_NO_FS_ENTER
, vfs_context_current());
2388 vn_getpath_fsenter(struct vnode
*vp
, char *pathbuf
, int *len
)
2390 return build_path(vp
, pathbuf
, *len
, len
, 0, vfs_context_current());
2394 vn_getcdhash(struct vnode
*vp
, off_t offset
, unsigned char *cdhash
)
2396 return ubc_cs_getcdhash(vp
, offset
, cdhash
);
2400 static char *extension_table
=NULL
;
2402 static int max_ext_width
;
2405 extension_cmp(const void *a
, const void *b
)
2407 return (strlen((const char *)a
) - strlen((const char *)b
));
2412 // This is the api LaunchServices uses to inform the kernel
2413 // the list of package extensions to ignore.
2415 // Internally we keep the list sorted by the length of the
2416 // the extension (from longest to shortest). We sort the
2417 // list of extensions so that we can speed up our searches
2418 // when comparing file names -- we only compare extensions
2419 // that could possibly fit into the file name, not all of
2420 // them (i.e. a short 8 character name can't have an 8
2421 // character extension).
2423 extern lck_mtx_t
*pkg_extensions_lck
;
2425 __private_extern__
int
2426 set_package_extensions_table(user_addr_t data
, int nentries
, int maxwidth
)
2428 char *new_exts
, *old_exts
;
2431 if (nentries
<= 0 || nentries
> 1024 || maxwidth
<= 0 || maxwidth
> 255) {
2436 // allocate one byte extra so we can guarantee null termination
2437 MALLOC(new_exts
, char *, (nentries
* maxwidth
) + 1, M_TEMP
, M_WAITOK
);
2438 if (new_exts
== NULL
) {
2442 error
= copyin(data
, new_exts
, nentries
* maxwidth
);
2444 FREE(new_exts
, M_TEMP
);
2448 new_exts
[(nentries
* maxwidth
)] = '\0'; // guarantee null termination of the block
2450 qsort(new_exts
, nentries
, maxwidth
, extension_cmp
);
2452 lck_mtx_lock(pkg_extensions_lck
);
2454 old_exts
= extension_table
;
2455 extension_table
= new_exts
;
2457 max_ext_width
= maxwidth
;
2459 lck_mtx_unlock(pkg_extensions_lck
);
2462 FREE(old_exts
, M_TEMP
);
2469 __private_extern__
int
2470 is_package_name(const char *name
, int len
)
2473 const char *ptr
, *name_ext
;
2480 for(ptr
=name
; *ptr
!= '\0'; ptr
++) {
2486 // if there is no "." extension, it can't match
2487 if (name_ext
== NULL
) {
2491 // advance over the "."
2494 lck_mtx_lock(pkg_extensions_lck
);
2496 // now iterate over all the extensions to see if any match
2497 ptr
= &extension_table
[0];
2498 for(i
=0; i
< nexts
; i
++, ptr
+=max_ext_width
) {
2499 extlen
= strlen(ptr
);
2500 if (strncasecmp(name_ext
, ptr
, extlen
) == 0 && name_ext
[extlen
] == '\0') {
2502 lck_mtx_unlock(pkg_extensions_lck
);
2507 lck_mtx_unlock(pkg_extensions_lck
);
2509 // if we get here, no extension matched
2514 vn_path_package_check(__unused vnode_t vp
, char *path
, int pathlen
, int *component
)
2525 while(end
< path
+ pathlen
&& *end
!= '\0') {
2526 while(end
< path
+ pathlen
&& *end
== '/' && *end
!= '\0') {
2532 while(end
< path
+ pathlen
&& *end
!= '/' && *end
!= '\0') {
2536 if (end
> path
+ pathlen
) {
2537 // hmm, string wasn't null terminated
2542 if (is_package_name(ptr
, end
- ptr
)) {
2555 * Determine if a name is inappropriate for a searchfs query.
2556 * This list consists of /System currently.
2559 int vn_searchfs_inappropriate_name(const char *name
, int len
) {
2560 const char *bad_names
[] = { "System" };
2561 int bad_len
[] = { 6 };
2564 for(i
=0; i
< (int) (sizeof(bad_names
) / sizeof(bad_names
[0])); i
++) {
2565 if (len
== bad_len
[i
] && strncmp(name
, bad_names
[i
], strlen(bad_names
[i
]) + 1) == 0) {
2570 // if we get here, no name matched
2575 * Top level filesystem related information gathering.
2577 extern unsigned int vfs_nummntops
;
2580 vfs_sysctl(int *name
, u_int namelen
, user_addr_t oldp
, size_t *oldlenp
,
2581 user_addr_t newp
, size_t newlen
, proc_t p
)
2583 struct vfstable
*vfsp
;
2587 struct vfsconf vfsc
;
2589 /* All non VFS_GENERIC and in VFS_GENERIC,
2590 * VFS_MAXTYPENUM, VFS_CONF, VFS_SET_PACKAGE_EXTS
2591 * needs to have root priv to have modifiers.
2592 * For rest the userland_sysctl(CTLFLAG_ANYBODY) would cover.
2594 if ((newp
!= USER_ADDR_NULL
) && ((name
[0] != VFS_GENERIC
) ||
2595 ((name
[1] == VFS_MAXTYPENUM
) ||
2596 (name
[1] == VFS_CONF
) ||
2597 (name
[1] == VFS_SET_PACKAGE_EXTS
)))
2598 && (error
= suser(kauth_cred_get(), &p
->p_acflag
))) {
2602 * The VFS_NUMMNTOPS shouldn't be at name[0] since
2603 * is a VFS generic variable. So now we must check
2604 * namelen so we don't end up covering any UFS
2605 * variables (sinc UFS vfc_typenum is 1).
2607 * It should have been:
2608 * name[0]: VFS_GENERIC
2609 * name[1]: VFS_NUMMNTOPS
2611 if (namelen
== 1 && name
[0] == VFS_NUMMNTOPS
) {
2612 return (sysctl_rdint(oldp
, oldlenp
, newp
, vfs_nummntops
));
2615 /* all sysctl names at this level are at least name and field */
2617 return (EISDIR
); /* overloaded */
2618 if (name
[0] != VFS_GENERIC
) {
2621 for (vfsp
= vfsconf
; vfsp
; vfsp
= vfsp
->vfc_next
)
2622 if (vfsp
->vfc_typenum
== name
[0]) {
2623 vfsp
->vfc_refcount
++;
2626 mount_list_unlock();
2631 /* XXX current context proxy for proc p? */
2632 error
= ((*vfsp
->vfc_vfsops
->vfs_sysctl
)(&name
[1], namelen
- 1,
2633 oldp
, oldlenp
, newp
, newlen
,
2634 vfs_context_current()));
2637 vfsp
->vfc_refcount
--;
2638 mount_list_unlock();
2642 case VFS_MAXTYPENUM
:
2643 return (sysctl_rdint(oldp
, oldlenp
, newp
, maxvfsconf
));
2646 return (ENOTDIR
); /* overloaded */
2649 for (vfsp
= vfsconf
; vfsp
; vfsp
= vfsp
->vfc_next
)
2650 if (vfsp
->vfc_typenum
== name
[2])
2654 mount_list_unlock();
2658 vfsc
.vfc_reserved1
= 0;
2659 bcopy(vfsp
->vfc_name
, vfsc
.vfc_name
, sizeof(vfsc
.vfc_name
));
2660 vfsc
.vfc_typenum
= vfsp
->vfc_typenum
;
2661 vfsc
.vfc_refcount
= vfsp
->vfc_refcount
;
2662 vfsc
.vfc_flags
= vfsp
->vfc_flags
;
2663 vfsc
.vfc_reserved2
= 0;
2664 vfsc
.vfc_reserved3
= 0;
2666 mount_list_unlock();
2667 return (sysctl_rdstruct(oldp
, oldlenp
, newp
, &vfsc
,
2668 sizeof(struct vfsconf
)));
2670 case VFS_SET_PACKAGE_EXTS
:
2671 return set_package_extensions_table((user_addr_t
)((unsigned)name
[1]), name
[2], name
[3]);
2674 * We need to get back into the general MIB, so we need to re-prepend
2675 * CTL_VFS to our name and try userland_sysctl().
2677 usernamelen
= namelen
+ 1;
2678 MALLOC(username
, int *, usernamelen
* sizeof(*username
),
2680 bcopy(name
, username
+ 1, namelen
* sizeof(*name
));
2681 username
[0] = CTL_VFS
;
2682 error
= userland_sysctl(p
, username
, usernamelen
, oldp
,
2683 oldlenp
, newp
, newlen
, oldlenp
);
2684 FREE(username
, M_TEMP
);
2689 * Dump vnode list (via sysctl) - defunct
2690 * use "pstat" instead
2695 (__unused
struct sysctl_oid
*oidp
, __unused
void *arg1
, __unused
int arg2
, __unused
struct sysctl_req
*req
)
2700 SYSCTL_PROC(_kern
, KERN_VNODE
, vnode
,
2701 CTLTYPE_STRUCT
| CTLFLAG_RD
| CTLFLAG_MASKED
,
2702 0, 0, sysctl_vnode
, "S,", "");
2706 * Check to see if a filesystem is mounted on a block device.
2709 vfs_mountedon(struct vnode
*vp
)
2715 if (vp
->v_specflags
& SI_MOUNTEDON
) {
2719 if (vp
->v_specflags
& SI_ALIASED
) {
2720 for (vq
= *vp
->v_hashchain
; vq
; vq
= vq
->v_specnext
) {
2721 if (vq
->v_rdev
!= vp
->v_rdev
||
2722 vq
->v_type
!= vp
->v_type
)
2724 if (vq
->v_specflags
& SI_MOUNTEDON
) {
2736 * Unmount all filesystems. The list is traversed in reverse order
2737 * of mounting to avoid dependencies.
2739 __private_extern__
void
2740 vfs_unmountall(void)
2746 * Since this only runs when rebooting, it is not interlocked.
2749 while(!TAILQ_EMPTY(&mountlist
)) {
2750 mp
= TAILQ_LAST(&mountlist
, mntlist
);
2751 mount_list_unlock();
2752 error
= dounmount(mp
, MNT_FORCE
, 0, vfs_context_current());
2753 if ((error
!= 0) && (error
!= EBUSY
)) {
2754 printf("unmount of %s failed (", mp
->mnt_vfsstat
.f_mntonname
);
2755 printf("%d)\n", error
);
2757 TAILQ_REMOVE(&mountlist
, mp
, mnt_list
);
2759 } else if (error
== EBUSY
) {
2760 /* If EBUSY is returned, the unmount was already in progress */
2761 printf("unmount of %p failed (", mp
);
2766 mount_list_unlock();
2771 * This routine is called from vnode_pager_deallocate out of the VM
2772 * The path to vnode_pager_deallocate can only be initiated by ubc_destroy_named
2773 * on a vnode that has a UBCINFO
2775 __private_extern__
void
2776 vnode_pager_vrele(vnode_t vp
)
2778 struct ubc_info
*uip
;
2780 vnode_lock_spin(vp
);
2782 vp
->v_lflag
&= ~VNAMED_UBC
;
2784 uip
= vp
->v_ubcinfo
;
2785 vp
->v_ubcinfo
= UBC_INFO_NULL
;
2789 ubc_info_deallocate(uip
);
2793 #include <sys/disk.h>
2796 vfs_init_io_attributes(vnode_t devvp
, mount_t mp
)
2799 off_t readblockcnt
= 0;
2800 off_t writeblockcnt
= 0;
2801 off_t readmaxcnt
= 0;
2802 off_t writemaxcnt
= 0;
2803 off_t readsegcnt
= 0;
2804 off_t writesegcnt
= 0;
2805 off_t readsegsize
= 0;
2806 off_t writesegsize
= 0;
2807 off_t alignment
= 0;
2808 off_t ioqueue_depth
= 0;
2812 vfs_context_t ctx
= vfs_context_current();
2816 * determine if this mount point exists on the same device as the root
2817 * partition... if so, then it comes under the hard throttle control
2820 static int rootunit
= -1;
2822 if (rootunit
== -1) {
2823 if (VNOP_IOCTL(rootvp
, DKIOCGETBSDUNIT
, (caddr_t
)&rootunit
, 0, ctx
))
2825 else if (rootvp
== devvp
)
2826 mp
->mnt_kern_flag
|= MNTK_ROOTDEV
;
2828 if (devvp
!= rootvp
&& rootunit
!= -1) {
2829 if (VNOP_IOCTL(devvp
, DKIOCGETBSDUNIT
, (caddr_t
)&thisunit
, 0, ctx
) == 0) {
2830 if (thisunit
== rootunit
)
2831 mp
->mnt_kern_flag
|= MNTK_ROOTDEV
;
2835 * force the spec device to re-cache
2836 * the underlying block size in case
2837 * the filesystem overrode the initial value
2839 set_fsblocksize(devvp
);
2842 if ((error
= VNOP_IOCTL(devvp
, DKIOCGETBLOCKSIZE
,
2843 (caddr_t
)&blksize
, 0, ctx
)))
2846 mp
->mnt_devblocksize
= blksize
;
2849 * set the maximum possible I/O size
2850 * this may get clipped to a smaller value
2851 * based on which constraints are being advertised
2852 * and if those advertised constraints result in a smaller
2853 * limit for a given I/O
2855 mp
->mnt_maxreadcnt
= MAX_UPL_SIZE
* PAGE_SIZE
;
2856 mp
->mnt_maxwritecnt
= MAX_UPL_SIZE
* PAGE_SIZE
;
2858 if (VNOP_IOCTL(devvp
, DKIOCISVIRTUAL
, (caddr_t
)&isvirtual
, 0, ctx
) == 0) {
2860 mp
->mnt_kern_flag
|= MNTK_VIRTUALDEV
;
2863 if ((error
= VNOP_IOCTL(devvp
, DKIOCGETFEATURES
,
2864 (caddr_t
)&features
, 0, ctx
)))
2867 if ((error
= VNOP_IOCTL(devvp
, DKIOCGETMAXBLOCKCOUNTREAD
,
2868 (caddr_t
)&readblockcnt
, 0, ctx
)))
2871 if ((error
= VNOP_IOCTL(devvp
, DKIOCGETMAXBLOCKCOUNTWRITE
,
2872 (caddr_t
)&writeblockcnt
, 0, ctx
)))
2875 if ((error
= VNOP_IOCTL(devvp
, DKIOCGETMAXBYTECOUNTREAD
,
2876 (caddr_t
)&readmaxcnt
, 0, ctx
)))
2879 if ((error
= VNOP_IOCTL(devvp
, DKIOCGETMAXBYTECOUNTWRITE
,
2880 (caddr_t
)&writemaxcnt
, 0, ctx
)))
2883 if ((error
= VNOP_IOCTL(devvp
, DKIOCGETMAXSEGMENTCOUNTREAD
,
2884 (caddr_t
)&readsegcnt
, 0, ctx
)))
2887 if ((error
= VNOP_IOCTL(devvp
, DKIOCGETMAXSEGMENTCOUNTWRITE
,
2888 (caddr_t
)&writesegcnt
, 0, ctx
)))
2891 if ((error
= VNOP_IOCTL(devvp
, DKIOCGETMAXSEGMENTBYTECOUNTREAD
,
2892 (caddr_t
)&readsegsize
, 0, ctx
)))
2895 if ((error
= VNOP_IOCTL(devvp
, DKIOCGETMAXSEGMENTBYTECOUNTWRITE
,
2896 (caddr_t
)&writesegsize
, 0, ctx
)))
2899 if ((error
= VNOP_IOCTL(devvp
, DKIOCGETMINSEGMENTALIGNMENTBYTECOUNT
,
2900 (caddr_t
)&alignment
, 0, ctx
)))
2903 if ((error
= VNOP_IOCTL(devvp
, DKIOCGETCOMMANDPOOLSIZE
,
2904 (caddr_t
)&ioqueue_depth
, 0, ctx
)))
2908 mp
->mnt_maxreadcnt
= (readmaxcnt
> UINT32_MAX
) ? UINT32_MAX
: readmaxcnt
;
2911 temp
= readblockcnt
* blksize
;
2912 temp
= (temp
> UINT32_MAX
) ? UINT32_MAX
: temp
;
2914 if (temp
< mp
->mnt_maxreadcnt
)
2915 mp
->mnt_maxreadcnt
= (u_int32_t
)temp
;
2919 mp
->mnt_maxwritecnt
= (writemaxcnt
> UINT32_MAX
) ? UINT32_MAX
: writemaxcnt
;
2921 if (writeblockcnt
) {
2922 temp
= writeblockcnt
* blksize
;
2923 temp
= (temp
> UINT32_MAX
) ? UINT32_MAX
: temp
;
2925 if (temp
< mp
->mnt_maxwritecnt
)
2926 mp
->mnt_maxwritecnt
= (u_int32_t
)temp
;
2930 temp
= (readsegcnt
> UINT16_MAX
) ? UINT16_MAX
: readsegcnt
;
2932 temp
= mp
->mnt_maxreadcnt
/ PAGE_SIZE
;
2934 if (temp
> UINT16_MAX
)
2937 mp
->mnt_segreadcnt
= (u_int16_t
)temp
;
2940 temp
= (writesegcnt
> UINT16_MAX
) ? UINT16_MAX
: writesegcnt
;
2942 temp
= mp
->mnt_maxwritecnt
/ PAGE_SIZE
;
2944 if (temp
> UINT16_MAX
)
2947 mp
->mnt_segwritecnt
= (u_int16_t
)temp
;
2950 temp
= (readsegsize
> UINT32_MAX
) ? UINT32_MAX
: readsegsize
;
2952 temp
= mp
->mnt_maxreadcnt
;
2953 mp
->mnt_maxsegreadsize
= (u_int32_t
)temp
;
2956 temp
= (writesegsize
> UINT32_MAX
) ? UINT32_MAX
: writesegsize
;
2958 temp
= mp
->mnt_maxwritecnt
;
2959 mp
->mnt_maxsegwritesize
= (u_int32_t
)temp
;
2962 temp
= (alignment
> PAGE_SIZE
) ? PAGE_MASK
: alignment
- 1;
2965 mp
->mnt_alignmentmask
= temp
;
2968 if (ioqueue_depth
> MNT_DEFAULT_IOQUEUE_DEPTH
)
2969 temp
= ioqueue_depth
;
2971 temp
= MNT_DEFAULT_IOQUEUE_DEPTH
;
2973 mp
->mnt_ioqueue_depth
= temp
;
2974 mp
->mnt_ioscale
= (mp
->mnt_ioqueue_depth
+ (MNT_DEFAULT_IOQUEUE_DEPTH
- 1)) / MNT_DEFAULT_IOQUEUE_DEPTH
;
2976 if (mp
->mnt_ioscale
> 1)
2977 printf("ioqueue_depth = %d, ioscale = %d\n", (int)mp
->mnt_ioqueue_depth
, (int)mp
->mnt_ioscale
);
2979 if (features
& DK_FEATURE_FORCE_UNIT_ACCESS
)
2980 mp
->mnt_ioflags
|= MNT_IOFLAGS_FUA_SUPPORTED
;
2985 static struct klist fs_klist
;
2986 lck_grp_t
*fs_klist_lck_grp
;
2987 lck_mtx_t
*fs_klist_lock
;
2990 vfs_event_init(void)
2993 klist_init(&fs_klist
);
2994 fs_klist_lck_grp
= lck_grp_alloc_init("fs_klist", NULL
);
2995 fs_klist_lock
= lck_mtx_alloc_init(fs_klist_lck_grp
, NULL
);
2999 vfs_event_signal(__unused fsid_t
*fsid
, u_int32_t event
, __unused
intptr_t data
)
3001 lck_mtx_lock(fs_klist_lock
);
3002 KNOTE(&fs_klist
, event
);
3003 lck_mtx_unlock(fs_klist_lock
);
3007 * return the number of mounted filesystems.
3010 sysctl_vfs_getvfscnt(void)
3012 return(mount_getvfscnt());
3017 mount_getvfscnt(void)
3023 mount_list_unlock();
3031 mount_fillfsids(fsid_t
*fsidlst
, int count
)
3038 TAILQ_FOREACH(mp
, &mountlist
, mnt_list
) {
3039 if (actual
<= count
) {
3040 fsidlst
[actual
] = mp
->mnt_vfsstat
.f_fsid
;
3044 mount_list_unlock();
3050 * fill in the array of fsid_t's up to a max of 'count', the actual
3051 * number filled in will be set in '*actual'. If there are more fsid_t's
3052 * than room in fsidlst then ENOMEM will be returned and '*actual' will
3053 * have the actual count.
3054 * having *actual filled out even in the error case is depended upon.
3057 sysctl_vfs_getvfslist(fsid_t
*fsidlst
, int count
, int *actual
)
3063 TAILQ_FOREACH(mp
, &mountlist
, mnt_list
) {
3065 if (*actual
<= count
)
3066 fsidlst
[(*actual
) - 1] = mp
->mnt_vfsstat
.f_fsid
;
3068 mount_list_unlock();
3069 return (*actual
<= count
? 0 : ENOMEM
);
3073 sysctl_vfs_vfslist(__unused
struct sysctl_oid
*oidp
, __unused
void *arg1
,
3074 __unused
int arg2
, struct sysctl_req
*req
)
3080 /* This is a readonly node. */
3081 if (req
->newptr
!= USER_ADDR_NULL
)
3084 /* they are querying us so just return the space required. */
3085 if (req
->oldptr
== USER_ADDR_NULL
) {
3086 req
->oldidx
= sysctl_vfs_getvfscnt() * sizeof(fsid_t
);
3091 * Retrieve an accurate count of the amount of space required to copy
3092 * out all the fsids in the system.
3094 space
= req
->oldlen
;
3095 req
->oldlen
= sysctl_vfs_getvfscnt() * sizeof(fsid_t
);
3097 /* they didn't give us enough space. */
3098 if (space
< req
->oldlen
)
3101 MALLOC(fsidlst
, fsid_t
*, req
->oldlen
, M_TEMP
, M_WAITOK
);
3102 if (fsidlst
== NULL
) {
3106 error
= sysctl_vfs_getvfslist(fsidlst
, req
->oldlen
/ sizeof(fsid_t
),
3109 * If we get back ENOMEM, then another mount has been added while we
3110 * slept in malloc above. If this is the case then try again.
3112 if (error
== ENOMEM
) {
3113 FREE(fsidlst
, M_TEMP
);
3114 req
->oldlen
= space
;
3118 error
= SYSCTL_OUT(req
, fsidlst
, actual
* sizeof(fsid_t
));
3120 FREE(fsidlst
, M_TEMP
);
3125 * Do a sysctl by fsid.
3128 sysctl_vfs_ctlbyfsid(__unused
struct sysctl_oid
*oidp
, void *arg1
, int arg2
,
3129 struct sysctl_req
*req
)
3131 union union_vfsidctl vc
;
3133 struct vfsstatfs
*sp
;
3134 int *name
, flags
, namelen
;
3135 int error
=0, gotref
=0;
3136 vfs_context_t ctx
= vfs_context_current();
3137 proc_t p
= req
->p
; /* XXX req->p != current_proc()? */
3138 boolean_t is_64_bit
;
3142 is_64_bit
= proc_is64bit(p
);
3144 error
= SYSCTL_IN(req
, &vc
, is_64_bit
? sizeof(vc
.vc64
):sizeof(vc
.vc32
));
3147 if (vc
.vc32
.vc_vers
!= VFS_CTL_VERS1
) { /* works for 32 and 64 */
3151 mp
= mount_list_lookupby_fsid(&vc
.vc32
.vc_fsid
, 0, 1); /* works for 32 and 64 */
3157 /* reset so that the fs specific code can fetch it. */
3160 * Note if this is a VFS_CTL then we pass the actual sysctl req
3161 * in for "oldp" so that the lower layer can DTRT and use the
3162 * SYSCTL_IN/OUT routines.
3164 if (mp
->mnt_op
->vfs_sysctl
!= NULL
) {
3166 if (vfs_64bitready(mp
)) {
3167 error
= mp
->mnt_op
->vfs_sysctl(name
, namelen
,
3168 CAST_USER_ADDR_T(req
),
3169 NULL
, USER_ADDR_NULL
, 0,
3177 error
= mp
->mnt_op
->vfs_sysctl(name
, namelen
,
3178 CAST_USER_ADDR_T(req
),
3179 NULL
, USER_ADDR_NULL
, 0,
3182 if (error
!= ENOTSUP
) {
3187 case VFS_CTL_UMOUNT
:
3190 req
->newptr
= vc
.vc64
.vc_ptr
;
3191 req
->newlen
= (size_t)vc
.vc64
.vc_len
;
3194 req
->newptr
= CAST_USER_ADDR_T(vc
.vc32
.vc_ptr
);
3195 req
->newlen
= vc
.vc32
.vc_len
;
3197 error
= SYSCTL_IN(req
, &flags
, sizeof(flags
));
3204 /* safedounmount consumes a ref */
3205 error
= safedounmount(mp
, flags
, ctx
);
3207 case VFS_CTL_STATFS
:
3210 req
->newptr
= vc
.vc64
.vc_ptr
;
3211 req
->newlen
= (size_t)vc
.vc64
.vc_len
;
3214 req
->newptr
= CAST_USER_ADDR_T(vc
.vc32
.vc_ptr
);
3215 req
->newlen
= vc
.vc32
.vc_len
;
3217 error
= SYSCTL_IN(req
, &flags
, sizeof(flags
));
3220 sp
= &mp
->mnt_vfsstat
;
3221 if (((flags
& MNT_NOWAIT
) == 0 || (flags
& (MNT_WAIT
| MNT_DWAIT
))) &&
3222 (error
= vfs_update_vfsstat(mp
, ctx
, VFS_USER_EVENT
)))
3225 struct user64_statfs sfs
;
3226 bzero(&sfs
, sizeof(sfs
));
3227 sfs
.f_flags
= mp
->mnt_flag
& MNT_VISFLAGMASK
;
3228 sfs
.f_type
= mp
->mnt_vtable
->vfc_typenum
;
3229 sfs
.f_bsize
= (user64_long_t
)sp
->f_bsize
;
3230 sfs
.f_iosize
= (user64_long_t
)sp
->f_iosize
;
3231 sfs
.f_blocks
= (user64_long_t
)sp
->f_blocks
;
3232 sfs
.f_bfree
= (user64_long_t
)sp
->f_bfree
;
3233 sfs
.f_bavail
= (user64_long_t
)sp
->f_bavail
;
3234 sfs
.f_files
= (user64_long_t
)sp
->f_files
;
3235 sfs
.f_ffree
= (user64_long_t
)sp
->f_ffree
;
3236 sfs
.f_fsid
= sp
->f_fsid
;
3237 sfs
.f_owner
= sp
->f_owner
;
3239 strlcpy(sfs
.f_fstypename
, sp
->f_fstypename
, MFSNAMELEN
);
3240 strlcpy(sfs
.f_mntonname
, sp
->f_mntonname
, MNAMELEN
);
3241 strlcpy(sfs
.f_mntfromname
, sp
->f_mntfromname
, MNAMELEN
);
3243 error
= SYSCTL_OUT(req
, &sfs
, sizeof(sfs
));
3246 struct user32_statfs sfs
;
3247 bzero(&sfs
, sizeof(sfs
));
3248 sfs
.f_flags
= mp
->mnt_flag
& MNT_VISFLAGMASK
;
3249 sfs
.f_type
= mp
->mnt_vtable
->vfc_typenum
;
3252 * It's possible for there to be more than 2^^31 blocks in the filesystem, so we
3253 * have to fudge the numbers here in that case. We inflate the blocksize in order
3254 * to reflect the filesystem size as best we can.
3256 if (sp
->f_blocks
> INT_MAX
) {
3260 * Work out how far we have to shift the block count down to make it fit.
3261 * Note that it's possible to have to shift so far that the resulting
3262 * blocksize would be unreportably large. At that point, we will clip
3263 * any values that don't fit.
3265 * For safety's sake, we also ensure that f_iosize is never reported as
3266 * being smaller than f_bsize.
3268 for (shift
= 0; shift
< 32; shift
++) {
3269 if ((sp
->f_blocks
>> shift
) <= INT_MAX
)
3271 if ((((long long)sp
->f_bsize
) << (shift
+ 1)) > INT_MAX
)
3274 #define __SHIFT_OR_CLIP(x, s) ((((x) >> (s)) > INT_MAX) ? INT_MAX : ((x) >> (s)))
3275 sfs
.f_blocks
= (user32_long_t
)__SHIFT_OR_CLIP(sp
->f_blocks
, shift
);
3276 sfs
.f_bfree
= (user32_long_t
)__SHIFT_OR_CLIP(sp
->f_bfree
, shift
);
3277 sfs
.f_bavail
= (user32_long_t
)__SHIFT_OR_CLIP(sp
->f_bavail
, shift
);
3278 #undef __SHIFT_OR_CLIP
3279 sfs
.f_bsize
= (user32_long_t
)(sp
->f_bsize
<< shift
);
3280 sfs
.f_iosize
= lmax(sp
->f_iosize
, sp
->f_bsize
);
3282 sfs
.f_bsize
= (user32_long_t
)sp
->f_bsize
;
3283 sfs
.f_iosize
= (user32_long_t
)sp
->f_iosize
;
3284 sfs
.f_blocks
= (user32_long_t
)sp
->f_blocks
;
3285 sfs
.f_bfree
= (user32_long_t
)sp
->f_bfree
;
3286 sfs
.f_bavail
= (user32_long_t
)sp
->f_bavail
;
3288 sfs
.f_files
= (user32_long_t
)sp
->f_files
;
3289 sfs
.f_ffree
= (user32_long_t
)sp
->f_ffree
;
3290 sfs
.f_fsid
= sp
->f_fsid
;
3291 sfs
.f_owner
= sp
->f_owner
;
3293 strlcpy(sfs
.f_fstypename
, sp
->f_fstypename
, MFSNAMELEN
);
3294 strlcpy(sfs
.f_mntonname
, sp
->f_mntonname
, MNAMELEN
);
3295 strlcpy(sfs
.f_mntfromname
, sp
->f_mntfromname
, MNAMELEN
);
3297 error
= SYSCTL_OUT(req
, &sfs
, sizeof(sfs
));
3310 static int filt_fsattach(struct knote
*kn
);
3311 static void filt_fsdetach(struct knote
*kn
);
3312 static int filt_fsevent(struct knote
*kn
, long hint
);
3313 struct filterops fs_filtops
= {
3314 .f_attach
= filt_fsattach
,
3315 .f_detach
= filt_fsdetach
,
3316 .f_event
= filt_fsevent
,
3320 filt_fsattach(struct knote
*kn
)
3323 lck_mtx_lock(fs_klist_lock
);
3324 kn
->kn_flags
|= EV_CLEAR
;
3325 KNOTE_ATTACH(&fs_klist
, kn
);
3326 lck_mtx_unlock(fs_klist_lock
);
3331 filt_fsdetach(struct knote
*kn
)
3333 lck_mtx_lock(fs_klist_lock
);
3334 KNOTE_DETACH(&fs_klist
, kn
);
3335 lck_mtx_unlock(fs_klist_lock
);
3339 filt_fsevent(struct knote
*kn
, long hint
)
3342 * Backwards compatibility:
3343 * Other filters would do nothing if kn->kn_sfflags == 0
3346 if ((kn
->kn_sfflags
== 0) || (kn
->kn_sfflags
& hint
)) {
3347 kn
->kn_fflags
|= hint
;
3350 return (kn
->kn_fflags
!= 0);
3354 sysctl_vfs_noremotehang(__unused
struct sysctl_oid
*oidp
,
3355 __unused
void *arg1
, __unused
int arg2
, struct sysctl_req
*req
)
3361 /* We need a pid. */
3362 if (req
->newptr
== USER_ADDR_NULL
)
3365 error
= SYSCTL_IN(req
, &pid
, sizeof(pid
));
3369 p
= proc_find(pid
< 0 ? -pid
: pid
);
3374 * Fetching the value is ok, but we only fetch if the old
3377 if (req
->oldptr
!= USER_ADDR_NULL
) {
3378 out
= !((p
->p_flag
& P_NOREMOTEHANG
) == 0);
3380 error
= SYSCTL_OUT(req
, &out
, sizeof(out
));
3384 /* cansignal offers us enough security. */
3385 if (p
!= req
->p
&& proc_suser(req
->p
) != 0) {
3391 OSBitAndAtomic(~((uint32_t)P_NOREMOTEHANG
), &p
->p_flag
);
3393 OSBitOrAtomic(P_NOREMOTEHANG
, &p
->p_flag
);
3399 /* the vfs.generic. branch. */
3400 SYSCTL_NODE(_vfs
, VFS_GENERIC
, generic
, CTLFLAG_RW
|CTLFLAG_LOCKED
, NULL
, "vfs generic hinge");
3401 /* retreive a list of mounted filesystem fsid_t */
3402 SYSCTL_PROC(_vfs_generic
, OID_AUTO
, vfsidlist
, CTLFLAG_RD
,
3403 NULL
, 0, sysctl_vfs_vfslist
, "S,fsid", "List of mounted filesystem ids");
3404 /* perform operations on filesystem via fsid_t */
3405 SYSCTL_NODE(_vfs_generic
, OID_AUTO
, ctlbyfsid
, CTLFLAG_RW
|CTLFLAG_LOCKED
,
3406 sysctl_vfs_ctlbyfsid
, "ctlbyfsid");
3407 SYSCTL_PROC(_vfs_generic
, OID_AUTO
, noremotehang
, CTLFLAG_RW
|CTLFLAG_ANYBODY
,
3408 NULL
, 0, sysctl_vfs_noremotehang
, "I", "noremotehang");
3411 long num_reusedvnodes
= 0;
3414 new_vnode(vnode_t
*vpp
)
3417 int retries
= 0; /* retry incase of tablefull */
3418 int force_alloc
= 0, walk_count
= 0;
3421 struct timeval current_tv
;
3423 struct unsafe_fsnode
*l_unsafefs
= 0;
3424 #endif /* __LP64__ */
3425 proc_t curproc
= current_proc();
3428 microuptime(¤t_tv
);
3434 if ( !TAILQ_EMPTY(&vnode_dead_list
)) {
3436 * Can always reuse a dead one
3438 vp
= TAILQ_FIRST(&vnode_dead_list
);
3442 * no dead vnodes available... if we're under
3443 * the limit, we'll create a new vnode
3445 if (numvnodes
< desiredvnodes
|| force_alloc
) {
3447 vnode_list_unlock();
3449 MALLOC_ZONE(vp
, struct vnode
*, sizeof(*vp
), M_VNODE
, M_WAITOK
);
3450 bzero((char *)vp
, sizeof(*vp
));
3451 VLISTNONE(vp
); /* avoid double queue removal */
3452 lck_mtx_init(&vp
->v_lock
, vnode_lck_grp
, vnode_lck_attr
);
3454 klist_init(&vp
->v_knotes
);
3456 vp
->v_id
= ts
.tv_nsec
;
3457 vp
->v_flag
= VSTANDARD
;
3460 if (mac_vnode_label_init_needed(vp
))
3461 mac_vnode_label_init(vp
);
3468 #define MAX_WALK_COUNT 1000
3470 if ( !TAILQ_EMPTY(&vnode_rage_list
) &&
3471 (ragevnodes
>= rage_limit
||
3472 (current_tv
.tv_sec
- rage_tv
.tv_sec
) >= RAGE_TIME_LIMIT
)) {
3474 TAILQ_FOREACH(vp
, &vnode_rage_list
, v_freelist
) {
3475 if ( !(vp
->v_listflag
& VLIST_RAGE
))
3476 panic("new_vnode: vp (%p) on RAGE list not marked VLIST_RAGE", vp
);
3478 // if we're a dependency-capable process, skip vnodes that can
3479 // cause recycling deadlocks. (i.e. this process is diskimages
3480 // helper and the vnode is in a disk image).
3482 if ((curproc
->p_flag
& P_DEPENDENCY_CAPABLE
) == 0 || vp
->v_mount
== NULL
|| vp
->v_mount
->mnt_dependent_process
== NULL
) {
3486 // don't iterate more than MAX_WALK_COUNT vnodes to
3487 // avoid keeping the vnode list lock held for too long.
3488 if (walk_count
++ > MAX_WALK_COUNT
) {
3496 if (vp
== NULL
&& !TAILQ_EMPTY(&vnode_free_list
)) {
3498 * Pick the first vp for possible reuse
3501 TAILQ_FOREACH(vp
, &vnode_free_list
, v_freelist
) {
3502 // if we're a dependency-capable process, skip vnodes that can
3503 // cause recycling deadlocks. (i.e. this process is diskimages
3504 // helper and the vnode is in a disk image)
3506 if ((curproc
->p_flag
& P_DEPENDENCY_CAPABLE
) == 0 || vp
->v_mount
== NULL
|| vp
->v_mount
->mnt_dependent_process
== NULL
) {
3510 // don't iterate more than MAX_WALK_COUNT vnodes to
3511 // avoid keeping the vnode list lock held for too long.
3512 if (walk_count
++ > MAX_WALK_COUNT
) {
3521 // if we don't have a vnode and the walk_count is >= MAX_WALK_COUNT
3522 // then we're trying to create a vnode on behalf of a
3523 // process like diskimages-helper that has file systems
3524 // mounted on top of itself (and thus we can't reclaim
3525 // vnodes in the file systems on top of us). if we can't
3526 // find a vnode to reclaim then we'll just have to force
3529 if (vp
== NULL
&& walk_count
>= MAX_WALK_COUNT
) {
3531 vnode_list_unlock();
3537 * we've reached the system imposed maximum number of vnodes
3538 * but there isn't a single one available
3539 * wait a bit and then retry... if we can't get a vnode
3540 * after 100 retries, than log a complaint
3542 if (++retries
<= 100) {
3543 vnode_list_unlock();
3544 delay_for_interval(1, 1000 * 1000);
3548 vnode_list_unlock();
3550 log(LOG_EMERG
, "%d desired, %d numvnodes, "
3551 "%d free, %d dead, %d rage\n",
3552 desiredvnodes
, numvnodes
, freevnodes
, deadvnodes
, ragevnodes
);
3555 * Running out of vnodes tends to make a system unusable. On an
3556 * embedded system, it's unlikely that the user can do anything
3557 * about it (or would know what to do, if they could). So panic
3558 * the system so it will automatically restart (and hopefully we
3559 * can get a panic log that tells us why we ran out).
3561 panic("vnode table is full\n");
3569 vnode_list_remove_locked(vp
);
3571 vnode_list_unlock();
3573 vnode_lock_spin(vp
);
3576 * We could wait for the vnode_lock after removing the vp from the freelist
3577 * and the vid is bumped only at the very end of reclaim. So it is possible
3578 * that we are looking at a vnode that is being terminated. If so skip it.
3580 if ((vpid
!= vp
->v_id
) || (vp
->v_usecount
!= 0) || (vp
->v_iocount
!= 0) ||
3581 VONLIST(vp
) || (vp
->v_lflag
& VL_TERMINATE
)) {
3583 * we lost the race between dropping the list lock
3584 * and picking up the vnode_lock... someone else
3585 * used this vnode and it is now in a new state
3586 * so we need to go back and try again
3591 if ( (vp
->v_lflag
& (VL_NEEDINACTIVE
| VL_MARKTERM
)) == VL_NEEDINACTIVE
) {
3593 * we did a vnode_rele_ext that asked for
3594 * us not to reenter the filesystem during
3595 * the release even though VL_NEEDINACTIVE was
3596 * set... we'll do it here by doing a
3597 * vnode_get/vnode_put
3599 * pick up an iocount so that we can call
3600 * vnode_put and drive the VNOP_INACTIVE...
3601 * vnode_put will either leave us off
3602 * the freelist if a new ref comes in,
3603 * or put us back on the end of the freelist
3604 * or recycle us if we were marked for termination...
3605 * so we'll just go grab a new candidate
3611 vnode_put_locked(vp
);
3615 OSAddAtomicLong(1, &num_reusedvnodes
);
3617 /* Checks for anyone racing us for recycle */
3618 if (vp
->v_type
!= VBAD
) {
3619 if (vp
->v_lflag
& VL_DEAD
)
3620 panic("new_vnode(%p): the vnode is VL_DEAD but not VBAD", vp
);
3621 vnode_lock_convert(vp
);
3622 (void)vnode_reclaim_internal(vp
, 1, 1, 0);
3625 panic("new_vnode(%p): vp on list", vp
);
3626 if (vp
->v_usecount
|| vp
->v_iocount
|| vp
->v_kusecount
||
3627 (vp
->v_lflag
& (VNAMED_UBC
| VNAMED_MOUNT
| VNAMED_FSHASH
)))
3628 panic("new_vnode(%p): free vnode still referenced", vp
);
3629 if ((vp
->v_mntvnodes
.tqe_prev
!= 0) && (vp
->v_mntvnodes
.tqe_next
!= 0))
3630 panic("new_vnode(%p): vnode seems to be on mount list", vp
);
3631 if ( !LIST_EMPTY(&vp
->v_nclinks
) || !LIST_EMPTY(&vp
->v_ncchildren
))
3632 panic("new_vnode(%p): vnode still hooked into the name cache", vp
);
3636 if (vp
->v_unsafefs
) {
3637 l_unsafefs
= vp
->v_unsafefs
;
3638 vp
->v_unsafefs
= (struct unsafe_fsnode
*)NULL
;
3640 #endif /* __LP64__ */
3644 * We should never see VL_LABELWAIT or VL_LABEL here.
3645 * as those operations hold a reference.
3647 assert ((vp
->v_lflag
& VL_LABELWAIT
) != VL_LABELWAIT
);
3648 assert ((vp
->v_lflag
& VL_LABEL
) != VL_LABEL
);
3649 if (vp
->v_lflag
& VL_LABELED
) {
3650 vnode_lock_convert(vp
);
3651 mac_vnode_label_recycle(vp
);
3652 } else if (mac_vnode_label_init_needed(vp
)) {
3653 vnode_lock_convert(vp
);
3654 mac_vnode_label_init(vp
);
3661 vp
->v_writecount
= 0;
3662 vp
->v_references
= 0;
3663 vp
->v_iterblkflags
= 0;
3664 vp
->v_flag
= VSTANDARD
;
3665 /* vbad vnodes can point to dead_mountp */
3667 vp
->v_defer_reclaimlist
= (vnode_t
)0;
3673 lck_mtx_destroy(&l_unsafefs
->fsnodelock
, vnode_lck_grp
);
3674 FREE_ZONE((void *)l_unsafefs
, sizeof(struct unsafe_fsnode
), M_UNSAFEFS
);
3676 #endif /* __LP64__ */
3685 vnode_lock(vnode_t vp
)
3687 lck_mtx_lock(&vp
->v_lock
);
3691 vnode_lock_spin(vnode_t vp
)
3693 lck_mtx_lock_spin(&vp
->v_lock
);
3697 vnode_unlock(vnode_t vp
)
3699 lck_mtx_unlock(&vp
->v_lock
);
3705 vnode_get(struct vnode
*vp
)
3709 vnode_lock_spin(vp
);
3710 retval
= vnode_get_locked(vp
);
3717 vnode_get_locked(struct vnode
*vp
)
3720 lck_mtx_assert(&vp
->v_lock
, LCK_MTX_ASSERT_OWNED
);
3722 if ((vp
->v_iocount
== 0) && (vp
->v_lflag
& (VL_TERMINATE
| VL_DEAD
))) {
3733 vnode_getwithvid(vnode_t vp
, uint32_t vid
)
3735 return(vget_internal(vp
, vid
, ( VNODE_NODEAD
| VNODE_WITHID
)));
3739 vnode_getwithref(vnode_t vp
)
3741 return(vget_internal(vp
, 0, 0));
3745 __private_extern__
int
3746 vnode_getalways(vnode_t vp
)
3748 return(vget_internal(vp
, 0, VNODE_ALWAYS
));
3752 vnode_put(vnode_t vp
)
3756 vnode_lock_spin(vp
);
3757 retval
= vnode_put_locked(vp
);
3764 vnode_put_locked(vnode_t vp
)
3766 vfs_context_t ctx
= vfs_context_current(); /* hoist outside loop */
3769 lck_mtx_assert(&vp
->v_lock
, LCK_MTX_ASSERT_OWNED
);
3772 if (vp
->v_iocount
< 1)
3773 panic("vnode_put(%p): iocount < 1", vp
);
3775 if ((vp
->v_usecount
> 0) || (vp
->v_iocount
> 1)) {
3776 vnode_dropiocount(vp
);
3779 if ((vp
->v_lflag
& (VL_MARKTERM
| VL_TERMINATE
| VL_DEAD
| VL_NEEDINACTIVE
)) == VL_NEEDINACTIVE
) {
3781 vp
->v_lflag
&= ~VL_NEEDINACTIVE
;
3784 VNOP_INACTIVE(vp
, ctx
);
3786 vnode_lock_spin(vp
);
3788 * because we had to drop the vnode lock before calling
3789 * VNOP_INACTIVE, the state of this vnode may have changed...
3790 * we may pick up both VL_MARTERM and either
3791 * an iocount or a usecount while in the VNOP_INACTIVE call
3792 * we don't want to call vnode_reclaim_internal on a vnode
3793 * that has active references on it... so loop back around
3794 * and reevaluate the state
3798 vp
->v_lflag
&= ~VL_NEEDINACTIVE
;
3800 if ((vp
->v_lflag
& (VL_MARKTERM
| VL_TERMINATE
| VL_DEAD
)) == VL_MARKTERM
) {
3801 vnode_lock_convert(vp
);
3802 vnode_reclaim_internal(vp
, 1, 1, 0);
3804 vnode_dropiocount(vp
);
3810 /* is vnode_t in use by others? */
3812 vnode_isinuse(vnode_t vp
, int refcnt
)
3814 return(vnode_isinuse_locked(vp
, refcnt
, 0));
3819 vnode_isinuse_locked(vnode_t vp
, int refcnt
, int locked
)
3824 vnode_lock_spin(vp
);
3825 if ((vp
->v_type
!= VREG
) && ((vp
->v_usecount
- vp
->v_kusecount
) > refcnt
)) {
3829 if (vp
->v_type
== VREG
) {
3830 retval
= ubc_isinuse_locked(vp
, refcnt
, 1);
3840 /* resume vnode_t */
3842 vnode_resume(vnode_t vp
)
3844 if ((vp
->v_lflag
& VL_SUSPENDED
) && vp
->v_owner
== current_thread()) {
3846 vnode_lock_spin(vp
);
3847 vp
->v_lflag
&= ~VL_SUSPENDED
;
3851 wakeup(&vp
->v_iocount
);
3857 * Please do not use on more than one vnode at a time as it may
3859 * xxx should we explicity prevent this from happening?
3863 vnode_suspend(vnode_t vp
)
3865 if (vp
->v_lflag
& VL_SUSPENDED
) {
3869 vnode_lock_spin(vp
);
3872 * xxx is this sufficient to check if a vnode_drain is
3876 if (vp
->v_owner
== NULL
) {
3877 vp
->v_lflag
|= VL_SUSPENDED
;
3878 vp
->v_owner
= current_thread();
3888 vnode_drain(vnode_t vp
)
3891 if (vp
->v_lflag
& VL_DRAIN
) {
3892 panic("vnode_drain: recursuve drain");
3895 vp
->v_lflag
|= VL_DRAIN
;
3896 vp
->v_owner
= current_thread();
3898 while (vp
->v_iocount
> 1)
3899 msleep(&vp
->v_iocount
, &vp
->v_lock
, PVFS
, "vnode_drain", NULL
);
3905 * if the number of recent references via vnode_getwithvid or vnode_getwithref
3906 * exceeds this threshhold, than 'UN-AGE' the vnode by removing it from
3907 * the LRU list if it's currently on it... once the iocount and usecount both drop
3908 * to 0, it will get put back on the end of the list, effectively making it younger
3909 * this allows us to keep actively referenced vnodes in the list without having
3910 * to constantly remove and add to the list each time a vnode w/o a usecount is
3911 * referenced which costs us taking and dropping a global lock twice.
3913 #define UNAGE_THRESHHOLD 25
3916 vnode_getiocount(vnode_t vp
, unsigned int vid
, int vflags
)
3918 int nodead
= vflags
& VNODE_NODEAD
;
3919 int nosusp
= vflags
& VNODE_NOSUSPEND
;
3920 int always
= vflags
& VNODE_ALWAYS
;
3924 * if it is a dead vnode with deadfs
3926 if (nodead
&& (vp
->v_lflag
& VL_DEAD
) && ((vp
->v_type
== VBAD
) || (vp
->v_data
== 0))) {
3930 * will return VL_DEAD ones
3932 if ((vp
->v_lflag
& (VL_SUSPENDED
| VL_DRAIN
| VL_TERMINATE
)) == 0 ) {
3936 * if suspended vnodes are to be failed
3938 if (nosusp
&& (vp
->v_lflag
& VL_SUSPENDED
)) {
3942 * if you are the owner of drain/suspend/termination , can acquire iocount
3943 * check for VL_TERMINATE; it does not set owner
3945 if ((vp
->v_lflag
& (VL_DRAIN
| VL_SUSPENDED
| VL_TERMINATE
)) &&
3946 (vp
->v_owner
== current_thread())) {
3952 vnode_lock_convert(vp
);
3954 if (vp
->v_lflag
& VL_TERMINATE
) {
3955 vp
->v_lflag
|= VL_TERMWANT
;
3957 msleep(&vp
->v_lflag
, &vp
->v_lock
, PVFS
, "vnode getiocount", NULL
);
3959 msleep(&vp
->v_iocount
, &vp
->v_lock
, PVFS
, "vnode_getiocount", NULL
);
3961 if (vid
!= vp
->v_id
) {
3964 if (++vp
->v_references
>= UNAGE_THRESHHOLD
) {
3965 vp
->v_references
= 0;
3966 vnode_list_remove(vp
);
3976 vnode_dropiocount (vnode_t vp
)
3978 if (vp
->v_iocount
< 1)
3979 panic("vnode_dropiocount(%p): v_iocount < 1", vp
);
3985 if ((vp
->v_lflag
& (VL_DRAIN
| VL_SUSPENDED
)) && (vp
->v_iocount
<= 1))
3986 wakeup(&vp
->v_iocount
);
3991 vnode_reclaim(struct vnode
* vp
)
3993 vnode_reclaim_internal(vp
, 0, 0, 0);
3998 vnode_reclaim_internal(struct vnode
* vp
, int locked
, int reuse
, int flags
)
4005 if (vp
->v_lflag
& VL_TERMINATE
) {
4006 panic("vnode reclaim in progress");
4008 vp
->v_lflag
|= VL_TERMINATE
;
4010 vn_clearunionwait(vp
, 1);
4014 isfifo
= (vp
->v_type
== VFIFO
);
4016 if (vp
->v_type
!= VBAD
)
4017 vgone(vp
, flags
); /* clean and reclaim the vnode */
4020 * give the vnode a new identity so that vnode_getwithvid will fail
4021 * on any stale cache accesses...
4022 * grab the list_lock so that if we're in "new_vnode"
4023 * behind the list_lock trying to steal this vnode, the v_id is stable...
4024 * once new_vnode drops the list_lock, it will block trying to take
4025 * the vnode lock until we release it... at that point it will evaluate
4026 * whether the v_vid has changed
4027 * also need to make sure that the vnode isn't on a list where "new_vnode"
4028 * can find it after the v_id has been bumped until we are completely done
4029 * with the vnode (i.e. putting it back on a list has to be the very last
4030 * thing we do to this vnode... many of the callers of vnode_reclaim_internal
4031 * are holding an io_count on the vnode... they need to drop the io_count
4032 * BEFORE doing a vnode_list_add or make sure to hold the vnode lock until
4033 * they are completely done with the vnode
4037 vnode_list_remove_locked(vp
);
4040 vnode_list_unlock();
4043 struct fifoinfo
* fip
;
4045 fip
= vp
->v_fifoinfo
;
4046 vp
->v_fifoinfo
= NULL
;
4052 panic("vnode_reclaim_internal: cleaned vnode isn't");
4053 if (vp
->v_numoutput
)
4054 panic("vnode_reclaim_internal: clean vnode has pending I/O's");
4055 if (UBCINFOEXISTS(vp
))
4056 panic("vnode_reclaim_internal: ubcinfo not cleaned");
4058 panic("vnode_reclaim_internal: vparent not removed");
4060 panic("vnode_reclaim_internal: vname not removed");
4062 vp
->v_socket
= NULL
;
4064 vp
->v_lflag
&= ~VL_TERMINATE
;
4065 vp
->v_lflag
&= ~VL_DRAIN
;
4068 KNOTE(&vp
->v_knotes
, NOTE_REVOKE
);
4070 /* Make sure that when we reuse the vnode, no knotes left over */
4071 klist_init(&vp
->v_knotes
);
4073 if (vp
->v_lflag
& VL_TERMWANT
) {
4074 vp
->v_lflag
&= ~VL_TERMWANT
;
4075 wakeup(&vp
->v_lflag
);
4079 * make sure we get on the
4080 * dead list if appropriate
4089 * The following api creates a vnode and associates all the parameter specified in vnode_fsparam
4090 * structure and returns a vnode handle with a reference. device aliasing is handled here so checkalias
4091 * is obsoleted by this.
4092 * vnode_create(int flavor, size_t size, void * param, vnode_t *vp)
4095 vnode_create(uint32_t flavor
, uint32_t size
, void *data
, vnode_t
*vpp
)
4103 struct componentname
*cnp
;
4104 struct vnode_fsparam
*param
= (struct vnode_fsparam
*)data
;
4106 if (flavor
== VNCREATE_FLAVOR
&& (size
== VCREATESIZE
) && param
) {
4107 if ( (error
= new_vnode(&vp
)) ) {
4110 dvp
= param
->vnfs_dvp
;
4111 cnp
= param
->vnfs_cnp
;
4113 vp
->v_op
= param
->vnfs_vops
;
4114 vp
->v_type
= param
->vnfs_vtype
;
4115 vp
->v_data
= param
->vnfs_fsnode
;
4117 if (param
->vnfs_markroot
)
4118 vp
->v_flag
|= VROOT
;
4119 if (param
->vnfs_marksystem
)
4120 vp
->v_flag
|= VSYSTEM
;
4121 if (vp
->v_type
== VREG
) {
4122 error
= ubc_info_init_withsize(vp
, param
->vnfs_filesize
);
4128 vp
->v_op
= dead_vnodeop_p
;
4132 vp
->v_lflag
|= VL_DEAD
;
4141 if (vp
->v_type
== VCHR
|| vp
->v_type
== VBLK
) {
4143 vp
->v_tag
= VT_DEVFS
; /* callers will reset if needed (bdevvp) */
4145 if ( (nvp
= checkalias(vp
, param
->vnfs_rdev
)) ) {
4147 * if checkalias returns a vnode, it will be locked
4149 * first get rid of the unneeded vnode we acquired
4152 vp
->v_op
= spec_vnodeop_p
;
4154 vp
->v_lflag
= VL_DEAD
;
4160 * switch to aliased vnode and finish
4166 vp
->v_op
= param
->vnfs_vops
;
4167 vp
->v_type
= param
->vnfs_vtype
;
4168 vp
->v_data
= param
->vnfs_fsnode
;
4171 insmntque(vp
, param
->vnfs_mp
);
4177 if (vp
->v_type
== VFIFO
) {
4178 struct fifoinfo
*fip
;
4180 MALLOC(fip
, struct fifoinfo
*,
4181 sizeof(*fip
), M_TEMP
, M_WAITOK
);
4182 bzero(fip
, sizeof(struct fifoinfo
));
4183 vp
->v_fifoinfo
= fip
;
4185 /* The file systems must pass the address of the location where
4186 * they store the vnode pointer. When we add the vnode into the mount
4187 * list and name cache they become discoverable. So the file system node
4188 * must have the connection to vnode setup by then
4192 /* Add fs named reference. */
4193 if (param
->vnfs_flags
& VNFS_ADDFSREF
) {
4194 vp
->v_lflag
|= VNAMED_FSHASH
;
4196 if (param
->vnfs_mp
) {
4197 if (param
->vnfs_mp
->mnt_kern_flag
& MNTK_LOCK_LOCAL
)
4198 vp
->v_flag
|= VLOCKLOCAL
;
4200 if ((vp
->v_freelist
.tqe_prev
!= (struct vnode
**)0xdeadb))
4201 panic("insmntque: vp on the free list\n");
4203 * enter in mount vnode list
4205 insmntque(vp
, param
->vnfs_mp
);
4208 if ((param
->vnfs_mp
->mnt_vtable
->vfc_vfsflags
& VFC_VFSTHREADSAFE
) == 0) {
4209 MALLOC_ZONE(vp
->v_unsafefs
, struct unsafe_fsnode
*,
4210 sizeof(struct unsafe_fsnode
), M_UNSAFEFS
, M_WAITOK
);
4211 vp
->v_unsafefs
->fsnode_count
= 0;
4212 vp
->v_unsafefs
->fsnodeowner
= (void *)NULL
;
4213 lck_mtx_init(&vp
->v_unsafefs
->fsnodelock
, vnode_lck_grp
, vnode_lck_attr
);
4215 #endif /* __LP64__ */
4217 if (dvp
&& vnode_ref(dvp
) == 0) {
4221 if (dvp
&& ((param
->vnfs_flags
& (VNFS_NOCACHE
| VNFS_CANTCACHE
)) == 0)) {
4223 * enter into name cache
4224 * we've got the info to enter it into the name cache now
4225 * cache_enter_create will pick up an extra reference on
4226 * the name entered into the string cache
4228 vp
->v_name
= cache_enter_create(dvp
, vp
, cnp
);
4230 vp
->v_name
= vfs_addname(cnp
->cn_nameptr
, cnp
->cn_namelen
, cnp
->cn_hash
, 0);
4232 if ((cnp
->cn_flags
& UNIONCREATED
) == UNIONCREATED
)
4233 vp
->v_flag
|= VISUNION
;
4235 if ((param
->vnfs_flags
& VNFS_CANTCACHE
) == 0) {
4237 * this vnode is being created as cacheable in the name cache
4238 * this allows us to re-enter it in the cache
4240 vp
->v_flag
|= VNCACHEABLE
;
4242 ut
= get_bsdthread_info(current_thread());
4244 if ((current_proc()->p_lflag
& P_LRAGE_VNODES
) ||
4245 (ut
->uu_flag
& UT_RAGE_VNODES
)) {
4247 * process has indicated that it wants any
4248 * vnodes created on its behalf to be rapidly
4249 * aged to reduce the impact on the cached set
4252 vp
->v_flag
|= VRAGE
;
4261 vnode_addfsref(vnode_t vp
)
4263 vnode_lock_spin(vp
);
4264 if (vp
->v_lflag
& VNAMED_FSHASH
)
4265 panic("add_fsref: vp already has named reference");
4266 if ((vp
->v_freelist
.tqe_prev
!= (struct vnode
**)0xdeadb))
4267 panic("addfsref: vp on the free list\n");
4268 vp
->v_lflag
|= VNAMED_FSHASH
;
4274 vnode_removefsref(vnode_t vp
)
4276 vnode_lock_spin(vp
);
4277 if ((vp
->v_lflag
& VNAMED_FSHASH
) == 0)
4278 panic("remove_fsref: no named reference");
4279 vp
->v_lflag
&= ~VNAMED_FSHASH
;
4287 vfs_iterate(__unused
int flags
, int (*callout
)(mount_t
, void *), void *arg
)
4292 int count
, actualcount
, i
;
4295 count
= mount_getvfscnt();
4298 fsid_list
= (fsid_t
*)kalloc(count
* sizeof(fsid_t
));
4299 allocmem
= (void *)fsid_list
;
4301 actualcount
= mount_fillfsids(fsid_list
, count
);
4303 for (i
=0; i
< actualcount
; i
++) {
4305 /* obtain the mount point with iteration reference */
4306 mp
= mount_list_lookupby_fsid(&fsid_list
[i
], 0, 1);
4308 if(mp
== (struct mount
*)0)
4311 if (mp
->mnt_lflag
& (MNT_LDEAD
| MNT_LUNMOUNT
)) {
4319 /* iterate over all the vnodes */
4320 ret
= callout(mp
, arg
);
4326 case VFS_RETURNED_DONE
:
4327 if (ret
== VFS_RETURNED_DONE
) {
4333 case VFS_CLAIMED_DONE
:
4344 kfree(allocmem
, (count
* sizeof(fsid_t
)));
4349 * Update the vfsstatfs structure in the mountpoint.
4350 * MAC: Parameter eventtype added, indicating whether the event that
4351 * triggered this update came from user space, via a system call
4352 * (VFS_USER_EVENT) or an internal kernel call (VFS_KERNEL_EVENT).
4355 vfs_update_vfsstat(mount_t mp
, vfs_context_t ctx
, __unused
int eventtype
)
4361 * Request the attributes we want to propagate into
4362 * the per-mount vfsstat structure.
4365 VFSATTR_WANTED(&va
, f_iosize
);
4366 VFSATTR_WANTED(&va
, f_blocks
);
4367 VFSATTR_WANTED(&va
, f_bfree
);
4368 VFSATTR_WANTED(&va
, f_bavail
);
4369 VFSATTR_WANTED(&va
, f_bused
);
4370 VFSATTR_WANTED(&va
, f_files
);
4371 VFSATTR_WANTED(&va
, f_ffree
);
4372 VFSATTR_WANTED(&va
, f_bsize
);
4373 VFSATTR_WANTED(&va
, f_fssubtype
);
4375 if (eventtype
== VFS_USER_EVENT
) {
4376 error
= mac_mount_check_getattr(ctx
, mp
, &va
);
4382 if ((error
= vfs_getattr(mp
, &va
, ctx
)) != 0) {
4383 KAUTH_DEBUG("STAT - filesystem returned error %d", error
);
4388 * Unpack into the per-mount structure.
4390 * We only overwrite these fields, which are likely to change:
4398 * And these which are not, but which the FS has no other way
4399 * of providing to us:
4405 if (VFSATTR_IS_SUPPORTED(&va
, f_bsize
)) {
4406 /* 4822056 - protect against malformed server mount */
4407 mp
->mnt_vfsstat
.f_bsize
= (va
.f_bsize
> 0 ? va
.f_bsize
: 512);
4409 mp
->mnt_vfsstat
.f_bsize
= mp
->mnt_devblocksize
; /* default from the device block size */
4411 if (VFSATTR_IS_SUPPORTED(&va
, f_iosize
)) {
4412 mp
->mnt_vfsstat
.f_iosize
= va
.f_iosize
;
4414 mp
->mnt_vfsstat
.f_iosize
= 1024 * 1024; /* 1MB sensible I/O size */
4416 if (VFSATTR_IS_SUPPORTED(&va
, f_blocks
))
4417 mp
->mnt_vfsstat
.f_blocks
= va
.f_blocks
;
4418 if (VFSATTR_IS_SUPPORTED(&va
, f_bfree
))
4419 mp
->mnt_vfsstat
.f_bfree
= va
.f_bfree
;
4420 if (VFSATTR_IS_SUPPORTED(&va
, f_bavail
))
4421 mp
->mnt_vfsstat
.f_bavail
= va
.f_bavail
;
4422 if (VFSATTR_IS_SUPPORTED(&va
, f_bused
))
4423 mp
->mnt_vfsstat
.f_bused
= va
.f_bused
;
4424 if (VFSATTR_IS_SUPPORTED(&va
, f_files
))
4425 mp
->mnt_vfsstat
.f_files
= va
.f_files
;
4426 if (VFSATTR_IS_SUPPORTED(&va
, f_ffree
))
4427 mp
->mnt_vfsstat
.f_ffree
= va
.f_ffree
;
4429 /* this is unlikely to change, but has to be queried for */
4430 if (VFSATTR_IS_SUPPORTED(&va
, f_fssubtype
))
4431 mp
->mnt_vfsstat
.f_fssubtype
= va
.f_fssubtype
;
4437 mount_list_add(mount_t mp
)
4442 if (system_inshutdown
!= 0) {
4445 TAILQ_INSERT_TAIL(&mountlist
, mp
, mnt_list
);
4449 mount_list_unlock();
4455 mount_list_remove(mount_t mp
)
4458 TAILQ_REMOVE(&mountlist
, mp
, mnt_list
);
4460 mp
->mnt_list
.tqe_next
= NULL
;
4461 mp
->mnt_list
.tqe_prev
= NULL
;
4462 mount_list_unlock();
4466 mount_lookupby_volfsid(int volfs_id
, int withref
)
4468 mount_t cur_mount
= (mount_t
)0;
4472 TAILQ_FOREACH(mp
, &mountlist
, mnt_list
) {
4473 if (!(mp
->mnt_kern_flag
& MNTK_UNMOUNT
) &&
4474 (mp
->mnt_kern_flag
& MNTK_PATH_FROM_ID
) &&
4475 (mp
->mnt_vfsstat
.f_fsid
.val
[0] == volfs_id
)) {
4478 if (mount_iterref(cur_mount
, 1)) {
4479 cur_mount
= (mount_t
)0;
4480 mount_list_unlock();
4487 mount_list_unlock();
4488 if (withref
&& (cur_mount
!= (mount_t
)0)) {
4490 if (vfs_busy(mp
, LK_NOWAIT
) != 0) {
4491 cur_mount
= (mount_t
)0;
4500 mount_list_lookupby_fsid(fsid_t
*fsid
, int locked
, int withref
)
4502 mount_t retmp
= (mount_t
)0;
4507 TAILQ_FOREACH(mp
, &mountlist
, mnt_list
)
4508 if (mp
->mnt_vfsstat
.f_fsid
.val
[0] == fsid
->val
[0] &&
4509 mp
->mnt_vfsstat
.f_fsid
.val
[1] == fsid
->val
[1]) {
4512 if (mount_iterref(retmp
, 1))
4519 mount_list_unlock();
4524 vnode_lookup(const char *path
, int flags
, vnode_t
*vpp
, vfs_context_t ctx
)
4526 struct nameidata nd
;
4528 u_int32_t ndflags
= 0;
4530 if (ctx
== NULL
) { /* XXX technically an error */
4531 ctx
= vfs_context_current();
4534 if (flags
& VNODE_LOOKUP_NOFOLLOW
)
4539 if (flags
& VNODE_LOOKUP_NOCROSSMOUNT
)
4540 ndflags
|= NOCROSSMOUNT
;
4541 if (flags
& VNODE_LOOKUP_DOWHITEOUT
)
4542 ndflags
|= DOWHITEOUT
;
4544 /* XXX AUDITVNPATH1 needed ? */
4545 NDINIT(&nd
, LOOKUP
, ndflags
, UIO_SYSSPACE
, CAST_USER_ADDR_T(path
), ctx
);
4547 if ((error
= namei(&nd
)))
4556 vnode_open(const char *path
, int fmode
, int cmode
, int flags
, vnode_t
*vpp
, vfs_context_t ctx
)
4558 struct nameidata nd
;
4560 u_int32_t ndflags
= 0;
4563 if (ctx
== NULL
) { /* XXX technically an error */
4564 ctx
= vfs_context_current();
4567 if (fmode
& O_NOFOLLOW
)
4568 lflags
|= VNODE_LOOKUP_NOFOLLOW
;
4570 if (lflags
& VNODE_LOOKUP_NOFOLLOW
)
4575 if (lflags
& VNODE_LOOKUP_NOCROSSMOUNT
)
4576 ndflags
|= NOCROSSMOUNT
;
4577 if (lflags
& VNODE_LOOKUP_DOWHITEOUT
)
4578 ndflags
|= DOWHITEOUT
;
4580 /* XXX AUDITVNPATH1 needed ? */
4581 NDINIT(&nd
, LOOKUP
, ndflags
, UIO_SYSSPACE
, CAST_USER_ADDR_T(path
), ctx
);
4583 if ((error
= vn_open(&nd
, fmode
, cmode
)))
4592 vnode_close(vnode_t vp
, int flags
, vfs_context_t ctx
)
4597 ctx
= vfs_context_current();
4600 error
= vn_close(vp
, flags
, ctx
);
4606 * Returns: 0 Success
4610 vnode_size(vnode_t vp
, off_t
*sizep
, vfs_context_t ctx
)
4612 struct vnode_attr va
;
4616 VATTR_WANTED(&va
, va_data_size
);
4617 error
= vnode_getattr(vp
, &va
, ctx
);
4619 *sizep
= va
.va_data_size
;
4624 vnode_setsize(vnode_t vp
, off_t size
, int ioflag
, vfs_context_t ctx
)
4626 struct vnode_attr va
;
4629 VATTR_SET(&va
, va_data_size
, size
);
4630 va
.va_vaflags
= ioflag
& 0xffff;
4631 return(vnode_setattr(vp
, &va
, ctx
));
4635 * Create a filesystem object of arbitrary type with arbitrary attributes in
4636 * the spevied directory with the specified name.
4638 * Parameters: dvp Pointer to the vnode of the directory
4639 * in which to create the object.
4640 * vpp Pointer to the area into which to
4641 * return the vnode of the created object.
4642 * cnp Component name pointer from the namei
4643 * data structure, containing the name to
4644 * use for the create object.
4645 * vap Pointer to the vnode_attr structure
4646 * describing the object to be created,
4647 * including the type of object.
4648 * flags VN_* flags controlling ACL inheritance
4649 * and whether or not authorization is to
4650 * be required for the operation.
4652 * Returns: 0 Success
4655 * Implicit: *vpp Contains the vnode of the object that
4656 * was created, if successful.
4657 * *cnp May be modified by the underlying VFS.
4658 * *vap May be modified by the underlying VFS.
4659 * modified by either ACL inheritance or
4662 * be modified, even if the operation is
4665 * Notes: The kauth_filesec_t in 'vap', if any, is in host byte order.
4667 * Modification of '*cnp' and '*vap' by the underlying VFS is
4668 * strongly discouraged.
4670 * XXX: This function is a 'vn_*' function; it belongs in vfs_vnops.c
4672 * XXX: We should enummerate the possible errno values here, and where
4673 * in the code they originated.
4676 vn_create(vnode_t dvp
, vnode_t
*vpp
, struct componentname
*cnp
, struct vnode_attr
*vap
, int flags
, vfs_context_t ctx
)
4678 kauth_acl_t oacl
, nacl
;
4681 vnode_t vp
= (vnode_t
)0;
4687 KAUTH_DEBUG("%p CREATE - '%s'", dvp
, cnp
->cn_nameptr
);
4690 * Handle ACL inheritance.
4692 if (!(flags
& VN_CREATE_NOINHERIT
) && vfs_extendedsecurity(dvp
->v_mount
)) {
4693 /* save the original filesec */
4694 if (VATTR_IS_ACTIVE(vap
, va_acl
)) {
4700 if ((error
= kauth_acl_inherit(dvp
,
4703 vap
->va_type
== VDIR
,
4705 KAUTH_DEBUG("%p CREATE - error %d processing inheritance", dvp
, error
);
4710 * If the generated ACL is NULL, then we can save ourselves some effort
4711 * by clearing the active bit.
4714 VATTR_CLEAR_ACTIVE(vap
, va_acl
);
4716 VATTR_SET(vap
, va_acl
, nacl
);
4721 * Check and default new attributes.
4722 * This will set va_uid, va_gid, va_mode and va_create_time at least, if the caller
4723 * hasn't supplied them.
4725 if ((error
= vnode_authattr_new(dvp
, vap
, flags
& VN_CREATE_NOAUTH
, ctx
)) != 0) {
4726 KAUTH_DEBUG("%p CREATE - error %d handing/defaulting attributes", dvp
, error
);
4732 * Create the requested node.
4734 switch(vap
->va_type
) {
4736 error
= VNOP_CREATE(dvp
, vpp
, cnp
, vap
, ctx
);
4739 error
= VNOP_MKDIR(dvp
, vpp
, cnp
, vap
, ctx
);
4745 error
= VNOP_MKNOD(dvp
, vpp
, cnp
, vap
, ctx
);
4748 panic("vnode_create: unknown vtype %d", vap
->va_type
);
4751 KAUTH_DEBUG("%p CREATE - error %d returned by filesystem", dvp
, error
);
4757 if (!(flags
& VN_CREATE_NOLABEL
)) {
4758 error
= vnode_label(vnode_mount(vp
), dvp
, vp
, cnp
, VNODE_LABEL_CREATE
, ctx
);
4765 * If some of the requested attributes weren't handled by the VNOP,
4766 * use our fallback code.
4768 if (!VATTR_ALL_SUPPORTED(vap
) && *vpp
) {
4769 KAUTH_DEBUG(" CREATE - doing fallback with ACL %p", vap
->va_acl
);
4770 error
= vnode_setattr_fallback(*vpp
, vap
, ctx
);
4775 if ((error
!= 0 ) && (vp
!= (vnode_t
)0)) {
4782 * If the caller supplied a filesec in vap, it has been replaced
4783 * now by the post-inheritance copy. We need to put the original back
4784 * and free the inherited product.
4787 VATTR_SET(vap
, va_acl
, oacl
);
4789 VATTR_CLEAR_ACTIVE(vap
, va_acl
);
4792 kauth_acl_free(nacl
);
4797 static kauth_scope_t vnode_scope
;
4798 static int vnode_authorize_callback(kauth_cred_t credential
, void *idata
, kauth_action_t action
,
4799 uintptr_t arg0
, uintptr_t arg1
, uintptr_t arg2
, uintptr_t arg3
);
4800 static int vnode_authorize_callback_int(__unused kauth_cred_t credential
, __unused
void *idata
, kauth_action_t action
,
4801 uintptr_t arg0
, uintptr_t arg1
, uintptr_t arg2
, uintptr_t arg3
);
4803 typedef struct _vnode_authorize_context
{
4805 struct vnode_attr
*vap
;
4807 struct vnode_attr
*dvap
;
4811 #define _VAC_IS_OWNER (1<<0)
4812 #define _VAC_IN_GROUP (1<<1)
4813 #define _VAC_IS_DIR_OWNER (1<<2)
4814 #define _VAC_IN_DIR_GROUP (1<<3)
4818 vnode_authorize_init(void)
4820 vnode_scope
= kauth_register_scope(KAUTH_SCOPE_VNODE
, vnode_authorize_callback
, NULL
);
4824 * Authorize an operation on a vnode.
4826 * This is KPI, but here because it needs vnode_scope.
4828 * Returns: 0 Success
4829 * kauth_authorize_action:EPERM ...
4830 * xlate => EACCES Permission denied
4831 * kauth_authorize_action:0 Success
4832 * kauth_authorize_action: Depends on callback return; this is
4833 * usually only vnode_authorize_callback(),
4834 * but may include other listerners, if any
4842 vnode_authorize(vnode_t vp
, vnode_t dvp
, kauth_action_t action
, vfs_context_t ctx
)
4847 * We can't authorize against a dead vnode; allow all operations through so that
4848 * the correct error can be returned.
4850 if (vp
->v_type
== VBAD
)
4854 result
= kauth_authorize_action(vnode_scope
, vfs_context_ucred(ctx
), action
,
4855 (uintptr_t)ctx
, (uintptr_t)vp
, (uintptr_t)dvp
, (uintptr_t)&error
);
4856 if (result
== EPERM
) /* traditional behaviour */
4858 /* did the lower layers give a better error return? */
4859 if ((result
!= 0) && (error
!= 0))
4865 * Test for vnode immutability.
4867 * The 'append' flag is set when the authorization request is constrained
4868 * to operations which only request the right to append to a file.
4870 * The 'ignore' flag is set when an operation modifying the immutability flags
4871 * is being authorized. We check the system securelevel to determine which
4872 * immutability flags we can ignore.
4875 vnode_immutable(struct vnode_attr
*vap
, int append
, int ignore
)
4879 /* start with all bits precluding the operation */
4880 mask
= IMMUTABLE
| APPEND
;
4882 /* if appending only, remove the append-only bits */
4886 /* ignore only set when authorizing flags changes */
4888 if (securelevel
<= 0) {
4889 /* in insecure state, flags do not inhibit changes */
4892 /* in secure state, user flags don't inhibit */
4893 mask
&= ~(UF_IMMUTABLE
| UF_APPEND
);
4896 KAUTH_DEBUG("IMMUTABLE - file flags 0x%x mask 0x%x append = %d ignore = %d", vap
->va_flags
, mask
, append
, ignore
);
4897 if ((vap
->va_flags
& mask
) != 0)
4903 vauth_node_owner(struct vnode_attr
*vap
, kauth_cred_t cred
)
4907 /* default assumption is not-owner */
4911 * If the filesystem has given us a UID, we treat this as authoritative.
4913 if (vap
&& VATTR_IS_SUPPORTED(vap
, va_uid
)) {
4914 result
= (vap
->va_uid
== kauth_cred_getuid(cred
)) ? 1 : 0;
4916 /* we could test the owner UUID here if we had a policy for it */
4922 vauth_node_group(struct vnode_attr
*vap
, kauth_cred_t cred
, int *ismember
)
4930 /* the caller is expected to have asked the filesystem for a group at some point */
4931 if (vap
&& VATTR_IS_SUPPORTED(vap
, va_gid
)) {
4932 error
= kauth_cred_ismember_gid(cred
, vap
->va_gid
, &result
);
4934 /* we could test the group UUID here if we had a policy for it */
4942 vauth_file_owner(vauth_ctx vcp
)
4946 if (vcp
->flags_valid
& _VAC_IS_OWNER
) {
4947 result
= (vcp
->flags
& _VAC_IS_OWNER
) ? 1 : 0;
4949 result
= vauth_node_owner(vcp
->vap
, vcp
->ctx
->vc_ucred
);
4951 /* cache our result */
4952 vcp
->flags_valid
|= _VAC_IS_OWNER
;
4954 vcp
->flags
|= _VAC_IS_OWNER
;
4956 vcp
->flags
&= ~_VAC_IS_OWNER
;
4963 vauth_file_ingroup(vauth_ctx vcp
, int *ismember
)
4967 if (vcp
->flags_valid
& _VAC_IN_GROUP
) {
4968 *ismember
= (vcp
->flags
& _VAC_IN_GROUP
) ? 1 : 0;
4971 error
= vauth_node_group(vcp
->vap
, vcp
->ctx
->vc_ucred
, ismember
);
4974 /* cache our result */
4975 vcp
->flags_valid
|= _VAC_IN_GROUP
;
4977 vcp
->flags
|= _VAC_IN_GROUP
;
4979 vcp
->flags
&= ~_VAC_IN_GROUP
;
4988 vauth_dir_owner(vauth_ctx vcp
)
4992 if (vcp
->flags_valid
& _VAC_IS_DIR_OWNER
) {
4993 result
= (vcp
->flags
& _VAC_IS_DIR_OWNER
) ? 1 : 0;
4995 result
= vauth_node_owner(vcp
->dvap
, vcp
->ctx
->vc_ucred
);
4997 /* cache our result */
4998 vcp
->flags_valid
|= _VAC_IS_DIR_OWNER
;
5000 vcp
->flags
|= _VAC_IS_DIR_OWNER
;
5002 vcp
->flags
&= ~_VAC_IS_DIR_OWNER
;
5009 vauth_dir_ingroup(vauth_ctx vcp
, int *ismember
)
5013 if (vcp
->flags_valid
& _VAC_IN_DIR_GROUP
) {
5014 *ismember
= (vcp
->flags
& _VAC_IN_DIR_GROUP
) ? 1 : 0;
5017 error
= vauth_node_group(vcp
->dvap
, vcp
->ctx
->vc_ucred
, ismember
);
5020 /* cache our result */
5021 vcp
->flags_valid
|= _VAC_IN_DIR_GROUP
;
5023 vcp
->flags
|= _VAC_IN_DIR_GROUP
;
5025 vcp
->flags
&= ~_VAC_IN_DIR_GROUP
;
5033 * Test the posix permissions in (vap) to determine whether (credential)
5034 * may perform (action)
5037 vnode_authorize_posix(vauth_ctx vcp
, int action
, int on_dir
)
5039 struct vnode_attr
*vap
;
5040 int needed
, error
, owner_ok
, group_ok
, world_ok
, ismember
;
5041 #ifdef KAUTH_DEBUG_ENABLE
5042 const char *where
= "uninitialized";
5043 # define _SETWHERE(c) where = c;
5045 # define _SETWHERE(c)
5048 /* checking file or directory? */
5058 * We want to do as little work here as possible. So first we check
5059 * which sets of permissions grant us the access we need, and avoid checking
5060 * whether specific permissions grant access when more generic ones would.
5063 /* owner permissions */
5067 if (action
& VWRITE
)
5071 owner_ok
= (needed
& vap
->va_mode
) == needed
;
5073 /* group permissions */
5077 if (action
& VWRITE
)
5081 group_ok
= (needed
& vap
->va_mode
) == needed
;
5083 /* world permissions */
5087 if (action
& VWRITE
)
5091 world_ok
= (needed
& vap
->va_mode
) == needed
;
5093 /* If granted/denied by all three, we're done */
5094 if (owner_ok
&& group_ok
&& world_ok
) {
5098 if (!owner_ok
&& !group_ok
&& !world_ok
) {
5104 /* Check ownership (relatively cheap) */
5105 if ((on_dir
&& vauth_dir_owner(vcp
)) ||
5106 (!on_dir
&& vauth_file_owner(vcp
))) {
5113 /* Not owner; if group and world both grant it we're done */
5114 if (group_ok
&& world_ok
) {
5115 _SETWHERE("group/world");
5118 if (!group_ok
&& !world_ok
) {
5119 _SETWHERE("group/world");
5124 /* Check group membership (most expensive) */
5127 error
= vauth_dir_ingroup(vcp
, &ismember
);
5129 error
= vauth_file_ingroup(vcp
, &ismember
);
5140 /* Not owner, not in group, use world result */
5148 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",
5149 vcp
->vp
, (error
== 0) ? "ALLOWED" : "DENIED", where
,
5150 (action
& VREAD
) ? "r" : "-",
5151 (action
& VWRITE
) ? "w" : "-",
5152 (action
& VEXEC
) ? "x" : "-",
5154 (vap
->va_mode
& S_IRUSR
) ? "r" : "-",
5155 (vap
->va_mode
& S_IWUSR
) ? "w" : "-",
5156 (vap
->va_mode
& S_IXUSR
) ? "x" : "-",
5157 (vap
->va_mode
& S_IRGRP
) ? "r" : "-",
5158 (vap
->va_mode
& S_IWGRP
) ? "w" : "-",
5159 (vap
->va_mode
& S_IXGRP
) ? "x" : "-",
5160 (vap
->va_mode
& S_IROTH
) ? "r" : "-",
5161 (vap
->va_mode
& S_IWOTH
) ? "w" : "-",
5162 (vap
->va_mode
& S_IXOTH
) ? "x" : "-",
5163 kauth_cred_getuid(vcp
->ctx
->vc_ucred
),
5164 on_dir
? vcp
->dvap
->va_uid
: vcp
->vap
->va_uid
,
5165 on_dir
? vcp
->dvap
->va_gid
: vcp
->vap
->va_gid
);
5170 * Authorize the deletion of the node vp from the directory dvp.
5173 * - Neither the node nor the directory are immutable.
5174 * - The user is not the superuser.
5176 * Deletion is not permitted if the directory is sticky and the caller is
5177 * not owner of the node or directory.
5179 * If either the node grants DELETE, or the directory grants DELETE_CHILD,
5180 * the node may be deleted. If neither denies the permission, and the
5181 * caller has Posix write access to the directory, then the node may be
5184 * As an optimization, we cache whether or not delete child is permitted
5185 * on directories without the sticky bit set.
5188 vnode_authorize_delete(vauth_ctx vcp
, boolean_t cached_delete_child
);
5190 vnode_authorize_delete(vauth_ctx vcp
, boolean_t cached_delete_child
)
5192 struct vnode_attr
*vap
= vcp
->vap
;
5193 struct vnode_attr
*dvap
= vcp
->dvap
;
5194 kauth_cred_t cred
= vcp
->ctx
->vc_ucred
;
5195 struct kauth_acl_eval eval
;
5196 int error
, delete_denied
, delete_child_denied
, ismember
;
5198 /* check the ACL on the directory */
5199 delete_child_denied
= 0;
5200 if (!cached_delete_child
&& VATTR_IS_NOT(dvap
, va_acl
, NULL
)) {
5201 eval
.ae_requested
= KAUTH_VNODE_DELETE_CHILD
;
5202 eval
.ae_acl
= &dvap
->va_acl
->acl_ace
[0];
5203 eval
.ae_count
= dvap
->va_acl
->acl_entrycount
;
5204 eval
.ae_options
= 0;
5205 if (vauth_dir_owner(vcp
))
5206 eval
.ae_options
|= KAUTH_AEVAL_IS_OWNER
;
5207 if ((error
= vauth_dir_ingroup(vcp
, &ismember
)) != 0)
5210 eval
.ae_options
|= KAUTH_AEVAL_IN_GROUP
;
5211 eval
.ae_exp_gall
= KAUTH_VNODE_GENERIC_ALL_BITS
;
5212 eval
.ae_exp_gread
= KAUTH_VNODE_GENERIC_READ_BITS
;
5213 eval
.ae_exp_gwrite
= KAUTH_VNODE_GENERIC_WRITE_BITS
;
5214 eval
.ae_exp_gexec
= KAUTH_VNODE_GENERIC_EXECUTE_BITS
;
5216 error
= kauth_acl_evaluate(cred
, &eval
);
5219 KAUTH_DEBUG("%p ERROR during ACL processing - %d", vcp
->vp
, error
);
5222 if (eval
.ae_result
== KAUTH_RESULT_DENY
)
5223 delete_child_denied
= 1;
5224 if (eval
.ae_result
== KAUTH_RESULT_ALLOW
) {
5225 KAUTH_DEBUG("%p ALLOWED - granted by directory ACL", vcp
->vp
);
5230 /* check the ACL on the node */
5232 if (VATTR_IS_NOT(vap
, va_acl
, NULL
)) {
5233 eval
.ae_requested
= KAUTH_VNODE_DELETE
;
5234 eval
.ae_acl
= &vap
->va_acl
->acl_ace
[0];
5235 eval
.ae_count
= vap
->va_acl
->acl_entrycount
;
5236 eval
.ae_options
= 0;
5237 if (vauth_file_owner(vcp
))
5238 eval
.ae_options
|= KAUTH_AEVAL_IS_OWNER
;
5239 if ((error
= vauth_file_ingroup(vcp
, &ismember
)) != 0)
5242 eval
.ae_options
|= KAUTH_AEVAL_IN_GROUP
;
5243 eval
.ae_exp_gall
= KAUTH_VNODE_GENERIC_ALL_BITS
;
5244 eval
.ae_exp_gread
= KAUTH_VNODE_GENERIC_READ_BITS
;
5245 eval
.ae_exp_gwrite
= KAUTH_VNODE_GENERIC_WRITE_BITS
;
5246 eval
.ae_exp_gexec
= KAUTH_VNODE_GENERIC_EXECUTE_BITS
;
5248 if ((error
= kauth_acl_evaluate(cred
, &eval
)) != 0) {
5249 KAUTH_DEBUG("%p ERROR during ACL processing - %d", vcp
->vp
, error
);
5252 if (eval
.ae_result
== KAUTH_RESULT_DENY
)
5254 if (eval
.ae_result
== KAUTH_RESULT_ALLOW
) {
5255 KAUTH_DEBUG("%p ALLOWED - granted by file ACL", vcp
->vp
);
5260 /* if denied by ACL on directory or node, return denial */
5261 if (delete_denied
|| delete_child_denied
) {
5262 KAUTH_DEBUG("%p ALLOWED - denied by ACL", vcp
->vp
);
5267 * enforce sticky bit behaviour; the cached_delete_child property will
5268 * be false and the dvap contents valis for sticky bit directories;
5269 * this makes us check the directory each time, but it's unavoidable,
5270 * as sticky bit is an exception to caching.
5272 if (!cached_delete_child
&& (dvap
->va_mode
& S_ISTXT
) && !vauth_file_owner(vcp
) && !vauth_dir_owner(vcp
)) {
5273 KAUTH_DEBUG("%p DENIED - sticky bit rules (user %d file %d dir %d)",
5274 vcp
->vp
, cred
->cr_uid
, vap
->va_uid
, dvap
->va_uid
);
5278 /* check the directory */
5279 if (!cached_delete_child
&& (error
= vnode_authorize_posix(vcp
, VWRITE
, 1 /* on_dir */)) != 0) {
5280 KAUTH_DEBUG("%p ALLOWED - granted by posix permisssions", vcp
->vp
);
5284 /* not denied, must be OK */
5290 * Authorize an operation based on the node's attributes.
5293 vnode_authorize_simple(vauth_ctx vcp
, kauth_ace_rights_t acl_rights
, kauth_ace_rights_t preauth_rights
, boolean_t
*found_deny
)
5295 struct vnode_attr
*vap
= vcp
->vap
;
5296 kauth_cred_t cred
= vcp
->ctx
->vc_ucred
;
5297 struct kauth_acl_eval eval
;
5298 int error
, ismember
;
5299 mode_t posix_action
;
5302 * If we are the file owner, we automatically have some rights.
5304 * Do we need to expand this to support group ownership?
5306 if (vauth_file_owner(vcp
))
5307 acl_rights
&= ~(KAUTH_VNODE_WRITE_SECURITY
);
5310 * If we are checking both TAKE_OWNERSHIP and WRITE_SECURITY, we can
5311 * mask the latter. If TAKE_OWNERSHIP is requested the caller is about to
5312 * change ownership to themselves, and WRITE_SECURITY is implicitly
5313 * granted to the owner. We need to do this because at this point
5314 * WRITE_SECURITY may not be granted as the caller is not currently
5317 if ((acl_rights
& KAUTH_VNODE_TAKE_OWNERSHIP
) &&
5318 (acl_rights
& KAUTH_VNODE_WRITE_SECURITY
))
5319 acl_rights
&= ~KAUTH_VNODE_WRITE_SECURITY
;
5321 if (acl_rights
== 0) {
5322 KAUTH_DEBUG("%p ALLOWED - implicit or no rights required", vcp
->vp
);
5326 /* if we have an ACL, evaluate it */
5327 if (VATTR_IS_NOT(vap
, va_acl
, NULL
)) {
5328 eval
.ae_requested
= acl_rights
;
5329 eval
.ae_acl
= &vap
->va_acl
->acl_ace
[0];
5330 eval
.ae_count
= vap
->va_acl
->acl_entrycount
;
5331 eval
.ae_options
= 0;
5332 if (vauth_file_owner(vcp
))
5333 eval
.ae_options
|= KAUTH_AEVAL_IS_OWNER
;
5334 if ((error
= vauth_file_ingroup(vcp
, &ismember
)) != 0)
5337 eval
.ae_options
|= KAUTH_AEVAL_IN_GROUP
;
5338 eval
.ae_exp_gall
= KAUTH_VNODE_GENERIC_ALL_BITS
;
5339 eval
.ae_exp_gread
= KAUTH_VNODE_GENERIC_READ_BITS
;
5340 eval
.ae_exp_gwrite
= KAUTH_VNODE_GENERIC_WRITE_BITS
;
5341 eval
.ae_exp_gexec
= KAUTH_VNODE_GENERIC_EXECUTE_BITS
;
5343 if ((error
= kauth_acl_evaluate(cred
, &eval
)) != 0) {
5344 KAUTH_DEBUG("%p ERROR during ACL processing - %d", vcp
->vp
, error
);
5348 if (eval
.ae_result
== KAUTH_RESULT_DENY
) {
5349 KAUTH_DEBUG("%p DENIED - by ACL", vcp
->vp
);
5350 return(EACCES
); /* deny, deny, counter-allege */
5352 if (eval
.ae_result
== KAUTH_RESULT_ALLOW
) {
5353 KAUTH_DEBUG("%p ALLOWED - all rights granted by ACL", vcp
->vp
);
5356 *found_deny
= eval
.ae_found_deny
;
5358 /* fall through and evaluate residual rights */
5360 /* no ACL, everything is residual */
5361 eval
.ae_residual
= acl_rights
;
5365 * Grant residual rights that have been pre-authorized.
5367 eval
.ae_residual
&= ~preauth_rights
;
5370 * We grant WRITE_ATTRIBUTES to the owner if it hasn't been denied.
5372 if (vauth_file_owner(vcp
))
5373 eval
.ae_residual
&= ~KAUTH_VNODE_WRITE_ATTRIBUTES
;
5375 if (eval
.ae_residual
== 0) {
5376 KAUTH_DEBUG("%p ALLOWED - rights already authorized", vcp
->vp
);
5381 * Bail if we have residual rights that can't be granted by posix permissions,
5382 * or aren't presumed granted at this point.
5384 * XXX these can be collapsed for performance
5386 if (eval
.ae_residual
& KAUTH_VNODE_CHANGE_OWNER
) {
5387 KAUTH_DEBUG("%p DENIED - CHANGE_OWNER not permitted", vcp
->vp
);
5390 if (eval
.ae_residual
& KAUTH_VNODE_WRITE_SECURITY
) {
5391 KAUTH_DEBUG("%p DENIED - WRITE_SECURITY not permitted", vcp
->vp
);
5396 if (eval
.ae_residual
& KAUTH_VNODE_DELETE
)
5397 panic("vnode_authorize: can't be checking delete permission here");
5401 * Compute the fallback posix permissions that will satisfy the remaining
5405 if (eval
.ae_residual
& (KAUTH_VNODE_READ_DATA
|
5406 KAUTH_VNODE_LIST_DIRECTORY
|
5407 KAUTH_VNODE_READ_EXTATTRIBUTES
))
5408 posix_action
|= VREAD
;
5409 if (eval
.ae_residual
& (KAUTH_VNODE_WRITE_DATA
|
5410 KAUTH_VNODE_ADD_FILE
|
5411 KAUTH_VNODE_ADD_SUBDIRECTORY
|
5412 KAUTH_VNODE_DELETE_CHILD
|
5413 KAUTH_VNODE_WRITE_ATTRIBUTES
|
5414 KAUTH_VNODE_WRITE_EXTATTRIBUTES
))
5415 posix_action
|= VWRITE
;
5416 if (eval
.ae_residual
& (KAUTH_VNODE_EXECUTE
|
5417 KAUTH_VNODE_SEARCH
))
5418 posix_action
|= VEXEC
;
5420 if (posix_action
!= 0) {
5421 return(vnode_authorize_posix(vcp
, posix_action
, 0 /* !on_dir */));
5423 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",
5425 (eval
.ae_residual
& KAUTH_VNODE_READ_DATA
)
5426 ? vnode_isdir(vcp
->vp
) ? " LIST_DIRECTORY" : " READ_DATA" : "",
5427 (eval
.ae_residual
& KAUTH_VNODE_WRITE_DATA
)
5428 ? vnode_isdir(vcp
->vp
) ? " ADD_FILE" : " WRITE_DATA" : "",
5429 (eval
.ae_residual
& KAUTH_VNODE_EXECUTE
)
5430 ? vnode_isdir(vcp
->vp
) ? " SEARCH" : " EXECUTE" : "",
5431 (eval
.ae_residual
& KAUTH_VNODE_DELETE
)
5433 (eval
.ae_residual
& KAUTH_VNODE_APPEND_DATA
)
5434 ? vnode_isdir(vcp
->vp
) ? " ADD_SUBDIRECTORY" : " APPEND_DATA" : "",
5435 (eval
.ae_residual
& KAUTH_VNODE_DELETE_CHILD
)
5436 ? " DELETE_CHILD" : "",
5437 (eval
.ae_residual
& KAUTH_VNODE_READ_ATTRIBUTES
)
5438 ? " READ_ATTRIBUTES" : "",
5439 (eval
.ae_residual
& KAUTH_VNODE_WRITE_ATTRIBUTES
)
5440 ? " WRITE_ATTRIBUTES" : "",
5441 (eval
.ae_residual
& KAUTH_VNODE_READ_EXTATTRIBUTES
)
5442 ? " READ_EXTATTRIBUTES" : "",
5443 (eval
.ae_residual
& KAUTH_VNODE_WRITE_EXTATTRIBUTES
)
5444 ? " WRITE_EXTATTRIBUTES" : "",
5445 (eval
.ae_residual
& KAUTH_VNODE_READ_SECURITY
)
5446 ? " READ_SECURITY" : "",
5447 (eval
.ae_residual
& KAUTH_VNODE_WRITE_SECURITY
)
5448 ? " WRITE_SECURITY" : "",
5449 (eval
.ae_residual
& KAUTH_VNODE_CHECKIMMUTABLE
)
5450 ? " CHECKIMMUTABLE" : "",
5451 (eval
.ae_residual
& KAUTH_VNODE_CHANGE_OWNER
)
5452 ? " CHANGE_OWNER" : "");
5456 * Lack of required Posix permissions implies no reason to deny access.
5462 * Check for file immutability.
5465 vnode_authorize_checkimmutable(vnode_t vp
, struct vnode_attr
*vap
, int rights
, int ignore
)
5472 * Perform immutability checks for operations that change data.
5474 * Sockets, fifos and devices require special handling.
5476 switch(vp
->v_type
) {
5482 * Writing to these nodes does not change the filesystem data,
5483 * so forget that it's being tried.
5485 rights
&= ~KAUTH_VNODE_WRITE_DATA
;
5492 if (rights
& KAUTH_VNODE_WRITE_RIGHTS
) {
5494 /* check per-filesystem options if possible */
5498 /* check for no-EA filesystems */
5499 if ((rights
& KAUTH_VNODE_WRITE_EXTATTRIBUTES
) &&
5500 (vfs_flags(mp
) & MNT_NOUSERXATTR
)) {
5501 KAUTH_DEBUG("%p DENIED - filesystem disallowed extended attributes", vp
);
5502 error
= EACCES
; /* User attributes disabled */
5508 * check for file immutability. first, check if the requested rights are
5509 * allowable for a UF_APPEND file.
5512 if (vp
->v_type
== VDIR
) {
5513 if ((rights
& (KAUTH_VNODE_ADD_FILE
| KAUTH_VNODE_ADD_SUBDIRECTORY
| KAUTH_VNODE_WRITE_EXTATTRIBUTES
)) == rights
)
5516 if ((rights
& (KAUTH_VNODE_APPEND_DATA
| KAUTH_VNODE_WRITE_EXTATTRIBUTES
)) == rights
)
5519 if ((error
= vnode_immutable(vap
, append
, ignore
)) != 0) {
5520 KAUTH_DEBUG("%p DENIED - file is immutable", vp
);
5529 * Handle authorization actions for filesystems that advertise that the
5530 * server will be enforcing.
5532 * Returns: 0 Authorization should be handled locally
5533 * 1 Authorization was handled by the FS
5535 * Note: Imputed returns will only occur if the authorization request
5536 * was handled by the FS.
5538 * Imputed: *resultp, modified Return code from FS when the request is
5539 * handled by the FS.
5544 vnode_authorize_opaque(vnode_t vp
, int *resultp
, kauth_action_t action
, vfs_context_t ctx
)
5549 * If the vp is a device node, socket or FIFO it actually represents a local
5550 * endpoint, so we need to handle it locally.
5552 switch(vp
->v_type
) {
5563 * In the advisory request case, if the filesystem doesn't think it's reliable
5564 * we will attempt to formulate a result ourselves based on VNOP_GETATTR data.
5566 if ((action
& KAUTH_VNODE_ACCESS
) && !vfs_authopaqueaccess(vp
->v_mount
))
5570 * Let the filesystem have a say in the matter. It's OK for it to not implemnent
5571 * VNOP_ACCESS, as most will authorise inline with the actual request.
5573 if ((error
= VNOP_ACCESS(vp
, action
, ctx
)) != ENOTSUP
) {
5575 KAUTH_DEBUG("%p DENIED - opaque filesystem VNOP_ACCESS denied access", vp
);
5580 * Typically opaque filesystems do authorisation in-line, but exec is a special case. In
5581 * order to be reasonably sure that exec will be permitted, we try a bit harder here.
5583 if ((action
& KAUTH_VNODE_EXECUTE
) && (vp
->v_type
== VREG
)) {
5584 /* try a VNOP_OPEN for readonly access */
5585 if ((error
= VNOP_OPEN(vp
, FREAD
, ctx
)) != 0) {
5587 KAUTH_DEBUG("%p DENIED - EXECUTE denied because file could not be opened readonly", vp
);
5590 VNOP_CLOSE(vp
, FREAD
, ctx
);
5594 * We don't have any reason to believe that the request has to be denied at this point,
5595 * so go ahead and allow it.
5598 KAUTH_DEBUG("%p ALLOWED - bypassing access check for non-local filesystem", vp
);
5606 * Returns: KAUTH_RESULT_ALLOW
5609 * Imputed: *arg3, modified Error code in the deny case
5610 * EROFS Read-only file system
5611 * EACCES Permission denied
5612 * EPERM Operation not permitted [no execute]
5613 * vnode_getattr:ENOMEM Not enough space [only if has filesec]
5615 * vnode_authorize_opaque:*arg2 ???
5616 * vnode_authorize_checkimmutable:???
5617 * vnode_authorize_delete:???
5618 * vnode_authorize_simple:???
5623 vnode_authorize_callback(kauth_cred_t cred
, void *idata
, kauth_action_t action
,
5624 uintptr_t arg0
, uintptr_t arg1
, uintptr_t arg2
, uintptr_t arg3
)
5627 vnode_t cvp
= NULLVP
;
5629 int result
= KAUTH_RESULT_DENY
;
5630 int parent_iocount
= 0;
5631 int parent_action
; /* In case we need to use namedstream's data fork for cached rights*/
5633 ctx
= (vfs_context_t
)arg0
;
5635 dvp
= (vnode_t
)arg2
;
5638 * if there are 2 vnodes passed in, we don't know at
5639 * this point which rights to look at based on the
5640 * combined action being passed in... defer until later...
5641 * otherwise check the kauth 'rights' cache hung
5642 * off of the vnode we're interested in... if we've already
5643 * been granted the right we're currently interested in,
5644 * we can just return success... otherwise we'll go through
5645 * the process of authorizing the requested right(s)... if that
5646 * succeeds, we'll add the right(s) to the cache.
5647 * VNOP_SETATTR and VNOP_SETXATTR will invalidate this cache
5655 * For named streams on local-authorization volumes, rights are cached on the parent;
5656 * authorization is determined by looking at the parent's properties anyway, so storing
5657 * on the parent means that we don't recompute for the named stream and that if
5658 * we need to flush rights (e.g. on VNOP_SETATTR()) we don't need to track down the
5659 * stream to flush its cache separately. If we miss in the cache, then we authorize
5660 * as if there were no cached rights (passing the named stream vnode and desired rights to
5661 * vnode_authorize_callback_int()).
5663 * On an opaquely authorized volume, we don't know the relationship between the
5664 * data fork's properties and the rights granted on a stream. Thus, named stream vnodes
5665 * on such a volume are authorized directly (rather than using the parent) and have their
5666 * own caches. When a named stream vnode is created, we mark the parent as having a named
5667 * stream. On a VNOP_SETATTR() for the parent that may invalidate cached authorization, we
5668 * find the stream and flush its cache.
5670 if (vnode_isnamedstream(vp
) && (!vfs_authopaque(vp
->v_mount
))) {
5672 if ((cvp
!= NULLVP
) && (vnode_getwithref(cvp
) == 0)) {
5676 goto defer
; /* If we can't use the parent, take the slow path */
5679 /* Have to translate some actions */
5680 parent_action
= action
;
5681 if (parent_action
& KAUTH_VNODE_READ_DATA
) {
5682 parent_action
&= ~KAUTH_VNODE_READ_DATA
;
5683 parent_action
|= KAUTH_VNODE_READ_EXTATTRIBUTES
;
5685 if (parent_action
& KAUTH_VNODE_WRITE_DATA
) {
5686 parent_action
&= ~KAUTH_VNODE_WRITE_DATA
;
5687 parent_action
|= KAUTH_VNODE_WRITE_EXTATTRIBUTES
;
5695 if (vnode_cache_is_authorized(cvp
, ctx
, parent_iocount
? parent_action
: action
) == TRUE
) {
5696 result
= KAUTH_RESULT_ALLOW
;
5700 result
= vnode_authorize_callback_int(cred
, idata
, action
, arg0
, arg1
, arg2
, arg3
);
5702 if (result
== KAUTH_RESULT_ALLOW
&& cvp
!= NULLVP
)
5703 vnode_cache_authorized_action(cvp
, ctx
, action
);
5706 if (parent_iocount
) {
5715 vnode_authorize_callback_int(__unused kauth_cred_t unused_cred
, __unused
void *idata
, kauth_action_t action
,
5716 uintptr_t arg0
, uintptr_t arg1
, uintptr_t arg2
, uintptr_t arg3
)
5718 struct _vnode_authorize_context auth_context
;
5723 kauth_ace_rights_t rights
;
5724 struct vnode_attr va
, dva
;
5728 boolean_t parent_authorized_for_delete_child
= FALSE
;
5729 boolean_t found_deny
= FALSE
;
5730 boolean_t parent_ref
= FALSE
;
5732 vcp
= &auth_context
;
5733 ctx
= vcp
->ctx
= (vfs_context_t
)arg0
;
5734 vp
= vcp
->vp
= (vnode_t
)arg1
;
5735 dvp
= vcp
->dvp
= (vnode_t
)arg2
;
5736 errorp
= (int *)arg3
;
5738 * Note that we authorize against the context, not the passed cred
5739 * (the same thing anyway)
5741 cred
= ctx
->vc_ucred
;
5748 vcp
->flags
= vcp
->flags_valid
= 0;
5751 if ((ctx
== NULL
) || (vp
== NULL
) || (cred
== NULL
))
5752 panic("vnode_authorize: bad arguments (context %p vp %p cred %p)", ctx
, vp
, cred
);
5755 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)",
5756 vp
, vfs_context_proc(ctx
)->p_comm
,
5757 (action
& KAUTH_VNODE_ACCESS
) ? "access" : "auth",
5758 (action
& KAUTH_VNODE_READ_DATA
) ? vnode_isdir(vp
) ? " LIST_DIRECTORY" : " READ_DATA" : "",
5759 (action
& KAUTH_VNODE_WRITE_DATA
) ? vnode_isdir(vp
) ? " ADD_FILE" : " WRITE_DATA" : "",
5760 (action
& KAUTH_VNODE_EXECUTE
) ? vnode_isdir(vp
) ? " SEARCH" : " EXECUTE" : "",
5761 (action
& KAUTH_VNODE_DELETE
) ? " DELETE" : "",
5762 (action
& KAUTH_VNODE_APPEND_DATA
) ? vnode_isdir(vp
) ? " ADD_SUBDIRECTORY" : " APPEND_DATA" : "",
5763 (action
& KAUTH_VNODE_DELETE_CHILD
) ? " DELETE_CHILD" : "",
5764 (action
& KAUTH_VNODE_READ_ATTRIBUTES
) ? " READ_ATTRIBUTES" : "",
5765 (action
& KAUTH_VNODE_WRITE_ATTRIBUTES
) ? " WRITE_ATTRIBUTES" : "",
5766 (action
& KAUTH_VNODE_READ_EXTATTRIBUTES
) ? " READ_EXTATTRIBUTES" : "",
5767 (action
& KAUTH_VNODE_WRITE_EXTATTRIBUTES
) ? " WRITE_EXTATTRIBUTES" : "",
5768 (action
& KAUTH_VNODE_READ_SECURITY
) ? " READ_SECURITY" : "",
5769 (action
& KAUTH_VNODE_WRITE_SECURITY
) ? " WRITE_SECURITY" : "",
5770 (action
& KAUTH_VNODE_CHANGE_OWNER
) ? " CHANGE_OWNER" : "",
5771 (action
& KAUTH_VNODE_NOIMMUTABLE
) ? " (noimmutable)" : "",
5772 vnode_isdir(vp
) ? "directory" : "file",
5773 vp
->v_name
? vp
->v_name
: "<NULL>", action
, vp
, dvp
);
5776 * Extract the control bits from the action, everything else is
5779 noimmutable
= (action
& KAUTH_VNODE_NOIMMUTABLE
) ? 1 : 0;
5780 rights
= action
& ~(KAUTH_VNODE_ACCESS
| KAUTH_VNODE_NOIMMUTABLE
);
5782 if (rights
& KAUTH_VNODE_DELETE
) {
5785 panic("vnode_authorize: KAUTH_VNODE_DELETE test requires a directory");
5788 * check to see if we've already authorized the parent
5789 * directory for deletion of its children... if so, we
5790 * can skip a whole bunch of work... we will still have to
5791 * authorize that this specific child can be removed
5793 if (vnode_cache_is_authorized(dvp
, ctx
, KAUTH_VNODE_DELETE_CHILD
) == TRUE
)
5794 parent_authorized_for_delete_child
= TRUE
;
5800 * Check for read-only filesystems.
5802 if ((rights
& KAUTH_VNODE_WRITE_RIGHTS
) &&
5803 (vp
->v_mount
->mnt_flag
& MNT_RDONLY
) &&
5804 ((vp
->v_type
== VREG
) || (vp
->v_type
== VDIR
) ||
5805 (vp
->v_type
== VLNK
) || (vp
->v_type
== VCPLX
) ||
5806 (rights
& KAUTH_VNODE_DELETE
) || (rights
& KAUTH_VNODE_DELETE_CHILD
))) {
5812 * Check for noexec filesystems.
5814 if ((rights
& KAUTH_VNODE_EXECUTE
) && (vp
->v_type
== VREG
) && (vp
->v_mount
->mnt_flag
& MNT_NOEXEC
)) {
5820 * Handle cases related to filesystems with non-local enforcement.
5821 * This call can return 0, in which case we will fall through to perform a
5822 * check based on VNOP_GETATTR data. Otherwise it returns 1 and sets
5823 * an appropriate result, at which point we can return immediately.
5825 if ((vp
->v_mount
->mnt_kern_flag
& MNTK_AUTH_OPAQUE
) && vnode_authorize_opaque(vp
, &result
, action
, ctx
))
5829 * Get vnode attributes and extended security information for the vnode
5830 * and directory if required.
5832 VATTR_WANTED(&va
, va_mode
);
5833 VATTR_WANTED(&va
, va_uid
);
5834 VATTR_WANTED(&va
, va_gid
);
5835 VATTR_WANTED(&va
, va_flags
);
5836 VATTR_WANTED(&va
, va_acl
);
5837 if ((result
= vnode_getattr(vp
, &va
, ctx
)) != 0) {
5838 KAUTH_DEBUG("%p ERROR - failed to get vnode attributes - %d", vp
, result
);
5841 if (dvp
&& parent_authorized_for_delete_child
== FALSE
) {
5842 VATTR_WANTED(&dva
, va_mode
);
5843 VATTR_WANTED(&dva
, va_uid
);
5844 VATTR_WANTED(&dva
, va_gid
);
5845 VATTR_WANTED(&dva
, va_flags
);
5846 VATTR_WANTED(&dva
, va_acl
);
5847 if ((result
= vnode_getattr(dvp
, &dva
, ctx
)) != 0) {
5848 KAUTH_DEBUG("%p ERROR - failed to get directory vnode attributes - %d", vp
, result
);
5854 * If the vnode is an extended attribute data vnode (eg. a resource fork), *_DATA becomes
5857 if (vnode_isnamedstream(vp
)) {
5858 if (rights
& KAUTH_VNODE_READ_DATA
) {
5859 rights
&= ~KAUTH_VNODE_READ_DATA
;
5860 rights
|= KAUTH_VNODE_READ_EXTATTRIBUTES
;
5862 if (rights
& KAUTH_VNODE_WRITE_DATA
) {
5863 rights
&= ~KAUTH_VNODE_WRITE_DATA
;
5864 rights
|= KAUTH_VNODE_WRITE_EXTATTRIBUTES
;
5869 * Point 'vp' to the resource fork's parent for ACL checking
5871 if (vnode_isnamedstream(vp
) &&
5872 (vp
->v_parent
!= NULL
) &&
5873 (vget_internal(vp
->v_parent
, 0, VNODE_NODEAD
) == 0)) {
5875 vcp
->vp
= vp
= vp
->v_parent
;
5876 if (VATTR_IS_SUPPORTED(&va
, va_acl
) && (va
.va_acl
!= NULL
))
5877 kauth_acl_free(va
.va_acl
);
5879 VATTR_WANTED(&va
, va_mode
);
5880 VATTR_WANTED(&va
, va_uid
);
5881 VATTR_WANTED(&va
, va_gid
);
5882 VATTR_WANTED(&va
, va_flags
);
5883 VATTR_WANTED(&va
, va_acl
);
5884 if ((result
= vnode_getattr(vp
, &va
, ctx
)) != 0)
5889 * Check for immutability.
5891 * In the deletion case, parent directory immutability vetoes specific
5894 if ((result
= vnode_authorize_checkimmutable(vp
, &va
, rights
, noimmutable
)) != 0)
5896 if ((rights
& KAUTH_VNODE_DELETE
) &&
5897 parent_authorized_for_delete_child
== FALSE
&&
5898 ((result
= vnode_authorize_checkimmutable(dvp
, &dva
, KAUTH_VNODE_DELETE_CHILD
, 0)) != 0))
5902 * Clear rights that have been authorized by reaching this point, bail if nothing left to
5905 rights
&= ~(KAUTH_VNODE_LINKTARGET
| KAUTH_VNODE_CHECKIMMUTABLE
);
5910 * If we're not the superuser, authorize based on file properties;
5911 * note that even if parent_authorized_for_delete_child is TRUE, we
5912 * need to check on the node itself.
5914 if (!vfs_context_issuser(ctx
)) {
5915 /* process delete rights */
5916 if ((rights
& KAUTH_VNODE_DELETE
) &&
5917 ((result
= vnode_authorize_delete(vcp
, parent_authorized_for_delete_child
)) != 0))
5920 /* process remaining rights */
5921 if ((rights
& ~KAUTH_VNODE_DELETE
) &&
5922 (result
= vnode_authorize_simple(vcp
, rights
, rights
& KAUTH_VNODE_DELETE
, &found_deny
)) != 0)
5927 * Execute is only granted to root if one of the x bits is set. This check only
5928 * makes sense if the posix mode bits are actually supported.
5930 if ((rights
& KAUTH_VNODE_EXECUTE
) &&
5931 (vp
->v_type
== VREG
) &&
5932 VATTR_IS_SUPPORTED(&va
, va_mode
) &&
5933 !(va
.va_mode
& (S_IXUSR
| S_IXGRP
| S_IXOTH
))) {
5935 KAUTH_DEBUG("%p DENIED - root execute requires at least one x bit in 0x%x", vp
, va
.va_mode
);
5939 KAUTH_DEBUG("%p ALLOWED - caller is superuser", vp
);
5942 if (VATTR_IS_SUPPORTED(&va
, va_acl
) && (va
.va_acl
!= NULL
))
5943 kauth_acl_free(va
.va_acl
);
5944 if (VATTR_IS_SUPPORTED(&dva
, va_acl
) && (dva
.va_acl
!= NULL
))
5945 kauth_acl_free(dva
.va_acl
);
5951 KAUTH_DEBUG("%p DENIED - auth denied", vp
);
5952 return(KAUTH_RESULT_DENY
);
5954 if ((rights
& KAUTH_VNODE_SEARCH
) && found_deny
== FALSE
&& vp
->v_type
== VDIR
) {
5956 * if we were successfully granted the right to search this directory
5957 * and there were NO ACL DENYs for search and the posix permissions also don't
5958 * deny execute, we can synthesize a global right that allows anyone to
5959 * traverse this directory during a pathname lookup without having to
5960 * match the credential associated with this cache of rights.
5962 if (!VATTR_IS_SUPPORTED(&va
, va_mode
) ||
5963 ((va
.va_mode
& (S_IXUSR
| S_IXGRP
| S_IXOTH
)) ==
5964 (S_IXUSR
| S_IXGRP
| S_IXOTH
))) {
5965 vnode_cache_authorized_action(vp
, ctx
, KAUTH_VNODE_SEARCHBYANYONE
);
5968 if ((rights
& KAUTH_VNODE_DELETE
) && parent_authorized_for_delete_child
== FALSE
) {
5970 * parent was successfully and newly authorized for content deletions
5971 * add it to the cache, but only if it doesn't have the sticky
5972 * bit set on it. This same check is done earlier guarding
5973 * fetching of dva, and if we jumped to out without having done
5974 * this, we will have returned already because of a non-zero
5977 if (VATTR_IS_SUPPORTED(&dva
, va_mode
) &&
5978 !(dva
.va_mode
& (S_ISVTX
))) {
5979 /* OK to cache delete rights */
5980 vnode_cache_authorized_action(dvp
, ctx
, KAUTH_VNODE_DELETE_CHILD
);
5986 * Note that this implies that we will allow requests for no rights, as well as
5987 * for rights that we do not recognise. There should be none of these.
5989 KAUTH_DEBUG("%p ALLOWED - auth granted", vp
);
5990 return(KAUTH_RESULT_ALLOW
);
5994 * Check that the attribute information in vattr can be legally applied to
5995 * a new file by the context.
5998 vnode_authattr_new(vnode_t dvp
, struct vnode_attr
*vap
, int noauth
, vfs_context_t ctx
)
6001 int has_priv_suser
, ismember
, defaulted_owner
, defaulted_group
, defaulted_mode
;
6007 defaulted_owner
= defaulted_group
= defaulted_mode
= 0;
6010 * Require that the filesystem support extended security to apply any.
6012 if (!vfs_extendedsecurity(dvp
->v_mount
) &&
6013 (VATTR_IS_ACTIVE(vap
, va_acl
) || VATTR_IS_ACTIVE(vap
, va_uuuid
) || VATTR_IS_ACTIVE(vap
, va_guuid
))) {
6019 * Default some fields.
6024 * If the filesystem is mounted IGNORE_OWNERSHIP and an explicit owner is set, that
6025 * owner takes ownership of all new files.
6027 if ((dmp
->mnt_flag
& MNT_IGNORE_OWNERSHIP
) && (dmp
->mnt_fsowner
!= KAUTH_UID_NONE
)) {
6028 VATTR_SET(vap
, va_uid
, dmp
->mnt_fsowner
);
6029 defaulted_owner
= 1;
6031 if (!VATTR_IS_ACTIVE(vap
, va_uid
)) {
6032 /* default owner is current user */
6033 VATTR_SET(vap
, va_uid
, kauth_cred_getuid(vfs_context_ucred(ctx
)));
6034 defaulted_owner
= 1;
6039 * If the filesystem is mounted IGNORE_OWNERSHIP and an explicit grouo is set, that
6040 * group takes ownership of all new files.
6042 if ((dmp
->mnt_flag
& MNT_IGNORE_OWNERSHIP
) && (dmp
->mnt_fsgroup
!= KAUTH_GID_NONE
)) {
6043 VATTR_SET(vap
, va_gid
, dmp
->mnt_fsgroup
);
6044 defaulted_group
= 1;
6046 if (!VATTR_IS_ACTIVE(vap
, va_gid
)) {
6047 /* default group comes from parent object, fallback to current user */
6048 struct vnode_attr dva
;
6050 VATTR_WANTED(&dva
, va_gid
);
6051 if ((error
= vnode_getattr(dvp
, &dva
, ctx
)) != 0)
6053 if (VATTR_IS_SUPPORTED(&dva
, va_gid
)) {
6054 VATTR_SET(vap
, va_gid
, dva
.va_gid
);
6056 VATTR_SET(vap
, va_gid
, kauth_cred_getgid(vfs_context_ucred(ctx
)));
6058 defaulted_group
= 1;
6062 if (!VATTR_IS_ACTIVE(vap
, va_flags
))
6063 VATTR_SET(vap
, va_flags
, 0);
6065 /* default mode is everything, masked with current umask */
6066 if (!VATTR_IS_ACTIVE(vap
, va_mode
)) {
6067 VATTR_SET(vap
, va_mode
, ACCESSPERMS
& ~vfs_context_proc(ctx
)->p_fd
->fd_cmask
);
6068 KAUTH_DEBUG("ATTR - defaulting new file mode to %o from umask %o", vap
->va_mode
, vfs_context_proc(ctx
)->p_fd
->fd_cmask
);
6071 /* set timestamps to now */
6072 if (!VATTR_IS_ACTIVE(vap
, va_create_time
)) {
6073 nanotime(&vap
->va_create_time
);
6074 VATTR_SET_ACTIVE(vap
, va_create_time
);
6078 * Check for attempts to set nonsensical fields.
6080 if (vap
->va_active
& ~VNODE_ATTR_NEWOBJ
) {
6082 KAUTH_DEBUG("ATTR - ERROR - attempt to set unsupported new-file attributes %llx",
6083 vap
->va_active
& ~VNODE_ATTR_NEWOBJ
);
6088 * Quickly check for the applicability of any enforcement here.
6089 * Tests below maintain the integrity of the local security model.
6091 if (vfs_authopaque(dvp
->v_mount
))
6095 * We need to know if the caller is the superuser, or if the work is
6096 * otherwise already authorised.
6098 cred
= vfs_context_ucred(ctx
);
6100 /* doing work for the kernel */
6103 has_priv_suser
= vfs_context_issuser(ctx
);
6107 if (VATTR_IS_ACTIVE(vap
, va_flags
)) {
6108 if (has_priv_suser
) {
6109 if ((vap
->va_flags
& (UF_SETTABLE
| SF_SETTABLE
)) != vap
->va_flags
) {
6111 KAUTH_DEBUG(" DENIED - superuser attempt to set illegal flag(s)");
6115 if ((vap
->va_flags
& UF_SETTABLE
) != vap
->va_flags
) {
6117 KAUTH_DEBUG(" DENIED - user attempt to set illegal flag(s)");
6123 /* if not superuser, validate legality of new-item attributes */
6124 if (!has_priv_suser
) {
6125 if (!defaulted_mode
&& VATTR_IS_ACTIVE(vap
, va_mode
)) {
6127 if (vap
->va_mode
& S_ISGID
) {
6128 if ((error
= kauth_cred_ismember_gid(cred
, vap
->va_gid
, &ismember
)) != 0) {
6129 KAUTH_DEBUG("ATTR - ERROR: got %d checking for membership in %d", error
, vap
->va_gid
);
6133 KAUTH_DEBUG(" DENIED - can't set SGID bit, not a member of %d", vap
->va_gid
);
6140 if ((vap
->va_mode
& S_ISUID
) && (vap
->va_uid
!= kauth_cred_getuid(cred
))) {
6141 KAUTH_DEBUG("ATTR - ERROR: illegal attempt to set the setuid bit");
6146 if (!defaulted_owner
&& (vap
->va_uid
!= kauth_cred_getuid(cred
))) {
6147 KAUTH_DEBUG(" DENIED - cannot create new item owned by %d", vap
->va_uid
);
6151 if (!defaulted_group
) {
6152 if ((error
= kauth_cred_ismember_gid(cred
, vap
->va_gid
, &ismember
)) != 0) {
6153 KAUTH_DEBUG(" ERROR - got %d checking for membership in %d", error
, vap
->va_gid
);
6157 KAUTH_DEBUG(" DENIED - cannot create new item with group %d - not a member", vap
->va_gid
);
6163 /* initialising owner/group UUID */
6164 if (VATTR_IS_ACTIVE(vap
, va_uuuid
)) {
6165 if ((error
= kauth_cred_getguid(cred
, &changer
)) != 0) {
6166 KAUTH_DEBUG(" ERROR - got %d trying to get caller UUID", error
);
6167 /* XXX ENOENT here - no GUID - should perhaps become EPERM */
6170 if (!kauth_guid_equal(&vap
->va_uuuid
, &changer
)) {
6171 KAUTH_DEBUG(" ERROR - cannot create item with supplied owner UUID - not us");
6176 if (VATTR_IS_ACTIVE(vap
, va_guuid
)) {
6177 if ((error
= kauth_cred_ismember_guid(cred
, &vap
->va_guuid
, &ismember
)) != 0) {
6178 KAUTH_DEBUG(" ERROR - got %d trying to check group membership", error
);
6182 KAUTH_DEBUG(" ERROR - cannot create item with supplied group UUID - not a member");
6193 * Check that the attribute information in vap can be legally written by the
6196 * Call this when you're not sure about the vnode_attr; either its contents
6197 * have come from an unknown source, or when they are variable.
6199 * Returns errno, or zero and sets *actionp to the KAUTH_VNODE_* actions that
6200 * must be authorized to be permitted to write the vattr.
6203 vnode_authattr(vnode_t vp
, struct vnode_attr
*vap
, kauth_action_t
*actionp
, vfs_context_t ctx
)
6205 struct vnode_attr ova
;
6206 kauth_action_t required_action
;
6207 int error
, has_priv_suser
, ismember
, chowner
, chgroup
, clear_suid
, clear_sgid
;
6216 required_action
= 0;
6220 * Quickly check for enforcement applicability.
6222 if (vfs_authopaque(vp
->v_mount
))
6226 * Check for attempts to set nonsensical fields.
6228 if (vap
->va_active
& VNODE_ATTR_RDONLY
) {
6229 KAUTH_DEBUG("ATTR - ERROR: attempt to set readonly attribute(s)");
6235 * We need to know if the caller is the superuser.
6237 cred
= vfs_context_ucred(ctx
);
6238 has_priv_suser
= kauth_cred_issuser(cred
);
6241 * If any of the following are changing, we need information from the old file:
6248 if (VATTR_IS_ACTIVE(vap
, va_uid
) ||
6249 VATTR_IS_ACTIVE(vap
, va_gid
) ||
6250 VATTR_IS_ACTIVE(vap
, va_mode
) ||
6251 VATTR_IS_ACTIVE(vap
, va_uuuid
) ||
6252 VATTR_IS_ACTIVE(vap
, va_guuid
)) {
6253 VATTR_WANTED(&ova
, va_mode
);
6254 VATTR_WANTED(&ova
, va_uid
);
6255 VATTR_WANTED(&ova
, va_gid
);
6256 VATTR_WANTED(&ova
, va_uuuid
);
6257 VATTR_WANTED(&ova
, va_guuid
);
6258 KAUTH_DEBUG("ATTR - security information changing, fetching existing attributes");
6262 * If timestamps are being changed, we need to know who the file is owned
6265 if (VATTR_IS_ACTIVE(vap
, va_create_time
) ||
6266 VATTR_IS_ACTIVE(vap
, va_change_time
) ||
6267 VATTR_IS_ACTIVE(vap
, va_modify_time
) ||
6268 VATTR_IS_ACTIVE(vap
, va_access_time
) ||
6269 VATTR_IS_ACTIVE(vap
, va_backup_time
)) {
6271 VATTR_WANTED(&ova
, va_uid
);
6272 #if 0 /* enable this when we support UUIDs as official owners */
6273 VATTR_WANTED(&ova
, va_uuuid
);
6275 KAUTH_DEBUG("ATTR - timestamps changing, fetching uid and GUID");
6279 * If flags are being changed, we need the old flags.
6281 if (VATTR_IS_ACTIVE(vap
, va_flags
)) {
6282 KAUTH_DEBUG("ATTR - flags changing, fetching old flags");
6283 VATTR_WANTED(&ova
, va_flags
);
6287 * If the size is being set, make sure it's not a directory.
6289 if (VATTR_IS_ACTIVE(vap
, va_data_size
)) {
6290 /* size is meaningless on a directory, don't permit this */
6291 if (vnode_isdir(vp
)) {
6292 KAUTH_DEBUG("ATTR - ERROR: size change requested on a directory");
6301 KAUTH_DEBUG("ATTR - fetching old attributes %016llx", ova
.va_active
);
6302 if ((error
= vnode_getattr(vp
, &ova
, ctx
)) != 0) {
6303 KAUTH_DEBUG(" ERROR - got %d trying to get attributes", error
);
6308 * Size changes require write access to the file data.
6310 if (VATTR_IS_ACTIVE(vap
, va_data_size
)) {
6311 /* if we can't get the size, or it's different, we need write access */
6312 KAUTH_DEBUG("ATTR - size change, requiring WRITE_DATA");
6313 required_action
|= KAUTH_VNODE_WRITE_DATA
;
6317 * Changing timestamps?
6319 * Note that we are only called to authorize user-requested time changes;
6320 * side-effect time changes are not authorized. Authorisation is only
6321 * required for existing files.
6323 * Non-owners are not permitted to change the time on an existing
6324 * file to anything other than the current time.
6326 if (VATTR_IS_ACTIVE(vap
, va_create_time
) ||
6327 VATTR_IS_ACTIVE(vap
, va_change_time
) ||
6328 VATTR_IS_ACTIVE(vap
, va_modify_time
) ||
6329 VATTR_IS_ACTIVE(vap
, va_access_time
) ||
6330 VATTR_IS_ACTIVE(vap
, va_backup_time
)) {
6332 * The owner and root may set any timestamps they like,
6333 * provided that the file is not immutable. The owner still needs
6334 * WRITE_ATTRIBUTES (implied by ownership but still deniable).
6336 if (has_priv_suser
|| vauth_node_owner(&ova
, cred
)) {
6337 KAUTH_DEBUG("ATTR - root or owner changing timestamps");
6338 required_action
|= KAUTH_VNODE_CHECKIMMUTABLE
| KAUTH_VNODE_WRITE_ATTRIBUTES
;
6340 /* just setting the current time? */
6341 if (vap
->va_vaflags
& VA_UTIMES_NULL
) {
6342 KAUTH_DEBUG("ATTR - non-root/owner changing timestamps, requiring WRITE_ATTRIBUTES");
6343 required_action
|= KAUTH_VNODE_WRITE_ATTRIBUTES
;
6345 KAUTH_DEBUG("ATTR - ERROR: illegal timestamp modification attempted");
6353 * Changing file mode?
6355 if (VATTR_IS_ACTIVE(vap
, va_mode
) && VATTR_IS_SUPPORTED(&ova
, va_mode
) && (ova
.va_mode
!= vap
->va_mode
)) {
6356 KAUTH_DEBUG("ATTR - mode change from %06o to %06o", ova
.va_mode
, vap
->va_mode
);
6359 * Mode changes always have the same basic auth requirements.
6361 if (has_priv_suser
) {
6362 KAUTH_DEBUG("ATTR - superuser mode change, requiring immutability check");
6363 required_action
|= KAUTH_VNODE_CHECKIMMUTABLE
;
6365 /* need WRITE_SECURITY */
6366 KAUTH_DEBUG("ATTR - non-superuser mode change, requiring WRITE_SECURITY");
6367 required_action
|= KAUTH_VNODE_WRITE_SECURITY
;
6371 * Can't set the setgid bit if you're not in the group and not root. Have to have
6372 * existing group information in the case we're not setting it right now.
6374 if (vap
->va_mode
& S_ISGID
) {
6375 required_action
|= KAUTH_VNODE_CHECKIMMUTABLE
; /* always required */
6376 if (!has_priv_suser
) {
6377 if (VATTR_IS_ACTIVE(vap
, va_gid
)) {
6378 group
= vap
->va_gid
;
6379 } else if (VATTR_IS_SUPPORTED(&ova
, va_gid
)) {
6382 KAUTH_DEBUG("ATTR - ERROR: setgid but no gid available");
6387 * This might be too restrictive; WRITE_SECURITY might be implied by
6388 * membership in this case, rather than being an additional requirement.
6390 if ((error
= kauth_cred_ismember_gid(cred
, group
, &ismember
)) != 0) {
6391 KAUTH_DEBUG("ATTR - ERROR: got %d checking for membership in %d", error
, vap
->va_gid
);
6395 KAUTH_DEBUG(" DENIED - can't set SGID bit, not a member of %d", group
);
6403 * Can't set the setuid bit unless you're root or the file's owner.
6405 if (vap
->va_mode
& S_ISUID
) {
6406 required_action
|= KAUTH_VNODE_CHECKIMMUTABLE
; /* always required */
6407 if (!has_priv_suser
) {
6408 if (VATTR_IS_ACTIVE(vap
, va_uid
)) {
6409 owner
= vap
->va_uid
;
6410 } else if (VATTR_IS_SUPPORTED(&ova
, va_uid
)) {
6413 KAUTH_DEBUG("ATTR - ERROR: setuid but no uid available");
6417 if (owner
!= kauth_cred_getuid(cred
)) {
6419 * We could allow this if WRITE_SECURITY is permitted, perhaps.
6421 KAUTH_DEBUG("ATTR - ERROR: illegal attempt to set the setuid bit");
6430 * Validate/mask flags changes. This checks that only the flags in
6431 * the UF_SETTABLE mask are being set, and preserves the flags in
6432 * the SF_SETTABLE case.
6434 * Since flags changes may be made in conjunction with other changes,
6435 * we will ask the auth code to ignore immutability in the case that
6436 * the SF_* flags are not set and we are only manipulating the file flags.
6439 if (VATTR_IS_ACTIVE(vap
, va_flags
)) {
6440 /* compute changing flags bits */
6441 if (VATTR_IS_SUPPORTED(&ova
, va_flags
)) {
6442 fdelta
= vap
->va_flags
^ ova
.va_flags
;
6444 fdelta
= vap
->va_flags
;
6448 KAUTH_DEBUG("ATTR - flags changing, requiring WRITE_SECURITY");
6449 required_action
|= KAUTH_VNODE_WRITE_SECURITY
;
6451 /* check that changing bits are legal */
6452 if (has_priv_suser
) {
6454 * The immutability check will prevent us from clearing the SF_*
6455 * flags unless the system securelevel permits it, so just check
6456 * for legal flags here.
6458 if (fdelta
& ~(UF_SETTABLE
| SF_SETTABLE
)) {
6460 KAUTH_DEBUG(" DENIED - superuser attempt to set illegal flag(s)");
6464 if (fdelta
& ~UF_SETTABLE
) {
6466 KAUTH_DEBUG(" DENIED - user attempt to set illegal flag(s)");
6471 * If the caller has the ability to manipulate file flags,
6472 * security is not reduced by ignoring them for this operation.
6474 * A more complete test here would consider the 'after' states of the flags
6475 * to determine whether it would permit the operation, but this becomes
6478 * Ignoring immutability is conditional on securelevel; this does not bypass
6479 * the SF_* flags if securelevel > 0.
6481 required_action
|= KAUTH_VNODE_NOIMMUTABLE
;
6486 * Validate ownership information.
6495 * Note that if the filesystem didn't give us a UID, we expect that it doesn't
6496 * support them in general, and will ignore it if/when we try to set it.
6497 * We might want to clear the uid out of vap completely here.
6499 if (VATTR_IS_ACTIVE(vap
, va_uid
)) {
6500 if (VATTR_IS_SUPPORTED(&ova
, va_uid
) && (vap
->va_uid
!= ova
.va_uid
)) {
6501 if (!has_priv_suser
&& (kauth_cred_getuid(cred
) != vap
->va_uid
)) {
6502 KAUTH_DEBUG(" DENIED - non-superuser cannot change ownershipt to a third party");
6513 * Note that if the filesystem didn't give us a GID, we expect that it doesn't
6514 * support them in general, and will ignore it if/when we try to set it.
6515 * We might want to clear the gid out of vap completely here.
6517 if (VATTR_IS_ACTIVE(vap
, va_gid
)) {
6518 if (VATTR_IS_SUPPORTED(&ova
, va_gid
) && (vap
->va_gid
!= ova
.va_gid
)) {
6519 if (!has_priv_suser
) {
6520 if ((error
= kauth_cred_ismember_gid(cred
, vap
->va_gid
, &ismember
)) != 0) {
6521 KAUTH_DEBUG(" ERROR - got %d checking for membership in %d", error
, vap
->va_gid
);
6525 KAUTH_DEBUG(" DENIED - group change from %d to %d but not a member of target group",
6526 ova
.va_gid
, vap
->va_gid
);
6537 * Owner UUID being set or changed.
6539 if (VATTR_IS_ACTIVE(vap
, va_uuuid
)) {
6540 /* if the owner UUID is not actually changing ... */
6541 if (VATTR_IS_SUPPORTED(&ova
, va_uuuid
)) {
6542 if (kauth_guid_equal(&vap
->va_uuuid
, &ova
.va_uuuid
))
6543 goto no_uuuid_change
;
6546 * If the current owner UUID is a null GUID, check
6547 * it against the UUID corresponding to the owner UID.
6549 if (kauth_guid_equal(&ova
.va_uuuid
, &kauth_null_guid
) &&
6550 VATTR_IS_SUPPORTED(&ova
, va_uid
)) {
6553 if (kauth_cred_uid2guid(ova
.va_uid
, &uid_guid
) == 0 &&
6554 kauth_guid_equal(&vap
->va_uuuid
, &uid_guid
))
6555 goto no_uuuid_change
;
6560 * The owner UUID cannot be set by a non-superuser to anything other than
6561 * their own or a null GUID (to "unset" the owner UUID).
6562 * Note that file systems must be prepared to handle the
6563 * null UUID case in a manner appropriate for that file
6566 if (!has_priv_suser
) {
6567 if ((error
= kauth_cred_getguid(cred
, &changer
)) != 0) {
6568 KAUTH_DEBUG(" ERROR - got %d trying to get caller UUID", error
);
6569 /* XXX ENOENT here - no UUID - should perhaps become EPERM */
6572 if (!kauth_guid_equal(&vap
->va_uuuid
, &changer
) &&
6573 !kauth_guid_equal(&vap
->va_uuuid
, &kauth_null_guid
)) {
6574 KAUTH_DEBUG(" ERROR - cannot set supplied owner UUID - not us / null");
6584 * Group UUID being set or changed.
6586 if (VATTR_IS_ACTIVE(vap
, va_guuid
)) {
6587 /* if the group UUID is not actually changing ... */
6588 if (VATTR_IS_SUPPORTED(&ova
, va_guuid
)) {
6589 if (kauth_guid_equal(&vap
->va_guuid
, &ova
.va_guuid
))
6590 goto no_guuid_change
;
6593 * If the current group UUID is a null UUID, check
6594 * it against the UUID corresponding to the group GID.
6596 if (kauth_guid_equal(&ova
.va_guuid
, &kauth_null_guid
) &&
6597 VATTR_IS_SUPPORTED(&ova
, va_gid
)) {
6600 if (kauth_cred_gid2guid(ova
.va_gid
, &gid_guid
) == 0 &&
6601 kauth_guid_equal(&vap
->va_guuid
, &gid_guid
))
6602 goto no_guuid_change
;
6607 * The group UUID cannot be set by a non-superuser to anything other than
6608 * one of which they are a member or a null GUID (to "unset"
6610 * Note that file systems must be prepared to handle the
6611 * null UUID case in a manner appropriate for that file
6614 if (!has_priv_suser
) {
6615 if (kauth_guid_equal(&vap
->va_guuid
, &kauth_null_guid
))
6617 else if ((error
= kauth_cred_ismember_guid(cred
, &vap
->va_guuid
, &ismember
)) != 0) {
6618 KAUTH_DEBUG(" ERROR - got %d trying to check group membership", error
);
6622 KAUTH_DEBUG(" ERROR - cannot set supplied group UUID - not a member / null");
6632 * Compute authorisation for group/ownership changes.
6634 if (chowner
|| chgroup
|| clear_suid
|| clear_sgid
) {
6635 if (has_priv_suser
) {
6636 KAUTH_DEBUG("ATTR - superuser changing file owner/group, requiring immutability check");
6637 required_action
|= KAUTH_VNODE_CHECKIMMUTABLE
;
6640 KAUTH_DEBUG("ATTR - ownership change, requiring TAKE_OWNERSHIP");
6641 required_action
|= KAUTH_VNODE_TAKE_OWNERSHIP
;
6643 if (chgroup
&& !chowner
) {
6644 KAUTH_DEBUG("ATTR - group change, requiring WRITE_SECURITY");
6645 required_action
|= KAUTH_VNODE_WRITE_SECURITY
;
6648 /* clear set-uid and set-gid bits as required by Posix */
6649 if (VATTR_IS_ACTIVE(vap
, va_mode
)) {
6650 newmode
= vap
->va_mode
;
6651 } else if (VATTR_IS_SUPPORTED(&ova
, va_mode
)) {
6652 newmode
= ova
.va_mode
;
6654 KAUTH_DEBUG("CHOWN - trying to change owner but cannot get mode from filesystem to mask setugid bits");
6657 if (newmode
& (S_ISUID
| S_ISGID
)) {
6658 VATTR_SET(vap
, va_mode
, newmode
& ~(S_ISUID
| S_ISGID
));
6659 KAUTH_DEBUG("CHOWN - masking setugid bits from mode %o to %o", newmode
, vap
->va_mode
);
6665 * Authorise changes in the ACL.
6667 if (VATTR_IS_ACTIVE(vap
, va_acl
)) {
6669 /* no existing ACL */
6670 if (!VATTR_IS_ACTIVE(&ova
, va_acl
) || (ova
.va_acl
== NULL
)) {
6673 if (vap
->va_acl
!= NULL
) {
6674 required_action
|= KAUTH_VNODE_WRITE_SECURITY
;
6675 KAUTH_DEBUG("CHMOD - adding ACL");
6678 /* removing an existing ACL */
6679 } else if (vap
->va_acl
== NULL
) {
6680 required_action
|= KAUTH_VNODE_WRITE_SECURITY
;
6681 KAUTH_DEBUG("CHMOD - removing ACL");
6683 /* updating an existing ACL */
6685 if (vap
->va_acl
->acl_entrycount
!= ova
.va_acl
->acl_entrycount
) {
6686 /* entry count changed, must be different */
6687 required_action
|= KAUTH_VNODE_WRITE_SECURITY
;
6688 KAUTH_DEBUG("CHMOD - adding/removing ACL entries");
6689 } else if (vap
->va_acl
->acl_entrycount
> 0) {
6690 /* both ACLs have the same ACE count, said count is 1 or more, bitwise compare ACLs */
6691 if (!memcmp(&vap
->va_acl
->acl_ace
[0], &ova
.va_acl
->acl_ace
[0],
6692 sizeof(struct kauth_ace
) * vap
->va_acl
->acl_entrycount
)) {
6693 required_action
|= KAUTH_VNODE_WRITE_SECURITY
;
6694 KAUTH_DEBUG("CHMOD - changing ACL entries");
6701 * Other attributes that require authorisation.
6703 if (VATTR_IS_ACTIVE(vap
, va_encoding
))
6704 required_action
|= KAUTH_VNODE_WRITE_ATTRIBUTES
;
6707 if (VATTR_IS_SUPPORTED(&ova
, va_acl
) && (ova
.va_acl
!= NULL
))
6708 kauth_acl_free(ova
.va_acl
);
6710 *actionp
= required_action
;
6716 vfs_setlocklocal(mount_t mp
)
6721 mp
->mnt_kern_flag
|= MNTK_LOCK_LOCAL
;
6724 * We do not expect anyone to be using any vnodes at the
6725 * time this routine is called. So no need for vnode locking
6727 TAILQ_FOREACH(vp
, &mp
->mnt_vnodelist
, v_mntvnodes
) {
6728 vp
->v_flag
|= VLOCKLOCAL
;
6730 TAILQ_FOREACH(vp
, &mp
->mnt_workerqueue
, v_mntvnodes
) {
6731 vp
->v_flag
|= VLOCKLOCAL
;
6733 TAILQ_FOREACH(vp
, &mp
->mnt_newvnodes
, v_mntvnodes
) {
6734 vp
->v_flag
|= VLOCKLOCAL
;
6740 vn_setunionwait(vnode_t vp
)
6742 vnode_lock_spin(vp
);
6743 vp
->v_flag
|= VISUNION
;
6749 vn_checkunionwait(vnode_t vp
)
6751 vnode_lock_spin(vp
);
6752 while ((vp
->v_flag
& VISUNION
) == VISUNION
)
6753 msleep((caddr_t
)&vp
->v_flag
, &vp
->v_lock
, 0, 0, 0);
6758 vn_clearunionwait(vnode_t vp
, int locked
)
6761 vnode_lock_spin(vp
);
6762 if((vp
->v_flag
& VISUNION
) == VISUNION
) {
6763 vp
->v_flag
&= ~VISUNION
;
6764 wakeup((caddr_t
)&vp
->v_flag
);
6771 * XXX - get "don't trigger mounts" flag for thread; used by autofs.
6773 extern int thread_notrigger(void);
6776 thread_notrigger(void)
6778 struct uthread
*uth
= (struct uthread
*)get_bsdthread_info(current_thread());
6779 return (uth
->uu_notrigger
);
6783 * Removes orphaned apple double files during a rmdir
6785 * 1. vnode_suspend().
6786 * 2. Call VNOP_READDIR() till the end of directory is reached.
6787 * 3. Check if the directory entries returned are regular files with name starting with "._". If not, return ENOTEMPTY.
6788 * 4. Continue (2) and (3) till end of directory is reached.
6789 * 5. If all the entries in the directory were files with "._" name, delete all the files.
6791 * 7. If deletion of all files succeeded, call VNOP_RMDIR() again.
6794 errno_t
rmdir_remove_orphaned_appleDouble(vnode_t vp
, vfs_context_t ctx
, int * restart_flag
)
6797 #define UIO_BUFF_SIZE 2048
6799 int eofflag
, siz
= UIO_BUFF_SIZE
, nentries
= 0;
6800 int open_flag
= 0, full_erase_flag
= 0;
6801 char uio_buf
[ UIO_SIZEOF(1) ];
6802 char *rbuf
= NULL
, *cpos
, *cend
;
6803 struct nameidata nd_temp
;
6807 error
= vnode_suspend(vp
);
6810 * restart_flag is set so that the calling rmdir sleeps and resets
6820 MALLOC(rbuf
, caddr_t
, siz
, M_TEMP
, M_WAITOK
);
6822 auio
= uio_createwithbuffer(1, 0, UIO_SYSSPACE
, UIO_READ
,
6823 &uio_buf
[0], sizeof(uio_buf
));
6824 if (!rbuf
|| !auio
) {
6829 uio_setoffset(auio
,0);
6833 if ((error
= VNOP_OPEN(vp
, FREAD
, ctx
)))
6839 * First pass checks if all files are appleDouble files.
6843 siz
= UIO_BUFF_SIZE
;
6844 uio_reset(auio
, uio_offset(auio
), UIO_SYSSPACE
, UIO_READ
);
6845 uio_addiov(auio
, CAST_USER_ADDR_T(rbuf
), UIO_BUFF_SIZE
);
6847 if((error
= VNOP_READDIR(vp
, auio
, 0, &eofflag
, &nentries
, ctx
)))
6850 if (uio_resid(auio
) != 0)
6851 siz
-= uio_resid(auio
);
6854 * Iterate through directory
6858 dp
= (struct dirent
*) cpos
;
6863 while ((cpos
< cend
)) {
6865 * Check for . and .. as well as directories
6867 if (dp
->d_ino
!= 0 &&
6868 !((dp
->d_namlen
== 1 && dp
->d_name
[0] == '.') ||
6869 (dp
->d_namlen
== 2 && dp
->d_name
[0] == '.' && dp
->d_name
[1] == '.'))) {
6871 * Check for irregular files and ._ files
6872 * If there is a ._._ file abort the op
6874 if ( dp
->d_namlen
< 2 ||
6875 strncmp(dp
->d_name
,"._",2) ||
6876 (dp
->d_namlen
>= 4 && !strncmp(&(dp
->d_name
[2]), "._",2))) {
6881 cpos
+= dp
->d_reclen
;
6882 dp
= (struct dirent
*)cpos
;
6886 * workaround for HFS/NFS setting eofflag before end of file
6888 if (vp
->v_tag
== VT_HFS
&& nentries
> 2)
6891 if (vp
->v_tag
== VT_NFS
) {
6892 if (eofflag
&& !full_erase_flag
) {
6893 full_erase_flag
= 1;
6895 uio_reset(auio
, 0, UIO_SYSSPACE
, UIO_READ
);
6897 else if (!eofflag
&& full_erase_flag
)
6898 full_erase_flag
= 0;
6903 * If we've made it here all the files in the dir are ._ files.
6904 * We can delete the files even though the node is suspended
6905 * because we are the owner of the file.
6908 uio_reset(auio
, 0, UIO_SYSSPACE
, UIO_READ
);
6910 full_erase_flag
= 0;
6913 siz
= UIO_BUFF_SIZE
;
6914 uio_reset(auio
, uio_offset(auio
), UIO_SYSSPACE
, UIO_READ
);
6915 uio_addiov(auio
, CAST_USER_ADDR_T(rbuf
), UIO_BUFF_SIZE
);
6917 error
= VNOP_READDIR(vp
, auio
, 0, &eofflag
, &nentries
, ctx
);
6922 if (uio_resid(auio
) != 0)
6923 siz
-= uio_resid(auio
);
6926 * Iterate through directory
6930 dp
= (struct dirent
*) cpos
;
6935 while ((cpos
< cend
)) {
6937 * Check for . and .. as well as directories
6939 if (dp
->d_ino
!= 0 &&
6940 !((dp
->d_namlen
== 1 && dp
->d_name
[0] == '.') ||
6941 (dp
->d_namlen
== 2 && dp
->d_name
[0] == '.' && dp
->d_name
[1] == '.'))
6944 NDINIT(&nd_temp
, DELETE
, USEDVP
, UIO_SYSSPACE
, CAST_USER_ADDR_T(dp
->d_name
), ctx
);
6945 nd_temp
.ni_dvp
= vp
;
6946 error
= unlink1(ctx
, &nd_temp
, 0);
6947 if (error
&& error
!= ENOENT
) {
6951 cpos
+= dp
->d_reclen
;
6952 dp
= (struct dirent
*)cpos
;
6956 * workaround for HFS/NFS setting eofflag before end of file
6958 if (vp
->v_tag
== VT_HFS
&& nentries
> 2)
6961 if (vp
->v_tag
== VT_NFS
) {
6962 if (eofflag
&& !full_erase_flag
) {
6963 full_erase_flag
= 1;
6965 uio_reset(auio
, 0, UIO_SYSSPACE
, UIO_READ
);
6967 else if (!eofflag
&& full_erase_flag
)
6968 full_erase_flag
= 0;
6978 VNOP_CLOSE(vp
, FREAD
, ctx
);
6992 lock_vnode_and_post(vnode_t vp
, int kevent_num
)
6994 /* Only take the lock if there's something there! */
6995 if (vp
->v_knotes
.slh_first
!= NULL
) {
6997 KNOTE(&vp
->v_knotes
, kevent_num
);
7003 static void record_vp(vnode_t vp
, int count
) {
7007 if ((vp
->v_flag
& VSYSTEM
))
7010 ut
= get_bsdthread_info(current_thread());
7011 ut
->uu_iocount
+= count
;
7013 if (ut
->uu_vpindex
< 32) {
7014 for (i
= 0; i
< ut
->uu_vpindex
; i
++) {
7015 if (ut
->uu_vps
[i
] == vp
)
7018 ut
->uu_vps
[ut
->uu_vpindex
] = vp
;