2 * Copyright (c) 2000-2007 Apple Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
28 /* Copyright (c) 1995 NeXT Computer, Inc. All Rights Reserved */
30 * Copyright (c) 1989, 1993
31 * The Regents of the University of California. All rights reserved.
32 * (c) UNIX System Laboratories, Inc.
33 * All or some portions of this file are derived from material licensed
34 * to the University of California by American Telephone and Telegraph
35 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
36 * the permission of UNIX System Laboratories, Inc.
38 * Redistribution and use in source and binary forms, with or without
39 * modification, are permitted provided that the following conditions
41 * 1. Redistributions of source code must retain the above copyright
42 * notice, this list of conditions and the following disclaimer.
43 * 2. Redistributions in binary form must reproduce the above copyright
44 * notice, this list of conditions and the following disclaimer in the
45 * documentation and/or other materials provided with the distribution.
46 * 3. All advertising materials mentioning features or use of this software
47 * must display the following acknowledgement:
48 * This product includes software developed by the University of
49 * California, Berkeley and its contributors.
50 * 4. Neither the name of the University nor the names of its contributors
51 * may be used to endorse or promote products derived from this software
52 * without specific prior written permission.
54 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
55 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
56 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
57 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
58 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
59 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
60 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
61 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
62 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
63 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
66 * @(#)vfs_subr.c 8.31 (Berkeley) 5/26/95
69 * NOTICE: This file was modified by SPARTA, Inc. in 2005 to introduce
70 * support for mandatory and extensible security protections. This notice
71 * is included in support of clause 2.2 (b) of the Apple Public License,
76 * External virtual filesystem routines
80 #include <sys/param.h>
81 #include <sys/systm.h>
82 #include <sys/proc_internal.h>
83 #include <sys/kauth.h>
84 #include <sys/mount_internal.h>
87 #include <sys/vnode.h>
88 #include <sys/vnode_internal.h>
90 #include <sys/namei.h>
91 #include <sys/ucred.h>
92 #include <sys/buf_internal.h>
93 #include <sys/errno.h>
94 #include <sys/malloc.h>
95 #include <sys/uio_internal.h>
97 #include <sys/domain.h>
99 #include <sys/syslog.h>
100 #include <sys/ubc_internal.h>
102 #include <sys/sysctl.h>
103 #include <sys/filedesc.h>
104 #include <sys/event.h>
105 #include <sys/kdebug.h>
106 #include <sys/kauth.h>
107 #include <sys/user.h>
108 #include <miscfs/fifofs/fifo.h>
111 #include <machine/spl.h>
114 #include <kern/assert.h>
116 #include <miscfs/specfs/specdev.h>
118 #include <mach/mach_types.h>
119 #include <mach/memory_object_types.h>
121 #include <kern/kalloc.h> /* kalloc()/kfree() */
122 #include <kern/clock.h> /* delay_for_interval() */
123 #include <libkern/OSAtomic.h> /* OSAddAtomic() */
126 #include <vm/vm_protos.h> /* vnode_pager_vrele() */
129 #include <security/mac_framework.h>
132 extern lck_grp_t
*vnode_lck_grp
;
133 extern lck_attr_t
*vnode_lck_attr
;
136 extern lck_mtx_t
* mnt_list_mtx_lock
;
138 enum vtype iftovt_tab
[16] = {
139 VNON
, VFIFO
, VCHR
, VNON
, VDIR
, VNON
, VBLK
, VNON
,
140 VREG
, VNON
, VLNK
, VNON
, VSOCK
, VNON
, VNON
, VBAD
,
142 int vttoif_tab
[9] = {
143 0, S_IFREG
, S_IFDIR
, S_IFBLK
, S_IFCHR
, S_IFLNK
,
144 S_IFSOCK
, S_IFIFO
, S_IFMT
,
147 /* XXX next protptype should be from <nfs/nfs.h> */
148 extern int nfs_vinvalbuf(vnode_t
, int, vfs_context_t
, int);
150 /* XXX next prototytype should be from libsa/stdlib.h> but conflicts libkern */
151 __private_extern__
void qsort(
155 int (*)(const void *, const void *));
157 extern kern_return_t
adjust_vm_object_cache(vm_size_t oval
, vm_size_t nval
);
158 __private_extern__
void vntblinit(void);
159 __private_extern__ kern_return_t
reset_vmobjectcache(unsigned int val1
,
161 __private_extern__
int unlink1(vfs_context_t
, struct nameidata
*, int);
163 static void vnode_list_add(vnode_t
);
164 static void vnode_list_remove(vnode_t
);
166 static errno_t
vnode_drain(vnode_t
);
167 static void vgone(vnode_t
, int flags
);
168 static void vclean(vnode_t vp
, int flag
);
169 static void vnode_reclaim_internal(vnode_t
, int, int, int);
171 static void vnode_dropiocount (vnode_t
);
172 static errno_t
vnode_getiocount(vnode_t vp
, int vid
, int vflags
);
173 static int vget_internal(vnode_t
, int, int);
175 static vnode_t
checkalias(vnode_t vp
, dev_t nvp_rdev
);
176 static int vnode_reload(vnode_t
);
177 static int vnode_isinuse_locked(vnode_t
, int, int);
179 static void insmntque(vnode_t vp
, mount_t mp
);
180 static int mount_getvfscnt(void);
181 static int mount_fillfsids(fsid_t
*, int );
182 static void vnode_iterate_setup(mount_t
);
183 static int vnode_umount_preflight(mount_t
, vnode_t
, int);
184 static int vnode_iterate_prepare(mount_t
);
185 static int vnode_iterate_reloadq(mount_t
);
186 static void vnode_iterate_clear(mount_t
);
188 errno_t
rmdir_remove_orphaned_appleDouble(vnode_t
, vfs_context_t
, int *);
190 TAILQ_HEAD(freelst
, vnode
) vnode_free_list
; /* vnode free list */
191 TAILQ_HEAD(deadlst
, vnode
) vnode_dead_list
; /* vnode dead list */
193 TAILQ_HEAD(ragelst
, vnode
) vnode_rage_list
; /* vnode rapid age list */
194 struct timeval rage_tv
;
198 #define RAGE_LIMIT_MIN 100
199 #define RAGE_TIME_LIMIT 5
201 struct mntlist mountlist
; /* mounted filesystem list */
202 static int nummounts
= 0;
205 #define VLISTCHECK(fun, vp, list) \
206 if ((vp)->v_freelist.tqe_prev == (struct vnode **)0xdeadb) \
207 panic("%s: %s vnode not on %slist", (fun), (list), (list));
209 #define VLISTCHECK(fun, vp, list)
210 #endif /* DIAGNOSTIC */
212 #define VLISTNONE(vp) \
214 (vp)->v_freelist.tqe_next = (struct vnode *)0; \
215 (vp)->v_freelist.tqe_prev = (struct vnode **)0xdeadb; \
218 #define VONLIST(vp) \
219 ((vp)->v_freelist.tqe_prev != (struct vnode **)0xdeadb)
221 /* remove a vnode from free vnode list */
222 #define VREMFREE(fun, vp) \
224 VLISTCHECK((fun), (vp), "free"); \
225 TAILQ_REMOVE(&vnode_free_list, (vp), v_freelist); \
232 /* remove a vnode from dead vnode list */
233 #define VREMDEAD(fun, vp) \
235 VLISTCHECK((fun), (vp), "dead"); \
236 TAILQ_REMOVE(&vnode_dead_list, (vp), v_freelist); \
238 vp->v_listflag &= ~VLIST_DEAD; \
243 /* remove a vnode from rage vnode list */
244 #define VREMRAGE(fun, vp) \
246 if ( !(vp->v_listflag & VLIST_RAGE)) \
247 panic("VREMRAGE: vp not on rage list"); \
248 VLISTCHECK((fun), (vp), "rage"); \
249 TAILQ_REMOVE(&vnode_rage_list, (vp), v_freelist); \
251 vp->v_listflag &= ~VLIST_RAGE; \
257 * vnodetarget hasn't been used in a long time, but
258 * it was exported for some reason... I'm leaving in
259 * place for now... it should be deprecated out of the
260 * exports and removed eventually.
262 unsigned long vnodetarget
; /* target for vnreclaim() */
263 #define VNODE_FREE_TARGET 20 /* Default value for vnodetarget */
266 * We need quite a few vnodes on the free list to sustain the
267 * rapid stat() the compilation process does, and still benefit from the name
268 * cache. Having too few vnodes on the free list causes serious disk
269 * thrashing as we cycle through them.
271 #define VNODE_FREE_MIN CONFIG_VNODE_FREE_MIN /* freelist should have at least this many */
274 * Initialize the vnode management data structures.
276 __private_extern__
void
279 TAILQ_INIT(&vnode_free_list
);
280 TAILQ_INIT(&vnode_rage_list
);
281 TAILQ_INIT(&vnode_dead_list
);
282 TAILQ_INIT(&mountlist
);
285 vnodetarget
= VNODE_FREE_TARGET
;
287 microuptime(&rage_tv
);
288 rage_limit
= desiredvnodes
/ 100;
290 if (rage_limit
< RAGE_LIMIT_MIN
)
291 rage_limit
= RAGE_LIMIT_MIN
;
294 * Scale the vm_object_cache to accomodate the vnodes
297 (void) adjust_vm_object_cache(0, desiredvnodes
- VNODE_FREE_MIN
);
300 /* Reset the VM Object Cache with the values passed in */
301 __private_extern__ kern_return_t
302 reset_vmobjectcache(unsigned int val1
, unsigned int val2
)
304 vm_size_t oval
= val1
- VNODE_FREE_MIN
;
307 if(val2
< VNODE_FREE_MIN
)
310 nval
= val2
- VNODE_FREE_MIN
;
312 return(adjust_vm_object_cache(oval
, nval
));
316 /* the timeout is in 10 msecs */
318 vnode_waitforwrites(vnode_t vp
, int output_target
, int slpflag
, int slptimeout
, const char *msg
) {
322 KERNEL_DEBUG(0x3010280 | DBG_FUNC_START
, (int)vp
, output_target
, vp
->v_numoutput
, 0, 0);
324 if (vp
->v_numoutput
> output_target
) {
330 while ((vp
->v_numoutput
> output_target
) && error
== 0) {
332 vp
->v_flag
|= VTHROTTLED
;
334 vp
->v_flag
|= VBWAIT
;
336 ts
.tv_sec
= (slptimeout
/100);
337 ts
.tv_nsec
= (slptimeout
% 1000) * 10 * NSEC_PER_USEC
* 1000 ;
338 error
= msleep((caddr_t
)&vp
->v_numoutput
, &vp
->v_lock
, (slpflag
| (PRIBIO
+ 1)), msg
, &ts
);
342 KERNEL_DEBUG(0x3010280 | DBG_FUNC_END
, (int)vp
, output_target
, vp
->v_numoutput
, error
, 0);
349 vnode_startwrite(vnode_t vp
) {
351 OSAddAtomic(1, &vp
->v_numoutput
);
356 vnode_writedone(vnode_t vp
)
359 OSAddAtomic(-1, &vp
->v_numoutput
);
361 if (vp
->v_numoutput
<= 1) {
366 if (vp
->v_numoutput
< 0)
367 panic("vnode_writedone: numoutput < 0");
369 if ((vp
->v_flag
& VTHROTTLED
) && (vp
->v_numoutput
<= 1)) {
370 vp
->v_flag
&= ~VTHROTTLED
;
373 if ((vp
->v_flag
& VBWAIT
) && (vp
->v_numoutput
== 0)) {
374 vp
->v_flag
&= ~VBWAIT
;
380 wakeup((caddr_t
)&vp
->v_numoutput
);
388 vnode_hasdirtyblks(vnode_t vp
)
390 struct cl_writebehind
*wbp
;
393 * Not taking the buf_mtxp as there is little
394 * point doing it. Even if the lock is taken the
395 * state can change right after that. If their
396 * needs to be a synchronization, it must be driven
399 if (vp
->v_dirtyblkhd
.lh_first
)
402 if (!UBCINFOEXISTS(vp
))
405 wbp
= vp
->v_ubcinfo
->cl_wbehind
;
407 if (wbp
&& (wbp
->cl_number
|| wbp
->cl_scmap
))
414 vnode_hascleanblks(vnode_t vp
)
417 * Not taking the buf_mtxp as there is little
418 * point doing it. Even if the lock is taken the
419 * state can change right after that. If their
420 * needs to be a synchronization, it must be driven
423 if (vp
->v_cleanblkhd
.lh_first
)
429 vnode_iterate_setup(mount_t mp
)
431 while (mp
->mnt_lflag
& MNT_LITER
) {
432 mp
->mnt_lflag
|= MNT_LITERWAIT
;
433 msleep((caddr_t
)mp
, &mp
->mnt_mlock
, PVFS
, "vnode_iterate_setup", NULL
);
436 mp
->mnt_lflag
|= MNT_LITER
;
441 vnode_umount_preflight(mount_t mp
, vnode_t skipvp
, int flags
)
445 TAILQ_FOREACH(vp
, &mp
->mnt_vnodelist
, v_mntvnodes
) {
446 /* disable preflight only for udf, a hack to be removed after 4073176 is fixed */
447 if (vp
->v_tag
== VT_UDF
)
449 if (vp
->v_type
== VDIR
)
453 if ((flags
& SKIPSYSTEM
) && ((vp
->v_flag
& VSYSTEM
) ||
454 (vp
->v_flag
& VNOFLUSH
)))
456 if ((flags
& SKIPSWAP
) && (vp
->v_flag
& VSWAP
))
458 if ((flags
& WRITECLOSE
) &&
459 (vp
->v_writecount
== 0 || vp
->v_type
!= VREG
))
461 /* Look for busy vnode */
462 if (((vp
->v_usecount
!= 0) &&
463 ((vp
->v_usecount
- vp
->v_kusecount
) != 0)))
471 * This routine prepares iteration by moving all the vnodes to worker queue
472 * called with mount lock held
475 vnode_iterate_prepare(mount_t mp
)
479 if (TAILQ_EMPTY(&mp
->mnt_vnodelist
)) {
484 vp
= TAILQ_FIRST(&mp
->mnt_vnodelist
);
485 vp
->v_mntvnodes
.tqe_prev
= &(mp
->mnt_workerqueue
.tqh_first
);
486 mp
->mnt_workerqueue
.tqh_first
= mp
->mnt_vnodelist
.tqh_first
;
487 mp
->mnt_workerqueue
.tqh_last
= mp
->mnt_vnodelist
.tqh_last
;
489 TAILQ_INIT(&mp
->mnt_vnodelist
);
490 if (mp
->mnt_newvnodes
.tqh_first
!= NULL
)
491 panic("vnode_iterate_prepare: newvnode when entering vnode");
492 TAILQ_INIT(&mp
->mnt_newvnodes
);
498 /* called with mount lock held */
500 vnode_iterate_reloadq(mount_t mp
)
504 /* add the remaining entries in workerq to the end of mount vnode list */
505 if (!TAILQ_EMPTY(&mp
->mnt_workerqueue
)) {
507 mvp
= TAILQ_LAST(&mp
->mnt_vnodelist
, vnodelst
);
509 /* Joining the workerque entities to mount vnode list */
511 mvp
->v_mntvnodes
.tqe_next
= mp
->mnt_workerqueue
.tqh_first
;
513 mp
->mnt_vnodelist
.tqh_first
= mp
->mnt_workerqueue
.tqh_first
;
514 mp
->mnt_workerqueue
.tqh_first
->v_mntvnodes
.tqe_prev
= mp
->mnt_vnodelist
.tqh_last
;
515 mp
->mnt_vnodelist
.tqh_last
= mp
->mnt_workerqueue
.tqh_last
;
516 TAILQ_INIT(&mp
->mnt_workerqueue
);
519 /* add the newvnodes to the head of mount vnode list */
520 if (!TAILQ_EMPTY(&mp
->mnt_newvnodes
)) {
522 nlvp
= TAILQ_LAST(&mp
->mnt_newvnodes
, vnodelst
);
524 mp
->mnt_newvnodes
.tqh_first
->v_mntvnodes
.tqe_prev
= &mp
->mnt_vnodelist
.tqh_first
;
525 nlvp
->v_mntvnodes
.tqe_next
= mp
->mnt_vnodelist
.tqh_first
;
526 if(mp
->mnt_vnodelist
.tqh_first
)
527 mp
->mnt_vnodelist
.tqh_first
->v_mntvnodes
.tqe_prev
= &nlvp
->v_mntvnodes
.tqe_next
;
529 mp
->mnt_vnodelist
.tqh_last
= mp
->mnt_newvnodes
.tqh_last
;
530 mp
->mnt_vnodelist
.tqh_first
= mp
->mnt_newvnodes
.tqh_first
;
531 TAILQ_INIT(&mp
->mnt_newvnodes
);
540 vnode_iterate_clear(mount_t mp
)
542 mp
->mnt_lflag
&= ~MNT_LITER
;
543 if (mp
->mnt_lflag
& MNT_LITERWAIT
) {
544 mp
->mnt_lflag
&= ~MNT_LITERWAIT
;
551 vnode_iterate(mount_t mp
, int flags
, int (*callout
)(struct vnode
*, void *),
560 vnode_iterate_setup(mp
);
562 /* it is returns 0 then there is nothing to do */
563 retval
= vnode_iterate_prepare(mp
);
566 vnode_iterate_clear(mp
);
571 /* iterate over all the vnodes */
572 while (!TAILQ_EMPTY(&mp
->mnt_workerqueue
)) {
573 vp
= TAILQ_FIRST(&mp
->mnt_workerqueue
);
574 TAILQ_REMOVE(&mp
->mnt_workerqueue
, vp
, v_mntvnodes
);
575 TAILQ_INSERT_TAIL(&mp
->mnt_vnodelist
, vp
, v_mntvnodes
);
577 if ((vp
->v_data
== NULL
) || (vp
->v_type
== VNON
) || (vp
->v_mount
!= mp
)) {
582 if ( vget_internal(vp
, vid
, (flags
| VNODE_NODEAD
| VNODE_WITHID
| VNODE_NOSUSPEND
))) {
586 if (flags
& VNODE_RELOAD
) {
588 * we're reloading the filesystem
589 * cast out any inactive vnodes...
591 if (vnode_reload(vp
)) {
592 /* vnode will be recycled on the refcount drop */
599 retval
= callout(vp
, arg
);
603 case VNODE_RETURNED_DONE
:
605 if (retval
== VNODE_RETURNED_DONE
) {
612 case VNODE_CLAIMED_DONE
:
624 (void)vnode_iterate_reloadq(mp
);
625 vnode_iterate_clear(mp
);
631 mount_lock_renames(mount_t mp
)
633 lck_mtx_lock(&mp
->mnt_renamelock
);
637 mount_unlock_renames(mount_t mp
)
639 lck_mtx_unlock(&mp
->mnt_renamelock
);
643 mount_lock(mount_t mp
)
645 lck_mtx_lock(&mp
->mnt_mlock
);
649 mount_unlock(mount_t mp
)
651 lck_mtx_unlock(&mp
->mnt_mlock
);
656 mount_ref(mount_t mp
, int locked
)
669 mount_drop(mount_t mp
, int locked
)
676 if (mp
->mnt_count
== 0 && (mp
->mnt_lflag
& MNT_LDRAIN
))
677 wakeup(&mp
->mnt_lflag
);
685 mount_iterref(mount_t mp
, int locked
)
691 if (mp
->mnt_iterref
< 0) {
702 mount_isdrained(mount_t mp
, int locked
)
708 if (mp
->mnt_iterref
< 0)
718 mount_iterdrop(mount_t mp
)
722 wakeup(&mp
->mnt_iterref
);
727 mount_iterdrain(mount_t mp
)
730 while (mp
->mnt_iterref
)
731 msleep((caddr_t
)&mp
->mnt_iterref
, mnt_list_mtx_lock
, PVFS
, "mount_iterdrain", NULL
);
732 /* mount iterations drained */
733 mp
->mnt_iterref
= -1;
737 mount_iterreset(mount_t mp
)
740 if (mp
->mnt_iterref
== -1)
745 /* always called with mount lock held */
747 mount_refdrain(mount_t mp
)
749 if (mp
->mnt_lflag
& MNT_LDRAIN
)
750 panic("already in drain");
751 mp
->mnt_lflag
|= MNT_LDRAIN
;
753 while (mp
->mnt_count
)
754 msleep((caddr_t
)&mp
->mnt_lflag
, &mp
->mnt_mlock
, PVFS
, "mount_drain", NULL
);
756 if (mp
->mnt_vnodelist
.tqh_first
!= NULL
)
757 panic("mount_refdrain: dangling vnode");
759 mp
->mnt_lflag
&= ~MNT_LDRAIN
;
766 * Mark a mount point as busy. Used to synchronize access and to delay
770 vfs_busy(mount_t mp
, int flags
)
774 if (mp
->mnt_lflag
& MNT_LDEAD
)
777 if (mp
->mnt_lflag
& MNT_LUNMOUNT
) {
778 if (flags
& LK_NOWAIT
)
783 if (mp
->mnt_lflag
& MNT_LDEAD
) {
787 if (mp
->mnt_lflag
& MNT_LUNMOUNT
) {
788 mp
->mnt_lflag
|= MNT_LWAIT
;
790 * Since all busy locks are shared except the exclusive
791 * lock granted when unmounting, the only place that a
792 * wakeup needs to be done is at the release of the
793 * exclusive lock at the end of dounmount.
795 msleep((caddr_t
)mp
, &mp
->mnt_mlock
, (PVFS
| PDROP
), "vfsbusy", NULL
);
801 lck_rw_lock_shared(&mp
->mnt_rwlock
);
804 * until we are granted the rwlock, it's possible for the mount point to
805 * change state, so reevaluate before granting the vfs_busy
807 if (mp
->mnt_lflag
& (MNT_LDEAD
| MNT_LUNMOUNT
)) {
808 lck_rw_done(&mp
->mnt_rwlock
);
815 * Free a busy filesystem.
819 vfs_unbusy(mount_t mp
)
821 lck_rw_done(&mp
->mnt_rwlock
);
827 vfs_rootmountfailed(mount_t mp
) {
830 mp
->mnt_vtable
->vfc_refcount
--;
835 mount_lock_destroy(mp
);
838 mac_mount_label_destroy(mp
);
841 FREE_ZONE(mp
, sizeof(struct mount
), M_MOUNT
);
845 * Lookup a filesystem type, and if found allocate and initialize
846 * a mount structure for it.
848 * Devname is usually updated by mount(8) after booting.
851 vfs_rootmountalloc_internal(struct vfstable
*vfsp
, const char *devname
)
855 mp
= _MALLOC_ZONE((u_long
)sizeof(struct mount
), M_MOUNT
, M_WAITOK
);
856 bzero((char *)mp
, (u_long
)sizeof(struct mount
));
858 /* Initialize the default IO constraints */
859 mp
->mnt_maxreadcnt
= mp
->mnt_maxwritecnt
= MAXPHYS
;
860 mp
->mnt_segreadcnt
= mp
->mnt_segwritecnt
= 32;
861 mp
->mnt_maxsegreadsize
= mp
->mnt_maxreadcnt
;
862 mp
->mnt_maxsegwritesize
= mp
->mnt_maxwritecnt
;
863 mp
->mnt_devblocksize
= DEV_BSIZE
;
864 mp
->mnt_alignmentmask
= PAGE_MASK
;
866 mp
->mnt_realrootvp
= NULLVP
;
867 mp
->mnt_authcache_ttl
= CACHED_LOOKUP_RIGHT_TTL
;
870 (void)vfs_busy(mp
, LK_NOWAIT
);
872 TAILQ_INIT(&mp
->mnt_vnodelist
);
873 TAILQ_INIT(&mp
->mnt_workerqueue
);
874 TAILQ_INIT(&mp
->mnt_newvnodes
);
876 mp
->mnt_vtable
= vfsp
;
877 mp
->mnt_op
= vfsp
->vfc_vfsops
;
878 mp
->mnt_flag
= MNT_RDONLY
| MNT_ROOTFS
;
879 mp
->mnt_vnodecovered
= NULLVP
;
880 //mp->mnt_stat.f_type = vfsp->vfc_typenum;
881 mp
->mnt_flag
|= vfsp
->vfc_flags
& MNT_VISFLAGMASK
;
884 vfsp
->vfc_refcount
++;
887 strncpy(mp
->mnt_vfsstat
.f_fstypename
, vfsp
->vfc_name
, MFSTYPENAMELEN
);
888 mp
->mnt_vfsstat
.f_mntonname
[0] = '/';
889 /* XXX const poisoning layering violation */
890 (void) copystr((const void *)devname
, mp
->mnt_vfsstat
.f_mntfromname
, MAXPATHLEN
- 1, NULL
);
893 mac_mount_label_init(mp
);
894 mac_mount_label_associate(vfs_context_kernel(), mp
);
900 vfs_rootmountalloc(const char *fstypename
, const char *devname
, mount_t
*mpp
)
902 struct vfstable
*vfsp
;
904 for (vfsp
= vfsconf
; vfsp
; vfsp
= vfsp
->vfc_next
)
905 if (!strncmp(vfsp
->vfc_name
, fstypename
,
906 sizeof(vfsp
->vfc_name
)))
911 *mpp
= vfs_rootmountalloc_internal(vfsp
, devname
);
921 * Find an appropriate filesystem to use for the root. If a filesystem
922 * has not been preselected, walk through the list of known filesystems
923 * trying those that have mountroot routines, and try them until one
924 * works or we have tried them all.
926 extern int (*mountroot
)(void);
934 struct vfstable
*vfsp
;
935 vfs_context_t ctx
= vfs_context_kernel();
936 struct vfs_attr vfsattr
;
939 vnode_t bdevvp_rootvp
;
941 if (mountroot
!= NULL
) {
943 * used for netboot which follows a different set of rules
945 error
= (*mountroot
)();
948 if ((error
= bdevvp(rootdev
, &rootvp
))) {
949 printf("vfs_mountroot: can't setup bdevvp\n");
953 * 4951998 - code we call in vfc_mountroot may replace rootvp
954 * so keep a local copy for some house keeping.
956 bdevvp_rootvp
= rootvp
;
958 for (vfsp
= vfsconf
; vfsp
; vfsp
= vfsp
->vfc_next
) {
959 if (vfsp
->vfc_mountroot
== NULL
)
962 mp
= vfs_rootmountalloc_internal(vfsp
, "root_device");
963 mp
->mnt_devvp
= rootvp
;
965 if ((error
= (*vfsp
->vfc_mountroot
)(mp
, rootvp
, ctx
)) == 0) {
966 if ( bdevvp_rootvp
!= rootvp
) {
969 * bump the iocount and fix up mnt_devvp for the
970 * new rootvp (it will already have a usecount taken)...
971 * drop the iocount and the usecount on the orignal
972 * since we are no longer going to use it...
974 vnode_getwithref(rootvp
);
975 mp
->mnt_devvp
= rootvp
;
977 vnode_rele(bdevvp_rootvp
);
978 vnode_put(bdevvp_rootvp
);
980 mp
->mnt_devvp
->v_specflags
|= SI_MOUNTEDON
;
987 * cache the IO attributes for the underlying physical media...
988 * an error return indicates the underlying driver doesn't
989 * support all the queries necessary... however, reasonable
990 * defaults will have been set, so no reason to bail or care
992 vfs_init_io_attributes(rootvp
, mp
);
995 * Shadow the VFC_VFSNATIVEXATTR flag to MNTK_EXTENDED_ATTRS.
997 if (mp
->mnt_vtable
->vfc_vfsflags
& VFC_VFSNATIVEXATTR
) {
998 mp
->mnt_kern_flag
|= MNTK_EXTENDED_ATTRS
;
1000 if (mp
->mnt_vtable
->vfc_vfsflags
& VFC_VFSPREFLIGHT
) {
1001 mp
->mnt_kern_flag
|= MNTK_UNMOUNT_PREFLIGHT
;
1005 * Probe root file system for additional features.
1007 (void)VFS_START(mp
, 0, ctx
);
1009 VFSATTR_INIT(&vfsattr
);
1010 VFSATTR_WANTED(&vfsattr
, f_capabilities
);
1011 if (vfs_getattr(mp
, &vfsattr
, ctx
) == 0 &&
1012 VFSATTR_IS_SUPPORTED(&vfsattr
, f_capabilities
)) {
1013 if ((vfsattr
.f_capabilities
.capabilities
[VOL_CAPABILITIES_INTERFACES
] & VOL_CAP_INT_EXTENDED_ATTR
) &&
1014 (vfsattr
.f_capabilities
.valid
[VOL_CAPABILITIES_INTERFACES
] & VOL_CAP_INT_EXTENDED_ATTR
)) {
1015 mp
->mnt_kern_flag
|= MNTK_EXTENDED_ATTRS
;
1018 if ((vfsattr
.f_capabilities
.capabilities
[VOL_CAPABILITIES_INTERFACES
] & VOL_CAP_INT_NAMEDSTREAMS
) &&
1019 (vfsattr
.f_capabilities
.valid
[VOL_CAPABILITIES_INTERFACES
] & VOL_CAP_INT_NAMEDSTREAMS
)) {
1020 mp
->mnt_kern_flag
|= MNTK_NAMED_STREAMS
;
1023 if ((vfsattr
.f_capabilities
.capabilities
[VOL_CAPABILITIES_FORMAT
] & VOL_CAP_FMT_PATH_FROM_ID
) &&
1024 (vfsattr
.f_capabilities
.valid
[VOL_CAPABILITIES_FORMAT
] & VOL_CAP_FMT_PATH_FROM_ID
)) {
1025 mp
->mnt_kern_flag
|= MNTK_PATH_FROM_ID
;
1030 * get rid of iocount reference returned
1031 * by bdevvp (or picked up by us on the substitued
1032 * rootvp)... it (or we) will have also taken
1033 * a usecount reference which we want to keep
1038 if ((vfs_flags(mp
) & MNT_MULTILABEL
) == 0)
1041 error
= VFS_ROOT(mp
, &vp
, ctx
);
1043 printf("%s() VFS_ROOT() returned %d\n",
1045 dounmount(mp
, MNT_FORCE
, 0, ctx
);
1049 /* VFS_ROOT provides reference so flags = 0 */
1050 error
= vnode_label(mp
, NULL
, vp
, NULL
, 0, ctx
);
1052 printf("%s() vnode_label() returned %d\n",
1054 dounmount(mp
, MNT_FORCE
, 0, ctx
);
1063 vfs_rootmountfailed(mp
);
1065 if (error
!= EINVAL
)
1066 printf("%s_mountroot failed: %d\n", vfsp
->vfc_name
, error
);
1072 * Lookup a mount point by filesystem identifier.
1074 extern mount_t
vfs_getvfs_locked(fsid_t
*);
1077 vfs_getvfs(fsid_t
*fsid
)
1079 return (mount_list_lookupby_fsid(fsid
, 0, 0));
1083 vfs_getvfs_locked(fsid_t
*fsid
)
1085 return(mount_list_lookupby_fsid(fsid
, 1, 0));
1089 vfs_getvfs_by_mntonname(char *path
)
1091 mount_t retmp
= (mount_t
)0;
1095 TAILQ_FOREACH(mp
, &mountlist
, mnt_list
) {
1096 if (!strncmp(mp
->mnt_vfsstat
.f_mntonname
, path
,
1097 sizeof(mp
->mnt_vfsstat
.f_mntonname
))) {
1103 mount_list_unlock();
1107 /* generation number for creation of new fsids */
1108 u_short mntid_gen
= 0;
1110 * Get a new unique fsid
1113 vfs_getnewfsid(struct mount
*mp
)
1122 /* generate a new fsid */
1123 mtype
= mp
->mnt_vtable
->vfc_typenum
;
1124 if (++mntid_gen
== 0)
1126 tfsid
.val
[0] = makedev(nblkdev
+ mtype
, mntid_gen
);
1127 tfsid
.val
[1] = mtype
;
1129 TAILQ_FOREACH(nmp
, &mountlist
, mnt_list
) {
1130 while (vfs_getvfs_locked(&tfsid
)) {
1131 if (++mntid_gen
== 0)
1133 tfsid
.val
[0] = makedev(nblkdev
+ mtype
, mntid_gen
);
1136 mp
->mnt_vfsstat
.f_fsid
.val
[0] = tfsid
.val
[0];
1137 mp
->mnt_vfsstat
.f_fsid
.val
[1] = tfsid
.val
[1];
1138 mount_list_unlock();
1142 * Routines having to do with the management of the vnode table.
1144 extern int (**dead_vnodeop_p
)(void *);
1145 long numvnodes
, freevnodes
, deadvnodes
;
1149 * Move a vnode from one mount queue to another.
1152 insmntque(vnode_t vp
, mount_t mp
)
1156 * Delete from old mount point vnode list, if on one.
1158 if ( (lmp
= vp
->v_mount
) != NULL
&& lmp
!= dead_mountp
) {
1159 if ((vp
->v_lflag
& VNAMED_MOUNT
) == 0)
1160 panic("insmntque: vp not in mount vnode list");
1161 vp
->v_lflag
&= ~VNAMED_MOUNT
;
1167 if (vp
->v_mntvnodes
.tqe_next
== NULL
) {
1168 if (TAILQ_LAST(&lmp
->mnt_vnodelist
, vnodelst
) == vp
)
1169 TAILQ_REMOVE(&lmp
->mnt_vnodelist
, vp
, v_mntvnodes
);
1170 else if (TAILQ_LAST(&lmp
->mnt_newvnodes
, vnodelst
) == vp
)
1171 TAILQ_REMOVE(&lmp
->mnt_newvnodes
, vp
, v_mntvnodes
);
1172 else if (TAILQ_LAST(&lmp
->mnt_workerqueue
, vnodelst
) == vp
)
1173 TAILQ_REMOVE(&lmp
->mnt_workerqueue
, vp
, v_mntvnodes
);
1175 vp
->v_mntvnodes
.tqe_next
->v_mntvnodes
.tqe_prev
= vp
->v_mntvnodes
.tqe_prev
;
1176 *vp
->v_mntvnodes
.tqe_prev
= vp
->v_mntvnodes
.tqe_next
;
1178 vp
->v_mntvnodes
.tqe_next
= NULL
;
1179 vp
->v_mntvnodes
.tqe_prev
= NULL
;
1185 * Insert into list of vnodes for the new mount point, if available.
1187 if ((vp
->v_mount
= mp
) != NULL
) {
1189 if ((vp
->v_mntvnodes
.tqe_next
!= 0) && (vp
->v_mntvnodes
.tqe_prev
!= 0))
1190 panic("vp already in mount list");
1191 if (mp
->mnt_lflag
& MNT_LITER
)
1192 TAILQ_INSERT_HEAD(&mp
->mnt_newvnodes
, vp
, v_mntvnodes
);
1194 TAILQ_INSERT_HEAD(&mp
->mnt_vnodelist
, vp
, v_mntvnodes
);
1195 if (vp
->v_lflag
& VNAMED_MOUNT
)
1196 panic("insmntque: vp already in mount vnode list");
1197 if ((vp
->v_freelist
.tqe_prev
!= (struct vnode
**)0xdeadb))
1198 panic("insmntque: vp on the free list\n");
1199 vp
->v_lflag
|= VNAMED_MOUNT
;
1207 * Create a vnode for a block device.
1208 * Used for root filesystem, argdev, and swap areas.
1209 * Also used for memory file system special devices.
1212 bdevvp(dev_t dev
, vnode_t
*vpp
)
1216 struct vnode_fsparam vfsp
;
1217 struct vfs_context context
;
1224 context
.vc_thread
= current_thread();
1225 context
.vc_ucred
= FSCRED
;
1227 vfsp
.vnfs_mp
= (struct mount
*)0;
1228 vfsp
.vnfs_vtype
= VBLK
;
1229 vfsp
.vnfs_str
= "bdevvp";
1230 vfsp
.vnfs_dvp
= NULL
;
1231 vfsp
.vnfs_fsnode
= NULL
;
1232 vfsp
.vnfs_cnp
= NULL
;
1233 vfsp
.vnfs_vops
= spec_vnodeop_p
;
1234 vfsp
.vnfs_rdev
= dev
;
1235 vfsp
.vnfs_filesize
= 0;
1237 vfsp
.vnfs_flags
= VNFS_NOCACHE
| VNFS_CANTCACHE
;
1239 vfsp
.vnfs_marksystem
= 0;
1240 vfsp
.vnfs_markroot
= 0;
1242 if ( (error
= vnode_create(VNCREATE_FLAVOR
, VCREATESIZE
, &vfsp
, &nvp
)) ) {
1246 vnode_lock_spin(nvp
);
1247 nvp
->v_flag
|= VBDEVVP
;
1248 nvp
->v_tag
= VT_NON
; /* set this to VT_NON so during aliasing it can be replaced */
1250 if ( (error
= vnode_ref(nvp
)) ) {
1251 panic("bdevvp failed: vnode_ref");
1254 if ( (error
= VNOP_FSYNC(nvp
, MNT_WAIT
, &context
)) ) {
1255 panic("bdevvp failed: fsync");
1258 if ( (error
= buf_invalidateblks(nvp
, BUF_WRITE_DATA
, 0, 0)) ) {
1259 panic("bdevvp failed: invalidateblks");
1265 * XXXMAC: We can't put a MAC check here, the system will
1266 * panic without this vnode.
1270 if ( (error
= VNOP_OPEN(nvp
, FREAD
, &context
)) ) {
1271 panic("bdevvp failed: open");
1280 * Check to see if the new vnode represents a special device
1281 * for which we already have a vnode (either because of
1282 * bdevvp() or because of a different vnode representing
1283 * the same block device). If such an alias exists, deallocate
1284 * the existing contents and return the aliased vnode. The
1285 * caller is responsible for filling it with its new contents.
1288 checkalias(struct vnode
*nvp
, dev_t nvp_rdev
)
1294 vpp
= &speclisth
[SPECHASH(nvp_rdev
)];
1298 for (vp
= *vpp
; vp
; vp
= vp
->v_specnext
) {
1299 if (nvp_rdev
== vp
->v_rdev
&& nvp
->v_type
== vp
->v_type
) {
1307 if (vnode_getwithvid(vp
,vid
)) {
1311 * Termination state is checked in vnode_getwithvid
1316 * Alias, but not in use, so flush it out.
1318 if ((vp
->v_iocount
== 1) && (vp
->v_usecount
== 0)) {
1319 vnode_reclaim_internal(vp
, 1, 0, 0);
1325 if (vp
== NULL
|| vp
->v_tag
!= VT_NON
) {
1327 MALLOC_ZONE(nvp
->v_specinfo
, struct specinfo
*, sizeof(struct specinfo
),
1328 M_SPECINFO
, M_WAITOK
);
1329 bzero(nvp
->v_specinfo
, sizeof(struct specinfo
));
1330 nvp
->v_rdev
= nvp_rdev
;
1331 nvp
->v_specflags
= 0;
1332 nvp
->v_speclastr
= -1;
1335 nvp
->v_hashchain
= vpp
;
1336 nvp
->v_specnext
= *vpp
;
1341 nvp
->v_flag
|= VALIASED
;
1342 vp
->v_flag
|= VALIASED
;
1348 if ((vp
->v_flag
& (VBDEVVP
| VDEVFLUSH
)) != 0)
1351 panic("checkalias with VT_NON vp that shouldn't: %x", (unsigned int)vp
);
1359 * Get a reference on a particular vnode and lock it if requested.
1360 * If the vnode was on the inactive list, remove it from the list.
1361 * If the vnode was on the free list, remove it from the list and
1362 * move it to inactive list as needed.
1363 * The vnode lock bit is set if the vnode is being eliminated in
1364 * vgone. The process is awakened when the transition is completed,
1365 * and an error returned to indicate that the vnode is no longer
1366 * usable (possibly having been changed to a new file system type).
1369 vget_internal(vnode_t vp
, int vid
, int vflags
)
1374 vnode_lock_spin(vp
);
1376 if (vflags
& VNODE_WITHID
)
1379 vpid
= vp
->v_id
; // save off the original v_id
1381 if ((vflags
& VNODE_WRITEABLE
) && (vp
->v_writecount
== 0))
1383 * vnode to be returned only if it has writers opened
1387 error
= vnode_getiocount(vp
, vpid
, vflags
);
1395 * Returns: 0 Success
1396 * ENOENT No such file or directory [terminating]
1399 vnode_ref(vnode_t vp
)
1402 return (vnode_ref_ext(vp
, 0));
1406 * Returns: 0 Success
1407 * ENOENT No such file or directory [terminating]
1410 vnode_ref_ext(vnode_t vp
, int fmode
)
1414 vnode_lock_spin(vp
);
1417 * once all the current call sites have been fixed to insure they have
1418 * taken an iocount, we can toughen this assert up and insist that the
1419 * iocount is non-zero... a non-zero usecount doesn't insure correctness
1421 if (vp
->v_iocount
<= 0 && vp
->v_usecount
<= 0)
1422 panic("vnode_ref_ext: vp %p has no valid reference %d, %d", vp
, vp
->v_iocount
, vp
->v_usecount
);
1425 * if you are the owner of drain/termination, can acquire usecount
1427 if ((vp
->v_lflag
& (VL_DRAIN
| VL_TERMINATE
| VL_DEAD
))) {
1428 if (vp
->v_owner
!= current_thread()) {
1435 if (fmode
& FWRITE
) {
1436 if (++vp
->v_writecount
<= 0)
1437 panic("vnode_ref_ext: v_writecount");
1439 if (fmode
& O_EVTONLY
) {
1440 if (++vp
->v_kusecount
<= 0)
1441 panic("vnode_ref_ext: v_kusecount");
1443 if (vp
->v_flag
& VRAGE
) {
1446 ut
= get_bsdthread_info(current_thread());
1448 if ( !(current_proc()->p_lflag
& P_LRAGE_VNODES
) &&
1449 !(ut
->uu_flag
& UT_RAGE_VNODES
)) {
1451 * a 'normal' process accessed this vnode
1452 * so make sure its no longer marked
1453 * for rapid aging... also, make sure
1454 * it gets removed from the rage list...
1455 * when v_usecount drops back to 0, it
1456 * will be put back on the real free list
1458 vp
->v_flag
&= ~VRAGE
;
1459 vp
->v_references
= 0;
1460 vnode_list_remove(vp
);
1471 * put the vnode on appropriate free list.
1472 * called with vnode LOCKED
1475 vnode_list_add(vnode_t vp
)
1478 * if it is already on a list or non zero references return
1480 if (VONLIST(vp
) || (vp
->v_usecount
!= 0) || (vp
->v_iocount
!= 0))
1484 if ((vp
->v_flag
& VRAGE
) && !(vp
->v_lflag
& VL_DEAD
)) {
1486 * add the new guy to the appropriate end of the RAGE list
1488 if ((vp
->v_flag
& VAGE
))
1489 TAILQ_INSERT_HEAD(&vnode_rage_list
, vp
, v_freelist
);
1491 TAILQ_INSERT_TAIL(&vnode_rage_list
, vp
, v_freelist
);
1493 vp
->v_listflag
|= VLIST_RAGE
;
1497 * reset the timestamp for the last inserted vp on the RAGE
1498 * queue to let new_vnode know that its not ok to start stealing
1499 * from this list... as long as we're actively adding to this list
1500 * we'll push out the vnodes we want to donate to the real free list
1501 * once we stop pushing, we'll let some time elapse before we start
1502 * stealing them in the new_vnode routine
1504 microuptime(&rage_tv
);
1507 * if VL_DEAD, insert it at head of the dead list
1508 * else insert at tail of LRU list or at head if VAGE is set
1510 if ( (vp
->v_lflag
& VL_DEAD
)) {
1511 TAILQ_INSERT_HEAD(&vnode_dead_list
, vp
, v_freelist
);
1512 vp
->v_listflag
|= VLIST_DEAD
;
1514 } else if ((vp
->v_flag
& VAGE
)) {
1515 TAILQ_INSERT_HEAD(&vnode_free_list
, vp
, v_freelist
);
1516 vp
->v_flag
&= ~VAGE
;
1519 TAILQ_INSERT_TAIL(&vnode_free_list
, vp
, v_freelist
);
1523 vnode_list_unlock();
1527 * remove the vnode from appropriate free list.
1530 vnode_list_remove(vnode_t vp
)
1533 * we want to avoid taking the list lock
1534 * in the case where we're not on the free
1535 * list... this will be true for most
1536 * directories and any currently in use files
1538 * we're guaranteed that we can't go from
1539 * the not-on-list state to the on-list
1540 * state since we hold the vnode lock...
1541 * all calls to vnode_list_add are done
1542 * under the vnode lock... so we can
1543 * check for that condition (the prevelant one)
1544 * without taking the list lock
1549 * however, we're not guaranteed that
1550 * we won't go from the on-list state
1551 * to the non-on-list state until we
1552 * hold the vnode_list_lock... this
1553 * is due to new_vnode removing vnodes
1554 * from the free list uder the list_lock
1555 * w/o the vnode lock... so we need to
1556 * check again whether we're currently
1560 if (vp
->v_listflag
& VLIST_RAGE
)
1561 VREMRAGE("vnode_list_remove", vp
);
1562 else if (vp
->v_listflag
& VLIST_DEAD
)
1563 VREMDEAD("vnode_list_remove", vp
);
1565 VREMFREE("vnode_list_remove", vp
);
1569 vnode_list_unlock();
1575 vnode_rele(vnode_t vp
)
1577 vnode_rele_internal(vp
, 0, 0, 0);
1582 vnode_rele_ext(vnode_t vp
, int fmode
, int dont_reenter
)
1584 vnode_rele_internal(vp
, fmode
, dont_reenter
, 0);
1589 vnode_rele_internal(vnode_t vp
, int fmode
, int dont_reenter
, int locked
)
1592 vnode_lock_spin(vp
);
1594 if (--vp
->v_usecount
< 0)
1595 panic("vnode_rele_ext: vp %p usecount -ve : %d", vp
, vp
->v_usecount
);
1597 if (fmode
& FWRITE
) {
1598 if (--vp
->v_writecount
< 0)
1599 panic("vnode_rele_ext: vp %p writecount -ve : %ld", vp
, vp
->v_writecount
);
1601 if (fmode
& O_EVTONLY
) {
1602 if (--vp
->v_kusecount
< 0)
1603 panic("vnode_rele_ext: vp %p kusecount -ve : %d", vp
, vp
->v_kusecount
);
1605 if (vp
->v_kusecount
> vp
->v_usecount
)
1606 panic("vnode_rele_ext: vp %p kusecount(%d) out of balance with usecount(%d)\n",vp
, vp
->v_kusecount
, vp
->v_usecount
);
1607 if ((vp
->v_iocount
> 0) || (vp
->v_usecount
> 0)) {
1609 * vnode is still busy... if we're the last
1610 * usecount, mark for a future call to VNOP_INACTIVE
1611 * when the iocount finally drops to 0
1613 if (vp
->v_usecount
== 0) {
1614 vp
->v_lflag
|= VL_NEEDINACTIVE
;
1615 vp
->v_flag
&= ~(VNOCACHE_DATA
| VRAOFF
| VOPENEVT
);
1621 vp
->v_flag
&= ~(VNOCACHE_DATA
| VRAOFF
| VOPENEVT
);
1623 if ( (vp
->v_lflag
& (VL_TERMINATE
| VL_DEAD
)) || dont_reenter
) {
1625 * vnode is being cleaned, or
1626 * we've requested that we don't reenter
1627 * the filesystem on this release... in
1628 * this case, we'll mark the vnode aged
1629 * if it's been marked for termination
1632 if ( !(vp
->v_lflag
& (VL_TERMINATE
| VL_DEAD
| VL_MARKTERM
)) )
1633 vp
->v_lflag
|= VL_NEEDINACTIVE
;
1642 * at this point both the iocount and usecount
1644 * pick up an iocount so that we can call
1645 * VNOP_INACTIVE with the vnode lock unheld
1651 vp
->v_lflag
&= ~VL_NEEDINACTIVE
;
1654 VNOP_INACTIVE(vp
, vfs_context_current());
1656 vnode_lock_spin(vp
);
1658 * because we dropped the vnode lock to call VNOP_INACTIVE
1659 * the state of the vnode may have changed... we may have
1660 * picked up an iocount, usecount or the MARKTERM may have
1661 * been set... we need to reevaluate the reference counts
1662 * to determine if we can call vnode_reclaim_internal at
1663 * this point... if the reference counts are up, we'll pick
1664 * up the MARKTERM state when they get subsequently dropped
1666 if ( (vp
->v_iocount
== 1) && (vp
->v_usecount
== 0) &&
1667 ((vp
->v_lflag
& (VL_MARKTERM
| VL_TERMINATE
| VL_DEAD
)) == VL_MARKTERM
)) {
1670 ut
= get_bsdthread_info(current_thread());
1672 if (ut
->uu_defer_reclaims
) {
1673 vp
->v_defer_reclaimlist
= ut
->uu_vreclaims
;
1674 ut
->uu_vreclaims
= vp
;
1677 vnode_lock_convert(vp
);
1678 vnode_reclaim_internal(vp
, 1, 0, 0);
1680 vnode_dropiocount(vp
);
1689 * Remove any vnodes in the vnode table belonging to mount point mp.
1691 * If MNT_NOFORCE is specified, there should not be any active ones,
1692 * return error if any are found (nb: this is a user error, not a
1693 * system error). If MNT_FORCE is specified, detach any active vnodes
1697 int busyprt
= 0; /* print out busy vnodes */
1699 struct ctldebug debug1
= { "busyprt", &busyprt
};
1704 vflush(struct mount
*mp
, struct vnode
*skipvp
, int flags
)
1713 vnode_iterate_setup(mp
);
1715 * On regular unmounts(not forced) do a
1716 * quick check for vnodes to be in use. This
1717 * preserves the caching of vnodes. automounter
1718 * tries unmounting every so often to see whether
1719 * it is still busy or not.
1721 if (((flags
& FORCECLOSE
)==0) && ((mp
->mnt_kern_flag
& MNTK_UNMOUNT_PREFLIGHT
) != 0)) {
1722 if (vnode_umount_preflight(mp
, skipvp
, flags
)) {
1723 vnode_iterate_clear(mp
);
1729 /* it is returns 0 then there is nothing to do */
1730 retval
= vnode_iterate_prepare(mp
);
1733 vnode_iterate_clear(mp
);
1738 /* iterate over all the vnodes */
1739 while (!TAILQ_EMPTY(&mp
->mnt_workerqueue
)) {
1740 vp
= TAILQ_FIRST(&mp
->mnt_workerqueue
);
1741 TAILQ_REMOVE(&mp
->mnt_workerqueue
, vp
, v_mntvnodes
);
1742 TAILQ_INSERT_TAIL(&mp
->mnt_vnodelist
, vp
, v_mntvnodes
);
1743 if ( (vp
->v_mount
!= mp
) || (vp
== skipvp
)) {
1750 if ((vp
->v_id
!= vid
) || ((vp
->v_lflag
& (VL_DEAD
| VL_TERMINATE
)))) {
1757 * If requested, skip over vnodes marked VSYSTEM.
1758 * Skip over all vnodes marked VNOFLUSH.
1760 if ((flags
& SKIPSYSTEM
) && ((vp
->v_flag
& VSYSTEM
) ||
1761 (vp
->v_flag
& VNOFLUSH
))) {
1767 * If requested, skip over vnodes marked VSWAP.
1769 if ((flags
& SKIPSWAP
) && (vp
->v_flag
& VSWAP
)) {
1775 * If requested, skip over vnodes marked VSWAP.
1777 if ((flags
& SKIPROOT
) && (vp
->v_flag
& VROOT
)) {
1783 * If WRITECLOSE is set, only flush out regular file
1784 * vnodes open for writing.
1786 if ((flags
& WRITECLOSE
) &&
1787 (vp
->v_writecount
== 0 || vp
->v_type
!= VREG
)) {
1793 * If the real usecount is 0, all we need to do is clear
1794 * out the vnode data structures and we are done.
1796 if (((vp
->v_usecount
== 0) ||
1797 ((vp
->v_usecount
- vp
->v_kusecount
) == 0))) {
1798 vp
->v_iocount
++; /* so that drain waits for * other iocounts */
1802 vnode_reclaim_internal(vp
, 1, 0, 0);
1803 vnode_dropiocount(vp
);
1812 * If FORCECLOSE is set, forcibly close the vnode.
1813 * For block or character devices, revert to an
1814 * anonymous device. For all other files, just kill them.
1816 if (flags
& FORCECLOSE
) {
1817 if (vp
->v_type
!= VBLK
&& vp
->v_type
!= VCHR
) {
1818 vp
->v_iocount
++; /* so that drain waits * for other iocounts */
1822 vnode_reclaim_internal(vp
, 1, 0, 0);
1823 vnode_dropiocount(vp
);
1828 vp
->v_lflag
&= ~VL_DEAD
;
1829 vp
->v_op
= spec_vnodeop_p
;
1830 vp
->v_flag
|= VDEVFLUSH
;
1838 vprint("vflush: busy vnode", vp
);
1845 /* At this point the worker queue is completed */
1846 if (busy
&& ((flags
& FORCECLOSE
)==0) && reclaimed
) {
1849 (void)vnode_iterate_reloadq(mp
);
1850 /* returned with mount lock held */
1854 /* if new vnodes were created in between retry the reclaim */
1855 if ( vnode_iterate_reloadq(mp
) != 0) {
1856 if (!(busy
&& ((flags
& FORCECLOSE
)==0)))
1859 vnode_iterate_clear(mp
);
1862 if (busy
&& ((flags
& FORCECLOSE
)==0))
1867 long num_recycledvnodes
= 0; /* long for OSAddAtomic */
1869 * Disassociate the underlying file system from a vnode.
1870 * The vnode lock is held on entry.
1873 vclean(vnode_t vp
, int flags
)
1875 vfs_context_t ctx
= vfs_context_current();
1878 int already_terminating
;
1882 * Check to see if the vnode is in use.
1883 * If so we have to reference it before we clean it out
1884 * so that its count cannot fall to zero and generate a
1885 * race against ourselves to recycle it.
1887 active
= vp
->v_usecount
;
1890 * just in case we missed sending a needed
1891 * VNOP_INACTIVE, we'll do it now
1893 need_inactive
= (vp
->v_lflag
& VL_NEEDINACTIVE
);
1895 vp
->v_lflag
&= ~VL_NEEDINACTIVE
;
1898 * Prevent the vnode from being recycled or
1899 * brought into use while we clean it out.
1901 already_terminating
= (vp
->v_lflag
& VL_TERMINATE
);
1903 vp
->v_lflag
|= VL_TERMINATE
;
1906 * remove the vnode from any mount list
1909 insmntque(vp
, (struct mount
*)0);
1913 OSAddAtomic(1, &num_recycledvnodes
);
1915 * purge from the name cache as early as possible...
1919 if (flags
& DOCLOSE
)
1920 clflags
|= IO_NDELAY
;
1921 if (flags
& REVOKEALL
)
1922 clflags
|= IO_REVOKE
;
1924 if (active
&& (flags
& DOCLOSE
))
1925 VNOP_CLOSE(vp
, clflags
, ctx
);
1928 * Clean out any buffers associated with the vnode.
1930 if (flags
& DOCLOSE
) {
1932 if (vp
->v_tag
== VT_NFS
)
1933 nfs_vinvalbuf(vp
, V_SAVE
, ctx
, 0);
1937 VNOP_FSYNC(vp
, MNT_WAIT
, ctx
);
1938 buf_invalidateblks(vp
, BUF_WRITE_DATA
, 0, 0);
1940 if (UBCINFOEXISTS(vp
))
1942 * Clean the pages in VM.
1944 (void)ubc_sync_range(vp
, (off_t
)0, ubc_getsize(vp
), UBC_PUSHALL
);
1946 if (active
|| need_inactive
)
1947 VNOP_INACTIVE(vp
, ctx
);
1950 * Destroy ubc named reference
1951 * cluster_release is done on this path
1952 * along with dropping the reference on the ucred
1954 ubc_destroy_named(vp
);
1957 * Reclaim the vnode.
1959 if (VNOP_RECLAIM(vp
, ctx
))
1960 panic("vclean: cannot reclaim");
1962 // make sure the name & parent ptrs get cleaned out!
1963 vnode_update_identity(vp
, NULLVP
, NULL
, 0, 0, VNODE_UPDATE_PARENT
| VNODE_UPDATE_NAME
);
1967 vp
->v_mount
= dead_mountp
;
1968 vp
->v_op
= dead_vnodeop_p
;
1972 vp
->v_lflag
|= VL_DEAD
;
1974 if (already_terminating
== 0) {
1975 vp
->v_lflag
&= ~VL_TERMINATE
;
1977 * Done with purge, notify sleepers of the grim news.
1979 if (vp
->v_lflag
& VL_TERMWANT
) {
1980 vp
->v_lflag
&= ~VL_TERMWANT
;
1981 wakeup(&vp
->v_lflag
);
1987 * Eliminate all activity associated with the requested vnode
1988 * and with all vnodes aliased to the requested vnode.
1992 vn_revoke(vnode_t vp
, int flags
, __unused vfs_context_t a_context
)
1994 vn_revoke(vnode_t vp
, __unused
int flags
, __unused vfs_context_t a_context
)
2001 if ((flags
& REVOKEALL
) == 0)
2002 panic("vnop_revoke");
2005 if (vp
->v_flag
& VALIASED
) {
2007 * If a vgone (or vclean) is already in progress,
2008 * wait until it is done and return.
2011 if (vp
->v_lflag
& VL_TERMINATE
) {
2017 * Ensure that vp will not be vgone'd while we
2018 * are eliminating its aliases.
2021 while (vp
->v_flag
& VALIASED
) {
2022 for (vq
= *vp
->v_hashchain
; vq
; vq
= vq
->v_specnext
) {
2023 if (vq
->v_rdev
!= vp
->v_rdev
||
2024 vq
->v_type
!= vp
->v_type
|| vp
== vq
)
2028 if (vnode_getwithvid(vq
,vid
)){
2032 vnode_reclaim_internal(vq
, 0, 0, 0);
2040 vnode_reclaim_internal(vp
, 0, 0, REVOKEALL
);
2046 * Recycle an unused vnode to the front of the free list.
2047 * Release the passed interlock if the vnode will be recycled.
2050 vnode_recycle(struct vnode
*vp
)
2054 if (vp
->v_iocount
|| vp
->v_usecount
) {
2055 vp
->v_lflag
|= VL_MARKTERM
;
2059 vnode_reclaim_internal(vp
, 1, 0, 0);
2066 vnode_reload(vnode_t vp
)
2068 vnode_lock_spin(vp
);
2070 if ((vp
->v_iocount
> 1) || vp
->v_usecount
) {
2074 if (vp
->v_iocount
<= 0)
2075 panic("vnode_reload with no iocount %d", vp
->v_iocount
);
2077 /* mark for release when iocount is dopped */
2078 vp
->v_lflag
|= VL_MARKTERM
;
2086 vgone(vnode_t vp
, int flags
)
2092 * Clean out the filesystem specific data.
2093 * vclean also takes care of removing the
2094 * vnode from any mount list it might be on
2096 vclean(vp
, flags
| DOCLOSE
);
2099 * If special device, remove it from special device alias list
2102 if ((vp
->v_type
== VBLK
|| vp
->v_type
== VCHR
) && vp
->v_specinfo
!= 0) {
2104 if (*vp
->v_hashchain
== vp
) {
2105 *vp
->v_hashchain
= vp
->v_specnext
;
2107 for (vq
= *vp
->v_hashchain
; vq
; vq
= vq
->v_specnext
) {
2108 if (vq
->v_specnext
!= vp
)
2110 vq
->v_specnext
= vp
->v_specnext
;
2114 panic("missing bdev");
2116 if (vp
->v_flag
& VALIASED
) {
2118 for (vq
= *vp
->v_hashchain
; vq
; vq
= vq
->v_specnext
) {
2119 if (vq
->v_rdev
!= vp
->v_rdev
||
2120 vq
->v_type
!= vp
->v_type
)
2127 panic("missing alias");
2129 vx
->v_flag
&= ~VALIASED
;
2130 vp
->v_flag
&= ~VALIASED
;
2134 struct specinfo
*tmp
= vp
->v_specinfo
;
2135 vp
->v_specinfo
= NULL
;
2136 FREE_ZONE((void *)tmp
, sizeof(struct specinfo
), M_SPECINFO
);
2142 * Lookup a vnode by device number.
2145 check_mountedon(dev_t dev
, enum vtype type
, int *errorp
)
2153 for (vp
= speclisth
[SPECHASH(dev
)]; vp
; vp
= vp
->v_specnext
) {
2154 if (dev
!= vp
->v_rdev
|| type
!= vp
->v_type
)
2158 if (vnode_getwithvid(vp
,vid
))
2160 vnode_lock_spin(vp
);
2161 if ((vp
->v_usecount
> 0) || (vp
->v_iocount
> 1)) {
2163 if ((*errorp
= vfs_mountedon(vp
)) != 0)
2175 * Calculate the total number of references to a special device.
2185 if ((vp
->v_flag
& VALIASED
) == 0)
2186 return (vp
->v_usecount
- vp
->v_kusecount
);
2191 * Grab first vnode and its vid.
2193 vq
= *vp
->v_hashchain
;
2194 vid
= vq
? vq
->v_id
: 0;
2200 * Attempt to get the vnode outside the SPECHASH lock.
2202 if (vnode_getwithvid(vq
, vid
)) {
2207 if (vq
->v_rdev
== vp
->v_rdev
&& vq
->v_type
== vp
->v_type
) {
2208 if ((vq
->v_usecount
== 0) && (vq
->v_iocount
== 1) && vq
!= vp
) {
2210 * Alias, but not in use, so flush it out.
2212 vnode_reclaim_internal(vq
, 1, 0, 0);
2217 count
+= (vq
->v_usecount
- vq
->v_kusecount
);
2223 * must do this with the reference still held on 'vq'
2224 * so that it can't be destroyed while we're poking
2225 * through v_specnext
2227 vnext
= vq
->v_specnext
;
2228 vid
= vnext
? vnext
->v_id
: 0;
2240 int prtactive
= 0; /* 1 => print out reclaim of active vnodes */
2243 * Print out a description of a vnode.
2245 #if !CONFIG_NO_PRINTF_STRINGS
2246 static const char *typename
[] =
2247 { "VNON", "VREG", "VDIR", "VBLK", "VCHR", "VLNK", "VSOCK", "VFIFO", "VBAD" };
2251 vprint(const char *label
, struct vnode
*vp
)
2256 printf("%s: ", label
);
2257 printf("type %s, usecount %d, writecount %ld",
2258 typename
[vp
->v_type
], vp
->v_usecount
, vp
->v_writecount
);
2260 if (vp
->v_flag
& VROOT
)
2261 strlcat(sbuf
, "|VROOT", sizeof(sbuf
));
2262 if (vp
->v_flag
& VTEXT
)
2263 strlcat(sbuf
, "|VTEXT", sizeof(sbuf
));
2264 if (vp
->v_flag
& VSYSTEM
)
2265 strlcat(sbuf
, "|VSYSTEM", sizeof(sbuf
));
2266 if (vp
->v_flag
& VNOFLUSH
)
2267 strlcat(sbuf
, "|VNOFLUSH", sizeof(sbuf
));
2268 if (vp
->v_flag
& VBWAIT
)
2269 strlcat(sbuf
, "|VBWAIT", sizeof(sbuf
));
2270 if (vp
->v_flag
& VALIASED
)
2271 strlcat(sbuf
, "|VALIASED", sizeof(sbuf
));
2272 if (sbuf
[0] != '\0')
2273 printf(" flags (%s)", &sbuf
[1]);
2278 vn_getpath(struct vnode
*vp
, char *pathbuf
, int *len
)
2280 return build_path(vp
, pathbuf
, *len
, len
, BUILDPATH_NO_FS_ENTER
, vfs_context_current());
2285 vn_getcdhash(struct vnode
*vp
, off_t offset
, unsigned char *cdhash
)
2287 return ubc_cs_getcdhash(vp
, offset
, cdhash
);
2291 static char *extension_table
=NULL
;
2293 static int max_ext_width
;
2296 extension_cmp(const void *a
, const void *b
)
2298 return (strlen((const char *)a
) - strlen((const char *)b
));
2303 // This is the api LaunchServices uses to inform the kernel
2304 // the list of package extensions to ignore.
2306 // Internally we keep the list sorted by the length of the
2307 // the extension (from longest to shortest). We sort the
2308 // list of extensions so that we can speed up our searches
2309 // when comparing file names -- we only compare extensions
2310 // that could possibly fit into the file name, not all of
2311 // them (i.e. a short 8 character name can't have an 8
2312 // character extension).
2314 __private_extern__
int
2315 set_package_extensions_table(void *data
, int nentries
, int maxwidth
)
2320 if (nentries
<= 0 || nentries
> 1024 || maxwidth
<= 0 || maxwidth
> 255) {
2324 MALLOC(new_exts
, char *, nentries
* maxwidth
, M_TEMP
, M_WAITOK
);
2326 error
= copyin(CAST_USER_ADDR_T(data
), new_exts
, nentries
* maxwidth
);
2328 FREE(new_exts
, M_TEMP
);
2332 if (extension_table
) {
2333 FREE(extension_table
, M_TEMP
);
2335 extension_table
= new_exts
;
2337 max_ext_width
= maxwidth
;
2339 qsort(extension_table
, nexts
, maxwidth
, extension_cmp
);
2345 __private_extern__
int
2346 is_package_name(const char *name
, int len
)
2349 const char *ptr
, *name_ext
;
2356 for(ptr
=name
; *ptr
!= '\0'; ptr
++) {
2362 // if there is no "." extension, it can't match
2363 if (name_ext
== NULL
) {
2367 // advance over the "."
2370 // now iterate over all the extensions to see if any match
2371 ptr
= &extension_table
[0];
2372 for(i
=0; i
< nexts
; i
++, ptr
+=max_ext_width
) {
2373 extlen
= strlen(ptr
);
2374 if (strncasecmp(name_ext
, ptr
, extlen
) == 0 && name_ext
[extlen
] == '\0') {
2380 // if we get here, no extension matched
2385 vn_path_package_check(__unused vnode_t vp
, char *path
, int pathlen
, int *component
)
2396 while(end
< path
+ pathlen
&& *end
!= '\0') {
2397 while(end
< path
+ pathlen
&& *end
== '/' && *end
!= '\0') {
2403 while(end
< path
+ pathlen
&& *end
!= '/' && *end
!= '\0') {
2407 if (end
> path
+ pathlen
) {
2408 // hmm, string wasn't null terminated
2413 if (is_package_name(ptr
, end
- ptr
)) {
2427 * Top level filesystem related information gathering.
2429 extern unsigned int vfs_nummntops
;
2432 vfs_sysctl(int *name
, u_int namelen
, user_addr_t oldp
, size_t *oldlenp
,
2433 user_addr_t newp
, size_t newlen
, proc_t p
)
2435 struct vfstable
*vfsp
;
2439 struct vfsconf
*vfsc
;
2441 /* All non VFS_GENERIC and in VFS_GENERIC,
2442 * VFS_MAXTYPENUM, VFS_CONF, VFS_SET_PACKAGE_EXTS
2443 * needs to have root priv to have modifiers.
2444 * For rest the userland_sysctl(CTLFLAG_ANYBODY) would cover.
2446 if ((newp
!= USER_ADDR_NULL
) && ((name
[0] != VFS_GENERIC
) ||
2447 ((name
[1] == VFS_MAXTYPENUM
) ||
2448 (name
[1] == VFS_CONF
) ||
2449 (name
[1] == VFS_SET_PACKAGE_EXTS
)))
2450 && (error
= suser(kauth_cred_get(), &p
->p_acflag
))) {
2454 * The VFS_NUMMNTOPS shouldn't be at name[0] since
2455 * is a VFS generic variable. So now we must check
2456 * namelen so we don't end up covering any UFS
2457 * variables (sinc UFS vfc_typenum is 1).
2459 * It should have been:
2460 * name[0]: VFS_GENERIC
2461 * name[1]: VFS_NUMMNTOPS
2463 if (namelen
== 1 && name
[0] == VFS_NUMMNTOPS
) {
2464 return (sysctl_rdint(oldp
, oldlenp
, newp
, vfs_nummntops
));
2467 /* all sysctl names at this level are at least name and field */
2469 return (EISDIR
); /* overloaded */
2470 if (name
[0] != VFS_GENERIC
) {
2471 for (vfsp
= vfsconf
; vfsp
; vfsp
= vfsp
->vfc_next
)
2472 if (vfsp
->vfc_typenum
== name
[0])
2477 /* XXX current context proxy for proc p? */
2478 return ((*vfsp
->vfc_vfsops
->vfs_sysctl
)(&name
[1], namelen
- 1,
2479 oldp
, oldlenp
, newp
, newlen
,
2480 vfs_context_current()));
2483 case VFS_MAXTYPENUM
:
2484 return (sysctl_rdint(oldp
, oldlenp
, newp
, maxvfsconf
));
2487 return (ENOTDIR
); /* overloaded */
2488 for (vfsp
= vfsconf
; vfsp
; vfsp
= vfsp
->vfc_next
)
2489 if (vfsp
->vfc_typenum
== name
[2])
2493 vfsc
= (struct vfsconf
*)vfsp
;
2494 if (proc_is64bit(p
)) {
2495 struct user_vfsconf usr_vfsc
;
2496 usr_vfsc
.vfc_vfsops
= CAST_USER_ADDR_T(vfsc
->vfc_vfsops
);
2497 bcopy(vfsc
->vfc_name
, usr_vfsc
.vfc_name
, sizeof(usr_vfsc
.vfc_name
));
2498 usr_vfsc
.vfc_typenum
= vfsc
->vfc_typenum
;
2499 usr_vfsc
.vfc_refcount
= vfsc
->vfc_refcount
;
2500 usr_vfsc
.vfc_flags
= vfsc
->vfc_flags
;
2501 usr_vfsc
.vfc_mountroot
= CAST_USER_ADDR_T(vfsc
->vfc_mountroot
);
2502 usr_vfsc
.vfc_next
= CAST_USER_ADDR_T(vfsc
->vfc_next
);
2503 return (sysctl_rdstruct(oldp
, oldlenp
, newp
, &usr_vfsc
,
2507 return (sysctl_rdstruct(oldp
, oldlenp
, newp
, vfsc
,
2508 sizeof(struct vfsconf
)));
2511 case VFS_SET_PACKAGE_EXTS
:
2512 return set_package_extensions_table((void *)name
[1], name
[2], name
[3]);
2515 * We need to get back into the general MIB, so we need to re-prepend
2516 * CTL_VFS to our name and try userland_sysctl().
2518 usernamelen
= namelen
+ 1;
2519 MALLOC(username
, int *, usernamelen
* sizeof(*username
),
2521 bcopy(name
, username
+ 1, namelen
* sizeof(*name
));
2522 username
[0] = CTL_VFS
;
2523 error
= userland_sysctl(p
, username
, usernamelen
, oldp
,
2524 oldlenp
, 1, newp
, newlen
, oldlenp
);
2525 FREE(username
, M_TEMP
);
2530 * Dump vnode list (via sysctl) - defunct
2531 * use "pstat" instead
2536 (__unused
struct sysctl_oid
*oidp
, __unused
void *arg1
, __unused
int arg2
, __unused
struct sysctl_req
*req
)
2541 SYSCTL_PROC(_kern
, KERN_VNODE
, vnode
,
2542 CTLTYPE_STRUCT
| CTLFLAG_RD
| CTLFLAG_MASKED
,
2543 0, 0, sysctl_vnode
, "S,", "");
2547 * Check to see if a filesystem is mounted on a block device.
2550 vfs_mountedon(struct vnode
*vp
)
2556 if (vp
->v_specflags
& SI_MOUNTEDON
) {
2560 if (vp
->v_flag
& VALIASED
) {
2561 for (vq
= *vp
->v_hashchain
; vq
; vq
= vq
->v_specnext
) {
2562 if (vq
->v_rdev
!= vp
->v_rdev
||
2563 vq
->v_type
!= vp
->v_type
)
2565 if (vq
->v_specflags
& SI_MOUNTEDON
) {
2577 * Unmount all filesystems. The list is traversed in reverse order
2578 * of mounting to avoid dependencies.
2580 __private_extern__
void
2581 vfs_unmountall(void)
2587 * Since this only runs when rebooting, it is not interlocked.
2590 while(!TAILQ_EMPTY(&mountlist
)) {
2591 mp
= TAILQ_LAST(&mountlist
, mntlist
);
2592 mount_list_unlock();
2593 error
= dounmount(mp
, MNT_FORCE
, 0, vfs_context_current());
2594 if ((error
!= 0) && (error
!= EBUSY
)) {
2595 printf("unmount of %s failed (", mp
->mnt_vfsstat
.f_mntonname
);
2596 printf("%d)\n", error
);
2598 TAILQ_REMOVE(&mountlist
, mp
, mnt_list
);
2600 } else if (error
== EBUSY
) {
2601 /* If EBUSY is returned, the unmount was already in progress */
2602 printf("unmount of %x failed (", (unsigned int)mp
);
2607 mount_list_unlock();
2612 * This routine is called from vnode_pager_deallocate out of the VM
2613 * The path to vnode_pager_deallocate can only be initiated by ubc_destroy_named
2614 * on a vnode that has a UBCINFO
2616 __private_extern__
void
2617 vnode_pager_vrele(vnode_t vp
)
2619 struct ubc_info
*uip
;
2623 vp
->v_lflag
&= ~VNAMED_UBC
;
2625 uip
= vp
->v_ubcinfo
;
2626 vp
->v_ubcinfo
= UBC_INFO_NULL
;
2628 ubc_info_deallocate(uip
);
2634 #include <sys/disk.h>
2637 vfs_init_io_attributes(vnode_t devvp
, mount_t mp
)
2641 off_t writeblockcnt
;
2652 vfs_context_t ctx
= vfs_context_current();
2656 * determine if this mount point exists on the same device as the root
2657 * partition... if so, then it comes under the hard throttle control
2660 static int rootunit
= -1;
2662 if (rootunit
== -1) {
2663 if (VNOP_IOCTL(rootvp
, DKIOCGETBSDUNIT
, (caddr_t
)&rootunit
, 0, ctx
))
2665 else if (rootvp
== devvp
)
2666 mp
->mnt_kern_flag
|= MNTK_ROOTDEV
;
2668 if (devvp
!= rootvp
&& rootunit
!= -1) {
2669 if (VNOP_IOCTL(devvp
, DKIOCGETBSDUNIT
, (caddr_t
)&thisunit
, 0, ctx
) == 0) {
2670 if (thisunit
== rootunit
)
2671 mp
->mnt_kern_flag
|= MNTK_ROOTDEV
;
2675 * force the spec device to re-cache
2676 * the underlying block size in case
2677 * the filesystem overrode the initial value
2679 set_fsblocksize(devvp
);
2682 if ((error
= VNOP_IOCTL(devvp
, DKIOCGETBLOCKSIZE
,
2683 (caddr_t
)&blksize
, 0, ctx
)))
2686 mp
->mnt_devblocksize
= blksize
;
2688 if (VNOP_IOCTL(devvp
, DKIOCISVIRTUAL
, (caddr_t
)&isvirtual
, 0, ctx
) == 0) {
2690 mp
->mnt_kern_flag
|= MNTK_VIRTUALDEV
;
2693 if ((error
= VNOP_IOCTL(devvp
, DKIOCGETFEATURES
,
2694 (caddr_t
)&features
, 0, ctx
)))
2697 if ((error
= VNOP_IOCTL(devvp
, DKIOCGETMAXBLOCKCOUNTREAD
,
2698 (caddr_t
)&readblockcnt
, 0, ctx
)))
2701 if ((error
= VNOP_IOCTL(devvp
, DKIOCGETMAXBLOCKCOUNTWRITE
,
2702 (caddr_t
)&writeblockcnt
, 0, ctx
)))
2705 if ((error
= VNOP_IOCTL(devvp
, DKIOCGETMAXBYTECOUNTREAD
,
2706 (caddr_t
)&readmaxcnt
, 0, ctx
)))
2709 if ((error
= VNOP_IOCTL(devvp
, DKIOCGETMAXBYTECOUNTWRITE
,
2710 (caddr_t
)&writemaxcnt
, 0, ctx
)))
2713 if ((error
= VNOP_IOCTL(devvp
, DKIOCGETMAXSEGMENTCOUNTREAD
,
2714 (caddr_t
)&readsegcnt
, 0, ctx
)))
2717 if ((error
= VNOP_IOCTL(devvp
, DKIOCGETMAXSEGMENTCOUNTWRITE
,
2718 (caddr_t
)&writesegcnt
, 0, ctx
)))
2721 if ((error
= VNOP_IOCTL(devvp
, DKIOCGETMAXSEGMENTBYTECOUNTREAD
,
2722 (caddr_t
)&readsegsize
, 0, ctx
)))
2725 if ((error
= VNOP_IOCTL(devvp
, DKIOCGETMAXSEGMENTBYTECOUNTWRITE
,
2726 (caddr_t
)&writesegsize
, 0, ctx
)))
2729 if ((error
= VNOP_IOCTL(devvp
, DKIOCGETMINSEGMENTALIGNMENTBYTECOUNT
,
2730 (caddr_t
)&alignment
, 0, ctx
)))
2734 temp
= (readmaxcnt
> UINT32_MAX
) ? UINT32_MAX
: readmaxcnt
;
2737 temp
= readblockcnt
* blksize
;
2738 temp
= (temp
> UINT32_MAX
) ? UINT32_MAX
: temp
;
2742 mp
->mnt_maxreadcnt
= (u_int32_t
)temp
;
2745 temp
= (writemaxcnt
> UINT32_MAX
) ? UINT32_MAX
: writemaxcnt
;
2747 if (writeblockcnt
) {
2748 temp
= writeblockcnt
* blksize
;
2749 temp
= (temp
> UINT32_MAX
) ? UINT32_MAX
: temp
;
2753 mp
->mnt_maxwritecnt
= (u_int32_t
)temp
;
2756 temp
= (readsegcnt
> UINT16_MAX
) ? UINT16_MAX
: readsegcnt
;
2757 mp
->mnt_segreadcnt
= (u_int16_t
)temp
;
2760 temp
= (writesegcnt
> UINT16_MAX
) ? UINT16_MAX
: writesegcnt
;
2761 mp
->mnt_segwritecnt
= (u_int16_t
)temp
;
2764 temp
= (readsegsize
> UINT32_MAX
) ? UINT32_MAX
: readsegsize
;
2766 temp
= mp
->mnt_maxreadcnt
;
2767 mp
->mnt_maxsegreadsize
= (u_int32_t
)temp
;
2770 temp
= (writesegsize
> UINT32_MAX
) ? UINT32_MAX
: writesegsize
;
2772 temp
= mp
->mnt_maxwritecnt
;
2773 mp
->mnt_maxsegwritesize
= (u_int32_t
)temp
;
2776 temp
= (alignment
> PAGE_SIZE
) ? PAGE_MASK
: alignment
- 1;
2779 mp
->mnt_alignmentmask
= temp
;
2781 if (features
& DK_FEATURE_FORCE_UNIT_ACCESS
)
2782 mp
->mnt_ioflags
|= MNT_IOFLAGS_FUA_SUPPORTED
;
2787 static struct klist fs_klist
;
2790 vfs_event_init(void)
2793 klist_init(&fs_klist
);
2797 vfs_event_signal(__unused fsid_t
*fsid
, u_int32_t event
, __unused
intptr_t data
)
2800 KNOTE(&fs_klist
, event
);
2804 * return the number of mounted filesystems.
2807 sysctl_vfs_getvfscnt(void)
2809 return(mount_getvfscnt());
2814 mount_getvfscnt(void)
2820 mount_list_unlock();
2828 mount_fillfsids(fsid_t
*fsidlst
, int count
)
2835 TAILQ_FOREACH(mp
, &mountlist
, mnt_list
) {
2836 if (actual
<= count
) {
2837 fsidlst
[actual
] = mp
->mnt_vfsstat
.f_fsid
;
2841 mount_list_unlock();
2847 * fill in the array of fsid_t's up to a max of 'count', the actual
2848 * number filled in will be set in '*actual'. If there are more fsid_t's
2849 * than room in fsidlst then ENOMEM will be returned and '*actual' will
2850 * have the actual count.
2851 * having *actual filled out even in the error case is depended upon.
2854 sysctl_vfs_getvfslist(fsid_t
*fsidlst
, int count
, int *actual
)
2860 TAILQ_FOREACH(mp
, &mountlist
, mnt_list
) {
2862 if (*actual
<= count
)
2863 fsidlst
[(*actual
) - 1] = mp
->mnt_vfsstat
.f_fsid
;
2865 mount_list_unlock();
2866 return (*actual
<= count
? 0 : ENOMEM
);
2870 sysctl_vfs_vfslist(__unused
struct sysctl_oid
*oidp
, __unused
void *arg1
,
2871 __unused
int arg2
, struct sysctl_req
*req
)
2877 /* This is a readonly node. */
2878 if (req
->newptr
!= USER_ADDR_NULL
)
2881 /* they are querying us so just return the space required. */
2882 if (req
->oldptr
== USER_ADDR_NULL
) {
2883 req
->oldidx
= sysctl_vfs_getvfscnt() * sizeof(fsid_t
);
2888 * Retrieve an accurate count of the amount of space required to copy
2889 * out all the fsids in the system.
2891 space
= req
->oldlen
;
2892 req
->oldlen
= sysctl_vfs_getvfscnt() * sizeof(fsid_t
);
2894 /* they didn't give us enough space. */
2895 if (space
< req
->oldlen
)
2898 MALLOC(fsidlst
, fsid_t
*, req
->oldlen
, M_TEMP
, M_WAITOK
);
2899 error
= sysctl_vfs_getvfslist(fsidlst
, req
->oldlen
/ sizeof(fsid_t
),
2902 * If we get back ENOMEM, then another mount has been added while we
2903 * slept in malloc above. If this is the case then try again.
2905 if (error
== ENOMEM
) {
2906 FREE(fsidlst
, M_TEMP
);
2907 req
->oldlen
= space
;
2911 error
= SYSCTL_OUT(req
, fsidlst
, actual
* sizeof(fsid_t
));
2913 FREE(fsidlst
, M_TEMP
);
2918 * Do a sysctl by fsid.
2921 sysctl_vfs_ctlbyfsid(__unused
struct sysctl_oid
*oidp
, void *arg1
, int arg2
,
2922 struct sysctl_req
*req
)
2925 struct user_vfsidctl user_vc
;
2927 struct vfsstatfs
*sp
;
2928 int *name
, flags
, namelen
;
2929 int error
=0, gotref
=0;
2930 vfs_context_t ctx
= vfs_context_current();
2931 proc_t p
= req
->p
; /* XXX req->p != current_proc()? */
2932 boolean_t is_64_bit
;
2936 is_64_bit
= proc_is64bit(p
);
2939 error
= SYSCTL_IN(req
, &user_vc
, sizeof(user_vc
));
2942 if (user_vc
.vc_vers
!= VFS_CTL_VERS1
) {
2946 mp
= mount_list_lookupby_fsid(&user_vc
.vc_fsid
, 0, 1);
2949 error
= SYSCTL_IN(req
, &vc
, sizeof(vc
));
2952 if (vc
.vc_vers
!= VFS_CTL_VERS1
) {
2956 mp
= mount_list_lookupby_fsid(&vc
.vc_fsid
, 0, 1);
2963 /* reset so that the fs specific code can fetch it. */
2966 * Note if this is a VFS_CTL then we pass the actual sysctl req
2967 * in for "oldp" so that the lower layer can DTRT and use the
2968 * SYSCTL_IN/OUT routines.
2970 if (mp
->mnt_op
->vfs_sysctl
!= NULL
) {
2972 if (vfs_64bitready(mp
)) {
2973 error
= mp
->mnt_op
->vfs_sysctl(name
, namelen
,
2974 CAST_USER_ADDR_T(req
),
2975 NULL
, USER_ADDR_NULL
, 0,
2983 error
= mp
->mnt_op
->vfs_sysctl(name
, namelen
,
2984 CAST_USER_ADDR_T(req
),
2985 NULL
, USER_ADDR_NULL
, 0,
2988 if (error
!= ENOTSUP
) {
2993 case VFS_CTL_UMOUNT
:
2996 req
->newptr
= user_vc
.vc_ptr
;
2997 req
->newlen
= (size_t)user_vc
.vc_len
;
3000 req
->newptr
= CAST_USER_ADDR_T(vc
.vc_ptr
);
3001 req
->newlen
= vc
.vc_len
;
3003 error
= SYSCTL_IN(req
, &flags
, sizeof(flags
));
3010 /* safedounmount consumes a ref */
3011 error
= safedounmount(mp
, flags
, ctx
);
3013 case VFS_CTL_STATFS
:
3016 req
->newptr
= user_vc
.vc_ptr
;
3017 req
->newlen
= (size_t)user_vc
.vc_len
;
3020 req
->newptr
= CAST_USER_ADDR_T(vc
.vc_ptr
);
3021 req
->newlen
= vc
.vc_len
;
3023 error
= SYSCTL_IN(req
, &flags
, sizeof(flags
));
3026 sp
= &mp
->mnt_vfsstat
;
3027 if (((flags
& MNT_NOWAIT
) == 0 || (flags
& MNT_WAIT
)) &&
3028 (error
= vfs_update_vfsstat(mp
, ctx
, VFS_USER_EVENT
)))
3031 struct user_statfs sfs
;
3032 bzero(&sfs
, sizeof(sfs
));
3033 sfs
.f_flags
= mp
->mnt_flag
& MNT_VISFLAGMASK
;
3034 sfs
.f_type
= mp
->mnt_vtable
->vfc_typenum
;
3035 sfs
.f_bsize
= (user_long_t
)sp
->f_bsize
;
3036 sfs
.f_iosize
= (user_long_t
)sp
->f_iosize
;
3037 sfs
.f_blocks
= (user_long_t
)sp
->f_blocks
;
3038 sfs
.f_bfree
= (user_long_t
)sp
->f_bfree
;
3039 sfs
.f_bavail
= (user_long_t
)sp
->f_bavail
;
3040 sfs
.f_files
= (user_long_t
)sp
->f_files
;
3041 sfs
.f_ffree
= (user_long_t
)sp
->f_ffree
;
3042 sfs
.f_fsid
= sp
->f_fsid
;
3043 sfs
.f_owner
= sp
->f_owner
;
3045 strlcpy(sfs
.f_fstypename
, sp
->f_fstypename
, MFSNAMELEN
);
3046 strlcpy(sfs
.f_mntonname
, sp
->f_mntonname
, MNAMELEN
);
3047 strlcpy(sfs
.f_mntfromname
, sp
->f_mntfromname
, MNAMELEN
);
3049 error
= SYSCTL_OUT(req
, &sfs
, sizeof(sfs
));
3053 bzero(&sfs
, sizeof(struct statfs
));
3054 sfs
.f_flags
= mp
->mnt_flag
& MNT_VISFLAGMASK
;
3055 sfs
.f_type
= mp
->mnt_vtable
->vfc_typenum
;
3058 * It's possible for there to be more than 2^^31 blocks in the filesystem, so we
3059 * have to fudge the numbers here in that case. We inflate the blocksize in order
3060 * to reflect the filesystem size as best we can.
3062 if (sp
->f_blocks
> LONG_MAX
) {
3066 * Work out how far we have to shift the block count down to make it fit.
3067 * Note that it's possible to have to shift so far that the resulting
3068 * blocksize would be unreportably large. At that point, we will clip
3069 * any values that don't fit.
3071 * For safety's sake, we also ensure that f_iosize is never reported as
3072 * being smaller than f_bsize.
3074 for (shift
= 0; shift
< 32; shift
++) {
3075 if ((sp
->f_blocks
>> shift
) <= LONG_MAX
)
3077 if ((sp
->f_bsize
<< (shift
+ 1)) > LONG_MAX
)
3080 #define __SHIFT_OR_CLIP(x, s) ((((x) >> (s)) > LONG_MAX) ? LONG_MAX : ((x) >> (s)))
3081 sfs
.f_blocks
= (long)__SHIFT_OR_CLIP(sp
->f_blocks
, shift
);
3082 sfs
.f_bfree
= (long)__SHIFT_OR_CLIP(sp
->f_bfree
, shift
);
3083 sfs
.f_bavail
= (long)__SHIFT_OR_CLIP(sp
->f_bavail
, shift
);
3084 #undef __SHIFT_OR_CLIP
3085 sfs
.f_bsize
= (long)(sp
->f_bsize
<< shift
);
3086 sfs
.f_iosize
= lmax(sp
->f_iosize
, sp
->f_bsize
);
3088 sfs
.f_bsize
= (long)sp
->f_bsize
;
3089 sfs
.f_iosize
= (long)sp
->f_iosize
;
3090 sfs
.f_blocks
= (long)sp
->f_blocks
;
3091 sfs
.f_bfree
= (long)sp
->f_bfree
;
3092 sfs
.f_bavail
= (long)sp
->f_bavail
;
3094 sfs
.f_files
= (long)sp
->f_files
;
3095 sfs
.f_ffree
= (long)sp
->f_ffree
;
3096 sfs
.f_fsid
= sp
->f_fsid
;
3097 sfs
.f_owner
= sp
->f_owner
;
3099 strlcpy(sfs
.f_fstypename
, sp
->f_fstypename
, MFSNAMELEN
);
3100 strlcpy(sfs
.f_mntonname
, sp
->f_mntonname
, MNAMELEN
);
3101 strlcpy(sfs
.f_mntfromname
, sp
->f_mntfromname
, MNAMELEN
);
3103 error
= SYSCTL_OUT(req
, &sfs
, sizeof(sfs
));
3116 static int filt_fsattach(struct knote
*kn
);
3117 static void filt_fsdetach(struct knote
*kn
);
3118 static int filt_fsevent(struct knote
*kn
, long hint
);
3120 struct filterops fs_filtops
=
3121 { 0, filt_fsattach
, filt_fsdetach
, filt_fsevent
};
3124 filt_fsattach(struct knote
*kn
)
3127 kn
->kn_flags
|= EV_CLEAR
;
3128 KNOTE_ATTACH(&fs_klist
, kn
);
3133 filt_fsdetach(struct knote
*kn
)
3136 KNOTE_DETACH(&fs_klist
, kn
);
3140 filt_fsevent(struct knote
*kn
, long hint
)
3143 * Backwards compatibility:
3144 * Other filters would do nothing if kn->kn_sfflags == 0
3147 if ((kn
->kn_sfflags
== 0) || (kn
->kn_sfflags
& hint
)) {
3148 kn
->kn_fflags
|= hint
;
3151 return (kn
->kn_fflags
!= 0);
3155 sysctl_vfs_noremotehang(__unused
struct sysctl_oid
*oidp
,
3156 __unused
void *arg1
, __unused
int arg2
, struct sysctl_req
*req
)
3162 /* We need a pid. */
3163 if (req
->newptr
== USER_ADDR_NULL
)
3166 error
= SYSCTL_IN(req
, &pid
, sizeof(pid
));
3170 p
= proc_find(pid
< 0 ? -pid
: pid
);
3175 * Fetching the value is ok, but we only fetch if the old
3178 if (req
->oldptr
!= USER_ADDR_NULL
) {
3179 out
= !((p
->p_flag
& P_NOREMOTEHANG
) == 0);
3181 error
= SYSCTL_OUT(req
, &out
, sizeof(out
));
3185 /* cansignal offers us enough security. */
3186 if (p
!= req
->p
&& proc_suser(req
->p
) != 0) {
3192 OSBitAndAtomic(~((uint32_t)P_NOREMOTEHANG
), (UInt32
*)&p
->p_flag
);
3194 OSBitOrAtomic(P_NOREMOTEHANG
, (UInt32
*)&p
->p_flag
);
3200 /* the vfs.generic. branch. */
3201 SYSCTL_NODE(_vfs
, VFS_GENERIC
, generic
, CTLFLAG_RW
|CTLFLAG_LOCKED
, NULL
, "vfs generic hinge");
3202 /* retreive a list of mounted filesystem fsid_t */
3203 SYSCTL_PROC(_vfs_generic
, OID_AUTO
, vfsidlist
, CTLFLAG_RD
,
3204 NULL
, 0, sysctl_vfs_vfslist
, "S,fsid", "List of mounted filesystem ids");
3205 /* perform operations on filesystem via fsid_t */
3206 SYSCTL_NODE(_vfs_generic
, OID_AUTO
, ctlbyfsid
, CTLFLAG_RW
|CTLFLAG_LOCKED
,
3207 sysctl_vfs_ctlbyfsid
, "ctlbyfsid");
3208 SYSCTL_PROC(_vfs_generic
, OID_AUTO
, noremotehang
, CTLFLAG_RW
|CTLFLAG_ANYBODY
,
3209 NULL
, 0, sysctl_vfs_noremotehang
, "I", "noremotehang");
3212 long num_reusedvnodes
= 0; /* long for OSAddAtomic */
3215 new_vnode(vnode_t
*vpp
)
3218 int retries
= 0; /* retry incase of tablefull */
3219 int force_alloc
= 0, walk_count
= 0;
3222 struct timeval current_tv
;
3223 struct unsafe_fsnode
*l_unsafefs
= 0;
3224 proc_t curproc
= current_proc();
3225 pid_t current_pid
= proc_pid(curproc
);
3228 microuptime(¤t_tv
);
3234 if ( !TAILQ_EMPTY(&vnode_dead_list
)) {
3236 * Can always reuse a dead one
3238 vp
= TAILQ_FIRST(&vnode_dead_list
);
3242 * no dead vnodes available... if we're under
3243 * the limit, we'll create a new vnode
3245 if (numvnodes
< desiredvnodes
|| force_alloc
) {
3247 vnode_list_unlock();
3248 MALLOC_ZONE(vp
, struct vnode
*, sizeof(*vp
), M_VNODE
, M_WAITOK
);
3249 bzero((char *)vp
, sizeof(*vp
));
3250 VLISTNONE(vp
); /* avoid double queue removal */
3251 lck_mtx_init(&vp
->v_lock
, vnode_lck_grp
, vnode_lck_attr
);
3254 vp
->v_id
= ts
.tv_nsec
;
3255 vp
->v_flag
= VSTANDARD
;
3258 mac_vnode_label_init(vp
);
3264 #define MAX_WALK_COUNT 1000
3266 if ( !TAILQ_EMPTY(&vnode_rage_list
) &&
3267 (ragevnodes
>= rage_limit
||
3268 (current_tv
.tv_sec
- rage_tv
.tv_sec
) >= RAGE_TIME_LIMIT
)) {
3270 TAILQ_FOREACH(vp
, &vnode_rage_list
, v_freelist
) {
3271 if ( !(vp
->v_listflag
& VLIST_RAGE
) || !(vp
->v_flag
& VRAGE
))
3272 panic("new_vnode: vp on RAGE list not marked both VLIST_RAGE and VRAGE");
3274 // skip vnodes which have a dependency on this process
3275 // (i.e. they're vnodes in a disk image and this process
3276 // is diskimages-helper)
3278 if (vp
->v_mount
&& vp
->v_mount
->mnt_dependent_pid
!= current_pid
&& vp
->v_mount
->mnt_dependent_process
!= curproc
) {
3282 // don't iterate more than MAX_WALK_COUNT vnodes to
3283 // avoid keeping the vnode list lock held for too long.
3284 if (walk_count
++ > MAX_WALK_COUNT
) {
3292 if (vp
== NULL
&& !TAILQ_EMPTY(&vnode_free_list
)) {
3294 * Pick the first vp for possible reuse
3297 TAILQ_FOREACH(vp
, &vnode_free_list
, v_freelist
) {
3298 // skip vnodes which have a dependency on this process
3299 // (i.e. they're vnodes in a disk image and this process
3300 // is diskimages-helper)
3302 if (vp
->v_mount
&& vp
->v_mount
->mnt_dependent_pid
!= current_pid
&& vp
->v_mount
->mnt_dependent_process
!= curproc
) {
3306 // don't iterate more than MAX_WALK_COUNT vnodes to
3307 // avoid keeping the vnode list lock held for too long.
3308 if (walk_count
++ > MAX_WALK_COUNT
) {
3317 // if we don't have a vnode and the walk_count is >= MAX_WALK_COUNT
3318 // then we're trying to create a vnode on behalf of a
3319 // process like diskimages-helper that has file systems
3320 // mounted on top of itself (and thus we can't reclaim
3321 // vnodes in the file systems on top of us). if we can't
3322 // find a vnode to reclaim then we'll just have to force
3325 if (vp
== NULL
&& walk_count
>= MAX_WALK_COUNT
) {
3327 vnode_list_unlock();
3333 * we've reached the system imposed maximum number of vnodes
3334 * but there isn't a single one available
3335 * wait a bit and then retry... if we can't get a vnode
3336 * after 100 retries, than log a complaint
3338 if (++retries
<= 100) {
3339 vnode_list_unlock();
3340 delay_for_interval(1, 1000 * 1000);
3344 vnode_list_unlock();
3346 log(LOG_EMERG
, "%d desired, %d numvnodes, "
3347 "%d free, %d dead, %d rage\n",
3348 desiredvnodes
, numvnodes
, freevnodes
, deadvnodes
, ragevnodes
);
3356 * the v_listflag field is
3357 * protected by the vnode_list_lock
3359 if (vp
->v_listflag
& VLIST_DEAD
)
3360 VREMDEAD("new_vnode", vp
);
3361 else if (vp
->v_listflag
& VLIST_RAGE
)
3362 VREMRAGE("new_vnode", vp
);
3364 VREMFREE("new_vnode", vp
);
3367 vnode_list_unlock();
3368 vnode_lock_spin(vp
);
3371 * We could wait for the vnode_lock after removing the vp from the freelist
3372 * and the vid is bumped only at the very end of reclaim. So it is possible
3373 * that we are looking at a vnode that is being terminated. If so skip it.
3375 if ((vpid
!= vp
->v_id
) || (vp
->v_usecount
!= 0) || (vp
->v_iocount
!= 0) ||
3376 VONLIST(vp
) || (vp
->v_lflag
& VL_TERMINATE
)) {
3378 * we lost the race between dropping the list lock
3379 * and picking up the vnode_lock... someone else
3380 * used this vnode and it is now in a new state
3381 * so we need to go back and try again
3386 if ( (vp
->v_lflag
& (VL_NEEDINACTIVE
| VL_MARKTERM
)) == VL_NEEDINACTIVE
) {
3388 * we did a vnode_rele_ext that asked for
3389 * us not to reenter the filesystem during
3390 * the release even though VL_NEEDINACTIVE was
3391 * set... we'll do it here by doing a
3392 * vnode_get/vnode_put
3394 * pick up an iocount so that we can call
3395 * vnode_put and drive the VNOP_INACTIVE...
3396 * vnode_put will either leave us off
3397 * the freelist if a new ref comes in,
3398 * or put us back on the end of the freelist
3399 * or recycle us if we were marked for termination...
3400 * so we'll just go grab a new candidate
3406 vnode_put_locked(vp
);
3410 OSAddAtomic(1, &num_reusedvnodes
);
3412 /* Checks for anyone racing us for recycle */
3413 if (vp
->v_type
!= VBAD
) {
3414 if (vp
->v_lflag
& VL_DEAD
)
3415 panic("new_vnode: the vnode is VL_DEAD but not VBAD");
3416 vnode_lock_convert(vp
);
3418 (void)vnode_reclaim_internal(vp
, 1, 1, 0);
3421 panic("new_vnode: vp on list ");
3422 if (vp
->v_usecount
|| vp
->v_iocount
|| vp
->v_kusecount
||
3423 (vp
->v_lflag
& (VNAMED_UBC
| VNAMED_MOUNT
| VNAMED_FSHASH
)))
3424 panic("new_vnode: free vnode still referenced\n");
3425 if ((vp
->v_mntvnodes
.tqe_prev
!= 0) && (vp
->v_mntvnodes
.tqe_next
!= 0))
3426 panic("new_vnode: vnode seems to be on mount list ");
3427 if ( !LIST_EMPTY(&vp
->v_nclinks
) || !LIST_EMPTY(&vp
->v_ncchildren
))
3428 panic("new_vnode: vnode still hooked into the name cache");
3430 if (vp
->v_unsafefs
) {
3431 l_unsafefs
= vp
->v_unsafefs
;
3432 vp
->v_unsafefs
= (struct unsafe_fsnode
*)NULL
;
3437 * We should never see VL_LABELWAIT or VL_LABEL here.
3438 * as those operations hold a reference.
3440 assert ((vp
->v_lflag
& VL_LABELWAIT
) != VL_LABELWAIT
);
3441 assert ((vp
->v_lflag
& VL_LABEL
) != VL_LABEL
);
3442 if (vp
->v_lflag
& VL_LABELED
) {
3443 vnode_lock_convert(vp
);
3444 mac_vnode_label_recycle(vp
);
3449 vp
->v_writecount
= 0;
3450 vp
->v_references
= 0;
3451 vp
->v_iterblkflags
= 0;
3452 vp
->v_flag
= VSTANDARD
;
3453 /* vbad vnodes can point to dead_mountp */
3455 vp
->v_defer_reclaimlist
= (vnode_t
)0;
3460 lck_mtx_destroy(&l_unsafefs
->fsnodelock
, vnode_lck_grp
);
3461 FREE_ZONE((void *)l_unsafefs
, sizeof(struct unsafe_fsnode
), M_UNSAFEFS
);
3470 vnode_lock(vnode_t vp
)
3472 lck_mtx_lock(&vp
->v_lock
);
3476 vnode_lock_spin(vnode_t vp
)
3478 lck_mtx_lock_spin(&vp
->v_lock
);
3482 vnode_unlock(vnode_t vp
)
3484 lck_mtx_unlock(&vp
->v_lock
);
3490 vnode_get(struct vnode
*vp
)
3494 vnode_lock_spin(vp
);
3495 retval
= vnode_get_locked(vp
);
3502 vnode_get_locked(struct vnode
*vp
)
3505 if ((vp
->v_iocount
== 0) && (vp
->v_lflag
& (VL_TERMINATE
| VL_DEAD
))) {
3516 vnode_getwithvid(vnode_t vp
, int vid
)
3518 return(vget_internal(vp
, vid
, ( VNODE_NODEAD
| VNODE_WITHID
)));
3522 vnode_getwithref(vnode_t vp
)
3524 return(vget_internal(vp
, 0, 0));
3529 vnode_put(vnode_t vp
)
3533 vnode_lock_spin(vp
);
3534 retval
= vnode_put_locked(vp
);
3541 vnode_put_locked(vnode_t vp
)
3543 vfs_context_t ctx
= vfs_context_current(); /* hoist outside loop */
3546 if (vp
->v_iocount
< 1)
3547 panic("vnode_put(%p): iocount < 1", vp
);
3549 if ((vp
->v_usecount
> 0) || (vp
->v_iocount
> 1)) {
3550 vnode_dropiocount(vp
);
3553 if ((vp
->v_lflag
& (VL_MARKTERM
| VL_TERMINATE
| VL_DEAD
| VL_NEEDINACTIVE
)) == VL_NEEDINACTIVE
) {
3555 vp
->v_lflag
&= ~VL_NEEDINACTIVE
;
3558 VNOP_INACTIVE(vp
, ctx
);
3560 vnode_lock_spin(vp
);
3562 * because we had to drop the vnode lock before calling
3563 * VNOP_INACTIVE, the state of this vnode may have changed...
3564 * we may pick up both VL_MARTERM and either
3565 * an iocount or a usecount while in the VNOP_INACTIVE call
3566 * we don't want to call vnode_reclaim_internal on a vnode
3567 * that has active references on it... so loop back around
3568 * and reevaluate the state
3572 vp
->v_lflag
&= ~VL_NEEDINACTIVE
;
3574 if ((vp
->v_lflag
& (VL_MARKTERM
| VL_TERMINATE
| VL_DEAD
)) == VL_MARKTERM
) {
3575 vnode_lock_convert(vp
);
3576 vnode_reclaim_internal(vp
, 1, 0, 0);
3578 vnode_dropiocount(vp
);
3584 /* is vnode_t in use by others? */
3586 vnode_isinuse(vnode_t vp
, int refcnt
)
3588 return(vnode_isinuse_locked(vp
, refcnt
, 0));
3593 vnode_isinuse_locked(vnode_t vp
, int refcnt
, int locked
)
3598 vnode_lock_spin(vp
);
3599 if ((vp
->v_type
!= VREG
) && ((vp
->v_usecount
- vp
->v_kusecount
) > refcnt
)) {
3603 if (vp
->v_type
== VREG
) {
3604 retval
= ubc_isinuse_locked(vp
, refcnt
, 1);
3614 /* resume vnode_t */
3616 vnode_resume(vnode_t vp
)
3619 vnode_lock_spin(vp
);
3621 if (vp
->v_owner
== current_thread()) {
3622 vp
->v_lflag
&= ~VL_SUSPENDED
;
3625 wakeup(&vp
->v_iocount
);
3633 * Please do not use on more than one vnode at a time as it may
3635 * xxx should we explicity prevent this from happening?
3639 vnode_suspend(vnode_t vp
)
3641 if (vp
->v_lflag
& VL_SUSPENDED
) {
3645 vnode_lock_spin(vp
);
3648 * xxx is this sufficient to check if a vnode_drain is
3652 if (vp
->v_owner
== NULL
) {
3653 vp
->v_lflag
|= VL_SUSPENDED
;
3654 vp
->v_owner
= current_thread();
3664 vnode_drain(vnode_t vp
)
3667 if (vp
->v_lflag
& VL_DRAIN
) {
3668 panic("vnode_drain: recursuve drain");
3671 vp
->v_lflag
|= VL_DRAIN
;
3672 vp
->v_owner
= current_thread();
3674 while (vp
->v_iocount
> 1)
3675 msleep(&vp
->v_iocount
, &vp
->v_lock
, PVFS
, "vnode_drain", NULL
);
3681 * if the number of recent references via vnode_getwithvid or vnode_getwithref
3682 * exceeds this threshhold, than 'UN-AGE' the vnode by removing it from
3683 * the LRU list if it's currently on it... once the iocount and usecount both drop
3684 * to 0, it will get put back on the end of the list, effectively making it younger
3685 * this allows us to keep actively referenced vnodes in the list without having
3686 * to constantly remove and add to the list each time a vnode w/o a usecount is
3687 * referenced which costs us taking and dropping a global lock twice.
3689 #define UNAGE_THRESHHOLD 25
3692 vnode_getiocount(vnode_t vp
, int vid
, int vflags
)
3694 int nodead
= vflags
& VNODE_NODEAD
;
3695 int nosusp
= vflags
& VNODE_NOSUSPEND
;
3699 * if it is a dead vnode with deadfs
3701 if (nodead
&& (vp
->v_lflag
& VL_DEAD
) && ((vp
->v_type
== VBAD
) || (vp
->v_data
== 0))) {
3705 * will return VL_DEAD ones
3707 if ((vp
->v_lflag
& (VL_SUSPENDED
| VL_DRAIN
| VL_TERMINATE
)) == 0 ) {
3711 * if suspended vnodes are to be failed
3713 if (nosusp
&& (vp
->v_lflag
& VL_SUSPENDED
)) {
3717 * if you are the owner of drain/suspend/termination , can acquire iocount
3718 * check for VL_TERMINATE; it does not set owner
3720 if ((vp
->v_lflag
& (VL_DRAIN
| VL_SUSPENDED
| VL_TERMINATE
)) &&
3721 (vp
->v_owner
== current_thread())) {
3724 vnode_lock_convert(vp
);
3726 if (vp
->v_lflag
& VL_TERMINATE
) {
3727 vp
->v_lflag
|= VL_TERMWANT
;
3729 msleep(&vp
->v_lflag
, &vp
->v_lock
, PVFS
, "vnode getiocount", NULL
);
3731 msleep(&vp
->v_iocount
, &vp
->v_lock
, PVFS
, "vnode_getiocount", NULL
);
3733 if (vid
!= vp
->v_id
) {
3736 if (++vp
->v_references
>= UNAGE_THRESHHOLD
) {
3737 vp
->v_references
= 0;
3738 vnode_list_remove(vp
);
3748 vnode_dropiocount (vnode_t vp
)
3750 if (vp
->v_iocount
< 1)
3751 panic("vnode_dropiocount(%p): v_iocount < 1", vp
);
3757 if ((vp
->v_lflag
& (VL_DRAIN
| VL_SUSPENDED
)) && (vp
->v_iocount
<= 1)) {
3758 vnode_lock_convert(vp
);
3759 wakeup(&vp
->v_iocount
);
3765 vnode_reclaim(struct vnode
* vp
)
3767 vnode_reclaim_internal(vp
, 0, 0, 0);
3772 vnode_reclaim_internal(struct vnode
* vp
, int locked
, int reuse
, int flags
)
3779 if (vp
->v_lflag
& VL_TERMINATE
) {
3780 panic("vnode reclaim in progress");
3782 vp
->v_lflag
|= VL_TERMINATE
;
3784 vn_clearunionwait(vp
, 1);
3786 if (vnode_drain(vp
)) {
3787 panic("vnode drain failed");
3791 isfifo
= (vp
->v_type
== VFIFO
);
3793 if (vp
->v_type
!= VBAD
)
3794 vgone(vp
, flags
); /* clean and reclaim the vnode */
3797 * give the vnode a new identity so
3798 * that vnode_getwithvid will fail
3799 * on any stale cache accesses
3803 struct fifoinfo
* fip
;
3805 fip
= vp
->v_fifoinfo
;
3806 vp
->v_fifoinfo
= NULL
;
3813 panic("vnode_reclaim_internal: cleaned vnode isn't");
3814 if (vp
->v_numoutput
)
3815 panic("vnode_reclaim_internal: Clean vnode has pending I/O's");
3816 if (UBCINFOEXISTS(vp
))
3817 panic("vnode_reclaim_internal: ubcinfo not cleaned");
3819 panic("vnode_reclaim_internal: vparent not removed");
3821 panic("vnode_reclaim_internal: vname not removed");
3823 vp
->v_socket
= NULL
;
3825 vp
->v_lflag
&= ~VL_TERMINATE
;
3826 vp
->v_lflag
&= ~VL_DRAIN
;
3829 if (vp
->v_lflag
& VL_TERMWANT
) {
3830 vp
->v_lflag
&= ~VL_TERMWANT
;
3831 wakeup(&vp
->v_lflag
);
3833 if (!reuse
&& vp
->v_usecount
== 0) {
3835 * make sure we get on the
3838 vnode_list_remove(vp
);
3846 * The following api creates a vnode and associates all the parameter specified in vnode_fsparam
3847 * structure and returns a vnode handle with a reference. device aliasing is handled here so checkalias
3848 * is obsoleted by this.
3849 * vnode_create(int flavor, size_t size, void * param, vnode_t *vp)
3852 vnode_create(int flavor
, size_t size
, void *data
, vnode_t
*vpp
)
3860 struct componentname
*cnp
;
3861 struct vnode_fsparam
*param
= (struct vnode_fsparam
*)data
;
3863 if (flavor
== VNCREATE_FLAVOR
&& (size
== VCREATESIZE
) && param
) {
3864 if ( (error
= new_vnode(&vp
)) ) {
3867 dvp
= param
->vnfs_dvp
;
3868 cnp
= param
->vnfs_cnp
;
3870 vp
->v_op
= param
->vnfs_vops
;
3871 vp
->v_type
= param
->vnfs_vtype
;
3872 vp
->v_data
= param
->vnfs_fsnode
;
3875 if (param
->vnfs_markroot
)
3876 vp
->v_flag
|= VROOT
;
3877 if (param
->vnfs_marksystem
)
3878 vp
->v_flag
|= VSYSTEM
;
3879 if (vp
->v_type
== VREG
) {
3880 error
= ubc_info_init_withsize(vp
, param
->vnfs_filesize
);
3886 vp
->v_op
= dead_vnodeop_p
;
3890 vp
->v_lflag
|= VL_DEAD
;
3899 if (vp
->v_type
== VCHR
|| vp
->v_type
== VBLK
) {
3901 vp
->v_tag
= VT_DEVFS
; /* callers will reset if needed (bdevvp) */
3903 if ( (nvp
= checkalias(vp
, param
->vnfs_rdev
)) ) {
3905 * if checkalias returns a vnode, it will be locked
3907 * first get rid of the unneeded vnode we acquired
3910 vp
->v_op
= spec_vnodeop_p
;
3912 vp
->v_lflag
= VL_DEAD
;
3918 * switch to aliased vnode and finish
3924 vp
->v_op
= param
->vnfs_vops
;
3925 vp
->v_type
= param
->vnfs_vtype
;
3926 vp
->v_data
= param
->vnfs_fsnode
;
3929 insmntque(vp
, param
->vnfs_mp
);
3935 if (vp
->v_type
== VFIFO
) {
3936 struct fifoinfo
*fip
;
3938 MALLOC(fip
, struct fifoinfo
*,
3939 sizeof(*fip
), M_TEMP
, M_WAITOK
);
3940 bzero(fip
, sizeof(struct fifoinfo
));
3941 vp
->v_fifoinfo
= fip
;
3943 /* The file systems usually pass the address of the location where
3944 * where there store the vnode pointer. When we add the vnode in mount
3945 * point and name cache they are discoverable. So the file system node
3946 * will have the connection to vnode setup by then
3950 /* Add fs named reference. */
3951 if (param
->vnfs_flags
& VNFS_ADDFSREF
) {
3952 vp
->v_lflag
|= VNAMED_FSHASH
;
3954 if (param
->vnfs_mp
) {
3955 if (param
->vnfs_mp
->mnt_kern_flag
& MNTK_LOCK_LOCAL
)
3956 vp
->v_flag
|= VLOCKLOCAL
;
3959 * enter in mount vnode list
3961 insmntque(vp
, param
->vnfs_mp
);
3963 #ifdef INTERIM_FSNODE_LOCK
3964 if (param
->vnfs_mp
->mnt_vtable
->vfc_threadsafe
== 0) {
3965 MALLOC_ZONE(vp
->v_unsafefs
, struct unsafe_fsnode
*,
3966 sizeof(struct unsafe_fsnode
), M_UNSAFEFS
, M_WAITOK
);
3967 vp
->v_unsafefs
->fsnode_count
= 0;
3968 vp
->v_unsafefs
->fsnodeowner
= (void *)NULL
;
3969 lck_mtx_init(&vp
->v_unsafefs
->fsnodelock
, vnode_lck_grp
, vnode_lck_attr
);
3971 #endif /* INTERIM_FSNODE_LOCK */
3973 if (dvp
&& vnode_ref(dvp
) == 0) {
3977 if (dvp
&& ((param
->vnfs_flags
& (VNFS_NOCACHE
| VNFS_CANTCACHE
)) == 0)) {
3979 * enter into name cache
3980 * we've got the info to enter it into the name cache now
3982 cache_enter(dvp
, vp
, cnp
);
3984 vp
->v_name
= vfs_addname(cnp
->cn_nameptr
, cnp
->cn_namelen
, cnp
->cn_hash
, 0);
3985 if ((cnp
->cn_flags
& UNIONCREATED
) == UNIONCREATED
)
3986 vp
->v_flag
|= VISUNION
;
3988 if ((param
->vnfs_flags
& VNFS_CANTCACHE
) == 0) {
3990 * this vnode is being created as cacheable in the name cache
3991 * this allows us to re-enter it in the cache
3993 vp
->v_flag
|= VNCACHEABLE
;
3995 ut
= get_bsdthread_info(current_thread());
3997 if ((current_proc()->p_lflag
& P_LRAGE_VNODES
) ||
3998 (ut
->uu_flag
& UT_RAGE_VNODES
)) {
4000 * process has indicated that it wants any
4001 * vnodes created on its behalf to be rapidly
4002 * aged to reduce the impact on the cached set
4005 vp
->v_flag
|= VRAGE
;
4014 vnode_addfsref(vnode_t vp
)
4016 vnode_lock_spin(vp
);
4017 if (vp
->v_lflag
& VNAMED_FSHASH
)
4018 panic("add_fsref: vp already has named reference");
4019 if ((vp
->v_freelist
.tqe_prev
!= (struct vnode
**)0xdeadb))
4020 panic("addfsref: vp on the free list\n");
4021 vp
->v_lflag
|= VNAMED_FSHASH
;
4027 vnode_removefsref(vnode_t vp
)
4029 vnode_lock_spin(vp
);
4030 if ((vp
->v_lflag
& VNAMED_FSHASH
) == 0)
4031 panic("remove_fsref: no named reference");
4032 vp
->v_lflag
&= ~VNAMED_FSHASH
;
4040 vfs_iterate(__unused
int flags
, int (*callout
)(mount_t
, void *), void *arg
)
4045 int count
, actualcount
, i
;
4048 count
= mount_getvfscnt();
4051 fsid_list
= (fsid_t
*)kalloc(count
* sizeof(fsid_t
));
4052 allocmem
= (void *)fsid_list
;
4054 actualcount
= mount_fillfsids(fsid_list
, count
);
4056 for (i
=0; i
< actualcount
; i
++) {
4058 /* obtain the mount point with iteration reference */
4059 mp
= mount_list_lookupby_fsid(&fsid_list
[i
], 0, 1);
4061 if(mp
== (struct mount
*)0)
4064 if (mp
->mnt_lflag
& (MNT_LDEAD
| MNT_LUNMOUNT
)) {
4072 /* iterate over all the vnodes */
4073 ret
= callout(mp
, arg
);
4079 case VFS_RETURNED_DONE
:
4080 if (ret
== VFS_RETURNED_DONE
) {
4086 case VFS_CLAIMED_DONE
:
4097 kfree(allocmem
, (count
* sizeof(fsid_t
)));
4102 * Update the vfsstatfs structure in the mountpoint.
4103 * MAC: Parameter eventtype added, indicating whether the event that
4104 * triggered this update came from user space, via a system call
4105 * (VFS_USER_EVENT) or an internal kernel call (VFS_KERNEL_EVENT).
4108 vfs_update_vfsstat(mount_t mp
, vfs_context_t ctx
, __unused
int eventtype
)
4114 * Request the attributes we want to propagate into
4115 * the per-mount vfsstat structure.
4118 VFSATTR_WANTED(&va
, f_iosize
);
4119 VFSATTR_WANTED(&va
, f_blocks
);
4120 VFSATTR_WANTED(&va
, f_bfree
);
4121 VFSATTR_WANTED(&va
, f_bavail
);
4122 VFSATTR_WANTED(&va
, f_bused
);
4123 VFSATTR_WANTED(&va
, f_files
);
4124 VFSATTR_WANTED(&va
, f_ffree
);
4125 VFSATTR_WANTED(&va
, f_bsize
);
4126 VFSATTR_WANTED(&va
, f_fssubtype
);
4128 if (eventtype
== VFS_USER_EVENT
) {
4129 error
= mac_mount_check_getattr(ctx
, mp
, &va
);
4135 if ((error
= vfs_getattr(mp
, &va
, ctx
)) != 0) {
4136 KAUTH_DEBUG("STAT - filesystem returned error %d", error
);
4141 * Unpack into the per-mount structure.
4143 * We only overwrite these fields, which are likely to change:
4151 * And these which are not, but which the FS has no other way
4152 * of providing to us:
4158 if (VFSATTR_IS_SUPPORTED(&va
, f_bsize
)) {
4159 /* 4822056 - protect against malformed server mount */
4160 mp
->mnt_vfsstat
.f_bsize
= (va
.f_bsize
> 0 ? va
.f_bsize
: 512);
4162 mp
->mnt_vfsstat
.f_bsize
= mp
->mnt_devblocksize
; /* default from the device block size */
4164 if (VFSATTR_IS_SUPPORTED(&va
, f_iosize
)) {
4165 mp
->mnt_vfsstat
.f_iosize
= va
.f_iosize
;
4167 mp
->mnt_vfsstat
.f_iosize
= 1024 * 1024; /* 1MB sensible I/O size */
4169 if (VFSATTR_IS_SUPPORTED(&va
, f_blocks
))
4170 mp
->mnt_vfsstat
.f_blocks
= va
.f_blocks
;
4171 if (VFSATTR_IS_SUPPORTED(&va
, f_bfree
))
4172 mp
->mnt_vfsstat
.f_bfree
= va
.f_bfree
;
4173 if (VFSATTR_IS_SUPPORTED(&va
, f_bavail
))
4174 mp
->mnt_vfsstat
.f_bavail
= va
.f_bavail
;
4175 if (VFSATTR_IS_SUPPORTED(&va
, f_bused
))
4176 mp
->mnt_vfsstat
.f_bused
= va
.f_bused
;
4177 if (VFSATTR_IS_SUPPORTED(&va
, f_files
))
4178 mp
->mnt_vfsstat
.f_files
= va
.f_files
;
4179 if (VFSATTR_IS_SUPPORTED(&va
, f_ffree
))
4180 mp
->mnt_vfsstat
.f_ffree
= va
.f_ffree
;
4182 /* this is unlikely to change, but has to be queried for */
4183 if (VFSATTR_IS_SUPPORTED(&va
, f_fssubtype
))
4184 mp
->mnt_vfsstat
.f_fssubtype
= va
.f_fssubtype
;
4190 mount_list_add(mount_t mp
)
4193 TAILQ_INSERT_TAIL(&mountlist
, mp
, mnt_list
);
4195 mount_list_unlock();
4199 mount_list_remove(mount_t mp
)
4202 TAILQ_REMOVE(&mountlist
, mp
, mnt_list
);
4204 mp
->mnt_list
.tqe_next
= NULL
;
4205 mp
->mnt_list
.tqe_prev
= NULL
;
4206 mount_list_unlock();
4211 mount_lookupby_volfsid(int volfs_id
, int withref
)
4213 mount_t cur_mount
= (mount_t
)0;
4217 TAILQ_FOREACH(mp
, &mountlist
, mnt_list
) {
4218 if (!(mp
->mnt_kern_flag
& MNTK_UNMOUNT
) &&
4219 (mp
->mnt_kern_flag
& MNTK_PATH_FROM_ID
) &&
4220 (mp
->mnt_vfsstat
.f_fsid
.val
[0] == volfs_id
)) {
4223 if (mount_iterref(cur_mount
, 1)) {
4224 cur_mount
= (mount_t
)0;
4225 mount_list_unlock();
4232 mount_list_unlock();
4233 if (withref
&& (cur_mount
!= (mount_t
)0)) {
4235 if (vfs_busy(mp
, LK_NOWAIT
) != 0) {
4236 cur_mount
= (mount_t
)0;
4247 mount_list_lookupby_fsid(fsid_t
*fsid
, int locked
, int withref
)
4249 mount_t retmp
= (mount_t
)0;
4254 TAILQ_FOREACH(mp
, &mountlist
, mnt_list
)
4255 if (mp
->mnt_vfsstat
.f_fsid
.val
[0] == fsid
->val
[0] &&
4256 mp
->mnt_vfsstat
.f_fsid
.val
[1] == fsid
->val
[1]) {
4259 if (mount_iterref(retmp
, 1))
4266 mount_list_unlock();
4271 vnode_lookup(const char *path
, int flags
, vnode_t
*vpp
, vfs_context_t ctx
)
4273 struct nameidata nd
;
4277 if (ctx
== NULL
) { /* XXX technically an error */
4278 ctx
= vfs_context_current();
4281 if (flags
& VNODE_LOOKUP_NOFOLLOW
)
4286 if (flags
& VNODE_LOOKUP_NOCROSSMOUNT
)
4287 ndflags
|= NOCROSSMOUNT
;
4288 if (flags
& VNODE_LOOKUP_DOWHITEOUT
)
4289 ndflags
|= DOWHITEOUT
;
4291 /* XXX AUDITVNPATH1 needed ? */
4292 NDINIT(&nd
, LOOKUP
, ndflags
, UIO_SYSSPACE
, CAST_USER_ADDR_T(path
), ctx
);
4294 if ((error
= namei(&nd
)))
4303 vnode_open(const char *path
, int fmode
, int cmode
, int flags
, vnode_t
*vpp
, vfs_context_t ctx
)
4305 struct nameidata nd
;
4310 if (ctx
== NULL
) { /* XXX technically an error */
4311 ctx
= vfs_context_current();
4314 if (fmode
& O_NOFOLLOW
)
4315 lflags
|= VNODE_LOOKUP_NOFOLLOW
;
4317 if (lflags
& VNODE_LOOKUP_NOFOLLOW
)
4322 if (lflags
& VNODE_LOOKUP_NOCROSSMOUNT
)
4323 ndflags
|= NOCROSSMOUNT
;
4324 if (lflags
& VNODE_LOOKUP_DOWHITEOUT
)
4325 ndflags
|= DOWHITEOUT
;
4327 /* XXX AUDITVNPATH1 needed ? */
4328 NDINIT(&nd
, LOOKUP
, ndflags
, UIO_SYSSPACE
, CAST_USER_ADDR_T(path
), ctx
);
4330 if ((error
= vn_open(&nd
, fmode
, cmode
)))
4339 vnode_close(vnode_t vp
, int flags
, vfs_context_t ctx
)
4344 ctx
= vfs_context_current();
4347 error
= vn_close(vp
, flags
, ctx
);
4353 * Returns: 0 Success
4357 vnode_size(vnode_t vp
, off_t
*sizep
, vfs_context_t ctx
)
4359 struct vnode_attr va
;
4363 VATTR_WANTED(&va
, va_data_size
);
4364 error
= vnode_getattr(vp
, &va
, ctx
);
4366 *sizep
= va
.va_data_size
;
4371 vnode_setsize(vnode_t vp
, off_t size
, int ioflag
, vfs_context_t ctx
)
4373 struct vnode_attr va
;
4376 VATTR_SET(&va
, va_data_size
, size
);
4377 va
.va_vaflags
= ioflag
& 0xffff;
4378 return(vnode_setattr(vp
, &va
, ctx
));
4382 * Create a filesystem object of arbitrary type with arbitrary attributes in
4383 * the spevied directory with the specified name.
4385 * Parameters: dvp Pointer to the vnode of the directory
4386 * in which to create the object.
4387 * vpp Pointer to the area into which to
4388 * return the vnode of the created object.
4389 * cnp Component name pointer from the namei
4390 * data structure, containing the name to
4391 * use for the create object.
4392 * vap Pointer to the vnode_attr structure
4393 * describing the object to be created,
4394 * including the type of object.
4395 * flags VN_* flags controlling ACL inheritance
4396 * and whether or not authorization is to
4397 * be required for the operation.
4399 * Returns: 0 Success
4402 * Implicit: *vpp Contains the vnode of the object that
4403 * was created, if successful.
4404 * *cnp May be modified by the underlying VFS.
4405 * *vap May be modified by the underlying VFS.
4406 * modified by either ACL inheritance or
4409 * be modified, even if the operation is
4412 * Notes: The kauth_filesec_t in 'vap', if any, is in host byte order.
4414 * Modification of '*cnp' and '*vap' by the underlying VFS is
4415 * strongly discouraged.
4417 * XXX: This function is a 'vn_*' function; it belongs in vfs_vnops.c
4419 * XXX: We should enummerate the possible errno values here, and where
4420 * in the code they originated.
4423 vn_create(vnode_t dvp
, vnode_t
*vpp
, struct componentname
*cnp
, struct vnode_attr
*vap
, int flags
, vfs_context_t ctx
)
4425 kauth_acl_t oacl
, nacl
;
4428 vnode_t vp
= (vnode_t
)0;
4434 KAUTH_DEBUG("%p CREATE - '%s'", dvp
, cnp
->cn_nameptr
);
4437 * Handle ACL inheritance.
4439 if (!(flags
& VN_CREATE_NOINHERIT
) && vfs_extendedsecurity(dvp
->v_mount
)) {
4440 /* save the original filesec */
4441 if (VATTR_IS_ACTIVE(vap
, va_acl
)) {
4447 if ((error
= kauth_acl_inherit(dvp
,
4450 vap
->va_type
== VDIR
,
4452 KAUTH_DEBUG("%p CREATE - error %d processing inheritance", dvp
, error
);
4457 * If the generated ACL is NULL, then we can save ourselves some effort
4458 * by clearing the active bit.
4461 VATTR_CLEAR_ACTIVE(vap
, va_acl
);
4463 VATTR_SET(vap
, va_acl
, nacl
);
4468 * Check and default new attributes.
4469 * This will set va_uid, va_gid, va_mode and va_create_time at least, if the caller
4470 * hasn't supplied them.
4472 if ((error
= vnode_authattr_new(dvp
, vap
, flags
& VN_CREATE_NOAUTH
, ctx
)) != 0) {
4473 KAUTH_DEBUG("%p CREATE - error %d handing/defaulting attributes", dvp
, error
);
4479 * Create the requested node.
4481 switch(vap
->va_type
) {
4483 error
= VNOP_CREATE(dvp
, vpp
, cnp
, vap
, ctx
);
4486 error
= VNOP_MKDIR(dvp
, vpp
, cnp
, vap
, ctx
);
4492 error
= VNOP_MKNOD(dvp
, vpp
, cnp
, vap
, ctx
);
4495 panic("vnode_create: unknown vtype %d", vap
->va_type
);
4498 KAUTH_DEBUG("%p CREATE - error %d returned by filesystem", dvp
, error
);
4504 if (!(flags
& VN_CREATE_NOLABEL
)) {
4505 error
= vnode_label(vnode_mount(vp
), dvp
, vp
, cnp
,
4506 VNODE_LABEL_CREATE
|VNODE_LABEL_NEEDREF
, ctx
);
4513 * If some of the requested attributes weren't handled by the VNOP,
4514 * use our fallback code.
4516 if (!VATTR_ALL_SUPPORTED(vap
) && *vpp
) {
4517 KAUTH_DEBUG(" CREATE - doing fallback with ACL %p", vap
->va_acl
);
4518 error
= vnode_setattr_fallback(*vpp
, vap
, ctx
);
4523 if ((error
!= 0 ) && (vp
!= (vnode_t
)0)) {
4530 * If the caller supplied a filesec in vap, it has been replaced
4531 * now by the post-inheritance copy. We need to put the original back
4532 * and free the inherited product.
4535 VATTR_SET(vap
, va_acl
, oacl
);
4537 VATTR_CLEAR_ACTIVE(vap
, va_acl
);
4540 kauth_acl_free(nacl
);
4545 static kauth_scope_t vnode_scope
;
4546 static int vnode_authorize_callback(kauth_cred_t credential
, void *idata
, kauth_action_t action
,
4547 uintptr_t arg0
, uintptr_t arg1
, uintptr_t arg2
, uintptr_t arg3
);
4548 static int vnode_authorize_callback_int(__unused kauth_cred_t credential
, __unused
void *idata
, kauth_action_t action
,
4549 uintptr_t arg0
, uintptr_t arg1
, uintptr_t arg2
, uintptr_t arg3
);
4551 typedef struct _vnode_authorize_context
{
4553 struct vnode_attr
*vap
;
4555 struct vnode_attr
*dvap
;
4559 #define _VAC_IS_OWNER (1<<0)
4560 #define _VAC_IN_GROUP (1<<1)
4561 #define _VAC_IS_DIR_OWNER (1<<2)
4562 #define _VAC_IN_DIR_GROUP (1<<3)
4566 vnode_authorize_init(void)
4568 vnode_scope
= kauth_register_scope(KAUTH_SCOPE_VNODE
, vnode_authorize_callback
, NULL
);
4572 * Authorize an operation on a vnode.
4574 * This is KPI, but here because it needs vnode_scope.
4576 * Returns: 0 Success
4577 * kauth_authorize_action:EPERM ...
4578 * xlate => EACCES Permission denied
4579 * kauth_authorize_action:0 Success
4580 * kauth_authorize_action: Depends on callback return; this is
4581 * usually only vnode_authorize_callback(),
4582 * but may include other listerners, if any
4590 vnode_authorize(vnode_t vp
, vnode_t dvp
, kauth_action_t action
, vfs_context_t ctx
)
4595 * We can't authorize against a dead vnode; allow all operations through so that
4596 * the correct error can be returned.
4598 if (vp
->v_type
== VBAD
)
4602 result
= kauth_authorize_action(vnode_scope
, vfs_context_ucred(ctx
), action
,
4603 (uintptr_t)ctx
, (uintptr_t)vp
, (uintptr_t)dvp
, (uintptr_t)&error
);
4604 if (result
== EPERM
) /* traditional behaviour */
4606 /* did the lower layers give a better error return? */
4607 if ((result
!= 0) && (error
!= 0))
4613 * Test for vnode immutability.
4615 * The 'append' flag is set when the authorization request is constrained
4616 * to operations which only request the right to append to a file.
4618 * The 'ignore' flag is set when an operation modifying the immutability flags
4619 * is being authorized. We check the system securelevel to determine which
4620 * immutability flags we can ignore.
4623 vnode_immutable(struct vnode_attr
*vap
, int append
, int ignore
)
4627 /* start with all bits precluding the operation */
4628 mask
= IMMUTABLE
| APPEND
;
4630 /* if appending only, remove the append-only bits */
4634 /* ignore only set when authorizing flags changes */
4636 if (securelevel
<= 0) {
4637 /* in insecure state, flags do not inhibit changes */
4640 /* in secure state, user flags don't inhibit */
4641 mask
&= ~(UF_IMMUTABLE
| UF_APPEND
);
4644 KAUTH_DEBUG("IMMUTABLE - file flags 0x%x mask 0x%x append = %d ignore = %d", vap
->va_flags
, mask
, append
, ignore
);
4645 if ((vap
->va_flags
& mask
) != 0)
4651 vauth_node_owner(struct vnode_attr
*vap
, kauth_cred_t cred
)
4655 /* default assumption is not-owner */
4659 * If the filesystem has given us a UID, we treat this as authoritative.
4661 if (vap
&& VATTR_IS_SUPPORTED(vap
, va_uid
)) {
4662 result
= (vap
->va_uid
== kauth_cred_getuid(cred
)) ? 1 : 0;
4664 /* we could test the owner UUID here if we had a policy for it */
4670 vauth_node_group(struct vnode_attr
*vap
, kauth_cred_t cred
, int *ismember
)
4678 /* the caller is expected to have asked the filesystem for a group at some point */
4679 if (vap
&& VATTR_IS_SUPPORTED(vap
, va_gid
)) {
4680 error
= kauth_cred_ismember_gid(cred
, vap
->va_gid
, &result
);
4682 /* we could test the group UUID here if we had a policy for it */
4690 vauth_file_owner(vauth_ctx vcp
)
4694 if (vcp
->flags_valid
& _VAC_IS_OWNER
) {
4695 result
= (vcp
->flags
& _VAC_IS_OWNER
) ? 1 : 0;
4697 result
= vauth_node_owner(vcp
->vap
, vcp
->ctx
->vc_ucred
);
4699 /* cache our result */
4700 vcp
->flags_valid
|= _VAC_IS_OWNER
;
4702 vcp
->flags
|= _VAC_IS_OWNER
;
4704 vcp
->flags
&= ~_VAC_IS_OWNER
;
4711 vauth_file_ingroup(vauth_ctx vcp
, int *ismember
)
4715 if (vcp
->flags_valid
& _VAC_IN_GROUP
) {
4716 *ismember
= (vcp
->flags
& _VAC_IN_GROUP
) ? 1 : 0;
4719 error
= vauth_node_group(vcp
->vap
, vcp
->ctx
->vc_ucred
, ismember
);
4722 /* cache our result */
4723 vcp
->flags_valid
|= _VAC_IN_GROUP
;
4725 vcp
->flags
|= _VAC_IN_GROUP
;
4727 vcp
->flags
&= ~_VAC_IN_GROUP
;
4736 vauth_dir_owner(vauth_ctx vcp
)
4740 if (vcp
->flags_valid
& _VAC_IS_DIR_OWNER
) {
4741 result
= (vcp
->flags
& _VAC_IS_DIR_OWNER
) ? 1 : 0;
4743 result
= vauth_node_owner(vcp
->dvap
, vcp
->ctx
->vc_ucred
);
4745 /* cache our result */
4746 vcp
->flags_valid
|= _VAC_IS_DIR_OWNER
;
4748 vcp
->flags
|= _VAC_IS_DIR_OWNER
;
4750 vcp
->flags
&= ~_VAC_IS_DIR_OWNER
;
4757 vauth_dir_ingroup(vauth_ctx vcp
, int *ismember
)
4761 if (vcp
->flags_valid
& _VAC_IN_DIR_GROUP
) {
4762 *ismember
= (vcp
->flags
& _VAC_IN_DIR_GROUP
) ? 1 : 0;
4765 error
= vauth_node_group(vcp
->dvap
, vcp
->ctx
->vc_ucred
, ismember
);
4768 /* cache our result */
4769 vcp
->flags_valid
|= _VAC_IN_DIR_GROUP
;
4771 vcp
->flags
|= _VAC_IN_DIR_GROUP
;
4773 vcp
->flags
&= ~_VAC_IN_DIR_GROUP
;
4781 * Test the posix permissions in (vap) to determine whether (credential)
4782 * may perform (action)
4785 vnode_authorize_posix(vauth_ctx vcp
, int action
, int on_dir
)
4787 struct vnode_attr
*vap
;
4788 int needed
, error
, owner_ok
, group_ok
, world_ok
, ismember
;
4789 #ifdef KAUTH_DEBUG_ENABLE
4790 const char *where
= "uninitialized";
4791 # define _SETWHERE(c) where = c;
4793 # define _SETWHERE(c)
4796 /* checking file or directory? */
4806 * We want to do as little work here as possible. So first we check
4807 * which sets of permissions grant us the access we need, and avoid checking
4808 * whether specific permissions grant access when more generic ones would.
4811 /* owner permissions */
4815 if (action
& VWRITE
)
4819 owner_ok
= (needed
& vap
->va_mode
) == needed
;
4821 /* group permissions */
4825 if (action
& VWRITE
)
4829 group_ok
= (needed
& vap
->va_mode
) == needed
;
4831 /* world permissions */
4835 if (action
& VWRITE
)
4839 world_ok
= (needed
& vap
->va_mode
) == needed
;
4841 /* If granted/denied by all three, we're done */
4842 if (owner_ok
&& group_ok
&& world_ok
) {
4846 if (!owner_ok
&& !group_ok
&& !world_ok
) {
4852 /* Check ownership (relatively cheap) */
4853 if ((on_dir
&& vauth_dir_owner(vcp
)) ||
4854 (!on_dir
&& vauth_file_owner(vcp
))) {
4861 /* Not owner; if group and world both grant it we're done */
4862 if (group_ok
&& world_ok
) {
4863 _SETWHERE("group/world");
4866 if (!group_ok
&& !world_ok
) {
4867 _SETWHERE("group/world");
4872 /* Check group membership (most expensive) */
4875 error
= vauth_dir_ingroup(vcp
, &ismember
);
4877 error
= vauth_file_ingroup(vcp
, &ismember
);
4888 /* Not owner, not in group, use world result */
4896 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",
4897 vcp
->vp
, (error
== 0) ? "ALLOWED" : "DENIED", where
,
4898 (action
& VREAD
) ? "r" : "-",
4899 (action
& VWRITE
) ? "w" : "-",
4900 (action
& VEXEC
) ? "x" : "-",
4902 (vap
->va_mode
& S_IRUSR
) ? "r" : "-",
4903 (vap
->va_mode
& S_IWUSR
) ? "w" : "-",
4904 (vap
->va_mode
& S_IXUSR
) ? "x" : "-",
4905 (vap
->va_mode
& S_IRGRP
) ? "r" : "-",
4906 (vap
->va_mode
& S_IWGRP
) ? "w" : "-",
4907 (vap
->va_mode
& S_IXGRP
) ? "x" : "-",
4908 (vap
->va_mode
& S_IROTH
) ? "r" : "-",
4909 (vap
->va_mode
& S_IWOTH
) ? "w" : "-",
4910 (vap
->va_mode
& S_IXOTH
) ? "x" : "-",
4911 kauth_cred_getuid(vcp
->ctx
->vc_ucred
),
4912 on_dir
? vcp
->dvap
->va_uid
: vcp
->vap
->va_uid
,
4913 on_dir
? vcp
->dvap
->va_gid
: vcp
->vap
->va_gid
);
4918 * Authorize the deletion of the node vp from the directory dvp.
4921 * - Neither the node nor the directory are immutable.
4922 * - The user is not the superuser.
4924 * Deletion is not permitted if the directory is sticky and the caller is not owner of the
4925 * node or directory.
4927 * If either the node grants DELETE, or the directory grants DELETE_CHILD, the node may be
4928 * deleted. If neither denies the permission, and the caller has Posix write access to the
4929 * directory, then the node may be deleted.
4932 vnode_authorize_delete(vauth_ctx vcp
)
4934 struct vnode_attr
*vap
= vcp
->vap
;
4935 struct vnode_attr
*dvap
= vcp
->dvap
;
4936 kauth_cred_t cred
= vcp
->ctx
->vc_ucred
;
4937 struct kauth_acl_eval eval
;
4938 int error
, delete_denied
, delete_child_denied
, ismember
;
4940 /* check the ACL on the directory */
4941 delete_child_denied
= 0;
4942 if (VATTR_IS_NOT(dvap
, va_acl
, NULL
)) {
4943 eval
.ae_requested
= KAUTH_VNODE_DELETE_CHILD
;
4944 eval
.ae_acl
= &dvap
->va_acl
->acl_ace
[0];
4945 eval
.ae_count
= dvap
->va_acl
->acl_entrycount
;
4946 eval
.ae_options
= 0;
4947 if (vauth_dir_owner(vcp
))
4948 eval
.ae_options
|= KAUTH_AEVAL_IS_OWNER
;
4949 if ((error
= vauth_dir_ingroup(vcp
, &ismember
)) != 0)
4952 eval
.ae_options
|= KAUTH_AEVAL_IN_GROUP
;
4953 eval
.ae_exp_gall
= KAUTH_VNODE_GENERIC_ALL_BITS
;
4954 eval
.ae_exp_gread
= KAUTH_VNODE_GENERIC_READ_BITS
;
4955 eval
.ae_exp_gwrite
= KAUTH_VNODE_GENERIC_WRITE_BITS
;
4956 eval
.ae_exp_gexec
= KAUTH_VNODE_GENERIC_EXECUTE_BITS
;
4958 error
= kauth_acl_evaluate(cred
, &eval
);
4961 KAUTH_DEBUG("%p ERROR during ACL processing - %d", vcp
->vp
, error
);
4964 if (eval
.ae_result
== KAUTH_RESULT_DENY
)
4965 delete_child_denied
= 1;
4966 if (eval
.ae_result
== KAUTH_RESULT_ALLOW
) {
4967 KAUTH_DEBUG("%p ALLOWED - granted by directory ACL", vcp
->vp
);
4972 /* check the ACL on the node */
4974 if (VATTR_IS_NOT(vap
, va_acl
, NULL
)) {
4975 eval
.ae_requested
= KAUTH_VNODE_DELETE
;
4976 eval
.ae_acl
= &vap
->va_acl
->acl_ace
[0];
4977 eval
.ae_count
= vap
->va_acl
->acl_entrycount
;
4978 eval
.ae_options
= 0;
4979 if (vauth_file_owner(vcp
))
4980 eval
.ae_options
|= KAUTH_AEVAL_IS_OWNER
;
4981 if ((error
= vauth_file_ingroup(vcp
, &ismember
)) != 0)
4984 eval
.ae_options
|= KAUTH_AEVAL_IN_GROUP
;
4985 eval
.ae_exp_gall
= KAUTH_VNODE_GENERIC_ALL_BITS
;
4986 eval
.ae_exp_gread
= KAUTH_VNODE_GENERIC_READ_BITS
;
4987 eval
.ae_exp_gwrite
= KAUTH_VNODE_GENERIC_WRITE_BITS
;
4988 eval
.ae_exp_gexec
= KAUTH_VNODE_GENERIC_EXECUTE_BITS
;
4990 if ((error
= kauth_acl_evaluate(cred
, &eval
)) != 0) {
4991 KAUTH_DEBUG("%p ERROR during ACL processing - %d", vcp
->vp
, error
);
4994 if (eval
.ae_result
== KAUTH_RESULT_DENY
)
4996 if (eval
.ae_result
== KAUTH_RESULT_ALLOW
) {
4997 KAUTH_DEBUG("%p ALLOWED - granted by file ACL", vcp
->vp
);
5002 /* if denied by ACL on directory or node, return denial */
5003 if (delete_denied
|| delete_child_denied
) {
5004 KAUTH_DEBUG("%p ALLOWED - denied by ACL", vcp
->vp
);
5008 /* enforce sticky bit behaviour */
5009 if ((dvap
->va_mode
& S_ISTXT
) && !vauth_file_owner(vcp
) && !vauth_dir_owner(vcp
)) {
5010 KAUTH_DEBUG("%p DENIED - sticky bit rules (user %d file %d dir %d)",
5011 vcp
->vp
, cred
->cr_uid
, vap
->va_uid
, dvap
->va_uid
);
5015 /* check the directory */
5016 if ((error
= vnode_authorize_posix(vcp
, VWRITE
, 1 /* on_dir */)) != 0) {
5017 KAUTH_DEBUG("%p ALLOWED - granted by posix permisssions", vcp
->vp
);
5021 /* not denied, must be OK */
5027 * Authorize an operation based on the node's attributes.
5030 vnode_authorize_simple(vauth_ctx vcp
, kauth_ace_rights_t acl_rights
, kauth_ace_rights_t preauth_rights
, boolean_t
*found_deny
)
5032 struct vnode_attr
*vap
= vcp
->vap
;
5033 kauth_cred_t cred
= vcp
->ctx
->vc_ucred
;
5034 struct kauth_acl_eval eval
;
5035 int error
, ismember
;
5036 mode_t posix_action
;
5039 * If we are the file owner, we automatically have some rights.
5041 * Do we need to expand this to support group ownership?
5043 if (vauth_file_owner(vcp
))
5044 acl_rights
&= ~(KAUTH_VNODE_WRITE_SECURITY
);
5047 * If we are checking both TAKE_OWNERSHIP and WRITE_SECURITY, we can
5048 * mask the latter. If TAKE_OWNERSHIP is requested the caller is about to
5049 * change ownership to themselves, and WRITE_SECURITY is implicitly
5050 * granted to the owner. We need to do this because at this point
5051 * WRITE_SECURITY may not be granted as the caller is not currently
5054 if ((acl_rights
& KAUTH_VNODE_TAKE_OWNERSHIP
) &&
5055 (acl_rights
& KAUTH_VNODE_WRITE_SECURITY
))
5056 acl_rights
&= ~KAUTH_VNODE_WRITE_SECURITY
;
5058 if (acl_rights
== 0) {
5059 KAUTH_DEBUG("%p ALLOWED - implicit or no rights required", vcp
->vp
);
5063 /* if we have an ACL, evaluate it */
5064 if (VATTR_IS_NOT(vap
, va_acl
, NULL
)) {
5065 eval
.ae_requested
= acl_rights
;
5066 eval
.ae_acl
= &vap
->va_acl
->acl_ace
[0];
5067 eval
.ae_count
= vap
->va_acl
->acl_entrycount
;
5068 eval
.ae_options
= 0;
5069 if (vauth_file_owner(vcp
))
5070 eval
.ae_options
|= KAUTH_AEVAL_IS_OWNER
;
5071 if ((error
= vauth_file_ingroup(vcp
, &ismember
)) != 0)
5074 eval
.ae_options
|= KAUTH_AEVAL_IN_GROUP
;
5075 eval
.ae_exp_gall
= KAUTH_VNODE_GENERIC_ALL_BITS
;
5076 eval
.ae_exp_gread
= KAUTH_VNODE_GENERIC_READ_BITS
;
5077 eval
.ae_exp_gwrite
= KAUTH_VNODE_GENERIC_WRITE_BITS
;
5078 eval
.ae_exp_gexec
= KAUTH_VNODE_GENERIC_EXECUTE_BITS
;
5080 if ((error
= kauth_acl_evaluate(cred
, &eval
)) != 0) {
5081 KAUTH_DEBUG("%p ERROR during ACL processing - %d", vcp
->vp
, error
);
5085 if (eval
.ae_result
== KAUTH_RESULT_DENY
) {
5086 KAUTH_DEBUG("%p DENIED - by ACL", vcp
->vp
);
5087 return(EACCES
); /* deny, deny, counter-allege */
5089 if (eval
.ae_result
== KAUTH_RESULT_ALLOW
) {
5090 KAUTH_DEBUG("%p ALLOWED - all rights granted by ACL", vcp
->vp
);
5093 *found_deny
= eval
.ae_found_deny
;
5095 /* fall through and evaluate residual rights */
5097 /* no ACL, everything is residual */
5098 eval
.ae_residual
= acl_rights
;
5102 * Grant residual rights that have been pre-authorized.
5104 eval
.ae_residual
&= ~preauth_rights
;
5107 * We grant WRITE_ATTRIBUTES to the owner if it hasn't been denied.
5109 if (vauth_file_owner(vcp
))
5110 eval
.ae_residual
&= ~KAUTH_VNODE_WRITE_ATTRIBUTES
;
5112 if (eval
.ae_residual
== 0) {
5113 KAUTH_DEBUG("%p ALLOWED - rights already authorized", vcp
->vp
);
5118 * Bail if we have residual rights that can't be granted by posix permissions,
5119 * or aren't presumed granted at this point.
5121 * XXX these can be collapsed for performance
5123 if (eval
.ae_residual
& KAUTH_VNODE_CHANGE_OWNER
) {
5124 KAUTH_DEBUG("%p DENIED - CHANGE_OWNER not permitted", vcp
->vp
);
5127 if (eval
.ae_residual
& KAUTH_VNODE_WRITE_SECURITY
) {
5128 KAUTH_DEBUG("%p DENIED - WRITE_SECURITY not permitted", vcp
->vp
);
5133 if (eval
.ae_residual
& KAUTH_VNODE_DELETE
)
5134 panic("vnode_authorize: can't be checking delete permission here");
5138 * Compute the fallback posix permissions that will satisfy the remaining
5142 if (eval
.ae_residual
& (KAUTH_VNODE_READ_DATA
|
5143 KAUTH_VNODE_LIST_DIRECTORY
|
5144 KAUTH_VNODE_READ_EXTATTRIBUTES
))
5145 posix_action
|= VREAD
;
5146 if (eval
.ae_residual
& (KAUTH_VNODE_WRITE_DATA
|
5147 KAUTH_VNODE_ADD_FILE
|
5148 KAUTH_VNODE_ADD_SUBDIRECTORY
|
5149 KAUTH_VNODE_DELETE_CHILD
|
5150 KAUTH_VNODE_WRITE_ATTRIBUTES
|
5151 KAUTH_VNODE_WRITE_EXTATTRIBUTES
))
5152 posix_action
|= VWRITE
;
5153 if (eval
.ae_residual
& (KAUTH_VNODE_EXECUTE
|
5154 KAUTH_VNODE_SEARCH
))
5155 posix_action
|= VEXEC
;
5157 if (posix_action
!= 0) {
5158 return(vnode_authorize_posix(vcp
, posix_action
, 0 /* !on_dir */));
5160 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",
5162 (eval
.ae_residual
& KAUTH_VNODE_READ_DATA
)
5163 ? vnode_isdir(vcp
->vp
) ? " LIST_DIRECTORY" : " READ_DATA" : "",
5164 (eval
.ae_residual
& KAUTH_VNODE_WRITE_DATA
)
5165 ? vnode_isdir(vcp
->vp
) ? " ADD_FILE" : " WRITE_DATA" : "",
5166 (eval
.ae_residual
& KAUTH_VNODE_EXECUTE
)
5167 ? vnode_isdir(vcp
->vp
) ? " SEARCH" : " EXECUTE" : "",
5168 (eval
.ae_residual
& KAUTH_VNODE_DELETE
)
5170 (eval
.ae_residual
& KAUTH_VNODE_APPEND_DATA
)
5171 ? vnode_isdir(vcp
->vp
) ? " ADD_SUBDIRECTORY" : " APPEND_DATA" : "",
5172 (eval
.ae_residual
& KAUTH_VNODE_DELETE_CHILD
)
5173 ? " DELETE_CHILD" : "",
5174 (eval
.ae_residual
& KAUTH_VNODE_READ_ATTRIBUTES
)
5175 ? " READ_ATTRIBUTES" : "",
5176 (eval
.ae_residual
& KAUTH_VNODE_WRITE_ATTRIBUTES
)
5177 ? " WRITE_ATTRIBUTES" : "",
5178 (eval
.ae_residual
& KAUTH_VNODE_READ_EXTATTRIBUTES
)
5179 ? " READ_EXTATTRIBUTES" : "",
5180 (eval
.ae_residual
& KAUTH_VNODE_WRITE_EXTATTRIBUTES
)
5181 ? " WRITE_EXTATTRIBUTES" : "",
5182 (eval
.ae_residual
& KAUTH_VNODE_READ_SECURITY
)
5183 ? " READ_SECURITY" : "",
5184 (eval
.ae_residual
& KAUTH_VNODE_WRITE_SECURITY
)
5185 ? " WRITE_SECURITY" : "",
5186 (eval
.ae_residual
& KAUTH_VNODE_CHECKIMMUTABLE
)
5187 ? " CHECKIMMUTABLE" : "",
5188 (eval
.ae_residual
& KAUTH_VNODE_CHANGE_OWNER
)
5189 ? " CHANGE_OWNER" : "");
5193 * Lack of required Posix permissions implies no reason to deny access.
5199 * Check for file immutability.
5202 vnode_authorize_checkimmutable(vnode_t vp
, struct vnode_attr
*vap
, int rights
, int ignore
)
5209 * Perform immutability checks for operations that change data.
5211 * Sockets, fifos and devices require special handling.
5213 switch(vp
->v_type
) {
5219 * Writing to these nodes does not change the filesystem data,
5220 * so forget that it's being tried.
5222 rights
&= ~KAUTH_VNODE_WRITE_DATA
;
5229 if (rights
& KAUTH_VNODE_WRITE_RIGHTS
) {
5231 /* check per-filesystem options if possible */
5235 /* check for no-EA filesystems */
5236 if ((rights
& KAUTH_VNODE_WRITE_EXTATTRIBUTES
) &&
5237 (vfs_flags(mp
) & MNT_NOUSERXATTR
)) {
5238 KAUTH_DEBUG("%p DENIED - filesystem disallowed extended attributes", vp
);
5239 error
= EACCES
; /* User attributes disabled */
5244 /* check for file immutability */
5246 if (vp
->v_type
== VDIR
) {
5247 if ((rights
& (KAUTH_VNODE_ADD_FILE
| KAUTH_VNODE_ADD_SUBDIRECTORY
)) == rights
)
5250 if ((rights
& KAUTH_VNODE_APPEND_DATA
) == rights
)
5253 if ((error
= vnode_immutable(vap
, append
, ignore
)) != 0) {
5254 KAUTH_DEBUG("%p DENIED - file is immutable", vp
);
5263 * Handle authorization actions for filesystems that advertise that the
5264 * server will be enforcing.
5266 * Returns: 0 Authorization should be handled locally
5267 * 1 Authorization was handled by the FS
5269 * Note: Imputed returns will only occur if the authorization request
5270 * was handled by the FS.
5272 * Imputed: *resultp, modified Return code from FS when the request is
5273 * handled by the FS.
5278 vnode_authorize_opaque(vnode_t vp
, int *resultp
, kauth_action_t action
, vfs_context_t ctx
)
5283 * If the vp is a device node, socket or FIFO it actually represents a local
5284 * endpoint, so we need to handle it locally.
5286 switch(vp
->v_type
) {
5297 * In the advisory request case, if the filesystem doesn't think it's reliable
5298 * we will attempt to formulate a result ourselves based on VNOP_GETATTR data.
5300 if ((action
& KAUTH_VNODE_ACCESS
) && !vfs_authopaqueaccess(vp
->v_mount
))
5304 * Let the filesystem have a say in the matter. It's OK for it to not implemnent
5305 * VNOP_ACCESS, as most will authorise inline with the actual request.
5307 if ((error
= VNOP_ACCESS(vp
, action
, ctx
)) != ENOTSUP
) {
5309 KAUTH_DEBUG("%p DENIED - opaque filesystem VNOP_ACCESS denied access", vp
);
5314 * Typically opaque filesystems do authorisation in-line, but exec is a special case. In
5315 * order to be reasonably sure that exec will be permitted, we try a bit harder here.
5317 if ((action
& KAUTH_VNODE_EXECUTE
) && (vp
->v_type
== VREG
)) {
5318 /* try a VNOP_OPEN for readonly access */
5319 if ((error
= VNOP_OPEN(vp
, FREAD
, ctx
)) != 0) {
5321 KAUTH_DEBUG("%p DENIED - EXECUTE denied because file could not be opened readonly", vp
);
5324 VNOP_CLOSE(vp
, FREAD
, ctx
);
5328 * We don't have any reason to believe that the request has to be denied at this point,
5329 * so go ahead and allow it.
5332 KAUTH_DEBUG("%p ALLOWED - bypassing access check for non-local filesystem", vp
);
5340 * Returns: KAUTH_RESULT_ALLOW
5343 * Imputed: *arg3, modified Error code in the deny case
5344 * EROFS Read-only file system
5345 * EACCES Permission denied
5346 * EPERM Operation not permitted [no execute]
5347 * vnode_getattr:ENOMEM Not enough space [only if has filesec]
5349 * vnode_authorize_opaque:*arg2 ???
5350 * vnode_authorize_checkimmutable:???
5351 * vnode_authorize_delete:???
5352 * vnode_authorize_simple:???
5357 vnode_authorize_callback(kauth_cred_t cred
, void *idata
, kauth_action_t action
,
5358 uintptr_t arg0
, uintptr_t arg1
, uintptr_t arg2
, uintptr_t arg3
)
5361 vnode_t cvp
= NULLVP
;
5365 ctx
= (vfs_context_t
)arg0
;
5367 dvp
= (vnode_t
)arg2
;
5370 * if there are 2 vnodes passed in, we don't know at
5371 * this point which rights to look at based on the
5372 * combined action being passed in... defer until later...
5373 * otherwise check the kauth 'rights' cache hung
5374 * off of the vnode we're interested in... if we've already
5375 * been granted the right we're currently interested in,
5376 * we can just return success... otherwise we'll go through
5377 * the process of authorizing the requested right(s)... if that
5378 * succeeds, we'll add the right(s) to the cache.
5379 * VNOP_SETATTR and VNOP_SETXATTR will invalidate this cache
5388 if (vnode_cache_is_authorized(cvp
, ctx
, action
) == TRUE
)
5389 return KAUTH_RESULT_ALLOW
;
5391 result
= vnode_authorize_callback_int(cred
, idata
, action
, arg0
, arg1
, arg2
, arg3
);
5393 if (result
== KAUTH_RESULT_ALLOW
&& cvp
!= NULLVP
)
5394 vnode_cache_authorized_action(cvp
, ctx
, action
);
5401 vnode_authorize_callback_int(__unused kauth_cred_t unused_cred
, __unused
void *idata
, kauth_action_t action
,
5402 uintptr_t arg0
, uintptr_t arg1
, uintptr_t arg2
, uintptr_t arg3
)
5404 struct _vnode_authorize_context auth_context
;
5409 kauth_ace_rights_t rights
;
5410 struct vnode_attr va
, dva
;
5414 boolean_t parent_authorized_for_delete
= FALSE
;
5415 boolean_t found_deny
= FALSE
;
5416 boolean_t parent_ref
= FALSE
;
5418 vcp
= &auth_context
;
5419 ctx
= vcp
->ctx
= (vfs_context_t
)arg0
;
5420 vp
= vcp
->vp
= (vnode_t
)arg1
;
5421 dvp
= vcp
->dvp
= (vnode_t
)arg2
;
5422 errorp
= (int *)arg3
;
5424 * Note that we authorize against the context, not the passed cred
5425 * (the same thing anyway)
5427 cred
= ctx
->vc_ucred
;
5434 vcp
->flags
= vcp
->flags_valid
= 0;
5437 if ((ctx
== NULL
) || (vp
== NULL
) || (cred
== NULL
))
5438 panic("vnode_authorize: bad arguments (context %p vp %p cred %p)", ctx
, vp
, cred
);
5441 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)",
5442 vp
, vfs_context_proc(ctx
)->p_comm
,
5443 (action
& KAUTH_VNODE_ACCESS
) ? "access" : "auth",
5444 (action
& KAUTH_VNODE_READ_DATA
) ? vnode_isdir(vp
) ? " LIST_DIRECTORY" : " READ_DATA" : "",
5445 (action
& KAUTH_VNODE_WRITE_DATA
) ? vnode_isdir(vp
) ? " ADD_FILE" : " WRITE_DATA" : "",
5446 (action
& KAUTH_VNODE_EXECUTE
) ? vnode_isdir(vp
) ? " SEARCH" : " EXECUTE" : "",
5447 (action
& KAUTH_VNODE_DELETE
) ? " DELETE" : "",
5448 (action
& KAUTH_VNODE_APPEND_DATA
) ? vnode_isdir(vp
) ? " ADD_SUBDIRECTORY" : " APPEND_DATA" : "",
5449 (action
& KAUTH_VNODE_DELETE_CHILD
) ? " DELETE_CHILD" : "",
5450 (action
& KAUTH_VNODE_READ_ATTRIBUTES
) ? " READ_ATTRIBUTES" : "",
5451 (action
& KAUTH_VNODE_WRITE_ATTRIBUTES
) ? " WRITE_ATTRIBUTES" : "",
5452 (action
& KAUTH_VNODE_READ_EXTATTRIBUTES
) ? " READ_EXTATTRIBUTES" : "",
5453 (action
& KAUTH_VNODE_WRITE_EXTATTRIBUTES
) ? " WRITE_EXTATTRIBUTES" : "",
5454 (action
& KAUTH_VNODE_READ_SECURITY
) ? " READ_SECURITY" : "",
5455 (action
& KAUTH_VNODE_WRITE_SECURITY
) ? " WRITE_SECURITY" : "",
5456 (action
& KAUTH_VNODE_CHANGE_OWNER
) ? " CHANGE_OWNER" : "",
5457 (action
& KAUTH_VNODE_NOIMMUTABLE
) ? " (noimmutable)" : "",
5458 vnode_isdir(vp
) ? "directory" : "file",
5459 vp
->v_name
? vp
->v_name
: "<NULL>", action
, vp
, dvp
);
5462 * Extract the control bits from the action, everything else is
5465 noimmutable
= (action
& KAUTH_VNODE_NOIMMUTABLE
) ? 1 : 0;
5466 rights
= action
& ~(KAUTH_VNODE_ACCESS
| KAUTH_VNODE_NOIMMUTABLE
);
5468 if (rights
& KAUTH_VNODE_DELETE
) {
5471 panic("vnode_authorize: KAUTH_VNODE_DELETE test requires a directory");
5474 * check to see if we've already authorized the parent
5475 * directory for deletion of its children... if so, we
5476 * can skip a whole bunch of work... we will still have to
5477 * authorize that this specific child can be removed
5479 if (vnode_cache_is_authorized(dvp
, ctx
, KAUTH_VNODE_DELETE
) == TRUE
)
5480 parent_authorized_for_delete
= TRUE
;
5486 * Check for read-only filesystems.
5488 if ((rights
& KAUTH_VNODE_WRITE_RIGHTS
) &&
5489 (vp
->v_mount
->mnt_flag
& MNT_RDONLY
) &&
5490 ((vp
->v_type
== VREG
) || (vp
->v_type
== VDIR
) ||
5491 (vp
->v_type
== VLNK
) || (vp
->v_type
== VCPLX
) ||
5492 (rights
& KAUTH_VNODE_DELETE
) || (rights
& KAUTH_VNODE_DELETE_CHILD
))) {
5498 * Check for noexec filesystems.
5500 if ((rights
& KAUTH_VNODE_EXECUTE
) && (vp
->v_type
== VREG
) && (vp
->v_mount
->mnt_flag
& MNT_NOEXEC
)) {
5506 * Handle cases related to filesystems with non-local enforcement.
5507 * This call can return 0, in which case we will fall through to perform a
5508 * check based on VNOP_GETATTR data. Otherwise it returns 1 and sets
5509 * an appropriate result, at which point we can return immediately.
5511 if ((vp
->v_mount
->mnt_kern_flag
& MNTK_AUTH_OPAQUE
) && vnode_authorize_opaque(vp
, &result
, action
, ctx
))
5515 * Get vnode attributes and extended security information for the vnode
5516 * and directory if required.
5518 VATTR_WANTED(&va
, va_mode
);
5519 VATTR_WANTED(&va
, va_uid
);
5520 VATTR_WANTED(&va
, va_gid
);
5521 VATTR_WANTED(&va
, va_flags
);
5522 VATTR_WANTED(&va
, va_acl
);
5523 if ((result
= vnode_getattr(vp
, &va
, ctx
)) != 0) {
5524 KAUTH_DEBUG("%p ERROR - failed to get vnode attributes - %d", vp
, result
);
5527 if (dvp
&& parent_authorized_for_delete
== FALSE
) {
5528 VATTR_WANTED(&dva
, va_mode
);
5529 VATTR_WANTED(&dva
, va_uid
);
5530 VATTR_WANTED(&dva
, va_gid
);
5531 VATTR_WANTED(&dva
, va_flags
);
5532 VATTR_WANTED(&dva
, va_acl
);
5533 if ((result
= vnode_getattr(dvp
, &dva
, ctx
)) != 0) {
5534 KAUTH_DEBUG("%p ERROR - failed to get directory vnode attributes - %d", vp
, result
);
5540 * If the vnode is an extended attribute data vnode (eg. a resource fork), *_DATA becomes
5543 if (S_ISXATTR(va
.va_mode
) || vnode_isnamedstream(vp
)) {
5544 if (rights
& KAUTH_VNODE_READ_DATA
) {
5545 rights
&= ~KAUTH_VNODE_READ_DATA
;
5546 rights
|= KAUTH_VNODE_READ_EXTATTRIBUTES
;
5548 if (rights
& KAUTH_VNODE_WRITE_DATA
) {
5549 rights
&= ~KAUTH_VNODE_WRITE_DATA
;
5550 rights
|= KAUTH_VNODE_WRITE_EXTATTRIBUTES
;
5555 * Point 'vp' to the resource fork's parent for ACL checking
5557 if (vnode_isnamedstream(vp
) &&
5558 (vp
->v_parent
!= NULL
) &&
5559 (vget_internal(vp
->v_parent
, 0, VNODE_NODEAD
) == 0)) {
5561 vcp
->vp
= vp
= vp
->v_parent
;
5562 if (VATTR_IS_SUPPORTED(&va
, va_acl
) && (va
.va_acl
!= NULL
))
5563 kauth_acl_free(va
.va_acl
);
5565 VATTR_WANTED(&va
, va_mode
);
5566 VATTR_WANTED(&va
, va_uid
);
5567 VATTR_WANTED(&va
, va_gid
);
5568 VATTR_WANTED(&va
, va_flags
);
5569 VATTR_WANTED(&va
, va_acl
);
5570 if ((result
= vnode_getattr(vp
, &va
, ctx
)) != 0)
5575 * Check for immutability.
5577 * In the deletion case, parent directory immutability vetoes specific
5580 if ((result
= vnode_authorize_checkimmutable(vp
, &va
, rights
, noimmutable
)) != 0)
5582 if ((rights
& KAUTH_VNODE_DELETE
) &&
5583 parent_authorized_for_delete
== FALSE
&&
5584 ((result
= vnode_authorize_checkimmutable(dvp
, &dva
, KAUTH_VNODE_DELETE_CHILD
, 0)) != 0))
5588 * Clear rights that have been authorized by reaching this point, bail if nothing left to
5591 rights
&= ~(KAUTH_VNODE_LINKTARGET
| KAUTH_VNODE_CHECKIMMUTABLE
);
5596 * If we're not the superuser, authorize based on file properties.
5598 if (!vfs_context_issuser(ctx
)) {
5599 /* process delete rights */
5600 if ((rights
& KAUTH_VNODE_DELETE
) &&
5601 parent_authorized_for_delete
== FALSE
&&
5602 ((result
= vnode_authorize_delete(vcp
)) != 0))
5605 /* process remaining rights */
5606 if ((rights
& ~KAUTH_VNODE_DELETE
) &&
5607 (result
= vnode_authorize_simple(vcp
, rights
, rights
& KAUTH_VNODE_DELETE
, &found_deny
)) != 0)
5612 * Execute is only granted to root if one of the x bits is set. This check only
5613 * makes sense if the posix mode bits are actually supported.
5615 if ((rights
& KAUTH_VNODE_EXECUTE
) &&
5616 (vp
->v_type
== VREG
) &&
5617 VATTR_IS_SUPPORTED(&va
, va_mode
) &&
5618 !(va
.va_mode
& (S_IXUSR
| S_IXGRP
| S_IXOTH
))) {
5620 KAUTH_DEBUG("%p DENIED - root execute requires at least one x bit in 0x%x", vp
, va
.va_mode
);
5624 KAUTH_DEBUG("%p ALLOWED - caller is superuser", vp
);
5627 if (VATTR_IS_SUPPORTED(&va
, va_acl
) && (va
.va_acl
!= NULL
))
5628 kauth_acl_free(va
.va_acl
);
5629 if (VATTR_IS_SUPPORTED(&dva
, va_acl
) && (dva
.va_acl
!= NULL
))
5630 kauth_acl_free(dva
.va_acl
);
5636 KAUTH_DEBUG("%p DENIED - auth denied", vp
);
5637 return(KAUTH_RESULT_DENY
);
5639 if ((rights
& KAUTH_VNODE_SEARCH
) && found_deny
== FALSE
&& vp
->v_type
== VDIR
) {
5641 * if we were successfully granted the right to search this directory
5642 * and there were NO ACL DENYs for search and the posix permissions also don't
5643 * deny execute, we can synthesize a global right that allows anyone to
5644 * traverse this directory during a pathname lookup without having to
5645 * match the credential associated with this cache of rights.
5647 if (!VATTR_IS_SUPPORTED(&va
, va_mode
) ||
5648 ((va
.va_mode
& (S_IXUSR
| S_IXGRP
| S_IXOTH
)) ==
5649 (S_IXUSR
| S_IXGRP
| S_IXOTH
))) {
5650 vnode_cache_authorized_action(vp
, ctx
, KAUTH_VNODE_SEARCHBYANYONE
);
5653 if ((rights
& KAUTH_VNODE_DELETE
) && parent_authorized_for_delete
== FALSE
) {
5655 * parent was successfully and newly authorized for deletions
5656 * add it to the cache
5658 vnode_cache_authorized_action(dvp
, ctx
, KAUTH_VNODE_DELETE
);
5663 * Note that this implies that we will allow requests for no rights, as well as
5664 * for rights that we do not recognise. There should be none of these.
5666 KAUTH_DEBUG("%p ALLOWED - auth granted", vp
);
5667 return(KAUTH_RESULT_ALLOW
);
5671 * Check that the attribute information in vattr can be legally applied to
5672 * a new file by the context.
5675 vnode_authattr_new(vnode_t dvp
, struct vnode_attr
*vap
, int noauth
, vfs_context_t ctx
)
5678 int has_priv_suser
, ismember
, defaulted_owner
, defaulted_group
, defaulted_mode
;
5684 defaulted_owner
= defaulted_group
= defaulted_mode
= 0;
5687 * Require that the filesystem support extended security to apply any.
5689 if (!vfs_extendedsecurity(dvp
->v_mount
) &&
5690 (VATTR_IS_ACTIVE(vap
, va_acl
) || VATTR_IS_ACTIVE(vap
, va_uuuid
) || VATTR_IS_ACTIVE(vap
, va_guuid
))) {
5696 * Default some fields.
5701 * If the filesystem is mounted IGNORE_OWNERSHIP and an explicit owner is set, that
5702 * owner takes ownership of all new files.
5704 if ((dmp
->mnt_flag
& MNT_IGNORE_OWNERSHIP
) && (dmp
->mnt_fsowner
!= KAUTH_UID_NONE
)) {
5705 VATTR_SET(vap
, va_uid
, dmp
->mnt_fsowner
);
5706 defaulted_owner
= 1;
5708 if (!VATTR_IS_ACTIVE(vap
, va_uid
)) {
5709 /* default owner is current user */
5710 VATTR_SET(vap
, va_uid
, kauth_cred_getuid(vfs_context_ucred(ctx
)));
5711 defaulted_owner
= 1;
5716 * If the filesystem is mounted IGNORE_OWNERSHIP and an explicit grouo is set, that
5717 * group takes ownership of all new files.
5719 if ((dmp
->mnt_flag
& MNT_IGNORE_OWNERSHIP
) && (dmp
->mnt_fsgroup
!= KAUTH_GID_NONE
)) {
5720 VATTR_SET(vap
, va_gid
, dmp
->mnt_fsgroup
);
5721 defaulted_group
= 1;
5723 if (!VATTR_IS_ACTIVE(vap
, va_gid
)) {
5724 /* default group comes from parent object, fallback to current user */
5725 struct vnode_attr dva
;
5727 VATTR_WANTED(&dva
, va_gid
);
5728 if ((error
= vnode_getattr(dvp
, &dva
, ctx
)) != 0)
5730 if (VATTR_IS_SUPPORTED(&dva
, va_gid
)) {
5731 VATTR_SET(vap
, va_gid
, dva
.va_gid
);
5733 VATTR_SET(vap
, va_gid
, kauth_cred_getgid(vfs_context_ucred(ctx
)));
5735 defaulted_group
= 1;
5739 if (!VATTR_IS_ACTIVE(vap
, va_flags
))
5740 VATTR_SET(vap
, va_flags
, 0);
5742 /* default mode is everything, masked with current umask */
5743 if (!VATTR_IS_ACTIVE(vap
, va_mode
)) {
5744 VATTR_SET(vap
, va_mode
, ACCESSPERMS
& ~vfs_context_proc(ctx
)->p_fd
->fd_cmask
);
5745 KAUTH_DEBUG("ATTR - defaulting new file mode to %o from umask %o", vap
->va_mode
, vfs_context_proc(ctx
)->p_fd
->fd_cmask
);
5748 /* set timestamps to now */
5749 if (!VATTR_IS_ACTIVE(vap
, va_create_time
)) {
5750 nanotime(&vap
->va_create_time
);
5751 VATTR_SET_ACTIVE(vap
, va_create_time
);
5755 * Check for attempts to set nonsensical fields.
5757 if (vap
->va_active
& ~VNODE_ATTR_NEWOBJ
) {
5759 KAUTH_DEBUG("ATTR - ERROR - attempt to set unsupported new-file attributes %llx",
5760 vap
->va_active
& ~VNODE_ATTR_NEWOBJ
);
5765 * Quickly check for the applicability of any enforcement here.
5766 * Tests below maintain the integrity of the local security model.
5768 if (vfs_authopaque(dvp
->v_mount
))
5772 * We need to know if the caller is the superuser, or if the work is
5773 * otherwise already authorised.
5775 cred
= vfs_context_ucred(ctx
);
5777 /* doing work for the kernel */
5780 has_priv_suser
= vfs_context_issuser(ctx
);
5784 if (VATTR_IS_ACTIVE(vap
, va_flags
)) {
5785 if (has_priv_suser
) {
5786 if ((vap
->va_flags
& (UF_SETTABLE
| SF_SETTABLE
)) != vap
->va_flags
) {
5788 KAUTH_DEBUG(" DENIED - superuser attempt to set illegal flag(s)");
5792 if ((vap
->va_flags
& UF_SETTABLE
) != vap
->va_flags
) {
5794 KAUTH_DEBUG(" DENIED - user attempt to set illegal flag(s)");
5800 /* if not superuser, validate legality of new-item attributes */
5801 if (!has_priv_suser
) {
5802 if (!defaulted_mode
&& VATTR_IS_ACTIVE(vap
, va_mode
)) {
5804 if (vap
->va_mode
& S_ISGID
) {
5805 if ((error
= kauth_cred_ismember_gid(cred
, vap
->va_gid
, &ismember
)) != 0) {
5806 KAUTH_DEBUG("ATTR - ERROR: got %d checking for membership in %d", error
, vap
->va_gid
);
5810 KAUTH_DEBUG(" DENIED - can't set SGID bit, not a member of %d", vap
->va_gid
);
5817 if ((vap
->va_mode
& S_ISUID
) && (vap
->va_uid
!= kauth_cred_getuid(cred
))) {
5818 KAUTH_DEBUG("ATTR - ERROR: illegal attempt to set the setuid bit");
5823 if (!defaulted_owner
&& (vap
->va_uid
!= kauth_cred_getuid(cred
))) {
5824 KAUTH_DEBUG(" DENIED - cannot create new item owned by %d", vap
->va_uid
);
5828 if (!defaulted_group
) {
5829 if ((error
= kauth_cred_ismember_gid(cred
, vap
->va_gid
, &ismember
)) != 0) {
5830 KAUTH_DEBUG(" ERROR - got %d checking for membership in %d", error
, vap
->va_gid
);
5834 KAUTH_DEBUG(" DENIED - cannot create new item with group %d - not a member", vap
->va_gid
);
5840 /* initialising owner/group UUID */
5841 if (VATTR_IS_ACTIVE(vap
, va_uuuid
)) {
5842 if ((error
= kauth_cred_getguid(cred
, &changer
)) != 0) {
5843 KAUTH_DEBUG(" ERROR - got %d trying to get caller UUID", error
);
5844 /* XXX ENOENT here - no GUID - should perhaps become EPERM */
5847 if (!kauth_guid_equal(&vap
->va_uuuid
, &changer
)) {
5848 KAUTH_DEBUG(" ERROR - cannot create item with supplied owner UUID - not us");
5853 if (VATTR_IS_ACTIVE(vap
, va_guuid
)) {
5854 if ((error
= kauth_cred_ismember_guid(cred
, &vap
->va_guuid
, &ismember
)) != 0) {
5855 KAUTH_DEBUG(" ERROR - got %d trying to check group membership", error
);
5859 KAUTH_DEBUG(" ERROR - cannot create item with supplied group UUID - not a member");
5870 * Check that the attribute information in vap can be legally written by the
5873 * Call this when you're not sure about the vnode_attr; either its contents
5874 * have come from an unknown source, or when they are variable.
5876 * Returns errno, or zero and sets *actionp to the KAUTH_VNODE_* actions that
5877 * must be authorized to be permitted to write the vattr.
5880 vnode_authattr(vnode_t vp
, struct vnode_attr
*vap
, kauth_action_t
*actionp
, vfs_context_t ctx
)
5882 struct vnode_attr ova
;
5883 kauth_action_t required_action
;
5884 int error
, has_priv_suser
, ismember
, chowner
, chgroup
, clear_suid
, clear_sgid
;
5893 required_action
= 0;
5897 * Quickly check for enforcement applicability.
5899 if (vfs_authopaque(vp
->v_mount
))
5903 * Check for attempts to set nonsensical fields.
5905 if (vap
->va_active
& VNODE_ATTR_RDONLY
) {
5906 KAUTH_DEBUG("ATTR - ERROR: attempt to set readonly attribute(s)");
5912 * We need to know if the caller is the superuser.
5914 cred
= vfs_context_ucred(ctx
);
5915 has_priv_suser
= kauth_cred_issuser(cred
);
5918 * If any of the following are changing, we need information from the old file:
5925 if (VATTR_IS_ACTIVE(vap
, va_uid
) ||
5926 VATTR_IS_ACTIVE(vap
, va_gid
) ||
5927 VATTR_IS_ACTIVE(vap
, va_mode
) ||
5928 VATTR_IS_ACTIVE(vap
, va_uuuid
) ||
5929 VATTR_IS_ACTIVE(vap
, va_guuid
)) {
5930 VATTR_WANTED(&ova
, va_mode
);
5931 VATTR_WANTED(&ova
, va_uid
);
5932 VATTR_WANTED(&ova
, va_gid
);
5933 VATTR_WANTED(&ova
, va_uuuid
);
5934 VATTR_WANTED(&ova
, va_guuid
);
5935 KAUTH_DEBUG("ATTR - security information changing, fetching existing attributes");
5939 * If timestamps are being changed, we need to know who the file is owned
5942 if (VATTR_IS_ACTIVE(vap
, va_create_time
) ||
5943 VATTR_IS_ACTIVE(vap
, va_change_time
) ||
5944 VATTR_IS_ACTIVE(vap
, va_modify_time
) ||
5945 VATTR_IS_ACTIVE(vap
, va_access_time
) ||
5946 VATTR_IS_ACTIVE(vap
, va_backup_time
)) {
5948 VATTR_WANTED(&ova
, va_uid
);
5949 #if 0 /* enable this when we support UUIDs as official owners */
5950 VATTR_WANTED(&ova
, va_uuuid
);
5952 KAUTH_DEBUG("ATTR - timestamps changing, fetching uid and GUID");
5956 * If flags are being changed, we need the old flags.
5958 if (VATTR_IS_ACTIVE(vap
, va_flags
)) {
5959 KAUTH_DEBUG("ATTR - flags changing, fetching old flags");
5960 VATTR_WANTED(&ova
, va_flags
);
5964 * If the size is being set, make sure it's not a directory.
5966 if (VATTR_IS_ACTIVE(vap
, va_data_size
)) {
5967 /* size is meaningless on a directory, don't permit this */
5968 if (vnode_isdir(vp
)) {
5969 KAUTH_DEBUG("ATTR - ERROR: size change requested on a directory");
5978 KAUTH_DEBUG("ATTR - fetching old attributes %016llx", ova
.va_active
);
5979 if ((error
= vnode_getattr(vp
, &ova
, ctx
)) != 0) {
5980 KAUTH_DEBUG(" ERROR - got %d trying to get attributes", error
);
5985 * Size changes require write access to the file data.
5987 if (VATTR_IS_ACTIVE(vap
, va_data_size
)) {
5988 /* if we can't get the size, or it's different, we need write access */
5989 KAUTH_DEBUG("ATTR - size change, requiring WRITE_DATA");
5990 required_action
|= KAUTH_VNODE_WRITE_DATA
;
5994 * Changing timestamps?
5996 * Note that we are only called to authorize user-requested time changes;
5997 * side-effect time changes are not authorized. Authorisation is only
5998 * required for existing files.
6000 * Non-owners are not permitted to change the time on an existing
6001 * file to anything other than the current time.
6003 if (VATTR_IS_ACTIVE(vap
, va_create_time
) ||
6004 VATTR_IS_ACTIVE(vap
, va_change_time
) ||
6005 VATTR_IS_ACTIVE(vap
, va_modify_time
) ||
6006 VATTR_IS_ACTIVE(vap
, va_access_time
) ||
6007 VATTR_IS_ACTIVE(vap
, va_backup_time
)) {
6009 * The owner and root may set any timestamps they like,
6010 * provided that the file is not immutable. The owner still needs
6011 * WRITE_ATTRIBUTES (implied by ownership but still deniable).
6013 if (has_priv_suser
|| vauth_node_owner(&ova
, cred
)) {
6014 KAUTH_DEBUG("ATTR - root or owner changing timestamps");
6015 required_action
|= KAUTH_VNODE_CHECKIMMUTABLE
| KAUTH_VNODE_WRITE_ATTRIBUTES
;
6017 /* just setting the current time? */
6018 if (vap
->va_vaflags
& VA_UTIMES_NULL
) {
6019 KAUTH_DEBUG("ATTR - non-root/owner changing timestamps, requiring WRITE_ATTRIBUTES");
6020 required_action
|= KAUTH_VNODE_WRITE_ATTRIBUTES
;
6022 KAUTH_DEBUG("ATTR - ERROR: illegal timestamp modification attempted");
6030 * Changing file mode?
6032 if (VATTR_IS_ACTIVE(vap
, va_mode
) && VATTR_IS_SUPPORTED(&ova
, va_mode
) && (ova
.va_mode
!= vap
->va_mode
)) {
6033 KAUTH_DEBUG("ATTR - mode change from %06o to %06o", ova
.va_mode
, vap
->va_mode
);
6036 * Mode changes always have the same basic auth requirements.
6038 if (has_priv_suser
) {
6039 KAUTH_DEBUG("ATTR - superuser mode change, requiring immutability check");
6040 required_action
|= KAUTH_VNODE_CHECKIMMUTABLE
;
6042 /* need WRITE_SECURITY */
6043 KAUTH_DEBUG("ATTR - non-superuser mode change, requiring WRITE_SECURITY");
6044 required_action
|= KAUTH_VNODE_WRITE_SECURITY
;
6048 * Can't set the setgid bit if you're not in the group and not root. Have to have
6049 * existing group information in the case we're not setting it right now.
6051 if (vap
->va_mode
& S_ISGID
) {
6052 required_action
|= KAUTH_VNODE_CHECKIMMUTABLE
; /* always required */
6053 if (!has_priv_suser
) {
6054 if (VATTR_IS_ACTIVE(vap
, va_gid
)) {
6055 group
= vap
->va_gid
;
6056 } else if (VATTR_IS_SUPPORTED(&ova
, va_gid
)) {
6059 KAUTH_DEBUG("ATTR - ERROR: setgid but no gid available");
6064 * This might be too restrictive; WRITE_SECURITY might be implied by
6065 * membership in this case, rather than being an additional requirement.
6067 if ((error
= kauth_cred_ismember_gid(cred
, group
, &ismember
)) != 0) {
6068 KAUTH_DEBUG("ATTR - ERROR: got %d checking for membership in %d", error
, vap
->va_gid
);
6072 KAUTH_DEBUG(" DENIED - can't set SGID bit, not a member of %d", group
);
6080 * Can't set the setuid bit unless you're root or the file's owner.
6082 if (vap
->va_mode
& S_ISUID
) {
6083 required_action
|= KAUTH_VNODE_CHECKIMMUTABLE
; /* always required */
6084 if (!has_priv_suser
) {
6085 if (VATTR_IS_ACTIVE(vap
, va_uid
)) {
6086 owner
= vap
->va_uid
;
6087 } else if (VATTR_IS_SUPPORTED(&ova
, va_uid
)) {
6090 KAUTH_DEBUG("ATTR - ERROR: setuid but no uid available");
6094 if (owner
!= kauth_cred_getuid(cred
)) {
6096 * We could allow this if WRITE_SECURITY is permitted, perhaps.
6098 KAUTH_DEBUG("ATTR - ERROR: illegal attempt to set the setuid bit");
6107 * Validate/mask flags changes. This checks that only the flags in
6108 * the UF_SETTABLE mask are being set, and preserves the flags in
6109 * the SF_SETTABLE case.
6111 * Since flags changes may be made in conjunction with other changes,
6112 * we will ask the auth code to ignore immutability in the case that
6113 * the SF_* flags are not set and we are only manipulating the file flags.
6116 if (VATTR_IS_ACTIVE(vap
, va_flags
)) {
6117 /* compute changing flags bits */
6118 if (VATTR_IS_SUPPORTED(&ova
, va_flags
)) {
6119 fdelta
= vap
->va_flags
^ ova
.va_flags
;
6121 fdelta
= vap
->va_flags
;
6125 KAUTH_DEBUG("ATTR - flags changing, requiring WRITE_SECURITY");
6126 required_action
|= KAUTH_VNODE_WRITE_SECURITY
;
6128 /* check that changing bits are legal */
6129 if (has_priv_suser
) {
6131 * The immutability check will prevent us from clearing the SF_*
6132 * flags unless the system securelevel permits it, so just check
6133 * for legal flags here.
6135 if (fdelta
& ~(UF_SETTABLE
| SF_SETTABLE
)) {
6137 KAUTH_DEBUG(" DENIED - superuser attempt to set illegal flag(s)");
6141 if (fdelta
& ~UF_SETTABLE
) {
6143 KAUTH_DEBUG(" DENIED - user attempt to set illegal flag(s)");
6148 * If the caller has the ability to manipulate file flags,
6149 * security is not reduced by ignoring them for this operation.
6151 * A more complete test here would consider the 'after' states of the flags
6152 * to determine whether it would permit the operation, but this becomes
6155 * Ignoring immutability is conditional on securelevel; this does not bypass
6156 * the SF_* flags if securelevel > 0.
6158 required_action
|= KAUTH_VNODE_NOIMMUTABLE
;
6163 * Validate ownership information.
6172 * Note that if the filesystem didn't give us a UID, we expect that it doesn't
6173 * support them in general, and will ignore it if/when we try to set it.
6174 * We might want to clear the uid out of vap completely here.
6176 if (VATTR_IS_ACTIVE(vap
, va_uid
)) {
6177 if (VATTR_IS_SUPPORTED(&ova
, va_uid
) && (vap
->va_uid
!= ova
.va_uid
)) {
6178 if (!has_priv_suser
&& (kauth_cred_getuid(cred
) != vap
->va_uid
)) {
6179 KAUTH_DEBUG(" DENIED - non-superuser cannot change ownershipt to a third party");
6190 * Note that if the filesystem didn't give us a GID, we expect that it doesn't
6191 * support them in general, and will ignore it if/when we try to set it.
6192 * We might want to clear the gid out of vap completely here.
6194 if (VATTR_IS_ACTIVE(vap
, va_gid
)) {
6195 if (VATTR_IS_SUPPORTED(&ova
, va_gid
) && (vap
->va_gid
!= ova
.va_gid
)) {
6196 if (!has_priv_suser
) {
6197 if ((error
= kauth_cred_ismember_gid(cred
, vap
->va_gid
, &ismember
)) != 0) {
6198 KAUTH_DEBUG(" ERROR - got %d checking for membership in %d", error
, vap
->va_gid
);
6202 KAUTH_DEBUG(" DENIED - group change from %d to %d but not a member of target group",
6203 ova
.va_gid
, vap
->va_gid
);
6214 * Owner UUID being set or changed.
6216 if (VATTR_IS_ACTIVE(vap
, va_uuuid
)) {
6217 /* if the owner UUID is not actually changing ... */
6218 if (VATTR_IS_SUPPORTED(&ova
, va_uuuid
) && kauth_guid_equal(&vap
->va_uuuid
, &ova
.va_uuuid
))
6219 goto no_uuuid_change
;
6222 * The owner UUID cannot be set by a non-superuser to anything other than
6225 if (!has_priv_suser
) {
6226 if ((error
= kauth_cred_getguid(cred
, &changer
)) != 0) {
6227 KAUTH_DEBUG(" ERROR - got %d trying to get caller UUID", error
);
6228 /* XXX ENOENT here - no UUID - should perhaps become EPERM */
6231 if (!kauth_guid_equal(&vap
->va_uuuid
, &changer
)) {
6232 KAUTH_DEBUG(" ERROR - cannot set supplied owner UUID - not us");
6242 * Group UUID being set or changed.
6244 if (VATTR_IS_ACTIVE(vap
, va_guuid
)) {
6245 /* if the group UUID is not actually changing ... */
6246 if (VATTR_IS_SUPPORTED(&ova
, va_guuid
) && kauth_guid_equal(&vap
->va_guuid
, &ova
.va_guuid
))
6247 goto no_guuid_change
;
6250 * The group UUID cannot be set by a non-superuser to anything other than
6251 * one of which they are a member.
6253 if (!has_priv_suser
) {
6254 if ((error
= kauth_cred_ismember_guid(cred
, &vap
->va_guuid
, &ismember
)) != 0) {
6255 KAUTH_DEBUG(" ERROR - got %d trying to check group membership", error
);
6259 KAUTH_DEBUG(" ERROR - cannot create item with supplied group UUID - not a member");
6269 * Compute authorisation for group/ownership changes.
6271 if (chowner
|| chgroup
|| clear_suid
|| clear_sgid
) {
6272 if (has_priv_suser
) {
6273 KAUTH_DEBUG("ATTR - superuser changing file owner/group, requiring immutability check");
6274 required_action
|= KAUTH_VNODE_CHECKIMMUTABLE
;
6277 KAUTH_DEBUG("ATTR - ownership change, requiring TAKE_OWNERSHIP");
6278 required_action
|= KAUTH_VNODE_TAKE_OWNERSHIP
;
6280 if (chgroup
&& !chowner
) {
6281 KAUTH_DEBUG("ATTR - group change, requiring WRITE_SECURITY");
6282 required_action
|= KAUTH_VNODE_WRITE_SECURITY
;
6285 /* clear set-uid and set-gid bits as required by Posix */
6286 if (VATTR_IS_ACTIVE(vap
, va_mode
)) {
6287 newmode
= vap
->va_mode
;
6288 } else if (VATTR_IS_SUPPORTED(&ova
, va_mode
)) {
6289 newmode
= ova
.va_mode
;
6291 KAUTH_DEBUG("CHOWN - trying to change owner but cannot get mode from filesystem to mask setugid bits");
6294 if (newmode
& (S_ISUID
| S_ISGID
)) {
6295 VATTR_SET(vap
, va_mode
, newmode
& ~(S_ISUID
| S_ISGID
));
6296 KAUTH_DEBUG("CHOWN - masking setugid bits from mode %o to %o", newmode
, vap
->va_mode
);
6302 * Authorise changes in the ACL.
6304 if (VATTR_IS_ACTIVE(vap
, va_acl
)) {
6306 /* no existing ACL */
6307 if (!VATTR_IS_ACTIVE(&ova
, va_acl
) || (ova
.va_acl
== NULL
)) {
6310 if (vap
->va_acl
!= NULL
) {
6311 required_action
|= KAUTH_VNODE_WRITE_SECURITY
;
6312 KAUTH_DEBUG("CHMOD - adding ACL");
6315 /* removing an existing ACL */
6316 } else if (vap
->va_acl
== NULL
) {
6317 required_action
|= KAUTH_VNODE_WRITE_SECURITY
;
6318 KAUTH_DEBUG("CHMOD - removing ACL");
6320 /* updating an existing ACL */
6322 if (vap
->va_acl
->acl_entrycount
!= ova
.va_acl
->acl_entrycount
) {
6323 /* entry count changed, must be different */
6324 required_action
|= KAUTH_VNODE_WRITE_SECURITY
;
6325 KAUTH_DEBUG("CHMOD - adding/removing ACL entries");
6326 } else if (vap
->va_acl
->acl_entrycount
> 0) {
6327 /* both ACLs have the same ACE count, said count is 1 or more, bitwise compare ACLs */
6328 if (!memcmp(&vap
->va_acl
->acl_ace
[0], &ova
.va_acl
->acl_ace
[0],
6329 sizeof(struct kauth_ace
) * vap
->va_acl
->acl_entrycount
)) {
6330 required_action
|= KAUTH_VNODE_WRITE_SECURITY
;
6331 KAUTH_DEBUG("CHMOD - changing ACL entries");
6338 * Other attributes that require authorisation.
6340 if (VATTR_IS_ACTIVE(vap
, va_encoding
))
6341 required_action
|= KAUTH_VNODE_WRITE_ATTRIBUTES
;
6344 if (VATTR_IS_SUPPORTED(&ova
, va_acl
) && (ova
.va_acl
!= NULL
))
6345 kauth_acl_free(ova
.va_acl
);
6347 *actionp
= required_action
;
6353 vfs_setlocklocal(mount_t mp
)
6358 mp
->mnt_kern_flag
|= MNTK_LOCK_LOCAL
;
6361 * We do not expect anyone to be using any vnodes at the
6362 * time this routine is called. So no need for vnode locking
6364 TAILQ_FOREACH(vp
, &mp
->mnt_vnodelist
, v_mntvnodes
) {
6365 vp
->v_flag
|= VLOCKLOCAL
;
6367 TAILQ_FOREACH(vp
, &mp
->mnt_workerqueue
, v_mntvnodes
) {
6368 vp
->v_flag
|= VLOCKLOCAL
;
6370 TAILQ_FOREACH(vp
, &mp
->mnt_newvnodes
, v_mntvnodes
) {
6371 vp
->v_flag
|= VLOCKLOCAL
;
6377 vn_setunionwait(vnode_t vp
)
6379 vnode_lock_spin(vp
);
6380 vp
->v_flag
|= VISUNION
;
6386 vn_checkunionwait(vnode_t vp
)
6389 while ((vp
->v_flag
& VISUNION
) == VISUNION
)
6390 msleep((caddr_t
)&vp
->v_flag
, &vp
->v_lock
, 0, 0, 0);
6395 vn_clearunionwait(vnode_t vp
, int locked
)
6399 if((vp
->v_flag
& VISUNION
) == VISUNION
) {
6400 vp
->v_flag
&= ~VISUNION
;
6401 wakeup((caddr_t
)&vp
->v_flag
);
6408 * XXX - get "don't trigger mounts" flag for thread; used by autofs.
6410 extern int thread_notrigger(void);
6413 thread_notrigger(void)
6415 struct uthread
*uth
= (struct uthread
*)get_bsdthread_info(current_thread());
6416 return (uth
->uu_notrigger
);
6420 * Removes orphaned apple double files during a rmdir
6422 * 1. vnode_suspend().
6423 * 2. Call VNOP_READDIR() till the end of directory is reached.
6424 * 3. Check if the directory entries returned are regular files with name starting with "._". If not, return ENOTEMPTY.
6425 * 4. Continue (2) and (3) till end of directory is reached.
6426 * 5. If all the entries in the directory were files with "._" name, delete all the files.
6428 * 7. If deletion of all files succeeded, call VNOP_RMDIR() again.
6431 errno_t
rmdir_remove_orphaned_appleDouble(vnode_t vp
, vfs_context_t ctx
, int * restart_flag
)
6434 #define UIO_BUFF_SIZE 2048
6436 int eofflag
, siz
= UIO_BUFF_SIZE
, nentries
= 0;
6437 int open_flag
= 0, full_erase_flag
= 0;
6438 char uio_buf
[ UIO_SIZEOF(1) ];
6439 char *rbuf
= NULL
, *cpos
, *cend
;
6440 struct nameidata nd_temp
;
6444 error
= vnode_suspend(vp
);
6447 * restart_flag is set so that the calling rmdir sleeps and resets
6457 MALLOC(rbuf
, caddr_t
, siz
, M_TEMP
, M_WAITOK
);
6459 auio
= uio_createwithbuffer(1, 0, UIO_SYSSPACE
, UIO_READ
,
6460 &uio_buf
[0], sizeof(uio_buf
));
6461 if (!rbuf
|| !auio
) {
6466 uio_setoffset(auio
,0);
6470 if ((error
= VNOP_OPEN(vp
, FREAD
, ctx
)))
6476 * First pass checks if all files are appleDouble files.
6480 siz
= UIO_BUFF_SIZE
;
6481 uio_reset(auio
, uio_offset(auio
), UIO_SYSSPACE
, UIO_READ
);
6482 uio_addiov(auio
, CAST_USER_ADDR_T(rbuf
), UIO_BUFF_SIZE
);
6484 if((error
= VNOP_READDIR(vp
, auio
, 0, &eofflag
, &nentries
, ctx
)))
6487 if (uio_resid(auio
) != 0)
6488 siz
-= uio_resid(auio
);
6491 * Iterate through directory
6495 dp
= (struct dirent
*) cpos
;
6500 while ((cpos
< cend
)) {
6502 * Check for . and .. as well as directories
6504 if (dp
->d_ino
!= 0 &&
6505 !((dp
->d_namlen
== 1 && dp
->d_name
[0] == '.') ||
6506 (dp
->d_namlen
== 2 && dp
->d_name
[0] == '.' && dp
->d_name
[1] == '.'))) {
6508 * Check for irregular files and ._ files
6509 * If there is a ._._ file abort the op
6511 if ( dp
->d_namlen
< 2 ||
6512 strncmp(dp
->d_name
,"._",2) ||
6513 (dp
->d_namlen
>= 4 && !strncmp(&(dp
->d_name
[2]), "._",2))) {
6518 cpos
+= dp
->d_reclen
;
6519 dp
= (struct dirent
*)cpos
;
6524 * If we've made it here all the files in the dir are AppleDouble
6525 * We can delete the files even though the node is suspended
6526 * because we are the owner of the file.
6529 uio_reset(auio
, 0, UIO_SYSSPACE
, UIO_READ
);
6533 siz
= UIO_BUFF_SIZE
;
6534 uio_reset(auio
, uio_offset(auio
), UIO_SYSSPACE
, UIO_READ
);
6535 uio_addiov(auio
, CAST_USER_ADDR_T(rbuf
), UIO_BUFF_SIZE
);
6537 error
= VNOP_READDIR(vp
, auio
, 0, &eofflag
, &nentries
, ctx
);
6542 if (uio_resid(auio
) != 0)
6543 siz
-= uio_resid(auio
);
6546 * Iterate through directory
6550 dp
= (struct dirent
*) cpos
;
6555 while ((cpos
< cend
)) {
6557 * Check for . and .. as well as directories
6559 if (dp
->d_ino
!= 0 &&
6560 !((dp
->d_namlen
== 1 && dp
->d_name
[0] == '.') ||
6561 (dp
->d_namlen
== 2 && dp
->d_name
[0] == '.' && dp
->d_name
[1] == '.'))
6563 NDINIT(&nd_temp
, DELETE
, USEDVP
, UIO_SYSSPACE
, CAST_USER_ADDR_T(dp
->d_name
), ctx
);
6564 nd_temp
.ni_dvp
= vp
;
6565 error
= unlink1(ctx
, &nd_temp
, 0);
6566 if(error
&& error
!= ENOENT
)
6569 cpos
+= dp
->d_reclen
;
6570 dp
= (struct dirent
*)cpos
;
6574 * workaround for HFS/NFS setting eofflag before end of file
6576 if (vp
->v_tag
== VT_HFS
&& nentries
> 2)
6579 if (vp
->v_tag
== VT_NFS
) {
6580 if (eofflag
&& !full_erase_flag
) {
6581 full_erase_flag
= 1;
6583 uio_reset(auio
, 0, UIO_SYSSPACE
, UIO_READ
);
6585 else if (!eofflag
&& full_erase_flag
)
6586 full_erase_flag
= 0;
6596 VNOP_CLOSE(vp
, FREAD
, ctx
);
6611 record_vp(vnode_t vp
, int count
) {
6615 if ((vp
->v_flag
& VSYSTEM
))
6618 ut
= get_bsdthread_info(current_thread());
6619 ut
->uu_iocount
+= count
;
6621 if (ut
->uu_vpindex
< 32) {
6622 for (i
= 0; i
< ut
->uu_vpindex
; i
++) {
6623 if (ut
->uu_vps
[i
] == vp
)
6626 ut
->uu_vps
[ut
->uu_vpindex
] = vp
;