2 * Copyright (c) 2000-2014 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 <sys/systm.h>
109 #include <sys/kern_memorystatus.h>
110 #include <sys/lockf.h>
111 #include <miscfs/fifofs/fifo.h>
114 #include <machine/spl.h>
117 #include <kern/assert.h>
118 #include <mach/kern_return.h>
119 #include <kern/thread.h>
120 #include <kern/sched_prim.h>
122 #include <miscfs/specfs/specdev.h>
124 #include <mach/mach_types.h>
125 #include <mach/memory_object_types.h>
126 #include <mach/memory_object_control.h>
128 #include <kern/kalloc.h> /* kalloc()/kfree() */
129 #include <kern/clock.h> /* delay_for_interval() */
130 #include <libkern/OSAtomic.h> /* OSAddAtomic() */
131 #include <console/video_console.h>
134 #include <libkern/OSDebug.h>
137 #include <vm/vm_protos.h> /* vnode_pager_vrele() */
140 #include <security/mac_framework.h>
143 #define PANIC_PRINTS_VNODES
145 extern lck_grp_t
*vnode_lck_grp
;
146 extern lck_attr_t
*vnode_lck_attr
;
149 extern lck_grp_t
*trigger_vnode_lck_grp
;
150 extern lck_attr_t
*trigger_vnode_lck_attr
;
153 extern lck_mtx_t
* mnt_list_mtx_lock
;
155 enum vtype iftovt_tab
[16] = {
156 VNON
, VFIFO
, VCHR
, VNON
, VDIR
, VNON
, VBLK
, VNON
,
157 VREG
, VNON
, VLNK
, VNON
, VSOCK
, VNON
, VNON
, VBAD
,
159 int vttoif_tab
[9] = {
160 0, S_IFREG
, S_IFDIR
, S_IFBLK
, S_IFCHR
, S_IFLNK
,
161 S_IFSOCK
, S_IFIFO
, S_IFMT
,
165 /* XXX These should be in a BSD accessible Mach header, but aren't. */
166 extern void memory_object_mark_used(
167 memory_object_control_t control
);
169 extern void memory_object_mark_unused(
170 memory_object_control_t control
,
173 extern void memory_object_mark_io_tracking(
174 memory_object_control_t control
);
176 /* XXX next protptype should be from <nfs/nfs.h> */
177 extern int nfs_vinvalbuf(vnode_t
, int, vfs_context_t
, int);
179 /* XXX next prototytype should be from libsa/stdlib.h> but conflicts libkern */
180 __private_extern__
void qsort(
184 int (*)(const void *, const void *));
186 extern kern_return_t
adjust_vm_object_cache(vm_size_t oval
, vm_size_t nval
);
187 __private_extern__
void vntblinit(void);
188 __private_extern__ kern_return_t
reset_vmobjectcache(unsigned int val1
,
190 __private_extern__
int unlink1(vfs_context_t
, vnode_t
, user_addr_t
,
193 extern int system_inshutdown
;
195 static void vnode_list_add(vnode_t
);
196 static void vnode_async_list_add(vnode_t
);
197 static void vnode_list_remove(vnode_t
);
198 static void vnode_list_remove_locked(vnode_t
);
200 static void vnode_abort_advlocks(vnode_t
);
201 static errno_t
vnode_drain(vnode_t
);
202 static void vgone(vnode_t
, int flags
);
203 static void vclean(vnode_t vp
, int flag
);
204 static void vnode_reclaim_internal(vnode_t
, int, int, int);
206 static void vnode_dropiocount (vnode_t
);
208 static vnode_t
checkalias(vnode_t vp
, dev_t nvp_rdev
);
209 static int vnode_reload(vnode_t
);
210 static int vnode_isinuse_locked(vnode_t
, int, int);
212 static int unmount_callback(mount_t
, __unused
void *);
214 static void insmntque(vnode_t vp
, mount_t mp
);
215 static int mount_getvfscnt(void);
216 static int mount_fillfsids(fsid_t
*, int );
217 static void vnode_iterate_setup(mount_t
);
218 int vnode_umount_preflight(mount_t
, vnode_t
, int);
219 static int vnode_iterate_prepare(mount_t
);
220 static int vnode_iterate_reloadq(mount_t
);
221 static void vnode_iterate_clear(mount_t
);
222 static mount_t
vfs_getvfs_locked(fsid_t
*);
223 static int vn_create_reg(vnode_t dvp
, vnode_t
*vpp
, struct nameidata
*ndp
,
224 struct vnode_attr
*vap
, uint32_t flags
, int fmode
, uint32_t *statusp
, vfs_context_t ctx
);
225 static int vnode_authattr_new_internal(vnode_t dvp
, struct vnode_attr
*vap
, int noauth
, uint32_t *defaulted_fieldsp
, vfs_context_t ctx
);
227 errno_t
rmdir_remove_orphaned_appleDouble(vnode_t
, vfs_context_t
, int *);
230 static void record_vp(vnode_t vp
, int count
);
234 static int vnode_resolver_create(mount_t
, vnode_t
, struct vnode_trigger_param
*, boolean_t external
);
235 static void vnode_resolver_detach(vnode_t
);
238 TAILQ_HEAD(freelst
, vnode
) vnode_free_list
; /* vnode free list */
239 TAILQ_HEAD(deadlst
, vnode
) vnode_dead_list
; /* vnode dead list */
240 TAILQ_HEAD(async_work_lst
, vnode
) vnode_async_work_list
;
243 TAILQ_HEAD(ragelst
, vnode
) vnode_rage_list
; /* vnode rapid age list */
244 struct timeval rage_tv
;
248 #define RAGE_LIMIT_MIN 100
249 #define RAGE_TIME_LIMIT 5
251 struct mntlist mountlist
; /* mounted filesystem list */
252 static int nummounts
= 0;
255 #define VLISTCHECK(fun, vp, list) \
256 if ((vp)->v_freelist.tqe_prev == (struct vnode **)0xdeadb) \
257 panic("%s: %s vnode not on %slist", (fun), (list), (list));
259 #define VLISTCHECK(fun, vp, list)
260 #endif /* DIAGNOSTIC */
262 #define VLISTNONE(vp) \
264 (vp)->v_freelist.tqe_next = (struct vnode *)0; \
265 (vp)->v_freelist.tqe_prev = (struct vnode **)0xdeadb; \
268 #define VONLIST(vp) \
269 ((vp)->v_freelist.tqe_prev != (struct vnode **)0xdeadb)
271 /* remove a vnode from free vnode list */
272 #define VREMFREE(fun, vp) \
274 VLISTCHECK((fun), (vp), "free"); \
275 TAILQ_REMOVE(&vnode_free_list, (vp), v_freelist); \
281 /* remove a vnode from dead vnode list */
282 #define VREMDEAD(fun, vp) \
284 VLISTCHECK((fun), (vp), "dead"); \
285 TAILQ_REMOVE(&vnode_dead_list, (vp), v_freelist); \
287 vp->v_listflag &= ~VLIST_DEAD; \
292 /* remove a vnode from async work vnode list */
293 #define VREMASYNC_WORK(fun, vp) \
295 VLISTCHECK((fun), (vp), "async_work"); \
296 TAILQ_REMOVE(&vnode_async_work_list, (vp), v_freelist); \
298 vp->v_listflag &= ~VLIST_ASYNC_WORK; \
299 async_work_vnodes--; \
303 /* remove a vnode from rage vnode list */
304 #define VREMRAGE(fun, vp) \
306 if ( !(vp->v_listflag & VLIST_RAGE)) \
307 panic("VREMRAGE: vp not on rage list"); \
308 VLISTCHECK((fun), (vp), "rage"); \
309 TAILQ_REMOVE(&vnode_rage_list, (vp), v_freelist); \
311 vp->v_listflag &= ~VLIST_RAGE; \
317 * vnodetarget hasn't been used in a long time, but
318 * it was exported for some reason... I'm leaving in
319 * place for now... it should be deprecated out of the
320 * exports and removed eventually.
322 u_int32_t vnodetarget
; /* target for vnreclaim() */
323 #define VNODE_FREE_TARGET 20 /* Default value for vnodetarget */
326 * We need quite a few vnodes on the free list to sustain the
327 * rapid stat() the compilation process does, and still benefit from the name
328 * cache. Having too few vnodes on the free list causes serious disk
329 * thrashing as we cycle through them.
331 #define VNODE_FREE_MIN CONFIG_VNODE_FREE_MIN /* freelist should have at least this many */
334 static void async_work_continue(void);
337 * Initialize the vnode management data structures.
339 __private_extern__
void
342 thread_t thread
= THREAD_NULL
;
344 TAILQ_INIT(&vnode_free_list
);
345 TAILQ_INIT(&vnode_rage_list
);
346 TAILQ_INIT(&vnode_dead_list
);
347 TAILQ_INIT(&vnode_async_work_list
);
348 TAILQ_INIT(&mountlist
);
351 vnodetarget
= VNODE_FREE_TARGET
;
353 microuptime(&rage_tv
);
354 rage_limit
= desiredvnodes
/ 100;
356 if (rage_limit
< RAGE_LIMIT_MIN
)
357 rage_limit
= RAGE_LIMIT_MIN
;
360 * Scale the vm_object_cache to accomodate the vnodes
363 (void) adjust_vm_object_cache(0, desiredvnodes
- VNODE_FREE_MIN
);
366 * create worker threads
368 kernel_thread_start((thread_continue_t
)async_work_continue
, NULL
, &thread
);
369 thread_deallocate(thread
);
372 /* Reset the VM Object Cache with the values passed in */
373 __private_extern__ kern_return_t
374 reset_vmobjectcache(unsigned int val1
, unsigned int val2
)
376 vm_size_t oval
= val1
- VNODE_FREE_MIN
;
383 if(val2
< VNODE_FREE_MIN
)
386 nval
= val2
- VNODE_FREE_MIN
;
388 return(adjust_vm_object_cache(oval
, nval
));
392 /* the timeout is in 10 msecs */
394 vnode_waitforwrites(vnode_t vp
, int output_target
, int slpflag
, int slptimeout
, const char *msg
) {
398 KERNEL_DEBUG(0x3010280 | DBG_FUNC_START
, (int)vp
, output_target
, vp
->v_numoutput
, 0, 0);
400 if (vp
->v_numoutput
> output_target
) {
406 while ((vp
->v_numoutput
> output_target
) && error
== 0) {
408 vp
->v_flag
|= VTHROTTLED
;
410 vp
->v_flag
|= VBWAIT
;
412 ts
.tv_sec
= (slptimeout
/100);
413 ts
.tv_nsec
= (slptimeout
% 1000) * 10 * NSEC_PER_USEC
* 1000 ;
414 error
= msleep((caddr_t
)&vp
->v_numoutput
, &vp
->v_lock
, (slpflag
| (PRIBIO
+ 1)), msg
, &ts
);
420 KERNEL_DEBUG(0x3010280 | DBG_FUNC_END
, (int)vp
, output_target
, vp
->v_numoutput
, error
, 0);
427 vnode_startwrite(vnode_t vp
) {
429 OSAddAtomic(1, &vp
->v_numoutput
);
434 vnode_writedone(vnode_t vp
)
439 OSAddAtomic(-1, &vp
->v_numoutput
);
443 if (vp
->v_numoutput
< 0)
444 panic("vnode_writedone: numoutput < 0");
446 if ((vp
->v_flag
& VTHROTTLED
)) {
447 vp
->v_flag
&= ~VTHROTTLED
;
450 if ((vp
->v_flag
& VBWAIT
) && (vp
->v_numoutput
== 0)) {
451 vp
->v_flag
&= ~VBWAIT
;
457 wakeup((caddr_t
)&vp
->v_numoutput
);
464 vnode_hasdirtyblks(vnode_t vp
)
466 struct cl_writebehind
*wbp
;
469 * Not taking the buf_mtxp as there is little
470 * point doing it. Even if the lock is taken the
471 * state can change right after that. If their
472 * needs to be a synchronization, it must be driven
475 if (vp
->v_dirtyblkhd
.lh_first
)
478 if (!UBCINFOEXISTS(vp
))
481 wbp
= vp
->v_ubcinfo
->cl_wbehind
;
483 if (wbp
&& (wbp
->cl_number
|| wbp
->cl_scmap
))
490 vnode_hascleanblks(vnode_t vp
)
493 * Not taking the buf_mtxp as there is little
494 * point doing it. Even if the lock is taken the
495 * state can change right after that. If their
496 * needs to be a synchronization, it must be driven
499 if (vp
->v_cleanblkhd
.lh_first
)
505 vnode_iterate_setup(mount_t mp
)
507 while (mp
->mnt_lflag
& MNT_LITER
) {
508 mp
->mnt_lflag
|= MNT_LITERWAIT
;
509 msleep((caddr_t
)mp
, &mp
->mnt_mlock
, PVFS
, "vnode_iterate_setup", NULL
);
512 mp
->mnt_lflag
|= MNT_LITER
;
517 vnode_umount_preflight(mount_t mp
, vnode_t skipvp
, int flags
)
521 TAILQ_FOREACH(vp
, &mp
->mnt_vnodelist
, v_mntvnodes
) {
522 if (vp
->v_type
== VDIR
)
526 if ((flags
& SKIPSYSTEM
) && ((vp
->v_flag
& VSYSTEM
) || (vp
->v_flag
& VNOFLUSH
)))
528 if ((flags
& SKIPSWAP
) && (vp
->v_flag
& VSWAP
))
530 if ((flags
& WRITECLOSE
) && (vp
->v_writecount
== 0 || vp
->v_type
!= VREG
))
533 /* Look for busy vnode */
534 if ((vp
->v_usecount
!= 0) && ((vp
->v_usecount
- vp
->v_kusecount
) != 0)) {
537 } else if (vp
->v_iocount
> 0) {
538 /* Busy if iocount is > 0 for more than 3 seconds */
539 tsleep(&vp
->v_iocount
, PVFS
, "vnode_drain_network", 3 * hz
);
540 if (vp
->v_iocount
> 0)
550 * This routine prepares iteration by moving all the vnodes to worker queue
551 * called with mount lock held
554 vnode_iterate_prepare(mount_t mp
)
558 if (TAILQ_EMPTY(&mp
->mnt_vnodelist
)) {
563 vp
= TAILQ_FIRST(&mp
->mnt_vnodelist
);
564 vp
->v_mntvnodes
.tqe_prev
= &(mp
->mnt_workerqueue
.tqh_first
);
565 mp
->mnt_workerqueue
.tqh_first
= mp
->mnt_vnodelist
.tqh_first
;
566 mp
->mnt_workerqueue
.tqh_last
= mp
->mnt_vnodelist
.tqh_last
;
568 TAILQ_INIT(&mp
->mnt_vnodelist
);
569 if (mp
->mnt_newvnodes
.tqh_first
!= NULL
)
570 panic("vnode_iterate_prepare: newvnode when entering vnode");
571 TAILQ_INIT(&mp
->mnt_newvnodes
);
577 /* called with mount lock held */
579 vnode_iterate_reloadq(mount_t mp
)
583 /* add the remaining entries in workerq to the end of mount vnode list */
584 if (!TAILQ_EMPTY(&mp
->mnt_workerqueue
)) {
586 mvp
= TAILQ_LAST(&mp
->mnt_vnodelist
, vnodelst
);
588 /* Joining the workerque entities to mount vnode list */
590 mvp
->v_mntvnodes
.tqe_next
= mp
->mnt_workerqueue
.tqh_first
;
592 mp
->mnt_vnodelist
.tqh_first
= mp
->mnt_workerqueue
.tqh_first
;
593 mp
->mnt_workerqueue
.tqh_first
->v_mntvnodes
.tqe_prev
= mp
->mnt_vnodelist
.tqh_last
;
594 mp
->mnt_vnodelist
.tqh_last
= mp
->mnt_workerqueue
.tqh_last
;
595 TAILQ_INIT(&mp
->mnt_workerqueue
);
598 /* add the newvnodes to the head of mount vnode list */
599 if (!TAILQ_EMPTY(&mp
->mnt_newvnodes
)) {
601 nlvp
= TAILQ_LAST(&mp
->mnt_newvnodes
, vnodelst
);
603 mp
->mnt_newvnodes
.tqh_first
->v_mntvnodes
.tqe_prev
= &mp
->mnt_vnodelist
.tqh_first
;
604 nlvp
->v_mntvnodes
.tqe_next
= mp
->mnt_vnodelist
.tqh_first
;
605 if(mp
->mnt_vnodelist
.tqh_first
)
606 mp
->mnt_vnodelist
.tqh_first
->v_mntvnodes
.tqe_prev
= &nlvp
->v_mntvnodes
.tqe_next
;
608 mp
->mnt_vnodelist
.tqh_last
= mp
->mnt_newvnodes
.tqh_last
;
609 mp
->mnt_vnodelist
.tqh_first
= mp
->mnt_newvnodes
.tqh_first
;
610 TAILQ_INIT(&mp
->mnt_newvnodes
);
619 vnode_iterate_clear(mount_t mp
)
621 mp
->mnt_lflag
&= ~MNT_LITER
;
622 if (mp
->mnt_lflag
& MNT_LITERWAIT
) {
623 mp
->mnt_lflag
&= ~MNT_LITERWAIT
;
629 #include <i386/panic_hooks.h>
631 struct vnode_iterate_panic_hook
{
637 static void vnode_iterate_panic_hook(panic_hook_t
*hook_
)
639 extern int kdb_log(const char *fmt
, ...);
640 struct vnode_iterate_panic_hook
*hook
= (struct vnode_iterate_panic_hook
*)hook_
;
641 panic_phys_range_t range
;
644 if (panic_phys_range_before(hook
->mp
, &phys
, &range
)) {
645 kdb_log("mp = %p, phys = %p, prev (%p: %p-%p)\n",
646 hook
->mp
, phys
, range
.type
, range
.phys_start
,
647 range
.phys_start
+ range
.len
);
649 kdb_log("mp = %p, phys = %p, prev (!)\n", hook
->mp
, phys
);
652 if (panic_phys_range_before(hook
->vp
, &phys
, &range
)) {
653 kdb_log("vp = %p, phys = %p, prev (%p: %p-%p)\n",
654 hook
->mp
, phys
, range
.type
, range
.phys_start
,
655 range
.phys_start
+ range
.len
);
657 kdb_log("vp = %p, phys = %p, prev (!)\n", hook
->vp
, phys
);
659 panic_dump_mem((void *)(((vm_offset_t
)hook
->mp
-4096) & ~4095), 12288);
663 vnode_iterate(mount_t mp
, int flags
, int (*callout
)(struct vnode
*, void *),
672 vnode_iterate_setup(mp
);
674 /* it is returns 0 then there is nothing to do */
675 retval
= vnode_iterate_prepare(mp
);
678 vnode_iterate_clear(mp
);
683 struct vnode_iterate_panic_hook hook
;
686 panic_hook(&hook
.hook
, vnode_iterate_panic_hook
);
687 /* iterate over all the vnodes */
688 while (!TAILQ_EMPTY(&mp
->mnt_workerqueue
)) {
689 vp
= TAILQ_FIRST(&mp
->mnt_workerqueue
);
691 TAILQ_REMOVE(&mp
->mnt_workerqueue
, vp
, v_mntvnodes
);
692 TAILQ_INSERT_TAIL(&mp
->mnt_vnodelist
, vp
, v_mntvnodes
);
694 if ((vp
->v_data
== NULL
) || (vp
->v_type
== VNON
) || (vp
->v_mount
!= mp
)) {
699 if ( vget_internal(vp
, vid
, (flags
| VNODE_NODEAD
| VNODE_WITHID
| VNODE_NOSUSPEND
))) {
703 if (flags
& VNODE_RELOAD
) {
705 * we're reloading the filesystem
706 * cast out any inactive vnodes...
708 if (vnode_reload(vp
)) {
709 /* vnode will be recycled on the refcount drop */
716 retval
= callout(vp
, arg
);
720 case VNODE_RETURNED_DONE
:
722 if (retval
== VNODE_RETURNED_DONE
) {
729 case VNODE_CLAIMED_DONE
:
741 panic_unhook(&hook
.hook
);
742 (void)vnode_iterate_reloadq(mp
);
743 vnode_iterate_clear(mp
);
749 mount_lock_renames(mount_t mp
)
751 lck_mtx_lock(&mp
->mnt_renamelock
);
755 mount_unlock_renames(mount_t mp
)
757 lck_mtx_unlock(&mp
->mnt_renamelock
);
761 mount_lock(mount_t mp
)
763 lck_mtx_lock(&mp
->mnt_mlock
);
767 mount_lock_spin(mount_t mp
)
769 lck_mtx_lock_spin(&mp
->mnt_mlock
);
773 mount_unlock(mount_t mp
)
775 lck_mtx_unlock(&mp
->mnt_mlock
);
780 mount_ref(mount_t mp
, int locked
)
793 mount_drop(mount_t mp
, int locked
)
800 if (mp
->mnt_count
== 0 && (mp
->mnt_lflag
& MNT_LDRAIN
))
801 wakeup(&mp
->mnt_lflag
);
809 mount_iterref(mount_t mp
, int locked
)
815 if (mp
->mnt_iterref
< 0) {
826 mount_isdrained(mount_t mp
, int locked
)
832 if (mp
->mnt_iterref
< 0)
842 mount_iterdrop(mount_t mp
)
846 wakeup(&mp
->mnt_iterref
);
851 mount_iterdrain(mount_t mp
)
854 while (mp
->mnt_iterref
)
855 msleep((caddr_t
)&mp
->mnt_iterref
, mnt_list_mtx_lock
, PVFS
, "mount_iterdrain", NULL
);
856 /* mount iterations drained */
857 mp
->mnt_iterref
= -1;
861 mount_iterreset(mount_t mp
)
864 if (mp
->mnt_iterref
== -1)
869 /* always called with mount lock held */
871 mount_refdrain(mount_t mp
)
873 if (mp
->mnt_lflag
& MNT_LDRAIN
)
874 panic("already in drain");
875 mp
->mnt_lflag
|= MNT_LDRAIN
;
877 while (mp
->mnt_count
)
878 msleep((caddr_t
)&mp
->mnt_lflag
, &mp
->mnt_mlock
, PVFS
, "mount_drain", NULL
);
880 if (mp
->mnt_vnodelist
.tqh_first
!= NULL
)
881 panic("mount_refdrain: dangling vnode");
883 mp
->mnt_lflag
&= ~MNT_LDRAIN
;
888 /* Tags the mount point as not supportine extended readdir for NFS exports */
890 mount_set_noreaddirext(mount_t mp
) {
892 mp
->mnt_kern_flag
|= MNTK_DENY_READDIREXT
;
897 * Mark a mount point as busy. Used to synchronize access and to delay
901 vfs_busy(mount_t mp
, int flags
)
905 if (mp
->mnt_lflag
& MNT_LDEAD
)
910 if (mp
->mnt_lflag
& MNT_LUNMOUNT
) {
911 if (flags
& LK_NOWAIT
|| mp
->mnt_lflag
& MNT_LDEAD
) {
917 * Since all busy locks are shared except the exclusive
918 * lock granted when unmounting, the only place that a
919 * wakeup needs to be done is at the release of the
920 * exclusive lock at the end of dounmount.
922 mp
->mnt_lflag
|= MNT_LWAIT
;
923 msleep((caddr_t
)mp
, &mp
->mnt_mlock
, (PVFS
| PDROP
), "vfsbusy", NULL
);
929 lck_rw_lock_shared(&mp
->mnt_rwlock
);
932 * Until we are granted the rwlock, it's possible for the mount point to
933 * change state, so re-evaluate before granting the vfs_busy.
935 if (mp
->mnt_lflag
& (MNT_LDEAD
| MNT_LUNMOUNT
)) {
936 lck_rw_done(&mp
->mnt_rwlock
);
943 * Free a busy filesystem.
946 vfs_unbusy(mount_t mp
)
948 lck_rw_done(&mp
->mnt_rwlock
);
954 vfs_rootmountfailed(mount_t mp
) {
957 mp
->mnt_vtable
->vfc_refcount
--;
962 mount_lock_destroy(mp
);
965 mac_mount_label_destroy(mp
);
968 FREE_ZONE(mp
, sizeof(struct mount
), M_MOUNT
);
972 * Lookup a filesystem type, and if found allocate and initialize
973 * a mount structure for it.
975 * Devname is usually updated by mount(8) after booting.
978 vfs_rootmountalloc_internal(struct vfstable
*vfsp
, const char *devname
)
982 mp
= _MALLOC_ZONE(sizeof(struct mount
), M_MOUNT
, M_WAITOK
);
983 bzero((char *)mp
, sizeof(struct mount
));
985 /* Initialize the default IO constraints */
986 mp
->mnt_maxreadcnt
= mp
->mnt_maxwritecnt
= MAXPHYS
;
987 mp
->mnt_segreadcnt
= mp
->mnt_segwritecnt
= 32;
988 mp
->mnt_maxsegreadsize
= mp
->mnt_maxreadcnt
;
989 mp
->mnt_maxsegwritesize
= mp
->mnt_maxwritecnt
;
990 mp
->mnt_devblocksize
= DEV_BSIZE
;
991 mp
->mnt_alignmentmask
= PAGE_MASK
;
992 mp
->mnt_ioqueue_depth
= MNT_DEFAULT_IOQUEUE_DEPTH
;
995 mp
->mnt_realrootvp
= NULLVP
;
996 mp
->mnt_authcache_ttl
= CACHED_LOOKUP_RIGHT_TTL
;
997 mp
->mnt_throttle_mask
= LOWPRI_MAX_NUM_DEV
- 1;
998 mp
->mnt_devbsdunit
= 0;
1000 mount_lock_init(mp
);
1001 (void)vfs_busy(mp
, LK_NOWAIT
);
1003 TAILQ_INIT(&mp
->mnt_vnodelist
);
1004 TAILQ_INIT(&mp
->mnt_workerqueue
);
1005 TAILQ_INIT(&mp
->mnt_newvnodes
);
1007 mp
->mnt_vtable
= vfsp
;
1008 mp
->mnt_op
= vfsp
->vfc_vfsops
;
1009 mp
->mnt_flag
= MNT_RDONLY
| MNT_ROOTFS
;
1010 mp
->mnt_vnodecovered
= NULLVP
;
1011 //mp->mnt_stat.f_type = vfsp->vfc_typenum;
1012 mp
->mnt_flag
|= vfsp
->vfc_flags
& MNT_VISFLAGMASK
;
1015 vfsp
->vfc_refcount
++;
1016 mount_list_unlock();
1018 strlcpy(mp
->mnt_vfsstat
.f_fstypename
, vfsp
->vfc_name
, MFSTYPENAMELEN
);
1019 mp
->mnt_vfsstat
.f_mntonname
[0] = '/';
1020 /* XXX const poisoning layering violation */
1021 (void) copystr((const void *)devname
, mp
->mnt_vfsstat
.f_mntfromname
, MAXPATHLEN
- 1, NULL
);
1024 mac_mount_label_init(mp
);
1025 mac_mount_label_associate(vfs_context_kernel(), mp
);
1031 vfs_rootmountalloc(const char *fstypename
, const char *devname
, mount_t
*mpp
)
1033 struct vfstable
*vfsp
;
1035 for (vfsp
= vfsconf
; vfsp
; vfsp
= vfsp
->vfc_next
)
1036 if (!strncmp(vfsp
->vfc_name
, fstypename
,
1037 sizeof(vfsp
->vfc_name
)))
1042 *mpp
= vfs_rootmountalloc_internal(vfsp
, devname
);
1052 * Find an appropriate filesystem to use for the root. If a filesystem
1053 * has not been preselected, walk through the list of known filesystems
1054 * trying those that have mountroot routines, and try them until one
1055 * works or we have tried them all.
1057 extern int (*mountroot
)(void);
1065 struct vfstable
*vfsp
;
1066 vfs_context_t ctx
= vfs_context_kernel();
1067 struct vfs_attr vfsattr
;
1070 vnode_t bdevvp_rootvp
;
1072 if (mountroot
!= NULL
) {
1074 * used for netboot which follows a different set of rules
1076 error
= (*mountroot
)();
1079 if ((error
= bdevvp(rootdev
, &rootvp
))) {
1080 printf("vfs_mountroot: can't setup bdevvp\n");
1084 * 4951998 - code we call in vfc_mountroot may replace rootvp
1085 * so keep a local copy for some house keeping.
1087 bdevvp_rootvp
= rootvp
;
1089 for (vfsp
= vfsconf
; vfsp
; vfsp
= vfsp
->vfc_next
) {
1090 if (vfsp
->vfc_mountroot
== NULL
)
1093 mp
= vfs_rootmountalloc_internal(vfsp
, "root_device");
1094 mp
->mnt_devvp
= rootvp
;
1096 if ((error
= (*vfsp
->vfc_mountroot
)(mp
, rootvp
, ctx
)) == 0) {
1097 if ( bdevvp_rootvp
!= rootvp
) {
1100 * bump the iocount and fix up mnt_devvp for the
1101 * new rootvp (it will already have a usecount taken)...
1102 * drop the iocount and the usecount on the orignal
1103 * since we are no longer going to use it...
1105 vnode_getwithref(rootvp
);
1106 mp
->mnt_devvp
= rootvp
;
1108 vnode_rele(bdevvp_rootvp
);
1109 vnode_put(bdevvp_rootvp
);
1111 mp
->mnt_devvp
->v_specflags
|= SI_MOUNTEDON
;
1118 * cache the IO attributes for the underlying physical media...
1119 * an error return indicates the underlying driver doesn't
1120 * support all the queries necessary... however, reasonable
1121 * defaults will have been set, so no reason to bail or care
1123 vfs_init_io_attributes(rootvp
, mp
);
1126 * Shadow the VFC_VFSNATIVEXATTR flag to MNTK_EXTENDED_ATTRS.
1128 if (mp
->mnt_vtable
->vfc_vfsflags
& VFC_VFSNATIVEXATTR
) {
1129 mp
->mnt_kern_flag
|= MNTK_EXTENDED_ATTRS
;
1131 if (mp
->mnt_vtable
->vfc_vfsflags
& VFC_VFSPREFLIGHT
) {
1132 mp
->mnt_kern_flag
|= MNTK_UNMOUNT_PREFLIGHT
;
1137 if (MNTK_VIRTUALDEV
& mp
->mnt_kern_flag
) speed
= 128;
1138 else if (MNTK_SSD
& mp
->mnt_kern_flag
) speed
= 7*256;
1140 vc_progress_setdiskspeed(speed
);
1142 * Probe root file system for additional features.
1144 (void)VFS_START(mp
, 0, ctx
);
1146 VFSATTR_INIT(&vfsattr
);
1147 VFSATTR_WANTED(&vfsattr
, f_capabilities
);
1148 if (vfs_getattr(mp
, &vfsattr
, ctx
) == 0 &&
1149 VFSATTR_IS_SUPPORTED(&vfsattr
, f_capabilities
)) {
1150 if ((vfsattr
.f_capabilities
.capabilities
[VOL_CAPABILITIES_INTERFACES
] & VOL_CAP_INT_EXTENDED_ATTR
) &&
1151 (vfsattr
.f_capabilities
.valid
[VOL_CAPABILITIES_INTERFACES
] & VOL_CAP_INT_EXTENDED_ATTR
)) {
1152 mp
->mnt_kern_flag
|= MNTK_EXTENDED_ATTRS
;
1155 if ((vfsattr
.f_capabilities
.capabilities
[VOL_CAPABILITIES_INTERFACES
] & VOL_CAP_INT_NAMEDSTREAMS
) &&
1156 (vfsattr
.f_capabilities
.valid
[VOL_CAPABILITIES_INTERFACES
] & VOL_CAP_INT_NAMEDSTREAMS
)) {
1157 mp
->mnt_kern_flag
|= MNTK_NAMED_STREAMS
;
1160 if ((vfsattr
.f_capabilities
.capabilities
[VOL_CAPABILITIES_FORMAT
] & VOL_CAP_FMT_PATH_FROM_ID
) &&
1161 (vfsattr
.f_capabilities
.valid
[VOL_CAPABILITIES_FORMAT
] & VOL_CAP_FMT_PATH_FROM_ID
)) {
1162 mp
->mnt_kern_flag
|= MNTK_PATH_FROM_ID
;
1167 * get rid of iocount reference returned
1168 * by bdevvp (or picked up by us on the substitued
1169 * rootvp)... it (or we) will have also taken
1170 * a usecount reference which we want to keep
1175 if ((vfs_flags(mp
) & MNT_MULTILABEL
) == 0)
1178 error
= VFS_ROOT(mp
, &vp
, ctx
);
1180 printf("%s() VFS_ROOT() returned %d\n",
1182 dounmount(mp
, MNT_FORCE
, 0, ctx
);
1185 error
= vnode_label(mp
, NULL
, vp
, NULL
, 0, ctx
);
1187 * get rid of reference provided by VFS_ROOT
1192 printf("%s() vnode_label() returned %d\n",
1194 dounmount(mp
, MNT_FORCE
, 0, ctx
);
1203 vfs_rootmountfailed(mp
);
1205 if (error
!= EINVAL
)
1206 printf("%s_mountroot failed: %d\n", vfsp
->vfc_name
, error
);
1212 * Lookup a mount point by filesystem identifier.
1216 vfs_getvfs(fsid_t
*fsid
)
1218 return (mount_list_lookupby_fsid(fsid
, 0, 0));
1221 static struct mount
*
1222 vfs_getvfs_locked(fsid_t
*fsid
)
1224 return(mount_list_lookupby_fsid(fsid
, 1, 0));
1228 vfs_getvfs_by_mntonname(char *path
)
1230 mount_t retmp
= (mount_t
)0;
1234 TAILQ_FOREACH(mp
, &mountlist
, mnt_list
) {
1235 if (!strncmp(mp
->mnt_vfsstat
.f_mntonname
, path
,
1236 sizeof(mp
->mnt_vfsstat
.f_mntonname
))) {
1238 if (mount_iterref(retmp
, 1))
1244 mount_list_unlock();
1248 /* generation number for creation of new fsids */
1249 u_short mntid_gen
= 0;
1251 * Get a new unique fsid
1254 vfs_getnewfsid(struct mount
*mp
)
1263 /* generate a new fsid */
1264 mtype
= mp
->mnt_vtable
->vfc_typenum
;
1265 if (++mntid_gen
== 0)
1267 tfsid
.val
[0] = makedev(nblkdev
+ mtype
, mntid_gen
);
1268 tfsid
.val
[1] = mtype
;
1270 TAILQ_FOREACH(nmp
, &mountlist
, mnt_list
) {
1271 while (vfs_getvfs_locked(&tfsid
)) {
1272 if (++mntid_gen
== 0)
1274 tfsid
.val
[0] = makedev(nblkdev
+ mtype
, mntid_gen
);
1277 mp
->mnt_vfsstat
.f_fsid
.val
[0] = tfsid
.val
[0];
1278 mp
->mnt_vfsstat
.f_fsid
.val
[1] = tfsid
.val
[1];
1279 mount_list_unlock();
1283 * Routines having to do with the management of the vnode table.
1285 extern int (**dead_vnodeop_p
)(void *);
1286 long numvnodes
, freevnodes
, deadvnodes
, async_work_vnodes
;
1289 int async_work_timed_out
= 0;
1290 int async_work_handled
= 0;
1291 int dead_vnode_wanted
= 0;
1292 int dead_vnode_waited
= 0;
1295 * Move a vnode from one mount queue to another.
1298 insmntque(vnode_t vp
, mount_t mp
)
1302 * Delete from old mount point vnode list, if on one.
1304 if ( (lmp
= vp
->v_mount
) != NULL
&& lmp
!= dead_mountp
) {
1305 if ((vp
->v_lflag
& VNAMED_MOUNT
) == 0)
1306 panic("insmntque: vp not in mount vnode list");
1307 vp
->v_lflag
&= ~VNAMED_MOUNT
;
1309 mount_lock_spin(lmp
);
1313 if (vp
->v_mntvnodes
.tqe_next
== NULL
) {
1314 if (TAILQ_LAST(&lmp
->mnt_vnodelist
, vnodelst
) == vp
)
1315 TAILQ_REMOVE(&lmp
->mnt_vnodelist
, vp
, v_mntvnodes
);
1316 else if (TAILQ_LAST(&lmp
->mnt_newvnodes
, vnodelst
) == vp
)
1317 TAILQ_REMOVE(&lmp
->mnt_newvnodes
, vp
, v_mntvnodes
);
1318 else if (TAILQ_LAST(&lmp
->mnt_workerqueue
, vnodelst
) == vp
)
1319 TAILQ_REMOVE(&lmp
->mnt_workerqueue
, vp
, v_mntvnodes
);
1321 vp
->v_mntvnodes
.tqe_next
->v_mntvnodes
.tqe_prev
= vp
->v_mntvnodes
.tqe_prev
;
1322 *vp
->v_mntvnodes
.tqe_prev
= vp
->v_mntvnodes
.tqe_next
;
1324 vp
->v_mntvnodes
.tqe_next
= NULL
;
1325 vp
->v_mntvnodes
.tqe_prev
= NULL
;
1331 * Insert into list of vnodes for the new mount point, if available.
1333 if ((vp
->v_mount
= mp
) != NULL
) {
1334 mount_lock_spin(mp
);
1335 if ((vp
->v_mntvnodes
.tqe_next
!= 0) && (vp
->v_mntvnodes
.tqe_prev
!= 0))
1336 panic("vp already in mount list");
1337 if (mp
->mnt_lflag
& MNT_LITER
)
1338 TAILQ_INSERT_HEAD(&mp
->mnt_newvnodes
, vp
, v_mntvnodes
);
1340 TAILQ_INSERT_HEAD(&mp
->mnt_vnodelist
, vp
, v_mntvnodes
);
1341 if (vp
->v_lflag
& VNAMED_MOUNT
)
1342 panic("insmntque: vp already in mount vnode list");
1343 vp
->v_lflag
|= VNAMED_MOUNT
;
1351 * Create a vnode for a block device.
1352 * Used for root filesystem, argdev, and swap areas.
1353 * Also used for memory file system special devices.
1356 bdevvp(dev_t dev
, vnode_t
*vpp
)
1360 struct vnode_fsparam vfsp
;
1361 struct vfs_context context
;
1368 context
.vc_thread
= current_thread();
1369 context
.vc_ucred
= FSCRED
;
1371 vfsp
.vnfs_mp
= (struct mount
*)0;
1372 vfsp
.vnfs_vtype
= VBLK
;
1373 vfsp
.vnfs_str
= "bdevvp";
1374 vfsp
.vnfs_dvp
= NULL
;
1375 vfsp
.vnfs_fsnode
= NULL
;
1376 vfsp
.vnfs_cnp
= NULL
;
1377 vfsp
.vnfs_vops
= spec_vnodeop_p
;
1378 vfsp
.vnfs_rdev
= dev
;
1379 vfsp
.vnfs_filesize
= 0;
1381 vfsp
.vnfs_flags
= VNFS_NOCACHE
| VNFS_CANTCACHE
;
1383 vfsp
.vnfs_marksystem
= 0;
1384 vfsp
.vnfs_markroot
= 0;
1386 if ( (error
= vnode_create(VNCREATE_FLAVOR
, VCREATESIZE
, &vfsp
, &nvp
)) ) {
1390 vnode_lock_spin(nvp
);
1391 nvp
->v_flag
|= VBDEVVP
;
1392 nvp
->v_tag
= VT_NON
; /* set this to VT_NON so during aliasing it can be replaced */
1394 if ( (error
= vnode_ref(nvp
)) ) {
1395 panic("bdevvp failed: vnode_ref");
1398 if ( (error
= VNOP_FSYNC(nvp
, MNT_WAIT
, &context
)) ) {
1399 panic("bdevvp failed: fsync");
1402 if ( (error
= buf_invalidateblks(nvp
, BUF_WRITE_DATA
, 0, 0)) ) {
1403 panic("bdevvp failed: invalidateblks");
1409 * XXXMAC: We can't put a MAC check here, the system will
1410 * panic without this vnode.
1414 if ( (error
= VNOP_OPEN(nvp
, FREAD
, &context
)) ) {
1415 panic("bdevvp failed: open");
1425 * Check to see if the new vnode represents a special device
1426 * for which we already have a vnode (either because of
1427 * bdevvp() or because of a different vnode representing
1428 * the same block device). If such an alias exists, deallocate
1429 * the existing contents and return the aliased vnode. The
1430 * caller is responsible for filling it with its new contents.
1433 checkalias(struct vnode
*nvp
, dev_t nvp_rdev
)
1437 struct specinfo
*sin
= NULL
;
1440 vpp
= &speclisth
[SPECHASH(nvp_rdev
)];
1444 for (vp
= *vpp
; vp
; vp
= vp
->v_specnext
) {
1445 if (nvp_rdev
== vp
->v_rdev
&& nvp
->v_type
== vp
->v_type
) {
1454 if (vnode_getwithvid(vp
,vid
)) {
1458 * Termination state is checked in vnode_getwithvid
1463 * Alias, but not in use, so flush it out.
1465 if ((vp
->v_iocount
== 1) && (vp
->v_usecount
== 0)) {
1466 vnode_reclaim_internal(vp
, 1, 1, 0);
1467 vnode_put_locked(vp
);
1473 if (vp
== NULL
|| vp
->v_tag
!= VT_NON
) {
1475 MALLOC_ZONE(sin
, struct specinfo
*, sizeof(struct specinfo
),
1476 M_SPECINFO
, M_WAITOK
);
1479 nvp
->v_specinfo
= sin
;
1480 bzero(nvp
->v_specinfo
, sizeof(struct specinfo
));
1481 nvp
->v_rdev
= nvp_rdev
;
1482 nvp
->v_specflags
= 0;
1483 nvp
->v_speclastr
= -1;
1484 nvp
->v_specinfo
->si_opencount
= 0;
1485 nvp
->v_specinfo
->si_initted
= 0;
1486 nvp
->v_specinfo
->si_throttleable
= 0;
1490 /* We dropped the lock, someone could have added */
1492 for (vp
= *vpp
; vp
; vp
= vp
->v_specnext
) {
1493 if (nvp_rdev
== vp
->v_rdev
&& nvp
->v_type
== vp
->v_type
) {
1501 nvp
->v_hashchain
= vpp
;
1502 nvp
->v_specnext
= *vpp
;
1506 nvp
->v_specflags
|= SI_ALIASED
;
1507 vp
->v_specflags
|= SI_ALIASED
;
1509 vnode_put_locked(vp
);
1519 FREE_ZONE(sin
, sizeof(struct specinfo
), M_SPECINFO
);
1522 if ((vp
->v_flag
& (VBDEVVP
| VDEVFLUSH
)) != 0)
1525 panic("checkalias with VT_NON vp that shouldn't: %p", vp
);
1532 * Get a reference on a particular vnode and lock it if requested.
1533 * If the vnode was on the inactive list, remove it from the list.
1534 * If the vnode was on the free list, remove it from the list and
1535 * move it to inactive list as needed.
1536 * The vnode lock bit is set if the vnode is being eliminated in
1537 * vgone. The process is awakened when the transition is completed,
1538 * and an error returned to indicate that the vnode is no longer
1539 * usable (possibly having been changed to a new file system type).
1542 vget_internal(vnode_t vp
, int vid
, int vflags
)
1546 vnode_lock_spin(vp
);
1548 if ((vflags
& VNODE_WRITEABLE
) && (vp
->v_writecount
== 0))
1550 * vnode to be returned only if it has writers opened
1554 error
= vnode_getiocount(vp
, vid
, vflags
);
1562 * Returns: 0 Success
1563 * ENOENT No such file or directory [terminating]
1566 vnode_ref(vnode_t vp
)
1569 return (vnode_ref_ext(vp
, 0, 0));
1573 * Returns: 0 Success
1574 * ENOENT No such file or directory [terminating]
1577 vnode_ref_ext(vnode_t vp
, int fmode
, int flags
)
1581 vnode_lock_spin(vp
);
1584 * once all the current call sites have been fixed to insure they have
1585 * taken an iocount, we can toughen this assert up and insist that the
1586 * iocount is non-zero... a non-zero usecount doesn't insure correctness
1588 if (vp
->v_iocount
<= 0 && vp
->v_usecount
<= 0)
1589 panic("vnode_ref_ext: vp %p has no valid reference %d, %d", vp
, vp
->v_iocount
, vp
->v_usecount
);
1592 * if you are the owner of drain/termination, can acquire usecount
1594 if ((flags
& VNODE_REF_FORCE
) == 0) {
1595 if ((vp
->v_lflag
& (VL_DRAIN
| VL_TERMINATE
| VL_DEAD
))) {
1596 if (vp
->v_owner
!= current_thread()) {
1604 if (fmode
& FWRITE
) {
1605 if (++vp
->v_writecount
<= 0)
1606 panic("vnode_ref_ext: v_writecount");
1608 if (fmode
& O_EVTONLY
) {
1609 if (++vp
->v_kusecount
<= 0)
1610 panic("vnode_ref_ext: v_kusecount");
1612 if (vp
->v_flag
& VRAGE
) {
1615 ut
= get_bsdthread_info(current_thread());
1617 if ( !(current_proc()->p_lflag
& P_LRAGE_VNODES
) &&
1618 !(ut
->uu_flag
& UT_RAGE_VNODES
)) {
1620 * a 'normal' process accessed this vnode
1621 * so make sure its no longer marked
1622 * for rapid aging... also, make sure
1623 * it gets removed from the rage list...
1624 * when v_usecount drops back to 0, it
1625 * will be put back on the real free list
1627 vp
->v_flag
&= ~VRAGE
;
1628 vp
->v_references
= 0;
1629 vnode_list_remove(vp
);
1632 if (vp
->v_usecount
== 1 && vp
->v_type
== VREG
&& !(vp
->v_flag
& VSYSTEM
)) {
1634 if (vp
->v_ubcinfo
) {
1635 vnode_lock_convert(vp
);
1636 memory_object_mark_used(vp
->v_ubcinfo
->ui_control
);
1647 vnode_on_reliable_media(vnode_t vp
)
1649 if ( !(vp
->v_mount
->mnt_kern_flag
& MNTK_VIRTUALDEV
) && (vp
->v_mount
->mnt_flag
& MNT_LOCAL
) )
1655 vnode_async_list_add(vnode_t vp
)
1659 if (VONLIST(vp
) || (vp
->v_lflag
& (VL_TERMINATE
|VL_DEAD
)))
1660 panic("vnode_async_list_add: %p is in wrong state", vp
);
1662 TAILQ_INSERT_HEAD(&vnode_async_work_list
, vp
, v_freelist
);
1663 vp
->v_listflag
|= VLIST_ASYNC_WORK
;
1665 async_work_vnodes
++;
1667 vnode_list_unlock();
1669 wakeup(&vnode_async_work_list
);
1675 * put the vnode on appropriate free list.
1676 * called with vnode LOCKED
1679 vnode_list_add(vnode_t vp
)
1681 boolean_t need_dead_wakeup
= FALSE
;
1684 lck_mtx_assert(&vp
->v_lock
, LCK_MTX_ASSERT_OWNED
);
1687 * if it is already on a list or non zero references return
1689 if (VONLIST(vp
) || (vp
->v_usecount
!= 0) || (vp
->v_iocount
!= 0) || (vp
->v_lflag
& VL_TERMINATE
))
1694 if ((vp
->v_flag
& VRAGE
) && !(vp
->v_lflag
& VL_DEAD
)) {
1696 * add the new guy to the appropriate end of the RAGE list
1698 if ((vp
->v_flag
& VAGE
))
1699 TAILQ_INSERT_HEAD(&vnode_rage_list
, vp
, v_freelist
);
1701 TAILQ_INSERT_TAIL(&vnode_rage_list
, vp
, v_freelist
);
1703 vp
->v_listflag
|= VLIST_RAGE
;
1707 * reset the timestamp for the last inserted vp on the RAGE
1708 * queue to let new_vnode know that its not ok to start stealing
1709 * from this list... as long as we're actively adding to this list
1710 * we'll push out the vnodes we want to donate to the real free list
1711 * once we stop pushing, we'll let some time elapse before we start
1712 * stealing them in the new_vnode routine
1714 microuptime(&rage_tv
);
1717 * if VL_DEAD, insert it at head of the dead list
1718 * else insert at tail of LRU list or at head if VAGE is set
1720 if ( (vp
->v_lflag
& VL_DEAD
)) {
1721 TAILQ_INSERT_HEAD(&vnode_dead_list
, vp
, v_freelist
);
1722 vp
->v_listflag
|= VLIST_DEAD
;
1725 if (dead_vnode_wanted
) {
1726 dead_vnode_wanted
--;
1727 need_dead_wakeup
= TRUE
;
1730 } else if ( (vp
->v_flag
& VAGE
) ) {
1731 TAILQ_INSERT_HEAD(&vnode_free_list
, vp
, v_freelist
);
1732 vp
->v_flag
&= ~VAGE
;
1735 TAILQ_INSERT_TAIL(&vnode_free_list
, vp
, v_freelist
);
1739 vnode_list_unlock();
1741 if (need_dead_wakeup
== TRUE
)
1742 wakeup_one((caddr_t
)&dead_vnode_wanted
);
1747 * remove the vnode from appropriate free list.
1748 * called with vnode LOCKED and
1749 * the list lock held
1752 vnode_list_remove_locked(vnode_t vp
)
1756 * the v_listflag field is
1757 * protected by the vnode_list_lock
1759 if (vp
->v_listflag
& VLIST_RAGE
)
1760 VREMRAGE("vnode_list_remove", vp
);
1761 else if (vp
->v_listflag
& VLIST_DEAD
)
1762 VREMDEAD("vnode_list_remove", vp
);
1763 else if (vp
->v_listflag
& VLIST_ASYNC_WORK
)
1764 VREMASYNC_WORK("vnode_list_remove", vp
);
1766 VREMFREE("vnode_list_remove", vp
);
1772 * remove the vnode from appropriate free list.
1773 * called with vnode LOCKED
1776 vnode_list_remove(vnode_t vp
)
1779 lck_mtx_assert(&vp
->v_lock
, LCK_MTX_ASSERT_OWNED
);
1782 * we want to avoid taking the list lock
1783 * in the case where we're not on the free
1784 * list... this will be true for most
1785 * directories and any currently in use files
1787 * we're guaranteed that we can't go from
1788 * the not-on-list state to the on-list
1789 * state since we hold the vnode lock...
1790 * all calls to vnode_list_add are done
1791 * under the vnode lock... so we can
1792 * check for that condition (the prevelant one)
1793 * without taking the list lock
1798 * however, we're not guaranteed that
1799 * we won't go from the on-list state
1800 * to the not-on-list state until we
1801 * hold the vnode_list_lock... this
1802 * is due to "new_vnode" removing vnodes
1803 * from the free list uder the list_lock
1804 * w/o the vnode lock... so we need to
1805 * check again whether we're currently
1808 vnode_list_remove_locked(vp
);
1810 vnode_list_unlock();
1816 vnode_rele(vnode_t vp
)
1818 vnode_rele_internal(vp
, 0, 0, 0);
1823 vnode_rele_ext(vnode_t vp
, int fmode
, int dont_reenter
)
1825 vnode_rele_internal(vp
, fmode
, dont_reenter
, 0);
1830 vnode_rele_internal(vnode_t vp
, int fmode
, int dont_reenter
, int locked
)
1834 vnode_lock_spin(vp
);
1837 lck_mtx_assert(&vp
->v_lock
, LCK_MTX_ASSERT_OWNED
);
1839 if (--vp
->v_usecount
< 0)
1840 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
);
1842 if (fmode
& FWRITE
) {
1843 if (--vp
->v_writecount
< 0)
1844 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
);
1846 if (fmode
& O_EVTONLY
) {
1847 if (--vp
->v_kusecount
< 0)
1848 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
);
1850 if (vp
->v_kusecount
> vp
->v_usecount
)
1851 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
);
1853 if ((vp
->v_iocount
> 0) || (vp
->v_usecount
> 0)) {
1855 * vnode is still busy... if we're the last
1856 * usecount, mark for a future call to VNOP_INACTIVE
1857 * when the iocount finally drops to 0
1859 if (vp
->v_usecount
== 0) {
1860 vp
->v_lflag
|= VL_NEEDINACTIVE
;
1861 vp
->v_flag
&= ~(VNOCACHE_DATA
| VRAOFF
| VOPENEVT
);
1865 vp
->v_flag
&= ~(VNOCACHE_DATA
| VRAOFF
| VOPENEVT
);
1867 if (ISSET(vp
->v_lflag
, VL_TERMINATE
| VL_DEAD
) || dont_reenter
) {
1869 * vnode is being cleaned, or
1870 * we've requested that we don't reenter
1871 * the filesystem on this release...in
1872 * the latter case, we'll mark the vnode aged
1875 if ( !(vp
->v_lflag
& (VL_TERMINATE
| VL_DEAD
| VL_MARKTERM
)) ) {
1876 vp
->v_lflag
|= VL_NEEDINACTIVE
;
1878 if (vnode_on_reliable_media(vp
) == FALSE
|| vp
->v_flag
& VISDIRTY
) {
1879 vnode_async_list_add(vp
);
1890 * at this point both the iocount and usecount
1892 * pick up an iocount so that we can call
1893 * VNOP_INACTIVE with the vnode lock unheld
1899 vp
->v_lflag
&= ~VL_NEEDINACTIVE
;
1902 VNOP_INACTIVE(vp
, vfs_context_current());
1904 vnode_lock_spin(vp
);
1906 * because we dropped the vnode lock to call VNOP_INACTIVE
1907 * the state of the vnode may have changed... we may have
1908 * picked up an iocount, usecount or the MARKTERM may have
1909 * been set... we need to reevaluate the reference counts
1910 * to determine if we can call vnode_reclaim_internal at
1911 * this point... if the reference counts are up, we'll pick
1912 * up the MARKTERM state when they get subsequently dropped
1914 if ( (vp
->v_iocount
== 1) && (vp
->v_usecount
== 0) &&
1915 ((vp
->v_lflag
& (VL_MARKTERM
| VL_TERMINATE
| VL_DEAD
)) == VL_MARKTERM
)) {
1918 ut
= get_bsdthread_info(current_thread());
1920 if (ut
->uu_defer_reclaims
) {
1921 vp
->v_defer_reclaimlist
= ut
->uu_vreclaims
;
1922 ut
->uu_vreclaims
= vp
;
1925 vnode_lock_convert(vp
);
1926 vnode_reclaim_internal(vp
, 1, 1, 0);
1928 vnode_dropiocount(vp
);
1931 if (vp
->v_usecount
== 0 && vp
->v_type
== VREG
&& !(vp
->v_flag
& VSYSTEM
)) {
1933 if (vp
->v_ubcinfo
) {
1934 vnode_lock_convert(vp
);
1935 memory_object_mark_unused(vp
->v_ubcinfo
->ui_control
, (vp
->v_flag
& VRAGE
) == VRAGE
);
1944 * Remove any vnodes in the vnode table belonging to mount point mp.
1946 * If MNT_NOFORCE is specified, there should not be any active ones,
1947 * return error if any are found (nb: this is a user error, not a
1948 * system error). If MNT_FORCE is specified, detach any active vnodes
1952 int busyprt
= 0; /* print out busy vnodes */
1956 vflush(struct mount
*mp
, struct vnode
*skipvp
, int flags
)
1965 vnode_iterate_setup(mp
);
1967 * On regular unmounts(not forced) do a
1968 * quick check for vnodes to be in use. This
1969 * preserves the caching of vnodes. automounter
1970 * tries unmounting every so often to see whether
1971 * it is still busy or not.
1973 if (((flags
& FORCECLOSE
)==0) && ((mp
->mnt_kern_flag
& MNTK_UNMOUNT_PREFLIGHT
) != 0)) {
1974 if (vnode_umount_preflight(mp
, skipvp
, flags
)) {
1975 vnode_iterate_clear(mp
);
1981 /* it is returns 0 then there is nothing to do */
1982 retval
= vnode_iterate_prepare(mp
);
1985 vnode_iterate_clear(mp
);
1990 /* iterate over all the vnodes */
1991 while (!TAILQ_EMPTY(&mp
->mnt_workerqueue
)) {
1993 vp
= TAILQ_FIRST(&mp
->mnt_workerqueue
);
1994 TAILQ_REMOVE(&mp
->mnt_workerqueue
, vp
, v_mntvnodes
);
1995 TAILQ_INSERT_TAIL(&mp
->mnt_vnodelist
, vp
, v_mntvnodes
);
1997 if ( (vp
->v_mount
!= mp
) || (vp
== skipvp
)) {
2003 vnode_lock_spin(vp
);
2005 if ((vp
->v_id
!= vid
) || ((vp
->v_lflag
& (VL_DEAD
| VL_TERMINATE
)))) {
2012 * If requested, skip over vnodes marked VSYSTEM.
2013 * Skip over all vnodes marked VNOFLUSH.
2015 if ((flags
& SKIPSYSTEM
) && ((vp
->v_flag
& VSYSTEM
) ||
2016 (vp
->v_flag
& VNOFLUSH
))) {
2022 * If requested, skip over vnodes marked VSWAP.
2024 if ((flags
& SKIPSWAP
) && (vp
->v_flag
& VSWAP
)) {
2030 * If requested, skip over vnodes marked VROOT.
2032 if ((flags
& SKIPROOT
) && (vp
->v_flag
& VROOT
)) {
2038 * If WRITECLOSE is set, only flush out regular file
2039 * vnodes open for writing.
2041 if ((flags
& WRITECLOSE
) &&
2042 (vp
->v_writecount
== 0 || vp
->v_type
!= VREG
)) {
2048 * If the real usecount is 0, all we need to do is clear
2049 * out the vnode data structures and we are done.
2051 if (((vp
->v_usecount
== 0) ||
2052 ((vp
->v_usecount
- vp
->v_kusecount
) == 0))) {
2054 vnode_lock_convert(vp
);
2055 vp
->v_iocount
++; /* so that drain waits for * other iocounts */
2059 vnode_reclaim_internal(vp
, 1, 1, 0);
2060 vnode_dropiocount(vp
);
2069 * If FORCECLOSE is set, forcibly close the vnode.
2070 * For block or character devices, revert to an
2071 * anonymous device. For all other files, just kill them.
2073 if (flags
& FORCECLOSE
) {
2074 vnode_lock_convert(vp
);
2076 if (vp
->v_type
!= VBLK
&& vp
->v_type
!= VCHR
) {
2077 vp
->v_iocount
++; /* so that drain waits * for other iocounts */
2081 vnode_abort_advlocks(vp
);
2082 vnode_reclaim_internal(vp
, 1, 1, 0);
2083 vnode_dropiocount(vp
);
2088 vp
->v_lflag
&= ~VL_DEAD
;
2089 vp
->v_op
= spec_vnodeop_p
;
2090 vp
->v_flag
|= VDEVFLUSH
;
2098 vprint("vflush: busy vnode", vp
);
2105 /* At this point the worker queue is completed */
2106 if (busy
&& ((flags
& FORCECLOSE
)==0) && reclaimed
) {
2109 (void)vnode_iterate_reloadq(mp
);
2110 /* returned with mount lock held */
2114 /* if new vnodes were created in between retry the reclaim */
2115 if ( vnode_iterate_reloadq(mp
) != 0) {
2116 if (!(busy
&& ((flags
& FORCECLOSE
)==0)))
2119 vnode_iterate_clear(mp
);
2122 if (busy
&& ((flags
& FORCECLOSE
)==0))
2127 long num_recycledvnodes
= 0;
2129 * Disassociate the underlying file system from a vnode.
2130 * The vnode lock is held on entry.
2133 vclean(vnode_t vp
, int flags
)
2135 vfs_context_t ctx
= vfs_context_current();
2138 int already_terminating
;
2145 * Check to see if the vnode is in use.
2146 * If so we have to reference it before we clean it out
2147 * so that its count cannot fall to zero and generate a
2148 * race against ourselves to recycle it.
2150 active
= vp
->v_usecount
;
2153 * just in case we missed sending a needed
2154 * VNOP_INACTIVE, we'll do it now
2156 need_inactive
= (vp
->v_lflag
& VL_NEEDINACTIVE
);
2158 vp
->v_lflag
&= ~VL_NEEDINACTIVE
;
2161 * Prevent the vnode from being recycled or
2162 * brought into use while we clean it out.
2164 already_terminating
= (vp
->v_lflag
& VL_TERMINATE
);
2166 vp
->v_lflag
|= VL_TERMINATE
;
2169 * remove the vnode from any mount list
2172 insmntque(vp
, (struct mount
*)0);
2175 is_namedstream
= vnode_isnamedstream(vp
);
2180 OSAddAtomicLong(1, &num_recycledvnodes
);
2182 if (flags
& DOCLOSE
)
2183 clflags
|= IO_NDELAY
;
2184 if (flags
& REVOKEALL
)
2185 clflags
|= IO_REVOKE
;
2187 if (active
&& (flags
& DOCLOSE
))
2188 VNOP_CLOSE(vp
, clflags
, ctx
);
2191 * Clean out any buffers associated with the vnode.
2193 if (flags
& DOCLOSE
) {
2195 if (vp
->v_tag
== VT_NFS
)
2196 nfs_vinvalbuf(vp
, V_SAVE
, ctx
, 0);
2200 VNOP_FSYNC(vp
, MNT_WAIT
, ctx
);
2201 buf_invalidateblks(vp
, BUF_WRITE_DATA
| BUF_INVALIDATE_LOCKED
, 0, 0);
2203 if (UBCINFOEXISTS(vp
))
2205 * Clean the pages in VM.
2207 (void)ubc_msync(vp
, (off_t
)0, ubc_getsize(vp
), NULL
, UBC_PUSHALL
| UBC_INVALIDATE
| UBC_SYNC
);
2209 if (active
|| need_inactive
)
2210 VNOP_INACTIVE(vp
, ctx
);
2213 if ((is_namedstream
!= 0) && (vp
->v_parent
!= NULLVP
)) {
2214 vnode_t pvp
= vp
->v_parent
;
2216 /* Delete the shadow stream file before we reclaim its vnode */
2217 if (vnode_isshadow(vp
)) {
2218 vnode_relenamedstream(pvp
, vp
);
2222 * No more streams associated with the parent. We
2223 * have a ref on it, so its identity is stable.
2224 * If the parent is on an opaque volume, then we need to know
2225 * whether it has associated named streams.
2227 if (vfs_authopaque(pvp
->v_mount
)) {
2228 vnode_lock_spin(pvp
);
2229 pvp
->v_lflag
&= ~VL_HASSTREAMS
;
2236 * Destroy ubc named reference
2237 * cluster_release is done on this path
2238 * along with dropping the reference on the ucred
2239 * (and in the case of forced unmount of an mmap-ed file,
2240 * the ubc reference on the vnode is dropped here too).
2242 ubc_destroy_named(vp
);
2246 * cleanup trigger info from vnode (if any)
2249 vnode_resolver_detach(vp
);
2253 * Reclaim the vnode.
2255 if (VNOP_RECLAIM(vp
, ctx
))
2256 panic("vclean: cannot reclaim");
2258 // make sure the name & parent ptrs get cleaned out!
2259 vnode_update_identity(vp
, NULLVP
, NULL
, 0, 0, VNODE_UPDATE_PARENT
| VNODE_UPDATE_NAME
| VNODE_UPDATE_PURGE
);
2263 vp
->v_mount
= dead_mountp
;
2264 vp
->v_op
= dead_vnodeop_p
;
2268 vp
->v_lflag
|= VL_DEAD
;
2269 vp
->v_flag
&= ~VISDIRTY
;
2271 if (already_terminating
== 0) {
2272 vp
->v_lflag
&= ~VL_TERMINATE
;
2274 * Done with purge, notify sleepers of the grim news.
2276 if (vp
->v_lflag
& VL_TERMWANT
) {
2277 vp
->v_lflag
&= ~VL_TERMWANT
;
2278 wakeup(&vp
->v_lflag
);
2284 * Eliminate all activity associated with the requested vnode
2285 * and with all vnodes aliased to the requested vnode.
2289 vn_revoke(vnode_t vp
, int flags
, __unused vfs_context_t a_context
)
2291 vn_revoke(vnode_t vp
, __unused
int flags
, __unused vfs_context_t a_context
)
2298 if ((flags
& REVOKEALL
) == 0)
2299 panic("vnop_revoke");
2302 if (vnode_isaliased(vp
)) {
2304 * If a vgone (or vclean) is already in progress,
2305 * return an immediate error
2307 if (vp
->v_lflag
& VL_TERMINATE
)
2311 * Ensure that vp will not be vgone'd while we
2312 * are eliminating its aliases.
2315 while ((vp
->v_specflags
& SI_ALIASED
)) {
2316 for (vq
= *vp
->v_hashchain
; vq
; vq
= vq
->v_specnext
) {
2317 if (vq
->v_rdev
!= vp
->v_rdev
||
2318 vq
->v_type
!= vp
->v_type
|| vp
== vq
)
2322 if (vnode_getwithvid(vq
,vid
)){
2327 if (!(vq
->v_lflag
& VL_TERMINATE
)) {
2328 vnode_reclaim_internal(vq
, 1, 1, 0);
2330 vnode_put_locked(vq
);
2339 if (vp
->v_lflag
& VL_TERMINATE
) {
2343 vnode_reclaim_internal(vp
, 1, 0, REVOKEALL
);
2350 * Recycle an unused vnode to the front of the free list.
2351 * Release the passed interlock if the vnode will be recycled.
2354 vnode_recycle(struct vnode
*vp
)
2356 vnode_lock_spin(vp
);
2358 if (vp
->v_iocount
|| vp
->v_usecount
) {
2359 vp
->v_lflag
|= VL_MARKTERM
;
2363 vnode_lock_convert(vp
);
2364 vnode_reclaim_internal(vp
, 1, 0, 0);
2372 vnode_reload(vnode_t vp
)
2374 vnode_lock_spin(vp
);
2376 if ((vp
->v_iocount
> 1) || vp
->v_usecount
) {
2380 if (vp
->v_iocount
<= 0)
2381 panic("vnode_reload with no iocount %d", vp
->v_iocount
);
2383 /* mark for release when iocount is dopped */
2384 vp
->v_lflag
|= VL_MARKTERM
;
2392 vgone(vnode_t vp
, int flags
)
2398 * Clean out the filesystem specific data.
2399 * vclean also takes care of removing the
2400 * vnode from any mount list it might be on
2402 vclean(vp
, flags
| DOCLOSE
);
2405 * If special device, remove it from special device alias list
2408 if ((vp
->v_type
== VBLK
|| vp
->v_type
== VCHR
) && vp
->v_specinfo
!= 0) {
2410 if (*vp
->v_hashchain
== vp
) {
2411 *vp
->v_hashchain
= vp
->v_specnext
;
2413 for (vq
= *vp
->v_hashchain
; vq
; vq
= vq
->v_specnext
) {
2414 if (vq
->v_specnext
!= vp
)
2416 vq
->v_specnext
= vp
->v_specnext
;
2420 panic("missing bdev");
2422 if (vp
->v_specflags
& SI_ALIASED
) {
2424 for (vq
= *vp
->v_hashchain
; vq
; vq
= vq
->v_specnext
) {
2425 if (vq
->v_rdev
!= vp
->v_rdev
||
2426 vq
->v_type
!= vp
->v_type
)
2433 panic("missing alias");
2435 vx
->v_specflags
&= ~SI_ALIASED
;
2436 vp
->v_specflags
&= ~SI_ALIASED
;
2440 struct specinfo
*tmp
= vp
->v_specinfo
;
2441 vp
->v_specinfo
= NULL
;
2442 FREE_ZONE((void *)tmp
, sizeof(struct specinfo
), M_SPECINFO
);
2448 * Lookup a vnode by device number.
2451 check_mountedon(dev_t dev
, enum vtype type
, int *errorp
)
2459 for (vp
= speclisth
[SPECHASH(dev
)]; vp
; vp
= vp
->v_specnext
) {
2460 if (dev
!= vp
->v_rdev
|| type
!= vp
->v_type
)
2464 if (vnode_getwithvid(vp
,vid
))
2466 vnode_lock_spin(vp
);
2467 if ((vp
->v_usecount
> 0) || (vp
->v_iocount
> 1)) {
2469 if ((*errorp
= vfs_mountedon(vp
)) != 0)
2481 * Calculate the total number of references to a special device.
2491 if (!vnode_isaliased(vp
))
2492 return (vp
->v_specinfo
->si_opencount
);
2497 * Grab first vnode and its vid.
2499 vq
= *vp
->v_hashchain
;
2500 vid
= vq
? vq
->v_id
: 0;
2506 * Attempt to get the vnode outside the SPECHASH lock.
2508 if (vnode_getwithvid(vq
, vid
)) {
2513 if (vq
->v_rdev
== vp
->v_rdev
&& vq
->v_type
== vp
->v_type
) {
2514 if ((vq
->v_usecount
== 0) && (vq
->v_iocount
== 1) && vq
!= vp
) {
2516 * Alias, but not in use, so flush it out.
2518 vnode_reclaim_internal(vq
, 1, 1, 0);
2519 vnode_put_locked(vq
);
2523 count
+= vq
->v_specinfo
->si_opencount
;
2529 * must do this with the reference still held on 'vq'
2530 * so that it can't be destroyed while we're poking
2531 * through v_specnext
2533 vnext
= vq
->v_specnext
;
2534 vid
= vnext
? vnext
->v_id
: 0;
2546 int prtactive
= 0; /* 1 => print out reclaim of active vnodes */
2549 * Print out a description of a vnode.
2551 static const char *typename
[] =
2552 { "VNON", "VREG", "VDIR", "VBLK", "VCHR", "VLNK", "VSOCK", "VFIFO", "VBAD" };
2555 vprint(const char *label
, struct vnode
*vp
)
2560 printf("%s: ", label
);
2561 printf("type %s, usecount %d, writecount %d",
2562 typename
[vp
->v_type
], vp
->v_usecount
, vp
->v_writecount
);
2564 if (vp
->v_flag
& VROOT
)
2565 strlcat(sbuf
, "|VROOT", sizeof(sbuf
));
2566 if (vp
->v_flag
& VTEXT
)
2567 strlcat(sbuf
, "|VTEXT", sizeof(sbuf
));
2568 if (vp
->v_flag
& VSYSTEM
)
2569 strlcat(sbuf
, "|VSYSTEM", sizeof(sbuf
));
2570 if (vp
->v_flag
& VNOFLUSH
)
2571 strlcat(sbuf
, "|VNOFLUSH", sizeof(sbuf
));
2572 if (vp
->v_flag
& VBWAIT
)
2573 strlcat(sbuf
, "|VBWAIT", sizeof(sbuf
));
2574 if (vnode_isaliased(vp
))
2575 strlcat(sbuf
, "|VALIASED", sizeof(sbuf
));
2576 if (sbuf
[0] != '\0')
2577 printf(" flags (%s)", &sbuf
[1]);
2582 vn_getpath(struct vnode
*vp
, char *pathbuf
, int *len
)
2584 return build_path(vp
, pathbuf
, *len
, len
, BUILDPATH_NO_FS_ENTER
, vfs_context_current());
2588 vn_getpath_fsenter(struct vnode
*vp
, char *pathbuf
, int *len
)
2590 return build_path(vp
, pathbuf
, *len
, len
, 0, vfs_context_current());
2594 vn_getcdhash(struct vnode
*vp
, off_t offset
, unsigned char *cdhash
)
2596 return ubc_cs_getcdhash(vp
, offset
, cdhash
);
2600 static char *extension_table
=NULL
;
2602 static int max_ext_width
;
2605 extension_cmp(const void *a
, const void *b
)
2607 return (strlen((const char *)a
) - strlen((const char *)b
));
2612 // This is the api LaunchServices uses to inform the kernel
2613 // the list of package extensions to ignore.
2615 // Internally we keep the list sorted by the length of the
2616 // the extension (from longest to shortest). We sort the
2617 // list of extensions so that we can speed up our searches
2618 // when comparing file names -- we only compare extensions
2619 // that could possibly fit into the file name, not all of
2620 // them (i.e. a short 8 character name can't have an 8
2621 // character extension).
2623 extern lck_mtx_t
*pkg_extensions_lck
;
2625 __private_extern__
int
2626 set_package_extensions_table(user_addr_t data
, int nentries
, int maxwidth
)
2628 char *new_exts
, *old_exts
;
2631 if (nentries
<= 0 || nentries
> 1024 || maxwidth
<= 0 || maxwidth
> 255) {
2636 // allocate one byte extra so we can guarantee null termination
2637 MALLOC(new_exts
, char *, (nentries
* maxwidth
) + 1, M_TEMP
, M_WAITOK
);
2638 if (new_exts
== NULL
) {
2642 error
= copyin(data
, new_exts
, nentries
* maxwidth
);
2644 FREE(new_exts
, M_TEMP
);
2648 new_exts
[(nentries
* maxwidth
)] = '\0'; // guarantee null termination of the block
2650 qsort(new_exts
, nentries
, maxwidth
, extension_cmp
);
2652 lck_mtx_lock(pkg_extensions_lck
);
2654 old_exts
= extension_table
;
2655 extension_table
= new_exts
;
2657 max_ext_width
= maxwidth
;
2659 lck_mtx_unlock(pkg_extensions_lck
);
2662 FREE(old_exts
, M_TEMP
);
2669 __private_extern__
int
2670 is_package_name(const char *name
, int len
)
2673 const char *ptr
, *name_ext
;
2680 for(ptr
=name
; *ptr
!= '\0'; ptr
++) {
2686 // if there is no "." extension, it can't match
2687 if (name_ext
== NULL
) {
2691 // advance over the "."
2694 lck_mtx_lock(pkg_extensions_lck
);
2696 // now iterate over all the extensions to see if any match
2697 ptr
= &extension_table
[0];
2698 for(i
=0; i
< nexts
; i
++, ptr
+=max_ext_width
) {
2699 extlen
= strlen(ptr
);
2700 if (strncasecmp(name_ext
, ptr
, extlen
) == 0 && name_ext
[extlen
] == '\0') {
2702 lck_mtx_unlock(pkg_extensions_lck
);
2707 lck_mtx_unlock(pkg_extensions_lck
);
2709 // if we get here, no extension matched
2714 vn_path_package_check(__unused vnode_t vp
, char *path
, int pathlen
, int *component
)
2725 while(end
< path
+ pathlen
&& *end
!= '\0') {
2726 while(end
< path
+ pathlen
&& *end
== '/' && *end
!= '\0') {
2732 while(end
< path
+ pathlen
&& *end
!= '/' && *end
!= '\0') {
2736 if (end
> path
+ pathlen
) {
2737 // hmm, string wasn't null terminated
2742 if (is_package_name(ptr
, end
- ptr
)) {
2755 * Determine if a name is inappropriate for a searchfs query.
2756 * This list consists of /System currently.
2759 int vn_searchfs_inappropriate_name(const char *name
, int len
) {
2760 const char *bad_names
[] = { "System" };
2761 int bad_len
[] = { 6 };
2764 for(i
=0; i
< (int) (sizeof(bad_names
) / sizeof(bad_names
[0])); i
++) {
2765 if (len
== bad_len
[i
] && strncmp(name
, bad_names
[i
], strlen(bad_names
[i
]) + 1) == 0) {
2770 // if we get here, no name matched
2775 * Top level filesystem related information gathering.
2777 extern unsigned int vfs_nummntops
;
2780 * The VFS_NUMMNTOPS shouldn't be at name[1] since
2781 * is a VFS generic variable. Since we no longer support
2782 * VT_UFS, we reserve its value to support this sysctl node.
2784 * It should have been:
2785 * name[0]: VFS_GENERIC
2786 * name[1]: VFS_NUMMNTOPS
2788 SYSCTL_INT(_vfs
, VFS_NUMMNTOPS
, nummntops
,
2789 CTLFLAG_RD
| CTLFLAG_KERN
| CTLFLAG_LOCKED
,
2790 &vfs_nummntops
, 0, "");
2793 vfs_sysctl(int *name __unused
, u_int namelen __unused
,
2794 user_addr_t oldp __unused
, size_t *oldlenp __unused
,
2795 user_addr_t newp __unused
, size_t newlen __unused
, proc_t p __unused
);
2798 vfs_sysctl(int *name __unused
, u_int namelen __unused
,
2799 user_addr_t oldp __unused
, size_t *oldlenp __unused
,
2800 user_addr_t newp __unused
, size_t newlen __unused
, proc_t p __unused
)
2807 // The following code disallows specific sysctl's that came through
2808 // the direct sysctl interface (vfs_sysctl_node) instead of the newer
2809 // sysctl_vfs_ctlbyfsid() interface. We can not allow these selectors
2810 // through vfs_sysctl_node() because it passes the user's oldp pointer
2811 // directly to the file system which (for these selectors) casts it
2812 // back to a struct sysctl_req and then proceed to use SYSCTL_IN()
2813 // which jumps through an arbitrary function pointer. When called
2814 // through the sysctl_vfs_ctlbyfsid() interface this does not happen
2815 // and so it's safe.
2817 // Unfortunately we have to pull in definitions from AFP and SMB and
2818 // perform explicit name checks on the file system to determine if
2819 // these selectors are being used.
2822 #define AFPFS_VFS_CTL_GETID 0x00020001
2823 #define AFPFS_VFS_CTL_NETCHANGE 0x00020002
2824 #define AFPFS_VFS_CTL_VOLCHANGE 0x00020003
2826 #define SMBFS_SYSCTL_REMOUNT 1
2827 #define SMBFS_SYSCTL_REMOUNT_INFO 2
2828 #define SMBFS_SYSCTL_GET_SERVER_SHARE 3
2832 is_bad_sysctl_name(struct vfstable
*vfsp
, int selector_name
)
2834 switch(selector_name
) {
2837 case VFS_CTL_NOLOCKS
:
2838 case VFS_CTL_NSTATUS
:
2841 case VFS_CTL_SERVERINFO
:
2849 // the more complicated check for some of SMB's special values
2850 if (strcmp(vfsp
->vfc_name
, "smbfs") == 0) {
2851 switch(selector_name
) {
2852 case SMBFS_SYSCTL_REMOUNT
:
2853 case SMBFS_SYSCTL_REMOUNT_INFO
:
2854 case SMBFS_SYSCTL_GET_SERVER_SHARE
:
2857 } else if (strcmp(vfsp
->vfc_name
, "afpfs") == 0) {
2858 switch(selector_name
) {
2859 case AFPFS_VFS_CTL_GETID
:
2860 case AFPFS_VFS_CTL_NETCHANGE
:
2861 case AFPFS_VFS_CTL_VOLCHANGE
:
2868 // If we get here we passed all the checks so the selector is ok
2874 int vfs_sysctl_node SYSCTL_HANDLER_ARGS
2877 struct vfstable
*vfsp
;
2881 fstypenum
= oidp
->oid_number
;
2885 /* all sysctl names at this level should have at least one name slot for the FS */
2887 return (EISDIR
); /* overloaded */
2890 for (vfsp
= vfsconf
; vfsp
; vfsp
= vfsp
->vfc_next
)
2891 if (vfsp
->vfc_typenum
== fstypenum
) {
2892 vfsp
->vfc_refcount
++;
2895 mount_list_unlock();
2901 if (is_bad_sysctl_name(vfsp
, name
[0])) {
2902 printf("vfs: bad selector 0x%.8x for old-style sysctl(). use the sysctl-by-fsid interface instead\n", name
[0]);
2906 error
= (vfsp
->vfc_vfsops
->vfs_sysctl
)(name
, namelen
, req
->oldptr
, &req
->oldlen
, req
->newptr
, req
->newlen
, vfs_context_current());
2909 vfsp
->vfc_refcount
--;
2910 mount_list_unlock();
2916 * Check to see if a filesystem is mounted on a block device.
2919 vfs_mountedon(struct vnode
*vp
)
2925 if (vp
->v_specflags
& SI_MOUNTEDON
) {
2929 if (vp
->v_specflags
& SI_ALIASED
) {
2930 for (vq
= *vp
->v_hashchain
; vq
; vq
= vq
->v_specnext
) {
2931 if (vq
->v_rdev
!= vp
->v_rdev
||
2932 vq
->v_type
!= vp
->v_type
)
2934 if (vq
->v_specflags
& SI_MOUNTEDON
) {
2945 struct unmount_info
{
2946 int u_errs
; // Total failed unmounts
2947 int u_busy
; // EBUSY failed unmounts
2951 unmount_callback(mount_t mp
, void *arg
)
2955 struct unmount_info
*uip
= arg
;
2958 mount_iterdrop(mp
); // avoid vfs_iterate deadlock in dounmount()
2960 MALLOC_ZONE(mntname
, void *, MAXPATHLEN
, M_NAMEI
, M_WAITOK
);
2962 strlcpy(mntname
, mp
->mnt_vfsstat
.f_mntonname
, MAXPATHLEN
);
2964 error
= dounmount(mp
, MNT_FORCE
, 1, vfs_context_current());
2967 printf("Unmount of %s failed (%d)\n", mntname
? mntname
:"?", error
);
2972 FREE_ZONE(mntname
, MAXPATHLEN
, M_NAMEI
);
2974 return (VFS_RETURNED
);
2978 * Unmount all filesystems. The list is traversed in reverse order
2979 * of mounting to avoid dependencies.
2980 * Busy mounts are retried.
2982 __private_extern__
void
2983 vfs_unmountall(void)
2985 int mounts
, sec
= 1;
2986 struct unmount_info ui
;
2989 ui
.u_errs
= ui
.u_busy
= 0;
2990 vfs_iterate(VFS_ITERATE_CB_DROPREF
| VFS_ITERATE_TAIL_FIRST
, unmount_callback
, &ui
);
2991 mounts
= mount_getvfscnt();
2995 if (ui
.u_busy
> 0) { // Busy mounts - wait & retry
2996 tsleep(&nummounts
, PVFS
, "busy mount", sec
* hz
);
3000 printf("Unmounting timed out\n");
3001 } else if (ui
.u_errs
< mounts
) {
3002 // If the vfs_iterate missed mounts in progress - wait a bit
3003 tsleep(&nummounts
, PVFS
, "missed mount", 2 * hz
);
3008 * This routine is called from vnode_pager_deallocate out of the VM
3009 * The path to vnode_pager_deallocate can only be initiated by ubc_destroy_named
3010 * on a vnode that has a UBCINFO
3012 __private_extern__
void
3013 vnode_pager_vrele(vnode_t vp
)
3015 struct ubc_info
*uip
;
3017 vnode_lock_spin(vp
);
3019 vp
->v_lflag
&= ~VNAMED_UBC
;
3020 if (vp
->v_usecount
!= 0) {
3022 * At the eleventh hour, just before the ubcinfo is
3023 * destroyed, ensure the ubc-specific v_usecount
3024 * reference has gone. We use v_usecount != 0 as a hint;
3025 * ubc_unmap() does nothing if there's no mapping.
3027 * This case is caused by coming here via forced unmount,
3028 * versus the usual vm_object_deallocate() path.
3029 * In the forced unmount case, ubc_destroy_named()
3030 * releases the pager before memory_object_last_unmap()
3035 vnode_lock_spin(vp
);
3038 uip
= vp
->v_ubcinfo
;
3039 vp
->v_ubcinfo
= UBC_INFO_NULL
;
3043 ubc_info_deallocate(uip
);
3047 #include <sys/disk.h>
3049 u_int32_t rootunit
= (u_int32_t
)-1;
3052 extern int lowpri_throttle_enabled
;
3053 extern int iosched_enabled
;
3057 vfs_init_io_attributes(vnode_t devvp
, mount_t mp
)
3060 off_t readblockcnt
= 0;
3061 off_t writeblockcnt
= 0;
3062 off_t readmaxcnt
= 0;
3063 off_t writemaxcnt
= 0;
3064 off_t readsegcnt
= 0;
3065 off_t writesegcnt
= 0;
3066 off_t readsegsize
= 0;
3067 off_t writesegsize
= 0;
3068 off_t alignment
= 0;
3069 u_int32_t ioqueue_depth
= 0;
3073 vfs_context_t ctx
= vfs_context_current();
3078 VNOP_IOCTL(devvp
, DKIOCGETTHROTTLEMASK
, (caddr_t
)&mp
->mnt_throttle_mask
, 0, NULL
);
3080 * as a reasonable approximation, only use the lowest bit of the mask
3081 * to generate a disk unit number
3083 mp
->mnt_devbsdunit
= num_trailing_0(mp
->mnt_throttle_mask
);
3085 if (devvp
== rootvp
)
3086 rootunit
= mp
->mnt_devbsdunit
;
3088 if (mp
->mnt_devbsdunit
== rootunit
) {
3090 * this mount point exists on the same device as the root
3091 * partition, so it comes under the hard throttle control...
3092 * this is true even for the root mount point itself
3094 mp
->mnt_kern_flag
|= MNTK_ROOTDEV
;
3097 * force the spec device to re-cache
3098 * the underlying block size in case
3099 * the filesystem overrode the initial value
3101 set_fsblocksize(devvp
);
3104 if ((error
= VNOP_IOCTL(devvp
, DKIOCGETBLOCKSIZE
,
3105 (caddr_t
)&blksize
, 0, ctx
)))
3108 mp
->mnt_devblocksize
= blksize
;
3111 * set the maximum possible I/O size
3112 * this may get clipped to a smaller value
3113 * based on which constraints are being advertised
3114 * and if those advertised constraints result in a smaller
3115 * limit for a given I/O
3117 mp
->mnt_maxreadcnt
= MAX_UPL_SIZE_BYTES
;
3118 mp
->mnt_maxwritecnt
= MAX_UPL_SIZE_BYTES
;
3120 if (VNOP_IOCTL(devvp
, DKIOCISVIRTUAL
, (caddr_t
)&isvirtual
, 0, ctx
) == 0) {
3122 mp
->mnt_kern_flag
|= MNTK_VIRTUALDEV
;
3124 if (VNOP_IOCTL(devvp
, DKIOCISSOLIDSTATE
, (caddr_t
)&isssd
, 0, ctx
) == 0) {
3126 mp
->mnt_kern_flag
|= MNTK_SSD
;
3128 if ((error
= VNOP_IOCTL(devvp
, DKIOCGETFEATURES
,
3129 (caddr_t
)&features
, 0, ctx
)))
3132 if ((error
= VNOP_IOCTL(devvp
, DKIOCGETMAXBLOCKCOUNTREAD
,
3133 (caddr_t
)&readblockcnt
, 0, ctx
)))
3136 if ((error
= VNOP_IOCTL(devvp
, DKIOCGETMAXBLOCKCOUNTWRITE
,
3137 (caddr_t
)&writeblockcnt
, 0, ctx
)))
3140 if ((error
= VNOP_IOCTL(devvp
, DKIOCGETMAXBYTECOUNTREAD
,
3141 (caddr_t
)&readmaxcnt
, 0, ctx
)))
3144 if ((error
= VNOP_IOCTL(devvp
, DKIOCGETMAXBYTECOUNTWRITE
,
3145 (caddr_t
)&writemaxcnt
, 0, ctx
)))
3148 if ((error
= VNOP_IOCTL(devvp
, DKIOCGETMAXSEGMENTCOUNTREAD
,
3149 (caddr_t
)&readsegcnt
, 0, ctx
)))
3152 if ((error
= VNOP_IOCTL(devvp
, DKIOCGETMAXSEGMENTCOUNTWRITE
,
3153 (caddr_t
)&writesegcnt
, 0, ctx
)))
3156 if ((error
= VNOP_IOCTL(devvp
, DKIOCGETMAXSEGMENTBYTECOUNTREAD
,
3157 (caddr_t
)&readsegsize
, 0, ctx
)))
3160 if ((error
= VNOP_IOCTL(devvp
, DKIOCGETMAXSEGMENTBYTECOUNTWRITE
,
3161 (caddr_t
)&writesegsize
, 0, ctx
)))
3164 if ((error
= VNOP_IOCTL(devvp
, DKIOCGETMINSEGMENTALIGNMENTBYTECOUNT
,
3165 (caddr_t
)&alignment
, 0, ctx
)))
3168 if ((error
= VNOP_IOCTL(devvp
, DKIOCGETCOMMANDPOOLSIZE
,
3169 (caddr_t
)&ioqueue_depth
, 0, ctx
)))
3173 mp
->mnt_maxreadcnt
= (readmaxcnt
> UINT32_MAX
) ? UINT32_MAX
: readmaxcnt
;
3176 temp
= readblockcnt
* blksize
;
3177 temp
= (temp
> UINT32_MAX
) ? UINT32_MAX
: temp
;
3179 if (temp
< mp
->mnt_maxreadcnt
)
3180 mp
->mnt_maxreadcnt
= (u_int32_t
)temp
;
3184 mp
->mnt_maxwritecnt
= (writemaxcnt
> UINT32_MAX
) ? UINT32_MAX
: writemaxcnt
;
3186 if (writeblockcnt
) {
3187 temp
= writeblockcnt
* blksize
;
3188 temp
= (temp
> UINT32_MAX
) ? UINT32_MAX
: temp
;
3190 if (temp
< mp
->mnt_maxwritecnt
)
3191 mp
->mnt_maxwritecnt
= (u_int32_t
)temp
;
3195 temp
= (readsegcnt
> UINT16_MAX
) ? UINT16_MAX
: readsegcnt
;
3197 temp
= mp
->mnt_maxreadcnt
/ PAGE_SIZE
;
3199 if (temp
> UINT16_MAX
)
3202 mp
->mnt_segreadcnt
= (u_int16_t
)temp
;
3205 temp
= (writesegcnt
> UINT16_MAX
) ? UINT16_MAX
: writesegcnt
;
3207 temp
= mp
->mnt_maxwritecnt
/ PAGE_SIZE
;
3209 if (temp
> UINT16_MAX
)
3212 mp
->mnt_segwritecnt
= (u_int16_t
)temp
;
3215 temp
= (readsegsize
> UINT32_MAX
) ? UINT32_MAX
: readsegsize
;
3217 temp
= mp
->mnt_maxreadcnt
;
3218 mp
->mnt_maxsegreadsize
= (u_int32_t
)temp
;
3221 temp
= (writesegsize
> UINT32_MAX
) ? UINT32_MAX
: writesegsize
;
3223 temp
= mp
->mnt_maxwritecnt
;
3224 mp
->mnt_maxsegwritesize
= (u_int32_t
)temp
;
3227 temp
= (alignment
> PAGE_SIZE
) ? PAGE_MASK
: alignment
- 1;
3230 mp
->mnt_alignmentmask
= temp
;
3233 if (ioqueue_depth
> MNT_DEFAULT_IOQUEUE_DEPTH
)
3234 temp
= ioqueue_depth
;
3236 temp
= MNT_DEFAULT_IOQUEUE_DEPTH
;
3238 mp
->mnt_ioqueue_depth
= temp
;
3239 mp
->mnt_ioscale
= (mp
->mnt_ioqueue_depth
+ (MNT_DEFAULT_IOQUEUE_DEPTH
- 1)) / MNT_DEFAULT_IOQUEUE_DEPTH
;
3241 if (mp
->mnt_ioscale
> 1)
3242 printf("ioqueue_depth = %d, ioscale = %d\n", (int)mp
->mnt_ioqueue_depth
, (int)mp
->mnt_ioscale
);
3244 if (features
& DK_FEATURE_FORCE_UNIT_ACCESS
)
3245 mp
->mnt_ioflags
|= MNT_IOFLAGS_FUA_SUPPORTED
;
3247 if (features
& DK_FEATURE_UNMAP
) {
3248 mp
->mnt_ioflags
|= MNT_IOFLAGS_UNMAP_SUPPORTED
;
3250 if (VNOP_IOCTL(devvp
, _DKIOCCORESTORAGE
, NULL
, 0, ctx
) == 0)
3251 mp
->mnt_ioflags
|= MNT_IOFLAGS_CSUNMAP_SUPPORTED
;
3254 if (iosched_enabled
&& (features
& DK_FEATURE_PRIORITY
)) {
3255 mp
->mnt_ioflags
|= MNT_IOFLAGS_IOSCHED_SUPPORTED
;
3256 throttle_info_disable_throttle(mp
->mnt_devbsdunit
);
3258 #endif /* CONFIG_IOSCHED */
3262 static struct klist fs_klist
;
3263 lck_grp_t
*fs_klist_lck_grp
;
3264 lck_mtx_t
*fs_klist_lock
;
3267 vfs_event_init(void)
3270 klist_init(&fs_klist
);
3271 fs_klist_lck_grp
= lck_grp_alloc_init("fs_klist", NULL
);
3272 fs_klist_lock
= lck_mtx_alloc_init(fs_klist_lck_grp
, NULL
);
3276 vfs_event_signal(fsid_t
*fsid
, u_int32_t event
, intptr_t data
)
3278 if (event
== VQ_DEAD
|| event
== VQ_NOTRESP
) {
3279 struct mount
*mp
= vfs_getvfs(fsid
);
3281 mount_lock_spin(mp
);
3283 mp
->mnt_kern_flag
&= ~MNT_LNOTRESP
; // Now responding
3285 mp
->mnt_kern_flag
|= MNT_LNOTRESP
; // Not responding
3290 lck_mtx_lock(fs_klist_lock
);
3291 KNOTE(&fs_klist
, event
);
3292 lck_mtx_unlock(fs_klist_lock
);
3296 * return the number of mounted filesystems.
3299 sysctl_vfs_getvfscnt(void)
3301 return(mount_getvfscnt());
3306 mount_getvfscnt(void)
3312 mount_list_unlock();
3320 mount_fillfsids(fsid_t
*fsidlst
, int count
)
3327 TAILQ_FOREACH(mp
, &mountlist
, mnt_list
) {
3328 if (actual
<= count
) {
3329 fsidlst
[actual
] = mp
->mnt_vfsstat
.f_fsid
;
3333 mount_list_unlock();
3339 * fill in the array of fsid_t's up to a max of 'count', the actual
3340 * number filled in will be set in '*actual'. If there are more fsid_t's
3341 * than room in fsidlst then ENOMEM will be returned and '*actual' will
3342 * have the actual count.
3343 * having *actual filled out even in the error case is depended upon.
3346 sysctl_vfs_getvfslist(fsid_t
*fsidlst
, int count
, int *actual
)
3352 TAILQ_FOREACH(mp
, &mountlist
, mnt_list
) {
3354 if (*actual
<= count
)
3355 fsidlst
[(*actual
) - 1] = mp
->mnt_vfsstat
.f_fsid
;
3357 mount_list_unlock();
3358 return (*actual
<= count
? 0 : ENOMEM
);
3362 sysctl_vfs_vfslist(__unused
struct sysctl_oid
*oidp
, __unused
void *arg1
,
3363 __unused
int arg2
, struct sysctl_req
*req
)
3369 /* This is a readonly node. */
3370 if (req
->newptr
!= USER_ADDR_NULL
)
3373 /* they are querying us so just return the space required. */
3374 if (req
->oldptr
== USER_ADDR_NULL
) {
3375 req
->oldidx
= sysctl_vfs_getvfscnt() * sizeof(fsid_t
);
3380 * Retrieve an accurate count of the amount of space required to copy
3381 * out all the fsids in the system.
3383 space
= req
->oldlen
;
3384 req
->oldlen
= sysctl_vfs_getvfscnt() * sizeof(fsid_t
);
3386 /* they didn't give us enough space. */
3387 if (space
< req
->oldlen
)
3390 MALLOC(fsidlst
, fsid_t
*, req
->oldlen
, M_TEMP
, M_WAITOK
);
3391 if (fsidlst
== NULL
) {
3395 error
= sysctl_vfs_getvfslist(fsidlst
, req
->oldlen
/ sizeof(fsid_t
),
3398 * If we get back ENOMEM, then another mount has been added while we
3399 * slept in malloc above. If this is the case then try again.
3401 if (error
== ENOMEM
) {
3402 FREE(fsidlst
, M_TEMP
);
3403 req
->oldlen
= space
;
3407 error
= SYSCTL_OUT(req
, fsidlst
, actual
* sizeof(fsid_t
));
3409 FREE(fsidlst
, M_TEMP
);
3414 * Do a sysctl by fsid.
3417 sysctl_vfs_ctlbyfsid(__unused
struct sysctl_oid
*oidp
, void *arg1
, int arg2
,
3418 struct sysctl_req
*req
)
3420 union union_vfsidctl vc
;
3422 struct vfsstatfs
*sp
;
3423 int *name
, flags
, namelen
;
3424 int error
=0, gotref
=0;
3425 vfs_context_t ctx
= vfs_context_current();
3426 proc_t p
= req
->p
; /* XXX req->p != current_proc()? */
3427 boolean_t is_64_bit
;
3431 is_64_bit
= proc_is64bit(p
);
3433 error
= SYSCTL_IN(req
, &vc
, is_64_bit
? sizeof(vc
.vc64
):sizeof(vc
.vc32
));
3436 if (vc
.vc32
.vc_vers
!= VFS_CTL_VERS1
) { /* works for 32 and 64 */
3440 mp
= mount_list_lookupby_fsid(&vc
.vc32
.vc_fsid
, 0, 1); /* works for 32 and 64 */
3446 /* reset so that the fs specific code can fetch it. */
3449 * Note if this is a VFS_CTL then we pass the actual sysctl req
3450 * in for "oldp" so that the lower layer can DTRT and use the
3451 * SYSCTL_IN/OUT routines.
3453 if (mp
->mnt_op
->vfs_sysctl
!= NULL
) {
3455 if (vfs_64bitready(mp
)) {
3456 error
= mp
->mnt_op
->vfs_sysctl(name
, namelen
,
3457 CAST_USER_ADDR_T(req
),
3458 NULL
, USER_ADDR_NULL
, 0,
3466 error
= mp
->mnt_op
->vfs_sysctl(name
, namelen
,
3467 CAST_USER_ADDR_T(req
),
3468 NULL
, USER_ADDR_NULL
, 0,
3471 if (error
!= ENOTSUP
) {
3476 case VFS_CTL_UMOUNT
:
3479 req
->newptr
= vc
.vc64
.vc_ptr
;
3480 req
->newlen
= (size_t)vc
.vc64
.vc_len
;
3483 req
->newptr
= CAST_USER_ADDR_T(vc
.vc32
.vc_ptr
);
3484 req
->newlen
= vc
.vc32
.vc_len
;
3486 error
= SYSCTL_IN(req
, &flags
, sizeof(flags
));
3493 /* safedounmount consumes a ref */
3494 error
= safedounmount(mp
, flags
, ctx
);
3496 case VFS_CTL_STATFS
:
3499 req
->newptr
= vc
.vc64
.vc_ptr
;
3500 req
->newlen
= (size_t)vc
.vc64
.vc_len
;
3503 req
->newptr
= CAST_USER_ADDR_T(vc
.vc32
.vc_ptr
);
3504 req
->newlen
= vc
.vc32
.vc_len
;
3506 error
= SYSCTL_IN(req
, &flags
, sizeof(flags
));
3509 sp
= &mp
->mnt_vfsstat
;
3510 if (((flags
& MNT_NOWAIT
) == 0 || (flags
& (MNT_WAIT
| MNT_DWAIT
))) &&
3511 (error
= vfs_update_vfsstat(mp
, ctx
, VFS_USER_EVENT
)))
3514 struct user64_statfs sfs
;
3515 bzero(&sfs
, sizeof(sfs
));
3516 sfs
.f_flags
= mp
->mnt_flag
& MNT_VISFLAGMASK
;
3517 sfs
.f_type
= mp
->mnt_vtable
->vfc_typenum
;
3518 sfs
.f_bsize
= (user64_long_t
)sp
->f_bsize
;
3519 sfs
.f_iosize
= (user64_long_t
)sp
->f_iosize
;
3520 sfs
.f_blocks
= (user64_long_t
)sp
->f_blocks
;
3521 sfs
.f_bfree
= (user64_long_t
)sp
->f_bfree
;
3522 sfs
.f_bavail
= (user64_long_t
)sp
->f_bavail
;
3523 sfs
.f_files
= (user64_long_t
)sp
->f_files
;
3524 sfs
.f_ffree
= (user64_long_t
)sp
->f_ffree
;
3525 sfs
.f_fsid
= sp
->f_fsid
;
3526 sfs
.f_owner
= sp
->f_owner
;
3528 if (mp
->mnt_kern_flag
& MNTK_TYPENAME_OVERRIDE
) {
3529 strlcpy(&sfs
.f_fstypename
[0], &mp
->fstypename_override
[0], MFSTYPENAMELEN
);
3531 strlcpy(sfs
.f_fstypename
, sp
->f_fstypename
, MFSNAMELEN
);
3533 strlcpy(sfs
.f_mntonname
, sp
->f_mntonname
, MNAMELEN
);
3534 strlcpy(sfs
.f_mntfromname
, sp
->f_mntfromname
, MNAMELEN
);
3536 error
= SYSCTL_OUT(req
, &sfs
, sizeof(sfs
));
3539 struct user32_statfs sfs
;
3540 bzero(&sfs
, sizeof(sfs
));
3541 sfs
.f_flags
= mp
->mnt_flag
& MNT_VISFLAGMASK
;
3542 sfs
.f_type
= mp
->mnt_vtable
->vfc_typenum
;
3545 * It's possible for there to be more than 2^^31 blocks in the filesystem, so we
3546 * have to fudge the numbers here in that case. We inflate the blocksize in order
3547 * to reflect the filesystem size as best we can.
3549 if (sp
->f_blocks
> INT_MAX
) {
3553 * Work out how far we have to shift the block count down to make it fit.
3554 * Note that it's possible to have to shift so far that the resulting
3555 * blocksize would be unreportably large. At that point, we will clip
3556 * any values that don't fit.
3558 * For safety's sake, we also ensure that f_iosize is never reported as
3559 * being smaller than f_bsize.
3561 for (shift
= 0; shift
< 32; shift
++) {
3562 if ((sp
->f_blocks
>> shift
) <= INT_MAX
)
3564 if ((((long long)sp
->f_bsize
) << (shift
+ 1)) > INT_MAX
)
3567 #define __SHIFT_OR_CLIP(x, s) ((((x) >> (s)) > INT_MAX) ? INT_MAX : ((x) >> (s)))
3568 sfs
.f_blocks
= (user32_long_t
)__SHIFT_OR_CLIP(sp
->f_blocks
, shift
);
3569 sfs
.f_bfree
= (user32_long_t
)__SHIFT_OR_CLIP(sp
->f_bfree
, shift
);
3570 sfs
.f_bavail
= (user32_long_t
)__SHIFT_OR_CLIP(sp
->f_bavail
, shift
);
3571 #undef __SHIFT_OR_CLIP
3572 sfs
.f_bsize
= (user32_long_t
)(sp
->f_bsize
<< shift
);
3573 sfs
.f_iosize
= lmax(sp
->f_iosize
, sp
->f_bsize
);
3575 sfs
.f_bsize
= (user32_long_t
)sp
->f_bsize
;
3576 sfs
.f_iosize
= (user32_long_t
)sp
->f_iosize
;
3577 sfs
.f_blocks
= (user32_long_t
)sp
->f_blocks
;
3578 sfs
.f_bfree
= (user32_long_t
)sp
->f_bfree
;
3579 sfs
.f_bavail
= (user32_long_t
)sp
->f_bavail
;
3581 sfs
.f_files
= (user32_long_t
)sp
->f_files
;
3582 sfs
.f_ffree
= (user32_long_t
)sp
->f_ffree
;
3583 sfs
.f_fsid
= sp
->f_fsid
;
3584 sfs
.f_owner
= sp
->f_owner
;
3586 if (mp
->mnt_kern_flag
& MNTK_TYPENAME_OVERRIDE
) {
3587 strlcpy(&sfs
.f_fstypename
[0], &mp
->fstypename_override
[0], MFSTYPENAMELEN
);
3589 strlcpy(sfs
.f_fstypename
, sp
->f_fstypename
, MFSNAMELEN
);
3591 strlcpy(sfs
.f_mntonname
, sp
->f_mntonname
, MNAMELEN
);
3592 strlcpy(sfs
.f_mntfromname
, sp
->f_mntfromname
, MNAMELEN
);
3594 error
= SYSCTL_OUT(req
, &sfs
, sizeof(sfs
));
3607 static int filt_fsattach(struct knote
*kn
);
3608 static void filt_fsdetach(struct knote
*kn
);
3609 static int filt_fsevent(struct knote
*kn
, long hint
);
3610 struct filterops fs_filtops
= {
3611 .f_attach
= filt_fsattach
,
3612 .f_detach
= filt_fsdetach
,
3613 .f_event
= filt_fsevent
,
3617 filt_fsattach(struct knote
*kn
)
3620 lck_mtx_lock(fs_klist_lock
);
3621 kn
->kn_flags
|= EV_CLEAR
;
3622 KNOTE_ATTACH(&fs_klist
, kn
);
3623 lck_mtx_unlock(fs_klist_lock
);
3628 filt_fsdetach(struct knote
*kn
)
3630 lck_mtx_lock(fs_klist_lock
);
3631 KNOTE_DETACH(&fs_klist
, kn
);
3632 lck_mtx_unlock(fs_klist_lock
);
3636 filt_fsevent(struct knote
*kn
, long hint
)
3639 * Backwards compatibility:
3640 * Other filters would do nothing if kn->kn_sfflags == 0
3643 if ((kn
->kn_sfflags
== 0) || (kn
->kn_sfflags
& hint
)) {
3644 kn
->kn_fflags
|= hint
;
3647 return (kn
->kn_fflags
!= 0);
3651 sysctl_vfs_noremotehang(__unused
struct sysctl_oid
*oidp
,
3652 __unused
void *arg1
, __unused
int arg2
, struct sysctl_req
*req
)
3658 /* We need a pid. */
3659 if (req
->newptr
== USER_ADDR_NULL
)
3662 error
= SYSCTL_IN(req
, &pid
, sizeof(pid
));
3666 p
= proc_find(pid
< 0 ? -pid
: pid
);
3671 * Fetching the value is ok, but we only fetch if the old
3674 if (req
->oldptr
!= USER_ADDR_NULL
) {
3675 out
= !((p
->p_flag
& P_NOREMOTEHANG
) == 0);
3677 error
= SYSCTL_OUT(req
, &out
, sizeof(out
));
3681 /* cansignal offers us enough security. */
3682 if (p
!= req
->p
&& proc_suser(req
->p
) != 0) {
3688 OSBitAndAtomic(~((uint32_t)P_NOREMOTEHANG
), &p
->p_flag
);
3690 OSBitOrAtomic(P_NOREMOTEHANG
, &p
->p_flag
);
3697 sysctl_vfs_generic_conf SYSCTL_HANDLER_ARGS
3700 struct vfstable
*vfsp
;
3701 struct vfsconf vfsc
;
3709 } else if (namelen
> 1) {
3714 for (vfsp
= vfsconf
; vfsp
; vfsp
= vfsp
->vfc_next
)
3715 if (vfsp
->vfc_typenum
== name
[0])
3719 mount_list_unlock();
3723 vfsc
.vfc_reserved1
= 0;
3724 bcopy(vfsp
->vfc_name
, vfsc
.vfc_name
, sizeof(vfsc
.vfc_name
));
3725 vfsc
.vfc_typenum
= vfsp
->vfc_typenum
;
3726 vfsc
.vfc_refcount
= vfsp
->vfc_refcount
;
3727 vfsc
.vfc_flags
= vfsp
->vfc_flags
;
3728 vfsc
.vfc_reserved2
= 0;
3729 vfsc
.vfc_reserved3
= 0;
3731 mount_list_unlock();
3732 return (SYSCTL_OUT(req
, &vfsc
, sizeof(struct vfsconf
)));
3735 /* the vfs.generic. branch. */
3736 SYSCTL_NODE(_vfs
, VFS_GENERIC
, generic
, CTLFLAG_RW
| CTLFLAG_LOCKED
, NULL
, "vfs generic hinge");
3737 /* retreive a list of mounted filesystem fsid_t */
3738 SYSCTL_PROC(_vfs_generic
, OID_AUTO
, vfsidlist
,
3739 CTLTYPE_STRUCT
| CTLFLAG_RD
| CTLFLAG_LOCKED
,
3740 NULL
, 0, sysctl_vfs_vfslist
, "S,fsid", "List of mounted filesystem ids");
3741 /* perform operations on filesystem via fsid_t */
3742 SYSCTL_NODE(_vfs_generic
, OID_AUTO
, ctlbyfsid
, CTLFLAG_RW
| CTLFLAG_LOCKED
,
3743 sysctl_vfs_ctlbyfsid
, "ctlbyfsid");
3744 SYSCTL_PROC(_vfs_generic
, OID_AUTO
, noremotehang
, CTLFLAG_RW
| CTLFLAG_ANYBODY
,
3745 NULL
, 0, sysctl_vfs_noremotehang
, "I", "noremotehang");
3746 SYSCTL_INT(_vfs_generic
, VFS_MAXTYPENUM
, maxtypenum
,
3747 CTLFLAG_RD
| CTLFLAG_KERN
| CTLFLAG_LOCKED
,
3748 &maxvfstypenum
, 0, "");
3749 SYSCTL_INT(_vfs_generic
, OID_AUTO
, sync_timeout
, CTLFLAG_RW
| CTLFLAG_LOCKED
, &sync_timeout
, 0, "");
3750 SYSCTL_NODE(_vfs_generic
, VFS_CONF
, conf
,
3751 CTLFLAG_RD
| CTLFLAG_LOCKED
,
3752 sysctl_vfs_generic_conf
, "");
3754 long num_reusedvnodes
= 0;
3758 process_vp(vnode_t vp
, int want_vp
, int *deferred
)
3766 vnode_list_remove_locked(vp
);
3768 vnode_list_unlock();
3770 vnode_lock_spin(vp
);
3773 * We could wait for the vnode_lock after removing the vp from the freelist
3774 * and the vid is bumped only at the very end of reclaim. So it is possible
3775 * that we are looking at a vnode that is being terminated. If so skip it.
3777 if ((vpid
!= vp
->v_id
) || (vp
->v_usecount
!= 0) || (vp
->v_iocount
!= 0) ||
3778 VONLIST(vp
) || (vp
->v_lflag
& VL_TERMINATE
)) {
3780 * we lost the race between dropping the list lock
3781 * and picking up the vnode_lock... someone else
3782 * used this vnode and it is now in a new state
3788 if ( (vp
->v_lflag
& (VL_NEEDINACTIVE
| VL_MARKTERM
)) == VL_NEEDINACTIVE
) {
3790 * we did a vnode_rele_ext that asked for
3791 * us not to reenter the filesystem during
3792 * the release even though VL_NEEDINACTIVE was
3793 * set... we'll do it here by doing a
3794 * vnode_get/vnode_put
3796 * pick up an iocount so that we can call
3797 * vnode_put and drive the VNOP_INACTIVE...
3798 * vnode_put will either leave us off
3799 * the freelist if a new ref comes in,
3800 * or put us back on the end of the freelist
3801 * or recycle us if we were marked for termination...
3802 * so we'll just go grab a new candidate
3808 vnode_put_locked(vp
);
3814 * Checks for anyone racing us for recycle
3816 if (vp
->v_type
!= VBAD
) {
3817 if (want_vp
&& (vnode_on_reliable_media(vp
) == FALSE
|| (vp
->v_flag
& VISDIRTY
))) {
3818 vnode_async_list_add(vp
);
3825 if (vp
->v_lflag
& VL_DEAD
)
3826 panic("new_vnode(%p): the vnode is VL_DEAD but not VBAD", vp
);
3828 vnode_lock_convert(vp
);
3829 (void)vnode_reclaim_internal(vp
, 1, want_vp
, 0);
3833 panic("new_vnode(%p): vp on list", vp
);
3834 if (vp
->v_usecount
|| vp
->v_iocount
|| vp
->v_kusecount
||
3835 (vp
->v_lflag
& (VNAMED_UBC
| VNAMED_MOUNT
| VNAMED_FSHASH
)))
3836 panic("new_vnode(%p): free vnode still referenced", vp
);
3837 if ((vp
->v_mntvnodes
.tqe_prev
!= 0) && (vp
->v_mntvnodes
.tqe_next
!= 0))
3838 panic("new_vnode(%p): vnode seems to be on mount list", vp
);
3839 if ( !LIST_EMPTY(&vp
->v_nclinks
) || !LIST_EMPTY(&vp
->v_ncchildren
))
3840 panic("new_vnode(%p): vnode still hooked into the name cache", vp
);
3852 async_work_continue(void)
3854 struct async_work_lst
*q
;
3858 q
= &vnode_async_work_list
;
3864 if ( TAILQ_EMPTY(q
) ) {
3865 assert_wait(q
, (THREAD_UNINT
));
3867 vnode_list_unlock();
3869 thread_block((thread_continue_t
)async_work_continue
);
3873 async_work_handled
++;
3875 vp
= TAILQ_FIRST(q
);
3877 vp
= process_vp(vp
, 0, &deferred
);
3880 panic("found VBAD vp (%p) on async queue", vp
);
3886 new_vnode(vnode_t
*vpp
)
3889 uint32_t retries
= 0, max_retries
= 100; /* retry incase of tablefull */
3890 int force_alloc
= 0, walk_count
= 0;
3891 boolean_t need_reliable_vp
= FALSE
;
3893 struct timeval initial_tv
;
3894 struct timeval current_tv
;
3895 proc_t curproc
= current_proc();
3897 initial_tv
.tv_sec
= 0;
3903 if (need_reliable_vp
== TRUE
)
3904 async_work_timed_out
++;
3906 if ((numvnodes
- deadvnodes
) < desiredvnodes
|| force_alloc
) {
3909 if ( !TAILQ_EMPTY(&vnode_dead_list
)) {
3911 * Can always reuse a dead one
3913 vp
= TAILQ_FIRST(&vnode_dead_list
);
3917 * no dead vnodes available... if we're under
3918 * the limit, we'll create a new vnode
3921 vnode_list_unlock();
3923 MALLOC_ZONE(vp
, struct vnode
*, sizeof(*vp
), M_VNODE
, M_WAITOK
);
3924 bzero((char *)vp
, sizeof(*vp
));
3925 VLISTNONE(vp
); /* avoid double queue removal */
3926 lck_mtx_init(&vp
->v_lock
, vnode_lck_grp
, vnode_lck_attr
);
3928 klist_init(&vp
->v_knotes
);
3930 vp
->v_id
= ts
.tv_nsec
;
3931 vp
->v_flag
= VSTANDARD
;
3934 if (mac_vnode_label_init_needed(vp
))
3935 mac_vnode_label_init(vp
);
3941 microuptime(¤t_tv
);
3943 #define MAX_WALK_COUNT 1000
3945 if ( !TAILQ_EMPTY(&vnode_rage_list
) &&
3946 (ragevnodes
>= rage_limit
||
3947 (current_tv
.tv_sec
- rage_tv
.tv_sec
) >= RAGE_TIME_LIMIT
)) {
3949 TAILQ_FOREACH(vp
, &vnode_rage_list
, v_freelist
) {
3950 if ( !(vp
->v_listflag
& VLIST_RAGE
))
3951 panic("new_vnode: vp (%p) on RAGE list not marked VLIST_RAGE", vp
);
3953 // if we're a dependency-capable process, skip vnodes that can
3954 // cause recycling deadlocks. (i.e. this process is diskimages
3955 // helper and the vnode is in a disk image). Querying the
3956 // mnt_kern_flag for the mount's virtual device status
3957 // is safer than checking the mnt_dependent_process, which
3958 // may not be updated if there are multiple devnode layers
3959 // in between the disk image and the final consumer.
3961 if ((curproc
->p_flag
& P_DEPENDENCY_CAPABLE
) == 0 || vp
->v_mount
== NULL
||
3962 (vp
->v_mount
->mnt_kern_flag
& MNTK_VIRTUALDEV
) == 0) {
3964 * if need_reliable_vp == TRUE, then we've already sent one or more
3965 * non-reliable vnodes to the async thread for processing and timed
3966 * out waiting for a dead vnode to show up. Use the MAX_WALK_COUNT
3967 * mechanism to first scan for a reliable vnode before forcing
3968 * a new vnode to be created
3970 if (need_reliable_vp
== FALSE
|| vnode_on_reliable_media(vp
) == TRUE
)
3974 // don't iterate more than MAX_WALK_COUNT vnodes to
3975 // avoid keeping the vnode list lock held for too long.
3977 if (walk_count
++ > MAX_WALK_COUNT
) {
3984 if (vp
== NULL
&& !TAILQ_EMPTY(&vnode_free_list
)) {
3986 * Pick the first vp for possible reuse
3989 TAILQ_FOREACH(vp
, &vnode_free_list
, v_freelist
) {
3991 // if we're a dependency-capable process, skip vnodes that can
3992 // cause recycling deadlocks. (i.e. this process is diskimages
3993 // helper and the vnode is in a disk image). Querying the
3994 // mnt_kern_flag for the mount's virtual device status
3995 // is safer than checking the mnt_dependent_process, which
3996 // may not be updated if there are multiple devnode layers
3997 // in between the disk image and the final consumer.
3999 if ((curproc
->p_flag
& P_DEPENDENCY_CAPABLE
) == 0 || vp
->v_mount
== NULL
||
4000 (vp
->v_mount
->mnt_kern_flag
& MNTK_VIRTUALDEV
) == 0) {
4002 * if need_reliable_vp == TRUE, then we've already sent one or more
4003 * non-reliable vnodes to the async thread for processing and timed
4004 * out waiting for a dead vnode to show up. Use the MAX_WALK_COUNT
4005 * mechanism to first scan for a reliable vnode before forcing
4006 * a new vnode to be created
4008 if (need_reliable_vp
== FALSE
|| vnode_on_reliable_media(vp
) == TRUE
)
4012 // don't iterate more than MAX_WALK_COUNT vnodes to
4013 // avoid keeping the vnode list lock held for too long.
4015 if (walk_count
++ > MAX_WALK_COUNT
) {
4023 // if we don't have a vnode and the walk_count is >= MAX_WALK_COUNT
4024 // then we're trying to create a vnode on behalf of a
4025 // process like diskimages-helper that has file systems
4026 // mounted on top of itself (and thus we can't reclaim
4027 // vnodes in the file systems on top of us). if we can't
4028 // find a vnode to reclaim then we'll just have to force
4031 if (vp
== NULL
&& walk_count
>= MAX_WALK_COUNT
) {
4033 vnode_list_unlock();
4039 * we've reached the system imposed maximum number of vnodes
4040 * but there isn't a single one available
4041 * wait a bit and then retry... if we can't get a vnode
4042 * after our target number of retries, than log a complaint
4044 if (++retries
<= max_retries
) {
4045 vnode_list_unlock();
4046 delay_for_interval(1, 1000 * 1000);
4050 vnode_list_unlock();
4052 log(LOG_EMERG
, "%d desired, %d numvnodes, "
4053 "%d free, %d dead, %d rage\n",
4054 desiredvnodes
, numvnodes
, freevnodes
, deadvnodes
, ragevnodes
);
4057 * Running out of vnodes tends to make a system unusable. Start killing
4058 * processes that jetsam knows are killable.
4060 if (memorystatus_kill_on_vnode_limit() == FALSE
) {
4062 * If jetsam can't find any more processes to kill and there
4063 * still aren't any free vnodes, panic. Hopefully we'll get a
4064 * panic log to tell us why we ran out.
4066 panic("vnode table is full\n");
4070 * Now that we've killed someone, wait a bit and continue looking
4071 * (with fewer retries before trying another kill).
4073 delay_for_interval(3, 1000 * 1000);
4083 if ((vp
= process_vp(vp
, 1, &deferred
)) == NULLVP
) {
4086 struct timeval elapsed_tv
;
4088 if (initial_tv
.tv_sec
== 0)
4089 microuptime(&initial_tv
);
4093 dead_vnode_waited
++;
4094 dead_vnode_wanted
++;
4097 * note that we're only going to explicitly wait 10ms
4098 * for a dead vnode to become available, since even if one
4099 * isn't available, a reliable vnode might now be available
4100 * at the head of the VRAGE or free lists... if so, we
4101 * can satisfy the new_vnode request with less latency then waiting
4102 * for the full 100ms duration we're ultimately willing to tolerate
4104 assert_wait_timeout((caddr_t
)&dead_vnode_wanted
, (THREAD_INTERRUPTIBLE
), 10000, NSEC_PER_USEC
);
4106 vnode_list_unlock();
4108 thread_block(THREAD_CONTINUE_NULL
);
4110 microuptime(&elapsed_tv
);
4112 timevalsub(&elapsed_tv
, &initial_tv
);
4113 elapsed_msecs
= elapsed_tv
.tv_sec
* 1000 + elapsed_tv
.tv_usec
/ 1000;
4115 if (elapsed_msecs
>= 100) {
4117 * we've waited long enough... 100ms is
4118 * somewhat arbitrary for this case, but the
4119 * normal worst case latency used for UI
4120 * interaction is 100ms, so I've chosen to
4123 * setting need_reliable_vp to TRUE
4124 * forces us to find a reliable vnode
4125 * that we can process synchronously, or
4126 * to create a new one if the scan for
4127 * a reliable one hits the scan limit
4129 need_reliable_vp
= TRUE
;
4134 OSAddAtomicLong(1, &num_reusedvnodes
);
4139 * We should never see VL_LABELWAIT or VL_LABEL here.
4140 * as those operations hold a reference.
4142 assert ((vp
->v_lflag
& VL_LABELWAIT
) != VL_LABELWAIT
);
4143 assert ((vp
->v_lflag
& VL_LABEL
) != VL_LABEL
);
4144 if (vp
->v_lflag
& VL_LABELED
) {
4145 vnode_lock_convert(vp
);
4146 mac_vnode_label_recycle(vp
);
4147 } else if (mac_vnode_label_init_needed(vp
)) {
4148 vnode_lock_convert(vp
);
4149 mac_vnode_label_init(vp
);
4156 vp
->v_writecount
= 0;
4157 vp
->v_references
= 0;
4158 vp
->v_iterblkflags
= 0;
4159 vp
->v_flag
= VSTANDARD
;
4160 /* vbad vnodes can point to dead_mountp */
4162 vp
->v_defer_reclaimlist
= (vnode_t
)0;
4173 vnode_lock(vnode_t vp
)
4175 lck_mtx_lock(&vp
->v_lock
);
4179 vnode_lock_spin(vnode_t vp
)
4181 lck_mtx_lock_spin(&vp
->v_lock
);
4185 vnode_unlock(vnode_t vp
)
4187 lck_mtx_unlock(&vp
->v_lock
);
4193 vnode_get(struct vnode
*vp
)
4197 vnode_lock_spin(vp
);
4198 retval
= vnode_get_locked(vp
);
4205 vnode_get_locked(struct vnode
*vp
)
4208 lck_mtx_assert(&vp
->v_lock
, LCK_MTX_ASSERT_OWNED
);
4210 if ((vp
->v_iocount
== 0) && (vp
->v_lflag
& (VL_TERMINATE
| VL_DEAD
))) {
4221 * vnode_getwithvid() cuts in line in front of a vnode drain (that is,
4222 * while the vnode is draining, but at no point after that) to prevent
4223 * deadlocks when getting vnodes from filesystem hashes while holding
4224 * resources that may prevent other iocounts from being released.
4227 vnode_getwithvid(vnode_t vp
, uint32_t vid
)
4229 return(vget_internal(vp
, vid
, ( VNODE_NODEAD
| VNODE_WITHID
| VNODE_DRAINO
)));
4233 * vnode_getwithvid_drainok() is like vnode_getwithvid(), but *does* block behind a vnode
4234 * drain; it exists for use in the VFS name cache, where we really do want to block behind
4235 * vnode drain to prevent holding off an unmount.
4238 vnode_getwithvid_drainok(vnode_t vp
, uint32_t vid
)
4240 return(vget_internal(vp
, vid
, ( VNODE_NODEAD
| VNODE_WITHID
)));
4244 vnode_getwithref(vnode_t vp
)
4246 return(vget_internal(vp
, 0, 0));
4250 __private_extern__
int
4251 vnode_getalways(vnode_t vp
)
4253 return(vget_internal(vp
, 0, VNODE_ALWAYS
));
4257 vnode_put(vnode_t vp
)
4261 vnode_lock_spin(vp
);
4262 retval
= vnode_put_locked(vp
);
4269 vnode_put_locked(vnode_t vp
)
4271 vfs_context_t ctx
= vfs_context_current(); /* hoist outside loop */
4274 lck_mtx_assert(&vp
->v_lock
, LCK_MTX_ASSERT_OWNED
);
4277 if (vp
->v_iocount
< 1)
4278 panic("vnode_put(%p): iocount < 1", vp
);
4280 if ((vp
->v_usecount
> 0) || (vp
->v_iocount
> 1)) {
4281 vnode_dropiocount(vp
);
4284 if ((vp
->v_lflag
& (VL_DEAD
| VL_NEEDINACTIVE
)) == VL_NEEDINACTIVE
) {
4286 vp
->v_lflag
&= ~VL_NEEDINACTIVE
;
4289 VNOP_INACTIVE(vp
, ctx
);
4291 vnode_lock_spin(vp
);
4293 * because we had to drop the vnode lock before calling
4294 * VNOP_INACTIVE, the state of this vnode may have changed...
4295 * we may pick up both VL_MARTERM and either
4296 * an iocount or a usecount while in the VNOP_INACTIVE call
4297 * we don't want to call vnode_reclaim_internal on a vnode
4298 * that has active references on it... so loop back around
4299 * and reevaluate the state
4303 vp
->v_lflag
&= ~VL_NEEDINACTIVE
;
4305 if ((vp
->v_lflag
& (VL_MARKTERM
| VL_TERMINATE
| VL_DEAD
)) == VL_MARKTERM
) {
4306 vnode_lock_convert(vp
);
4307 vnode_reclaim_internal(vp
, 1, 1, 0);
4309 vnode_dropiocount(vp
);
4315 /* is vnode_t in use by others? */
4317 vnode_isinuse(vnode_t vp
, int refcnt
)
4319 return(vnode_isinuse_locked(vp
, refcnt
, 0));
4324 vnode_isinuse_locked(vnode_t vp
, int refcnt
, int locked
)
4329 vnode_lock_spin(vp
);
4330 if ((vp
->v_type
!= VREG
) && ((vp
->v_usecount
- vp
->v_kusecount
) > refcnt
)) {
4334 if (vp
->v_type
== VREG
) {
4335 retval
= ubc_isinuse_locked(vp
, refcnt
, 1);
4345 /* resume vnode_t */
4347 vnode_resume(vnode_t vp
)
4349 if ((vp
->v_lflag
& VL_SUSPENDED
) && vp
->v_owner
== current_thread()) {
4351 vnode_lock_spin(vp
);
4352 vp
->v_lflag
&= ~VL_SUSPENDED
;
4356 wakeup(&vp
->v_iocount
);
4362 * Please do not use on more than one vnode at a time as it may
4364 * xxx should we explicity prevent this from happening?
4368 vnode_suspend(vnode_t vp
)
4370 if (vp
->v_lflag
& VL_SUSPENDED
) {
4374 vnode_lock_spin(vp
);
4377 * xxx is this sufficient to check if a vnode_drain is
4381 if (vp
->v_owner
== NULL
) {
4382 vp
->v_lflag
|= VL_SUSPENDED
;
4383 vp
->v_owner
= current_thread();
4391 * Release any blocked locking requests on the vnode.
4392 * Used for forced-unmounts.
4394 * XXX What about network filesystems?
4397 vnode_abort_advlocks(vnode_t vp
)
4399 if (vp
->v_flag
& VLOCKLOCAL
)
4400 lf_abort_advlocks(vp
);
4405 vnode_drain(vnode_t vp
)
4408 if (vp
->v_lflag
& VL_DRAIN
) {
4409 panic("vnode_drain: recursive drain");
4412 vp
->v_lflag
|= VL_DRAIN
;
4413 vp
->v_owner
= current_thread();
4415 while (vp
->v_iocount
> 1)
4416 msleep(&vp
->v_iocount
, &vp
->v_lock
, PVFS
, "vnode_drain", NULL
);
4418 vp
->v_lflag
&= ~VL_DRAIN
;
4425 * if the number of recent references via vnode_getwithvid or vnode_getwithref
4426 * exceeds this threshold, than 'UN-AGE' the vnode by removing it from
4427 * the LRU list if it's currently on it... once the iocount and usecount both drop
4428 * to 0, it will get put back on the end of the list, effectively making it younger
4429 * this allows us to keep actively referenced vnodes in the list without having
4430 * to constantly remove and add to the list each time a vnode w/o a usecount is
4431 * referenced which costs us taking and dropping a global lock twice.
4432 * However, if the vnode is marked DIRTY, we want to pull it out much earlier
4434 #define UNAGE_THRESHHOLD 25
4435 #define UNAGE_DIRTYTHRESHHOLD 6
4438 vnode_getiocount(vnode_t vp
, unsigned int vid
, int vflags
)
4440 int nodead
= vflags
& VNODE_NODEAD
;
4441 int nosusp
= vflags
& VNODE_NOSUSPEND
;
4442 int always
= vflags
& VNODE_ALWAYS
;
4443 int beatdrain
= vflags
& VNODE_DRAINO
;
4444 int withvid
= vflags
& VNODE_WITHID
;
4448 * if it is a dead vnode with deadfs
4450 if (nodead
&& (vp
->v_lflag
& VL_DEAD
) && ((vp
->v_type
== VBAD
) || (vp
->v_data
== 0))) {
4454 * will return VL_DEAD ones
4456 if ((vp
->v_lflag
& (VL_SUSPENDED
| VL_DRAIN
| VL_TERMINATE
)) == 0 ) {
4460 * if suspended vnodes are to be failed
4462 if (nosusp
&& (vp
->v_lflag
& VL_SUSPENDED
)) {
4466 * if you are the owner of drain/suspend/termination , can acquire iocount
4467 * check for VL_TERMINATE; it does not set owner
4469 if ((vp
->v_lflag
& (VL_DRAIN
| VL_SUSPENDED
| VL_TERMINATE
)) &&
4470 (vp
->v_owner
== current_thread())) {
4478 * If this vnode is getting drained, there are some cases where
4481 if (vp
->v_lflag
& VL_DRAIN
) {
4483 * In some situations, we want to get an iocount
4484 * even if the vnode is draining to prevent deadlock,
4485 * e.g. if we're in the filesystem, potentially holding
4486 * resources that could prevent other iocounts from
4492 * Don't block if the vnode's mount point is unmounting as
4493 * we may be the thread the unmount is itself waiting on
4494 * Only callers who pass in vids (at this point, we've already
4495 * handled nosusp and nodead) are expecting error returns
4496 * from this function, so only we can only return errors for
4497 * those. ENODEV is intended to inform callers that the call
4498 * failed because an unmount is in progress.
4500 if (withvid
&& (vp
->v_mount
) && vfs_isunmount(vp
->v_mount
))
4504 vnode_lock_convert(vp
);
4506 if (vp
->v_lflag
& VL_TERMINATE
) {
4507 vp
->v_lflag
|= VL_TERMWANT
;
4509 msleep(&vp
->v_lflag
, &vp
->v_lock
, PVFS
, "vnode getiocount", NULL
);
4511 msleep(&vp
->v_iocount
, &vp
->v_lock
, PVFS
, "vnode_getiocount", NULL
);
4513 if (withvid
&& vid
!= vp
->v_id
) {
4516 if (++vp
->v_references
>= UNAGE_THRESHHOLD
||
4517 (vp
->v_flag
& VISDIRTY
&& vp
->v_references
>= UNAGE_DIRTYTHRESHHOLD
)) {
4518 vp
->v_references
= 0;
4519 vnode_list_remove(vp
);
4529 vnode_dropiocount (vnode_t vp
)
4531 if (vp
->v_iocount
< 1)
4532 panic("vnode_dropiocount(%p): v_iocount < 1", vp
);
4538 if ((vp
->v_lflag
& (VL_DRAIN
| VL_SUSPENDED
)) && (vp
->v_iocount
<= 1))
4539 wakeup(&vp
->v_iocount
);
4544 vnode_reclaim(struct vnode
* vp
)
4546 vnode_reclaim_internal(vp
, 0, 0, 0);
4551 vnode_reclaim_internal(struct vnode
* vp
, int locked
, int reuse
, int flags
)
4558 if (vp
->v_lflag
& VL_TERMINATE
) {
4559 panic("vnode reclaim in progress");
4561 vp
->v_lflag
|= VL_TERMINATE
;
4563 vn_clearunionwait(vp
, 1);
4567 isfifo
= (vp
->v_type
== VFIFO
);
4569 if (vp
->v_type
!= VBAD
)
4570 vgone(vp
, flags
); /* clean and reclaim the vnode */
4573 * give the vnode a new identity so that vnode_getwithvid will fail
4574 * on any stale cache accesses...
4575 * grab the list_lock so that if we're in "new_vnode"
4576 * behind the list_lock trying to steal this vnode, the v_id is stable...
4577 * once new_vnode drops the list_lock, it will block trying to take
4578 * the vnode lock until we release it... at that point it will evaluate
4579 * whether the v_vid has changed
4580 * also need to make sure that the vnode isn't on a list where "new_vnode"
4581 * can find it after the v_id has been bumped until we are completely done
4582 * with the vnode (i.e. putting it back on a list has to be the very last
4583 * thing we do to this vnode... many of the callers of vnode_reclaim_internal
4584 * are holding an io_count on the vnode... they need to drop the io_count
4585 * BEFORE doing a vnode_list_add or make sure to hold the vnode lock until
4586 * they are completely done with the vnode
4590 vnode_list_remove_locked(vp
);
4593 vnode_list_unlock();
4596 struct fifoinfo
* fip
;
4598 fip
= vp
->v_fifoinfo
;
4599 vp
->v_fifoinfo
= NULL
;
4605 panic("vnode_reclaim_internal: cleaned vnode isn't");
4606 if (vp
->v_numoutput
)
4607 panic("vnode_reclaim_internal: clean vnode has pending I/O's");
4608 if (UBCINFOEXISTS(vp
))
4609 panic("vnode_reclaim_internal: ubcinfo not cleaned");
4611 panic("vnode_reclaim_internal: vparent not removed");
4613 panic("vnode_reclaim_internal: vname not removed");
4615 vp
->v_socket
= NULL
;
4617 vp
->v_lflag
&= ~VL_TERMINATE
;
4620 KNOTE(&vp
->v_knotes
, NOTE_REVOKE
);
4622 /* Make sure that when we reuse the vnode, no knotes left over */
4623 klist_init(&vp
->v_knotes
);
4625 if (vp
->v_lflag
& VL_TERMWANT
) {
4626 vp
->v_lflag
&= ~VL_TERMWANT
;
4627 wakeup(&vp
->v_lflag
);
4631 * make sure we get on the
4632 * dead list if appropriate
4641 * The following api creates a vnode and associates all the parameter specified in vnode_fsparam
4642 * structure and returns a vnode handle with a reference. device aliasing is handled here so checkalias
4643 * is obsoleted by this.
4646 vnode_create(uint32_t flavor
, uint32_t size
, void *data
, vnode_t
*vpp
)
4654 struct componentname
*cnp
;
4655 struct vnode_fsparam
*param
= (struct vnode_fsparam
*)data
;
4657 struct vnode_trigger_param
*tinfo
= NULL
;
4662 /* Do quick sanity check on the parameters. */
4663 if (param
->vnfs_vtype
== VBAD
) {
4668 if ((flavor
== VNCREATE_TRIGGER
) && (size
== VNCREATE_TRIGGER_SIZE
)) {
4669 tinfo
= (struct vnode_trigger_param
*)data
;
4671 /* Validate trigger vnode input */
4672 if ((param
->vnfs_vtype
!= VDIR
) ||
4673 (tinfo
->vnt_resolve_func
== NULL
) ||
4674 (tinfo
->vnt_flags
& ~VNT_VALID_MASK
)) {
4677 /* Fall through a normal create (params will be the same) */
4678 flavor
= VNCREATE_FLAVOR
;
4682 if ((flavor
!= VNCREATE_FLAVOR
) || (size
!= VCREATESIZE
))
4685 if ( (error
= new_vnode(&vp
)) )
4688 dvp
= param
->vnfs_dvp
;
4689 cnp
= param
->vnfs_cnp
;
4691 vp
->v_op
= param
->vnfs_vops
;
4692 vp
->v_type
= param
->vnfs_vtype
;
4693 vp
->v_data
= param
->vnfs_fsnode
;
4695 if (param
->vnfs_markroot
)
4696 vp
->v_flag
|= VROOT
;
4697 if (param
->vnfs_marksystem
)
4698 vp
->v_flag
|= VSYSTEM
;
4699 if (vp
->v_type
== VREG
) {
4700 error
= ubc_info_init_withsize(vp
, param
->vnfs_filesize
);
4706 vp
->v_op
= dead_vnodeop_p
;
4710 vp
->v_lflag
|= VL_DEAD
;
4715 if (param
->vnfs_mp
->mnt_ioflags
& MNT_IOFLAGS_IOSCHED_SUPPORTED
)
4716 memory_object_mark_io_tracking(vp
->v_ubcinfo
->ui_control
);
4724 * For trigger vnodes, attach trigger info to vnode
4726 if ((vp
->v_type
== VDIR
) && (tinfo
!= NULL
)) {
4728 * Note: has a side effect of incrementing trigger count on the
4729 * mount if successful, which we would need to undo on a
4730 * subsequent failure.
4735 error
= vnode_resolver_create(param
->vnfs_mp
, vp
, tinfo
, FALSE
);
4737 printf("vnode_create: vnode_resolver_create() err %d\n", error
);
4739 vp
->v_op
= dead_vnodeop_p
;
4743 vp
->v_lflag
|= VL_DEAD
;
4752 if (vp
->v_type
== VCHR
|| vp
->v_type
== VBLK
) {
4754 vp
->v_tag
= VT_DEVFS
; /* callers will reset if needed (bdevvp) */
4756 if ( (nvp
= checkalias(vp
, param
->vnfs_rdev
)) ) {
4758 * if checkalias returns a vnode, it will be locked
4760 * first get rid of the unneeded vnode we acquired
4763 vp
->v_op
= spec_vnodeop_p
;
4765 vp
->v_lflag
= VL_DEAD
;
4771 * switch to aliased vnode and finish
4777 vp
->v_op
= param
->vnfs_vops
;
4778 vp
->v_type
= param
->vnfs_vtype
;
4779 vp
->v_data
= param
->vnfs_fsnode
;
4782 insmntque(vp
, param
->vnfs_mp
);
4787 if (VCHR
== vp
->v_type
) {
4788 u_int maj
= major(vp
->v_rdev
);
4790 if (maj
< (u_int
)nchrdev
&& cdevsw
[maj
].d_type
== D_TTY
)
4791 vp
->v_flag
|= VISTTY
;
4795 if (vp
->v_type
== VFIFO
) {
4796 struct fifoinfo
*fip
;
4798 MALLOC(fip
, struct fifoinfo
*,
4799 sizeof(*fip
), M_TEMP
, M_WAITOK
);
4800 bzero(fip
, sizeof(struct fifoinfo
));
4801 vp
->v_fifoinfo
= fip
;
4803 /* The file systems must pass the address of the location where
4804 * they store the vnode pointer. When we add the vnode into the mount
4805 * list and name cache they become discoverable. So the file system node
4806 * must have the connection to vnode setup by then
4810 /* Add fs named reference. */
4811 if (param
->vnfs_flags
& VNFS_ADDFSREF
) {
4812 vp
->v_lflag
|= VNAMED_FSHASH
;
4814 if (param
->vnfs_mp
) {
4815 if (param
->vnfs_mp
->mnt_kern_flag
& MNTK_LOCK_LOCAL
)
4816 vp
->v_flag
|= VLOCKLOCAL
;
4818 if ((vp
->v_freelist
.tqe_prev
!= (struct vnode
**)0xdeadb))
4819 panic("insmntque: vp on the free list\n");
4822 * enter in mount vnode list
4824 insmntque(vp
, param
->vnfs_mp
);
4827 if (dvp
&& vnode_ref(dvp
) == 0) {
4831 if (dvp
&& ((param
->vnfs_flags
& (VNFS_NOCACHE
| VNFS_CANTCACHE
)) == 0)) {
4833 * enter into name cache
4834 * we've got the info to enter it into the name cache now
4835 * cache_enter_create will pick up an extra reference on
4836 * the name entered into the string cache
4838 vp
->v_name
= cache_enter_create(dvp
, vp
, cnp
);
4840 vp
->v_name
= vfs_addname(cnp
->cn_nameptr
, cnp
->cn_namelen
, cnp
->cn_hash
, 0);
4842 if ((cnp
->cn_flags
& UNIONCREATED
) == UNIONCREATED
)
4843 vp
->v_flag
|= VISUNION
;
4845 if ((param
->vnfs_flags
& VNFS_CANTCACHE
) == 0) {
4847 * this vnode is being created as cacheable in the name cache
4848 * this allows us to re-enter it in the cache
4850 vp
->v_flag
|= VNCACHEABLE
;
4852 ut
= get_bsdthread_info(current_thread());
4854 if ((current_proc()->p_lflag
& P_LRAGE_VNODES
) ||
4855 (ut
->uu_flag
& UT_RAGE_VNODES
)) {
4857 * process has indicated that it wants any
4858 * vnodes created on its behalf to be rapidly
4859 * aged to reduce the impact on the cached set
4862 vp
->v_flag
|= VRAGE
;
4868 vnode_addfsref(vnode_t vp
)
4870 vnode_lock_spin(vp
);
4871 if (vp
->v_lflag
& VNAMED_FSHASH
)
4872 panic("add_fsref: vp already has named reference");
4873 if ((vp
->v_freelist
.tqe_prev
!= (struct vnode
**)0xdeadb))
4874 panic("addfsref: vp on the free list\n");
4875 vp
->v_lflag
|= VNAMED_FSHASH
;
4881 vnode_removefsref(vnode_t vp
)
4883 vnode_lock_spin(vp
);
4884 if ((vp
->v_lflag
& VNAMED_FSHASH
) == 0)
4885 panic("remove_fsref: no named reference");
4886 vp
->v_lflag
&= ~VNAMED_FSHASH
;
4894 vfs_iterate(int flags
, int (*callout
)(mount_t
, void *), void *arg
)
4899 int count
, actualcount
, i
;
4901 int indx_start
, indx_stop
, indx_incr
;
4902 int cb_dropref
= (flags
& VFS_ITERATE_CB_DROPREF
);
4904 count
= mount_getvfscnt();
4907 fsid_list
= (fsid_t
*)kalloc(count
* sizeof(fsid_t
));
4908 allocmem
= (void *)fsid_list
;
4910 actualcount
= mount_fillfsids(fsid_list
, count
);
4913 * Establish the iteration direction
4914 * VFS_ITERATE_TAIL_FIRST overrides default head first order (oldest first)
4916 if (flags
& VFS_ITERATE_TAIL_FIRST
) {
4917 indx_start
= actualcount
- 1;
4920 } else /* Head first by default */ {
4922 indx_stop
= actualcount
;
4926 for (i
=indx_start
; i
!= indx_stop
; i
+= indx_incr
) {
4928 /* obtain the mount point with iteration reference */
4929 mp
= mount_list_lookupby_fsid(&fsid_list
[i
], 0, 1);
4931 if(mp
== (struct mount
*)0)
4934 if (mp
->mnt_lflag
& (MNT_LDEAD
| MNT_LUNMOUNT
)) {
4942 /* iterate over all the vnodes */
4943 ret
= callout(mp
, arg
);
4946 * Drop the iterref here if the callback didn't do it.
4947 * Note: If cb_dropref is set the mp may no longer exist.
4954 case VFS_RETURNED_DONE
:
4955 if (ret
== VFS_RETURNED_DONE
) {
4961 case VFS_CLAIMED_DONE
:
4972 kfree(allocmem
, (count
* sizeof(fsid_t
)));
4977 * Update the vfsstatfs structure in the mountpoint.
4978 * MAC: Parameter eventtype added, indicating whether the event that
4979 * triggered this update came from user space, via a system call
4980 * (VFS_USER_EVENT) or an internal kernel call (VFS_KERNEL_EVENT).
4983 vfs_update_vfsstat(mount_t mp
, vfs_context_t ctx
, __unused
int eventtype
)
4989 * Request the attributes we want to propagate into
4990 * the per-mount vfsstat structure.
4993 VFSATTR_WANTED(&va
, f_iosize
);
4994 VFSATTR_WANTED(&va
, f_blocks
);
4995 VFSATTR_WANTED(&va
, f_bfree
);
4996 VFSATTR_WANTED(&va
, f_bavail
);
4997 VFSATTR_WANTED(&va
, f_bused
);
4998 VFSATTR_WANTED(&va
, f_files
);
4999 VFSATTR_WANTED(&va
, f_ffree
);
5000 VFSATTR_WANTED(&va
, f_bsize
);
5001 VFSATTR_WANTED(&va
, f_fssubtype
);
5003 if (eventtype
== VFS_USER_EVENT
) {
5004 error
= mac_mount_check_getattr(ctx
, mp
, &va
);
5010 if ((error
= vfs_getattr(mp
, &va
, ctx
)) != 0) {
5011 KAUTH_DEBUG("STAT - filesystem returned error %d", error
);
5016 * Unpack into the per-mount structure.
5018 * We only overwrite these fields, which are likely to change:
5026 * And these which are not, but which the FS has no other way
5027 * of providing to us:
5033 if (VFSATTR_IS_SUPPORTED(&va
, f_bsize
)) {
5034 /* 4822056 - protect against malformed server mount */
5035 mp
->mnt_vfsstat
.f_bsize
= (va
.f_bsize
> 0 ? va
.f_bsize
: 512);
5037 mp
->mnt_vfsstat
.f_bsize
= mp
->mnt_devblocksize
; /* default from the device block size */
5039 if (VFSATTR_IS_SUPPORTED(&va
, f_iosize
)) {
5040 mp
->mnt_vfsstat
.f_iosize
= va
.f_iosize
;
5042 mp
->mnt_vfsstat
.f_iosize
= 1024 * 1024; /* 1MB sensible I/O size */
5044 if (VFSATTR_IS_SUPPORTED(&va
, f_blocks
))
5045 mp
->mnt_vfsstat
.f_blocks
= va
.f_blocks
;
5046 if (VFSATTR_IS_SUPPORTED(&va
, f_bfree
))
5047 mp
->mnt_vfsstat
.f_bfree
= va
.f_bfree
;
5048 if (VFSATTR_IS_SUPPORTED(&va
, f_bavail
))
5049 mp
->mnt_vfsstat
.f_bavail
= va
.f_bavail
;
5050 if (VFSATTR_IS_SUPPORTED(&va
, f_bused
))
5051 mp
->mnt_vfsstat
.f_bused
= va
.f_bused
;
5052 if (VFSATTR_IS_SUPPORTED(&va
, f_files
))
5053 mp
->mnt_vfsstat
.f_files
= va
.f_files
;
5054 if (VFSATTR_IS_SUPPORTED(&va
, f_ffree
))
5055 mp
->mnt_vfsstat
.f_ffree
= va
.f_ffree
;
5057 /* this is unlikely to change, but has to be queried for */
5058 if (VFSATTR_IS_SUPPORTED(&va
, f_fssubtype
))
5059 mp
->mnt_vfsstat
.f_fssubtype
= va
.f_fssubtype
;
5065 mount_list_add(mount_t mp
)
5070 if (system_inshutdown
!= 0) {
5073 TAILQ_INSERT_TAIL(&mountlist
, mp
, mnt_list
);
5077 mount_list_unlock();
5083 mount_list_remove(mount_t mp
)
5086 TAILQ_REMOVE(&mountlist
, mp
, mnt_list
);
5088 mp
->mnt_list
.tqe_next
= NULL
;
5089 mp
->mnt_list
.tqe_prev
= NULL
;
5090 mount_list_unlock();
5094 mount_lookupby_volfsid(int volfs_id
, int withref
)
5096 mount_t cur_mount
= (mount_t
)0;
5100 TAILQ_FOREACH(mp
, &mountlist
, mnt_list
) {
5101 if (!(mp
->mnt_kern_flag
& MNTK_UNMOUNT
) &&
5102 (mp
->mnt_kern_flag
& MNTK_PATH_FROM_ID
) &&
5103 (mp
->mnt_vfsstat
.f_fsid
.val
[0] == volfs_id
)) {
5106 if (mount_iterref(cur_mount
, 1)) {
5107 cur_mount
= (mount_t
)0;
5108 mount_list_unlock();
5115 mount_list_unlock();
5116 if (withref
&& (cur_mount
!= (mount_t
)0)) {
5118 if (vfs_busy(mp
, LK_NOWAIT
) != 0) {
5119 cur_mount
= (mount_t
)0;
5128 mount_list_lookupby_fsid(fsid_t
*fsid
, int locked
, int withref
)
5130 mount_t retmp
= (mount_t
)0;
5135 TAILQ_FOREACH(mp
, &mountlist
, mnt_list
)
5136 if (mp
->mnt_vfsstat
.f_fsid
.val
[0] == fsid
->val
[0] &&
5137 mp
->mnt_vfsstat
.f_fsid
.val
[1] == fsid
->val
[1]) {
5140 if (mount_iterref(retmp
, 1))
5147 mount_list_unlock();
5152 vnode_lookup(const char *path
, int flags
, vnode_t
*vpp
, vfs_context_t ctx
)
5154 struct nameidata nd
;
5156 u_int32_t ndflags
= 0;
5162 if (flags
& VNODE_LOOKUP_NOFOLLOW
)
5167 if (flags
& VNODE_LOOKUP_NOCROSSMOUNT
)
5168 ndflags
|= NOCROSSMOUNT
;
5170 /* XXX AUDITVNPATH1 needed ? */
5171 NDINIT(&nd
, LOOKUP
, OP_LOOKUP
, ndflags
, UIO_SYSSPACE
,
5172 CAST_USER_ADDR_T(path
), ctx
);
5174 if ((error
= namei(&nd
)))
5183 vnode_open(const char *path
, int fmode
, int cmode
, int flags
, vnode_t
*vpp
, vfs_context_t ctx
)
5185 struct nameidata nd
;
5187 u_int32_t ndflags
= 0;
5190 if (ctx
== NULL
) { /* XXX technically an error */
5191 ctx
= vfs_context_current();
5194 if (fmode
& O_NOFOLLOW
)
5195 lflags
|= VNODE_LOOKUP_NOFOLLOW
;
5197 if (lflags
& VNODE_LOOKUP_NOFOLLOW
)
5202 if (lflags
& VNODE_LOOKUP_NOCROSSMOUNT
)
5203 ndflags
|= NOCROSSMOUNT
;
5205 /* XXX AUDITVNPATH1 needed ? */
5206 NDINIT(&nd
, LOOKUP
, OP_OPEN
, ndflags
, UIO_SYSSPACE
,
5207 CAST_USER_ADDR_T(path
), ctx
);
5209 if ((error
= vn_open(&nd
, fmode
, cmode
)))
5218 vnode_close(vnode_t vp
, int flags
, vfs_context_t ctx
)
5223 ctx
= vfs_context_current();
5226 error
= vn_close(vp
, flags
, ctx
);
5232 vnode_mtime(vnode_t vp
, struct timespec
*mtime
, vfs_context_t ctx
)
5234 struct vnode_attr va
;
5238 VATTR_WANTED(&va
, va_modify_time
);
5239 error
= vnode_getattr(vp
, &va
, ctx
);
5241 *mtime
= va
.va_modify_time
;
5246 vnode_flags(vnode_t vp
, uint32_t *flags
, vfs_context_t ctx
)
5248 struct vnode_attr va
;
5252 VATTR_WANTED(&va
, va_flags
);
5253 error
= vnode_getattr(vp
, &va
, ctx
);
5255 *flags
= va
.va_flags
;
5260 * Returns: 0 Success
5264 vnode_size(vnode_t vp
, off_t
*sizep
, vfs_context_t ctx
)
5266 struct vnode_attr va
;
5270 VATTR_WANTED(&va
, va_data_size
);
5271 error
= vnode_getattr(vp
, &va
, ctx
);
5273 *sizep
= va
.va_data_size
;
5278 vnode_setsize(vnode_t vp
, off_t size
, int ioflag
, vfs_context_t ctx
)
5280 struct vnode_attr va
;
5283 VATTR_SET(&va
, va_data_size
, size
);
5284 va
.va_vaflags
= ioflag
& 0xffff;
5285 return(vnode_setattr(vp
, &va
, ctx
));
5289 vnode_setdirty(vnode_t vp
)
5291 vnode_lock_spin(vp
);
5292 vp
->v_flag
|= VISDIRTY
;
5298 vnode_cleardirty(vnode_t vp
)
5300 vnode_lock_spin(vp
);
5301 vp
->v_flag
&= ~VISDIRTY
;
5307 vnode_isdirty(vnode_t vp
)
5311 vnode_lock_spin(vp
);
5312 dirty
= (vp
->v_flag
& VISDIRTY
) ? 1 : 0;
5319 vn_create_reg(vnode_t dvp
, vnode_t
*vpp
, struct nameidata
*ndp
, struct vnode_attr
*vap
, uint32_t flags
, int fmode
, uint32_t *statusp
, vfs_context_t ctx
)
5321 /* Only use compound VNOP for compound operation */
5322 if (vnode_compound_open_available(dvp
) && ((flags
& VN_CREATE_DOOPEN
) != 0)) {
5324 return VNOP_COMPOUND_OPEN(dvp
, vpp
, ndp
, O_CREAT
, fmode
, statusp
, vap
, ctx
);
5326 return VNOP_CREATE(dvp
, vpp
, &ndp
->ni_cnd
, vap
, ctx
);
5331 * Create a filesystem object of arbitrary type with arbitrary attributes in
5332 * the spevied directory with the specified name.
5334 * Parameters: dvp Pointer to the vnode of the directory
5335 * in which to create the object.
5336 * vpp Pointer to the area into which to
5337 * return the vnode of the created object.
5338 * cnp Component name pointer from the namei
5339 * data structure, containing the name to
5340 * use for the create object.
5341 * vap Pointer to the vnode_attr structure
5342 * describing the object to be created,
5343 * including the type of object.
5344 * flags VN_* flags controlling ACL inheritance
5345 * and whether or not authorization is to
5346 * be required for the operation.
5348 * Returns: 0 Success
5351 * Implicit: *vpp Contains the vnode of the object that
5352 * was created, if successful.
5353 * *cnp May be modified by the underlying VFS.
5354 * *vap May be modified by the underlying VFS.
5355 * modified by either ACL inheritance or
5358 * be modified, even if the operation is
5361 * Notes: The kauth_filesec_t in 'vap', if any, is in host byte order.
5363 * Modification of '*cnp' and '*vap' by the underlying VFS is
5364 * strongly discouraged.
5366 * XXX: This function is a 'vn_*' function; it belongs in vfs_vnops.c
5368 * XXX: We should enummerate the possible errno values here, and where
5369 * in the code they originated.
5372 vn_create(vnode_t dvp
, vnode_t
*vpp
, struct nameidata
*ndp
, struct vnode_attr
*vap
, uint32_t flags
, int fmode
, uint32_t *statusp
, vfs_context_t ctx
)
5374 errno_t error
, old_error
;
5375 vnode_t vp
= (vnode_t
)0;
5377 struct componentname
*cnp
;
5379 uint32_t dfflags
; // Directory file flags
5383 batched
= namei_compound_available(dvp
, ndp
) ? TRUE
: FALSE
;
5385 KAUTH_DEBUG("%p CREATE - '%s'", dvp
, cnp
->cn_nameptr
);
5387 if (flags
& VN_CREATE_NOINHERIT
)
5388 vap
->va_vaflags
|= VA_NOINHERIT
;
5389 if (flags
& VN_CREATE_NOAUTH
)
5390 vap
->va_vaflags
|= VA_NOAUTH
;
5392 * Handle ACL inheritance, initialize vap.
5394 error
= vn_attribute_prepare(dvp
, vap
, &defaulted
, ctx
);
5399 if (vap
->va_type
!= VREG
&& (fmode
!= 0 || (flags
& VN_CREATE_DOOPEN
) || statusp
)) {
5400 panic("Open parameters, but not a regular file.");
5402 if ((fmode
!= 0) && ((flags
& VN_CREATE_DOOPEN
) == 0)) {
5403 panic("Mode for open, but not trying to open...");
5407 * Handle inheritance of restricted flag
5409 error
= vnode_flags(dvp
, &dfflags
, ctx
);
5412 if (dfflags
& SF_RESTRICTED
)
5413 VATTR_SET(vap
, va_flags
, SF_RESTRICTED
);
5416 * Create the requested node.
5418 switch(vap
->va_type
) {
5420 error
= vn_create_reg(dvp
, vpp
, ndp
, vap
, flags
, fmode
, statusp
, ctx
);
5423 error
= vn_mkdir(dvp
, vpp
, ndp
, vap
, ctx
);
5429 error
= VNOP_MKNOD(dvp
, vpp
, cnp
, vap
, ctx
);
5432 panic("vnode_create: unknown vtype %d", vap
->va_type
);
5435 KAUTH_DEBUG("%p CREATE - error %d returned by filesystem", dvp
, error
);
5443 if (!(flags
& VN_CREATE_NOLABEL
)) {
5444 error
= vnode_label(vnode_mount(vp
), dvp
, vp
, cnp
, VNODE_LABEL_CREATE
, ctx
);
5451 * If some of the requested attributes weren't handled by the VNOP,
5452 * use our fallback code.
5454 if (!VATTR_ALL_SUPPORTED(vap
) && *vpp
) {
5455 KAUTH_DEBUG(" CREATE - doing fallback with ACL %p", vap
->va_acl
);
5456 error
= vnode_setattr_fallback(*vpp
, vap
, ctx
);
5461 if ((error
!= 0) && (vp
!= (vnode_t
)0)) {
5463 /* If we've done a compound open, close */
5464 if (batched
&& (old_error
== 0) && (vap
->va_type
== VREG
)) {
5465 VNOP_CLOSE(vp
, fmode
, ctx
);
5468 /* Need to provide notifications if a create succeeded */
5476 vn_attribute_cleanup(vap
, defaulted
);
5481 static kauth_scope_t vnode_scope
;
5482 static int vnode_authorize_callback(kauth_cred_t credential
, void *idata
, kauth_action_t action
,
5483 uintptr_t arg0
, uintptr_t arg1
, uintptr_t arg2
, uintptr_t arg3
);
5484 static int vnode_authorize_callback_int(__unused kauth_cred_t credential
, __unused
void *idata
, kauth_action_t action
,
5485 uintptr_t arg0
, uintptr_t arg1
, uintptr_t arg2
, uintptr_t arg3
);
5487 typedef struct _vnode_authorize_context
{
5489 struct vnode_attr
*vap
;
5491 struct vnode_attr
*dvap
;
5495 #define _VAC_IS_OWNER (1<<0)
5496 #define _VAC_IN_GROUP (1<<1)
5497 #define _VAC_IS_DIR_OWNER (1<<2)
5498 #define _VAC_IN_DIR_GROUP (1<<3)
5502 vnode_authorize_init(void)
5504 vnode_scope
= kauth_register_scope(KAUTH_SCOPE_VNODE
, vnode_authorize_callback
, NULL
);
5507 #define VATTR_PREPARE_DEFAULTED_UID 0x1
5508 #define VATTR_PREPARE_DEFAULTED_GID 0x2
5509 #define VATTR_PREPARE_DEFAULTED_MODE 0x4
5512 vn_attribute_prepare(vnode_t dvp
, struct vnode_attr
*vap
, uint32_t *defaulted_fieldsp
, vfs_context_t ctx
)
5514 kauth_acl_t nacl
= NULL
, oacl
= NULL
;
5518 * Handle ACL inheritance.
5520 if (!(vap
->va_vaflags
& VA_NOINHERIT
) && vfs_extendedsecurity(dvp
->v_mount
)) {
5521 /* save the original filesec */
5522 if (VATTR_IS_ACTIVE(vap
, va_acl
)) {
5527 if ((error
= kauth_acl_inherit(dvp
,
5530 vap
->va_type
== VDIR
,
5532 KAUTH_DEBUG("%p CREATE - error %d processing inheritance", dvp
, error
);
5537 * If the generated ACL is NULL, then we can save ourselves some effort
5538 * by clearing the active bit.
5541 VATTR_CLEAR_ACTIVE(vap
, va_acl
);
5543 vap
->va_base_acl
= oacl
;
5544 VATTR_SET(vap
, va_acl
, nacl
);
5548 error
= vnode_authattr_new_internal(dvp
, vap
, (vap
->va_vaflags
& VA_NOAUTH
), defaulted_fieldsp
, ctx
);
5550 vn_attribute_cleanup(vap
, *defaulted_fieldsp
);
5557 vn_attribute_cleanup(struct vnode_attr
*vap
, uint32_t defaulted_fields
)
5560 * If the caller supplied a filesec in vap, it has been replaced
5561 * now by the post-inheritance copy. We need to put the original back
5562 * and free the inherited product.
5564 kauth_acl_t nacl
, oacl
;
5566 if (VATTR_IS_ACTIVE(vap
, va_acl
)) {
5568 oacl
= vap
->va_base_acl
;
5571 VATTR_SET(vap
, va_acl
, oacl
);
5572 vap
->va_base_acl
= NULL
;
5574 VATTR_CLEAR_ACTIVE(vap
, va_acl
);
5578 kauth_acl_free(nacl
);
5582 if ((defaulted_fields
& VATTR_PREPARE_DEFAULTED_MODE
) != 0) {
5583 VATTR_CLEAR_ACTIVE(vap
, va_mode
);
5585 if ((defaulted_fields
& VATTR_PREPARE_DEFAULTED_GID
) != 0) {
5586 VATTR_CLEAR_ACTIVE(vap
, va_gid
);
5588 if ((defaulted_fields
& VATTR_PREPARE_DEFAULTED_UID
) != 0) {
5589 VATTR_CLEAR_ACTIVE(vap
, va_uid
);
5596 vn_authorize_unlink(vnode_t dvp
, vnode_t vp
, struct componentname
*cnp
, vfs_context_t ctx
, __unused
void *reserved
)
5604 * Normally, unlinking of directories is not supported.
5605 * However, some file systems may have limited support.
5607 if ((vp
->v_type
== VDIR
) &&
5608 !(vp
->v_mount
->mnt_vtable
->vfc_vfsflags
& VFC_VFSDIRLINKS
)) {
5609 return (EPERM
); /* POSIX */
5612 /* authorize the delete operation */
5615 error
= mac_vnode_check_unlink(ctx
, dvp
, vp
, cnp
);
5618 error
= vnode_authorize(vp
, dvp
, KAUTH_VNODE_DELETE
, ctx
);
5624 vn_authorize_open_existing(vnode_t vp
, struct componentname
*cnp
, int fmode
, vfs_context_t ctx
, void *reserved
)
5626 /* Open of existing case */
5627 kauth_action_t action
;
5629 if (cnp
->cn_ndp
== NULL
) {
5632 if (reserved
!= NULL
) {
5633 panic("reserved not NULL.");
5637 /* XXX may do duplicate work here, but ignore that for now (idempotent) */
5638 if (vfs_flags(vnode_mount(vp
)) & MNT_MULTILABEL
) {
5639 error
= vnode_label(vnode_mount(vp
), NULL
, vp
, NULL
, 0, ctx
);
5645 if ( (fmode
& O_DIRECTORY
) && vp
->v_type
!= VDIR
) {
5649 if (vp
->v_type
== VSOCK
&& vp
->v_tag
!= VT_FDESC
) {
5650 return (EOPNOTSUPP
); /* Operation not supported on socket */
5653 if (vp
->v_type
== VLNK
&& (fmode
& O_NOFOLLOW
) != 0) {
5654 return (ELOOP
); /* O_NOFOLLOW was specified and the target is a symbolic link */
5657 /* disallow write operations on directories */
5658 if (vnode_isdir(vp
) && (fmode
& (FWRITE
| O_TRUNC
))) {
5662 if ((cnp
->cn_ndp
->ni_flag
& NAMEI_TRAILINGSLASH
)) {
5663 if (vp
->v_type
!= VDIR
) {
5669 /* If a file being opened is a shadow file containing
5670 * namedstream data, ignore the macf checks because it
5671 * is a kernel internal file and access should always
5674 if (!(vnode_isshadow(vp
) && vnode_isnamedstream(vp
))) {
5675 error
= mac_vnode_check_open(ctx
, vp
, fmode
);
5682 /* compute action to be authorized */
5684 if (fmode
& FREAD
) {
5685 action
|= KAUTH_VNODE_READ_DATA
;
5687 if (fmode
& (FWRITE
| O_TRUNC
)) {
5689 * If we are writing, appending, and not truncating,
5690 * indicate that we are appending so that if the
5691 * UF_APPEND or SF_APPEND bits are set, we do not deny
5694 if ((fmode
& O_APPEND
) && !(fmode
& O_TRUNC
)) {
5695 action
|= KAUTH_VNODE_APPEND_DATA
;
5697 action
|= KAUTH_VNODE_WRITE_DATA
;
5700 error
= vnode_authorize(vp
, NULL
, action
, ctx
);
5702 if (error
== EACCES
) {
5704 * Shadow files may exist on-disk with a different UID/GID
5705 * than that of the current context. Verify that this file
5706 * is really a shadow file. If it was created successfully
5707 * then it should be authorized.
5709 if (vnode_isshadow(vp
) && vnode_isnamedstream (vp
)) {
5710 error
= vnode_verifynamedstream(vp
);
5719 vn_authorize_create(vnode_t dvp
, struct componentname
*cnp
, struct vnode_attr
*vap
, vfs_context_t ctx
, void *reserved
)
5727 if (cnp
->cn_ndp
== NULL
) {
5728 panic("NULL cn_ndp");
5730 if (reserved
!= NULL
) {
5731 panic("reserved not NULL.");
5734 /* Only validate path for creation if we didn't do a complete lookup */
5735 if (cnp
->cn_ndp
->ni_flag
& NAMEI_UNFINISHED
) {
5736 error
= lookup_validate_creation_path(cnp
->cn_ndp
);
5742 error
= mac_vnode_check_create(ctx
, dvp
, cnp
, vap
);
5745 #endif /* CONFIG_MACF */
5747 return (vnode_authorize(dvp
, NULL
, KAUTH_VNODE_ADD_FILE
, ctx
));
5751 vn_authorize_rename(struct vnode
*fdvp
, struct vnode
*fvp
, struct componentname
*fcnp
,
5752 struct vnode
*tdvp
, struct vnode
*tvp
, struct componentname
*tcnp
,
5753 vfs_context_t ctx
, void *reserved
)
5758 if (reserved
!= NULL
) {
5759 panic("Passed something other than NULL as reserved field!");
5763 * Avoid renaming "." and "..".
5765 * XXX No need to check for this in the FS. We should always have the leaves
5766 * in VFS in this case.
5768 if (fvp
->v_type
== VDIR
&&
5770 (fcnp
->cn_namelen
== 1 && fcnp
->cn_nameptr
[0] == '.') ||
5771 ((fcnp
->cn_flags
| tcnp
->cn_flags
) & ISDOTDOT
)) ) {
5776 if (tvp
== NULLVP
&& vnode_compound_rename_available(tdvp
)) {
5777 error
= lookup_validate_creation_path(tcnp
->cn_ndp
);
5782 /***** <MACF> *****/
5784 error
= mac_vnode_check_rename(ctx
, fdvp
, fvp
, fcnp
, tdvp
, tvp
, tcnp
);
5788 /***** </MACF> *****/
5790 /***** <MiscChecks> *****/
5792 if (fvp
->v_type
== VDIR
&& tvp
->v_type
!= VDIR
) {
5795 } else if (fvp
->v_type
!= VDIR
&& tvp
->v_type
== VDIR
) {
5807 * The following edge case is caught here:
5808 * (to cannot be a descendent of from)
5821 if (tdvp
->v_parent
== fvp
) {
5825 /***** </MiscChecks> *****/
5827 /***** <Kauth> *****/
5830 if ((tvp
!= NULL
) && vnode_isdir(tvp
)) {
5833 } else if (tdvp
!= fdvp
) {
5839 * must have delete rights to remove the old name even in
5840 * the simple case of fdvp == tdvp.
5842 * If fvp is a directory, and we are changing it's parent,
5843 * then we also need rights to rewrite its ".." entry as well.
5845 if (vnode_isdir(fvp
)) {
5846 if ((error
= vnode_authorize(fvp
, fdvp
, KAUTH_VNODE_DELETE
| KAUTH_VNODE_ADD_SUBDIRECTORY
, ctx
)) != 0)
5849 if ((error
= vnode_authorize(fvp
, fdvp
, KAUTH_VNODE_DELETE
, ctx
)) != 0)
5853 /* moving into tdvp or tvp, must have rights to add */
5854 if ((error
= vnode_authorize(((tvp
!= NULL
) && vnode_isdir(tvp
)) ? tvp
: tdvp
,
5856 vnode_isdir(fvp
) ? KAUTH_VNODE_ADD_SUBDIRECTORY
: KAUTH_VNODE_ADD_FILE
,
5861 /* node staying in same directory, must be allowed to add new name */
5862 if ((error
= vnode_authorize(fdvp
, NULL
,
5863 vnode_isdir(fvp
) ? KAUTH_VNODE_ADD_SUBDIRECTORY
: KAUTH_VNODE_ADD_FILE
, ctx
)) != 0)
5866 /* overwriting tvp */
5867 if ((tvp
!= NULL
) && !vnode_isdir(tvp
) &&
5868 ((error
= vnode_authorize(tvp
, tdvp
, KAUTH_VNODE_DELETE
, ctx
)) != 0)) {
5872 /***** </Kauth> *****/
5874 /* XXX more checks? */
5880 vn_authorize_mkdir(vnode_t dvp
, struct componentname
*cnp
, struct vnode_attr
*vap
, vfs_context_t ctx
, void *reserved
)
5887 if (reserved
!= NULL
) {
5888 panic("reserved not NULL in vn_authorize_mkdir()");
5891 /* XXX A hack for now, to make shadow files work */
5892 if (cnp
->cn_ndp
== NULL
) {
5896 if (vnode_compound_mkdir_available(dvp
)) {
5897 error
= lookup_validate_creation_path(cnp
->cn_ndp
);
5903 error
= mac_vnode_check_create(ctx
,
5909 /* authorize addition of a directory to the parent */
5910 if ((error
= vnode_authorize(dvp
, NULL
, KAUTH_VNODE_ADD_SUBDIRECTORY
, ctx
)) != 0)
5918 vn_authorize_rmdir(vnode_t dvp
, vnode_t vp
, struct componentname
*cnp
, vfs_context_t ctx
, void *reserved
)
5925 if (reserved
!= NULL
) {
5926 panic("Non-NULL reserved argument to vn_authorize_rmdir()");
5929 if (vp
->v_type
!= VDIR
) {
5931 * rmdir only deals with directories
5938 * No rmdir "." please.
5944 error
= mac_vnode_check_unlink(ctx
, dvp
,
5950 return vnode_authorize(vp
, dvp
, KAUTH_VNODE_DELETE
, ctx
);
5954 * Authorize an operation on a vnode.
5956 * This is KPI, but here because it needs vnode_scope.
5958 * Returns: 0 Success
5959 * kauth_authorize_action:EPERM ...
5960 * xlate => EACCES Permission denied
5961 * kauth_authorize_action:0 Success
5962 * kauth_authorize_action: Depends on callback return; this is
5963 * usually only vnode_authorize_callback(),
5964 * but may include other listerners, if any
5972 vnode_authorize(vnode_t vp
, vnode_t dvp
, kauth_action_t action
, vfs_context_t ctx
)
5977 * We can't authorize against a dead vnode; allow all operations through so that
5978 * the correct error can be returned.
5980 if (vp
->v_type
== VBAD
)
5984 result
= kauth_authorize_action(vnode_scope
, vfs_context_ucred(ctx
), action
,
5985 (uintptr_t)ctx
, (uintptr_t)vp
, (uintptr_t)dvp
, (uintptr_t)&error
);
5986 if (result
== EPERM
) /* traditional behaviour */
5988 /* did the lower layers give a better error return? */
5989 if ((result
!= 0) && (error
!= 0))
5995 * Test for vnode immutability.
5997 * The 'append' flag is set when the authorization request is constrained
5998 * to operations which only request the right to append to a file.
6000 * The 'ignore' flag is set when an operation modifying the immutability flags
6001 * is being authorized. We check the system securelevel to determine which
6002 * immutability flags we can ignore.
6005 vnode_immutable(struct vnode_attr
*vap
, int append
, int ignore
)
6009 /* start with all bits precluding the operation */
6010 mask
= IMMUTABLE
| APPEND
;
6012 /* if appending only, remove the append-only bits */
6016 /* ignore only set when authorizing flags changes */
6018 if (securelevel
<= 0) {
6019 /* in insecure state, flags do not inhibit changes */
6022 /* in secure state, user flags don't inhibit */
6023 mask
&= ~(UF_IMMUTABLE
| UF_APPEND
);
6026 KAUTH_DEBUG("IMMUTABLE - file flags 0x%x mask 0x%x append = %d ignore = %d", vap
->va_flags
, mask
, append
, ignore
);
6027 if ((vap
->va_flags
& mask
) != 0)
6033 vauth_node_owner(struct vnode_attr
*vap
, kauth_cred_t cred
)
6037 /* default assumption is not-owner */
6041 * If the filesystem has given us a UID, we treat this as authoritative.
6043 if (vap
&& VATTR_IS_SUPPORTED(vap
, va_uid
)) {
6044 result
= (vap
->va_uid
== kauth_cred_getuid(cred
)) ? 1 : 0;
6046 /* we could test the owner UUID here if we had a policy for it */
6054 * Description: Ask if a cred is a member of the group owning the vnode object
6056 * Parameters: vap vnode attribute
6057 * vap->va_gid group owner of vnode object
6058 * cred credential to check
6059 * ismember pointer to where to put the answer
6060 * idontknow Return this if we can't get an answer
6062 * Returns: 0 Success
6063 * idontknow Can't get information
6064 * kauth_cred_ismember_gid:? Error from kauth subsystem
6065 * kauth_cred_ismember_gid:? Error from kauth subsystem
6068 vauth_node_group(struct vnode_attr
*vap
, kauth_cred_t cred
, int *ismember
, int idontknow
)
6077 * The caller is expected to have asked the filesystem for a group
6078 * at some point prior to calling this function. The answer may
6079 * have been that there is no group ownership supported for the
6080 * vnode object, in which case we return
6082 if (vap
&& VATTR_IS_SUPPORTED(vap
, va_gid
)) {
6083 error
= kauth_cred_ismember_gid(cred
, vap
->va_gid
, &result
);
6085 * Credentials which are opted into external group membership
6086 * resolution which are not known to the external resolver
6087 * will result in an ENOENT error. We translate this into
6088 * the appropriate 'idontknow' response for our caller.
6090 * XXX We do not make a distinction here between an ENOENT
6091 * XXX arising from a response from the external resolver,
6092 * XXX and an ENOENT which is internally generated. This is
6093 * XXX a deficiency of the published kauth_cred_ismember_gid()
6094 * XXX KPI which can not be overcome without new KPI. For
6095 * XXX all currently known cases, however, this wil result
6096 * XXX in correct behaviour.
6098 if (error
== ENOENT
)
6102 * XXX We could test the group UUID here if we had a policy for it,
6103 * XXX but this is problematic from the perspective of synchronizing
6104 * XXX group UUID and POSIX GID ownership of a file and keeping the
6105 * XXX values coherent over time. The problem is that the local
6106 * XXX system will vend transient group UUIDs for unknown POSIX GID
6107 * XXX values, and these are not persistent, whereas storage of values
6108 * XXX is persistent. One potential solution to this is a local
6109 * XXX (persistent) replica of remote directory entries and vended
6110 * XXX local ids in a local directory server (think in terms of a
6111 * XXX caching DNS server).
6120 vauth_file_owner(vauth_ctx vcp
)
6124 if (vcp
->flags_valid
& _VAC_IS_OWNER
) {
6125 result
= (vcp
->flags
& _VAC_IS_OWNER
) ? 1 : 0;
6127 result
= vauth_node_owner(vcp
->vap
, vcp
->ctx
->vc_ucred
);
6129 /* cache our result */
6130 vcp
->flags_valid
|= _VAC_IS_OWNER
;
6132 vcp
->flags
|= _VAC_IS_OWNER
;
6134 vcp
->flags
&= ~_VAC_IS_OWNER
;
6142 * vauth_file_ingroup
6144 * Description: Ask if a user is a member of the group owning the directory
6146 * Parameters: vcp The vnode authorization context that
6147 * contains the user and directory info
6148 * vcp->flags_valid Valid flags
6149 * vcp->flags Flags values
6150 * vcp->vap File vnode attributes
6151 * vcp->ctx VFS Context (for user)
6152 * ismember pointer to where to put the answer
6153 * idontknow Return this if we can't get an answer
6155 * Returns: 0 Success
6156 * vauth_node_group:? Error from vauth_node_group()
6158 * Implicit returns: *ismember 0 The user is not a group member
6159 * 1 The user is a group member
6162 vauth_file_ingroup(vauth_ctx vcp
, int *ismember
, int idontknow
)
6166 /* Check for a cached answer first, to avoid the check if possible */
6167 if (vcp
->flags_valid
& _VAC_IN_GROUP
) {
6168 *ismember
= (vcp
->flags
& _VAC_IN_GROUP
) ? 1 : 0;
6171 /* Otherwise, go look for it */
6172 error
= vauth_node_group(vcp
->vap
, vcp
->ctx
->vc_ucred
, ismember
, idontknow
);
6175 /* cache our result */
6176 vcp
->flags_valid
|= _VAC_IN_GROUP
;
6178 vcp
->flags
|= _VAC_IN_GROUP
;
6180 vcp
->flags
&= ~_VAC_IN_GROUP
;
6189 vauth_dir_owner(vauth_ctx vcp
)
6193 if (vcp
->flags_valid
& _VAC_IS_DIR_OWNER
) {
6194 result
= (vcp
->flags
& _VAC_IS_DIR_OWNER
) ? 1 : 0;
6196 result
= vauth_node_owner(vcp
->dvap
, vcp
->ctx
->vc_ucred
);
6198 /* cache our result */
6199 vcp
->flags_valid
|= _VAC_IS_DIR_OWNER
;
6201 vcp
->flags
|= _VAC_IS_DIR_OWNER
;
6203 vcp
->flags
&= ~_VAC_IS_DIR_OWNER
;
6212 * Description: Ask if a user is a member of the group owning the directory
6214 * Parameters: vcp The vnode authorization context that
6215 * contains the user and directory info
6216 * vcp->flags_valid Valid flags
6217 * vcp->flags Flags values
6218 * vcp->dvap Dir vnode attributes
6219 * vcp->ctx VFS Context (for user)
6220 * ismember pointer to where to put the answer
6221 * idontknow Return this if we can't get an answer
6223 * Returns: 0 Success
6224 * vauth_node_group:? Error from vauth_node_group()
6226 * Implicit returns: *ismember 0 The user is not a group member
6227 * 1 The user is a group member
6230 vauth_dir_ingroup(vauth_ctx vcp
, int *ismember
, int idontknow
)
6234 /* Check for a cached answer first, to avoid the check if possible */
6235 if (vcp
->flags_valid
& _VAC_IN_DIR_GROUP
) {
6236 *ismember
= (vcp
->flags
& _VAC_IN_DIR_GROUP
) ? 1 : 0;
6239 /* Otherwise, go look for it */
6240 error
= vauth_node_group(vcp
->dvap
, vcp
->ctx
->vc_ucred
, ismember
, idontknow
);
6243 /* cache our result */
6244 vcp
->flags_valid
|= _VAC_IN_DIR_GROUP
;
6246 vcp
->flags
|= _VAC_IN_DIR_GROUP
;
6248 vcp
->flags
&= ~_VAC_IN_DIR_GROUP
;
6256 * Test the posix permissions in (vap) to determine whether (credential)
6257 * may perform (action)
6260 vnode_authorize_posix(vauth_ctx vcp
, int action
, int on_dir
)
6262 struct vnode_attr
*vap
;
6263 int needed
, error
, owner_ok
, group_ok
, world_ok
, ismember
;
6264 #ifdef KAUTH_DEBUG_ENABLE
6265 const char *where
= "uninitialized";
6266 # define _SETWHERE(c) where = c;
6268 # define _SETWHERE(c)
6271 /* checking file or directory? */
6281 * We want to do as little work here as possible. So first we check
6282 * which sets of permissions grant us the access we need, and avoid checking
6283 * whether specific permissions grant access when more generic ones would.
6286 /* owner permissions */
6290 if (action
& VWRITE
)
6294 owner_ok
= (needed
& vap
->va_mode
) == needed
;
6296 /* group permissions */
6300 if (action
& VWRITE
)
6304 group_ok
= (needed
& vap
->va_mode
) == needed
;
6306 /* world permissions */
6310 if (action
& VWRITE
)
6314 world_ok
= (needed
& vap
->va_mode
) == needed
;
6316 /* If granted/denied by all three, we're done */
6317 if (owner_ok
&& group_ok
&& world_ok
) {
6321 if (!owner_ok
&& !group_ok
&& !world_ok
) {
6327 /* Check ownership (relatively cheap) */
6328 if ((on_dir
&& vauth_dir_owner(vcp
)) ||
6329 (!on_dir
&& vauth_file_owner(vcp
))) {
6336 /* Not owner; if group and world both grant it we're done */
6337 if (group_ok
&& world_ok
) {
6338 _SETWHERE("group/world");
6341 if (!group_ok
&& !world_ok
) {
6342 _SETWHERE("group/world");
6347 /* Check group membership (most expensive) */
6348 ismember
= 0; /* Default to allow, if the target has no group owner */
6351 * In the case we can't get an answer about the user from the call to
6352 * vauth_dir_ingroup() or vauth_file_ingroup(), we want to fail on
6353 * the side of caution, rather than simply granting access, or we will
6354 * fail to correctly implement exclusion groups, so we set the third
6355 * parameter on the basis of the state of 'group_ok'.
6358 error
= vauth_dir_ingroup(vcp
, &ismember
, (!group_ok
? EACCES
: 0));
6360 error
= vauth_file_ingroup(vcp
, &ismember
, (!group_ok
? EACCES
: 0));
6374 /* Not owner, not in group, use world result */
6382 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",
6383 vcp
->vp
, (error
== 0) ? "ALLOWED" : "DENIED", where
,
6384 (action
& VREAD
) ? "r" : "-",
6385 (action
& VWRITE
) ? "w" : "-",
6386 (action
& VEXEC
) ? "x" : "-",
6388 (vap
->va_mode
& S_IRUSR
) ? "r" : "-",
6389 (vap
->va_mode
& S_IWUSR
) ? "w" : "-",
6390 (vap
->va_mode
& S_IXUSR
) ? "x" : "-",
6391 (vap
->va_mode
& S_IRGRP
) ? "r" : "-",
6392 (vap
->va_mode
& S_IWGRP
) ? "w" : "-",
6393 (vap
->va_mode
& S_IXGRP
) ? "x" : "-",
6394 (vap
->va_mode
& S_IROTH
) ? "r" : "-",
6395 (vap
->va_mode
& S_IWOTH
) ? "w" : "-",
6396 (vap
->va_mode
& S_IXOTH
) ? "x" : "-",
6397 kauth_cred_getuid(vcp
->ctx
->vc_ucred
),
6398 on_dir
? vcp
->dvap
->va_uid
: vcp
->vap
->va_uid
,
6399 on_dir
? vcp
->dvap
->va_gid
: vcp
->vap
->va_gid
);
6404 * Authorize the deletion of the node vp from the directory dvp.
6407 * - Neither the node nor the directory are immutable.
6408 * - The user is not the superuser.
6410 * Deletion is not permitted if the directory is sticky and the caller is
6411 * not owner of the node or directory.
6413 * If either the node grants DELETE, or the directory grants DELETE_CHILD,
6414 * the node may be deleted. If neither denies the permission, and the
6415 * caller has Posix write access to the directory, then the node may be
6418 * As an optimization, we cache whether or not delete child is permitted
6419 * on directories without the sticky bit set.
6422 vnode_authorize_delete(vauth_ctx vcp
, boolean_t cached_delete_child
);
6424 vnode_authorize_delete(vauth_ctx vcp
, boolean_t cached_delete_child
)
6426 struct vnode_attr
*vap
= vcp
->vap
;
6427 struct vnode_attr
*dvap
= vcp
->dvap
;
6428 kauth_cred_t cred
= vcp
->ctx
->vc_ucred
;
6429 struct kauth_acl_eval eval
;
6430 int error
, delete_denied
, delete_child_denied
, ismember
;
6432 /* check the ACL on the directory */
6433 delete_child_denied
= 0;
6434 if (!cached_delete_child
&& VATTR_IS_NOT(dvap
, va_acl
, NULL
)) {
6435 eval
.ae_requested
= KAUTH_VNODE_DELETE_CHILD
;
6436 eval
.ae_acl
= &dvap
->va_acl
->acl_ace
[0];
6437 eval
.ae_count
= dvap
->va_acl
->acl_entrycount
;
6438 eval
.ae_options
= 0;
6439 if (vauth_dir_owner(vcp
))
6440 eval
.ae_options
|= KAUTH_AEVAL_IS_OWNER
;
6442 * We use ENOENT as a marker to indicate we could not get
6443 * information in order to delay evaluation until after we
6444 * have the ACL evaluation answer. Previously, we would
6445 * always deny the operation at this point.
6447 if ((error
= vauth_dir_ingroup(vcp
, &ismember
, ENOENT
)) != 0 && error
!= ENOENT
)
6449 if (error
== ENOENT
)
6450 eval
.ae_options
|= KAUTH_AEVAL_IN_GROUP_UNKNOWN
;
6452 eval
.ae_options
|= KAUTH_AEVAL_IN_GROUP
;
6453 eval
.ae_exp_gall
= KAUTH_VNODE_GENERIC_ALL_BITS
;
6454 eval
.ae_exp_gread
= KAUTH_VNODE_GENERIC_READ_BITS
;
6455 eval
.ae_exp_gwrite
= KAUTH_VNODE_GENERIC_WRITE_BITS
;
6456 eval
.ae_exp_gexec
= KAUTH_VNODE_GENERIC_EXECUTE_BITS
;
6459 * If there is no entry, we are going to defer to other
6460 * authorization mechanisms.
6462 error
= kauth_acl_evaluate(cred
, &eval
);
6465 KAUTH_DEBUG("%p ERROR during ACL processing - %d", vcp
->vp
, error
);
6468 switch(eval
.ae_result
) {
6469 case KAUTH_RESULT_DENY
:
6470 delete_child_denied
= 1;
6473 case KAUTH_RESULT_ALLOW
:
6474 KAUTH_DEBUG("%p ALLOWED - granted by directory ACL", vcp
->vp
);
6476 case KAUTH_RESULT_DEFER
:
6478 /* Effectively the same as !delete_child_denied */
6479 KAUTH_DEBUG("%p DEFERRED - directory ACL", vcp
->vp
);
6484 /* check the ACL on the node */
6486 if (VATTR_IS_NOT(vap
, va_acl
, NULL
)) {
6487 eval
.ae_requested
= KAUTH_VNODE_DELETE
;
6488 eval
.ae_acl
= &vap
->va_acl
->acl_ace
[0];
6489 eval
.ae_count
= vap
->va_acl
->acl_entrycount
;
6490 eval
.ae_options
= 0;
6491 if (vauth_file_owner(vcp
))
6492 eval
.ae_options
|= KAUTH_AEVAL_IS_OWNER
;
6494 * We use ENOENT as a marker to indicate we could not get
6495 * information in order to delay evaluation until after we
6496 * have the ACL evaluation answer. Previously, we would
6497 * always deny the operation at this point.
6499 if ((error
= vauth_file_ingroup(vcp
, &ismember
, ENOENT
)) != 0 && error
!= ENOENT
)
6501 if (error
== ENOENT
)
6502 eval
.ae_options
|= KAUTH_AEVAL_IN_GROUP_UNKNOWN
;
6504 eval
.ae_options
|= KAUTH_AEVAL_IN_GROUP
;
6505 eval
.ae_exp_gall
= KAUTH_VNODE_GENERIC_ALL_BITS
;
6506 eval
.ae_exp_gread
= KAUTH_VNODE_GENERIC_READ_BITS
;
6507 eval
.ae_exp_gwrite
= KAUTH_VNODE_GENERIC_WRITE_BITS
;
6508 eval
.ae_exp_gexec
= KAUTH_VNODE_GENERIC_EXECUTE_BITS
;
6510 if ((error
= kauth_acl_evaluate(cred
, &eval
)) != 0) {
6511 KAUTH_DEBUG("%p ERROR during ACL processing - %d", vcp
->vp
, error
);
6515 switch(eval
.ae_result
) {
6516 case KAUTH_RESULT_DENY
:
6519 case KAUTH_RESULT_ALLOW
:
6520 KAUTH_DEBUG("%p ALLOWED - granted by file ACL", vcp
->vp
);
6522 case KAUTH_RESULT_DEFER
:
6524 /* Effectively the same as !delete_child_denied */
6525 KAUTH_DEBUG("%p DEFERRED%s - by file ACL", vcp
->vp
, delete_denied
? "(DENY)" : "");
6530 /* if denied by ACL on directory or node, return denial */
6531 if (delete_denied
|| delete_child_denied
) {
6532 KAUTH_DEBUG("%p DENIED - denied by ACL", vcp
->vp
);
6536 /* enforce sticky bit behaviour */
6537 if ((dvap
->va_mode
& S_ISTXT
) && !vauth_file_owner(vcp
) && !vauth_dir_owner(vcp
)) {
6538 KAUTH_DEBUG("%p DENIED - sticky bit rules (user %d file %d dir %d)",
6539 vcp
->vp
, cred
->cr_posix
.cr_uid
, vap
->va_uid
, dvap
->va_uid
);
6543 /* check the directory */
6544 if (!cached_delete_child
&& (error
= vnode_authorize_posix(vcp
, VWRITE
, 1 /* on_dir */)) != 0) {
6545 KAUTH_DEBUG("%p DENIED - denied by posix permisssions", vcp
->vp
);
6549 /* not denied, must be OK */
6555 * Authorize an operation based on the node's attributes.
6558 vnode_authorize_simple(vauth_ctx vcp
, kauth_ace_rights_t acl_rights
, kauth_ace_rights_t preauth_rights
, boolean_t
*found_deny
)
6560 struct vnode_attr
*vap
= vcp
->vap
;
6561 kauth_cred_t cred
= vcp
->ctx
->vc_ucred
;
6562 struct kauth_acl_eval eval
;
6563 int error
, ismember
;
6564 mode_t posix_action
;
6567 * If we are the file owner, we automatically have some rights.
6569 * Do we need to expand this to support group ownership?
6571 if (vauth_file_owner(vcp
))
6572 acl_rights
&= ~(KAUTH_VNODE_WRITE_SECURITY
);
6575 * If we are checking both TAKE_OWNERSHIP and WRITE_SECURITY, we can
6576 * mask the latter. If TAKE_OWNERSHIP is requested the caller is about to
6577 * change ownership to themselves, and WRITE_SECURITY is implicitly
6578 * granted to the owner. We need to do this because at this point
6579 * WRITE_SECURITY may not be granted as the caller is not currently
6582 if ((acl_rights
& KAUTH_VNODE_TAKE_OWNERSHIP
) &&
6583 (acl_rights
& KAUTH_VNODE_WRITE_SECURITY
))
6584 acl_rights
&= ~KAUTH_VNODE_WRITE_SECURITY
;
6586 if (acl_rights
== 0) {
6587 KAUTH_DEBUG("%p ALLOWED - implicit or no rights required", vcp
->vp
);
6591 /* if we have an ACL, evaluate it */
6592 if (VATTR_IS_NOT(vap
, va_acl
, NULL
)) {
6593 eval
.ae_requested
= acl_rights
;
6594 eval
.ae_acl
= &vap
->va_acl
->acl_ace
[0];
6595 eval
.ae_count
= vap
->va_acl
->acl_entrycount
;
6596 eval
.ae_options
= 0;
6597 if (vauth_file_owner(vcp
))
6598 eval
.ae_options
|= KAUTH_AEVAL_IS_OWNER
;
6600 * We use ENOENT as a marker to indicate we could not get
6601 * information in order to delay evaluation until after we
6602 * have the ACL evaluation answer. Previously, we would
6603 * always deny the operation at this point.
6605 if ((error
= vauth_file_ingroup(vcp
, &ismember
, ENOENT
)) != 0 && error
!= ENOENT
)
6607 if (error
== ENOENT
)
6608 eval
.ae_options
|= KAUTH_AEVAL_IN_GROUP_UNKNOWN
;
6610 eval
.ae_options
|= KAUTH_AEVAL_IN_GROUP
;
6611 eval
.ae_exp_gall
= KAUTH_VNODE_GENERIC_ALL_BITS
;
6612 eval
.ae_exp_gread
= KAUTH_VNODE_GENERIC_READ_BITS
;
6613 eval
.ae_exp_gwrite
= KAUTH_VNODE_GENERIC_WRITE_BITS
;
6614 eval
.ae_exp_gexec
= KAUTH_VNODE_GENERIC_EXECUTE_BITS
;
6616 if ((error
= kauth_acl_evaluate(cred
, &eval
)) != 0) {
6617 KAUTH_DEBUG("%p ERROR during ACL processing - %d", vcp
->vp
, error
);
6621 switch(eval
.ae_result
) {
6622 case KAUTH_RESULT_DENY
:
6623 KAUTH_DEBUG("%p DENIED - by ACL", vcp
->vp
);
6624 return(EACCES
); /* deny, deny, counter-allege */
6625 case KAUTH_RESULT_ALLOW
:
6626 KAUTH_DEBUG("%p ALLOWED - all rights granted by ACL", vcp
->vp
);
6628 case KAUTH_RESULT_DEFER
:
6630 /* Effectively the same as !delete_child_denied */
6631 KAUTH_DEBUG("%p DEFERRED - directory ACL", vcp
->vp
);
6635 *found_deny
= eval
.ae_found_deny
;
6637 /* fall through and evaluate residual rights */
6639 /* no ACL, everything is residual */
6640 eval
.ae_residual
= acl_rights
;
6644 * Grant residual rights that have been pre-authorized.
6646 eval
.ae_residual
&= ~preauth_rights
;
6649 * We grant WRITE_ATTRIBUTES to the owner if it hasn't been denied.
6651 if (vauth_file_owner(vcp
))
6652 eval
.ae_residual
&= ~KAUTH_VNODE_WRITE_ATTRIBUTES
;
6654 if (eval
.ae_residual
== 0) {
6655 KAUTH_DEBUG("%p ALLOWED - rights already authorized", vcp
->vp
);
6660 * Bail if we have residual rights that can't be granted by posix permissions,
6661 * or aren't presumed granted at this point.
6663 * XXX these can be collapsed for performance
6665 if (eval
.ae_residual
& KAUTH_VNODE_CHANGE_OWNER
) {
6666 KAUTH_DEBUG("%p DENIED - CHANGE_OWNER not permitted", vcp
->vp
);
6669 if (eval
.ae_residual
& KAUTH_VNODE_WRITE_SECURITY
) {
6670 KAUTH_DEBUG("%p DENIED - WRITE_SECURITY not permitted", vcp
->vp
);
6675 if (eval
.ae_residual
& KAUTH_VNODE_DELETE
)
6676 panic("vnode_authorize: can't be checking delete permission here");
6680 * Compute the fallback posix permissions that will satisfy the remaining
6684 if (eval
.ae_residual
& (KAUTH_VNODE_READ_DATA
|
6685 KAUTH_VNODE_LIST_DIRECTORY
|
6686 KAUTH_VNODE_READ_EXTATTRIBUTES
))
6687 posix_action
|= VREAD
;
6688 if (eval
.ae_residual
& (KAUTH_VNODE_WRITE_DATA
|
6689 KAUTH_VNODE_ADD_FILE
|
6690 KAUTH_VNODE_ADD_SUBDIRECTORY
|
6691 KAUTH_VNODE_DELETE_CHILD
|
6692 KAUTH_VNODE_WRITE_ATTRIBUTES
|
6693 KAUTH_VNODE_WRITE_EXTATTRIBUTES
))
6694 posix_action
|= VWRITE
;
6695 if (eval
.ae_residual
& (KAUTH_VNODE_EXECUTE
|
6696 KAUTH_VNODE_SEARCH
))
6697 posix_action
|= VEXEC
;
6699 if (posix_action
!= 0) {
6700 return(vnode_authorize_posix(vcp
, posix_action
, 0 /* !on_dir */));
6702 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",
6704 (eval
.ae_residual
& KAUTH_VNODE_READ_DATA
)
6705 ? vnode_isdir(vcp
->vp
) ? " LIST_DIRECTORY" : " READ_DATA" : "",
6706 (eval
.ae_residual
& KAUTH_VNODE_WRITE_DATA
)
6707 ? vnode_isdir(vcp
->vp
) ? " ADD_FILE" : " WRITE_DATA" : "",
6708 (eval
.ae_residual
& KAUTH_VNODE_EXECUTE
)
6709 ? vnode_isdir(vcp
->vp
) ? " SEARCH" : " EXECUTE" : "",
6710 (eval
.ae_residual
& KAUTH_VNODE_DELETE
)
6712 (eval
.ae_residual
& KAUTH_VNODE_APPEND_DATA
)
6713 ? vnode_isdir(vcp
->vp
) ? " ADD_SUBDIRECTORY" : " APPEND_DATA" : "",
6714 (eval
.ae_residual
& KAUTH_VNODE_DELETE_CHILD
)
6715 ? " DELETE_CHILD" : "",
6716 (eval
.ae_residual
& KAUTH_VNODE_READ_ATTRIBUTES
)
6717 ? " READ_ATTRIBUTES" : "",
6718 (eval
.ae_residual
& KAUTH_VNODE_WRITE_ATTRIBUTES
)
6719 ? " WRITE_ATTRIBUTES" : "",
6720 (eval
.ae_residual
& KAUTH_VNODE_READ_EXTATTRIBUTES
)
6721 ? " READ_EXTATTRIBUTES" : "",
6722 (eval
.ae_residual
& KAUTH_VNODE_WRITE_EXTATTRIBUTES
)
6723 ? " WRITE_EXTATTRIBUTES" : "",
6724 (eval
.ae_residual
& KAUTH_VNODE_READ_SECURITY
)
6725 ? " READ_SECURITY" : "",
6726 (eval
.ae_residual
& KAUTH_VNODE_WRITE_SECURITY
)
6727 ? " WRITE_SECURITY" : "",
6728 (eval
.ae_residual
& KAUTH_VNODE_CHECKIMMUTABLE
)
6729 ? " CHECKIMMUTABLE" : "",
6730 (eval
.ae_residual
& KAUTH_VNODE_CHANGE_OWNER
)
6731 ? " CHANGE_OWNER" : "");
6735 * Lack of required Posix permissions implies no reason to deny access.
6741 * Check for file immutability.
6744 vnode_authorize_checkimmutable(vnode_t vp
, struct vnode_attr
*vap
, int rights
, int ignore
)
6751 * Perform immutability checks for operations that change data.
6753 * Sockets, fifos and devices require special handling.
6755 switch(vp
->v_type
) {
6761 * Writing to these nodes does not change the filesystem data,
6762 * so forget that it's being tried.
6764 rights
&= ~KAUTH_VNODE_WRITE_DATA
;
6771 if (rights
& KAUTH_VNODE_WRITE_RIGHTS
) {
6773 /* check per-filesystem options if possible */
6777 /* check for no-EA filesystems */
6778 if ((rights
& KAUTH_VNODE_WRITE_EXTATTRIBUTES
) &&
6779 (vfs_flags(mp
) & MNT_NOUSERXATTR
)) {
6780 KAUTH_DEBUG("%p DENIED - filesystem disallowed extended attributes", vp
);
6781 error
= EACCES
; /* User attributes disabled */
6787 * check for file immutability. first, check if the requested rights are
6788 * allowable for a UF_APPEND file.
6791 if (vp
->v_type
== VDIR
) {
6792 if ((rights
& (KAUTH_VNODE_ADD_FILE
| KAUTH_VNODE_ADD_SUBDIRECTORY
| KAUTH_VNODE_WRITE_EXTATTRIBUTES
)) == rights
)
6795 if ((rights
& (KAUTH_VNODE_APPEND_DATA
| KAUTH_VNODE_WRITE_EXTATTRIBUTES
)) == rights
)
6798 if ((error
= vnode_immutable(vap
, append
, ignore
)) != 0) {
6799 KAUTH_DEBUG("%p DENIED - file is immutable", vp
);
6808 * Handle authorization actions for filesystems that advertise that the
6809 * server will be enforcing.
6811 * Returns: 0 Authorization should be handled locally
6812 * 1 Authorization was handled by the FS
6814 * Note: Imputed returns will only occur if the authorization request
6815 * was handled by the FS.
6817 * Imputed: *resultp, modified Return code from FS when the request is
6818 * handled by the FS.
6823 vnode_authorize_opaque(vnode_t vp
, int *resultp
, kauth_action_t action
, vfs_context_t ctx
)
6828 * If the vp is a device node, socket or FIFO it actually represents a local
6829 * endpoint, so we need to handle it locally.
6831 switch(vp
->v_type
) {
6842 * In the advisory request case, if the filesystem doesn't think it's reliable
6843 * we will attempt to formulate a result ourselves based on VNOP_GETATTR data.
6845 if ((action
& KAUTH_VNODE_ACCESS
) && !vfs_authopaqueaccess(vp
->v_mount
))
6849 * Let the filesystem have a say in the matter. It's OK for it to not implemnent
6850 * VNOP_ACCESS, as most will authorise inline with the actual request.
6852 if ((error
= VNOP_ACCESS(vp
, action
, ctx
)) != ENOTSUP
) {
6854 KAUTH_DEBUG("%p DENIED - opaque filesystem VNOP_ACCESS denied access", vp
);
6859 * Typically opaque filesystems do authorisation in-line, but exec is a special case. In
6860 * order to be reasonably sure that exec will be permitted, we try a bit harder here.
6862 if ((action
& KAUTH_VNODE_EXECUTE
) && (vp
->v_type
== VREG
)) {
6863 /* try a VNOP_OPEN for readonly access */
6864 if ((error
= VNOP_OPEN(vp
, FREAD
, ctx
)) != 0) {
6866 KAUTH_DEBUG("%p DENIED - EXECUTE denied because file could not be opened readonly", vp
);
6869 VNOP_CLOSE(vp
, FREAD
, ctx
);
6873 * We don't have any reason to believe that the request has to be denied at this point,
6874 * so go ahead and allow it.
6877 KAUTH_DEBUG("%p ALLOWED - bypassing access check for non-local filesystem", vp
);
6885 * Returns: KAUTH_RESULT_ALLOW
6888 * Imputed: *arg3, modified Error code in the deny case
6889 * EROFS Read-only file system
6890 * EACCES Permission denied
6891 * EPERM Operation not permitted [no execute]
6892 * vnode_getattr:ENOMEM Not enough space [only if has filesec]
6894 * vnode_authorize_opaque:*arg2 ???
6895 * vnode_authorize_checkimmutable:???
6896 * vnode_authorize_delete:???
6897 * vnode_authorize_simple:???
6902 vnode_authorize_callback(kauth_cred_t cred
, void *idata
, kauth_action_t action
,
6903 uintptr_t arg0
, uintptr_t arg1
, uintptr_t arg2
, uintptr_t arg3
)
6906 vnode_t cvp
= NULLVP
;
6908 int result
= KAUTH_RESULT_DENY
;
6909 int parent_iocount
= 0;
6910 int parent_action
; /* In case we need to use namedstream's data fork for cached rights*/
6912 ctx
= (vfs_context_t
)arg0
;
6914 dvp
= (vnode_t
)arg2
;
6917 * if there are 2 vnodes passed in, we don't know at
6918 * this point which rights to look at based on the
6919 * combined action being passed in... defer until later...
6920 * otherwise check the kauth 'rights' cache hung
6921 * off of the vnode we're interested in... if we've already
6922 * been granted the right we're currently interested in,
6923 * we can just return success... otherwise we'll go through
6924 * the process of authorizing the requested right(s)... if that
6925 * succeeds, we'll add the right(s) to the cache.
6926 * VNOP_SETATTR and VNOP_SETXATTR will invalidate this cache
6934 * For named streams on local-authorization volumes, rights are cached on the parent;
6935 * authorization is determined by looking at the parent's properties anyway, so storing
6936 * on the parent means that we don't recompute for the named stream and that if
6937 * we need to flush rights (e.g. on VNOP_SETATTR()) we don't need to track down the
6938 * stream to flush its cache separately. If we miss in the cache, then we authorize
6939 * as if there were no cached rights (passing the named stream vnode and desired rights to
6940 * vnode_authorize_callback_int()).
6942 * On an opaquely authorized volume, we don't know the relationship between the
6943 * data fork's properties and the rights granted on a stream. Thus, named stream vnodes
6944 * on such a volume are authorized directly (rather than using the parent) and have their
6945 * own caches. When a named stream vnode is created, we mark the parent as having a named
6946 * stream. On a VNOP_SETATTR() for the parent that may invalidate cached authorization, we
6947 * find the stream and flush its cache.
6949 if (vnode_isnamedstream(vp
) && (!vfs_authopaque(vp
->v_mount
))) {
6950 cvp
= vnode_getparent(vp
);
6951 if (cvp
!= NULLVP
) {
6955 goto defer
; /* If we can't use the parent, take the slow path */
6958 /* Have to translate some actions */
6959 parent_action
= action
;
6960 if (parent_action
& KAUTH_VNODE_READ_DATA
) {
6961 parent_action
&= ~KAUTH_VNODE_READ_DATA
;
6962 parent_action
|= KAUTH_VNODE_READ_EXTATTRIBUTES
;
6964 if (parent_action
& KAUTH_VNODE_WRITE_DATA
) {
6965 parent_action
&= ~KAUTH_VNODE_WRITE_DATA
;
6966 parent_action
|= KAUTH_VNODE_WRITE_EXTATTRIBUTES
;
6974 if (vnode_cache_is_authorized(cvp
, ctx
, parent_iocount
? parent_action
: action
) == TRUE
) {
6975 result
= KAUTH_RESULT_ALLOW
;
6979 result
= vnode_authorize_callback_int(cred
, idata
, action
, arg0
, arg1
, arg2
, arg3
);
6981 if (result
== KAUTH_RESULT_ALLOW
&& cvp
!= NULLVP
) {
6982 KAUTH_DEBUG("%p - caching action = %x", cvp
, action
);
6983 vnode_cache_authorized_action(cvp
, ctx
, action
);
6987 if (parent_iocount
) {
6996 vnode_authorize_callback_int(__unused kauth_cred_t unused_cred
, __unused
void *idata
, kauth_action_t action
,
6997 uintptr_t arg0
, uintptr_t arg1
, uintptr_t arg2
, uintptr_t arg3
)
6999 struct _vnode_authorize_context auth_context
;
7004 kauth_ace_rights_t rights
;
7005 struct vnode_attr va
, dva
;
7009 boolean_t parent_authorized_for_delete_child
= FALSE
;
7010 boolean_t found_deny
= FALSE
;
7011 boolean_t parent_ref
= FALSE
;
7013 vcp
= &auth_context
;
7014 ctx
= vcp
->ctx
= (vfs_context_t
)arg0
;
7015 vp
= vcp
->vp
= (vnode_t
)arg1
;
7016 dvp
= vcp
->dvp
= (vnode_t
)arg2
;
7017 errorp
= (int *)arg3
;
7019 * Note that we authorize against the context, not the passed cred
7020 * (the same thing anyway)
7022 cred
= ctx
->vc_ucred
;
7029 vcp
->flags
= vcp
->flags_valid
= 0;
7032 if ((ctx
== NULL
) || (vp
== NULL
) || (cred
== NULL
))
7033 panic("vnode_authorize: bad arguments (context %p vp %p cred %p)", ctx
, vp
, cred
);
7036 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)",
7037 vp
, vfs_context_proc(ctx
)->p_comm
,
7038 (action
& KAUTH_VNODE_ACCESS
) ? "access" : "auth",
7039 (action
& KAUTH_VNODE_READ_DATA
) ? vnode_isdir(vp
) ? " LIST_DIRECTORY" : " READ_DATA" : "",
7040 (action
& KAUTH_VNODE_WRITE_DATA
) ? vnode_isdir(vp
) ? " ADD_FILE" : " WRITE_DATA" : "",
7041 (action
& KAUTH_VNODE_EXECUTE
) ? vnode_isdir(vp
) ? " SEARCH" : " EXECUTE" : "",
7042 (action
& KAUTH_VNODE_DELETE
) ? " DELETE" : "",
7043 (action
& KAUTH_VNODE_APPEND_DATA
) ? vnode_isdir(vp
) ? " ADD_SUBDIRECTORY" : " APPEND_DATA" : "",
7044 (action
& KAUTH_VNODE_DELETE_CHILD
) ? " DELETE_CHILD" : "",
7045 (action
& KAUTH_VNODE_READ_ATTRIBUTES
) ? " READ_ATTRIBUTES" : "",
7046 (action
& KAUTH_VNODE_WRITE_ATTRIBUTES
) ? " WRITE_ATTRIBUTES" : "",
7047 (action
& KAUTH_VNODE_READ_EXTATTRIBUTES
) ? " READ_EXTATTRIBUTES" : "",
7048 (action
& KAUTH_VNODE_WRITE_EXTATTRIBUTES
) ? " WRITE_EXTATTRIBUTES" : "",
7049 (action
& KAUTH_VNODE_READ_SECURITY
) ? " READ_SECURITY" : "",
7050 (action
& KAUTH_VNODE_WRITE_SECURITY
) ? " WRITE_SECURITY" : "",
7051 (action
& KAUTH_VNODE_CHANGE_OWNER
) ? " CHANGE_OWNER" : "",
7052 (action
& KAUTH_VNODE_NOIMMUTABLE
) ? " (noimmutable)" : "",
7053 vnode_isdir(vp
) ? "directory" : "file",
7054 vp
->v_name
? vp
->v_name
: "<NULL>", action
, vp
, dvp
);
7057 * Extract the control bits from the action, everything else is
7060 noimmutable
= (action
& KAUTH_VNODE_NOIMMUTABLE
) ? 1 : 0;
7061 rights
= action
& ~(KAUTH_VNODE_ACCESS
| KAUTH_VNODE_NOIMMUTABLE
);
7063 if (rights
& KAUTH_VNODE_DELETE
) {
7066 panic("vnode_authorize: KAUTH_VNODE_DELETE test requires a directory");
7069 * check to see if we've already authorized the parent
7070 * directory for deletion of its children... if so, we
7071 * can skip a whole bunch of work... we will still have to
7072 * authorize that this specific child can be removed
7074 if (vnode_cache_is_authorized(dvp
, ctx
, KAUTH_VNODE_DELETE_CHILD
) == TRUE
)
7075 parent_authorized_for_delete_child
= TRUE
;
7081 * Check for read-only filesystems.
7083 if ((rights
& KAUTH_VNODE_WRITE_RIGHTS
) &&
7084 (vp
->v_mount
->mnt_flag
& MNT_RDONLY
) &&
7085 ((vp
->v_type
== VREG
) || (vp
->v_type
== VDIR
) ||
7086 (vp
->v_type
== VLNK
) || (vp
->v_type
== VCPLX
) ||
7087 (rights
& KAUTH_VNODE_DELETE
) || (rights
& KAUTH_VNODE_DELETE_CHILD
))) {
7093 * Check for noexec filesystems.
7095 if ((rights
& KAUTH_VNODE_EXECUTE
) && (vp
->v_type
== VREG
) && (vp
->v_mount
->mnt_flag
& MNT_NOEXEC
)) {
7101 * Handle cases related to filesystems with non-local enforcement.
7102 * This call can return 0, in which case we will fall through to perform a
7103 * check based on VNOP_GETATTR data. Otherwise it returns 1 and sets
7104 * an appropriate result, at which point we can return immediately.
7106 if ((vp
->v_mount
->mnt_kern_flag
& MNTK_AUTH_OPAQUE
) && vnode_authorize_opaque(vp
, &result
, action
, ctx
))
7110 * Get vnode attributes and extended security information for the vnode
7111 * and directory if required.
7113 VATTR_WANTED(&va
, va_mode
);
7114 VATTR_WANTED(&va
, va_uid
);
7115 VATTR_WANTED(&va
, va_gid
);
7116 VATTR_WANTED(&va
, va_flags
);
7117 VATTR_WANTED(&va
, va_acl
);
7118 if ((result
= vnode_getattr(vp
, &va
, ctx
)) != 0) {
7119 KAUTH_DEBUG("%p ERROR - failed to get vnode attributes - %d", vp
, result
);
7123 VATTR_WANTED(&dva
, va_mode
);
7124 VATTR_WANTED(&dva
, va_uid
);
7125 VATTR_WANTED(&dva
, va_gid
);
7126 VATTR_WANTED(&dva
, va_flags
);
7127 VATTR_WANTED(&dva
, va_acl
);
7128 if ((result
= vnode_getattr(dvp
, &dva
, ctx
)) != 0) {
7129 KAUTH_DEBUG("%p ERROR - failed to get directory vnode attributes - %d", vp
, result
);
7135 * If the vnode is an extended attribute data vnode (eg. a resource fork), *_DATA becomes
7138 if (vnode_isnamedstream(vp
)) {
7139 if (rights
& KAUTH_VNODE_READ_DATA
) {
7140 rights
&= ~KAUTH_VNODE_READ_DATA
;
7141 rights
|= KAUTH_VNODE_READ_EXTATTRIBUTES
;
7143 if (rights
& KAUTH_VNODE_WRITE_DATA
) {
7144 rights
&= ~KAUTH_VNODE_WRITE_DATA
;
7145 rights
|= KAUTH_VNODE_WRITE_EXTATTRIBUTES
;
7150 * Point 'vp' to the resource fork's parent for ACL checking
7152 if (vnode_isnamedstream(vp
) &&
7153 (vp
->v_parent
!= NULL
) &&
7154 (vget_internal(vp
->v_parent
, 0, VNODE_NODEAD
| VNODE_DRAINO
) == 0)) {
7156 vcp
->vp
= vp
= vp
->v_parent
;
7157 if (VATTR_IS_SUPPORTED(&va
, va_acl
) && (va
.va_acl
!= NULL
))
7158 kauth_acl_free(va
.va_acl
);
7160 VATTR_WANTED(&va
, va_mode
);
7161 VATTR_WANTED(&va
, va_uid
);
7162 VATTR_WANTED(&va
, va_gid
);
7163 VATTR_WANTED(&va
, va_flags
);
7164 VATTR_WANTED(&va
, va_acl
);
7165 if ((result
= vnode_getattr(vp
, &va
, ctx
)) != 0)
7170 * Check for immutability.
7172 * In the deletion case, parent directory immutability vetoes specific
7175 if ((result
= vnode_authorize_checkimmutable(vp
, &va
, rights
, noimmutable
)) != 0)
7177 if ((rights
& KAUTH_VNODE_DELETE
) &&
7178 parent_authorized_for_delete_child
== FALSE
&&
7179 ((result
= vnode_authorize_checkimmutable(dvp
, &dva
, KAUTH_VNODE_DELETE_CHILD
, 0)) != 0))
7183 * Clear rights that have been authorized by reaching this point, bail if nothing left to
7186 rights
&= ~(KAUTH_VNODE_LINKTARGET
| KAUTH_VNODE_CHECKIMMUTABLE
);
7191 * If we're not the superuser, authorize based on file properties;
7192 * note that even if parent_authorized_for_delete_child is TRUE, we
7193 * need to check on the node itself.
7195 if (!vfs_context_issuser(ctx
)) {
7196 /* process delete rights */
7197 if ((rights
& KAUTH_VNODE_DELETE
) &&
7198 ((result
= vnode_authorize_delete(vcp
, parent_authorized_for_delete_child
)) != 0))
7201 /* process remaining rights */
7202 if ((rights
& ~KAUTH_VNODE_DELETE
) &&
7203 (result
= vnode_authorize_simple(vcp
, rights
, rights
& KAUTH_VNODE_DELETE
, &found_deny
)) != 0)
7208 * Execute is only granted to root if one of the x bits is set. This check only
7209 * makes sense if the posix mode bits are actually supported.
7211 if ((rights
& KAUTH_VNODE_EXECUTE
) &&
7212 (vp
->v_type
== VREG
) &&
7213 VATTR_IS_SUPPORTED(&va
, va_mode
) &&
7214 !(va
.va_mode
& (S_IXUSR
| S_IXGRP
| S_IXOTH
))) {
7216 KAUTH_DEBUG("%p DENIED - root execute requires at least one x bit in 0x%x", vp
, va
.va_mode
);
7220 KAUTH_DEBUG("%p ALLOWED - caller is superuser", vp
);
7223 if (VATTR_IS_SUPPORTED(&va
, va_acl
) && (va
.va_acl
!= NULL
))
7224 kauth_acl_free(va
.va_acl
);
7225 if (VATTR_IS_SUPPORTED(&dva
, va_acl
) && (dva
.va_acl
!= NULL
))
7226 kauth_acl_free(dva
.va_acl
);
7232 KAUTH_DEBUG("%p DENIED - auth denied", vp
);
7233 return(KAUTH_RESULT_DENY
);
7235 if ((rights
& KAUTH_VNODE_SEARCH
) && found_deny
== FALSE
&& vp
->v_type
== VDIR
) {
7237 * if we were successfully granted the right to search this directory
7238 * and there were NO ACL DENYs for search and the posix permissions also don't
7239 * deny execute, we can synthesize a global right that allows anyone to
7240 * traverse this directory during a pathname lookup without having to
7241 * match the credential associated with this cache of rights.
7243 if (!VATTR_IS_SUPPORTED(&va
, va_mode
) ||
7244 ((va
.va_mode
& (S_IXUSR
| S_IXGRP
| S_IXOTH
)) ==
7245 (S_IXUSR
| S_IXGRP
| S_IXOTH
))) {
7246 vnode_cache_authorized_action(vp
, ctx
, KAUTH_VNODE_SEARCHBYANYONE
);
7249 if ((rights
& KAUTH_VNODE_DELETE
) && parent_authorized_for_delete_child
== FALSE
) {
7251 * parent was successfully and newly authorized for content deletions
7252 * add it to the cache, but only if it doesn't have the sticky
7253 * bit set on it. This same check is done earlier guarding
7254 * fetching of dva, and if we jumped to out without having done
7255 * this, we will have returned already because of a non-zero
7258 if (VATTR_IS_SUPPORTED(&dva
, va_mode
) &&
7259 !(dva
.va_mode
& (S_ISVTX
))) {
7260 /* OK to cache delete rights */
7261 KAUTH_DEBUG("%p - caching DELETE_CHILD rights", dvp
);
7262 vnode_cache_authorized_action(dvp
, ctx
, KAUTH_VNODE_DELETE_CHILD
);
7268 * Note that this implies that we will allow requests for no rights, as well as
7269 * for rights that we do not recognise. There should be none of these.
7271 KAUTH_DEBUG("%p ALLOWED - auth granted", vp
);
7272 return(KAUTH_RESULT_ALLOW
);
7276 vnode_authattr_new(vnode_t dvp
, struct vnode_attr
*vap
, int noauth
, vfs_context_t ctx
)
7278 return vnode_authattr_new_internal(dvp
, vap
, noauth
, NULL
, ctx
);
7282 * Check that the attribute information in vattr can be legally applied to
7283 * a new file by the context.
7286 vnode_authattr_new_internal(vnode_t dvp
, struct vnode_attr
*vap
, int noauth
, uint32_t *defaulted_fieldsp
, vfs_context_t ctx
)
7289 int has_priv_suser
, ismember
, defaulted_owner
, defaulted_group
, defaulted_mode
;
7296 if (defaulted_fieldsp
) {
7297 *defaulted_fieldsp
= 0;
7300 defaulted_owner
= defaulted_group
= defaulted_mode
= 0;
7303 * Require that the filesystem support extended security to apply any.
7305 if (!vfs_extendedsecurity(dvp
->v_mount
) &&
7306 (VATTR_IS_ACTIVE(vap
, va_acl
) || VATTR_IS_ACTIVE(vap
, va_uuuid
) || VATTR_IS_ACTIVE(vap
, va_guuid
))) {
7312 * Default some fields.
7317 * If the filesystem is mounted IGNORE_OWNERSHIP and an explicit owner is set, that
7318 * owner takes ownership of all new files.
7320 if ((dmp
->mnt_flag
& MNT_IGNORE_OWNERSHIP
) && (dmp
->mnt_fsowner
!= KAUTH_UID_NONE
)) {
7321 VATTR_SET(vap
, va_uid
, dmp
->mnt_fsowner
);
7322 defaulted_owner
= 1;
7324 if (!VATTR_IS_ACTIVE(vap
, va_uid
)) {
7325 /* default owner is current user */
7326 VATTR_SET(vap
, va_uid
, kauth_cred_getuid(vfs_context_ucred(ctx
)));
7327 defaulted_owner
= 1;
7332 * If the filesystem is mounted IGNORE_OWNERSHIP and an explicit grouo is set, that
7333 * group takes ownership of all new files.
7335 if ((dmp
->mnt_flag
& MNT_IGNORE_OWNERSHIP
) && (dmp
->mnt_fsgroup
!= KAUTH_GID_NONE
)) {
7336 VATTR_SET(vap
, va_gid
, dmp
->mnt_fsgroup
);
7337 defaulted_group
= 1;
7339 if (!VATTR_IS_ACTIVE(vap
, va_gid
)) {
7340 /* default group comes from parent object, fallback to current user */
7341 struct vnode_attr dva
;
7343 VATTR_WANTED(&dva
, va_gid
);
7344 if ((error
= vnode_getattr(dvp
, &dva
, ctx
)) != 0)
7346 if (VATTR_IS_SUPPORTED(&dva
, va_gid
)) {
7347 VATTR_SET(vap
, va_gid
, dva
.va_gid
);
7349 VATTR_SET(vap
, va_gid
, kauth_cred_getgid(vfs_context_ucred(ctx
)));
7351 defaulted_group
= 1;
7355 if (!VATTR_IS_ACTIVE(vap
, va_flags
))
7356 VATTR_SET(vap
, va_flags
, 0);
7358 /* default mode is everything, masked with current umask */
7359 if (!VATTR_IS_ACTIVE(vap
, va_mode
)) {
7360 VATTR_SET(vap
, va_mode
, ACCESSPERMS
& ~vfs_context_proc(ctx
)->p_fd
->fd_cmask
);
7361 KAUTH_DEBUG("ATTR - defaulting new file mode to %o from umask %o", vap
->va_mode
, vfs_context_proc(ctx
)->p_fd
->fd_cmask
);
7364 /* set timestamps to now */
7365 if (!VATTR_IS_ACTIVE(vap
, va_create_time
)) {
7366 nanotime(&vap
->va_create_time
);
7367 VATTR_SET_ACTIVE(vap
, va_create_time
);
7371 * Check for attempts to set nonsensical fields.
7373 if (vap
->va_active
& ~VNODE_ATTR_NEWOBJ
) {
7375 KAUTH_DEBUG("ATTR - ERROR - attempt to set unsupported new-file attributes %llx",
7376 vap
->va_active
& ~VNODE_ATTR_NEWOBJ
);
7381 * Quickly check for the applicability of any enforcement here.
7382 * Tests below maintain the integrity of the local security model.
7384 if (vfs_authopaque(dvp
->v_mount
))
7388 * We need to know if the caller is the superuser, or if the work is
7389 * otherwise already authorised.
7391 cred
= vfs_context_ucred(ctx
);
7393 /* doing work for the kernel */
7396 has_priv_suser
= vfs_context_issuser(ctx
);
7400 if (VATTR_IS_ACTIVE(vap
, va_flags
)) {
7401 if (has_priv_suser
) {
7402 if ((vap
->va_flags
& (UF_SETTABLE
| SF_SETTABLE
)) != vap
->va_flags
) {
7404 KAUTH_DEBUG(" DENIED - superuser attempt to set illegal flag(s)");
7408 if ((vap
->va_flags
& UF_SETTABLE
) != vap
->va_flags
) {
7410 KAUTH_DEBUG(" DENIED - user attempt to set illegal flag(s)");
7416 /* if not superuser, validate legality of new-item attributes */
7417 if (!has_priv_suser
) {
7418 if (!defaulted_mode
&& VATTR_IS_ACTIVE(vap
, va_mode
)) {
7420 if (vap
->va_mode
& S_ISGID
) {
7421 if ((error
= kauth_cred_ismember_gid(cred
, vap
->va_gid
, &ismember
)) != 0) {
7422 KAUTH_DEBUG("ATTR - ERROR: got %d checking for membership in %d", error
, vap
->va_gid
);
7426 KAUTH_DEBUG(" DENIED - can't set SGID bit, not a member of %d", vap
->va_gid
);
7433 if ((vap
->va_mode
& S_ISUID
) && (vap
->va_uid
!= kauth_cred_getuid(cred
))) {
7434 KAUTH_DEBUG("ATTR - ERROR: illegal attempt to set the setuid bit");
7439 if (!defaulted_owner
&& (vap
->va_uid
!= kauth_cred_getuid(cred
))) {
7440 KAUTH_DEBUG(" DENIED - cannot create new item owned by %d", vap
->va_uid
);
7444 if (!defaulted_group
) {
7445 if ((error
= kauth_cred_ismember_gid(cred
, vap
->va_gid
, &ismember
)) != 0) {
7446 KAUTH_DEBUG(" ERROR - got %d checking for membership in %d", error
, vap
->va_gid
);
7450 KAUTH_DEBUG(" DENIED - cannot create new item with group %d - not a member", vap
->va_gid
);
7456 /* initialising owner/group UUID */
7457 if (VATTR_IS_ACTIVE(vap
, va_uuuid
)) {
7458 if ((error
= kauth_cred_getguid(cred
, &changer
)) != 0) {
7459 KAUTH_DEBUG(" ERROR - got %d trying to get caller UUID", error
);
7460 /* XXX ENOENT here - no GUID - should perhaps become EPERM */
7463 if (!kauth_guid_equal(&vap
->va_uuuid
, &changer
)) {
7464 KAUTH_DEBUG(" ERROR - cannot create item with supplied owner UUID - not us");
7469 if (VATTR_IS_ACTIVE(vap
, va_guuid
)) {
7470 if ((error
= kauth_cred_ismember_guid(cred
, &vap
->va_guuid
, &ismember
)) != 0) {
7471 KAUTH_DEBUG(" ERROR - got %d trying to check group membership", error
);
7475 KAUTH_DEBUG(" ERROR - cannot create item with supplied group UUID - not a member");
7482 if (defaulted_fieldsp
) {
7483 if (defaulted_mode
) {
7484 *defaulted_fieldsp
|= VATTR_PREPARE_DEFAULTED_MODE
;
7486 if (defaulted_group
) {
7487 *defaulted_fieldsp
|= VATTR_PREPARE_DEFAULTED_GID
;
7489 if (defaulted_owner
) {
7490 *defaulted_fieldsp
|= VATTR_PREPARE_DEFAULTED_UID
;
7497 * Check that the attribute information in vap can be legally written by the
7500 * Call this when you're not sure about the vnode_attr; either its contents
7501 * have come from an unknown source, or when they are variable.
7503 * Returns errno, or zero and sets *actionp to the KAUTH_VNODE_* actions that
7504 * must be authorized to be permitted to write the vattr.
7507 vnode_authattr(vnode_t vp
, struct vnode_attr
*vap
, kauth_action_t
*actionp
, vfs_context_t ctx
)
7509 struct vnode_attr ova
;
7510 kauth_action_t required_action
;
7511 int error
, has_priv_suser
, ismember
, chowner
, chgroup
, clear_suid
, clear_sgid
;
7520 required_action
= 0;
7524 * Quickly check for enforcement applicability.
7526 if (vfs_authopaque(vp
->v_mount
))
7530 * Check for attempts to set nonsensical fields.
7532 if (vap
->va_active
& VNODE_ATTR_RDONLY
) {
7533 KAUTH_DEBUG("ATTR - ERROR: attempt to set readonly attribute(s)");
7539 * We need to know if the caller is the superuser.
7541 cred
= vfs_context_ucred(ctx
);
7542 has_priv_suser
= kauth_cred_issuser(cred
);
7545 * If any of the following are changing, we need information from the old file:
7552 if (VATTR_IS_ACTIVE(vap
, va_uid
) ||
7553 VATTR_IS_ACTIVE(vap
, va_gid
) ||
7554 VATTR_IS_ACTIVE(vap
, va_mode
) ||
7555 VATTR_IS_ACTIVE(vap
, va_uuuid
) ||
7556 VATTR_IS_ACTIVE(vap
, va_guuid
)) {
7557 VATTR_WANTED(&ova
, va_mode
);
7558 VATTR_WANTED(&ova
, va_uid
);
7559 VATTR_WANTED(&ova
, va_gid
);
7560 VATTR_WANTED(&ova
, va_uuuid
);
7561 VATTR_WANTED(&ova
, va_guuid
);
7562 KAUTH_DEBUG("ATTR - security information changing, fetching existing attributes");
7566 * If timestamps are being changed, we need to know who the file is owned
7569 if (VATTR_IS_ACTIVE(vap
, va_create_time
) ||
7570 VATTR_IS_ACTIVE(vap
, va_change_time
) ||
7571 VATTR_IS_ACTIVE(vap
, va_modify_time
) ||
7572 VATTR_IS_ACTIVE(vap
, va_access_time
) ||
7573 VATTR_IS_ACTIVE(vap
, va_backup_time
)) {
7575 VATTR_WANTED(&ova
, va_uid
);
7576 #if 0 /* enable this when we support UUIDs as official owners */
7577 VATTR_WANTED(&ova
, va_uuuid
);
7579 KAUTH_DEBUG("ATTR - timestamps changing, fetching uid and GUID");
7583 * If flags are being changed, we need the old flags.
7585 if (VATTR_IS_ACTIVE(vap
, va_flags
)) {
7586 KAUTH_DEBUG("ATTR - flags changing, fetching old flags");
7587 VATTR_WANTED(&ova
, va_flags
);
7591 * If ACLs are being changed, we need the old ACLs.
7593 if (VATTR_IS_ACTIVE(vap
, va_acl
)) {
7594 KAUTH_DEBUG("ATTR - acl changing, fetching old flags");
7595 VATTR_WANTED(&ova
, va_acl
);
7599 * If the size is being set, make sure it's not a directory.
7601 if (VATTR_IS_ACTIVE(vap
, va_data_size
)) {
7602 /* size is meaningless on a directory, don't permit this */
7603 if (vnode_isdir(vp
)) {
7604 KAUTH_DEBUG("ATTR - ERROR: size change requested on a directory");
7613 KAUTH_DEBUG("ATTR - fetching old attributes %016llx", ova
.va_active
);
7614 if ((error
= vnode_getattr(vp
, &ova
, ctx
)) != 0) {
7615 KAUTH_DEBUG(" ERROR - got %d trying to get attributes", error
);
7620 * Size changes require write access to the file data.
7622 if (VATTR_IS_ACTIVE(vap
, va_data_size
)) {
7623 /* if we can't get the size, or it's different, we need write access */
7624 KAUTH_DEBUG("ATTR - size change, requiring WRITE_DATA");
7625 required_action
|= KAUTH_VNODE_WRITE_DATA
;
7629 * Changing timestamps?
7631 * Note that we are only called to authorize user-requested time changes;
7632 * side-effect time changes are not authorized. Authorisation is only
7633 * required for existing files.
7635 * Non-owners are not permitted to change the time on an existing
7636 * file to anything other than the current time.
7638 if (VATTR_IS_ACTIVE(vap
, va_create_time
) ||
7639 VATTR_IS_ACTIVE(vap
, va_change_time
) ||
7640 VATTR_IS_ACTIVE(vap
, va_modify_time
) ||
7641 VATTR_IS_ACTIVE(vap
, va_access_time
) ||
7642 VATTR_IS_ACTIVE(vap
, va_backup_time
)) {
7644 * The owner and root may set any timestamps they like,
7645 * provided that the file is not immutable. The owner still needs
7646 * WRITE_ATTRIBUTES (implied by ownership but still deniable).
7648 if (has_priv_suser
|| vauth_node_owner(&ova
, cred
)) {
7649 KAUTH_DEBUG("ATTR - root or owner changing timestamps");
7650 required_action
|= KAUTH_VNODE_CHECKIMMUTABLE
| KAUTH_VNODE_WRITE_ATTRIBUTES
;
7652 /* just setting the current time? */
7653 if (vap
->va_vaflags
& VA_UTIMES_NULL
) {
7654 KAUTH_DEBUG("ATTR - non-root/owner changing timestamps, requiring WRITE_ATTRIBUTES");
7655 required_action
|= KAUTH_VNODE_WRITE_ATTRIBUTES
;
7657 KAUTH_DEBUG("ATTR - ERROR: illegal timestamp modification attempted");
7665 * Changing file mode?
7667 if (VATTR_IS_ACTIVE(vap
, va_mode
) && VATTR_IS_SUPPORTED(&ova
, va_mode
) && (ova
.va_mode
!= vap
->va_mode
)) {
7668 KAUTH_DEBUG("ATTR - mode change from %06o to %06o", ova
.va_mode
, vap
->va_mode
);
7671 * Mode changes always have the same basic auth requirements.
7673 if (has_priv_suser
) {
7674 KAUTH_DEBUG("ATTR - superuser mode change, requiring immutability check");
7675 required_action
|= KAUTH_VNODE_CHECKIMMUTABLE
;
7677 /* need WRITE_SECURITY */
7678 KAUTH_DEBUG("ATTR - non-superuser mode change, requiring WRITE_SECURITY");
7679 required_action
|= KAUTH_VNODE_WRITE_SECURITY
;
7683 * Can't set the setgid bit if you're not in the group and not root. Have to have
7684 * existing group information in the case we're not setting it right now.
7686 if (vap
->va_mode
& S_ISGID
) {
7687 required_action
|= KAUTH_VNODE_CHECKIMMUTABLE
; /* always required */
7688 if (!has_priv_suser
) {
7689 if (VATTR_IS_ACTIVE(vap
, va_gid
)) {
7690 group
= vap
->va_gid
;
7691 } else if (VATTR_IS_SUPPORTED(&ova
, va_gid
)) {
7694 KAUTH_DEBUG("ATTR - ERROR: setgid but no gid available");
7699 * This might be too restrictive; WRITE_SECURITY might be implied by
7700 * membership in this case, rather than being an additional requirement.
7702 if ((error
= kauth_cred_ismember_gid(cred
, group
, &ismember
)) != 0) {
7703 KAUTH_DEBUG("ATTR - ERROR: got %d checking for membership in %d", error
, vap
->va_gid
);
7707 KAUTH_DEBUG(" DENIED - can't set SGID bit, not a member of %d", group
);
7715 * Can't set the setuid bit unless you're root or the file's owner.
7717 if (vap
->va_mode
& S_ISUID
) {
7718 required_action
|= KAUTH_VNODE_CHECKIMMUTABLE
; /* always required */
7719 if (!has_priv_suser
) {
7720 if (VATTR_IS_ACTIVE(vap
, va_uid
)) {
7721 owner
= vap
->va_uid
;
7722 } else if (VATTR_IS_SUPPORTED(&ova
, va_uid
)) {
7725 KAUTH_DEBUG("ATTR - ERROR: setuid but no uid available");
7729 if (owner
!= kauth_cred_getuid(cred
)) {
7731 * We could allow this if WRITE_SECURITY is permitted, perhaps.
7733 KAUTH_DEBUG("ATTR - ERROR: illegal attempt to set the setuid bit");
7742 * Validate/mask flags changes. This checks that only the flags in
7743 * the UF_SETTABLE mask are being set, and preserves the flags in
7744 * the SF_SETTABLE case.
7746 * Since flags changes may be made in conjunction with other changes,
7747 * we will ask the auth code to ignore immutability in the case that
7748 * the SF_* flags are not set and we are only manipulating the file flags.
7751 if (VATTR_IS_ACTIVE(vap
, va_flags
)) {
7752 /* compute changing flags bits */
7753 if (VATTR_IS_SUPPORTED(&ova
, va_flags
)) {
7754 fdelta
= vap
->va_flags
^ ova
.va_flags
;
7756 fdelta
= vap
->va_flags
;
7760 KAUTH_DEBUG("ATTR - flags changing, requiring WRITE_SECURITY");
7761 required_action
|= KAUTH_VNODE_WRITE_SECURITY
;
7763 /* check that changing bits are legal */
7764 if (has_priv_suser
) {
7766 * The immutability check will prevent us from clearing the SF_*
7767 * flags unless the system securelevel permits it, so just check
7768 * for legal flags here.
7770 if (fdelta
& ~(UF_SETTABLE
| SF_SETTABLE
)) {
7772 KAUTH_DEBUG(" DENIED - superuser attempt to set illegal flag(s)");
7776 if (fdelta
& ~UF_SETTABLE
) {
7778 KAUTH_DEBUG(" DENIED - user attempt to set illegal flag(s)");
7783 * If the caller has the ability to manipulate file flags,
7784 * security is not reduced by ignoring them for this operation.
7786 * A more complete test here would consider the 'after' states of the flags
7787 * to determine whether it would permit the operation, but this becomes
7790 * Ignoring immutability is conditional on securelevel; this does not bypass
7791 * the SF_* flags if securelevel > 0.
7793 required_action
|= KAUTH_VNODE_NOIMMUTABLE
;
7798 * Validate ownership information.
7807 * Note that if the filesystem didn't give us a UID, we expect that it doesn't
7808 * support them in general, and will ignore it if/when we try to set it.
7809 * We might want to clear the uid out of vap completely here.
7811 if (VATTR_IS_ACTIVE(vap
, va_uid
)) {
7812 if (VATTR_IS_SUPPORTED(&ova
, va_uid
) && (vap
->va_uid
!= ova
.va_uid
)) {
7813 if (!has_priv_suser
&& (kauth_cred_getuid(cred
) != vap
->va_uid
)) {
7814 KAUTH_DEBUG(" DENIED - non-superuser cannot change ownershipt to a third party");
7825 * Note that if the filesystem didn't give us a GID, we expect that it doesn't
7826 * support them in general, and will ignore it if/when we try to set it.
7827 * We might want to clear the gid out of vap completely here.
7829 if (VATTR_IS_ACTIVE(vap
, va_gid
)) {
7830 if (VATTR_IS_SUPPORTED(&ova
, va_gid
) && (vap
->va_gid
!= ova
.va_gid
)) {
7831 if (!has_priv_suser
) {
7832 if ((error
= kauth_cred_ismember_gid(cred
, vap
->va_gid
, &ismember
)) != 0) {
7833 KAUTH_DEBUG(" ERROR - got %d checking for membership in %d", error
, vap
->va_gid
);
7837 KAUTH_DEBUG(" DENIED - group change from %d to %d but not a member of target group",
7838 ova
.va_gid
, vap
->va_gid
);
7849 * Owner UUID being set or changed.
7851 if (VATTR_IS_ACTIVE(vap
, va_uuuid
)) {
7852 /* if the owner UUID is not actually changing ... */
7853 if (VATTR_IS_SUPPORTED(&ova
, va_uuuid
)) {
7854 if (kauth_guid_equal(&vap
->va_uuuid
, &ova
.va_uuuid
))
7855 goto no_uuuid_change
;
7858 * If the current owner UUID is a null GUID, check
7859 * it against the UUID corresponding to the owner UID.
7861 if (kauth_guid_equal(&ova
.va_uuuid
, &kauth_null_guid
) &&
7862 VATTR_IS_SUPPORTED(&ova
, va_uid
)) {
7865 if (kauth_cred_uid2guid(ova
.va_uid
, &uid_guid
) == 0 &&
7866 kauth_guid_equal(&vap
->va_uuuid
, &uid_guid
))
7867 goto no_uuuid_change
;
7872 * The owner UUID cannot be set by a non-superuser to anything other than
7873 * their own or a null GUID (to "unset" the owner UUID).
7874 * Note that file systems must be prepared to handle the
7875 * null UUID case in a manner appropriate for that file
7878 if (!has_priv_suser
) {
7879 if ((error
= kauth_cred_getguid(cred
, &changer
)) != 0) {
7880 KAUTH_DEBUG(" ERROR - got %d trying to get caller UUID", error
);
7881 /* XXX ENOENT here - no UUID - should perhaps become EPERM */
7884 if (!kauth_guid_equal(&vap
->va_uuuid
, &changer
) &&
7885 !kauth_guid_equal(&vap
->va_uuuid
, &kauth_null_guid
)) {
7886 KAUTH_DEBUG(" ERROR - cannot set supplied owner UUID - not us / null");
7896 * Group UUID being set or changed.
7898 if (VATTR_IS_ACTIVE(vap
, va_guuid
)) {
7899 /* if the group UUID is not actually changing ... */
7900 if (VATTR_IS_SUPPORTED(&ova
, va_guuid
)) {
7901 if (kauth_guid_equal(&vap
->va_guuid
, &ova
.va_guuid
))
7902 goto no_guuid_change
;
7905 * If the current group UUID is a null UUID, check
7906 * it against the UUID corresponding to the group GID.
7908 if (kauth_guid_equal(&ova
.va_guuid
, &kauth_null_guid
) &&
7909 VATTR_IS_SUPPORTED(&ova
, va_gid
)) {
7912 if (kauth_cred_gid2guid(ova
.va_gid
, &gid_guid
) == 0 &&
7913 kauth_guid_equal(&vap
->va_guuid
, &gid_guid
))
7914 goto no_guuid_change
;
7919 * The group UUID cannot be set by a non-superuser to anything other than
7920 * one of which they are a member or a null GUID (to "unset"
7922 * Note that file systems must be prepared to handle the
7923 * null UUID case in a manner appropriate for that file
7926 if (!has_priv_suser
) {
7927 if (kauth_guid_equal(&vap
->va_guuid
, &kauth_null_guid
))
7929 else if ((error
= kauth_cred_ismember_guid(cred
, &vap
->va_guuid
, &ismember
)) != 0) {
7930 KAUTH_DEBUG(" ERROR - got %d trying to check group membership", error
);
7934 KAUTH_DEBUG(" ERROR - cannot set supplied group UUID - not a member / null");
7944 * Compute authorisation for group/ownership changes.
7946 if (chowner
|| chgroup
|| clear_suid
|| clear_sgid
) {
7947 if (has_priv_suser
) {
7948 KAUTH_DEBUG("ATTR - superuser changing file owner/group, requiring immutability check");
7949 required_action
|= KAUTH_VNODE_CHECKIMMUTABLE
;
7952 KAUTH_DEBUG("ATTR - ownership change, requiring TAKE_OWNERSHIP");
7953 required_action
|= KAUTH_VNODE_TAKE_OWNERSHIP
;
7955 if (chgroup
&& !chowner
) {
7956 KAUTH_DEBUG("ATTR - group change, requiring WRITE_SECURITY");
7957 required_action
|= KAUTH_VNODE_WRITE_SECURITY
;
7960 /* clear set-uid and set-gid bits as required by Posix */
7961 if (VATTR_IS_ACTIVE(vap
, va_mode
)) {
7962 newmode
= vap
->va_mode
;
7963 } else if (VATTR_IS_SUPPORTED(&ova
, va_mode
)) {
7964 newmode
= ova
.va_mode
;
7966 KAUTH_DEBUG("CHOWN - trying to change owner but cannot get mode from filesystem to mask setugid bits");
7969 if (newmode
& (S_ISUID
| S_ISGID
)) {
7970 VATTR_SET(vap
, va_mode
, newmode
& ~(S_ISUID
| S_ISGID
));
7971 KAUTH_DEBUG("CHOWN - masking setugid bits from mode %o to %o", newmode
, vap
->va_mode
);
7977 * Authorise changes in the ACL.
7979 if (VATTR_IS_ACTIVE(vap
, va_acl
)) {
7981 /* no existing ACL */
7982 if (!VATTR_IS_ACTIVE(&ova
, va_acl
) || (ova
.va_acl
== NULL
)) {
7985 if (vap
->va_acl
!= NULL
) {
7986 required_action
|= KAUTH_VNODE_WRITE_SECURITY
;
7987 KAUTH_DEBUG("CHMOD - adding ACL");
7990 /* removing an existing ACL */
7991 } else if (vap
->va_acl
== NULL
) {
7992 required_action
|= KAUTH_VNODE_WRITE_SECURITY
;
7993 KAUTH_DEBUG("CHMOD - removing ACL");
7995 /* updating an existing ACL */
7997 if (vap
->va_acl
->acl_entrycount
!= ova
.va_acl
->acl_entrycount
) {
7998 /* entry count changed, must be different */
7999 required_action
|= KAUTH_VNODE_WRITE_SECURITY
;
8000 KAUTH_DEBUG("CHMOD - adding/removing ACL entries");
8001 } else if (vap
->va_acl
->acl_entrycount
> 0) {
8002 /* both ACLs have the same ACE count, said count is 1 or more, bitwise compare ACLs */
8003 if (memcmp(&vap
->va_acl
->acl_ace
[0], &ova
.va_acl
->acl_ace
[0],
8004 sizeof(struct kauth_ace
) * vap
->va_acl
->acl_entrycount
)) {
8005 required_action
|= KAUTH_VNODE_WRITE_SECURITY
;
8006 KAUTH_DEBUG("CHMOD - changing ACL entries");
8013 * Other attributes that require authorisation.
8015 if (VATTR_IS_ACTIVE(vap
, va_encoding
))
8016 required_action
|= KAUTH_VNODE_WRITE_ATTRIBUTES
;
8019 if (VATTR_IS_SUPPORTED(&ova
, va_acl
) && (ova
.va_acl
!= NULL
))
8020 kauth_acl_free(ova
.va_acl
);
8022 *actionp
= required_action
;
8027 setlocklocal_callback(struct vnode
*vp
, __unused
void *cargs
)
8029 vnode_lock_spin(vp
);
8030 vp
->v_flag
|= VLOCKLOCAL
;
8033 return (VNODE_RETURNED
);
8037 vfs_setlocklocal(mount_t mp
)
8039 mount_lock_spin(mp
);
8040 mp
->mnt_kern_flag
|= MNTK_LOCK_LOCAL
;
8044 * The number of active vnodes is expected to be
8045 * very small when vfs_setlocklocal is invoked.
8047 vnode_iterate(mp
, 0, setlocklocal_callback
, NULL
);
8051 vfs_setcompoundopen(mount_t mp
)
8053 mount_lock_spin(mp
);
8054 mp
->mnt_compound_ops
|= COMPOUND_VNOP_OPEN
;
8060 vnode_setswapmount(vnode_t vp
)
8062 mount_lock(vp
->v_mount
);
8063 vp
->v_mount
->mnt_kern_flag
|= MNTK_SWAP_MOUNT
;
8064 mount_unlock(vp
->v_mount
);
8069 vn_setunionwait(vnode_t vp
)
8071 vnode_lock_spin(vp
);
8072 vp
->v_flag
|= VISUNION
;
8078 vn_checkunionwait(vnode_t vp
)
8080 vnode_lock_spin(vp
);
8081 while ((vp
->v_flag
& VISUNION
) == VISUNION
)
8082 msleep((caddr_t
)&vp
->v_flag
, &vp
->v_lock
, 0, 0, 0);
8087 vn_clearunionwait(vnode_t vp
, int locked
)
8090 vnode_lock_spin(vp
);
8091 if((vp
->v_flag
& VISUNION
) == VISUNION
) {
8092 vp
->v_flag
&= ~VISUNION
;
8093 wakeup((caddr_t
)&vp
->v_flag
);
8100 * Removes orphaned apple double files during a rmdir
8102 * 1. vnode_suspend().
8103 * 2. Call VNOP_READDIR() till the end of directory is reached.
8104 * 3. Check if the directory entries returned are regular files with name starting with "._". If not, return ENOTEMPTY.
8105 * 4. Continue (2) and (3) till end of directory is reached.
8106 * 5. If all the entries in the directory were files with "._" name, delete all the files.
8108 * 7. If deletion of all files succeeded, call VNOP_RMDIR() again.
8111 errno_t
rmdir_remove_orphaned_appleDouble(vnode_t vp
, vfs_context_t ctx
, int * restart_flag
)
8114 #define UIO_BUFF_SIZE 2048
8116 int eofflag
, siz
= UIO_BUFF_SIZE
, nentries
= 0;
8117 int open_flag
= 0, full_erase_flag
= 0;
8118 char uio_buf
[ UIO_SIZEOF(1) ];
8125 error
= vnode_suspend(vp
);
8128 * restart_flag is set so that the calling rmdir sleeps and resets
8138 MALLOC(rbuf
, caddr_t
, siz
, M_TEMP
, M_WAITOK
);
8140 auio
= uio_createwithbuffer(1, 0, UIO_SYSSPACE
, UIO_READ
,
8141 &uio_buf
[0], sizeof(uio_buf
));
8142 if (!rbuf
|| !auio
) {
8147 uio_setoffset(auio
,0);
8151 if ((error
= VNOP_OPEN(vp
, FREAD
, ctx
)))
8157 * First pass checks if all files are appleDouble files.
8161 siz
= UIO_BUFF_SIZE
;
8162 uio_reset(auio
, uio_offset(auio
), UIO_SYSSPACE
, UIO_READ
);
8163 uio_addiov(auio
, CAST_USER_ADDR_T(rbuf
), UIO_BUFF_SIZE
);
8165 if((error
= VNOP_READDIR(vp
, auio
, 0, &eofflag
, &nentries
, ctx
)))
8168 if (uio_resid(auio
) != 0)
8169 siz
-= uio_resid(auio
);
8172 * Iterate through directory
8174 dir_pos
= (void*) rbuf
;
8175 dir_end
= (void*) (rbuf
+ siz
);
8176 dp
= (struct dirent
*) (dir_pos
);
8178 if (dir_pos
== dir_end
)
8181 while (dir_pos
< dir_end
) {
8183 * Check for . and .. as well as directories
8185 if (dp
->d_ino
!= 0 &&
8186 !((dp
->d_namlen
== 1 && dp
->d_name
[0] == '.') ||
8187 (dp
->d_namlen
== 2 && dp
->d_name
[0] == '.' && dp
->d_name
[1] == '.'))) {
8189 * Check for irregular files and ._ files
8190 * If there is a ._._ file abort the op
8192 if ( dp
->d_namlen
< 2 ||
8193 strncmp(dp
->d_name
,"._",2) ||
8194 (dp
->d_namlen
>= 4 && !strncmp(&(dp
->d_name
[2]), "._",2))) {
8199 dir_pos
= (void*) ((uint8_t*)dir_pos
+ dp
->d_reclen
);
8200 dp
= (struct dirent
*)dir_pos
;
8204 * workaround for HFS/NFS setting eofflag before end of file
8206 if (vp
->v_tag
== VT_HFS
&& nentries
> 2)
8209 if (vp
->v_tag
== VT_NFS
) {
8210 if (eofflag
&& !full_erase_flag
) {
8211 full_erase_flag
= 1;
8213 uio_reset(auio
, 0, UIO_SYSSPACE
, UIO_READ
);
8215 else if (!eofflag
&& full_erase_flag
)
8216 full_erase_flag
= 0;
8221 * If we've made it here all the files in the dir are ._ files.
8222 * We can delete the files even though the node is suspended
8223 * because we are the owner of the file.
8226 uio_reset(auio
, 0, UIO_SYSSPACE
, UIO_READ
);
8228 full_erase_flag
= 0;
8231 siz
= UIO_BUFF_SIZE
;
8232 uio_reset(auio
, uio_offset(auio
), UIO_SYSSPACE
, UIO_READ
);
8233 uio_addiov(auio
, CAST_USER_ADDR_T(rbuf
), UIO_BUFF_SIZE
);
8235 error
= VNOP_READDIR(vp
, auio
, 0, &eofflag
, &nentries
, ctx
);
8240 if (uio_resid(auio
) != 0)
8241 siz
-= uio_resid(auio
);
8244 * Iterate through directory
8246 dir_pos
= (void*) rbuf
;
8247 dir_end
= (void*) (rbuf
+ siz
);
8248 dp
= (struct dirent
*) dir_pos
;
8250 if (dir_pos
== dir_end
)
8253 while (dir_pos
< dir_end
) {
8255 * Check for . and .. as well as directories
8257 if (dp
->d_ino
!= 0 &&
8258 !((dp
->d_namlen
== 1 && dp
->d_name
[0] == '.') ||
8259 (dp
->d_namlen
== 2 && dp
->d_name
[0] == '.' && dp
->d_name
[1] == '.'))
8262 error
= unlink1(ctx
, vp
,
8263 CAST_USER_ADDR_T(dp
->d_name
), UIO_SYSSPACE
,
8264 VNODE_REMOVE_SKIP_NAMESPACE_EVENT
|
8265 VNODE_REMOVE_NO_AUDIT_PATH
);
8267 if (error
&& error
!= ENOENT
) {
8272 dir_pos
= (void*) ((uint8_t*)dir_pos
+ dp
->d_reclen
);
8273 dp
= (struct dirent
*)dir_pos
;
8277 * workaround for HFS/NFS setting eofflag before end of file
8279 if (vp
->v_tag
== VT_HFS
&& nentries
> 2)
8282 if (vp
->v_tag
== VT_NFS
) {
8283 if (eofflag
&& !full_erase_flag
) {
8284 full_erase_flag
= 1;
8286 uio_reset(auio
, 0, UIO_SYSSPACE
, UIO_READ
);
8288 else if (!eofflag
&& full_erase_flag
)
8289 full_erase_flag
= 0;
8299 VNOP_CLOSE(vp
, FREAD
, ctx
);
8313 lock_vnode_and_post(vnode_t vp
, int kevent_num
)
8315 /* Only take the lock if there's something there! */
8316 if (vp
->v_knotes
.slh_first
!= NULL
) {
8318 KNOTE(&vp
->v_knotes
, kevent_num
);
8324 #ifdef PANIC_PRINTS_VNODES
8326 void panic_print_vnodes(void);
8328 static const char *__vtype(uint16_t vtype
)
8357 * build a path from the bottom up
8358 * NOTE: called from the panic path - no alloc'ing of memory and no locks!
8360 static char *__vpath(vnode_t vp
, char *str
, int len
, int depth
)
8367 /* str + len is the start of the string we created */
8371 /* follow mount vnodes to get the full path */
8372 if ((vp
->v_flag
& VROOT
)) {
8373 if (vp
->v_mount
!= NULL
&& vp
->v_mount
->mnt_vnodecovered
) {
8376 return __vpath(vp
->v_mount
->mnt_vnodecovered
,
8382 src
= (char *)vp
->v_name
;
8383 vnm_len
= strlen(src
);
8384 if (vnm_len
> len
) {
8385 /* truncate the name to fit in the string */
8386 src
+= (vnm_len
- len
);
8390 /* start from the back and copy just characters (no NULLs) */
8392 /* this will chop off leaf path (file) names */
8394 dst
= str
+ len
- vnm_len
;
8395 memcpy(dst
, src
, vnm_len
);
8401 if (vp
->v_parent
&& len
> 1) {
8402 /* follow parents up the chain */
8405 return __vpath(vp
->v_parent
, str
, len
, depth
+ 1);
8411 extern int kdb_printf(const char *format
, ...) __printflike(1,2);
8413 #define SANE_VNODE_PRINT_LIMIT 5000
8414 void panic_print_vnodes(void)
8423 kdb_printf("\n***** VNODES *****\n"
8424 "TYPE UREF ICNT PATH\n");
8426 /* NULL-terminate the path name */
8427 vname
[sizeof(vname
)-1] = '\0';
8430 * iterate all vnodelist items in all mounts (mntlist) -> mnt_vnodelist
8432 TAILQ_FOREACH(mnt
, &mountlist
, mnt_list
) {
8433 TAILQ_FOREACH(vp
, &mnt
->mnt_vnodelist
, v_mntvnodes
) {
8434 if (++nvnodes
> SANE_VNODE_PRINT_LIMIT
)
8436 type
= __vtype(vp
->v_type
);
8437 nm
= __vpath(vp
, vname
, sizeof(vname
)-1, 0);
8438 kdb_printf("%s %0d %0d %s\n",
8439 type
, vp
->v_usecount
, vp
->v_iocount
, nm
);
8444 #else /* !PANIC_PRINTS_VNODES */
8445 void panic_print_vnodes(void)
8453 static void record_vp(vnode_t vp
, int count
) {
8460 if ((vp
->v_flag
& VSYSTEM
))
8463 ut
= get_bsdthread_info(current_thread());
8464 ut
->uu_iocount
+= count
;
8467 if (ut
->uu_vpindex
< 32) {
8468 OSBacktrace((void **)&ut
->uu_pcs
[ut
->uu_vpindex
][0], 10);
8470 ut
->uu_vps
[ut
->uu_vpindex
] = vp
;
8480 #define TRIG_DEBUG 0
8483 #define TRIG_LOG(...) do { printf("%s: ", __FUNCTION__); printf(__VA_ARGS__); } while (0)
8485 #define TRIG_LOG(...)
8489 * Resolver result functions
8493 vfs_resolver_result(uint32_t seq
, enum resolver_status stat
, int aux
)
8496 * |<--- 32 --->|<--- 28 --->|<- 4 ->|
8497 * sequence auxiliary status
8499 return (((uint64_t)seq
) << 32) |
8500 (((uint64_t)(aux
& 0x0fffffff)) << 4) |
8501 (uint64_t)(stat
& 0x0000000F);
8504 enum resolver_status
8505 vfs_resolver_status(resolver_result_t result
)
8507 /* lower 4 bits is status */
8508 return (result
& 0x0000000F);
8512 vfs_resolver_sequence(resolver_result_t result
)
8514 /* upper 32 bits is sequence */
8515 return (uint32_t)(result
>> 32);
8519 vfs_resolver_auxiliary(resolver_result_t result
)
8521 /* 28 bits of auxiliary */
8522 return (int)(((uint32_t)(result
& 0xFFFFFFF0)) >> 4);
8527 * Call in for resolvers to update vnode trigger state
8530 vnode_trigger_update(vnode_t vp
, resolver_result_t result
)
8534 enum resolver_status stat
;
8536 if (vp
->v_resolve
== NULL
) {
8540 stat
= vfs_resolver_status(result
);
8541 seq
= vfs_resolver_sequence(result
);
8543 if ((stat
!= RESOLVER_RESOLVED
) && (stat
!= RESOLVER_UNRESOLVED
)) {
8548 lck_mtx_lock(&rp
->vr_lock
);
8550 if (seq
> rp
->vr_lastseq
) {
8551 if (stat
== RESOLVER_RESOLVED
)
8552 rp
->vr_flags
|= VNT_RESOLVED
;
8554 rp
->vr_flags
&= ~VNT_RESOLVED
;
8556 rp
->vr_lastseq
= seq
;
8559 lck_mtx_unlock(&rp
->vr_lock
);
8565 vnode_resolver_attach(vnode_t vp
, vnode_resolve_t rp
, boolean_t ref
)
8569 vnode_lock_spin(vp
);
8570 if (vp
->v_resolve
!= NULL
) {
8579 error
= vnode_ref_ext(vp
, O_EVTONLY
, VNODE_REF_FORCE
);
8581 panic("VNODE_REF_FORCE didn't help...");
8589 * VFS internal interfaces for vnode triggers
8591 * vnode must already have an io count on entry
8592 * v_resolve is stable when io count is non-zero
8595 vnode_resolver_create(mount_t mp
, vnode_t vp
, struct vnode_trigger_param
*tinfo
, boolean_t external
)
8602 /* minimum pointer test (debugging) */
8603 if (tinfo
->vnt_data
)
8604 byte
= *((char *)tinfo
->vnt_data
);
8606 MALLOC(rp
, vnode_resolve_t
, sizeof(*rp
), M_TEMP
, M_WAITOK
);
8610 lck_mtx_init(&rp
->vr_lock
, trigger_vnode_lck_grp
, trigger_vnode_lck_attr
);
8612 rp
->vr_resolve_func
= tinfo
->vnt_resolve_func
;
8613 rp
->vr_unresolve_func
= tinfo
->vnt_unresolve_func
;
8614 rp
->vr_rearm_func
= tinfo
->vnt_rearm_func
;
8615 rp
->vr_reclaim_func
= tinfo
->vnt_reclaim_func
;
8616 rp
->vr_data
= tinfo
->vnt_data
;
8618 rp
->vr_flags
= tinfo
->vnt_flags
& VNT_VALID_MASK
;
8620 rp
->vr_flags
|= VNT_EXTERNAL
;
8623 result
= vnode_resolver_attach(vp
, rp
, external
);
8629 OSAddAtomic(1, &mp
->mnt_numtriggers
);
8640 vnode_resolver_release(vnode_resolve_t rp
)
8643 * Give them a chance to free any private data
8645 if (rp
->vr_data
&& rp
->vr_reclaim_func
) {
8646 rp
->vr_reclaim_func(NULLVP
, rp
->vr_data
);
8649 lck_mtx_destroy(&rp
->vr_lock
, trigger_vnode_lck_grp
);
8654 /* Called after the vnode has been drained */
8656 vnode_resolver_detach(vnode_t vp
)
8661 mp
= vnode_mount(vp
);
8665 vp
->v_resolve
= NULL
;
8668 if ((rp
->vr_flags
& VNT_EXTERNAL
) != 0) {
8669 vnode_rele_ext(vp
, O_EVTONLY
, 1);
8672 vnode_resolver_release(rp
);
8674 /* Keep count of active trigger vnodes per mount */
8675 OSAddAtomic(-1, &mp
->mnt_numtriggers
);
8680 vnode_trigger_rearm(vnode_t vp
, vfs_context_t ctx
)
8683 resolver_result_t result
;
8684 enum resolver_status status
;
8687 if ((vp
->v_resolve
== NULL
) ||
8688 (vp
->v_resolve
->vr_rearm_func
== NULL
) ||
8689 (vp
->v_resolve
->vr_flags
& VNT_AUTO_REARM
) == 0) {
8694 lck_mtx_lock(&rp
->vr_lock
);
8697 * Check if VFS initiated this unmount. If so, we'll catch it after the unresolve completes.
8699 if (rp
->vr_flags
& VNT_VFS_UNMOUNTED
) {
8700 lck_mtx_unlock(&rp
->vr_lock
);
8704 /* Check if this vnode is already armed */
8705 if ((rp
->vr_flags
& VNT_RESOLVED
) == 0) {
8706 lck_mtx_unlock(&rp
->vr_lock
);
8710 lck_mtx_unlock(&rp
->vr_lock
);
8712 result
= rp
->vr_rearm_func(vp
, 0, rp
->vr_data
, ctx
);
8713 status
= vfs_resolver_status(result
);
8714 seq
= vfs_resolver_sequence(result
);
8716 lck_mtx_lock(&rp
->vr_lock
);
8717 if (seq
> rp
->vr_lastseq
) {
8718 if (status
== RESOLVER_UNRESOLVED
)
8719 rp
->vr_flags
&= ~VNT_RESOLVED
;
8720 rp
->vr_lastseq
= seq
;
8722 lck_mtx_unlock(&rp
->vr_lock
);
8727 vnode_trigger_resolve(vnode_t vp
, struct nameidata
*ndp
, vfs_context_t ctx
)
8730 enum path_operation op
;
8731 resolver_result_t result
;
8732 enum resolver_status status
;
8735 /* Only trigger on topmost vnodes */
8736 if ((vp
->v_resolve
== NULL
) ||
8737 (vp
->v_resolve
->vr_resolve_func
== NULL
) ||
8738 (vp
->v_mountedhere
!= NULL
)) {
8743 lck_mtx_lock(&rp
->vr_lock
);
8745 /* Check if this vnode is already resolved */
8746 if (rp
->vr_flags
& VNT_RESOLVED
) {
8747 lck_mtx_unlock(&rp
->vr_lock
);
8751 lck_mtx_unlock(&rp
->vr_lock
);
8755 * assumes that resolver will not access this trigger vnode (otherwise the kernel will deadlock)
8756 * is there anyway to know this???
8757 * there can also be other legitimate lookups in parallel
8759 * XXX - should we call this on a separate thread with a timeout?
8761 * XXX - should we use ISLASTCN to pick the op value??? Perhaps only leafs should
8762 * get the richer set and non-leafs should get generic OP_LOOKUP? TBD
8764 op
= (ndp
->ni_op
< OP_MAXOP
) ? ndp
->ni_op
: OP_LOOKUP
;
8766 result
= rp
->vr_resolve_func(vp
, &ndp
->ni_cnd
, op
, 0, rp
->vr_data
, ctx
);
8767 status
= vfs_resolver_status(result
);
8768 seq
= vfs_resolver_sequence(result
);
8770 lck_mtx_lock(&rp
->vr_lock
);
8771 if (seq
> rp
->vr_lastseq
) {
8772 if (status
== RESOLVER_RESOLVED
)
8773 rp
->vr_flags
|= VNT_RESOLVED
;
8774 rp
->vr_lastseq
= seq
;
8776 lck_mtx_unlock(&rp
->vr_lock
);
8778 /* On resolver errors, propagate the error back up */
8779 return (status
== RESOLVER_ERROR
? vfs_resolver_auxiliary(result
) : 0);
8783 vnode_trigger_unresolve(vnode_t vp
, int flags
, vfs_context_t ctx
)
8786 resolver_result_t result
;
8787 enum resolver_status status
;
8790 if ((vp
->v_resolve
== NULL
) || (vp
->v_resolve
->vr_unresolve_func
== NULL
)) {
8795 lck_mtx_lock(&rp
->vr_lock
);
8797 /* Check if this vnode is already resolved */
8798 if ((rp
->vr_flags
& VNT_RESOLVED
) == 0) {
8799 printf("vnode_trigger_unresolve: not currently resolved\n");
8800 lck_mtx_unlock(&rp
->vr_lock
);
8804 rp
->vr_flags
|= VNT_VFS_UNMOUNTED
;
8806 lck_mtx_unlock(&rp
->vr_lock
);
8810 * assumes that resolver will not access this trigger vnode (otherwise the kernel will deadlock)
8811 * there can also be other legitimate lookups in parallel
8813 * XXX - should we call this on a separate thread with a timeout?
8816 result
= rp
->vr_unresolve_func(vp
, flags
, rp
->vr_data
, ctx
);
8817 status
= vfs_resolver_status(result
);
8818 seq
= vfs_resolver_sequence(result
);
8820 lck_mtx_lock(&rp
->vr_lock
);
8821 if (seq
> rp
->vr_lastseq
) {
8822 if (status
== RESOLVER_UNRESOLVED
)
8823 rp
->vr_flags
&= ~VNT_RESOLVED
;
8824 rp
->vr_lastseq
= seq
;
8826 rp
->vr_flags
&= ~VNT_VFS_UNMOUNTED
;
8827 lck_mtx_unlock(&rp
->vr_lock
);
8829 /* On resolver errors, propagate the error back up */
8830 return (status
== RESOLVER_ERROR
? vfs_resolver_auxiliary(result
) : 0);
8834 triggerisdescendant(mount_t mp
, mount_t rmp
)
8839 * walk up vnode covered chain looking for a match
8841 name_cache_lock_shared();
8846 /* did we encounter "/" ? */
8847 if (mp
->mnt_flag
& MNT_ROOTFS
)
8850 vp
= mp
->mnt_vnodecovered
;
8861 name_cache_unlock();
8866 struct trigger_unmount_info
{
8871 uint32_t trigger_vid
;
8876 trigger_unmount_callback(mount_t mp
, void * arg
)
8878 struct trigger_unmount_info
* infop
= (struct trigger_unmount_info
*)arg
;
8879 boolean_t mountedtrigger
= FALSE
;
8882 * When we encounter the top level mount we're done
8884 if (mp
== infop
->top_mp
)
8885 return (VFS_RETURNED_DONE
);
8887 if ((mp
->mnt_vnodecovered
== NULL
) ||
8888 (vnode_getwithref(mp
->mnt_vnodecovered
) != 0)) {
8889 return (VFS_RETURNED
);
8892 if ((mp
->mnt_vnodecovered
->v_mountedhere
== mp
) &&
8893 (mp
->mnt_vnodecovered
->v_resolve
!= NULL
) &&
8894 (mp
->mnt_vnodecovered
->v_resolve
->vr_flags
& VNT_RESOLVED
)) {
8895 mountedtrigger
= TRUE
;
8897 vnode_put(mp
->mnt_vnodecovered
);
8900 * When we encounter a mounted trigger, check if its under the top level mount
8902 if ( !mountedtrigger
|| !triggerisdescendant(mp
, infop
->top_mp
) )
8903 return (VFS_RETURNED
);
8906 * Process any pending nested mount (now that its not referenced)
8908 if ((infop
->trigger_vp
!= NULLVP
) &&
8909 (vnode_getwithvid(infop
->trigger_vp
, infop
->trigger_vid
) == 0)) {
8910 vnode_t vp
= infop
->trigger_vp
;
8913 infop
->trigger_vp
= NULLVP
;
8915 if (mp
== vp
->v_mountedhere
) {
8917 printf("trigger_unmount_callback: unexpected match '%s'\n",
8918 mp
->mnt_vfsstat
.f_mntonname
);
8919 return (VFS_RETURNED
);
8921 if (infop
->trigger_mp
!= vp
->v_mountedhere
) {
8923 printf("trigger_unmount_callback: trigger mnt changed! (%p != %p)\n",
8924 infop
->trigger_mp
, vp
->v_mountedhere
);
8928 error
= vnode_trigger_unresolve(vp
, infop
->flags
, infop
->ctx
);
8931 printf("unresolving: '%s', err %d\n",
8932 vp
->v_mountedhere
? vp
->v_mountedhere
->mnt_vfsstat
.f_mntonname
:
8934 return (VFS_RETURNED_DONE
); /* stop iteration on errors */
8939 * We can't call resolver here since we hold a mount iter
8940 * ref on mp so save its covered vp for later processing
8942 infop
->trigger_vp
= mp
->mnt_vnodecovered
;
8943 if ((infop
->trigger_vp
!= NULLVP
) &&
8944 (vnode_getwithref(infop
->trigger_vp
) == 0)) {
8945 if (infop
->trigger_vp
->v_mountedhere
== mp
) {
8946 infop
->trigger_vid
= infop
->trigger_vp
->v_id
;
8947 infop
->trigger_mp
= mp
;
8949 vnode_put(infop
->trigger_vp
);
8952 return (VFS_RETURNED
);
8956 * Attempt to unmount any trigger mounts nested underneath a mount.
8957 * This is a best effort attempt and no retries are performed here.
8959 * Note: mp->mnt_rwlock is held exclusively on entry (so be carefull)
8963 vfs_nested_trigger_unmounts(mount_t mp
, int flags
, vfs_context_t ctx
)
8965 struct trigger_unmount_info info
;
8967 /* Must have trigger vnodes */
8968 if (mp
->mnt_numtriggers
== 0) {
8971 /* Avoid recursive requests (by checking covered vnode) */
8972 if ((mp
->mnt_vnodecovered
!= NULL
) &&
8973 (vnode_getwithref(mp
->mnt_vnodecovered
) == 0)) {
8974 boolean_t recursive
= FALSE
;
8976 if ((mp
->mnt_vnodecovered
->v_mountedhere
== mp
) &&
8977 (mp
->mnt_vnodecovered
->v_resolve
!= NULL
) &&
8978 (mp
->mnt_vnodecovered
->v_resolve
->vr_flags
& VNT_VFS_UNMOUNTED
)) {
8981 vnode_put(mp
->mnt_vnodecovered
);
8987 * Attempt to unmount any nested trigger mounts (best effort)
8991 info
.trigger_vp
= NULLVP
;
8992 info
.trigger_vid
= 0;
8993 info
.trigger_mp
= NULL
;
8996 (void) vfs_iterate(VFS_ITERATE_TAIL_FIRST
, trigger_unmount_callback
, &info
);
8999 * Process remaining nested mount (now that its not referenced)
9001 if ((info
.trigger_vp
!= NULLVP
) &&
9002 (vnode_getwithvid(info
.trigger_vp
, info
.trigger_vid
) == 0)) {
9003 vnode_t vp
= info
.trigger_vp
;
9005 if (info
.trigger_mp
== vp
->v_mountedhere
) {
9006 (void) vnode_trigger_unresolve(vp
, flags
, ctx
);
9013 vfs_addtrigger(mount_t mp
, const char *relpath
, struct vnode_trigger_info
*vtip
, vfs_context_t ctx
)
9015 struct nameidata nd
;
9018 struct vnode_trigger_param vtp
;
9021 * Must be called for trigger callback, wherein rwlock is held
9023 lck_rw_assert(&mp
->mnt_rwlock
, LCK_RW_ASSERT_HELD
);
9025 TRIG_LOG("Adding trigger at %s\n", relpath
);
9026 TRIG_LOG("Trying VFS_ROOT\n");
9029 * We do a lookup starting at the root of the mountpoint, unwilling
9030 * to cross into other mountpoints.
9032 res
= VFS_ROOT(mp
, &rvp
, ctx
);
9037 TRIG_LOG("Trying namei\n");
9039 NDINIT(&nd
, LOOKUP
, OP_LOOKUP
, USEDVP
| NOCROSSMOUNT
| FOLLOW
, UIO_SYSSPACE
,
9040 CAST_USER_ADDR_T(relpath
), ctx
);
9052 TRIG_LOG("Trying vnode_resolver_create()\n");
9055 * Set up blob. vnode_create() takes a larger structure
9056 * with creation info, and we needed something different
9057 * for this case. One needs to win, or we need to munge both;
9058 * vnode_create() wins.
9060 bzero(&vtp
, sizeof(vtp
));
9061 vtp
.vnt_resolve_func
= vtip
->vti_resolve_func
;
9062 vtp
.vnt_unresolve_func
= vtip
->vti_unresolve_func
;
9063 vtp
.vnt_rearm_func
= vtip
->vti_rearm_func
;
9064 vtp
.vnt_reclaim_func
= vtip
->vti_reclaim_func
;
9065 vtp
.vnt_reclaim_func
= vtip
->vti_reclaim_func
;
9066 vtp
.vnt_data
= vtip
->vti_data
;
9067 vtp
.vnt_flags
= vtip
->vti_flags
;
9069 res
= vnode_resolver_create(mp
, vp
, &vtp
, TRUE
);
9072 TRIG_LOG("Returning %d\n", res
);
9076 #endif /* CONFIG_TRIGGERS */