]> git.saurik.com Git - apple/xnu.git/blob - bsd/vfs/vfs_subr.c
xnu-3789.70.16.tar.gz
[apple/xnu.git] / bsd / vfs / vfs_subr.c
1 /*
2 * Copyright (c) 2000-2016 Apple Inc. All rights reserved.
3 *
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5 *
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.
14 *
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
17 *
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.
25 *
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27 */
28 /* Copyright (c) 1995 NeXT Computer, Inc. All Rights Reserved */
29 /*
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.
37 *
38 * Redistribution and use in source and binary forms, with or without
39 * modification, are permitted provided that the following conditions
40 * are met:
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.
53 *
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
64 * SUCH DAMAGE.
65 *
66 * @(#)vfs_subr.c 8.31 (Berkeley) 5/26/95
67 */
68 /*
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,
72 * Version 2.0.
73 */
74
75 /*
76 * External virtual filesystem routines
77 */
78
79 #include <sys/param.h>
80 #include <sys/systm.h>
81 #include <sys/proc_internal.h>
82 #include <sys/kauth.h>
83 #include <sys/mount_internal.h>
84 #include <sys/time.h>
85 #include <sys/lock.h>
86 #include <sys/vnode.h>
87 #include <sys/vnode_internal.h>
88 #include <sys/stat.h>
89 #include <sys/namei.h>
90 #include <sys/ucred.h>
91 #include <sys/buf_internal.h>
92 #include <sys/errno.h>
93 #include <sys/malloc.h>
94 #include <sys/uio_internal.h>
95 #include <sys/uio.h>
96 #include <sys/domain.h>
97 #include <sys/mbuf.h>
98 #include <sys/syslog.h>
99 #include <sys/ubc_internal.h>
100 #include <sys/vm.h>
101 #include <sys/sysctl.h>
102 #include <sys/filedesc.h>
103 #include <sys/event.h>
104 #include <sys/kdebug.h>
105 #include <sys/kauth.h>
106 #include <sys/user.h>
107 #include <sys/systm.h>
108 #include <sys/kern_memorystatus.h>
109 #include <sys/lockf.h>
110 #include <miscfs/fifofs/fifo.h>
111
112 #include <string.h>
113 #include <machine/spl.h>
114 #include <machine/machine_routines.h>
115
116 #include <kern/assert.h>
117 #include <mach/kern_return.h>
118 #include <kern/thread.h>
119 #include <kern/sched_prim.h>
120
121 #include <miscfs/specfs/specdev.h>
122
123 #include <mach/mach_types.h>
124 #include <mach/memory_object_types.h>
125 #include <mach/memory_object_control.h>
126
127 #include <kern/kalloc.h> /* kalloc()/kfree() */
128 #include <kern/clock.h> /* delay_for_interval() */
129 #include <libkern/OSAtomic.h> /* OSAddAtomic() */
130 #include <console/video_console.h>
131
132 #ifdef JOE_DEBUG
133 #include <libkern/OSDebug.h>
134 #endif
135
136 #include <vm/vm_protos.h> /* vnode_pager_vrele() */
137
138 #if CONFIG_MACF
139 #include <security/mac_framework.h>
140 #endif
141
142 extern lck_grp_t *vnode_lck_grp;
143 extern lck_attr_t *vnode_lck_attr;
144
145 #if CONFIG_TRIGGERS
146 extern lck_grp_t *trigger_vnode_lck_grp;
147 extern lck_attr_t *trigger_vnode_lck_attr;
148 #endif
149
150 extern lck_mtx_t * mnt_list_mtx_lock;
151
152 enum vtype iftovt_tab[16] = {
153 VNON, VFIFO, VCHR, VNON, VDIR, VNON, VBLK, VNON,
154 VREG, VNON, VLNK, VNON, VSOCK, VNON, VNON, VBAD,
155 };
156 int vttoif_tab[9] = {
157 0, S_IFREG, S_IFDIR, S_IFBLK, S_IFCHR, S_IFLNK,
158 S_IFSOCK, S_IFIFO, S_IFMT,
159 };
160
161
162 /* XXX These should be in a BSD accessible Mach header, but aren't. */
163 extern void memory_object_mark_used(
164 memory_object_control_t control);
165
166 extern void memory_object_mark_unused(
167 memory_object_control_t control,
168 boolean_t rage);
169
170 extern void memory_object_mark_io_tracking(
171 memory_object_control_t control);
172
173 /* XXX next protptype should be from <nfs/nfs.h> */
174 extern int nfs_vinvalbuf(vnode_t, int, vfs_context_t, int);
175
176 /* XXX next prototytype should be from libsa/stdlib.h> but conflicts libkern */
177 __private_extern__ void qsort(
178 void * array,
179 size_t nmembers,
180 size_t member_size,
181 int (*)(const void *, const void *));
182
183 extern kern_return_t adjust_vm_object_cache(vm_size_t oval, vm_size_t nval);
184 __private_extern__ void vntblinit(void);
185 __private_extern__ kern_return_t reset_vmobjectcache(unsigned int val1,
186 unsigned int val2);
187 __private_extern__ int unlink1(vfs_context_t, vnode_t, user_addr_t,
188 enum uio_seg, int);
189
190 extern int system_inshutdown;
191
192 static void vnode_list_add(vnode_t);
193 static void vnode_async_list_add(vnode_t);
194 static void vnode_list_remove(vnode_t);
195 static void vnode_list_remove_locked(vnode_t);
196
197 static void vnode_abort_advlocks(vnode_t);
198 static errno_t vnode_drain(vnode_t);
199 static void vgone(vnode_t, int flags);
200 static void vclean(vnode_t vp, int flag);
201 static void vnode_reclaim_internal(vnode_t, int, int, int);
202
203 static void vnode_dropiocount (vnode_t);
204
205 static vnode_t checkalias(vnode_t vp, dev_t nvp_rdev);
206 static int vnode_reload(vnode_t);
207 static int vnode_isinuse_locked(vnode_t, int, int);
208
209 static int unmount_callback(mount_t, __unused void *);
210
211 static void insmntque(vnode_t vp, mount_t mp);
212 static int mount_getvfscnt(void);
213 static int mount_fillfsids(fsid_t *, int );
214 static void vnode_iterate_setup(mount_t);
215 int vnode_umount_preflight(mount_t, vnode_t, int);
216 static int vnode_iterate_prepare(mount_t);
217 static int vnode_iterate_reloadq(mount_t);
218 static void vnode_iterate_clear(mount_t);
219 static mount_t vfs_getvfs_locked(fsid_t *);
220 static int vn_create_reg(vnode_t dvp, vnode_t *vpp, struct nameidata *ndp,
221 struct vnode_attr *vap, uint32_t flags, int fmode, uint32_t *statusp, vfs_context_t ctx);
222 static int vnode_authattr_new_internal(vnode_t dvp, struct vnode_attr *vap, int noauth, uint32_t *defaulted_fieldsp, vfs_context_t ctx);
223
224 errno_t rmdir_remove_orphaned_appleDouble(vnode_t, vfs_context_t, int *);
225
226 #ifdef JOE_DEBUG
227 static void record_vp(vnode_t vp, int count);
228 #endif
229
230 #if CONFIG_JETSAM && (DEVELOPMENT || DEBUG)
231 extern int bootarg_no_vnode_jetsam; /* from bsd_init.c default value is 0 */
232 #endif /* CONFIG_JETSAM && (DEVELOPMENT || DEBUG) */
233
234 boolean_t root_is_CF_drive = FALSE;
235
236 #if CONFIG_TRIGGERS
237 static int vnode_resolver_create(mount_t, vnode_t, struct vnode_trigger_param *, boolean_t external);
238 static void vnode_resolver_detach(vnode_t);
239 #endif
240
241 TAILQ_HEAD(freelst, vnode) vnode_free_list; /* vnode free list */
242 TAILQ_HEAD(deadlst, vnode) vnode_dead_list; /* vnode dead list */
243 TAILQ_HEAD(async_work_lst, vnode) vnode_async_work_list;
244
245
246 TAILQ_HEAD(ragelst, vnode) vnode_rage_list; /* vnode rapid age list */
247 struct timeval rage_tv;
248 int rage_limit = 0;
249 int ragevnodes = 0;
250
251 #define RAGE_LIMIT_MIN 100
252 #define RAGE_TIME_LIMIT 5
253
254 struct mntlist mountlist; /* mounted filesystem list */
255 static int nummounts = 0;
256
257 #if DIAGNOSTIC
258 #define VLISTCHECK(fun, vp, list) \
259 if ((vp)->v_freelist.tqe_prev == (struct vnode **)0xdeadb) \
260 panic("%s: %s vnode not on %slist", (fun), (list), (list));
261 #else
262 #define VLISTCHECK(fun, vp, list)
263 #endif /* DIAGNOSTIC */
264
265 #define VLISTNONE(vp) \
266 do { \
267 (vp)->v_freelist.tqe_next = (struct vnode *)0; \
268 (vp)->v_freelist.tqe_prev = (struct vnode **)0xdeadb; \
269 } while(0)
270
271 #define VONLIST(vp) \
272 ((vp)->v_freelist.tqe_prev != (struct vnode **)0xdeadb)
273
274 /* remove a vnode from free vnode list */
275 #define VREMFREE(fun, vp) \
276 do { \
277 VLISTCHECK((fun), (vp), "free"); \
278 TAILQ_REMOVE(&vnode_free_list, (vp), v_freelist); \
279 VLISTNONE((vp)); \
280 freevnodes--; \
281 } while(0)
282
283
284 /* remove a vnode from dead vnode list */
285 #define VREMDEAD(fun, vp) \
286 do { \
287 VLISTCHECK((fun), (vp), "dead"); \
288 TAILQ_REMOVE(&vnode_dead_list, (vp), v_freelist); \
289 VLISTNONE((vp)); \
290 vp->v_listflag &= ~VLIST_DEAD; \
291 deadvnodes--; \
292 } while(0)
293
294
295 /* remove a vnode from async work vnode list */
296 #define VREMASYNC_WORK(fun, vp) \
297 do { \
298 VLISTCHECK((fun), (vp), "async_work"); \
299 TAILQ_REMOVE(&vnode_async_work_list, (vp), v_freelist); \
300 VLISTNONE((vp)); \
301 vp->v_listflag &= ~VLIST_ASYNC_WORK; \
302 async_work_vnodes--; \
303 } while(0)
304
305
306 /* remove a vnode from rage vnode list */
307 #define VREMRAGE(fun, vp) \
308 do { \
309 if ( !(vp->v_listflag & VLIST_RAGE)) \
310 panic("VREMRAGE: vp not on rage list"); \
311 VLISTCHECK((fun), (vp), "rage"); \
312 TAILQ_REMOVE(&vnode_rage_list, (vp), v_freelist); \
313 VLISTNONE((vp)); \
314 vp->v_listflag &= ~VLIST_RAGE; \
315 ragevnodes--; \
316 } while(0)
317
318
319 /*
320 * vnodetarget hasn't been used in a long time, but
321 * it was exported for some reason... I'm leaving in
322 * place for now... it should be deprecated out of the
323 * exports and removed eventually.
324 */
325 u_int32_t vnodetarget; /* target for vnreclaim() */
326 #define VNODE_FREE_TARGET 20 /* Default value for vnodetarget */
327
328 /*
329 * We need quite a few vnodes on the free list to sustain the
330 * rapid stat() the compilation process does, and still benefit from the name
331 * cache. Having too few vnodes on the free list causes serious disk
332 * thrashing as we cycle through them.
333 */
334 #define VNODE_FREE_MIN CONFIG_VNODE_FREE_MIN /* freelist should have at least this many */
335
336
337 static void async_work_continue(void);
338
339 /*
340 * Initialize the vnode management data structures.
341 */
342 __private_extern__ void
343 vntblinit(void)
344 {
345 thread_t thread = THREAD_NULL;
346
347 TAILQ_INIT(&vnode_free_list);
348 TAILQ_INIT(&vnode_rage_list);
349 TAILQ_INIT(&vnode_dead_list);
350 TAILQ_INIT(&vnode_async_work_list);
351 TAILQ_INIT(&mountlist);
352
353 if (!vnodetarget)
354 vnodetarget = VNODE_FREE_TARGET;
355
356 microuptime(&rage_tv);
357 rage_limit = desiredvnodes / 100;
358
359 if (rage_limit < RAGE_LIMIT_MIN)
360 rage_limit = RAGE_LIMIT_MIN;
361
362 /*
363 * Scale the vm_object_cache to accomodate the vnodes
364 * we want to cache
365 */
366 (void) adjust_vm_object_cache(0, desiredvnodes - VNODE_FREE_MIN);
367
368 /*
369 * create worker threads
370 */
371 kernel_thread_start((thread_continue_t)async_work_continue, NULL, &thread);
372 thread_deallocate(thread);
373 }
374
375 /* Reset the VM Object Cache with the values passed in */
376 __private_extern__ kern_return_t
377 reset_vmobjectcache(unsigned int val1, unsigned int val2)
378 {
379 vm_size_t oval = val1 - VNODE_FREE_MIN;
380 vm_size_t nval;
381
382 if (val1 == val2) {
383 return KERN_SUCCESS;
384 }
385
386 if(val2 < VNODE_FREE_MIN)
387 nval = 0;
388 else
389 nval = val2 - VNODE_FREE_MIN;
390
391 return(adjust_vm_object_cache(oval, nval));
392 }
393
394
395 /* the timeout is in 10 msecs */
396 int
397 vnode_waitforwrites(vnode_t vp, int output_target, int slpflag, int slptimeout, const char *msg) {
398 int error = 0;
399 struct timespec ts;
400
401 KERNEL_DEBUG(0x3010280 | DBG_FUNC_START, (int)vp, output_target, vp->v_numoutput, 0, 0);
402
403 if (vp->v_numoutput > output_target) {
404
405 slpflag |= PDROP;
406
407 vnode_lock_spin(vp);
408
409 while ((vp->v_numoutput > output_target) && error == 0) {
410 if (output_target)
411 vp->v_flag |= VTHROTTLED;
412 else
413 vp->v_flag |= VBWAIT;
414
415 ts.tv_sec = (slptimeout/100);
416 ts.tv_nsec = (slptimeout % 1000) * 10 * NSEC_PER_USEC * 1000 ;
417 error = msleep((caddr_t)&vp->v_numoutput, &vp->v_lock, (slpflag | (PRIBIO + 1)), msg, &ts);
418
419 vnode_lock_spin(vp);
420 }
421 vnode_unlock(vp);
422 }
423 KERNEL_DEBUG(0x3010280 | DBG_FUNC_END, (int)vp, output_target, vp->v_numoutput, error, 0);
424
425 return error;
426 }
427
428
429 void
430 vnode_startwrite(vnode_t vp) {
431
432 OSAddAtomic(1, &vp->v_numoutput);
433 }
434
435
436 void
437 vnode_writedone(vnode_t vp)
438 {
439 if (vp) {
440 int need_wakeup = 0;
441
442 OSAddAtomic(-1, &vp->v_numoutput);
443
444 vnode_lock_spin(vp);
445
446 if (vp->v_numoutput < 0)
447 panic("vnode_writedone: numoutput < 0");
448
449 if ((vp->v_flag & VTHROTTLED)) {
450 vp->v_flag &= ~VTHROTTLED;
451 need_wakeup = 1;
452 }
453 if ((vp->v_flag & VBWAIT) && (vp->v_numoutput == 0)) {
454 vp->v_flag &= ~VBWAIT;
455 need_wakeup = 1;
456 }
457 vnode_unlock(vp);
458
459 if (need_wakeup)
460 wakeup((caddr_t)&vp->v_numoutput);
461 }
462 }
463
464
465
466 int
467 vnode_hasdirtyblks(vnode_t vp)
468 {
469 struct cl_writebehind *wbp;
470
471 /*
472 * Not taking the buf_mtxp as there is little
473 * point doing it. Even if the lock is taken the
474 * state can change right after that. If their
475 * needs to be a synchronization, it must be driven
476 * by the caller
477 */
478 if (vp->v_dirtyblkhd.lh_first)
479 return (1);
480
481 if (!UBCINFOEXISTS(vp))
482 return (0);
483
484 wbp = vp->v_ubcinfo->cl_wbehind;
485
486 if (wbp && (wbp->cl_number || wbp->cl_scmap))
487 return (1);
488
489 return (0);
490 }
491
492 int
493 vnode_hascleanblks(vnode_t vp)
494 {
495 /*
496 * Not taking the buf_mtxp as there is little
497 * point doing it. Even if the lock is taken the
498 * state can change right after that. If their
499 * needs to be a synchronization, it must be driven
500 * by the caller
501 */
502 if (vp->v_cleanblkhd.lh_first)
503 return (1);
504 return (0);
505 }
506
507 void
508 vnode_iterate_setup(mount_t mp)
509 {
510 mp->mnt_lflag |= MNT_LITER;
511 }
512
513 int
514 vnode_umount_preflight(mount_t mp, vnode_t skipvp, int flags)
515 {
516 vnode_t vp;
517
518 TAILQ_FOREACH(vp, &mp->mnt_vnodelist, v_mntvnodes) {
519 if (vp->v_type == VDIR)
520 continue;
521 if (vp == skipvp)
522 continue;
523 if ((flags & SKIPSYSTEM) && ((vp->v_flag & VSYSTEM) || (vp->v_flag & VNOFLUSH)))
524 continue;
525 if ((flags & SKIPSWAP) && (vp->v_flag & VSWAP))
526 continue;
527 if ((flags & WRITECLOSE) && (vp->v_writecount == 0 || vp->v_type != VREG))
528 continue;
529
530 /* Look for busy vnode */
531 if ((vp->v_usecount != 0) && ((vp->v_usecount - vp->v_kusecount) != 0)) {
532 return 1;
533
534 } else if (vp->v_iocount > 0) {
535 /* Busy if iocount is > 0 for more than 3 seconds */
536 tsleep(&vp->v_iocount, PVFS, "vnode_drain_network", 3 * hz);
537 if (vp->v_iocount > 0)
538 return 1;
539 continue;
540 }
541 }
542
543 return 0;
544 }
545
546 /*
547 * This routine prepares iteration by moving all the vnodes to worker queue
548 * called with mount lock held
549 */
550 int
551 vnode_iterate_prepare(mount_t mp)
552 {
553 vnode_t vp;
554
555 if (TAILQ_EMPTY(&mp->mnt_vnodelist)) {
556 /* nothing to do */
557 return (0);
558 }
559
560 vp = TAILQ_FIRST(&mp->mnt_vnodelist);
561 vp->v_mntvnodes.tqe_prev = &(mp->mnt_workerqueue.tqh_first);
562 mp->mnt_workerqueue.tqh_first = mp->mnt_vnodelist.tqh_first;
563 mp->mnt_workerqueue.tqh_last = mp->mnt_vnodelist.tqh_last;
564
565 TAILQ_INIT(&mp->mnt_vnodelist);
566 if (mp->mnt_newvnodes.tqh_first != NULL)
567 panic("vnode_iterate_prepare: newvnode when entering vnode");
568 TAILQ_INIT(&mp->mnt_newvnodes);
569
570 return (1);
571 }
572
573
574 /* called with mount lock held */
575 int
576 vnode_iterate_reloadq(mount_t mp)
577 {
578 int moved = 0;
579
580 /* add the remaining entries in workerq to the end of mount vnode list */
581 if (!TAILQ_EMPTY(&mp->mnt_workerqueue)) {
582 struct vnode * mvp;
583 mvp = TAILQ_LAST(&mp->mnt_vnodelist, vnodelst);
584
585 /* Joining the workerque entities to mount vnode list */
586 if (mvp)
587 mvp->v_mntvnodes.tqe_next = mp->mnt_workerqueue.tqh_first;
588 else
589 mp->mnt_vnodelist.tqh_first = mp->mnt_workerqueue.tqh_first;
590 mp->mnt_workerqueue.tqh_first->v_mntvnodes.tqe_prev = mp->mnt_vnodelist.tqh_last;
591 mp->mnt_vnodelist.tqh_last = mp->mnt_workerqueue.tqh_last;
592 TAILQ_INIT(&mp->mnt_workerqueue);
593 }
594
595 /* add the newvnodes to the head of mount vnode list */
596 if (!TAILQ_EMPTY(&mp->mnt_newvnodes)) {
597 struct vnode * nlvp;
598 nlvp = TAILQ_LAST(&mp->mnt_newvnodes, vnodelst);
599
600 mp->mnt_newvnodes.tqh_first->v_mntvnodes.tqe_prev = &mp->mnt_vnodelist.tqh_first;
601 nlvp->v_mntvnodes.tqe_next = mp->mnt_vnodelist.tqh_first;
602 if(mp->mnt_vnodelist.tqh_first)
603 mp->mnt_vnodelist.tqh_first->v_mntvnodes.tqe_prev = &nlvp->v_mntvnodes.tqe_next;
604 else
605 mp->mnt_vnodelist.tqh_last = mp->mnt_newvnodes.tqh_last;
606 mp->mnt_vnodelist.tqh_first = mp->mnt_newvnodes.tqh_first;
607 TAILQ_INIT(&mp->mnt_newvnodes);
608 moved = 1;
609 }
610
611 return(moved);
612 }
613
614
615 void
616 vnode_iterate_clear(mount_t mp)
617 {
618 mp->mnt_lflag &= ~MNT_LITER;
619 }
620
621
622 #include <i386/panic_hooks.h>
623
624 struct vnode_iterate_panic_hook {
625 panic_hook_t hook;
626 mount_t mp;
627 struct vnode *vp;
628 };
629
630 static void vnode_iterate_panic_hook(panic_hook_t *hook_)
631 {
632 extern int kdb_log(const char *fmt, ...);
633 struct vnode_iterate_panic_hook *hook = (struct vnode_iterate_panic_hook *)hook_;
634 panic_phys_range_t range;
635 uint64_t phys;
636
637 if (panic_phys_range_before(hook->mp, &phys, &range)) {
638 kdb_log("mp = %p, phys = %p, prev (%p: %p-%p)\n",
639 hook->mp, phys, range.type, range.phys_start,
640 range.phys_start + range.len);
641 } else {
642 kdb_log("mp = %p, phys = %p, prev (!)\n", hook->mp, phys);
643 }
644
645 if (panic_phys_range_before(hook->vp, &phys, &range)) {
646 kdb_log("vp = %p, phys = %p, prev (%p: %p-%p)\n",
647 hook->vp, phys, range.type, range.phys_start,
648 range.phys_start + range.len);
649 } else {
650 kdb_log("vp = %p, phys = %p, prev (!)\n", hook->vp, phys);
651 }
652 panic_dump_mem((void *)(((vm_offset_t)hook->mp -4096) & ~4095), 12288);
653 }
654
655 int
656 vnode_iterate(mount_t mp, int flags, int (*callout)(struct vnode *, void *),
657 void *arg)
658 {
659 struct vnode *vp;
660 int vid, retval;
661 int ret = 0;
662
663 /*
664 * The mount iterate mutex is held for the duration of the iteration.
665 * This can be done by a state flag on the mount structure but we can
666 * run into priority inversion issues sometimes.
667 * Using a mutex allows us to benefit from the priority donation
668 * mechanisms in the kernel for locks. This mutex should never be
669 * acquired in spin mode and it should be acquired before attempting to
670 * acquire the mount lock.
671 */
672 mount_iterate_lock(mp);
673
674 mount_lock(mp);
675
676 vnode_iterate_setup(mp);
677
678 /* If it returns 0 then there is nothing to do */
679 retval = vnode_iterate_prepare(mp);
680
681 if (retval == 0) {
682 vnode_iterate_clear(mp);
683 mount_unlock(mp);
684 mount_iterate_unlock(mp);
685 return(ret);
686 }
687
688 struct vnode_iterate_panic_hook hook;
689 hook.mp = mp;
690 hook.vp = NULL;
691 panic_hook(&hook.hook, vnode_iterate_panic_hook);
692 /* iterate over all the vnodes */
693 while (!TAILQ_EMPTY(&mp->mnt_workerqueue)) {
694 vp = TAILQ_FIRST(&mp->mnt_workerqueue);
695 hook.vp = vp;
696 TAILQ_REMOVE(&mp->mnt_workerqueue, vp, v_mntvnodes);
697 TAILQ_INSERT_TAIL(&mp->mnt_vnodelist, vp, v_mntvnodes);
698 vid = vp->v_id;
699 if ((vp->v_data == NULL) || (vp->v_type == VNON) || (vp->v_mount != mp)) {
700 continue;
701 }
702 mount_unlock(mp);
703
704 if ( vget_internal(vp, vid, (flags | VNODE_NODEAD| VNODE_WITHID | VNODE_NOSUSPEND))) {
705 mount_lock(mp);
706 continue;
707 }
708 if (flags & VNODE_RELOAD) {
709 /*
710 * we're reloading the filesystem
711 * cast out any inactive vnodes...
712 */
713 if (vnode_reload(vp)) {
714 /* vnode will be recycled on the refcount drop */
715 vnode_put(vp);
716 mount_lock(mp);
717 continue;
718 }
719 }
720
721 retval = callout(vp, arg);
722
723 switch (retval) {
724 case VNODE_RETURNED:
725 case VNODE_RETURNED_DONE:
726 vnode_put(vp);
727 if (retval == VNODE_RETURNED_DONE) {
728 mount_lock(mp);
729 ret = 0;
730 goto out;
731 }
732 break;
733
734 case VNODE_CLAIMED_DONE:
735 mount_lock(mp);
736 ret = 0;
737 goto out;
738 case VNODE_CLAIMED:
739 default:
740 break;
741 }
742 mount_lock(mp);
743 }
744
745 out:
746 panic_unhook(&hook.hook);
747 (void)vnode_iterate_reloadq(mp);
748 vnode_iterate_clear(mp);
749 mount_unlock(mp);
750 mount_iterate_unlock(mp);
751 return (ret);
752 }
753
754 void
755 mount_lock_renames(mount_t mp)
756 {
757 lck_mtx_lock(&mp->mnt_renamelock);
758 }
759
760 void
761 mount_unlock_renames(mount_t mp)
762 {
763 lck_mtx_unlock(&mp->mnt_renamelock);
764 }
765
766 void
767 mount_iterate_lock(mount_t mp)
768 {
769 lck_mtx_lock(&mp->mnt_iter_lock);
770 }
771
772 void
773 mount_iterate_unlock(mount_t mp)
774 {
775 lck_mtx_unlock(&mp->mnt_iter_lock);
776 }
777
778 void
779 mount_lock(mount_t mp)
780 {
781 lck_mtx_lock(&mp->mnt_mlock);
782 }
783
784 void
785 mount_lock_spin(mount_t mp)
786 {
787 lck_mtx_lock_spin(&mp->mnt_mlock);
788 }
789
790 void
791 mount_unlock(mount_t mp)
792 {
793 lck_mtx_unlock(&mp->mnt_mlock);
794 }
795
796
797 void
798 mount_ref(mount_t mp, int locked)
799 {
800 if ( !locked)
801 mount_lock_spin(mp);
802
803 mp->mnt_count++;
804
805 if ( !locked)
806 mount_unlock(mp);
807 }
808
809
810 void
811 mount_drop(mount_t mp, int locked)
812 {
813 if ( !locked)
814 mount_lock_spin(mp);
815
816 mp->mnt_count--;
817
818 if (mp->mnt_count == 0 && (mp->mnt_lflag & MNT_LDRAIN))
819 wakeup(&mp->mnt_lflag);
820
821 if ( !locked)
822 mount_unlock(mp);
823 }
824
825
826 int
827 mount_iterref(mount_t mp, int locked)
828 {
829 int retval = 0;
830
831 if (!locked)
832 mount_list_lock();
833 if (mp->mnt_iterref < 0) {
834 retval = 1;
835 } else {
836 mp->mnt_iterref++;
837 }
838 if (!locked)
839 mount_list_unlock();
840 return(retval);
841 }
842
843 int
844 mount_isdrained(mount_t mp, int locked)
845 {
846 int retval;
847
848 if (!locked)
849 mount_list_lock();
850 if (mp->mnt_iterref < 0)
851 retval = 1;
852 else
853 retval = 0;
854 if (!locked)
855 mount_list_unlock();
856 return(retval);
857 }
858
859 void
860 mount_iterdrop(mount_t mp)
861 {
862 mount_list_lock();
863 mp->mnt_iterref--;
864 wakeup(&mp->mnt_iterref);
865 mount_list_unlock();
866 }
867
868 void
869 mount_iterdrain(mount_t mp)
870 {
871 mount_list_lock();
872 while (mp->mnt_iterref)
873 msleep((caddr_t)&mp->mnt_iterref, mnt_list_mtx_lock, PVFS, "mount_iterdrain", NULL);
874 /* mount iterations drained */
875 mp->mnt_iterref = -1;
876 mount_list_unlock();
877 }
878 void
879 mount_iterreset(mount_t mp)
880 {
881 mount_list_lock();
882 if (mp->mnt_iterref == -1)
883 mp->mnt_iterref = 0;
884 mount_list_unlock();
885 }
886
887 /* always called with mount lock held */
888 int
889 mount_refdrain(mount_t mp)
890 {
891 if (mp->mnt_lflag & MNT_LDRAIN)
892 panic("already in drain");
893 mp->mnt_lflag |= MNT_LDRAIN;
894
895 while (mp->mnt_count)
896 msleep((caddr_t)&mp->mnt_lflag, &mp->mnt_mlock, PVFS, "mount_drain", NULL);
897
898 if (mp->mnt_vnodelist.tqh_first != NULL)
899 panic("mount_refdrain: dangling vnode");
900
901 mp->mnt_lflag &= ~MNT_LDRAIN;
902
903 return(0);
904 }
905
906 /* Tags the mount point as not supportine extended readdir for NFS exports */
907 void
908 mount_set_noreaddirext(mount_t mp) {
909 mount_lock (mp);
910 mp->mnt_kern_flag |= MNTK_DENY_READDIREXT;
911 mount_unlock (mp);
912 }
913
914 /*
915 * Mark a mount point as busy. Used to synchronize access and to delay
916 * unmounting.
917 */
918 int
919 vfs_busy(mount_t mp, int flags)
920 {
921
922 restart:
923 if (mp->mnt_lflag & MNT_LDEAD)
924 return (ENOENT);
925
926 mount_lock(mp);
927
928 if (mp->mnt_lflag & MNT_LUNMOUNT) {
929 if (flags & LK_NOWAIT || mp->mnt_lflag & MNT_LDEAD) {
930 mount_unlock(mp);
931 return (ENOENT);
932 }
933
934 /*
935 * Since all busy locks are shared except the exclusive
936 * lock granted when unmounting, the only place that a
937 * wakeup needs to be done is at the release of the
938 * exclusive lock at the end of dounmount.
939 */
940 mp->mnt_lflag |= MNT_LWAIT;
941 msleep((caddr_t)mp, &mp->mnt_mlock, (PVFS | PDROP), "vfsbusy", NULL);
942 return (ENOENT);
943 }
944
945 mount_unlock(mp);
946
947 lck_rw_lock_shared(&mp->mnt_rwlock);
948
949 /*
950 * Until we are granted the rwlock, it's possible for the mount point to
951 * change state, so re-evaluate before granting the vfs_busy.
952 */
953 if (mp->mnt_lflag & (MNT_LDEAD | MNT_LUNMOUNT)) {
954 lck_rw_done(&mp->mnt_rwlock);
955 goto restart;
956 }
957 return (0);
958 }
959
960 /*
961 * Free a busy filesystem.
962 */
963 void
964 vfs_unbusy(mount_t mp)
965 {
966 lck_rw_done(&mp->mnt_rwlock);
967 }
968
969
970
971 static void
972 vfs_rootmountfailed(mount_t mp) {
973
974 mount_list_lock();
975 mp->mnt_vtable->vfc_refcount--;
976 mount_list_unlock();
977
978 vfs_unbusy(mp);
979
980 mount_lock_destroy(mp);
981
982 #if CONFIG_MACF
983 mac_mount_label_destroy(mp);
984 #endif
985
986 FREE_ZONE(mp, sizeof(struct mount), M_MOUNT);
987 }
988
989 /*
990 * Lookup a filesystem type, and if found allocate and initialize
991 * a mount structure for it.
992 *
993 * Devname is usually updated by mount(8) after booting.
994 */
995 static mount_t
996 vfs_rootmountalloc_internal(struct vfstable *vfsp, const char *devname)
997 {
998 mount_t mp;
999
1000 mp = _MALLOC_ZONE(sizeof(struct mount), M_MOUNT, M_WAITOK);
1001 bzero((char *)mp, sizeof(struct mount));
1002
1003 /* Initialize the default IO constraints */
1004 mp->mnt_maxreadcnt = mp->mnt_maxwritecnt = MAXPHYS;
1005 mp->mnt_segreadcnt = mp->mnt_segwritecnt = 32;
1006 mp->mnt_maxsegreadsize = mp->mnt_maxreadcnt;
1007 mp->mnt_maxsegwritesize = mp->mnt_maxwritecnt;
1008 mp->mnt_devblocksize = DEV_BSIZE;
1009 mp->mnt_alignmentmask = PAGE_MASK;
1010 mp->mnt_ioqueue_depth = MNT_DEFAULT_IOQUEUE_DEPTH;
1011 mp->mnt_ioscale = 1;
1012 mp->mnt_ioflags = 0;
1013 mp->mnt_realrootvp = NULLVP;
1014 mp->mnt_authcache_ttl = CACHED_LOOKUP_RIGHT_TTL;
1015 mp->mnt_throttle_mask = LOWPRI_MAX_NUM_DEV - 1;
1016 mp->mnt_devbsdunit = 0;
1017
1018 mount_lock_init(mp);
1019 (void)vfs_busy(mp, LK_NOWAIT);
1020
1021 TAILQ_INIT(&mp->mnt_vnodelist);
1022 TAILQ_INIT(&mp->mnt_workerqueue);
1023 TAILQ_INIT(&mp->mnt_newvnodes);
1024
1025 mp->mnt_vtable = vfsp;
1026 mp->mnt_op = vfsp->vfc_vfsops;
1027 mp->mnt_flag = MNT_RDONLY | MNT_ROOTFS;
1028 mp->mnt_vnodecovered = NULLVP;
1029 //mp->mnt_stat.f_type = vfsp->vfc_typenum;
1030 mp->mnt_flag |= vfsp->vfc_flags & MNT_VISFLAGMASK;
1031
1032 mount_list_lock();
1033 vfsp->vfc_refcount++;
1034 mount_list_unlock();
1035
1036 strlcpy(mp->mnt_vfsstat.f_fstypename, vfsp->vfc_name, MFSTYPENAMELEN);
1037 mp->mnt_vfsstat.f_mntonname[0] = '/';
1038 /* XXX const poisoning layering violation */
1039 (void) copystr((const void *)devname, mp->mnt_vfsstat.f_mntfromname, MAXPATHLEN - 1, NULL);
1040
1041 #if CONFIG_MACF
1042 mac_mount_label_init(mp);
1043 mac_mount_label_associate(vfs_context_kernel(), mp);
1044 #endif
1045 return (mp);
1046 }
1047
1048 errno_t
1049 vfs_rootmountalloc(const char *fstypename, const char *devname, mount_t *mpp)
1050 {
1051 struct vfstable *vfsp;
1052
1053 for (vfsp = vfsconf; vfsp; vfsp = vfsp->vfc_next)
1054 if (!strncmp(vfsp->vfc_name, fstypename,
1055 sizeof(vfsp->vfc_name)))
1056 break;
1057 if (vfsp == NULL)
1058 return (ENODEV);
1059
1060 *mpp = vfs_rootmountalloc_internal(vfsp, devname);
1061
1062 if (*mpp)
1063 return (0);
1064
1065 return (ENOMEM);
1066 }
1067
1068
1069 /*
1070 * Find an appropriate filesystem to use for the root. If a filesystem
1071 * has not been preselected, walk through the list of known filesystems
1072 * trying those that have mountroot routines, and try them until one
1073 * works or we have tried them all.
1074 */
1075 extern int (*mountroot)(void);
1076
1077 int
1078 vfs_mountroot(void)
1079 {
1080 #if CONFIG_MACF
1081 struct vnode *vp;
1082 #endif
1083 struct vfstable *vfsp;
1084 vfs_context_t ctx = vfs_context_kernel();
1085 struct vfs_attr vfsattr;
1086 int error;
1087 mount_t mp;
1088 vnode_t bdevvp_rootvp;
1089
1090 if (mountroot != NULL) {
1091 /*
1092 * used for netboot which follows a different set of rules
1093 */
1094 error = (*mountroot)();
1095 return (error);
1096 }
1097 if ((error = bdevvp(rootdev, &rootvp))) {
1098 printf("vfs_mountroot: can't setup bdevvp\n");
1099 return (error);
1100 }
1101 /*
1102 * 4951998 - code we call in vfc_mountroot may replace rootvp
1103 * so keep a local copy for some house keeping.
1104 */
1105 bdevvp_rootvp = rootvp;
1106
1107 for (vfsp = vfsconf; vfsp; vfsp = vfsp->vfc_next) {
1108 if (vfsp->vfc_mountroot == NULL
1109 && !ISSET(vfsp->vfc_vfsflags, VFC_VFSCANMOUNTROOT)) {
1110 continue;
1111 }
1112
1113 mp = vfs_rootmountalloc_internal(vfsp, "root_device");
1114 mp->mnt_devvp = rootvp;
1115
1116 if (vfsp->vfc_mountroot)
1117 error = (*vfsp->vfc_mountroot)(mp, rootvp, ctx);
1118 else
1119 error = VFS_MOUNT(mp, rootvp, 0, ctx);
1120
1121 if (!error) {
1122 if ( bdevvp_rootvp != rootvp ) {
1123 /*
1124 * rootvp changed...
1125 * bump the iocount and fix up mnt_devvp for the
1126 * new rootvp (it will already have a usecount taken)...
1127 * drop the iocount and the usecount on the orignal
1128 * since we are no longer going to use it...
1129 */
1130 vnode_getwithref(rootvp);
1131 mp->mnt_devvp = rootvp;
1132
1133 vnode_rele(bdevvp_rootvp);
1134 vnode_put(bdevvp_rootvp);
1135 }
1136 mp->mnt_devvp->v_specflags |= SI_MOUNTEDON;
1137
1138 vfs_unbusy(mp);
1139
1140 mount_list_add(mp);
1141
1142 /*
1143 * cache the IO attributes for the underlying physical media...
1144 * an error return indicates the underlying driver doesn't
1145 * support all the queries necessary... however, reasonable
1146 * defaults will have been set, so no reason to bail or care
1147 */
1148 vfs_init_io_attributes(rootvp, mp);
1149
1150 if (mp->mnt_ioflags & MNT_IOFLAGS_FUSION_DRIVE) {
1151 root_is_CF_drive = TRUE;
1152 }
1153
1154 /*
1155 * Shadow the VFC_VFSNATIVEXATTR flag to MNTK_EXTENDED_ATTRS.
1156 */
1157 if (mp->mnt_vtable->vfc_vfsflags & VFC_VFSNATIVEXATTR) {
1158 mp->mnt_kern_flag |= MNTK_EXTENDED_ATTRS;
1159 }
1160 if (mp->mnt_vtable->vfc_vfsflags & VFC_VFSPREFLIGHT) {
1161 mp->mnt_kern_flag |= MNTK_UNMOUNT_PREFLIGHT;
1162 }
1163
1164 uint32_t speed;
1165
1166 if (MNTK_VIRTUALDEV & mp->mnt_kern_flag) speed = 128;
1167 else if (MNTK_SSD & mp->mnt_kern_flag) speed = 7*256;
1168 else speed = 256;
1169 vc_progress_setdiskspeed(speed);
1170 /*
1171 * Probe root file system for additional features.
1172 */
1173 (void)VFS_START(mp, 0, ctx);
1174
1175 VFSATTR_INIT(&vfsattr);
1176 VFSATTR_WANTED(&vfsattr, f_capabilities);
1177 if (vfs_getattr(mp, &vfsattr, ctx) == 0 &&
1178 VFSATTR_IS_SUPPORTED(&vfsattr, f_capabilities)) {
1179 if ((vfsattr.f_capabilities.capabilities[VOL_CAPABILITIES_INTERFACES] & VOL_CAP_INT_EXTENDED_ATTR) &&
1180 (vfsattr.f_capabilities.valid[VOL_CAPABILITIES_INTERFACES] & VOL_CAP_INT_EXTENDED_ATTR)) {
1181 mp->mnt_kern_flag |= MNTK_EXTENDED_ATTRS;
1182 }
1183 #if NAMEDSTREAMS
1184 if ((vfsattr.f_capabilities.capabilities[VOL_CAPABILITIES_INTERFACES] & VOL_CAP_INT_NAMEDSTREAMS) &&
1185 (vfsattr.f_capabilities.valid[VOL_CAPABILITIES_INTERFACES] & VOL_CAP_INT_NAMEDSTREAMS)) {
1186 mp->mnt_kern_flag |= MNTK_NAMED_STREAMS;
1187 }
1188 #endif
1189 if ((vfsattr.f_capabilities.capabilities[VOL_CAPABILITIES_FORMAT] & VOL_CAP_FMT_PATH_FROM_ID) &&
1190 (vfsattr.f_capabilities.valid[VOL_CAPABILITIES_FORMAT] & VOL_CAP_FMT_PATH_FROM_ID)) {
1191 mp->mnt_kern_flag |= MNTK_PATH_FROM_ID;
1192 }
1193
1194 if ((vfsattr.f_capabilities.capabilities[VOL_CAPABILITIES_FORMAT] & VOL_CAP_FMT_DIR_HARDLINKS) &&
1195 (vfsattr.f_capabilities.valid[VOL_CAPABILITIES_FORMAT] & VOL_CAP_FMT_DIR_HARDLINKS)) {
1196 mp->mnt_kern_flag |= MNTK_DIR_HARDLINKS;
1197 }
1198 }
1199
1200 /*
1201 * get rid of iocount reference returned
1202 * by bdevvp (or picked up by us on the substitued
1203 * rootvp)... it (or we) will have also taken
1204 * a usecount reference which we want to keep
1205 */
1206 vnode_put(rootvp);
1207
1208 #if CONFIG_MACF
1209 if ((vfs_flags(mp) & MNT_MULTILABEL) == 0)
1210 return (0);
1211
1212 error = VFS_ROOT(mp, &vp, ctx);
1213 if (error) {
1214 printf("%s() VFS_ROOT() returned %d\n",
1215 __func__, error);
1216 dounmount(mp, MNT_FORCE, 0, ctx);
1217 goto fail;
1218 }
1219 error = vnode_label(mp, NULL, vp, NULL, 0, ctx);
1220 /*
1221 * get rid of reference provided by VFS_ROOT
1222 */
1223 vnode_put(vp);
1224
1225 if (error) {
1226 printf("%s() vnode_label() returned %d\n",
1227 __func__, error);
1228 dounmount(mp, MNT_FORCE, 0, ctx);
1229 goto fail;
1230 }
1231 #endif
1232 return (0);
1233 }
1234 #if CONFIG_MACF
1235 fail:
1236 #endif
1237 vfs_rootmountfailed(mp);
1238
1239 if (error != EINVAL)
1240 printf("%s_mountroot failed: %d\n", vfsp->vfc_name, error);
1241 }
1242 return (ENODEV);
1243 }
1244
1245 /*
1246 * Lookup a mount point by filesystem identifier.
1247 */
1248
1249 struct mount *
1250 vfs_getvfs(fsid_t *fsid)
1251 {
1252 return (mount_list_lookupby_fsid(fsid, 0, 0));
1253 }
1254
1255 static struct mount *
1256 vfs_getvfs_locked(fsid_t *fsid)
1257 {
1258 return(mount_list_lookupby_fsid(fsid, 1, 0));
1259 }
1260
1261 struct mount *
1262 vfs_getvfs_by_mntonname(char *path)
1263 {
1264 mount_t retmp = (mount_t)0;
1265 mount_t mp;
1266
1267 mount_list_lock();
1268 TAILQ_FOREACH(mp, &mountlist, mnt_list) {
1269 if (!strncmp(mp->mnt_vfsstat.f_mntonname, path,
1270 sizeof(mp->mnt_vfsstat.f_mntonname))) {
1271 retmp = mp;
1272 if (mount_iterref(retmp, 1))
1273 retmp = NULL;
1274 goto out;
1275 }
1276 }
1277 out:
1278 mount_list_unlock();
1279 return (retmp);
1280 }
1281
1282 /* generation number for creation of new fsids */
1283 u_short mntid_gen = 0;
1284 /*
1285 * Get a new unique fsid
1286 */
1287 void
1288 vfs_getnewfsid(struct mount *mp)
1289 {
1290
1291 fsid_t tfsid;
1292 int mtype;
1293
1294 mount_list_lock();
1295
1296 /* generate a new fsid */
1297 mtype = mp->mnt_vtable->vfc_typenum;
1298 if (++mntid_gen == 0)
1299 mntid_gen++;
1300 tfsid.val[0] = makedev(nblkdev + mtype, mntid_gen);
1301 tfsid.val[1] = mtype;
1302
1303 while (vfs_getvfs_locked(&tfsid)) {
1304 if (++mntid_gen == 0)
1305 mntid_gen++;
1306 tfsid.val[0] = makedev(nblkdev + mtype, mntid_gen);
1307 }
1308
1309 mp->mnt_vfsstat.f_fsid.val[0] = tfsid.val[0];
1310 mp->mnt_vfsstat.f_fsid.val[1] = tfsid.val[1];
1311 mount_list_unlock();
1312 }
1313
1314 /*
1315 * Routines having to do with the management of the vnode table.
1316 */
1317 extern int (**dead_vnodeop_p)(void *);
1318 long numvnodes, freevnodes, deadvnodes, async_work_vnodes;
1319
1320
1321 int async_work_timed_out = 0;
1322 int async_work_handled = 0;
1323 int dead_vnode_wanted = 0;
1324 int dead_vnode_waited = 0;
1325
1326 /*
1327 * Move a vnode from one mount queue to another.
1328 */
1329 static void
1330 insmntque(vnode_t vp, mount_t mp)
1331 {
1332 mount_t lmp;
1333 /*
1334 * Delete from old mount point vnode list, if on one.
1335 */
1336 if ( (lmp = vp->v_mount) != NULL && lmp != dead_mountp) {
1337 if ((vp->v_lflag & VNAMED_MOUNT) == 0)
1338 panic("insmntque: vp not in mount vnode list");
1339 vp->v_lflag &= ~VNAMED_MOUNT;
1340
1341 mount_lock_spin(lmp);
1342
1343 mount_drop(lmp, 1);
1344
1345 if (vp->v_mntvnodes.tqe_next == NULL) {
1346 if (TAILQ_LAST(&lmp->mnt_vnodelist, vnodelst) == vp)
1347 TAILQ_REMOVE(&lmp->mnt_vnodelist, vp, v_mntvnodes);
1348 else if (TAILQ_LAST(&lmp->mnt_newvnodes, vnodelst) == vp)
1349 TAILQ_REMOVE(&lmp->mnt_newvnodes, vp, v_mntvnodes);
1350 else if (TAILQ_LAST(&lmp->mnt_workerqueue, vnodelst) == vp)
1351 TAILQ_REMOVE(&lmp->mnt_workerqueue, vp, v_mntvnodes);
1352 } else {
1353 vp->v_mntvnodes.tqe_next->v_mntvnodes.tqe_prev = vp->v_mntvnodes.tqe_prev;
1354 *vp->v_mntvnodes.tqe_prev = vp->v_mntvnodes.tqe_next;
1355 }
1356 vp->v_mntvnodes.tqe_next = NULL;
1357 vp->v_mntvnodes.tqe_prev = NULL;
1358 mount_unlock(lmp);
1359 return;
1360 }
1361
1362 /*
1363 * Insert into list of vnodes for the new mount point, if available.
1364 */
1365 if ((vp->v_mount = mp) != NULL) {
1366 mount_lock_spin(mp);
1367 if ((vp->v_mntvnodes.tqe_next != 0) && (vp->v_mntvnodes.tqe_prev != 0))
1368 panic("vp already in mount list");
1369 if (mp->mnt_lflag & MNT_LITER)
1370 TAILQ_INSERT_HEAD(&mp->mnt_newvnodes, vp, v_mntvnodes);
1371 else
1372 TAILQ_INSERT_HEAD(&mp->mnt_vnodelist, vp, v_mntvnodes);
1373 if (vp->v_lflag & VNAMED_MOUNT)
1374 panic("insmntque: vp already in mount vnode list");
1375 vp->v_lflag |= VNAMED_MOUNT;
1376 mount_ref(mp, 1);
1377 mount_unlock(mp);
1378 }
1379 }
1380
1381
1382 /*
1383 * Create a vnode for a block device.
1384 * Used for root filesystem, argdev, and swap areas.
1385 * Also used for memory file system special devices.
1386 */
1387 int
1388 bdevvp(dev_t dev, vnode_t *vpp)
1389 {
1390 vnode_t nvp;
1391 int error;
1392 struct vnode_fsparam vfsp;
1393 struct vfs_context context;
1394
1395 if (dev == NODEV) {
1396 *vpp = NULLVP;
1397 return (ENODEV);
1398 }
1399
1400 context.vc_thread = current_thread();
1401 context.vc_ucred = FSCRED;
1402
1403 vfsp.vnfs_mp = (struct mount *)0;
1404 vfsp.vnfs_vtype = VBLK;
1405 vfsp.vnfs_str = "bdevvp";
1406 vfsp.vnfs_dvp = NULL;
1407 vfsp.vnfs_fsnode = NULL;
1408 vfsp.vnfs_cnp = NULL;
1409 vfsp.vnfs_vops = spec_vnodeop_p;
1410 vfsp.vnfs_rdev = dev;
1411 vfsp.vnfs_filesize = 0;
1412
1413 vfsp.vnfs_flags = VNFS_NOCACHE | VNFS_CANTCACHE;
1414
1415 vfsp.vnfs_marksystem = 0;
1416 vfsp.vnfs_markroot = 0;
1417
1418 if ( (error = vnode_create(VNCREATE_FLAVOR, VCREATESIZE, &vfsp, &nvp)) ) {
1419 *vpp = NULLVP;
1420 return (error);
1421 }
1422 vnode_lock_spin(nvp);
1423 nvp->v_flag |= VBDEVVP;
1424 nvp->v_tag = VT_NON; /* set this to VT_NON so during aliasing it can be replaced */
1425 vnode_unlock(nvp);
1426 if ( (error = vnode_ref(nvp)) ) {
1427 panic("bdevvp failed: vnode_ref");
1428 return (error);
1429 }
1430 if ( (error = VNOP_FSYNC(nvp, MNT_WAIT, &context)) ) {
1431 panic("bdevvp failed: fsync");
1432 return (error);
1433 }
1434 if ( (error = buf_invalidateblks(nvp, BUF_WRITE_DATA, 0, 0)) ) {
1435 panic("bdevvp failed: invalidateblks");
1436 return (error);
1437 }
1438
1439 #if CONFIG_MACF
1440 /*
1441 * XXXMAC: We can't put a MAC check here, the system will
1442 * panic without this vnode.
1443 */
1444 #endif /* MAC */
1445
1446 if ( (error = VNOP_OPEN(nvp, FREAD, &context)) ) {
1447 panic("bdevvp failed: open");
1448 return (error);
1449 }
1450 *vpp = nvp;
1451
1452 return (0);
1453 }
1454
1455
1456 /*
1457 * Check to see if the new vnode represents a special device
1458 * for which we already have a vnode (either because of
1459 * bdevvp() or because of a different vnode representing
1460 * the same block device). If such an alias exists, deallocate
1461 * the existing contents and return the aliased vnode. The
1462 * caller is responsible for filling it with its new contents.
1463 */
1464 static vnode_t
1465 checkalias(struct vnode *nvp, dev_t nvp_rdev)
1466 {
1467 struct vnode *vp;
1468 struct vnode **vpp;
1469 struct specinfo *sin = NULL;
1470 int vid = 0;
1471
1472 vpp = &speclisth[SPECHASH(nvp_rdev)];
1473 loop:
1474 SPECHASH_LOCK();
1475
1476 for (vp = *vpp; vp; vp = vp->v_specnext) {
1477 if (nvp_rdev == vp->v_rdev && nvp->v_type == vp->v_type) {
1478 vid = vp->v_id;
1479 break;
1480 }
1481 }
1482 SPECHASH_UNLOCK();
1483
1484 if (vp) {
1485 found_alias:
1486 if (vnode_getwithvid(vp,vid)) {
1487 goto loop;
1488 }
1489 /*
1490 * Termination state is checked in vnode_getwithvid
1491 */
1492 vnode_lock(vp);
1493
1494 /*
1495 * Alias, but not in use, so flush it out.
1496 */
1497 if ((vp->v_iocount == 1) && (vp->v_usecount == 0)) {
1498 vnode_reclaim_internal(vp, 1, 1, 0);
1499 vnode_put_locked(vp);
1500 vnode_unlock(vp);
1501 goto loop;
1502 }
1503
1504 }
1505 if (vp == NULL || vp->v_tag != VT_NON) {
1506 if (sin == NULL) {
1507 MALLOC_ZONE(sin, struct specinfo *, sizeof(struct specinfo),
1508 M_SPECINFO, M_WAITOK);
1509 }
1510
1511 nvp->v_specinfo = sin;
1512 bzero(nvp->v_specinfo, sizeof(struct specinfo));
1513 nvp->v_rdev = nvp_rdev;
1514 nvp->v_specflags = 0;
1515 nvp->v_speclastr = -1;
1516 nvp->v_specinfo->si_opencount = 0;
1517 nvp->v_specinfo->si_initted = 0;
1518 nvp->v_specinfo->si_throttleable = 0;
1519
1520 SPECHASH_LOCK();
1521
1522 /* We dropped the lock, someone could have added */
1523 if (vp == NULLVP) {
1524 for (vp = *vpp; vp; vp = vp->v_specnext) {
1525 if (nvp_rdev == vp->v_rdev && nvp->v_type == vp->v_type) {
1526 vid = vp->v_id;
1527 SPECHASH_UNLOCK();
1528 goto found_alias;
1529 }
1530 }
1531 }
1532
1533 nvp->v_hashchain = vpp;
1534 nvp->v_specnext = *vpp;
1535 *vpp = nvp;
1536
1537 if (vp != NULLVP) {
1538 nvp->v_specflags |= SI_ALIASED;
1539 vp->v_specflags |= SI_ALIASED;
1540 SPECHASH_UNLOCK();
1541 vnode_put_locked(vp);
1542 vnode_unlock(vp);
1543 } else {
1544 SPECHASH_UNLOCK();
1545 }
1546
1547 return (NULLVP);
1548 }
1549
1550 if (sin) {
1551 FREE_ZONE(sin, sizeof(struct specinfo), M_SPECINFO);
1552 }
1553
1554 if ((vp->v_flag & (VBDEVVP | VDEVFLUSH)) != 0)
1555 return(vp);
1556
1557 panic("checkalias with VT_NON vp that shouldn't: %p", vp);
1558
1559 return (vp);
1560 }
1561
1562
1563 /*
1564 * Get a reference on a particular vnode and lock it if requested.
1565 * If the vnode was on the inactive list, remove it from the list.
1566 * If the vnode was on the free list, remove it from the list and
1567 * move it to inactive list as needed.
1568 * The vnode lock bit is set if the vnode is being eliminated in
1569 * vgone. The process is awakened when the transition is completed,
1570 * and an error returned to indicate that the vnode is no longer
1571 * usable (possibly having been changed to a new file system type).
1572 */
1573 int
1574 vget_internal(vnode_t vp, int vid, int vflags)
1575 {
1576 int error = 0;
1577
1578 vnode_lock_spin(vp);
1579
1580 if ((vflags & VNODE_WRITEABLE) && (vp->v_writecount == 0))
1581 /*
1582 * vnode to be returned only if it has writers opened
1583 */
1584 error = EINVAL;
1585 else
1586 error = vnode_getiocount(vp, vid, vflags);
1587
1588 vnode_unlock(vp);
1589
1590 return (error);
1591 }
1592
1593 /*
1594 * Returns: 0 Success
1595 * ENOENT No such file or directory [terminating]
1596 */
1597 int
1598 vnode_ref(vnode_t vp)
1599 {
1600
1601 return (vnode_ref_ext(vp, 0, 0));
1602 }
1603
1604 /*
1605 * Returns: 0 Success
1606 * ENOENT No such file or directory [terminating]
1607 */
1608 int
1609 vnode_ref_ext(vnode_t vp, int fmode, int flags)
1610 {
1611 int error = 0;
1612
1613 vnode_lock_spin(vp);
1614
1615 /*
1616 * once all the current call sites have been fixed to insure they have
1617 * taken an iocount, we can toughen this assert up and insist that the
1618 * iocount is non-zero... a non-zero usecount doesn't insure correctness
1619 */
1620 if (vp->v_iocount <= 0 && vp->v_usecount <= 0)
1621 panic("vnode_ref_ext: vp %p has no valid reference %d, %d", vp, vp->v_iocount, vp->v_usecount);
1622
1623 /*
1624 * if you are the owner of drain/termination, can acquire usecount
1625 */
1626 if ((flags & VNODE_REF_FORCE) == 0) {
1627 if ((vp->v_lflag & (VL_DRAIN | VL_TERMINATE | VL_DEAD))) {
1628 if (vp->v_owner != current_thread()) {
1629 error = ENOENT;
1630 goto out;
1631 }
1632 }
1633 }
1634 vp->v_usecount++;
1635
1636 if (fmode & FWRITE) {
1637 if (++vp->v_writecount <= 0)
1638 panic("vnode_ref_ext: v_writecount");
1639 }
1640 if (fmode & O_EVTONLY) {
1641 if (++vp->v_kusecount <= 0)
1642 panic("vnode_ref_ext: v_kusecount");
1643 }
1644 if (vp->v_flag & VRAGE) {
1645 struct uthread *ut;
1646
1647 ut = get_bsdthread_info(current_thread());
1648
1649 if ( !(current_proc()->p_lflag & P_LRAGE_VNODES) &&
1650 !(ut->uu_flag & UT_RAGE_VNODES)) {
1651 /*
1652 * a 'normal' process accessed this vnode
1653 * so make sure its no longer marked
1654 * for rapid aging... also, make sure
1655 * it gets removed from the rage list...
1656 * when v_usecount drops back to 0, it
1657 * will be put back on the real free list
1658 */
1659 vp->v_flag &= ~VRAGE;
1660 vp->v_references = 0;
1661 vnode_list_remove(vp);
1662 }
1663 }
1664 if (vp->v_usecount == 1 && vp->v_type == VREG && !(vp->v_flag & VSYSTEM)) {
1665
1666 if (vp->v_ubcinfo) {
1667 vnode_lock_convert(vp);
1668 memory_object_mark_used(vp->v_ubcinfo->ui_control);
1669 }
1670 }
1671 out:
1672 vnode_unlock(vp);
1673
1674 return (error);
1675 }
1676
1677
1678 boolean_t
1679 vnode_on_reliable_media(vnode_t vp)
1680 {
1681 if ( !(vp->v_mount->mnt_kern_flag & MNTK_VIRTUALDEV) && (vp->v_mount->mnt_flag & MNT_LOCAL) )
1682 return (TRUE);
1683 return (FALSE);
1684 }
1685
1686 static void
1687 vnode_async_list_add(vnode_t vp)
1688 {
1689 vnode_list_lock();
1690
1691 if (VONLIST(vp) || (vp->v_lflag & (VL_TERMINATE|VL_DEAD)))
1692 panic("vnode_async_list_add: %p is in wrong state", vp);
1693
1694 TAILQ_INSERT_HEAD(&vnode_async_work_list, vp, v_freelist);
1695 vp->v_listflag |= VLIST_ASYNC_WORK;
1696
1697 async_work_vnodes++;
1698
1699 vnode_list_unlock();
1700
1701 wakeup(&vnode_async_work_list);
1702
1703 }
1704
1705
1706 /*
1707 * put the vnode on appropriate free list.
1708 * called with vnode LOCKED
1709 */
1710 static void
1711 vnode_list_add(vnode_t vp)
1712 {
1713 boolean_t need_dead_wakeup = FALSE;
1714
1715 #if DIAGNOSTIC
1716 lck_mtx_assert(&vp->v_lock, LCK_MTX_ASSERT_OWNED);
1717 #endif
1718
1719 again:
1720
1721 /*
1722 * if it is already on a list or non zero references return
1723 */
1724 if (VONLIST(vp) || (vp->v_usecount != 0) || (vp->v_iocount != 0) || (vp->v_lflag & VL_TERMINATE))
1725 return;
1726
1727 /*
1728 * In vclean, we might have deferred ditching locked buffers
1729 * because something was still referencing them (indicated by
1730 * usecount). We can ditch them now.
1731 */
1732 if (ISSET(vp->v_lflag, VL_DEAD)
1733 && (!LIST_EMPTY(&vp->v_cleanblkhd) || !LIST_EMPTY(&vp->v_dirtyblkhd))) {
1734 ++vp->v_iocount; // Probably not necessary, but harmless
1735 #ifdef JOE_DEBUG
1736 record_vp(vp, 1);
1737 #endif
1738 vnode_unlock(vp);
1739 buf_invalidateblks(vp, BUF_INVALIDATE_LOCKED, 0, 0);
1740 vnode_lock(vp);
1741 vnode_dropiocount(vp);
1742 goto again;
1743 }
1744
1745 vnode_list_lock();
1746
1747 if ((vp->v_flag & VRAGE) && !(vp->v_lflag & VL_DEAD)) {
1748 /*
1749 * add the new guy to the appropriate end of the RAGE list
1750 */
1751 if ((vp->v_flag & VAGE))
1752 TAILQ_INSERT_HEAD(&vnode_rage_list, vp, v_freelist);
1753 else
1754 TAILQ_INSERT_TAIL(&vnode_rage_list, vp, v_freelist);
1755
1756 vp->v_listflag |= VLIST_RAGE;
1757 ragevnodes++;
1758
1759 /*
1760 * reset the timestamp for the last inserted vp on the RAGE
1761 * queue to let new_vnode know that its not ok to start stealing
1762 * from this list... as long as we're actively adding to this list
1763 * we'll push out the vnodes we want to donate to the real free list
1764 * once we stop pushing, we'll let some time elapse before we start
1765 * stealing them in the new_vnode routine
1766 */
1767 microuptime(&rage_tv);
1768 } else {
1769 /*
1770 * if VL_DEAD, insert it at head of the dead list
1771 * else insert at tail of LRU list or at head if VAGE is set
1772 */
1773 if ( (vp->v_lflag & VL_DEAD)) {
1774 TAILQ_INSERT_HEAD(&vnode_dead_list, vp, v_freelist);
1775 vp->v_listflag |= VLIST_DEAD;
1776 deadvnodes++;
1777
1778 if (dead_vnode_wanted) {
1779 dead_vnode_wanted--;
1780 need_dead_wakeup = TRUE;
1781 }
1782
1783 } else if ( (vp->v_flag & VAGE) ) {
1784 TAILQ_INSERT_HEAD(&vnode_free_list, vp, v_freelist);
1785 vp->v_flag &= ~VAGE;
1786 freevnodes++;
1787 } else {
1788 TAILQ_INSERT_TAIL(&vnode_free_list, vp, v_freelist);
1789 freevnodes++;
1790 }
1791 }
1792 vnode_list_unlock();
1793
1794 if (need_dead_wakeup == TRUE)
1795 wakeup_one((caddr_t)&dead_vnode_wanted);
1796 }
1797
1798
1799 /*
1800 * remove the vnode from appropriate free list.
1801 * called with vnode LOCKED and
1802 * the list lock held
1803 */
1804 static void
1805 vnode_list_remove_locked(vnode_t vp)
1806 {
1807 if (VONLIST(vp)) {
1808 /*
1809 * the v_listflag field is
1810 * protected by the vnode_list_lock
1811 */
1812 if (vp->v_listflag & VLIST_RAGE)
1813 VREMRAGE("vnode_list_remove", vp);
1814 else if (vp->v_listflag & VLIST_DEAD)
1815 VREMDEAD("vnode_list_remove", vp);
1816 else if (vp->v_listflag & VLIST_ASYNC_WORK)
1817 VREMASYNC_WORK("vnode_list_remove", vp);
1818 else
1819 VREMFREE("vnode_list_remove", vp);
1820 }
1821 }
1822
1823
1824 /*
1825 * remove the vnode from appropriate free list.
1826 * called with vnode LOCKED
1827 */
1828 static void
1829 vnode_list_remove(vnode_t vp)
1830 {
1831 #if DIAGNOSTIC
1832 lck_mtx_assert(&vp->v_lock, LCK_MTX_ASSERT_OWNED);
1833 #endif
1834 /*
1835 * we want to avoid taking the list lock
1836 * in the case where we're not on the free
1837 * list... this will be true for most
1838 * directories and any currently in use files
1839 *
1840 * we're guaranteed that we can't go from
1841 * the not-on-list state to the on-list
1842 * state since we hold the vnode lock...
1843 * all calls to vnode_list_add are done
1844 * under the vnode lock... so we can
1845 * check for that condition (the prevelant one)
1846 * without taking the list lock
1847 */
1848 if (VONLIST(vp)) {
1849 vnode_list_lock();
1850 /*
1851 * however, we're not guaranteed that
1852 * we won't go from the on-list state
1853 * to the not-on-list state until we
1854 * hold the vnode_list_lock... this
1855 * is due to "new_vnode" removing vnodes
1856 * from the free list uder the list_lock
1857 * w/o the vnode lock... so we need to
1858 * check again whether we're currently
1859 * on the free list
1860 */
1861 vnode_list_remove_locked(vp);
1862
1863 vnode_list_unlock();
1864 }
1865 }
1866
1867
1868 void
1869 vnode_rele(vnode_t vp)
1870 {
1871 vnode_rele_internal(vp, 0, 0, 0);
1872 }
1873
1874
1875 void
1876 vnode_rele_ext(vnode_t vp, int fmode, int dont_reenter)
1877 {
1878 vnode_rele_internal(vp, fmode, dont_reenter, 0);
1879 }
1880
1881
1882 void
1883 vnode_rele_internal(vnode_t vp, int fmode, int dont_reenter, int locked)
1884 {
1885
1886 if ( !locked)
1887 vnode_lock_spin(vp);
1888 #if DIAGNOSTIC
1889 else
1890 lck_mtx_assert(&vp->v_lock, LCK_MTX_ASSERT_OWNED);
1891 #endif
1892 if (--vp->v_usecount < 0)
1893 panic("vnode_rele_ext: vp %p usecount -ve : %d. v_tag = %d, v_type = %d, v_flag = %x.", vp, vp->v_usecount, vp->v_tag, vp->v_type, vp->v_flag);
1894
1895 if (fmode & FWRITE) {
1896 if (--vp->v_writecount < 0)
1897 panic("vnode_rele_ext: vp %p writecount -ve : %d. v_tag = %d, v_type = %d, v_flag = %x.", vp, vp->v_writecount, vp->v_tag, vp->v_type, vp->v_flag);
1898 }
1899 if (fmode & O_EVTONLY) {
1900 if (--vp->v_kusecount < 0)
1901 panic("vnode_rele_ext: vp %p kusecount -ve : %d. v_tag = %d, v_type = %d, v_flag = %x.", vp, vp->v_kusecount, vp->v_tag, vp->v_type, vp->v_flag);
1902 }
1903 if (vp->v_kusecount > vp->v_usecount)
1904 panic("vnode_rele_ext: vp %p kusecount(%d) out of balance with usecount(%d). v_tag = %d, v_type = %d, v_flag = %x.",vp, vp->v_kusecount, vp->v_usecount, vp->v_tag, vp->v_type, vp->v_flag);
1905
1906 if ((vp->v_iocount > 0) || (vp->v_usecount > 0)) {
1907 /*
1908 * vnode is still busy... if we're the last
1909 * usecount, mark for a future call to VNOP_INACTIVE
1910 * when the iocount finally drops to 0
1911 */
1912 if (vp->v_usecount == 0) {
1913 vp->v_lflag |= VL_NEEDINACTIVE;
1914 vp->v_flag &= ~(VNOCACHE_DATA | VRAOFF | VOPENEVT);
1915 }
1916 goto done;
1917 }
1918 vp->v_flag &= ~(VNOCACHE_DATA | VRAOFF | VOPENEVT);
1919
1920 if (ISSET(vp->v_lflag, VL_TERMINATE | VL_DEAD) || dont_reenter) {
1921 /*
1922 * vnode is being cleaned, or
1923 * we've requested that we don't reenter
1924 * the filesystem on this release...in
1925 * the latter case, we'll mark the vnode aged
1926 */
1927 if (dont_reenter) {
1928 if ( !(vp->v_lflag & (VL_TERMINATE | VL_DEAD | VL_MARKTERM)) ) {
1929 vp->v_lflag |= VL_NEEDINACTIVE;
1930
1931 if (vnode_on_reliable_media(vp) == FALSE || vp->v_flag & VISDIRTY) {
1932 vnode_async_list_add(vp);
1933 goto done;
1934 }
1935 }
1936 vp->v_flag |= VAGE;
1937 }
1938 vnode_list_add(vp);
1939
1940 goto done;
1941 }
1942 /*
1943 * at this point both the iocount and usecount
1944 * are zero
1945 * pick up an iocount so that we can call
1946 * VNOP_INACTIVE with the vnode lock unheld
1947 */
1948 vp->v_iocount++;
1949 #ifdef JOE_DEBUG
1950 record_vp(vp, 1);
1951 #endif
1952 vp->v_lflag &= ~VL_NEEDINACTIVE;
1953 vnode_unlock(vp);
1954
1955 VNOP_INACTIVE(vp, vfs_context_current());
1956
1957 vnode_lock_spin(vp);
1958 /*
1959 * because we dropped the vnode lock to call VNOP_INACTIVE
1960 * the state of the vnode may have changed... we may have
1961 * picked up an iocount, usecount or the MARKTERM may have
1962 * been set... we need to reevaluate the reference counts
1963 * to determine if we can call vnode_reclaim_internal at
1964 * this point... if the reference counts are up, we'll pick
1965 * up the MARKTERM state when they get subsequently dropped
1966 */
1967 if ( (vp->v_iocount == 1) && (vp->v_usecount == 0) &&
1968 ((vp->v_lflag & (VL_MARKTERM | VL_TERMINATE | VL_DEAD)) == VL_MARKTERM)) {
1969 struct uthread *ut;
1970
1971 ut = get_bsdthread_info(current_thread());
1972
1973 if (ut->uu_defer_reclaims) {
1974 vp->v_defer_reclaimlist = ut->uu_vreclaims;
1975 ut->uu_vreclaims = vp;
1976 goto done;
1977 }
1978 vnode_lock_convert(vp);
1979 vnode_reclaim_internal(vp, 1, 1, 0);
1980 }
1981 vnode_dropiocount(vp);
1982 vnode_list_add(vp);
1983 done:
1984 if (vp->v_usecount == 0 && vp->v_type == VREG && !(vp->v_flag & VSYSTEM)) {
1985
1986 if (vp->v_ubcinfo) {
1987 vnode_lock_convert(vp);
1988 memory_object_mark_unused(vp->v_ubcinfo->ui_control, (vp->v_flag & VRAGE) == VRAGE);
1989 }
1990 }
1991 if ( !locked)
1992 vnode_unlock(vp);
1993 return;
1994 }
1995
1996 /*
1997 * Remove any vnodes in the vnode table belonging to mount point mp.
1998 *
1999 * If MNT_NOFORCE is specified, there should not be any active ones,
2000 * return error if any are found (nb: this is a user error, not a
2001 * system error). If MNT_FORCE is specified, detach any active vnodes
2002 * that are found.
2003 */
2004 #if DIAGNOSTIC
2005 int busyprt = 0; /* print out busy vnodes */
2006 #endif
2007
2008 int
2009 vflush(struct mount *mp, struct vnode *skipvp, int flags)
2010 {
2011 struct vnode *vp;
2012 int busy = 0;
2013 int reclaimed = 0;
2014 int retval;
2015 unsigned int vid;
2016
2017 /*
2018 * See comments in vnode_iterate() for the rationale for this lock
2019 */
2020 mount_iterate_lock(mp);
2021
2022 mount_lock(mp);
2023 vnode_iterate_setup(mp);
2024 /*
2025 * On regular unmounts(not forced) do a
2026 * quick check for vnodes to be in use. This
2027 * preserves the caching of vnodes. automounter
2028 * tries unmounting every so often to see whether
2029 * it is still busy or not.
2030 */
2031 if (((flags & FORCECLOSE)==0) && ((mp->mnt_kern_flag & MNTK_UNMOUNT_PREFLIGHT) != 0)) {
2032 if (vnode_umount_preflight(mp, skipvp, flags)) {
2033 vnode_iterate_clear(mp);
2034 mount_unlock(mp);
2035 mount_iterate_unlock(mp);
2036 return(EBUSY);
2037 }
2038 }
2039 loop:
2040 /* If it returns 0 then there is nothing to do */
2041 retval = vnode_iterate_prepare(mp);
2042
2043 if (retval == 0) {
2044 vnode_iterate_clear(mp);
2045 mount_unlock(mp);
2046 mount_iterate_unlock(mp);
2047 return(retval);
2048 }
2049
2050 /* iterate over all the vnodes */
2051 while (!TAILQ_EMPTY(&mp->mnt_workerqueue)) {
2052
2053 vp = TAILQ_FIRST(&mp->mnt_workerqueue);
2054 TAILQ_REMOVE(&mp->mnt_workerqueue, vp, v_mntvnodes);
2055 TAILQ_INSERT_TAIL(&mp->mnt_vnodelist, vp, v_mntvnodes);
2056
2057 if ( (vp->v_mount != mp) || (vp == skipvp)) {
2058 continue;
2059 }
2060 vid = vp->v_id;
2061 mount_unlock(mp);
2062
2063 vnode_lock_spin(vp);
2064
2065 // If vnode is already terminating, wait for it...
2066 while (vp->v_id == vid && ISSET(vp->v_lflag, VL_TERMINATE)) {
2067 vp->v_lflag |= VL_TERMWANT;
2068 msleep(&vp->v_lflag, &vp->v_lock, PVFS, "vflush", NULL);
2069 }
2070
2071 if ((vp->v_id != vid) || ISSET(vp->v_lflag, VL_DEAD)) {
2072 vnode_unlock(vp);
2073 mount_lock(mp);
2074 continue;
2075 }
2076
2077 /*
2078 * If requested, skip over vnodes marked VSYSTEM.
2079 * Skip over all vnodes marked VNOFLUSH.
2080 */
2081 if ((flags & SKIPSYSTEM) && ((vp->v_flag & VSYSTEM) ||
2082 (vp->v_flag & VNOFLUSH))) {
2083 vnode_unlock(vp);
2084 mount_lock(mp);
2085 continue;
2086 }
2087 /*
2088 * If requested, skip over vnodes marked VSWAP.
2089 */
2090 if ((flags & SKIPSWAP) && (vp->v_flag & VSWAP)) {
2091 vnode_unlock(vp);
2092 mount_lock(mp);
2093 continue;
2094 }
2095 /*
2096 * If requested, skip over vnodes marked VROOT.
2097 */
2098 if ((flags & SKIPROOT) && (vp->v_flag & VROOT)) {
2099 vnode_unlock(vp);
2100 mount_lock(mp);
2101 continue;
2102 }
2103 /*
2104 * If WRITECLOSE is set, only flush out regular file
2105 * vnodes open for writing.
2106 */
2107 if ((flags & WRITECLOSE) &&
2108 (vp->v_writecount == 0 || vp->v_type != VREG)) {
2109 vnode_unlock(vp);
2110 mount_lock(mp);
2111 continue;
2112 }
2113 /*
2114 * If the real usecount is 0, all we need to do is clear
2115 * out the vnode data structures and we are done.
2116 */
2117 if (((vp->v_usecount == 0) ||
2118 ((vp->v_usecount - vp->v_kusecount) == 0))) {
2119
2120 vnode_lock_convert(vp);
2121 vp->v_iocount++; /* so that drain waits for * other iocounts */
2122 #ifdef JOE_DEBUG
2123 record_vp(vp, 1);
2124 #endif
2125 vnode_reclaim_internal(vp, 1, 1, 0);
2126 vnode_dropiocount(vp);
2127 vnode_list_add(vp);
2128 vnode_unlock(vp);
2129
2130 reclaimed++;
2131 mount_lock(mp);
2132 continue;
2133 }
2134 /*
2135 * If FORCECLOSE is set, forcibly close the vnode.
2136 * For block or character devices, revert to an
2137 * anonymous device. For all other files, just kill them.
2138 */
2139 if (flags & FORCECLOSE) {
2140 vnode_lock_convert(vp);
2141
2142 if (vp->v_type != VBLK && vp->v_type != VCHR) {
2143 vp->v_iocount++; /* so that drain waits * for other iocounts */
2144 #ifdef JOE_DEBUG
2145 record_vp(vp, 1);
2146 #endif
2147 vnode_abort_advlocks(vp);
2148 vnode_reclaim_internal(vp, 1, 1, 0);
2149 vnode_dropiocount(vp);
2150 vnode_list_add(vp);
2151 vnode_unlock(vp);
2152 } else {
2153 vclean(vp, 0);
2154 vp->v_lflag &= ~VL_DEAD;
2155 vp->v_op = spec_vnodeop_p;
2156 vp->v_flag |= VDEVFLUSH;
2157 vnode_unlock(vp);
2158 }
2159 mount_lock(mp);
2160 continue;
2161 }
2162 #if DIAGNOSTIC
2163 if (busyprt)
2164 vprint("vflush: busy vnode", vp);
2165 #endif
2166 vnode_unlock(vp);
2167 mount_lock(mp);
2168 busy++;
2169 }
2170
2171 /* At this point the worker queue is completed */
2172 if (busy && ((flags & FORCECLOSE)==0) && reclaimed) {
2173 busy = 0;
2174 reclaimed = 0;
2175 (void)vnode_iterate_reloadq(mp);
2176 /* returned with mount lock held */
2177 goto loop;
2178 }
2179
2180 /* if new vnodes were created in between retry the reclaim */
2181 if ( vnode_iterate_reloadq(mp) != 0) {
2182 if (!(busy && ((flags & FORCECLOSE)==0)))
2183 goto loop;
2184 }
2185 vnode_iterate_clear(mp);
2186 mount_unlock(mp);
2187 mount_iterate_unlock(mp);
2188
2189 if (busy && ((flags & FORCECLOSE)==0))
2190 return (EBUSY);
2191 return (0);
2192 }
2193
2194 long num_recycledvnodes = 0;
2195 /*
2196 * Disassociate the underlying file system from a vnode.
2197 * The vnode lock is held on entry.
2198 */
2199 static void
2200 vclean(vnode_t vp, int flags)
2201 {
2202 vfs_context_t ctx = vfs_context_current();
2203 int active;
2204 int need_inactive;
2205 int already_terminating;
2206 int clflags = 0;
2207 #if NAMEDSTREAMS
2208 int is_namedstream;
2209 #endif
2210
2211 /*
2212 * Check to see if the vnode is in use.
2213 * If so we have to reference it before we clean it out
2214 * so that its count cannot fall to zero and generate a
2215 * race against ourselves to recycle it.
2216 */
2217 active = vp->v_usecount;
2218
2219 /*
2220 * just in case we missed sending a needed
2221 * VNOP_INACTIVE, we'll do it now
2222 */
2223 need_inactive = (vp->v_lflag & VL_NEEDINACTIVE);
2224
2225 vp->v_lflag &= ~VL_NEEDINACTIVE;
2226
2227 /*
2228 * Prevent the vnode from being recycled or
2229 * brought into use while we clean it out.
2230 */
2231 already_terminating = (vp->v_lflag & VL_TERMINATE);
2232
2233 vp->v_lflag |= VL_TERMINATE;
2234
2235 #if NAMEDSTREAMS
2236 is_namedstream = vnode_isnamedstream(vp);
2237 #endif
2238
2239 vnode_unlock(vp);
2240
2241 OSAddAtomicLong(1, &num_recycledvnodes);
2242
2243 if (flags & DOCLOSE)
2244 clflags |= IO_NDELAY;
2245 if (flags & REVOKEALL)
2246 clflags |= IO_REVOKE;
2247
2248 if (active && (flags & DOCLOSE))
2249 VNOP_CLOSE(vp, clflags, ctx);
2250
2251 /*
2252 * Clean out any buffers associated with the vnode.
2253 */
2254 if (flags & DOCLOSE) {
2255 #if NFSCLIENT
2256 if (vp->v_tag == VT_NFS)
2257 nfs_vinvalbuf(vp, V_SAVE, ctx, 0);
2258 else
2259 #endif
2260 {
2261 VNOP_FSYNC(vp, MNT_WAIT, ctx);
2262
2263 /*
2264 * If the vnode is still in use (by the journal for
2265 * example) we don't want to invalidate locked buffers
2266 * here. In that case, either the journal will tidy them
2267 * up, or we will deal with it when the usecount is
2268 * finally released in vnode_rele_internal.
2269 */
2270 buf_invalidateblks(vp, BUF_WRITE_DATA | (active ? 0 : BUF_INVALIDATE_LOCKED), 0, 0);
2271 }
2272 if (UBCINFOEXISTS(vp))
2273 /*
2274 * Clean the pages in VM.
2275 */
2276 (void)ubc_msync(vp, (off_t)0, ubc_getsize(vp), NULL, UBC_PUSHALL | UBC_INVALIDATE | UBC_SYNC);
2277 }
2278 if (active || need_inactive)
2279 VNOP_INACTIVE(vp, ctx);
2280
2281 #if NAMEDSTREAMS
2282 if ((is_namedstream != 0) && (vp->v_parent != NULLVP)) {
2283 vnode_t pvp = vp->v_parent;
2284
2285 /* Delete the shadow stream file before we reclaim its vnode */
2286 if (vnode_isshadow(vp)) {
2287 vnode_relenamedstream(pvp, vp);
2288 }
2289
2290 /*
2291 * No more streams associated with the parent. We
2292 * have a ref on it, so its identity is stable.
2293 * If the parent is on an opaque volume, then we need to know
2294 * whether it has associated named streams.
2295 */
2296 if (vfs_authopaque(pvp->v_mount)) {
2297 vnode_lock_spin(pvp);
2298 pvp->v_lflag &= ~VL_HASSTREAMS;
2299 vnode_unlock(pvp);
2300 }
2301 }
2302 #endif
2303
2304 /*
2305 * Destroy ubc named reference
2306 * cluster_release is done on this path
2307 * along with dropping the reference on the ucred
2308 * (and in the case of forced unmount of an mmap-ed file,
2309 * the ubc reference on the vnode is dropped here too).
2310 */
2311 ubc_destroy_named(vp);
2312
2313 #if CONFIG_TRIGGERS
2314 /*
2315 * cleanup trigger info from vnode (if any)
2316 */
2317 if (vp->v_resolve)
2318 vnode_resolver_detach(vp);
2319 #endif
2320
2321 /*
2322 * Reclaim the vnode.
2323 */
2324 if (VNOP_RECLAIM(vp, ctx))
2325 panic("vclean: cannot reclaim");
2326
2327 // make sure the name & parent ptrs get cleaned out!
2328 vnode_update_identity(vp, NULLVP, NULL, 0, 0, VNODE_UPDATE_PARENT | VNODE_UPDATE_NAME | VNODE_UPDATE_PURGE);
2329
2330 vnode_lock(vp);
2331
2332 /*
2333 * Remove the vnode from any mount list it might be on. It is not
2334 * safe to do this any earlier because unmount needs to wait for
2335 * any vnodes to terminate and it cannot do that if it cannot find
2336 * them.
2337 */
2338 insmntque(vp, (struct mount *)0);
2339
2340 vp->v_mount = dead_mountp;
2341 vp->v_op = dead_vnodeop_p;
2342 vp->v_tag = VT_NON;
2343 vp->v_data = NULL;
2344
2345 vp->v_lflag |= VL_DEAD;
2346 vp->v_flag &= ~VISDIRTY;
2347
2348 if (already_terminating == 0) {
2349 vp->v_lflag &= ~VL_TERMINATE;
2350 /*
2351 * Done with purge, notify sleepers of the grim news.
2352 */
2353 if (vp->v_lflag & VL_TERMWANT) {
2354 vp->v_lflag &= ~VL_TERMWANT;
2355 wakeup(&vp->v_lflag);
2356 }
2357 }
2358 }
2359
2360 /*
2361 * Eliminate all activity associated with the requested vnode
2362 * and with all vnodes aliased to the requested vnode.
2363 */
2364 int
2365 #if DIAGNOSTIC
2366 vn_revoke(vnode_t vp, int flags, __unused vfs_context_t a_context)
2367 #else
2368 vn_revoke(vnode_t vp, __unused int flags, __unused vfs_context_t a_context)
2369 #endif
2370 {
2371 struct vnode *vq;
2372 int vid;
2373
2374 #if DIAGNOSTIC
2375 if ((flags & REVOKEALL) == 0)
2376 panic("vnop_revoke");
2377 #endif
2378
2379 if (vnode_isaliased(vp)) {
2380 /*
2381 * If a vgone (or vclean) is already in progress,
2382 * return an immediate error
2383 */
2384 if (vp->v_lflag & VL_TERMINATE)
2385 return(ENOENT);
2386
2387 /*
2388 * Ensure that vp will not be vgone'd while we
2389 * are eliminating its aliases.
2390 */
2391 SPECHASH_LOCK();
2392 while ((vp->v_specflags & SI_ALIASED)) {
2393 for (vq = *vp->v_hashchain; vq; vq = vq->v_specnext) {
2394 if (vq->v_rdev != vp->v_rdev ||
2395 vq->v_type != vp->v_type || vp == vq)
2396 continue;
2397 vid = vq->v_id;
2398 SPECHASH_UNLOCK();
2399 if (vnode_getwithvid(vq,vid)){
2400 SPECHASH_LOCK();
2401 break;
2402 }
2403 vnode_lock(vq);
2404 if (!(vq->v_lflag & VL_TERMINATE)) {
2405 vnode_reclaim_internal(vq, 1, 1, 0);
2406 }
2407 vnode_put_locked(vq);
2408 vnode_unlock(vq);
2409 SPECHASH_LOCK();
2410 break;
2411 }
2412 }
2413 SPECHASH_UNLOCK();
2414 }
2415 vnode_lock(vp);
2416 if (vp->v_lflag & VL_TERMINATE) {
2417 vnode_unlock(vp);
2418 return (ENOENT);
2419 }
2420 vnode_reclaim_internal(vp, 1, 0, REVOKEALL);
2421 vnode_unlock(vp);
2422
2423 return (0);
2424 }
2425
2426 /*
2427 * Recycle an unused vnode to the front of the free list.
2428 * Release the passed interlock if the vnode will be recycled.
2429 */
2430 int
2431 vnode_recycle(struct vnode *vp)
2432 {
2433 vnode_lock_spin(vp);
2434
2435 if (vp->v_iocount || vp->v_usecount) {
2436 vp->v_lflag |= VL_MARKTERM;
2437 vnode_unlock(vp);
2438 return(0);
2439 }
2440 vnode_lock_convert(vp);
2441 vnode_reclaim_internal(vp, 1, 0, 0);
2442
2443 vnode_unlock(vp);
2444
2445 return (1);
2446 }
2447
2448 static int
2449 vnode_reload(vnode_t vp)
2450 {
2451 vnode_lock_spin(vp);
2452
2453 if ((vp->v_iocount > 1) || vp->v_usecount) {
2454 vnode_unlock(vp);
2455 return(0);
2456 }
2457 if (vp->v_iocount <= 0)
2458 panic("vnode_reload with no iocount %d", vp->v_iocount);
2459
2460 /* mark for release when iocount is dopped */
2461 vp->v_lflag |= VL_MARKTERM;
2462 vnode_unlock(vp);
2463
2464 return (1);
2465 }
2466
2467
2468 static void
2469 vgone(vnode_t vp, int flags)
2470 {
2471 struct vnode *vq;
2472 struct vnode *vx;
2473
2474 /*
2475 * Clean out the filesystem specific data.
2476 * vclean also takes care of removing the
2477 * vnode from any mount list it might be on
2478 */
2479 vclean(vp, flags | DOCLOSE);
2480
2481 /*
2482 * If special device, remove it from special device alias list
2483 * if it is on one.
2484 */
2485 if ((vp->v_type == VBLK || vp->v_type == VCHR) && vp->v_specinfo != 0) {
2486 SPECHASH_LOCK();
2487 if (*vp->v_hashchain == vp) {
2488 *vp->v_hashchain = vp->v_specnext;
2489 } else {
2490 for (vq = *vp->v_hashchain; vq; vq = vq->v_specnext) {
2491 if (vq->v_specnext != vp)
2492 continue;
2493 vq->v_specnext = vp->v_specnext;
2494 break;
2495 }
2496 if (vq == NULL)
2497 panic("missing bdev");
2498 }
2499 if (vp->v_specflags & SI_ALIASED) {
2500 vx = NULL;
2501 for (vq = *vp->v_hashchain; vq; vq = vq->v_specnext) {
2502 if (vq->v_rdev != vp->v_rdev ||
2503 vq->v_type != vp->v_type)
2504 continue;
2505 if (vx)
2506 break;
2507 vx = vq;
2508 }
2509 if (vx == NULL)
2510 panic("missing alias");
2511 if (vq == NULL)
2512 vx->v_specflags &= ~SI_ALIASED;
2513 vp->v_specflags &= ~SI_ALIASED;
2514 }
2515 SPECHASH_UNLOCK();
2516 {
2517 struct specinfo *tmp = vp->v_specinfo;
2518 vp->v_specinfo = NULL;
2519 FREE_ZONE((void *)tmp, sizeof(struct specinfo), M_SPECINFO);
2520 }
2521 }
2522 }
2523
2524 /*
2525 * Lookup a vnode by device number.
2526 */
2527 int
2528 check_mountedon(dev_t dev, enum vtype type, int *errorp)
2529 {
2530 vnode_t vp;
2531 int rc = 0;
2532 int vid;
2533
2534 loop:
2535 SPECHASH_LOCK();
2536 for (vp = speclisth[SPECHASH(dev)]; vp; vp = vp->v_specnext) {
2537 if (dev != vp->v_rdev || type != vp->v_type)
2538 continue;
2539 vid = vp->v_id;
2540 SPECHASH_UNLOCK();
2541 if (vnode_getwithvid(vp,vid))
2542 goto loop;
2543 vnode_lock_spin(vp);
2544 if ((vp->v_usecount > 0) || (vp->v_iocount > 1)) {
2545 vnode_unlock(vp);
2546 if ((*errorp = vfs_mountedon(vp)) != 0)
2547 rc = 1;
2548 } else
2549 vnode_unlock(vp);
2550 vnode_put(vp);
2551 return(rc);
2552 }
2553 SPECHASH_UNLOCK();
2554 return (0);
2555 }
2556
2557 /*
2558 * Calculate the total number of references to a special device.
2559 */
2560 int
2561 vcount(vnode_t vp)
2562 {
2563 vnode_t vq, vnext;
2564 int count;
2565 int vid;
2566
2567 if (!vnode_isspec(vp)) {
2568 return (vp->v_usecount - vp->v_kusecount);
2569 }
2570
2571 loop:
2572 if (!vnode_isaliased(vp))
2573 return (vp->v_specinfo->si_opencount);
2574 count = 0;
2575
2576 SPECHASH_LOCK();
2577 /*
2578 * Grab first vnode and its vid.
2579 */
2580 vq = *vp->v_hashchain;
2581 vid = vq ? vq->v_id : 0;
2582
2583 SPECHASH_UNLOCK();
2584
2585 while (vq) {
2586 /*
2587 * Attempt to get the vnode outside the SPECHASH lock.
2588 */
2589 if (vnode_getwithvid(vq, vid)) {
2590 goto loop;
2591 }
2592 vnode_lock(vq);
2593
2594 if (vq->v_rdev == vp->v_rdev && vq->v_type == vp->v_type) {
2595 if ((vq->v_usecount == 0) && (vq->v_iocount == 1) && vq != vp) {
2596 /*
2597 * Alias, but not in use, so flush it out.
2598 */
2599 vnode_reclaim_internal(vq, 1, 1, 0);
2600 vnode_put_locked(vq);
2601 vnode_unlock(vq);
2602 goto loop;
2603 }
2604 count += vq->v_specinfo->si_opencount;
2605 }
2606 vnode_unlock(vq);
2607
2608 SPECHASH_LOCK();
2609 /*
2610 * must do this with the reference still held on 'vq'
2611 * so that it can't be destroyed while we're poking
2612 * through v_specnext
2613 */
2614 vnext = vq->v_specnext;
2615 vid = vnext ? vnext->v_id : 0;
2616
2617 SPECHASH_UNLOCK();
2618
2619 vnode_put(vq);
2620
2621 vq = vnext;
2622 }
2623
2624 return (count);
2625 }
2626
2627 int prtactive = 0; /* 1 => print out reclaim of active vnodes */
2628
2629 /*
2630 * Print out a description of a vnode.
2631 */
2632 static const char *typename[] =
2633 { "VNON", "VREG", "VDIR", "VBLK", "VCHR", "VLNK", "VSOCK", "VFIFO", "VBAD" };
2634
2635 void
2636 vprint(const char *label, struct vnode *vp)
2637 {
2638 char sbuf[64];
2639
2640 if (label != NULL)
2641 printf("%s: ", label);
2642 printf("type %s, usecount %d, writecount %d",
2643 typename[vp->v_type], vp->v_usecount, vp->v_writecount);
2644 sbuf[0] = '\0';
2645 if (vp->v_flag & VROOT)
2646 strlcat(sbuf, "|VROOT", sizeof(sbuf));
2647 if (vp->v_flag & VTEXT)
2648 strlcat(sbuf, "|VTEXT", sizeof(sbuf));
2649 if (vp->v_flag & VSYSTEM)
2650 strlcat(sbuf, "|VSYSTEM", sizeof(sbuf));
2651 if (vp->v_flag & VNOFLUSH)
2652 strlcat(sbuf, "|VNOFLUSH", sizeof(sbuf));
2653 if (vp->v_flag & VBWAIT)
2654 strlcat(sbuf, "|VBWAIT", sizeof(sbuf));
2655 if (vnode_isaliased(vp))
2656 strlcat(sbuf, "|VALIASED", sizeof(sbuf));
2657 if (sbuf[0] != '\0')
2658 printf(" flags (%s)", &sbuf[1]);
2659 }
2660
2661
2662 int
2663 vn_getpath(struct vnode *vp, char *pathbuf, int *len)
2664 {
2665 return build_path(vp, pathbuf, *len, len, BUILDPATH_NO_FS_ENTER, vfs_context_current());
2666 }
2667
2668 int
2669 vn_getpath_fsenter(struct vnode *vp, char *pathbuf, int *len)
2670 {
2671 return build_path(vp, pathbuf, *len, len, 0, vfs_context_current());
2672 }
2673
2674 int
2675 vn_getcdhash(struct vnode *vp, off_t offset, unsigned char *cdhash)
2676 {
2677 return ubc_cs_getcdhash(vp, offset, cdhash);
2678 }
2679
2680
2681 static char *extension_table=NULL;
2682 static int nexts;
2683 static int max_ext_width;
2684
2685 static int
2686 extension_cmp(const void *a, const void *b)
2687 {
2688 return (strlen((const char *)a) - strlen((const char *)b));
2689 }
2690
2691
2692 //
2693 // This is the api LaunchServices uses to inform the kernel
2694 // the list of package extensions to ignore.
2695 //
2696 // Internally we keep the list sorted by the length of the
2697 // the extension (from longest to shortest). We sort the
2698 // list of extensions so that we can speed up our searches
2699 // when comparing file names -- we only compare extensions
2700 // that could possibly fit into the file name, not all of
2701 // them (i.e. a short 8 character name can't have an 8
2702 // character extension).
2703 //
2704 extern lck_mtx_t *pkg_extensions_lck;
2705
2706 __private_extern__ int
2707 set_package_extensions_table(user_addr_t data, int nentries, int maxwidth)
2708 {
2709 char *new_exts, *old_exts;
2710 int error;
2711
2712 if (nentries <= 0 || nentries > 1024 || maxwidth <= 0 || maxwidth > 255) {
2713 return EINVAL;
2714 }
2715
2716
2717 // allocate one byte extra so we can guarantee null termination
2718 MALLOC(new_exts, char *, (nentries * maxwidth) + 1, M_TEMP, M_WAITOK);
2719 if (new_exts == NULL) {
2720 return ENOMEM;
2721 }
2722
2723 error = copyin(data, new_exts, nentries * maxwidth);
2724 if (error) {
2725 FREE(new_exts, M_TEMP);
2726 return error;
2727 }
2728
2729 new_exts[(nentries * maxwidth)] = '\0'; // guarantee null termination of the block
2730
2731 qsort(new_exts, nentries, maxwidth, extension_cmp);
2732
2733 lck_mtx_lock(pkg_extensions_lck);
2734
2735 old_exts = extension_table;
2736 extension_table = new_exts;
2737 nexts = nentries;
2738 max_ext_width = maxwidth;
2739
2740 lck_mtx_unlock(pkg_extensions_lck);
2741
2742 if (old_exts) {
2743 FREE(old_exts, M_TEMP);
2744 }
2745
2746 return 0;
2747 }
2748
2749
2750 int is_package_name(const char *name, int len)
2751 {
2752 int i, extlen;
2753 const char *ptr, *name_ext;
2754
2755 if (len <= 3) {
2756 return 0;
2757 }
2758
2759 name_ext = NULL;
2760 for(ptr=name; *ptr != '\0'; ptr++) {
2761 if (*ptr == '.') {
2762 name_ext = ptr;
2763 }
2764 }
2765
2766 // if there is no "." extension, it can't match
2767 if (name_ext == NULL) {
2768 return 0;
2769 }
2770
2771 // advance over the "."
2772 name_ext++;
2773
2774 lck_mtx_lock(pkg_extensions_lck);
2775
2776 // now iterate over all the extensions to see if any match
2777 ptr = &extension_table[0];
2778 for(i=0; i < nexts; i++, ptr+=max_ext_width) {
2779 extlen = strlen(ptr);
2780 if (strncasecmp(name_ext, ptr, extlen) == 0 && name_ext[extlen] == '\0') {
2781 // aha, a match!
2782 lck_mtx_unlock(pkg_extensions_lck);
2783 return 1;
2784 }
2785 }
2786
2787 lck_mtx_unlock(pkg_extensions_lck);
2788
2789 // if we get here, no extension matched
2790 return 0;
2791 }
2792
2793 int
2794 vn_path_package_check(__unused vnode_t vp, char *path, int pathlen, int *component)
2795 {
2796 char *ptr, *end;
2797 int comp=0;
2798
2799 *component = -1;
2800 if (*path != '/') {
2801 return EINVAL;
2802 }
2803
2804 end = path + 1;
2805 while(end < path + pathlen && *end != '\0') {
2806 while(end < path + pathlen && *end == '/' && *end != '\0') {
2807 end++;
2808 }
2809
2810 ptr = end;
2811
2812 while(end < path + pathlen && *end != '/' && *end != '\0') {
2813 end++;
2814 }
2815
2816 if (end > path + pathlen) {
2817 // hmm, string wasn't null terminated
2818 return EINVAL;
2819 }
2820
2821 *end = '\0';
2822 if (is_package_name(ptr, end - ptr)) {
2823 *component = comp;
2824 break;
2825 }
2826
2827 end++;
2828 comp++;
2829 }
2830
2831 return 0;
2832 }
2833
2834 /*
2835 * Determine if a name is inappropriate for a searchfs query.
2836 * This list consists of /System currently.
2837 */
2838
2839 int vn_searchfs_inappropriate_name(const char *name, int len) {
2840 const char *bad_names[] = { "System" };
2841 int bad_len[] = { 6 };
2842 int i;
2843
2844 for(i=0; i < (int) (sizeof(bad_names) / sizeof(bad_names[0])); i++) {
2845 if (len == bad_len[i] && strncmp(name, bad_names[i], strlen(bad_names[i]) + 1) == 0) {
2846 return 1;
2847 }
2848 }
2849
2850 // if we get here, no name matched
2851 return 0;
2852 }
2853
2854 /*
2855 * Top level filesystem related information gathering.
2856 */
2857 extern unsigned int vfs_nummntops;
2858
2859 /*
2860 * The VFS_NUMMNTOPS shouldn't be at name[1] since
2861 * is a VFS generic variable. Since we no longer support
2862 * VT_UFS, we reserve its value to support this sysctl node.
2863 *
2864 * It should have been:
2865 * name[0]: VFS_GENERIC
2866 * name[1]: VFS_NUMMNTOPS
2867 */
2868 SYSCTL_INT(_vfs, VFS_NUMMNTOPS, nummntops,
2869 CTLFLAG_RD | CTLFLAG_KERN | CTLFLAG_LOCKED,
2870 &vfs_nummntops, 0, "");
2871
2872 int
2873 vfs_sysctl(int *name __unused, u_int namelen __unused,
2874 user_addr_t oldp __unused, size_t *oldlenp __unused,
2875 user_addr_t newp __unused, size_t newlen __unused, proc_t p __unused);
2876
2877 int
2878 vfs_sysctl(int *name __unused, u_int namelen __unused,
2879 user_addr_t oldp __unused, size_t *oldlenp __unused,
2880 user_addr_t newp __unused, size_t newlen __unused, proc_t p __unused)
2881 {
2882 return (EINVAL);
2883 }
2884
2885
2886 //
2887 // The following code disallows specific sysctl's that came through
2888 // the direct sysctl interface (vfs_sysctl_node) instead of the newer
2889 // sysctl_vfs_ctlbyfsid() interface. We can not allow these selectors
2890 // through vfs_sysctl_node() because it passes the user's oldp pointer
2891 // directly to the file system which (for these selectors) casts it
2892 // back to a struct sysctl_req and then proceed to use SYSCTL_IN()
2893 // which jumps through an arbitrary function pointer. When called
2894 // through the sysctl_vfs_ctlbyfsid() interface this does not happen
2895 // and so it's safe.
2896 //
2897 // Unfortunately we have to pull in definitions from AFP and SMB and
2898 // perform explicit name checks on the file system to determine if
2899 // these selectors are being used.
2900 //
2901
2902 #define AFPFS_VFS_CTL_GETID 0x00020001
2903 #define AFPFS_VFS_CTL_NETCHANGE 0x00020002
2904 #define AFPFS_VFS_CTL_VOLCHANGE 0x00020003
2905
2906 #define SMBFS_SYSCTL_REMOUNT 1
2907 #define SMBFS_SYSCTL_REMOUNT_INFO 2
2908 #define SMBFS_SYSCTL_GET_SERVER_SHARE 3
2909
2910
2911 static int
2912 is_bad_sysctl_name(struct vfstable *vfsp, int selector_name)
2913 {
2914 switch(selector_name) {
2915 case VFS_CTL_QUERY:
2916 case VFS_CTL_TIMEO:
2917 case VFS_CTL_NOLOCKS:
2918 case VFS_CTL_NSTATUS:
2919 case VFS_CTL_SADDR:
2920 case VFS_CTL_DISC:
2921 case VFS_CTL_SERVERINFO:
2922 return 1;
2923
2924 default:
2925 break;
2926 }
2927
2928 // the more complicated check for some of SMB's special values
2929 if (strcmp(vfsp->vfc_name, "smbfs") == 0) {
2930 switch(selector_name) {
2931 case SMBFS_SYSCTL_REMOUNT:
2932 case SMBFS_SYSCTL_REMOUNT_INFO:
2933 case SMBFS_SYSCTL_GET_SERVER_SHARE:
2934 return 1;
2935 }
2936 } else if (strcmp(vfsp->vfc_name, "afpfs") == 0) {
2937 switch(selector_name) {
2938 case AFPFS_VFS_CTL_GETID:
2939 case AFPFS_VFS_CTL_NETCHANGE:
2940 case AFPFS_VFS_CTL_VOLCHANGE:
2941 return 1;
2942 }
2943 }
2944
2945 //
2946 // If we get here we passed all the checks so the selector is ok
2947 //
2948 return 0;
2949 }
2950
2951
2952 int vfs_sysctl_node SYSCTL_HANDLER_ARGS
2953 {
2954 int *name, namelen;
2955 struct vfstable *vfsp;
2956 int error;
2957 int fstypenum;
2958
2959 fstypenum = oidp->oid_number;
2960 name = arg1;
2961 namelen = arg2;
2962
2963 /* all sysctl names at this level should have at least one name slot for the FS */
2964 if (namelen < 1)
2965 return (EISDIR); /* overloaded */
2966
2967 mount_list_lock();
2968 for (vfsp = vfsconf; vfsp; vfsp = vfsp->vfc_next)
2969 if (vfsp->vfc_typenum == fstypenum) {
2970 vfsp->vfc_refcount++;
2971 break;
2972 }
2973 mount_list_unlock();
2974
2975 if (vfsp == NULL) {
2976 return (ENOTSUP);
2977 }
2978
2979 if (is_bad_sysctl_name(vfsp, name[0])) {
2980 printf("vfs: bad selector 0x%.8x for old-style sysctl(). use the sysctl-by-fsid interface instead\n", name[0]);
2981 return EPERM;
2982 }
2983
2984 error = (vfsp->vfc_vfsops->vfs_sysctl)(name, namelen, req->oldptr, &req->oldlen, req->newptr, req->newlen, vfs_context_current());
2985
2986 mount_list_lock();
2987 vfsp->vfc_refcount--;
2988 mount_list_unlock();
2989
2990 return error;
2991 }
2992
2993 /*
2994 * Check to see if a filesystem is mounted on a block device.
2995 */
2996 int
2997 vfs_mountedon(struct vnode *vp)
2998 {
2999 struct vnode *vq;
3000 int error = 0;
3001
3002 SPECHASH_LOCK();
3003 if (vp->v_specflags & SI_MOUNTEDON) {
3004 error = EBUSY;
3005 goto out;
3006 }
3007 if (vp->v_specflags & SI_ALIASED) {
3008 for (vq = *vp->v_hashchain; vq; vq = vq->v_specnext) {
3009 if (vq->v_rdev != vp->v_rdev ||
3010 vq->v_type != vp->v_type)
3011 continue;
3012 if (vq->v_specflags & SI_MOUNTEDON) {
3013 error = EBUSY;
3014 break;
3015 }
3016 }
3017 }
3018 out:
3019 SPECHASH_UNLOCK();
3020 return (error);
3021 }
3022
3023 struct unmount_info {
3024 int u_errs; // Total failed unmounts
3025 int u_busy; // EBUSY failed unmounts
3026 };
3027
3028 static int
3029 unmount_callback(mount_t mp, void *arg)
3030 {
3031 int error;
3032 char *mntname;
3033 struct unmount_info *uip = arg;
3034
3035 mount_ref(mp, 0);
3036 mount_iterdrop(mp); // avoid vfs_iterate deadlock in dounmount()
3037
3038 MALLOC_ZONE(mntname, void *, MAXPATHLEN, M_NAMEI, M_WAITOK);
3039 if (mntname)
3040 strlcpy(mntname, mp->mnt_vfsstat.f_mntonname, MAXPATHLEN);
3041
3042 error = dounmount(mp, MNT_FORCE, 1, vfs_context_current());
3043 if (error) {
3044 uip->u_errs++;
3045 printf("Unmount of %s failed (%d)\n", mntname ? mntname:"?", error);
3046 if (error == EBUSY)
3047 uip->u_busy++;
3048 }
3049 if (mntname)
3050 FREE_ZONE(mntname, MAXPATHLEN, M_NAMEI);
3051
3052 return (VFS_RETURNED);
3053 }
3054
3055 /*
3056 * Unmount all filesystems. The list is traversed in reverse order
3057 * of mounting to avoid dependencies.
3058 * Busy mounts are retried.
3059 */
3060 __private_extern__ void
3061 vfs_unmountall(void)
3062 {
3063 int mounts, sec = 1;
3064 struct unmount_info ui;
3065
3066 retry:
3067 ui.u_errs = ui.u_busy = 0;
3068 vfs_iterate(VFS_ITERATE_CB_DROPREF | VFS_ITERATE_TAIL_FIRST, unmount_callback, &ui);
3069 mounts = mount_getvfscnt();
3070 if (mounts == 0)
3071 return;
3072
3073 if (ui.u_busy > 0) { // Busy mounts - wait & retry
3074 tsleep(&nummounts, PVFS, "busy mount", sec * hz);
3075 sec *= 2;
3076 if (sec <= 32)
3077 goto retry;
3078 printf("Unmounting timed out\n");
3079 } else if (ui.u_errs < mounts) {
3080 // If the vfs_iterate missed mounts in progress - wait a bit
3081 tsleep(&nummounts, PVFS, "missed mount", 2 * hz);
3082 }
3083 }
3084
3085 /*
3086 * This routine is called from vnode_pager_deallocate out of the VM
3087 * The path to vnode_pager_deallocate can only be initiated by ubc_destroy_named
3088 * on a vnode that has a UBCINFO
3089 */
3090 __private_extern__ void
3091 vnode_pager_vrele(vnode_t vp)
3092 {
3093 struct ubc_info *uip;
3094
3095 vnode_lock_spin(vp);
3096
3097 vp->v_lflag &= ~VNAMED_UBC;
3098 if (vp->v_usecount != 0) {
3099 /*
3100 * At the eleventh hour, just before the ubcinfo is
3101 * destroyed, ensure the ubc-specific v_usecount
3102 * reference has gone. We use v_usecount != 0 as a hint;
3103 * ubc_unmap() does nothing if there's no mapping.
3104 *
3105 * This case is caused by coming here via forced unmount,
3106 * versus the usual vm_object_deallocate() path.
3107 * In the forced unmount case, ubc_destroy_named()
3108 * releases the pager before memory_object_last_unmap()
3109 * can be called.
3110 */
3111 vnode_unlock(vp);
3112 ubc_unmap(vp);
3113 vnode_lock_spin(vp);
3114 }
3115
3116 uip = vp->v_ubcinfo;
3117 vp->v_ubcinfo = UBC_INFO_NULL;
3118
3119 vnode_unlock(vp);
3120
3121 ubc_info_deallocate(uip);
3122 }
3123
3124
3125 #include <sys/disk.h>
3126
3127 u_int32_t rootunit = (u_int32_t)-1;
3128
3129 #if CONFIG_IOSCHED
3130 extern int lowpri_throttle_enabled;
3131 extern int iosched_enabled;
3132 #endif
3133
3134 errno_t
3135 vfs_init_io_attributes(vnode_t devvp, mount_t mp)
3136 {
3137 int error;
3138 off_t readblockcnt = 0;
3139 off_t writeblockcnt = 0;
3140 off_t readmaxcnt = 0;
3141 off_t writemaxcnt = 0;
3142 off_t readsegcnt = 0;
3143 off_t writesegcnt = 0;
3144 off_t readsegsize = 0;
3145 off_t writesegsize = 0;
3146 off_t alignment = 0;
3147 u_int32_t minsaturationbytecount = 0;
3148 u_int32_t ioqueue_depth = 0;
3149 u_int32_t blksize;
3150 u_int64_t temp;
3151 u_int32_t features;
3152 vfs_context_t ctx = vfs_context_current();
3153 dk_corestorage_info_t cs_info;
3154 boolean_t cs_present = FALSE;;
3155 int isssd = 0;
3156 int isvirtual = 0;
3157
3158
3159 VNOP_IOCTL(devvp, DKIOCGETTHROTTLEMASK, (caddr_t)&mp->mnt_throttle_mask, 0, NULL);
3160 /*
3161 * as a reasonable approximation, only use the lowest bit of the mask
3162 * to generate a disk unit number
3163 */
3164 mp->mnt_devbsdunit = num_trailing_0(mp->mnt_throttle_mask);
3165
3166 if (devvp == rootvp)
3167 rootunit = mp->mnt_devbsdunit;
3168
3169 if (mp->mnt_devbsdunit == rootunit) {
3170 /*
3171 * this mount point exists on the same device as the root
3172 * partition, so it comes under the hard throttle control...
3173 * this is true even for the root mount point itself
3174 */
3175 mp->mnt_kern_flag |= MNTK_ROOTDEV;
3176 }
3177 /*
3178 * force the spec device to re-cache
3179 * the underlying block size in case
3180 * the filesystem overrode the initial value
3181 */
3182 set_fsblocksize(devvp);
3183
3184
3185 if ((error = VNOP_IOCTL(devvp, DKIOCGETBLOCKSIZE,
3186 (caddr_t)&blksize, 0, ctx)))
3187 return (error);
3188
3189 mp->mnt_devblocksize = blksize;
3190
3191 /*
3192 * set the maximum possible I/O size
3193 * this may get clipped to a smaller value
3194 * based on which constraints are being advertised
3195 * and if those advertised constraints result in a smaller
3196 * limit for a given I/O
3197 */
3198 mp->mnt_maxreadcnt = MAX_UPL_SIZE_BYTES;
3199 mp->mnt_maxwritecnt = MAX_UPL_SIZE_BYTES;
3200
3201 if (VNOP_IOCTL(devvp, DKIOCISVIRTUAL, (caddr_t)&isvirtual, 0, ctx) == 0) {
3202 if (isvirtual)
3203 mp->mnt_kern_flag |= MNTK_VIRTUALDEV;
3204 }
3205 if (VNOP_IOCTL(devvp, DKIOCISSOLIDSTATE, (caddr_t)&isssd, 0, ctx) == 0) {
3206 if (isssd)
3207 mp->mnt_kern_flag |= MNTK_SSD;
3208 }
3209 if ((error = VNOP_IOCTL(devvp, DKIOCGETFEATURES,
3210 (caddr_t)&features, 0, ctx)))
3211 return (error);
3212
3213 if ((error = VNOP_IOCTL(devvp, DKIOCGETMAXBLOCKCOUNTREAD,
3214 (caddr_t)&readblockcnt, 0, ctx)))
3215 return (error);
3216
3217 if ((error = VNOP_IOCTL(devvp, DKIOCGETMAXBLOCKCOUNTWRITE,
3218 (caddr_t)&writeblockcnt, 0, ctx)))
3219 return (error);
3220
3221 if ((error = VNOP_IOCTL(devvp, DKIOCGETMAXBYTECOUNTREAD,
3222 (caddr_t)&readmaxcnt, 0, ctx)))
3223 return (error);
3224
3225 if ((error = VNOP_IOCTL(devvp, DKIOCGETMAXBYTECOUNTWRITE,
3226 (caddr_t)&writemaxcnt, 0, ctx)))
3227 return (error);
3228
3229 if ((error = VNOP_IOCTL(devvp, DKIOCGETMAXSEGMENTCOUNTREAD,
3230 (caddr_t)&readsegcnt, 0, ctx)))
3231 return (error);
3232
3233 if ((error = VNOP_IOCTL(devvp, DKIOCGETMAXSEGMENTCOUNTWRITE,
3234 (caddr_t)&writesegcnt, 0, ctx)))
3235 return (error);
3236
3237 if ((error = VNOP_IOCTL(devvp, DKIOCGETMAXSEGMENTBYTECOUNTREAD,
3238 (caddr_t)&readsegsize, 0, ctx)))
3239 return (error);
3240
3241 if ((error = VNOP_IOCTL(devvp, DKIOCGETMAXSEGMENTBYTECOUNTWRITE,
3242 (caddr_t)&writesegsize, 0, ctx)))
3243 return (error);
3244
3245 if ((error = VNOP_IOCTL(devvp, DKIOCGETMINSEGMENTALIGNMENTBYTECOUNT,
3246 (caddr_t)&alignment, 0, ctx)))
3247 return (error);
3248
3249 if ((error = VNOP_IOCTL(devvp, DKIOCGETCOMMANDPOOLSIZE,
3250 (caddr_t)&ioqueue_depth, 0, ctx)))
3251 return (error);
3252
3253 if (readmaxcnt)
3254 mp->mnt_maxreadcnt = (readmaxcnt > UINT32_MAX) ? UINT32_MAX : readmaxcnt;
3255
3256 if (readblockcnt) {
3257 temp = readblockcnt * blksize;
3258 temp = (temp > UINT32_MAX) ? UINT32_MAX : temp;
3259
3260 if (temp < mp->mnt_maxreadcnt)
3261 mp->mnt_maxreadcnt = (u_int32_t)temp;
3262 }
3263
3264 if (writemaxcnt)
3265 mp->mnt_maxwritecnt = (writemaxcnt > UINT32_MAX) ? UINT32_MAX : writemaxcnt;
3266
3267 if (writeblockcnt) {
3268 temp = writeblockcnt * blksize;
3269 temp = (temp > UINT32_MAX) ? UINT32_MAX : temp;
3270
3271 if (temp < mp->mnt_maxwritecnt)
3272 mp->mnt_maxwritecnt = (u_int32_t)temp;
3273 }
3274
3275 if (readsegcnt) {
3276 temp = (readsegcnt > UINT16_MAX) ? UINT16_MAX : readsegcnt;
3277 } else {
3278 temp = mp->mnt_maxreadcnt / PAGE_SIZE;
3279
3280 if (temp > UINT16_MAX)
3281 temp = UINT16_MAX;
3282 }
3283 mp->mnt_segreadcnt = (u_int16_t)temp;
3284
3285 if (writesegcnt) {
3286 temp = (writesegcnt > UINT16_MAX) ? UINT16_MAX : writesegcnt;
3287 } else {
3288 temp = mp->mnt_maxwritecnt / PAGE_SIZE;
3289
3290 if (temp > UINT16_MAX)
3291 temp = UINT16_MAX;
3292 }
3293 mp->mnt_segwritecnt = (u_int16_t)temp;
3294
3295 if (readsegsize)
3296 temp = (readsegsize > UINT32_MAX) ? UINT32_MAX : readsegsize;
3297 else
3298 temp = mp->mnt_maxreadcnt;
3299 mp->mnt_maxsegreadsize = (u_int32_t)temp;
3300
3301 if (writesegsize)
3302 temp = (writesegsize > UINT32_MAX) ? UINT32_MAX : writesegsize;
3303 else
3304 temp = mp->mnt_maxwritecnt;
3305 mp->mnt_maxsegwritesize = (u_int32_t)temp;
3306
3307 if (alignment)
3308 temp = (alignment > PAGE_SIZE) ? PAGE_MASK : alignment - 1;
3309 else
3310 temp = 0;
3311 mp->mnt_alignmentmask = temp;
3312
3313
3314 if (ioqueue_depth > MNT_DEFAULT_IOQUEUE_DEPTH)
3315 temp = ioqueue_depth;
3316 else
3317 temp = MNT_DEFAULT_IOQUEUE_DEPTH;
3318
3319 mp->mnt_ioqueue_depth = temp;
3320 mp->mnt_ioscale = (mp->mnt_ioqueue_depth + (MNT_DEFAULT_IOQUEUE_DEPTH - 1)) / MNT_DEFAULT_IOQUEUE_DEPTH;
3321
3322 if (mp->mnt_ioscale > 1)
3323 printf("ioqueue_depth = %d, ioscale = %d\n", (int)mp->mnt_ioqueue_depth, (int)mp->mnt_ioscale);
3324
3325 if (features & DK_FEATURE_FORCE_UNIT_ACCESS)
3326 mp->mnt_ioflags |= MNT_IOFLAGS_FUA_SUPPORTED;
3327
3328 if (VNOP_IOCTL(devvp, DKIOCGETIOMINSATURATIONBYTECOUNT, (caddr_t)&minsaturationbytecount, 0, ctx) == 0) {
3329 mp->mnt_minsaturationbytecount = minsaturationbytecount;
3330 } else {
3331 mp->mnt_minsaturationbytecount = 0;
3332 }
3333
3334 if (VNOP_IOCTL(devvp, DKIOCCORESTORAGE, (caddr_t)&cs_info, 0, ctx) == 0)
3335 cs_present = TRUE;
3336
3337 if (features & DK_FEATURE_UNMAP) {
3338 mp->mnt_ioflags |= MNT_IOFLAGS_UNMAP_SUPPORTED;
3339
3340 if (cs_present == TRUE)
3341 mp->mnt_ioflags |= MNT_IOFLAGS_CSUNMAP_SUPPORTED;
3342 }
3343 if (cs_present == TRUE) {
3344 /*
3345 * for now we'll use the following test as a proxy for
3346 * the underlying drive being FUSION in nature
3347 */
3348 if ((cs_info.flags & DK_CORESTORAGE_PIN_YOUR_METADATA))
3349 mp->mnt_ioflags |= MNT_IOFLAGS_FUSION_DRIVE;
3350 }
3351
3352 #if CONFIG_IOSCHED
3353 if (iosched_enabled && (features & DK_FEATURE_PRIORITY)) {
3354 mp->mnt_ioflags |= MNT_IOFLAGS_IOSCHED_SUPPORTED;
3355 throttle_info_disable_throttle(mp->mnt_devbsdunit, (mp->mnt_ioflags & MNT_IOFLAGS_FUSION_DRIVE) != 0);
3356 }
3357 #endif /* CONFIG_IOSCHED */
3358 return (error);
3359 }
3360
3361 static struct klist fs_klist;
3362 lck_grp_t *fs_klist_lck_grp;
3363 lck_mtx_t *fs_klist_lock;
3364
3365 void
3366 vfs_event_init(void)
3367 {
3368
3369 klist_init(&fs_klist);
3370 fs_klist_lck_grp = lck_grp_alloc_init("fs_klist", NULL);
3371 fs_klist_lock = lck_mtx_alloc_init(fs_klist_lck_grp, NULL);
3372 }
3373
3374 void
3375 vfs_event_signal(fsid_t *fsid, u_int32_t event, intptr_t data)
3376 {
3377 if (event == VQ_DEAD || event == VQ_NOTRESP) {
3378 struct mount *mp = vfs_getvfs(fsid);
3379 if (mp) {
3380 mount_lock_spin(mp);
3381 if (data)
3382 mp->mnt_kern_flag &= ~MNT_LNOTRESP; // Now responding
3383 else
3384 mp->mnt_kern_flag |= MNT_LNOTRESP; // Not responding
3385 mount_unlock(mp);
3386 }
3387 }
3388
3389 lck_mtx_lock(fs_klist_lock);
3390 KNOTE(&fs_klist, event);
3391 lck_mtx_unlock(fs_klist_lock);
3392 }
3393
3394 /*
3395 * return the number of mounted filesystems.
3396 */
3397 static int
3398 sysctl_vfs_getvfscnt(void)
3399 {
3400 return(mount_getvfscnt());
3401 }
3402
3403
3404 static int
3405 mount_getvfscnt(void)
3406 {
3407 int ret;
3408
3409 mount_list_lock();
3410 ret = nummounts;
3411 mount_list_unlock();
3412 return (ret);
3413
3414 }
3415
3416
3417
3418 static int
3419 mount_fillfsids(fsid_t *fsidlst, int count)
3420 {
3421 struct mount *mp;
3422 int actual=0;
3423
3424 actual = 0;
3425 mount_list_lock();
3426 TAILQ_FOREACH(mp, &mountlist, mnt_list) {
3427 if (actual <= count) {
3428 fsidlst[actual] = mp->mnt_vfsstat.f_fsid;
3429 actual++;
3430 }
3431 }
3432 mount_list_unlock();
3433 return (actual);
3434
3435 }
3436
3437 /*
3438 * fill in the array of fsid_t's up to a max of 'count', the actual
3439 * number filled in will be set in '*actual'. If there are more fsid_t's
3440 * than room in fsidlst then ENOMEM will be returned and '*actual' will
3441 * have the actual count.
3442 * having *actual filled out even in the error case is depended upon.
3443 */
3444 static int
3445 sysctl_vfs_getvfslist(fsid_t *fsidlst, int count, int *actual)
3446 {
3447 struct mount *mp;
3448
3449 *actual = 0;
3450 mount_list_lock();
3451 TAILQ_FOREACH(mp, &mountlist, mnt_list) {
3452 (*actual)++;
3453 if (*actual <= count)
3454 fsidlst[(*actual) - 1] = mp->mnt_vfsstat.f_fsid;
3455 }
3456 mount_list_unlock();
3457 return (*actual <= count ? 0 : ENOMEM);
3458 }
3459
3460 static int
3461 sysctl_vfs_vfslist(__unused struct sysctl_oid *oidp, __unused void *arg1,
3462 __unused int arg2, struct sysctl_req *req)
3463 {
3464 int actual, error;
3465 size_t space;
3466 fsid_t *fsidlst;
3467
3468 /* This is a readonly node. */
3469 if (req->newptr != USER_ADDR_NULL)
3470 return (EPERM);
3471
3472 /* they are querying us so just return the space required. */
3473 if (req->oldptr == USER_ADDR_NULL) {
3474 req->oldidx = sysctl_vfs_getvfscnt() * sizeof(fsid_t);
3475 return 0;
3476 }
3477 again:
3478 /*
3479 * Retrieve an accurate count of the amount of space required to copy
3480 * out all the fsids in the system.
3481 */
3482 space = req->oldlen;
3483 req->oldlen = sysctl_vfs_getvfscnt() * sizeof(fsid_t);
3484
3485 /* they didn't give us enough space. */
3486 if (space < req->oldlen)
3487 return (ENOMEM);
3488
3489 MALLOC(fsidlst, fsid_t *, req->oldlen, M_TEMP, M_WAITOK);
3490 if (fsidlst == NULL) {
3491 return (ENOMEM);
3492 }
3493
3494 error = sysctl_vfs_getvfslist(fsidlst, req->oldlen / sizeof(fsid_t),
3495 &actual);
3496 /*
3497 * If we get back ENOMEM, then another mount has been added while we
3498 * slept in malloc above. If this is the case then try again.
3499 */
3500 if (error == ENOMEM) {
3501 FREE(fsidlst, M_TEMP);
3502 req->oldlen = space;
3503 goto again;
3504 }
3505 if (error == 0) {
3506 error = SYSCTL_OUT(req, fsidlst, actual * sizeof(fsid_t));
3507 }
3508 FREE(fsidlst, M_TEMP);
3509 return (error);
3510 }
3511
3512 /*
3513 * Do a sysctl by fsid.
3514 */
3515 static int
3516 sysctl_vfs_ctlbyfsid(__unused struct sysctl_oid *oidp, void *arg1, int arg2,
3517 struct sysctl_req *req)
3518 {
3519 union union_vfsidctl vc;
3520 struct mount *mp;
3521 struct vfsstatfs *sp;
3522 int *name, flags, namelen;
3523 int error=0, gotref=0;
3524 vfs_context_t ctx = vfs_context_current();
3525 proc_t p = req->p; /* XXX req->p != current_proc()? */
3526 boolean_t is_64_bit;
3527
3528 name = arg1;
3529 namelen = arg2;
3530 is_64_bit = proc_is64bit(p);
3531
3532 error = SYSCTL_IN(req, &vc, is_64_bit? sizeof(vc.vc64):sizeof(vc.vc32));
3533 if (error)
3534 goto out;
3535 if (vc.vc32.vc_vers != VFS_CTL_VERS1) { /* works for 32 and 64 */
3536 error = EINVAL;
3537 goto out;
3538 }
3539 mp = mount_list_lookupby_fsid(&vc.vc32.vc_fsid, 0, 1); /* works for 32 and 64 */
3540 if (mp == NULL) {
3541 error = ENOENT;
3542 goto out;
3543 }
3544 gotref = 1;
3545 /* reset so that the fs specific code can fetch it. */
3546 req->newidx = 0;
3547 /*
3548 * Note if this is a VFS_CTL then we pass the actual sysctl req
3549 * in for "oldp" so that the lower layer can DTRT and use the
3550 * SYSCTL_IN/OUT routines.
3551 */
3552 if (mp->mnt_op->vfs_sysctl != NULL) {
3553 if (is_64_bit) {
3554 if (vfs_64bitready(mp)) {
3555 error = mp->mnt_op->vfs_sysctl(name, namelen,
3556 CAST_USER_ADDR_T(req),
3557 NULL, USER_ADDR_NULL, 0,
3558 ctx);
3559 }
3560 else {
3561 error = ENOTSUP;
3562 }
3563 }
3564 else {
3565 error = mp->mnt_op->vfs_sysctl(name, namelen,
3566 CAST_USER_ADDR_T(req),
3567 NULL, USER_ADDR_NULL, 0,
3568 ctx);
3569 }
3570 if (error != ENOTSUP) {
3571 goto out;
3572 }
3573 }
3574 switch (name[0]) {
3575 case VFS_CTL_UMOUNT:
3576 req->newidx = 0;
3577 if (is_64_bit) {
3578 req->newptr = vc.vc64.vc_ptr;
3579 req->newlen = (size_t)vc.vc64.vc_len;
3580 }
3581 else {
3582 req->newptr = CAST_USER_ADDR_T(vc.vc32.vc_ptr);
3583 req->newlen = vc.vc32.vc_len;
3584 }
3585 error = SYSCTL_IN(req, &flags, sizeof(flags));
3586 if (error)
3587 break;
3588
3589 mount_ref(mp, 0);
3590 mount_iterdrop(mp);
3591 gotref = 0;
3592 /* safedounmount consumes a ref */
3593 error = safedounmount(mp, flags, ctx);
3594 break;
3595 case VFS_CTL_STATFS:
3596 req->newidx = 0;
3597 if (is_64_bit) {
3598 req->newptr = vc.vc64.vc_ptr;
3599 req->newlen = (size_t)vc.vc64.vc_len;
3600 }
3601 else {
3602 req->newptr = CAST_USER_ADDR_T(vc.vc32.vc_ptr);
3603 req->newlen = vc.vc32.vc_len;
3604 }
3605 error = SYSCTL_IN(req, &flags, sizeof(flags));
3606 if (error)
3607 break;
3608 sp = &mp->mnt_vfsstat;
3609 if (((flags & MNT_NOWAIT) == 0 || (flags & (MNT_WAIT | MNT_DWAIT))) &&
3610 (error = vfs_update_vfsstat(mp, ctx, VFS_USER_EVENT)))
3611 goto out;
3612 if (is_64_bit) {
3613 struct user64_statfs sfs;
3614 bzero(&sfs, sizeof(sfs));
3615 sfs.f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
3616 sfs.f_type = mp->mnt_vtable->vfc_typenum;
3617 sfs.f_bsize = (user64_long_t)sp->f_bsize;
3618 sfs.f_iosize = (user64_long_t)sp->f_iosize;
3619 sfs.f_blocks = (user64_long_t)sp->f_blocks;
3620 sfs.f_bfree = (user64_long_t)sp->f_bfree;
3621 sfs.f_bavail = (user64_long_t)sp->f_bavail;
3622 sfs.f_files = (user64_long_t)sp->f_files;
3623 sfs.f_ffree = (user64_long_t)sp->f_ffree;
3624 sfs.f_fsid = sp->f_fsid;
3625 sfs.f_owner = sp->f_owner;
3626 #ifdef NFSCLIENT
3627 if (mp->mnt_kern_flag & MNTK_TYPENAME_OVERRIDE) {
3628 strlcpy(&sfs.f_fstypename[0], &mp->fstypename_override[0], MFSTYPENAMELEN);
3629 } else
3630 #endif
3631 {
3632 strlcpy(sfs.f_fstypename, sp->f_fstypename, MFSNAMELEN);
3633 }
3634 strlcpy(sfs.f_mntonname, sp->f_mntonname, MNAMELEN);
3635 strlcpy(sfs.f_mntfromname, sp->f_mntfromname, MNAMELEN);
3636
3637 error = SYSCTL_OUT(req, &sfs, sizeof(sfs));
3638 }
3639 else {
3640 struct user32_statfs sfs;
3641 bzero(&sfs, sizeof(sfs));
3642 sfs.f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
3643 sfs.f_type = mp->mnt_vtable->vfc_typenum;
3644
3645 /*
3646 * It's possible for there to be more than 2^^31 blocks in the filesystem, so we
3647 * have to fudge the numbers here in that case. We inflate the blocksize in order
3648 * to reflect the filesystem size as best we can.
3649 */
3650 if (sp->f_blocks > INT_MAX) {
3651 int shift;
3652
3653 /*
3654 * Work out how far we have to shift the block count down to make it fit.
3655 * Note that it's possible to have to shift so far that the resulting
3656 * blocksize would be unreportably large. At that point, we will clip
3657 * any values that don't fit.
3658 *
3659 * For safety's sake, we also ensure that f_iosize is never reported as
3660 * being smaller than f_bsize.
3661 */
3662 for (shift = 0; shift < 32; shift++) {
3663 if ((sp->f_blocks >> shift) <= INT_MAX)
3664 break;
3665 if ((((long long)sp->f_bsize) << (shift + 1)) > INT_MAX)
3666 break;
3667 }
3668 #define __SHIFT_OR_CLIP(x, s) ((((x) >> (s)) > INT_MAX) ? INT_MAX : ((x) >> (s)))
3669 sfs.f_blocks = (user32_long_t)__SHIFT_OR_CLIP(sp->f_blocks, shift);
3670 sfs.f_bfree = (user32_long_t)__SHIFT_OR_CLIP(sp->f_bfree, shift);
3671 sfs.f_bavail = (user32_long_t)__SHIFT_OR_CLIP(sp->f_bavail, shift);
3672 #undef __SHIFT_OR_CLIP
3673 sfs.f_bsize = (user32_long_t)(sp->f_bsize << shift);
3674 sfs.f_iosize = lmax(sp->f_iosize, sp->f_bsize);
3675 } else {
3676 sfs.f_bsize = (user32_long_t)sp->f_bsize;
3677 sfs.f_iosize = (user32_long_t)sp->f_iosize;
3678 sfs.f_blocks = (user32_long_t)sp->f_blocks;
3679 sfs.f_bfree = (user32_long_t)sp->f_bfree;
3680 sfs.f_bavail = (user32_long_t)sp->f_bavail;
3681 }
3682 sfs.f_files = (user32_long_t)sp->f_files;
3683 sfs.f_ffree = (user32_long_t)sp->f_ffree;
3684 sfs.f_fsid = sp->f_fsid;
3685 sfs.f_owner = sp->f_owner;
3686
3687 #ifdef NFS_CLIENT
3688 if (mp->mnt_kern_flag & MNTK_TYPENAME_OVERRIDE) {
3689 strlcpy(&sfs.f_fstypename[0], &mp->fstypename_override[0], MFSTYPENAMELEN);
3690 } else
3691 #endif
3692 {
3693 strlcpy(sfs.f_fstypename, sp->f_fstypename, MFSNAMELEN);
3694 }
3695 strlcpy(sfs.f_mntonname, sp->f_mntonname, MNAMELEN);
3696 strlcpy(sfs.f_mntfromname, sp->f_mntfromname, MNAMELEN);
3697
3698 error = SYSCTL_OUT(req, &sfs, sizeof(sfs));
3699 }
3700 break;
3701 default:
3702 error = ENOTSUP;
3703 goto out;
3704 }
3705 out:
3706 if(gotref != 0)
3707 mount_iterdrop(mp);
3708 return (error);
3709 }
3710
3711 static int filt_fsattach(struct knote *kn);
3712 static void filt_fsdetach(struct knote *kn);
3713 static int filt_fsevent(struct knote *kn, long hint);
3714 static int filt_fstouch(struct knote *kn, struct kevent_internal_s *kev);
3715 static int filt_fsprocess(struct knote *kn, struct filt_process_s *data, struct kevent_internal_s *kev);
3716 struct filterops fs_filtops = {
3717 .f_attach = filt_fsattach,
3718 .f_detach = filt_fsdetach,
3719 .f_event = filt_fsevent,
3720 .f_touch = filt_fstouch,
3721 .f_process = filt_fsprocess,
3722 };
3723
3724 static int
3725 filt_fsattach(struct knote *kn)
3726 {
3727 lck_mtx_lock(fs_klist_lock);
3728 KNOTE_ATTACH(&fs_klist, kn);
3729 lck_mtx_unlock(fs_klist_lock);
3730
3731 /*
3732 * filter only sees future events,
3733 * so it can't be fired already.
3734 */
3735 return (0);
3736 }
3737
3738 static void
3739 filt_fsdetach(struct knote *kn)
3740 {
3741 lck_mtx_lock(fs_klist_lock);
3742 KNOTE_DETACH(&fs_klist, kn);
3743 lck_mtx_unlock(fs_klist_lock);
3744 }
3745
3746 static int
3747 filt_fsevent(struct knote *kn, long hint)
3748 {
3749 /*
3750 * Backwards compatibility:
3751 * Other filters would do nothing if kn->kn_sfflags == 0
3752 */
3753
3754 if ((kn->kn_sfflags == 0) || (kn->kn_sfflags & hint)) {
3755 kn->kn_fflags |= hint;
3756 }
3757
3758 return (kn->kn_fflags != 0);
3759 }
3760
3761 static int
3762 filt_fstouch(struct knote *kn, struct kevent_internal_s *kev)
3763 {
3764 int res;
3765
3766 lck_mtx_lock(fs_klist_lock);
3767
3768 kn->kn_sfflags = kev->fflags;
3769 if ((kn->kn_status & KN_UDATA_SPECIFIC) == 0)
3770 kn->kn_udata = kev->udata;
3771
3772 /*
3773 * the above filter function sets bits even if nobody is looking for them.
3774 * Just preserve those bits even in the new mask is more selective
3775 * than before.
3776 *
3777 * For compatibility with previous implementations, we leave kn_fflags
3778 * as they were before.
3779 */
3780 //if (kn->kn_sfflags)
3781 // kn->kn_fflags &= kn->kn_sfflags;
3782 res = (kn->kn_fflags != 0);
3783
3784 lck_mtx_unlock(fs_klist_lock);
3785
3786 return res;
3787 }
3788
3789 static int
3790 filt_fsprocess(struct knote *kn, struct filt_process_s *data, struct kevent_internal_s *kev)
3791 {
3792 #pragma unused(data)
3793 int res;
3794
3795 lck_mtx_lock(fs_klist_lock);
3796 res = (kn->kn_fflags != 0);
3797 if (res) {
3798 *kev = kn->kn_kevent;
3799 kn->kn_flags |= EV_CLEAR; /* automatic */
3800 kn->kn_fflags = 0;
3801 kn->kn_data = 0;
3802 }
3803 lck_mtx_unlock(fs_klist_lock);
3804 return res;
3805 }
3806
3807 static int
3808 sysctl_vfs_noremotehang(__unused struct sysctl_oid *oidp,
3809 __unused void *arg1, __unused int arg2, struct sysctl_req *req)
3810 {
3811 int out, error;
3812 pid_t pid;
3813 proc_t p;
3814
3815 /* We need a pid. */
3816 if (req->newptr == USER_ADDR_NULL)
3817 return (EINVAL);
3818
3819 error = SYSCTL_IN(req, &pid, sizeof(pid));
3820 if (error)
3821 return (error);
3822
3823 p = proc_find(pid < 0 ? -pid : pid);
3824 if (p == NULL)
3825 return (ESRCH);
3826
3827 /*
3828 * Fetching the value is ok, but we only fetch if the old
3829 * pointer is given.
3830 */
3831 if (req->oldptr != USER_ADDR_NULL) {
3832 out = !((p->p_flag & P_NOREMOTEHANG) == 0);
3833 proc_rele(p);
3834 error = SYSCTL_OUT(req, &out, sizeof(out));
3835 return (error);
3836 }
3837
3838 /* cansignal offers us enough security. */
3839 if (p != req->p && proc_suser(req->p) != 0) {
3840 proc_rele(p);
3841 return (EPERM);
3842 }
3843
3844 if (pid < 0)
3845 OSBitAndAtomic(~((uint32_t)P_NOREMOTEHANG), &p->p_flag);
3846 else
3847 OSBitOrAtomic(P_NOREMOTEHANG, &p->p_flag);
3848 proc_rele(p);
3849
3850 return (0);
3851 }
3852
3853 static int
3854 sysctl_vfs_generic_conf SYSCTL_HANDLER_ARGS
3855 {
3856 int *name, namelen;
3857 struct vfstable *vfsp;
3858 struct vfsconf vfsc;
3859
3860 (void)oidp;
3861 name = arg1;
3862 namelen = arg2;
3863
3864 if (namelen < 1) {
3865 return (EISDIR);
3866 } else if (namelen > 1) {
3867 return (ENOTDIR);
3868 }
3869
3870 mount_list_lock();
3871 for (vfsp = vfsconf; vfsp; vfsp = vfsp->vfc_next)
3872 if (vfsp->vfc_typenum == name[0])
3873 break;
3874
3875 if (vfsp == NULL) {
3876 mount_list_unlock();
3877 return (ENOTSUP);
3878 }
3879
3880 vfsc.vfc_reserved1 = 0;
3881 bcopy(vfsp->vfc_name, vfsc.vfc_name, sizeof(vfsc.vfc_name));
3882 vfsc.vfc_typenum = vfsp->vfc_typenum;
3883 vfsc.vfc_refcount = vfsp->vfc_refcount;
3884 vfsc.vfc_flags = vfsp->vfc_flags;
3885 vfsc.vfc_reserved2 = 0;
3886 vfsc.vfc_reserved3 = 0;
3887
3888 mount_list_unlock();
3889 return (SYSCTL_OUT(req, &vfsc, sizeof(struct vfsconf)));
3890 }
3891
3892 /* the vfs.generic. branch. */
3893 SYSCTL_NODE(_vfs, VFS_GENERIC, generic, CTLFLAG_RW | CTLFLAG_LOCKED, NULL, "vfs generic hinge");
3894 /* retreive a list of mounted filesystem fsid_t */
3895 SYSCTL_PROC(_vfs_generic, OID_AUTO, vfsidlist,
3896 CTLTYPE_STRUCT | CTLFLAG_RD | CTLFLAG_LOCKED,
3897 NULL, 0, sysctl_vfs_vfslist, "S,fsid", "List of mounted filesystem ids");
3898 /* perform operations on filesystem via fsid_t */
3899 SYSCTL_NODE(_vfs_generic, OID_AUTO, ctlbyfsid, CTLFLAG_RW | CTLFLAG_LOCKED,
3900 sysctl_vfs_ctlbyfsid, "ctlbyfsid");
3901 SYSCTL_PROC(_vfs_generic, OID_AUTO, noremotehang, CTLFLAG_RW | CTLFLAG_ANYBODY,
3902 NULL, 0, sysctl_vfs_noremotehang, "I", "noremotehang");
3903 SYSCTL_INT(_vfs_generic, VFS_MAXTYPENUM, maxtypenum,
3904 CTLFLAG_RD | CTLFLAG_KERN | CTLFLAG_LOCKED,
3905 &maxvfstypenum, 0, "");
3906 SYSCTL_INT(_vfs_generic, OID_AUTO, sync_timeout, CTLFLAG_RW | CTLFLAG_LOCKED, &sync_timeout, 0, "");
3907 SYSCTL_NODE(_vfs_generic, VFS_CONF, conf,
3908 CTLFLAG_RD | CTLFLAG_LOCKED,
3909 sysctl_vfs_generic_conf, "");
3910
3911 /*
3912 * Print vnode state.
3913 */
3914 void
3915 vn_print_state(struct vnode *vp, const char *fmt, ...)
3916 {
3917 va_list ap;
3918 char perm_str[] = "(VM_KERNEL_ADDRPERM pointer)";
3919 char fs_name[MFSNAMELEN];
3920
3921 va_start(ap, fmt);
3922 vprintf(fmt, ap);
3923 va_end(ap);
3924 printf("vp 0x%0llx %s: ", (uint64_t)VM_KERNEL_ADDRPERM(vp), perm_str);
3925 printf("tag %d, type %d\n", vp->v_tag, vp->v_type);
3926 /* Counts .. */
3927 printf(" iocount %d, usecount %d, kusecount %d references %d\n",
3928 vp->v_iocount, vp->v_usecount, vp->v_kusecount, vp->v_references);
3929 printf(" writecount %d, numoutput %d\n", vp->v_writecount,
3930 vp->v_numoutput);
3931 /* Flags */
3932 printf(" flag 0x%x, lflag 0x%x, listflag 0x%x\n", vp->v_flag,
3933 vp->v_lflag, vp->v_listflag);
3934
3935 if (vp->v_mount == NULL || vp->v_mount == dead_mountp) {
3936 strlcpy(fs_name, "deadfs", MFSNAMELEN);
3937 } else {
3938 vfs_name(vp->v_mount, fs_name);
3939 }
3940
3941 printf(" v_data 0x%0llx %s\n",
3942 (vp->v_data ? (uint64_t)VM_KERNEL_ADDRPERM(vp->v_data) : 0),
3943 perm_str);
3944 printf(" v_mount 0x%0llx %s vfs_name %s\n",
3945 (vp->v_mount ? (uint64_t)VM_KERNEL_ADDRPERM(vp->v_mount) : 0),
3946 perm_str, fs_name);
3947 }
3948
3949 long num_reusedvnodes = 0;
3950
3951
3952 static vnode_t
3953 process_vp(vnode_t vp, int want_vp, int *deferred)
3954 {
3955 unsigned int vpid;
3956
3957 *deferred = 0;
3958
3959 vpid = vp->v_id;
3960
3961 vnode_list_remove_locked(vp);
3962
3963 vnode_list_unlock();
3964
3965 vnode_lock_spin(vp);
3966
3967 /*
3968 * We could wait for the vnode_lock after removing the vp from the freelist
3969 * and the vid is bumped only at the very end of reclaim. So it is possible
3970 * that we are looking at a vnode that is being terminated. If so skip it.
3971 */
3972 if ((vpid != vp->v_id) || (vp->v_usecount != 0) || (vp->v_iocount != 0) ||
3973 VONLIST(vp) || (vp->v_lflag & VL_TERMINATE)) {
3974 /*
3975 * we lost the race between dropping the list lock
3976 * and picking up the vnode_lock... someone else
3977 * used this vnode and it is now in a new state
3978 */
3979 vnode_unlock(vp);
3980
3981 return (NULLVP);
3982 }
3983 if ( (vp->v_lflag & (VL_NEEDINACTIVE | VL_MARKTERM)) == VL_NEEDINACTIVE ) {
3984 /*
3985 * we did a vnode_rele_ext that asked for
3986 * us not to reenter the filesystem during
3987 * the release even though VL_NEEDINACTIVE was
3988 * set... we'll do it here by doing a
3989 * vnode_get/vnode_put
3990 *
3991 * pick up an iocount so that we can call
3992 * vnode_put and drive the VNOP_INACTIVE...
3993 * vnode_put will either leave us off
3994 * the freelist if a new ref comes in,
3995 * or put us back on the end of the freelist
3996 * or recycle us if we were marked for termination...
3997 * so we'll just go grab a new candidate
3998 */
3999 vp->v_iocount++;
4000 #ifdef JOE_DEBUG
4001 record_vp(vp, 1);
4002 #endif
4003 vnode_put_locked(vp);
4004 vnode_unlock(vp);
4005
4006 return (NULLVP);
4007 }
4008 /*
4009 * Checks for anyone racing us for recycle
4010 */
4011 if (vp->v_type != VBAD) {
4012 if (want_vp && (vnode_on_reliable_media(vp) == FALSE || (vp->v_flag & VISDIRTY))) {
4013 vnode_async_list_add(vp);
4014 vnode_unlock(vp);
4015
4016 *deferred = 1;
4017
4018 return (NULLVP);
4019 }
4020 if (vp->v_lflag & VL_DEAD)
4021 panic("new_vnode(%p): the vnode is VL_DEAD but not VBAD", vp);
4022
4023 vnode_lock_convert(vp);
4024 (void)vnode_reclaim_internal(vp, 1, want_vp, 0);
4025
4026 if (want_vp) {
4027 if ((VONLIST(vp)))
4028 panic("new_vnode(%p): vp on list", vp);
4029 if (vp->v_usecount || vp->v_iocount || vp->v_kusecount ||
4030 (vp->v_lflag & (VNAMED_UBC | VNAMED_MOUNT | VNAMED_FSHASH)))
4031 panic("new_vnode(%p): free vnode still referenced", vp);
4032 if ((vp->v_mntvnodes.tqe_prev != 0) && (vp->v_mntvnodes.tqe_next != 0))
4033 panic("new_vnode(%p): vnode seems to be on mount list", vp);
4034 if ( !LIST_EMPTY(&vp->v_nclinks) || !TAILQ_EMPTY(&vp->v_ncchildren))
4035 panic("new_vnode(%p): vnode still hooked into the name cache", vp);
4036 } else {
4037 vnode_unlock(vp);
4038 vp = NULLVP;
4039 }
4040 }
4041 return (vp);
4042 }
4043
4044 __attribute__((noreturn))
4045 static void
4046 async_work_continue(void)
4047 {
4048 struct async_work_lst *q;
4049 int deferred;
4050 vnode_t vp;
4051
4052 q = &vnode_async_work_list;
4053
4054 for (;;) {
4055
4056 vnode_list_lock();
4057
4058 if ( TAILQ_EMPTY(q) ) {
4059 assert_wait(q, (THREAD_UNINT));
4060
4061 vnode_list_unlock();
4062
4063 thread_block((thread_continue_t)async_work_continue);
4064
4065 continue;
4066 }
4067 async_work_handled++;
4068
4069 vp = TAILQ_FIRST(q);
4070
4071 vp = process_vp(vp, 0, &deferred);
4072
4073 if (vp != NULLVP)
4074 panic("found VBAD vp (%p) on async queue", vp);
4075 }
4076 }
4077
4078
4079 static int
4080 new_vnode(vnode_t *vpp)
4081 {
4082 vnode_t vp;
4083 uint32_t retries = 0, max_retries = 100; /* retry incase of tablefull */
4084 int force_alloc = 0, walk_count = 0;
4085 boolean_t need_reliable_vp = FALSE;
4086 int deferred;
4087 struct timeval initial_tv;
4088 struct timeval current_tv;
4089 proc_t curproc = current_proc();
4090
4091 initial_tv.tv_sec = 0;
4092 retry:
4093 vp = NULLVP;
4094
4095 vnode_list_lock();
4096
4097 if (need_reliable_vp == TRUE)
4098 async_work_timed_out++;
4099
4100 if ((numvnodes - deadvnodes) < desiredvnodes || force_alloc) {
4101 struct timespec ts;
4102
4103 if ( !TAILQ_EMPTY(&vnode_dead_list)) {
4104 /*
4105 * Can always reuse a dead one
4106 */
4107 vp = TAILQ_FIRST(&vnode_dead_list);
4108 goto steal_this_vp;
4109 }
4110 /*
4111 * no dead vnodes available... if we're under
4112 * the limit, we'll create a new vnode
4113 */
4114 numvnodes++;
4115 vnode_list_unlock();
4116
4117 MALLOC_ZONE(vp, struct vnode *, sizeof(*vp), M_VNODE, M_WAITOK);
4118 bzero((char *)vp, sizeof(*vp));
4119 VLISTNONE(vp); /* avoid double queue removal */
4120 lck_mtx_init(&vp->v_lock, vnode_lck_grp, vnode_lck_attr);
4121
4122 TAILQ_INIT(&vp->v_ncchildren);
4123
4124 klist_init(&vp->v_knotes);
4125 nanouptime(&ts);
4126 vp->v_id = ts.tv_nsec;
4127 vp->v_flag = VSTANDARD;
4128
4129 #if CONFIG_MACF
4130 if (mac_vnode_label_init_needed(vp))
4131 mac_vnode_label_init(vp);
4132 #endif /* MAC */
4133
4134 vp->v_iocount = 1;
4135 goto done;
4136 }
4137 microuptime(&current_tv);
4138
4139 #define MAX_WALK_COUNT 1000
4140
4141 if ( !TAILQ_EMPTY(&vnode_rage_list) &&
4142 (ragevnodes >= rage_limit ||
4143 (current_tv.tv_sec - rage_tv.tv_sec) >= RAGE_TIME_LIMIT)) {
4144
4145 TAILQ_FOREACH(vp, &vnode_rage_list, v_freelist) {
4146 if ( !(vp->v_listflag & VLIST_RAGE))
4147 panic("new_vnode: vp (%p) on RAGE list not marked VLIST_RAGE", vp);
4148
4149 // if we're a dependency-capable process, skip vnodes that can
4150 // cause recycling deadlocks. (i.e. this process is diskimages
4151 // helper and the vnode is in a disk image). Querying the
4152 // mnt_kern_flag for the mount's virtual device status
4153 // is safer than checking the mnt_dependent_process, which
4154 // may not be updated if there are multiple devnode layers
4155 // in between the disk image and the final consumer.
4156
4157 if ((curproc->p_flag & P_DEPENDENCY_CAPABLE) == 0 || vp->v_mount == NULL ||
4158 (vp->v_mount->mnt_kern_flag & MNTK_VIRTUALDEV) == 0) {
4159 /*
4160 * if need_reliable_vp == TRUE, then we've already sent one or more
4161 * non-reliable vnodes to the async thread for processing and timed
4162 * out waiting for a dead vnode to show up. Use the MAX_WALK_COUNT
4163 * mechanism to first scan for a reliable vnode before forcing
4164 * a new vnode to be created
4165 */
4166 if (need_reliable_vp == FALSE || vnode_on_reliable_media(vp) == TRUE)
4167 break;
4168 }
4169
4170 // don't iterate more than MAX_WALK_COUNT vnodes to
4171 // avoid keeping the vnode list lock held for too long.
4172
4173 if (walk_count++ > MAX_WALK_COUNT) {
4174 vp = NULL;
4175 break;
4176 }
4177 }
4178 }
4179
4180 if (vp == NULL && !TAILQ_EMPTY(&vnode_free_list)) {
4181 /*
4182 * Pick the first vp for possible reuse
4183 */
4184 walk_count = 0;
4185 TAILQ_FOREACH(vp, &vnode_free_list, v_freelist) {
4186
4187 // if we're a dependency-capable process, skip vnodes that can
4188 // cause recycling deadlocks. (i.e. this process is diskimages
4189 // helper and the vnode is in a disk image). Querying the
4190 // mnt_kern_flag for the mount's virtual device status
4191 // is safer than checking the mnt_dependent_process, which
4192 // may not be updated if there are multiple devnode layers
4193 // in between the disk image and the final consumer.
4194
4195 if ((curproc->p_flag & P_DEPENDENCY_CAPABLE) == 0 || vp->v_mount == NULL ||
4196 (vp->v_mount->mnt_kern_flag & MNTK_VIRTUALDEV) == 0) {
4197 /*
4198 * if need_reliable_vp == TRUE, then we've already sent one or more
4199 * non-reliable vnodes to the async thread for processing and timed
4200 * out waiting for a dead vnode to show up. Use the MAX_WALK_COUNT
4201 * mechanism to first scan for a reliable vnode before forcing
4202 * a new vnode to be created
4203 */
4204 if (need_reliable_vp == FALSE || vnode_on_reliable_media(vp) == TRUE)
4205 break;
4206 }
4207
4208 // don't iterate more than MAX_WALK_COUNT vnodes to
4209 // avoid keeping the vnode list lock held for too long.
4210
4211 if (walk_count++ > MAX_WALK_COUNT) {
4212 vp = NULL;
4213 break;
4214 }
4215 }
4216 }
4217
4218 //
4219 // if we don't have a vnode and the walk_count is >= MAX_WALK_COUNT
4220 // then we're trying to create a vnode on behalf of a
4221 // process like diskimages-helper that has file systems
4222 // mounted on top of itself (and thus we can't reclaim
4223 // vnodes in the file systems on top of us). if we can't
4224 // find a vnode to reclaim then we'll just have to force
4225 // the allocation.
4226 //
4227 if (vp == NULL && walk_count >= MAX_WALK_COUNT) {
4228 force_alloc = 1;
4229 vnode_list_unlock();
4230 goto retry;
4231 }
4232
4233 if (vp == NULL) {
4234 /*
4235 * we've reached the system imposed maximum number of vnodes
4236 * but there isn't a single one available
4237 * wait a bit and then retry... if we can't get a vnode
4238 * after our target number of retries, than log a complaint
4239 */
4240 if (++retries <= max_retries) {
4241 vnode_list_unlock();
4242 delay_for_interval(1, 1000 * 1000);
4243 goto retry;
4244 }
4245
4246 vnode_list_unlock();
4247 tablefull("vnode");
4248 log(LOG_EMERG, "%d desired, %d numvnodes, "
4249 "%d free, %d dead, %d async, %d rage\n",
4250 desiredvnodes, numvnodes, freevnodes, deadvnodes, async_work_vnodes, ragevnodes);
4251 #if CONFIG_JETSAM
4252
4253 #if DEVELOPMENT || DEBUG
4254 if (bootarg_no_vnode_jetsam)
4255 panic("vnode table is full\n");
4256 #endif /* DEVELOPMENT || DEBUG */
4257
4258 /*
4259 * Running out of vnodes tends to make a system unusable. Start killing
4260 * processes that jetsam knows are killable.
4261 */
4262 if (memorystatus_kill_on_vnode_limit() == FALSE) {
4263 /*
4264 * If jetsam can't find any more processes to kill and there
4265 * still aren't any free vnodes, panic. Hopefully we'll get a
4266 * panic log to tell us why we ran out.
4267 */
4268 panic("vnode table is full\n");
4269 }
4270
4271 /*
4272 * Now that we've killed someone, wait a bit and continue looking
4273 * (with fewer retries before trying another kill).
4274 */
4275 delay_for_interval(3, 1000 * 1000);
4276 retries = 0;
4277 max_retries = 10;
4278 goto retry;
4279 #endif
4280
4281 *vpp = NULL;
4282 return (ENFILE);
4283 }
4284 steal_this_vp:
4285 if ((vp = process_vp(vp, 1, &deferred)) == NULLVP) {
4286 if (deferred) {
4287 int elapsed_msecs;
4288 struct timeval elapsed_tv;
4289
4290 if (initial_tv.tv_sec == 0)
4291 microuptime(&initial_tv);
4292
4293 vnode_list_lock();
4294
4295 dead_vnode_waited++;
4296 dead_vnode_wanted++;
4297
4298 /*
4299 * note that we're only going to explicitly wait 10ms
4300 * for a dead vnode to become available, since even if one
4301 * isn't available, a reliable vnode might now be available
4302 * at the head of the VRAGE or free lists... if so, we
4303 * can satisfy the new_vnode request with less latency then waiting
4304 * for the full 100ms duration we're ultimately willing to tolerate
4305 */
4306 assert_wait_timeout((caddr_t)&dead_vnode_wanted, (THREAD_INTERRUPTIBLE), 10000, NSEC_PER_USEC);
4307
4308 vnode_list_unlock();
4309
4310 thread_block(THREAD_CONTINUE_NULL);
4311
4312 microuptime(&elapsed_tv);
4313
4314 timevalsub(&elapsed_tv, &initial_tv);
4315 elapsed_msecs = elapsed_tv.tv_sec * 1000 + elapsed_tv.tv_usec / 1000;
4316
4317 if (elapsed_msecs >= 100) {
4318 /*
4319 * we've waited long enough... 100ms is
4320 * somewhat arbitrary for this case, but the
4321 * normal worst case latency used for UI
4322 * interaction is 100ms, so I've chosen to
4323 * go with that.
4324 *
4325 * setting need_reliable_vp to TRUE
4326 * forces us to find a reliable vnode
4327 * that we can process synchronously, or
4328 * to create a new one if the scan for
4329 * a reliable one hits the scan limit
4330 */
4331 need_reliable_vp = TRUE;
4332 }
4333 }
4334 goto retry;
4335 }
4336 OSAddAtomicLong(1, &num_reusedvnodes);
4337
4338
4339 #if CONFIG_MACF
4340 /*
4341 * We should never see VL_LABELWAIT or VL_LABEL here.
4342 * as those operations hold a reference.
4343 */
4344 assert ((vp->v_lflag & VL_LABELWAIT) != VL_LABELWAIT);
4345 assert ((vp->v_lflag & VL_LABEL) != VL_LABEL);
4346 if (vp->v_lflag & VL_LABELED) {
4347 vnode_lock_convert(vp);
4348 mac_vnode_label_recycle(vp);
4349 } else if (mac_vnode_label_init_needed(vp)) {
4350 vnode_lock_convert(vp);
4351 mac_vnode_label_init(vp);
4352 }
4353
4354 #endif /* MAC */
4355
4356 vp->v_iocount = 1;
4357 vp->v_lflag = 0;
4358 vp->v_writecount = 0;
4359 vp->v_references = 0;
4360 vp->v_iterblkflags = 0;
4361 vp->v_flag = VSTANDARD;
4362 /* vbad vnodes can point to dead_mountp */
4363 vp->v_mount = NULL;
4364 vp->v_defer_reclaimlist = (vnode_t)0;
4365
4366 vnode_unlock(vp);
4367
4368 done:
4369 *vpp = vp;
4370
4371 return (0);
4372 }
4373
4374 void
4375 vnode_lock(vnode_t vp)
4376 {
4377 lck_mtx_lock(&vp->v_lock);
4378 }
4379
4380 void
4381 vnode_lock_spin(vnode_t vp)
4382 {
4383 lck_mtx_lock_spin(&vp->v_lock);
4384 }
4385
4386 void
4387 vnode_unlock(vnode_t vp)
4388 {
4389 lck_mtx_unlock(&vp->v_lock);
4390 }
4391
4392
4393
4394 int
4395 vnode_get(struct vnode *vp)
4396 {
4397 int retval;
4398
4399 vnode_lock_spin(vp);
4400 retval = vnode_get_locked(vp);
4401 vnode_unlock(vp);
4402
4403 return(retval);
4404 }
4405
4406 int
4407 vnode_get_locked(struct vnode *vp)
4408 {
4409 #if DIAGNOSTIC
4410 lck_mtx_assert(&vp->v_lock, LCK_MTX_ASSERT_OWNED);
4411 #endif
4412 if ((vp->v_iocount == 0) && (vp->v_lflag & (VL_TERMINATE | VL_DEAD))) {
4413 return(ENOENT);
4414 }
4415 vp->v_iocount++;
4416 #ifdef JOE_DEBUG
4417 record_vp(vp, 1);
4418 #endif
4419 return (0);
4420 }
4421
4422 /*
4423 * vnode_getwithvid() cuts in line in front of a vnode drain (that is,
4424 * while the vnode is draining, but at no point after that) to prevent
4425 * deadlocks when getting vnodes from filesystem hashes while holding
4426 * resources that may prevent other iocounts from being released.
4427 */
4428 int
4429 vnode_getwithvid(vnode_t vp, uint32_t vid)
4430 {
4431 return(vget_internal(vp, vid, ( VNODE_NODEAD | VNODE_WITHID | VNODE_DRAINO )));
4432 }
4433
4434 /*
4435 * vnode_getwithvid_drainok() is like vnode_getwithvid(), but *does* block behind a vnode
4436 * drain; it exists for use in the VFS name cache, where we really do want to block behind
4437 * vnode drain to prevent holding off an unmount.
4438 */
4439 int
4440 vnode_getwithvid_drainok(vnode_t vp, uint32_t vid)
4441 {
4442 return(vget_internal(vp, vid, ( VNODE_NODEAD | VNODE_WITHID )));
4443 }
4444
4445 int
4446 vnode_getwithref(vnode_t vp)
4447 {
4448 return(vget_internal(vp, 0, 0));
4449 }
4450
4451
4452 __private_extern__ int
4453 vnode_getalways(vnode_t vp)
4454 {
4455 return(vget_internal(vp, 0, VNODE_ALWAYS));
4456 }
4457
4458 int
4459 vnode_put(vnode_t vp)
4460 {
4461 int retval;
4462
4463 vnode_lock_spin(vp);
4464 retval = vnode_put_locked(vp);
4465 vnode_unlock(vp);
4466
4467 return(retval);
4468 }
4469
4470 static inline void
4471 vn_set_dead(vnode_t vp)
4472 {
4473 vp->v_mount = NULL;
4474 vp->v_op = dead_vnodeop_p;
4475 vp->v_tag = VT_NON;
4476 vp->v_data = NULL;
4477 vp->v_type = VBAD;
4478 vp->v_lflag |= VL_DEAD;
4479 }
4480
4481 int
4482 vnode_put_locked(vnode_t vp)
4483 {
4484 vfs_context_t ctx = vfs_context_current(); /* hoist outside loop */
4485
4486 #if DIAGNOSTIC
4487 lck_mtx_assert(&vp->v_lock, LCK_MTX_ASSERT_OWNED);
4488 #endif
4489 retry:
4490 if (vp->v_iocount < 1)
4491 panic("vnode_put(%p): iocount < 1", vp);
4492
4493 if ((vp->v_usecount > 0) || (vp->v_iocount > 1)) {
4494 vnode_dropiocount(vp);
4495 return(0);
4496 }
4497 if ((vp->v_lflag & (VL_DEAD | VL_NEEDINACTIVE)) == VL_NEEDINACTIVE) {
4498
4499 vp->v_lflag &= ~VL_NEEDINACTIVE;
4500 vnode_unlock(vp);
4501
4502 VNOP_INACTIVE(vp, ctx);
4503
4504 vnode_lock_spin(vp);
4505 /*
4506 * because we had to drop the vnode lock before calling
4507 * VNOP_INACTIVE, the state of this vnode may have changed...
4508 * we may pick up both VL_MARTERM and either
4509 * an iocount or a usecount while in the VNOP_INACTIVE call
4510 * we don't want to call vnode_reclaim_internal on a vnode
4511 * that has active references on it... so loop back around
4512 * and reevaluate the state
4513 */
4514 goto retry;
4515 }
4516 vp->v_lflag &= ~VL_NEEDINACTIVE;
4517
4518 if ((vp->v_lflag & (VL_MARKTERM | VL_TERMINATE | VL_DEAD)) == VL_MARKTERM) {
4519 vnode_lock_convert(vp);
4520 vnode_reclaim_internal(vp, 1, 1, 0);
4521 }
4522 vnode_dropiocount(vp);
4523 vnode_list_add(vp);
4524
4525 return(0);
4526 }
4527
4528 /* is vnode_t in use by others? */
4529 int
4530 vnode_isinuse(vnode_t vp, int refcnt)
4531 {
4532 return(vnode_isinuse_locked(vp, refcnt, 0));
4533 }
4534
4535 int vnode_usecount(vnode_t vp)
4536 {
4537 return vp->v_usecount;
4538 }
4539
4540 int vnode_iocount(vnode_t vp)
4541 {
4542 return vp->v_iocount;
4543 }
4544
4545 static int
4546 vnode_isinuse_locked(vnode_t vp, int refcnt, int locked)
4547 {
4548 int retval = 0;
4549
4550 if (!locked)
4551 vnode_lock_spin(vp);
4552 if ((vp->v_type != VREG) && ((vp->v_usecount - vp->v_kusecount) > refcnt)) {
4553 retval = 1;
4554 goto out;
4555 }
4556 if (vp->v_type == VREG) {
4557 retval = ubc_isinuse_locked(vp, refcnt, 1);
4558 }
4559
4560 out:
4561 if (!locked)
4562 vnode_unlock(vp);
4563 return(retval);
4564 }
4565
4566
4567 /* resume vnode_t */
4568 errno_t
4569 vnode_resume(vnode_t vp)
4570 {
4571 if ((vp->v_lflag & VL_SUSPENDED) && vp->v_owner == current_thread()) {
4572
4573 vnode_lock_spin(vp);
4574 vp->v_lflag &= ~VL_SUSPENDED;
4575 vp->v_owner = NULL;
4576 vnode_unlock(vp);
4577
4578 wakeup(&vp->v_iocount);
4579 }
4580 return(0);
4581 }
4582
4583 /* suspend vnode_t
4584 * Please do not use on more than one vnode at a time as it may
4585 * cause deadlocks.
4586 * xxx should we explicity prevent this from happening?
4587 */
4588
4589 errno_t
4590 vnode_suspend(vnode_t vp)
4591 {
4592 if (vp->v_lflag & VL_SUSPENDED) {
4593 return(EBUSY);
4594 }
4595
4596 vnode_lock_spin(vp);
4597
4598 /*
4599 * xxx is this sufficient to check if a vnode_drain is
4600 * progress?
4601 */
4602
4603 if (vp->v_owner == NULL) {
4604 vp->v_lflag |= VL_SUSPENDED;
4605 vp->v_owner = current_thread();
4606 }
4607 vnode_unlock(vp);
4608
4609 return(0);
4610 }
4611
4612 /*
4613 * Release any blocked locking requests on the vnode.
4614 * Used for forced-unmounts.
4615 *
4616 * XXX What about network filesystems?
4617 */
4618 static void
4619 vnode_abort_advlocks(vnode_t vp)
4620 {
4621 if (vp->v_flag & VLOCKLOCAL)
4622 lf_abort_advlocks(vp);
4623 }
4624
4625
4626 static errno_t
4627 vnode_drain(vnode_t vp)
4628 {
4629
4630 if (vp->v_lflag & VL_DRAIN) {
4631 panic("vnode_drain: recursive drain");
4632 return(ENOENT);
4633 }
4634 vp->v_lflag |= VL_DRAIN;
4635 vp->v_owner = current_thread();
4636
4637 while (vp->v_iocount > 1)
4638 msleep(&vp->v_iocount, &vp->v_lock, PVFS, "vnode_drain", NULL);
4639
4640 vp->v_lflag &= ~VL_DRAIN;
4641
4642 return(0);
4643 }
4644
4645
4646 /*
4647 * if the number of recent references via vnode_getwithvid or vnode_getwithref
4648 * exceeds this threshold, than 'UN-AGE' the vnode by removing it from
4649 * the LRU list if it's currently on it... once the iocount and usecount both drop
4650 * to 0, it will get put back on the end of the list, effectively making it younger
4651 * this allows us to keep actively referenced vnodes in the list without having
4652 * to constantly remove and add to the list each time a vnode w/o a usecount is
4653 * referenced which costs us taking and dropping a global lock twice.
4654 * However, if the vnode is marked DIRTY, we want to pull it out much earlier
4655 */
4656 #define UNAGE_THRESHHOLD 25
4657 #define UNAGE_DIRTYTHRESHHOLD 6
4658
4659 errno_t
4660 vnode_getiocount(vnode_t vp, unsigned int vid, int vflags)
4661 {
4662 int nodead = vflags & VNODE_NODEAD;
4663 int nosusp = vflags & VNODE_NOSUSPEND;
4664 int always = vflags & VNODE_ALWAYS;
4665 int beatdrain = vflags & VNODE_DRAINO;
4666 int withvid = vflags & VNODE_WITHID;
4667
4668 for (;;) {
4669 int sleepflg = 0;
4670
4671 /*
4672 * if it is a dead vnode with deadfs
4673 */
4674 if (nodead && (vp->v_lflag & VL_DEAD) && ((vp->v_type == VBAD) || (vp->v_data == 0))) {
4675 return(ENOENT);
4676 }
4677 /*
4678 * will return VL_DEAD ones
4679 */
4680 if ((vp->v_lflag & (VL_SUSPENDED | VL_DRAIN | VL_TERMINATE)) == 0 ) {
4681 break;
4682 }
4683 /*
4684 * if suspended vnodes are to be failed
4685 */
4686 if (nosusp && (vp->v_lflag & VL_SUSPENDED)) {
4687 return(ENOENT);
4688 }
4689 /*
4690 * if you are the owner of drain/suspend/termination , can acquire iocount
4691 * check for VL_TERMINATE; it does not set owner
4692 */
4693 if ((vp->v_lflag & (VL_DRAIN | VL_SUSPENDED | VL_TERMINATE)) &&
4694 (vp->v_owner == current_thread())) {
4695 break;
4696 }
4697
4698 if (always != 0)
4699 break;
4700
4701 /*
4702 * If this vnode is getting drained, there are some cases where
4703 * we can't block or, in case of tty vnodes, want to be
4704 * interruptible.
4705 */
4706 if (vp->v_lflag & VL_DRAIN) {
4707 /*
4708 * In some situations, we want to get an iocount
4709 * even if the vnode is draining to prevent deadlock,
4710 * e.g. if we're in the filesystem, potentially holding
4711 * resources that could prevent other iocounts from
4712 * being released.
4713 */
4714 if (beatdrain)
4715 break;
4716 /*
4717 * Don't block if the vnode's mount point is unmounting as
4718 * we may be the thread the unmount is itself waiting on
4719 * Only callers who pass in vids (at this point, we've already
4720 * handled nosusp and nodead) are expecting error returns
4721 * from this function, so only we can only return errors for
4722 * those. ENODEV is intended to inform callers that the call
4723 * failed because an unmount is in progress.
4724 */
4725 if (withvid && (vp->v_mount) && vfs_isunmount(vp->v_mount))
4726 return (ENODEV);
4727
4728 if (vnode_istty(vp)) {
4729 sleepflg = PCATCH;
4730 }
4731 }
4732
4733 vnode_lock_convert(vp);
4734
4735 if (vp->v_lflag & VL_TERMINATE) {
4736 int error;
4737
4738 vp->v_lflag |= VL_TERMWANT;
4739
4740 error = msleep(&vp->v_lflag, &vp->v_lock,
4741 (PVFS | sleepflg), "vnode getiocount", NULL);
4742 if (error)
4743 return (error);
4744 } else
4745 msleep(&vp->v_iocount, &vp->v_lock, PVFS, "vnode_getiocount", NULL);
4746 }
4747 if (withvid && vid != vp->v_id) {
4748 return(ENOENT);
4749 }
4750 if (++vp->v_references >= UNAGE_THRESHHOLD ||
4751 (vp->v_flag & VISDIRTY && vp->v_references >= UNAGE_DIRTYTHRESHHOLD)) {
4752 vp->v_references = 0;
4753 vnode_list_remove(vp);
4754 }
4755 vp->v_iocount++;
4756 #ifdef JOE_DEBUG
4757 record_vp(vp, 1);
4758 #endif
4759 return(0);
4760 }
4761
4762 static void
4763 vnode_dropiocount (vnode_t vp)
4764 {
4765 if (vp->v_iocount < 1)
4766 panic("vnode_dropiocount(%p): v_iocount < 1", vp);
4767
4768 vp->v_iocount--;
4769 #ifdef JOE_DEBUG
4770 record_vp(vp, -1);
4771 #endif
4772 if ((vp->v_lflag & (VL_DRAIN | VL_SUSPENDED)) && (vp->v_iocount <= 1))
4773 wakeup(&vp->v_iocount);
4774 }
4775
4776
4777 void
4778 vnode_reclaim(struct vnode * vp)
4779 {
4780 vnode_reclaim_internal(vp, 0, 0, 0);
4781 }
4782
4783 __private_extern__
4784 void
4785 vnode_reclaim_internal(struct vnode * vp, int locked, int reuse, int flags)
4786 {
4787 int isfifo = 0;
4788
4789 if (!locked)
4790 vnode_lock(vp);
4791
4792 if (vp->v_lflag & VL_TERMINATE) {
4793 panic("vnode reclaim in progress");
4794 }
4795 vp->v_lflag |= VL_TERMINATE;
4796
4797 vn_clearunionwait(vp, 1);
4798
4799 vnode_drain(vp);
4800
4801 isfifo = (vp->v_type == VFIFO);
4802
4803 if (vp->v_type != VBAD)
4804 vgone(vp, flags); /* clean and reclaim the vnode */
4805
4806 /*
4807 * give the vnode a new identity so that vnode_getwithvid will fail
4808 * on any stale cache accesses...
4809 * grab the list_lock so that if we're in "new_vnode"
4810 * behind the list_lock trying to steal this vnode, the v_id is stable...
4811 * once new_vnode drops the list_lock, it will block trying to take
4812 * the vnode lock until we release it... at that point it will evaluate
4813 * whether the v_vid has changed
4814 * also need to make sure that the vnode isn't on a list where "new_vnode"
4815 * can find it after the v_id has been bumped until we are completely done
4816 * with the vnode (i.e. putting it back on a list has to be the very last
4817 * thing we do to this vnode... many of the callers of vnode_reclaim_internal
4818 * are holding an io_count on the vnode... they need to drop the io_count
4819 * BEFORE doing a vnode_list_add or make sure to hold the vnode lock until
4820 * they are completely done with the vnode
4821 */
4822 vnode_list_lock();
4823
4824 vnode_list_remove_locked(vp);
4825 vp->v_id++;
4826
4827 vnode_list_unlock();
4828
4829 if (isfifo) {
4830 struct fifoinfo * fip;
4831
4832 fip = vp->v_fifoinfo;
4833 vp->v_fifoinfo = NULL;
4834 FREE(fip, M_TEMP);
4835 }
4836 vp->v_type = VBAD;
4837
4838 if (vp->v_data)
4839 panic("vnode_reclaim_internal: cleaned vnode isn't");
4840 if (vp->v_numoutput)
4841 panic("vnode_reclaim_internal: clean vnode has pending I/O's");
4842 if (UBCINFOEXISTS(vp))
4843 panic("vnode_reclaim_internal: ubcinfo not cleaned");
4844 if (vp->v_parent)
4845 panic("vnode_reclaim_internal: vparent not removed");
4846 if (vp->v_name)
4847 panic("vnode_reclaim_internal: vname not removed");
4848
4849 vp->v_socket = NULL;
4850
4851 vp->v_lflag &= ~VL_TERMINATE;
4852 vp->v_owner = NULL;
4853
4854 KNOTE(&vp->v_knotes, NOTE_REVOKE);
4855
4856 /* Make sure that when we reuse the vnode, no knotes left over */
4857 klist_init(&vp->v_knotes);
4858
4859 if (vp->v_lflag & VL_TERMWANT) {
4860 vp->v_lflag &= ~VL_TERMWANT;
4861 wakeup(&vp->v_lflag);
4862 }
4863 if (!reuse) {
4864 /*
4865 * make sure we get on the
4866 * dead list if appropriate
4867 */
4868 vnode_list_add(vp);
4869 }
4870 if (!locked)
4871 vnode_unlock(vp);
4872 }
4873
4874 static int
4875 vnode_create_internal(uint32_t flavor, uint32_t size, void *data, vnode_t *vpp,
4876 int init_vnode)
4877 {
4878 int error;
4879 int insert = 1;
4880 int existing_vnode;
4881 vnode_t vp;
4882 vnode_t nvp;
4883 vnode_t dvp;
4884 struct uthread *ut;
4885 struct componentname *cnp;
4886 struct vnode_fsparam *param = (struct vnode_fsparam *)data;
4887 #if CONFIG_TRIGGERS
4888 struct vnode_trigger_param *tinfo = NULL;
4889 #endif
4890 if (*vpp) {
4891 vp = *vpp;
4892 *vpp = NULLVP;
4893 existing_vnode = 1;
4894 } else {
4895 existing_vnode = 0;
4896 }
4897
4898 if (init_vnode) {
4899 /* Do quick sanity check on the parameters. */
4900 if ((param == NULL) || (param->vnfs_vtype == VBAD)) {
4901 error = EINVAL;
4902 goto error_out;
4903 }
4904
4905 #if CONFIG_TRIGGERS
4906 if ((flavor == VNCREATE_TRIGGER) && (size == VNCREATE_TRIGGER_SIZE)) {
4907 tinfo = (struct vnode_trigger_param *)data;
4908
4909 /* Validate trigger vnode input */
4910 if ((param->vnfs_vtype != VDIR) ||
4911 (tinfo->vnt_resolve_func == NULL) ||
4912 (tinfo->vnt_flags & ~VNT_VALID_MASK)) {
4913 error = EINVAL;
4914 goto error_out;
4915 }
4916 /* Fall through a normal create (params will be the same) */
4917 flavor = VNCREATE_FLAVOR;
4918 size = VCREATESIZE;
4919 }
4920 #endif
4921 if ((flavor != VNCREATE_FLAVOR) || (size != VCREATESIZE)) {
4922 error = EINVAL;
4923 goto error_out;
4924 }
4925 }
4926
4927 if (!existing_vnode) {
4928 if ((error = new_vnode(&vp)) ) {
4929 return (error);
4930 }
4931 if (!init_vnode) {
4932 /* Make it so that it can be released by a vnode_put) */
4933 vn_set_dead(vp);
4934 *vpp = vp;
4935 return (0);
4936 }
4937 } else {
4938 /*
4939 * A vnode obtained by vnode_create_empty has been passed to
4940 * vnode_initialize - Unset VL_DEAD set by vn_set_dead. After
4941 * this point, it is set back on any error.
4942 *
4943 * N.B. vnode locking - We make the same assumptions as the
4944 * "unsplit" vnode_create did - i.e. it is safe to update the
4945 * vnode's fields without the vnode lock. This vnode has been
4946 * out and about with the filesystem and hopefully nothing
4947 * was done to the vnode between the vnode_create_empty and
4948 * now when it has come in through vnode_initialize.
4949 */
4950 vp->v_lflag &= ~VL_DEAD;
4951 }
4952
4953 dvp = param->vnfs_dvp;
4954 cnp = param->vnfs_cnp;
4955
4956 vp->v_op = param->vnfs_vops;
4957 vp->v_type = param->vnfs_vtype;
4958 vp->v_data = param->vnfs_fsnode;
4959
4960 if (param->vnfs_markroot)
4961 vp->v_flag |= VROOT;
4962 if (param->vnfs_marksystem)
4963 vp->v_flag |= VSYSTEM;
4964 if (vp->v_type == VREG) {
4965 error = ubc_info_init_withsize(vp, param->vnfs_filesize);
4966 if (error) {
4967 #ifdef JOE_DEBUG
4968 record_vp(vp, 1);
4969 #endif
4970 vn_set_dead(vp);
4971
4972 vnode_put(vp);
4973 return(error);
4974 }
4975 if (param->vnfs_mp->mnt_ioflags & MNT_IOFLAGS_IOSCHED_SUPPORTED)
4976 memory_object_mark_io_tracking(vp->v_ubcinfo->ui_control);
4977 }
4978 #ifdef JOE_DEBUG
4979 record_vp(vp, 1);
4980 #endif
4981
4982 #if CONFIG_TRIGGERS
4983 /*
4984 * For trigger vnodes, attach trigger info to vnode
4985 */
4986 if ((vp->v_type == VDIR) && (tinfo != NULL)) {
4987 /*
4988 * Note: has a side effect of incrementing trigger count on the
4989 * mount if successful, which we would need to undo on a
4990 * subsequent failure.
4991 */
4992 #ifdef JOE_DEBUG
4993 record_vp(vp, -1);
4994 #endif
4995 error = vnode_resolver_create(param->vnfs_mp, vp, tinfo, FALSE);
4996 if (error) {
4997 printf("vnode_create: vnode_resolver_create() err %d\n", error);
4998 vn_set_dead(vp);
4999 #ifdef JOE_DEBUG
5000 record_vp(vp, 1);
5001 #endif
5002 vnode_put(vp);
5003 return (error);
5004 }
5005 }
5006 #endif
5007 if (vp->v_type == VCHR || vp->v_type == VBLK) {
5008
5009 vp->v_tag = VT_DEVFS; /* callers will reset if needed (bdevvp) */
5010
5011 if ( (nvp = checkalias(vp, param->vnfs_rdev)) ) {
5012 /*
5013 * if checkalias returns a vnode, it will be locked
5014 *
5015 * first get rid of the unneeded vnode we acquired
5016 */
5017 vp->v_data = NULL;
5018 vp->v_op = spec_vnodeop_p;
5019 vp->v_type = VBAD;
5020 vp->v_lflag = VL_DEAD;
5021 vp->v_data = NULL;
5022 vp->v_tag = VT_NON;
5023 vnode_put(vp);
5024
5025 /*
5026 * switch to aliased vnode and finish
5027 * preparing it
5028 */
5029 vp = nvp;
5030
5031 vclean(vp, 0);
5032 vp->v_op = param->vnfs_vops;
5033 vp->v_type = param->vnfs_vtype;
5034 vp->v_data = param->vnfs_fsnode;
5035 vp->v_lflag = 0;
5036 vp->v_mount = NULL;
5037 insmntque(vp, param->vnfs_mp);
5038 insert = 0;
5039 vnode_unlock(vp);
5040 }
5041
5042 if (VCHR == vp->v_type) {
5043 u_int maj = major(vp->v_rdev);
5044
5045 if (maj < (u_int)nchrdev && cdevsw[maj].d_type == D_TTY)
5046 vp->v_flag |= VISTTY;
5047 }
5048 }
5049
5050 if (vp->v_type == VFIFO) {
5051 struct fifoinfo *fip;
5052
5053 MALLOC(fip, struct fifoinfo *,
5054 sizeof(*fip), M_TEMP, M_WAITOK);
5055 bzero(fip, sizeof(struct fifoinfo ));
5056 vp->v_fifoinfo = fip;
5057 }
5058 /* The file systems must pass the address of the location where
5059 * they store the vnode pointer. When we add the vnode into the mount
5060 * list and name cache they become discoverable. So the file system node
5061 * must have the connection to vnode setup by then
5062 */
5063 *vpp = vp;
5064
5065 /* Add fs named reference. */
5066 if (param->vnfs_flags & VNFS_ADDFSREF) {
5067 vp->v_lflag |= VNAMED_FSHASH;
5068 }
5069 if (param->vnfs_mp) {
5070 if (param->vnfs_mp->mnt_kern_flag & MNTK_LOCK_LOCAL)
5071 vp->v_flag |= VLOCKLOCAL;
5072 if (insert) {
5073 if ((vp->v_freelist.tqe_prev != (struct vnode **)0xdeadb))
5074 panic("insmntque: vp on the free list\n");
5075
5076 /*
5077 * enter in mount vnode list
5078 */
5079 insmntque(vp, param->vnfs_mp);
5080 }
5081 }
5082 if (dvp && vnode_ref(dvp) == 0) {
5083 vp->v_parent = dvp;
5084 }
5085 if (cnp) {
5086 if (dvp && ((param->vnfs_flags & (VNFS_NOCACHE | VNFS_CANTCACHE)) == 0)) {
5087 /*
5088 * enter into name cache
5089 * we've got the info to enter it into the name cache now
5090 * cache_enter_create will pick up an extra reference on
5091 * the name entered into the string cache
5092 */
5093 vp->v_name = cache_enter_create(dvp, vp, cnp);
5094 } else
5095 vp->v_name = vfs_addname(cnp->cn_nameptr, cnp->cn_namelen, cnp->cn_hash, 0);
5096
5097 if ((cnp->cn_flags & UNIONCREATED) == UNIONCREATED)
5098 vp->v_flag |= VISUNION;
5099 }
5100 if ((param->vnfs_flags & VNFS_CANTCACHE) == 0) {
5101 /*
5102 * this vnode is being created as cacheable in the name cache
5103 * this allows us to re-enter it in the cache
5104 */
5105 vp->v_flag |= VNCACHEABLE;
5106 }
5107 ut = get_bsdthread_info(current_thread());
5108
5109 if ((current_proc()->p_lflag & P_LRAGE_VNODES) ||
5110 (ut->uu_flag & UT_RAGE_VNODES)) {
5111 /*
5112 * process has indicated that it wants any
5113 * vnodes created on its behalf to be rapidly
5114 * aged to reduce the impact on the cached set
5115 * of vnodes
5116 */
5117 vp->v_flag |= VRAGE;
5118 }
5119
5120 #if CONFIG_SECLUDED_MEMORY
5121 switch (secluded_for_filecache) {
5122 case 0:
5123 /*
5124 * secluded_for_filecache == 0:
5125 * + no file contents in secluded pool
5126 */
5127 break;
5128 case 1:
5129 /*
5130 * secluded_for_filecache == 1:
5131 * + no files from /
5132 * + files from /Applications/ are OK
5133 * + files from /Applications/Camera are not OK
5134 * + no files that are open for write
5135 */
5136 if (vnode_vtype(vp) == VREG &&
5137 vnode_mount(vp) != NULL &&
5138 (! (vfs_flags(vnode_mount(vp)) & MNT_ROOTFS))) {
5139 /* not from root filesystem: eligible for secluded pages */
5140 memory_object_mark_eligible_for_secluded(
5141 ubc_getobject(vp, UBC_FLAGS_NONE),
5142 TRUE);
5143 }
5144 break;
5145 case 2:
5146 /*
5147 * secluded_for_filecache == 2:
5148 * + all read-only files OK, except:
5149 * + dyld_shared_cache_arm64*
5150 * + Camera
5151 * + mediaserverd
5152 */
5153 if (vnode_vtype(vp) == VREG) {
5154 memory_object_mark_eligible_for_secluded(
5155 ubc_getobject(vp, UBC_FLAGS_NONE),
5156 TRUE);
5157 }
5158 break;
5159 default:
5160 break;
5161 }
5162 #endif /* CONFIG_SECLUDED_MEMORY */
5163
5164 return (0);
5165
5166 error_out:
5167 if (existing_vnode) {
5168 vnode_put(vp);
5169 }
5170 return (error);
5171 }
5172
5173 /* USAGE:
5174 * The following api creates a vnode and associates all the parameter specified in vnode_fsparam
5175 * structure and returns a vnode handle with a reference. device aliasing is handled here so checkalias
5176 * is obsoleted by this.
5177 */
5178 int
5179 vnode_create(uint32_t flavor, uint32_t size, void *data, vnode_t *vpp)
5180 {
5181 *vpp = NULLVP;
5182 return (vnode_create_internal(flavor, size, data, vpp, 1));
5183 }
5184
5185 int
5186 vnode_create_empty(vnode_t *vpp)
5187 {
5188 *vpp = NULLVP;
5189 return (vnode_create_internal(VNCREATE_FLAVOR, VCREATESIZE, NULL,
5190 vpp, 0));
5191 }
5192
5193 int
5194 vnode_initialize(uint32_t flavor, uint32_t size, void *data, vnode_t *vpp)
5195 {
5196 if (*vpp == NULLVP) {
5197 panic("NULL vnode passed to vnode_initialize");
5198 }
5199 #if DEVELOPMENT || DEBUG
5200 /*
5201 * We lock to check that vnode is fit for unlocked use in
5202 * vnode_create_internal.
5203 */
5204 vnode_lock_spin(*vpp);
5205 VNASSERT(((*vpp)->v_iocount == 1), *vpp,
5206 ("vnode_initialize : iocount not 1, is %d", (*vpp)->v_iocount));
5207 VNASSERT(((*vpp)->v_usecount == 0), *vpp,
5208 ("vnode_initialize : usecount not 0, is %d", (*vpp)->v_usecount));
5209 VNASSERT(((*vpp)->v_lflag & VL_DEAD), *vpp,
5210 ("vnode_initialize : v_lflag does not have VL_DEAD, is 0x%x",
5211 (*vpp)->v_lflag));
5212 VNASSERT(((*vpp)->v_data == NULL), *vpp,
5213 ("vnode_initialize : v_data not NULL"));
5214 vnode_unlock(*vpp);
5215 #endif
5216 return (vnode_create_internal(flavor, size, data, vpp, 1));
5217 }
5218
5219 int
5220 vnode_addfsref(vnode_t vp)
5221 {
5222 vnode_lock_spin(vp);
5223 if (vp->v_lflag & VNAMED_FSHASH)
5224 panic("add_fsref: vp already has named reference");
5225 if ((vp->v_freelist.tqe_prev != (struct vnode **)0xdeadb))
5226 panic("addfsref: vp on the free list\n");
5227 vp->v_lflag |= VNAMED_FSHASH;
5228 vnode_unlock(vp);
5229 return(0);
5230
5231 }
5232 int
5233 vnode_removefsref(vnode_t vp)
5234 {
5235 vnode_lock_spin(vp);
5236 if ((vp->v_lflag & VNAMED_FSHASH) == 0)
5237 panic("remove_fsref: no named reference");
5238 vp->v_lflag &= ~VNAMED_FSHASH;
5239 vnode_unlock(vp);
5240 return(0);
5241
5242 }
5243
5244
5245 int
5246 vfs_iterate(int flags, int (*callout)(mount_t, void *), void *arg)
5247 {
5248 mount_t mp;
5249 int ret = 0;
5250 fsid_t * fsid_list;
5251 int count, actualcount, i;
5252 void * allocmem;
5253 int indx_start, indx_stop, indx_incr;
5254 int cb_dropref = (flags & VFS_ITERATE_CB_DROPREF);
5255
5256 count = mount_getvfscnt();
5257 count += 10;
5258
5259 fsid_list = (fsid_t *)kalloc(count * sizeof(fsid_t));
5260 allocmem = (void *)fsid_list;
5261
5262 actualcount = mount_fillfsids(fsid_list, count);
5263
5264 /*
5265 * Establish the iteration direction
5266 * VFS_ITERATE_TAIL_FIRST overrides default head first order (oldest first)
5267 */
5268 if (flags & VFS_ITERATE_TAIL_FIRST) {
5269 indx_start = actualcount - 1;
5270 indx_stop = -1;
5271 indx_incr = -1;
5272 } else /* Head first by default */ {
5273 indx_start = 0;
5274 indx_stop = actualcount;
5275 indx_incr = 1;
5276 }
5277
5278 for (i=indx_start; i != indx_stop; i += indx_incr) {
5279
5280 /* obtain the mount point with iteration reference */
5281 mp = mount_list_lookupby_fsid(&fsid_list[i], 0, 1);
5282
5283 if(mp == (struct mount *)0)
5284 continue;
5285 mount_lock(mp);
5286 if (mp->mnt_lflag & (MNT_LDEAD | MNT_LUNMOUNT)) {
5287 mount_unlock(mp);
5288 mount_iterdrop(mp);
5289 continue;
5290
5291 }
5292 mount_unlock(mp);
5293
5294 /* iterate over all the vnodes */
5295 ret = callout(mp, arg);
5296
5297 /*
5298 * Drop the iterref here if the callback didn't do it.
5299 * Note: If cb_dropref is set the mp may no longer exist.
5300 */
5301 if (!cb_dropref)
5302 mount_iterdrop(mp);
5303
5304 switch (ret) {
5305 case VFS_RETURNED:
5306 case VFS_RETURNED_DONE:
5307 if (ret == VFS_RETURNED_DONE) {
5308 ret = 0;
5309 goto out;
5310 }
5311 break;
5312
5313 case VFS_CLAIMED_DONE:
5314 ret = 0;
5315 goto out;
5316 case VFS_CLAIMED:
5317 default:
5318 break;
5319 }
5320 ret = 0;
5321 }
5322
5323 out:
5324 kfree(allocmem, (count * sizeof(fsid_t)));
5325 return (ret);
5326 }
5327
5328 /*
5329 * Update the vfsstatfs structure in the mountpoint.
5330 * MAC: Parameter eventtype added, indicating whether the event that
5331 * triggered this update came from user space, via a system call
5332 * (VFS_USER_EVENT) or an internal kernel call (VFS_KERNEL_EVENT).
5333 */
5334 int
5335 vfs_update_vfsstat(mount_t mp, vfs_context_t ctx, __unused int eventtype)
5336 {
5337 struct vfs_attr va;
5338 int error;
5339
5340 /*
5341 * Request the attributes we want to propagate into
5342 * the per-mount vfsstat structure.
5343 */
5344 VFSATTR_INIT(&va);
5345 VFSATTR_WANTED(&va, f_iosize);
5346 VFSATTR_WANTED(&va, f_blocks);
5347 VFSATTR_WANTED(&va, f_bfree);
5348 VFSATTR_WANTED(&va, f_bavail);
5349 VFSATTR_WANTED(&va, f_bused);
5350 VFSATTR_WANTED(&va, f_files);
5351 VFSATTR_WANTED(&va, f_ffree);
5352 VFSATTR_WANTED(&va, f_bsize);
5353 VFSATTR_WANTED(&va, f_fssubtype);
5354
5355 if ((error = vfs_getattr(mp, &va, ctx)) != 0) {
5356 KAUTH_DEBUG("STAT - filesystem returned error %d", error);
5357 return(error);
5358 }
5359 #if CONFIG_MACF
5360 if (eventtype == VFS_USER_EVENT) {
5361 error = mac_mount_check_getattr(ctx, mp, &va);
5362 if (error != 0)
5363 return (error);
5364 }
5365 #endif
5366 /*
5367 * Unpack into the per-mount structure.
5368 *
5369 * We only overwrite these fields, which are likely to change:
5370 * f_blocks
5371 * f_bfree
5372 * f_bavail
5373 * f_bused
5374 * f_files
5375 * f_ffree
5376 *
5377 * And these which are not, but which the FS has no other way
5378 * of providing to us:
5379 * f_bsize
5380 * f_iosize
5381 * f_fssubtype
5382 *
5383 */
5384 if (VFSATTR_IS_SUPPORTED(&va, f_bsize)) {
5385 /* 4822056 - protect against malformed server mount */
5386 mp->mnt_vfsstat.f_bsize = (va.f_bsize > 0 ? va.f_bsize : 512);
5387 } else {
5388 mp->mnt_vfsstat.f_bsize = mp->mnt_devblocksize; /* default from the device block size */
5389 }
5390 if (VFSATTR_IS_SUPPORTED(&va, f_iosize)) {
5391 mp->mnt_vfsstat.f_iosize = va.f_iosize;
5392 } else {
5393 mp->mnt_vfsstat.f_iosize = 1024 * 1024; /* 1MB sensible I/O size */
5394 }
5395 if (VFSATTR_IS_SUPPORTED(&va, f_blocks))
5396 mp->mnt_vfsstat.f_blocks = va.f_blocks;
5397 if (VFSATTR_IS_SUPPORTED(&va, f_bfree))
5398 mp->mnt_vfsstat.f_bfree = va.f_bfree;
5399 if (VFSATTR_IS_SUPPORTED(&va, f_bavail))
5400 mp->mnt_vfsstat.f_bavail = va.f_bavail;
5401 if (VFSATTR_IS_SUPPORTED(&va, f_bused))
5402 mp->mnt_vfsstat.f_bused = va.f_bused;
5403 if (VFSATTR_IS_SUPPORTED(&va, f_files))
5404 mp->mnt_vfsstat.f_files = va.f_files;
5405 if (VFSATTR_IS_SUPPORTED(&va, f_ffree))
5406 mp->mnt_vfsstat.f_ffree = va.f_ffree;
5407
5408 /* this is unlikely to change, but has to be queried for */
5409 if (VFSATTR_IS_SUPPORTED(&va, f_fssubtype))
5410 mp->mnt_vfsstat.f_fssubtype = va.f_fssubtype;
5411
5412 return(0);
5413 }
5414
5415 int
5416 mount_list_add(mount_t mp)
5417 {
5418 int res;
5419
5420 mount_list_lock();
5421 if (system_inshutdown != 0) {
5422 res = -1;
5423 } else {
5424 TAILQ_INSERT_TAIL(&mountlist, mp, mnt_list);
5425 nummounts++;
5426 res = 0;
5427 }
5428 mount_list_unlock();
5429
5430 return res;
5431 }
5432
5433 void
5434 mount_list_remove(mount_t mp)
5435 {
5436 mount_list_lock();
5437 TAILQ_REMOVE(&mountlist, mp, mnt_list);
5438 nummounts--;
5439 mp->mnt_list.tqe_next = NULL;
5440 mp->mnt_list.tqe_prev = NULL;
5441 mount_list_unlock();
5442 }
5443
5444 mount_t
5445 mount_lookupby_volfsid(int volfs_id, int withref)
5446 {
5447 mount_t cur_mount = (mount_t)0;
5448 mount_t mp;
5449
5450 mount_list_lock();
5451 TAILQ_FOREACH(mp, &mountlist, mnt_list) {
5452 if (!(mp->mnt_kern_flag & MNTK_UNMOUNT) &&
5453 (mp->mnt_kern_flag & MNTK_PATH_FROM_ID) &&
5454 (mp->mnt_vfsstat.f_fsid.val[0] == volfs_id)) {
5455 cur_mount = mp;
5456 if (withref) {
5457 if (mount_iterref(cur_mount, 1)) {
5458 cur_mount = (mount_t)0;
5459 mount_list_unlock();
5460 goto out;
5461 }
5462 }
5463 break;
5464 }
5465 }
5466 mount_list_unlock();
5467 if (withref && (cur_mount != (mount_t)0)) {
5468 mp = cur_mount;
5469 if (vfs_busy(mp, LK_NOWAIT) != 0) {
5470 cur_mount = (mount_t)0;
5471 }
5472 mount_iterdrop(mp);
5473 }
5474 out:
5475 return(cur_mount);
5476 }
5477
5478 mount_t
5479 mount_list_lookupby_fsid(fsid_t *fsid, int locked, int withref)
5480 {
5481 mount_t retmp = (mount_t)0;
5482 mount_t mp;
5483
5484 if (!locked)
5485 mount_list_lock();
5486 TAILQ_FOREACH(mp, &mountlist, mnt_list)
5487 if (mp->mnt_vfsstat.f_fsid.val[0] == fsid->val[0] &&
5488 mp->mnt_vfsstat.f_fsid.val[1] == fsid->val[1]) {
5489 retmp = mp;
5490 if (withref) {
5491 if (mount_iterref(retmp, 1))
5492 retmp = (mount_t)0;
5493 }
5494 goto out;
5495 }
5496 out:
5497 if (!locked)
5498 mount_list_unlock();
5499 return (retmp);
5500 }
5501
5502 errno_t
5503 vnode_lookup(const char *path, int flags, vnode_t *vpp, vfs_context_t ctx)
5504 {
5505 struct nameidata nd;
5506 int error;
5507 u_int32_t ndflags = 0;
5508
5509 if (ctx == NULL) {
5510 return EINVAL;
5511 }
5512
5513 if (flags & VNODE_LOOKUP_NOFOLLOW)
5514 ndflags = NOFOLLOW;
5515 else
5516 ndflags = FOLLOW;
5517
5518 if (flags & VNODE_LOOKUP_NOCROSSMOUNT)
5519 ndflags |= NOCROSSMOUNT;
5520
5521 if (flags & VNODE_LOOKUP_CROSSMOUNTNOWAIT)
5522 ndflags |= CN_NBMOUNTLOOK;
5523
5524 /* XXX AUDITVNPATH1 needed ? */
5525 NDINIT(&nd, LOOKUP, OP_LOOKUP, ndflags, UIO_SYSSPACE,
5526 CAST_USER_ADDR_T(path), ctx);
5527
5528 if ((error = namei(&nd)))
5529 return (error);
5530 *vpp = nd.ni_vp;
5531 nameidone(&nd);
5532
5533 return (0);
5534 }
5535
5536 errno_t
5537 vnode_open(const char *path, int fmode, int cmode, int flags, vnode_t *vpp, vfs_context_t ctx)
5538 {
5539 struct nameidata nd;
5540 int error;
5541 u_int32_t ndflags = 0;
5542 int lflags = flags;
5543
5544 if (ctx == NULL) { /* XXX technically an error */
5545 ctx = vfs_context_current();
5546 }
5547
5548 if (fmode & O_NOFOLLOW)
5549 lflags |= VNODE_LOOKUP_NOFOLLOW;
5550
5551 if (lflags & VNODE_LOOKUP_NOFOLLOW)
5552 ndflags = NOFOLLOW;
5553 else
5554 ndflags = FOLLOW;
5555
5556 if (lflags & VNODE_LOOKUP_NOCROSSMOUNT)
5557 ndflags |= NOCROSSMOUNT;
5558
5559 if (lflags & VNODE_LOOKUP_CROSSMOUNTNOWAIT)
5560 ndflags |= CN_NBMOUNTLOOK;
5561
5562 /* XXX AUDITVNPATH1 needed ? */
5563 NDINIT(&nd, LOOKUP, OP_OPEN, ndflags, UIO_SYSSPACE,
5564 CAST_USER_ADDR_T(path), ctx);
5565
5566 if ((error = vn_open(&nd, fmode, cmode)))
5567 *vpp = NULL;
5568 else
5569 *vpp = nd.ni_vp;
5570
5571 return (error);
5572 }
5573
5574 errno_t
5575 vnode_close(vnode_t vp, int flags, vfs_context_t ctx)
5576 {
5577 int error;
5578
5579 if (ctx == NULL) {
5580 ctx = vfs_context_current();
5581 }
5582
5583 error = vn_close(vp, flags, ctx);
5584 vnode_put(vp);
5585 return (error);
5586 }
5587
5588 errno_t
5589 vnode_mtime(vnode_t vp, struct timespec *mtime, vfs_context_t ctx)
5590 {
5591 struct vnode_attr va;
5592 int error;
5593
5594 VATTR_INIT(&va);
5595 VATTR_WANTED(&va, va_modify_time);
5596 error = vnode_getattr(vp, &va, ctx);
5597 if (!error)
5598 *mtime = va.va_modify_time;
5599 return error;
5600 }
5601
5602 errno_t
5603 vnode_flags(vnode_t vp, uint32_t *flags, vfs_context_t ctx)
5604 {
5605 struct vnode_attr va;
5606 int error;
5607
5608 VATTR_INIT(&va);
5609 VATTR_WANTED(&va, va_flags);
5610 error = vnode_getattr(vp, &va, ctx);
5611 if (!error)
5612 *flags = va.va_flags;
5613 return error;
5614 }
5615
5616 /*
5617 * Returns: 0 Success
5618 * vnode_getattr:???
5619 */
5620 errno_t
5621 vnode_size(vnode_t vp, off_t *sizep, vfs_context_t ctx)
5622 {
5623 struct vnode_attr va;
5624 int error;
5625
5626 VATTR_INIT(&va);
5627 VATTR_WANTED(&va, va_data_size);
5628 error = vnode_getattr(vp, &va, ctx);
5629 if (!error)
5630 *sizep = va.va_data_size;
5631 return(error);
5632 }
5633
5634 errno_t
5635 vnode_setsize(vnode_t vp, off_t size, int ioflag, vfs_context_t ctx)
5636 {
5637 struct vnode_attr va;
5638
5639 VATTR_INIT(&va);
5640 VATTR_SET(&va, va_data_size, size);
5641 va.va_vaflags = ioflag & 0xffff;
5642 return(vnode_setattr(vp, &va, ctx));
5643 }
5644
5645 int
5646 vnode_setdirty(vnode_t vp)
5647 {
5648 vnode_lock_spin(vp);
5649 vp->v_flag |= VISDIRTY;
5650 vnode_unlock(vp);
5651 return 0;
5652 }
5653
5654 int
5655 vnode_cleardirty(vnode_t vp)
5656 {
5657 vnode_lock_spin(vp);
5658 vp->v_flag &= ~VISDIRTY;
5659 vnode_unlock(vp);
5660 return 0;
5661 }
5662
5663 int
5664 vnode_isdirty(vnode_t vp)
5665 {
5666 int dirty;
5667
5668 vnode_lock_spin(vp);
5669 dirty = (vp->v_flag & VISDIRTY) ? 1 : 0;
5670 vnode_unlock(vp);
5671
5672 return dirty;
5673 }
5674
5675 static int
5676 vn_create_reg(vnode_t dvp, vnode_t *vpp, struct nameidata *ndp, struct vnode_attr *vap, uint32_t flags, int fmode, uint32_t *statusp, vfs_context_t ctx)
5677 {
5678 /* Only use compound VNOP for compound operation */
5679 if (vnode_compound_open_available(dvp) && ((flags & VN_CREATE_DOOPEN) != 0)) {
5680 *vpp = NULLVP;
5681 return VNOP_COMPOUND_OPEN(dvp, vpp, ndp, O_CREAT, fmode, statusp, vap, ctx);
5682 } else {
5683 return VNOP_CREATE(dvp, vpp, &ndp->ni_cnd, vap, ctx);
5684 }
5685 }
5686
5687 /*
5688 * Create a filesystem object of arbitrary type with arbitrary attributes in
5689 * the spevied directory with the specified name.
5690 *
5691 * Parameters: dvp Pointer to the vnode of the directory
5692 * in which to create the object.
5693 * vpp Pointer to the area into which to
5694 * return the vnode of the created object.
5695 * cnp Component name pointer from the namei
5696 * data structure, containing the name to
5697 * use for the create object.
5698 * vap Pointer to the vnode_attr structure
5699 * describing the object to be created,
5700 * including the type of object.
5701 * flags VN_* flags controlling ACL inheritance
5702 * and whether or not authorization is to
5703 * be required for the operation.
5704 *
5705 * Returns: 0 Success
5706 * !0 errno value
5707 *
5708 * Implicit: *vpp Contains the vnode of the object that
5709 * was created, if successful.
5710 * *cnp May be modified by the underlying VFS.
5711 * *vap May be modified by the underlying VFS.
5712 * modified by either ACL inheritance or
5713 *
5714 *
5715 * be modified, even if the operation is
5716 *
5717 *
5718 * Notes: The kauth_filesec_t in 'vap', if any, is in host byte order.
5719 *
5720 * Modification of '*cnp' and '*vap' by the underlying VFS is
5721 * strongly discouraged.
5722 *
5723 * XXX: This function is a 'vn_*' function; it belongs in vfs_vnops.c
5724 *
5725 * XXX: We should enummerate the possible errno values here, and where
5726 * in the code they originated.
5727 */
5728 errno_t
5729 vn_create(vnode_t dvp, vnode_t *vpp, struct nameidata *ndp, struct vnode_attr *vap, uint32_t flags, int fmode, uint32_t *statusp, vfs_context_t ctx)
5730 {
5731 errno_t error, old_error;
5732 vnode_t vp = (vnode_t)0;
5733 boolean_t batched;
5734 struct componentname *cnp;
5735 uint32_t defaulted;
5736
5737 cnp = &ndp->ni_cnd;
5738 error = 0;
5739 batched = namei_compound_available(dvp, ndp) ? TRUE : FALSE;
5740
5741 KAUTH_DEBUG("%p CREATE - '%s'", dvp, cnp->cn_nameptr);
5742
5743 if (flags & VN_CREATE_NOINHERIT)
5744 vap->va_vaflags |= VA_NOINHERIT;
5745 if (flags & VN_CREATE_NOAUTH)
5746 vap->va_vaflags |= VA_NOAUTH;
5747 /*
5748 * Handle ACL inheritance, initialize vap.
5749 */
5750 error = vn_attribute_prepare(dvp, vap, &defaulted, ctx);
5751 if (error) {
5752 return error;
5753 }
5754
5755 if (vap->va_type != VREG && (fmode != 0 || (flags & VN_CREATE_DOOPEN) || statusp)) {
5756 panic("Open parameters, but not a regular file.");
5757 }
5758 if ((fmode != 0) && ((flags & VN_CREATE_DOOPEN) == 0)) {
5759 panic("Mode for open, but not trying to open...");
5760 }
5761
5762
5763 /*
5764 * Create the requested node.
5765 */
5766 switch(vap->va_type) {
5767 case VREG:
5768 error = vn_create_reg(dvp, vpp, ndp, vap, flags, fmode, statusp, ctx);
5769 break;
5770 case VDIR:
5771 error = vn_mkdir(dvp, vpp, ndp, vap, ctx);
5772 break;
5773 case VSOCK:
5774 case VFIFO:
5775 case VBLK:
5776 case VCHR:
5777 error = VNOP_MKNOD(dvp, vpp, cnp, vap, ctx);
5778 break;
5779 default:
5780 panic("vnode_create: unknown vtype %d", vap->va_type);
5781 }
5782 if (error != 0) {
5783 KAUTH_DEBUG("%p CREATE - error %d returned by filesystem", dvp, error);
5784 goto out;
5785 }
5786
5787 vp = *vpp;
5788 old_error = error;
5789
5790 #if CONFIG_MACF
5791 if (!(flags & VN_CREATE_NOLABEL)) {
5792 error = vnode_label(vnode_mount(vp), dvp, vp, cnp, VNODE_LABEL_CREATE, ctx);
5793 if (error)
5794 goto error;
5795 }
5796 #endif
5797
5798 /*
5799 * If some of the requested attributes weren't handled by the VNOP,
5800 * use our fallback code.
5801 */
5802 if (!VATTR_ALL_SUPPORTED(vap) && *vpp) {
5803 KAUTH_DEBUG(" CREATE - doing fallback with ACL %p", vap->va_acl);
5804 error = vnode_setattr_fallback(*vpp, vap, ctx);
5805 }
5806 #if CONFIG_MACF
5807 error:
5808 #endif
5809 if ((error != 0) && (vp != (vnode_t)0)) {
5810
5811 /* If we've done a compound open, close */
5812 if (batched && (old_error == 0) && (vap->va_type == VREG)) {
5813 VNOP_CLOSE(vp, fmode, ctx);
5814 }
5815
5816 /* Need to provide notifications if a create succeeded */
5817 if (!batched) {
5818 *vpp = (vnode_t) 0;
5819 vnode_put(vp);
5820 }
5821 }
5822
5823 out:
5824 vn_attribute_cleanup(vap, defaulted);
5825
5826 return(error);
5827 }
5828
5829 static kauth_scope_t vnode_scope;
5830 static int vnode_authorize_callback(kauth_cred_t credential, void *idata, kauth_action_t action,
5831 uintptr_t arg0, uintptr_t arg1, uintptr_t arg2, uintptr_t arg3);
5832 static int vnode_authorize_callback_int(kauth_action_t action, vfs_context_t ctx,
5833 vnode_t vp, vnode_t dvp, int *errorp);
5834
5835 typedef struct _vnode_authorize_context {
5836 vnode_t vp;
5837 struct vnode_attr *vap;
5838 vnode_t dvp;
5839 struct vnode_attr *dvap;
5840 vfs_context_t ctx;
5841 int flags;
5842 int flags_valid;
5843 #define _VAC_IS_OWNER (1<<0)
5844 #define _VAC_IN_GROUP (1<<1)
5845 #define _VAC_IS_DIR_OWNER (1<<2)
5846 #define _VAC_IN_DIR_GROUP (1<<3)
5847 #define _VAC_NO_VNODE_POINTERS (1<<4)
5848 } *vauth_ctx;
5849
5850 void
5851 vnode_authorize_init(void)
5852 {
5853 vnode_scope = kauth_register_scope(KAUTH_SCOPE_VNODE, vnode_authorize_callback, NULL);
5854 }
5855
5856 #define VATTR_PREPARE_DEFAULTED_UID 0x1
5857 #define VATTR_PREPARE_DEFAULTED_GID 0x2
5858 #define VATTR_PREPARE_DEFAULTED_MODE 0x4
5859
5860 int
5861 vn_attribute_prepare(vnode_t dvp, struct vnode_attr *vap, uint32_t *defaulted_fieldsp, vfs_context_t ctx)
5862 {
5863 kauth_acl_t nacl = NULL, oacl = NULL;
5864 int error;
5865
5866 /*
5867 * Handle ACL inheritance.
5868 */
5869 if (!(vap->va_vaflags & VA_NOINHERIT) && vfs_extendedsecurity(dvp->v_mount)) {
5870 /* save the original filesec */
5871 if (VATTR_IS_ACTIVE(vap, va_acl)) {
5872 oacl = vap->va_acl;
5873 }
5874
5875 vap->va_acl = NULL;
5876 if ((error = kauth_acl_inherit(dvp,
5877 oacl,
5878 &nacl,
5879 vap->va_type == VDIR,
5880 ctx)) != 0) {
5881 KAUTH_DEBUG("%p CREATE - error %d processing inheritance", dvp, error);
5882 return(error);
5883 }
5884
5885 /*
5886 * If the generated ACL is NULL, then we can save ourselves some effort
5887 * by clearing the active bit.
5888 */
5889 if (nacl == NULL) {
5890 VATTR_CLEAR_ACTIVE(vap, va_acl);
5891 } else {
5892 vap->va_base_acl = oacl;
5893 VATTR_SET(vap, va_acl, nacl);
5894 }
5895 }
5896
5897 error = vnode_authattr_new_internal(dvp, vap, (vap->va_vaflags & VA_NOAUTH), defaulted_fieldsp, ctx);
5898 if (error) {
5899 vn_attribute_cleanup(vap, *defaulted_fieldsp);
5900 }
5901
5902 return error;
5903 }
5904
5905 void
5906 vn_attribute_cleanup(struct vnode_attr *vap, uint32_t defaulted_fields)
5907 {
5908 /*
5909 * If the caller supplied a filesec in vap, it has been replaced
5910 * now by the post-inheritance copy. We need to put the original back
5911 * and free the inherited product.
5912 */
5913 kauth_acl_t nacl, oacl;
5914
5915 if (VATTR_IS_ACTIVE(vap, va_acl)) {
5916 nacl = vap->va_acl;
5917 oacl = vap->va_base_acl;
5918
5919 if (oacl) {
5920 VATTR_SET(vap, va_acl, oacl);
5921 vap->va_base_acl = NULL;
5922 } else {
5923 VATTR_CLEAR_ACTIVE(vap, va_acl);
5924 }
5925
5926 if (nacl != NULL) {
5927 kauth_acl_free(nacl);
5928 }
5929 }
5930
5931 if ((defaulted_fields & VATTR_PREPARE_DEFAULTED_MODE) != 0) {
5932 VATTR_CLEAR_ACTIVE(vap, va_mode);
5933 }
5934 if ((defaulted_fields & VATTR_PREPARE_DEFAULTED_GID) != 0) {
5935 VATTR_CLEAR_ACTIVE(vap, va_gid);
5936 }
5937 if ((defaulted_fields & VATTR_PREPARE_DEFAULTED_UID) != 0) {
5938 VATTR_CLEAR_ACTIVE(vap, va_uid);
5939 }
5940
5941 return;
5942 }
5943
5944 int
5945 vn_authorize_unlink(vnode_t dvp, vnode_t vp, struct componentname *cnp, vfs_context_t ctx, __unused void *reserved)
5946 {
5947 #if !CONFIG_MACF
5948 #pragma unused(cnp)
5949 #endif
5950 int error = 0;
5951
5952 /*
5953 * Normally, unlinking of directories is not supported.
5954 * However, some file systems may have limited support.
5955 */
5956 if ((vp->v_type == VDIR) &&
5957 !(vp->v_mount->mnt_kern_flag & MNTK_DIR_HARDLINKS)) {
5958 return (EPERM); /* POSIX */
5959 }
5960
5961 /* authorize the delete operation */
5962 #if CONFIG_MACF
5963 if (!error)
5964 error = mac_vnode_check_unlink(ctx, dvp, vp, cnp);
5965 #endif /* MAC */
5966 if (!error)
5967 error = vnode_authorize(vp, dvp, KAUTH_VNODE_DELETE, ctx);
5968
5969 return error;
5970 }
5971
5972 int
5973 vn_authorize_open_existing(vnode_t vp, struct componentname *cnp, int fmode, vfs_context_t ctx, void *reserved)
5974 {
5975 /* Open of existing case */
5976 kauth_action_t action;
5977 int error = 0;
5978 if (cnp->cn_ndp == NULL) {
5979 panic("NULL ndp");
5980 }
5981 if (reserved != NULL) {
5982 panic("reserved not NULL.");
5983 }
5984
5985 #if CONFIG_MACF
5986 /* XXX may do duplicate work here, but ignore that for now (idempotent) */
5987 if (vfs_flags(vnode_mount(vp)) & MNT_MULTILABEL) {
5988 error = vnode_label(vnode_mount(vp), NULL, vp, NULL, 0, ctx);
5989 if (error)
5990 return (error);
5991 }
5992 #endif
5993
5994 if ( (fmode & O_DIRECTORY) && vp->v_type != VDIR ) {
5995 return (ENOTDIR);
5996 }
5997
5998 if (vp->v_type == VSOCK && vp->v_tag != VT_FDESC) {
5999 return (EOPNOTSUPP); /* Operation not supported on socket */
6000 }
6001
6002 if (vp->v_type == VLNK && (fmode & O_NOFOLLOW) != 0) {
6003 return (ELOOP); /* O_NOFOLLOW was specified and the target is a symbolic link */
6004 }
6005
6006 /* disallow write operations on directories */
6007 if (vnode_isdir(vp) && (fmode & (FWRITE | O_TRUNC))) {
6008 return (EISDIR);
6009 }
6010
6011 if ((cnp->cn_ndp->ni_flag & NAMEI_TRAILINGSLASH)) {
6012 if (vp->v_type != VDIR) {
6013 return (ENOTDIR);
6014 }
6015 }
6016
6017 #if CONFIG_MACF
6018 /* If a file being opened is a shadow file containing
6019 * namedstream data, ignore the macf checks because it
6020 * is a kernel internal file and access should always
6021 * be allowed.
6022 */
6023 if (!(vnode_isshadow(vp) && vnode_isnamedstream(vp))) {
6024 error = mac_vnode_check_open(ctx, vp, fmode);
6025 if (error) {
6026 return (error);
6027 }
6028 }
6029 #endif
6030
6031 /* compute action to be authorized */
6032 action = 0;
6033 if (fmode & FREAD) {
6034 action |= KAUTH_VNODE_READ_DATA;
6035 }
6036 if (fmode & (FWRITE | O_TRUNC)) {
6037 /*
6038 * If we are writing, appending, and not truncating,
6039 * indicate that we are appending so that if the
6040 * UF_APPEND or SF_APPEND bits are set, we do not deny
6041 * the open.
6042 */
6043 if ((fmode & O_APPEND) && !(fmode & O_TRUNC)) {
6044 action |= KAUTH_VNODE_APPEND_DATA;
6045 } else {
6046 action |= KAUTH_VNODE_WRITE_DATA;
6047 }
6048 }
6049 error = vnode_authorize(vp, NULL, action, ctx);
6050 #if NAMEDSTREAMS
6051 if (error == EACCES) {
6052 /*
6053 * Shadow files may exist on-disk with a different UID/GID
6054 * than that of the current context. Verify that this file
6055 * is really a shadow file. If it was created successfully
6056 * then it should be authorized.
6057 */
6058 if (vnode_isshadow(vp) && vnode_isnamedstream (vp)) {
6059 error = vnode_verifynamedstream(vp);
6060 }
6061 }
6062 #endif
6063
6064 return error;
6065 }
6066
6067 int
6068 vn_authorize_create(vnode_t dvp, struct componentname *cnp, struct vnode_attr *vap, vfs_context_t ctx, void *reserved)
6069 {
6070 #if !CONFIG_MACF
6071 #pragma unused(vap)
6072 #endif
6073 /* Creation case */
6074 int error;
6075
6076 if (cnp->cn_ndp == NULL) {
6077 panic("NULL cn_ndp");
6078 }
6079 if (reserved != NULL) {
6080 panic("reserved not NULL.");
6081 }
6082
6083 /* Only validate path for creation if we didn't do a complete lookup */
6084 if (cnp->cn_ndp->ni_flag & NAMEI_UNFINISHED) {
6085 error = lookup_validate_creation_path(cnp->cn_ndp);
6086 if (error)
6087 return (error);
6088 }
6089
6090 #if CONFIG_MACF
6091 error = mac_vnode_check_create(ctx, dvp, cnp, vap);
6092 if (error)
6093 return (error);
6094 #endif /* CONFIG_MACF */
6095
6096 return (vnode_authorize(dvp, NULL, KAUTH_VNODE_ADD_FILE, ctx));
6097 }
6098
6099 int
6100 vn_authorize_rename(struct vnode *fdvp, struct vnode *fvp, struct componentname *fcnp,
6101 struct vnode *tdvp, struct vnode *tvp, struct componentname *tcnp,
6102 vfs_context_t ctx, void *reserved)
6103 {
6104 return vn_authorize_renamex(fdvp, fvp, fcnp, tdvp, tvp, tcnp, ctx, 0, reserved);
6105 }
6106
6107 int
6108 vn_authorize_renamex(struct vnode *fdvp, struct vnode *fvp, struct componentname *fcnp,
6109 struct vnode *tdvp, struct vnode *tvp, struct componentname *tcnp,
6110 vfs_context_t ctx, vfs_rename_flags_t flags, void *reserved)
6111 {
6112 int error = 0;
6113 int moving = 0;
6114 bool swap = flags & VFS_RENAME_SWAP;
6115
6116 if (reserved != NULL) {
6117 panic("Passed something other than NULL as reserved field!");
6118 }
6119
6120 /*
6121 * Avoid renaming "." and "..".
6122 *
6123 * XXX No need to check for this in the FS. We should always have the leaves
6124 * in VFS in this case.
6125 */
6126 if (fvp->v_type == VDIR &&
6127 ((fdvp == fvp) ||
6128 (fcnp->cn_namelen == 1 && fcnp->cn_nameptr[0] == '.') ||
6129 ((fcnp->cn_flags | tcnp->cn_flags) & ISDOTDOT)) ) {
6130 error = EINVAL;
6131 goto out;
6132 }
6133
6134 if (tvp == NULLVP && vnode_compound_rename_available(tdvp)) {
6135 error = lookup_validate_creation_path(tcnp->cn_ndp);
6136 if (error)
6137 goto out;
6138 }
6139
6140 /***** <MACF> *****/
6141 #if CONFIG_MACF
6142 error = mac_vnode_check_rename(ctx, fdvp, fvp, fcnp, tdvp, tvp, tcnp);
6143 if (error)
6144 goto out;
6145 if (swap) {
6146 error = mac_vnode_check_rename(ctx, tdvp, tvp, tcnp, fdvp, fvp, fcnp);
6147 if (error)
6148 goto out;
6149 }
6150 #endif
6151 /***** </MACF> *****/
6152
6153 /***** <MiscChecks> *****/
6154 if (tvp != NULL) {
6155 if (!swap) {
6156 if (fvp->v_type == VDIR && tvp->v_type != VDIR) {
6157 error = ENOTDIR;
6158 goto out;
6159 } else if (fvp->v_type != VDIR && tvp->v_type == VDIR) {
6160 error = EISDIR;
6161 goto out;
6162 }
6163 }
6164 } else if (swap) {
6165 /*
6166 * Caller should have already checked this and returned
6167 * ENOENT. If we send back ENOENT here, caller will retry
6168 * which isn't what we want so we send back EINVAL here
6169 * instead.
6170 */
6171 error = EINVAL;
6172 goto out;
6173 }
6174
6175 if (fvp == tdvp) {
6176 error = EINVAL;
6177 goto out;
6178 }
6179
6180 /*
6181 * The following edge case is caught here:
6182 * (to cannot be a descendent of from)
6183 *
6184 * o fdvp
6185 * /
6186 * /
6187 * o fvp
6188 * \
6189 * \
6190 * o tdvp
6191 * /
6192 * /
6193 * o tvp
6194 */
6195 if (tdvp->v_parent == fvp) {
6196 error = EINVAL;
6197 goto out;
6198 }
6199
6200 if (swap && fdvp->v_parent == tvp) {
6201 error = EINVAL;
6202 goto out;
6203 }
6204 /***** </MiscChecks> *****/
6205
6206 /***** <Kauth> *****/
6207
6208 if (swap) {
6209 kauth_action_t f = 0, t = 0;
6210
6211 /*
6212 * Directories changing parents need ...ADD_SUBDIR... to
6213 * permit changing ".."
6214 */
6215 if (fdvp != tdvp) {
6216 if (vnode_isdir(fvp))
6217 f = KAUTH_VNODE_ADD_SUBDIRECTORY;
6218 if (vnode_isdir(tvp))
6219 t = KAUTH_VNODE_ADD_SUBDIRECTORY;
6220 }
6221 error = vnode_authorize(fvp, fdvp, KAUTH_VNODE_DELETE | f, ctx);
6222 if (error)
6223 goto out;
6224 error = vnode_authorize(tvp, tdvp, KAUTH_VNODE_DELETE | t, ctx);
6225 if (error)
6226 goto out;
6227 f = vnode_isdir(fvp) ? KAUTH_VNODE_ADD_SUBDIRECTORY : KAUTH_VNODE_ADD_FILE;
6228 t = vnode_isdir(tvp) ? KAUTH_VNODE_ADD_SUBDIRECTORY : KAUTH_VNODE_ADD_FILE;
6229 if (fdvp == tdvp)
6230 error = vnode_authorize(fdvp, NULL, f | t, ctx);
6231 else {
6232 error = vnode_authorize(fdvp, NULL, t, ctx);
6233 if (error)
6234 goto out;
6235 error = vnode_authorize(tdvp, NULL, f, ctx);
6236 }
6237 if (error)
6238 goto out;
6239 } else {
6240 error = 0;
6241 if ((tvp != NULL) && vnode_isdir(tvp)) {
6242 if (tvp != fdvp)
6243 moving = 1;
6244 } else if (tdvp != fdvp) {
6245 moving = 1;
6246 }
6247
6248 /*
6249 * must have delete rights to remove the old name even in
6250 * the simple case of fdvp == tdvp.
6251 *
6252 * If fvp is a directory, and we are changing it's parent,
6253 * then we also need rights to rewrite its ".." entry as well.
6254 */
6255 if (vnode_isdir(fvp)) {
6256 if ((error = vnode_authorize(fvp, fdvp, KAUTH_VNODE_DELETE | KAUTH_VNODE_ADD_SUBDIRECTORY, ctx)) != 0)
6257 goto out;
6258 } else {
6259 if ((error = vnode_authorize(fvp, fdvp, KAUTH_VNODE_DELETE, ctx)) != 0)
6260 goto out;
6261 }
6262 if (moving) {
6263 /* moving into tdvp or tvp, must have rights to add */
6264 if ((error = vnode_authorize(((tvp != NULL) && vnode_isdir(tvp)) ? tvp : tdvp,
6265 NULL,
6266 vnode_isdir(fvp) ? KAUTH_VNODE_ADD_SUBDIRECTORY : KAUTH_VNODE_ADD_FILE,
6267 ctx)) != 0) {
6268 goto out;
6269 }
6270 } else {
6271 /* node staying in same directory, must be allowed to add new name */
6272 if ((error = vnode_authorize(fdvp, NULL,
6273 vnode_isdir(fvp) ? KAUTH_VNODE_ADD_SUBDIRECTORY : KAUTH_VNODE_ADD_FILE, ctx)) != 0)
6274 goto out;
6275 }
6276 /* overwriting tvp */
6277 if ((tvp != NULL) && !vnode_isdir(tvp) &&
6278 ((error = vnode_authorize(tvp, tdvp, KAUTH_VNODE_DELETE, ctx)) != 0)) {
6279 goto out;
6280 }
6281 }
6282
6283 /***** </Kauth> *****/
6284
6285 /* XXX more checks? */
6286 out:
6287 return error;
6288 }
6289
6290 int
6291 vn_authorize_mkdir(vnode_t dvp, struct componentname *cnp, struct vnode_attr *vap, vfs_context_t ctx, void *reserved)
6292 {
6293 #if !CONFIG_MACF
6294 #pragma unused(vap)
6295 #endif
6296 int error;
6297
6298 if (reserved != NULL) {
6299 panic("reserved not NULL in vn_authorize_mkdir()");
6300 }
6301
6302 /* XXX A hack for now, to make shadow files work */
6303 if (cnp->cn_ndp == NULL) {
6304 return 0;
6305 }
6306
6307 if (vnode_compound_mkdir_available(dvp)) {
6308 error = lookup_validate_creation_path(cnp->cn_ndp);
6309 if (error)
6310 goto out;
6311 }
6312
6313 #if CONFIG_MACF
6314 error = mac_vnode_check_create(ctx,
6315 dvp, cnp, vap);
6316 if (error)
6317 goto out;
6318 #endif
6319
6320 /* authorize addition of a directory to the parent */
6321 if ((error = vnode_authorize(dvp, NULL, KAUTH_VNODE_ADD_SUBDIRECTORY, ctx)) != 0)
6322 goto out;
6323
6324 out:
6325 return error;
6326 }
6327
6328 int
6329 vn_authorize_rmdir(vnode_t dvp, vnode_t vp, struct componentname *cnp, vfs_context_t ctx, void *reserved)
6330 {
6331 #if CONFIG_MACF
6332 int error;
6333 #else
6334 #pragma unused(cnp)
6335 #endif
6336 if (reserved != NULL) {
6337 panic("Non-NULL reserved argument to vn_authorize_rmdir()");
6338 }
6339
6340 if (vp->v_type != VDIR) {
6341 /*
6342 * rmdir only deals with directories
6343 */
6344 return ENOTDIR;
6345 }
6346
6347 if (dvp == vp) {
6348 /*
6349 * No rmdir "." please.
6350 */
6351 return EINVAL;
6352 }
6353
6354 #if CONFIG_MACF
6355 error = mac_vnode_check_unlink(ctx, dvp,
6356 vp, cnp);
6357 if (error)
6358 return error;
6359 #endif
6360
6361 return vnode_authorize(vp, dvp, KAUTH_VNODE_DELETE, ctx);
6362 }
6363
6364 /*
6365 * Authorizer for directory cloning. This does not use vnodes but instead
6366 * uses prefilled vnode attributes from the filesystem.
6367 *
6368 * The same function is called to set up the attributes required, perform the
6369 * authorization and cleanup (if required)
6370 */
6371 int
6372 vnode_attr_authorize_dir_clone(struct vnode_attr *vap, kauth_action_t action,
6373 struct vnode_attr *dvap, __unused vnode_t sdvp, mount_t mp,
6374 dir_clone_authorizer_op_t vattr_op, vfs_context_t ctx,
6375 __unused void *reserved)
6376 {
6377 int error;
6378 int is_suser = vfs_context_issuser(ctx);
6379
6380 if (vattr_op == OP_VATTR_SETUP) {
6381 VATTR_INIT(vap);
6382
6383 /*
6384 * When ACL inheritence is implemented, both vap->va_acl and
6385 * dvap->va_acl will be required (even as superuser).
6386 */
6387 VATTR_WANTED(vap, va_type);
6388 VATTR_WANTED(vap, va_mode);
6389 VATTR_WANTED(vap, va_flags);
6390 VATTR_WANTED(vap, va_uid);
6391 VATTR_WANTED(vap, va_gid);
6392 if (dvap) {
6393 VATTR_INIT(dvap);
6394 VATTR_WANTED(dvap, va_flags);
6395 }
6396
6397 if (!is_suser) {
6398 /*
6399 * If not superuser, we have to evaluate ACLs and
6400 * need the target directory gid to set the initial
6401 * gid of the new object.
6402 */
6403 VATTR_WANTED(vap, va_acl);
6404 if (dvap)
6405 VATTR_WANTED(dvap, va_gid);
6406 }
6407
6408 return (0);
6409 } else if (vattr_op == OP_VATTR_CLEANUP) {
6410 return (0); /* Nothing to do for now */
6411 }
6412
6413 /* dvap isn't used for authorization */
6414 error = vnode_attr_authorize(vap, NULL, mp, action, ctx);
6415
6416 if (error)
6417 return (error);
6418
6419 /*
6420 * vn_attribute_prepare should be able to accept attributes as well as
6421 * vnodes but for now we do this inline.
6422 */
6423 if (!is_suser) {
6424 /*
6425 * If the filesystem is mounted IGNORE_OWNERSHIP and an explicit
6426 * owner is set, that owner takes ownership of all new files.
6427 */
6428 if ((mp->mnt_flag & MNT_IGNORE_OWNERSHIP) &&
6429 (mp->mnt_fsowner != KAUTH_UID_NONE)) {
6430 VATTR_SET(vap, va_uid, mp->mnt_fsowner);
6431 } else {
6432 /* default owner is current user */
6433 VATTR_SET(vap, va_uid,
6434 kauth_cred_getuid(vfs_context_ucred(ctx)));
6435 }
6436
6437 if ((mp->mnt_flag & MNT_IGNORE_OWNERSHIP) &&
6438 (mp->mnt_fsgroup != KAUTH_GID_NONE)) {
6439 VATTR_SET(vap, va_gid, mp->mnt_fsgroup);
6440 } else {
6441 /*
6442 * default group comes from parent object,
6443 * fallback to current user
6444 */
6445 if (VATTR_IS_SUPPORTED(dvap, va_gid)) {
6446 VATTR_SET(vap, va_gid, dvap->va_gid);
6447 } else {
6448 VATTR_SET(vap, va_gid,
6449 kauth_cred_getgid(vfs_context_ucred(ctx)));
6450 }
6451 }
6452 }
6453
6454 /* Inherit SF_RESTRICTED bit from destination directory only */
6455 if (VATTR_IS_ACTIVE(vap, va_flags)) {
6456 VATTR_SET(vap, va_flags,
6457 ((vap->va_flags & ~SF_RESTRICTED))); /* Turn off from source */
6458 if (VATTR_IS_ACTIVE(dvap, va_flags))
6459 VATTR_SET(vap, va_flags,
6460 vap->va_flags | (dvap->va_flags & SF_RESTRICTED));
6461 } else if (VATTR_IS_ACTIVE(dvap, va_flags)) {
6462 VATTR_SET(vap, va_flags, (dvap->va_flags & SF_RESTRICTED));
6463 }
6464
6465 return (0);
6466 }
6467
6468
6469 /*
6470 * Authorize an operation on a vnode.
6471 *
6472 * This is KPI, but here because it needs vnode_scope.
6473 *
6474 * Returns: 0 Success
6475 * kauth_authorize_action:EPERM ...
6476 * xlate => EACCES Permission denied
6477 * kauth_authorize_action:0 Success
6478 * kauth_authorize_action: Depends on callback return; this is
6479 * usually only vnode_authorize_callback(),
6480 * but may include other listerners, if any
6481 * exist.
6482 * EROFS
6483 * EACCES
6484 * EPERM
6485 * ???
6486 */
6487 int
6488 vnode_authorize(vnode_t vp, vnode_t dvp, kauth_action_t action, vfs_context_t ctx)
6489 {
6490 int error, result;
6491
6492 /*
6493 * We can't authorize against a dead vnode; allow all operations through so that
6494 * the correct error can be returned.
6495 */
6496 if (vp->v_type == VBAD)
6497 return(0);
6498
6499 error = 0;
6500 result = kauth_authorize_action(vnode_scope, vfs_context_ucred(ctx), action,
6501 (uintptr_t)ctx, (uintptr_t)vp, (uintptr_t)dvp, (uintptr_t)&error);
6502 if (result == EPERM) /* traditional behaviour */
6503 result = EACCES;
6504 /* did the lower layers give a better error return? */
6505 if ((result != 0) && (error != 0))
6506 return(error);
6507 return(result);
6508 }
6509
6510 /*
6511 * Test for vnode immutability.
6512 *
6513 * The 'append' flag is set when the authorization request is constrained
6514 * to operations which only request the right to append to a file.
6515 *
6516 * The 'ignore' flag is set when an operation modifying the immutability flags
6517 * is being authorized. We check the system securelevel to determine which
6518 * immutability flags we can ignore.
6519 */
6520 static int
6521 vnode_immutable(struct vnode_attr *vap, int append, int ignore)
6522 {
6523 int mask;
6524
6525 /* start with all bits precluding the operation */
6526 mask = IMMUTABLE | APPEND;
6527
6528 /* if appending only, remove the append-only bits */
6529 if (append)
6530 mask &= ~APPEND;
6531
6532 /* ignore only set when authorizing flags changes */
6533 if (ignore) {
6534 if (securelevel <= 0) {
6535 /* in insecure state, flags do not inhibit changes */
6536 mask = 0;
6537 } else {
6538 /* in secure state, user flags don't inhibit */
6539 mask &= ~(UF_IMMUTABLE | UF_APPEND);
6540 }
6541 }
6542 KAUTH_DEBUG("IMMUTABLE - file flags 0x%x mask 0x%x append = %d ignore = %d", vap->va_flags, mask, append, ignore);
6543 if ((vap->va_flags & mask) != 0)
6544 return(EPERM);
6545 return(0);
6546 }
6547
6548 static int
6549 vauth_node_owner(struct vnode_attr *vap, kauth_cred_t cred)
6550 {
6551 int result;
6552
6553 /* default assumption is not-owner */
6554 result = 0;
6555
6556 /*
6557 * If the filesystem has given us a UID, we treat this as authoritative.
6558 */
6559 if (vap && VATTR_IS_SUPPORTED(vap, va_uid)) {
6560 result = (vap->va_uid == kauth_cred_getuid(cred)) ? 1 : 0;
6561 }
6562 /* we could test the owner UUID here if we had a policy for it */
6563
6564 return(result);
6565 }
6566
6567 /*
6568 * vauth_node_group
6569 *
6570 * Description: Ask if a cred is a member of the group owning the vnode object
6571 *
6572 * Parameters: vap vnode attribute
6573 * vap->va_gid group owner of vnode object
6574 * cred credential to check
6575 * ismember pointer to where to put the answer
6576 * idontknow Return this if we can't get an answer
6577 *
6578 * Returns: 0 Success
6579 * idontknow Can't get information
6580 * kauth_cred_ismember_gid:? Error from kauth subsystem
6581 * kauth_cred_ismember_gid:? Error from kauth subsystem
6582 */
6583 static int
6584 vauth_node_group(struct vnode_attr *vap, kauth_cred_t cred, int *ismember, int idontknow)
6585 {
6586 int error;
6587 int result;
6588
6589 error = 0;
6590 result = 0;
6591
6592 /*
6593 * The caller is expected to have asked the filesystem for a group
6594 * at some point prior to calling this function. The answer may
6595 * have been that there is no group ownership supported for the
6596 * vnode object, in which case we return
6597 */
6598 if (vap && VATTR_IS_SUPPORTED(vap, va_gid)) {
6599 error = kauth_cred_ismember_gid(cred, vap->va_gid, &result);
6600 /*
6601 * Credentials which are opted into external group membership
6602 * resolution which are not known to the external resolver
6603 * will result in an ENOENT error. We translate this into
6604 * the appropriate 'idontknow' response for our caller.
6605 *
6606 * XXX We do not make a distinction here between an ENOENT
6607 * XXX arising from a response from the external resolver,
6608 * XXX and an ENOENT which is internally generated. This is
6609 * XXX a deficiency of the published kauth_cred_ismember_gid()
6610 * XXX KPI which can not be overcome without new KPI. For
6611 * XXX all currently known cases, however, this wil result
6612 * XXX in correct behaviour.
6613 */
6614 if (error == ENOENT)
6615 error = idontknow;
6616 }
6617 /*
6618 * XXX We could test the group UUID here if we had a policy for it,
6619 * XXX but this is problematic from the perspective of synchronizing
6620 * XXX group UUID and POSIX GID ownership of a file and keeping the
6621 * XXX values coherent over time. The problem is that the local
6622 * XXX system will vend transient group UUIDs for unknown POSIX GID
6623 * XXX values, and these are not persistent, whereas storage of values
6624 * XXX is persistent. One potential solution to this is a local
6625 * XXX (persistent) replica of remote directory entries and vended
6626 * XXX local ids in a local directory server (think in terms of a
6627 * XXX caching DNS server).
6628 */
6629
6630 if (!error)
6631 *ismember = result;
6632 return(error);
6633 }
6634
6635 static int
6636 vauth_file_owner(vauth_ctx vcp)
6637 {
6638 int result;
6639
6640 if (vcp->flags_valid & _VAC_IS_OWNER) {
6641 result = (vcp->flags & _VAC_IS_OWNER) ? 1 : 0;
6642 } else {
6643 result = vauth_node_owner(vcp->vap, vcp->ctx->vc_ucred);
6644
6645 /* cache our result */
6646 vcp->flags_valid |= _VAC_IS_OWNER;
6647 if (result) {
6648 vcp->flags |= _VAC_IS_OWNER;
6649 } else {
6650 vcp->flags &= ~_VAC_IS_OWNER;
6651 }
6652 }
6653 return(result);
6654 }
6655
6656
6657 /*
6658 * vauth_file_ingroup
6659 *
6660 * Description: Ask if a user is a member of the group owning the directory
6661 *
6662 * Parameters: vcp The vnode authorization context that
6663 * contains the user and directory info
6664 * vcp->flags_valid Valid flags
6665 * vcp->flags Flags values
6666 * vcp->vap File vnode attributes
6667 * vcp->ctx VFS Context (for user)
6668 * ismember pointer to where to put the answer
6669 * idontknow Return this if we can't get an answer
6670 *
6671 * Returns: 0 Success
6672 * vauth_node_group:? Error from vauth_node_group()
6673 *
6674 * Implicit returns: *ismember 0 The user is not a group member
6675 * 1 The user is a group member
6676 */
6677 static int
6678 vauth_file_ingroup(vauth_ctx vcp, int *ismember, int idontknow)
6679 {
6680 int error;
6681
6682 /* Check for a cached answer first, to avoid the check if possible */
6683 if (vcp->flags_valid & _VAC_IN_GROUP) {
6684 *ismember = (vcp->flags & _VAC_IN_GROUP) ? 1 : 0;
6685 error = 0;
6686 } else {
6687 /* Otherwise, go look for it */
6688 error = vauth_node_group(vcp->vap, vcp->ctx->vc_ucred, ismember, idontknow);
6689
6690 if (!error) {
6691 /* cache our result */
6692 vcp->flags_valid |= _VAC_IN_GROUP;
6693 if (*ismember) {
6694 vcp->flags |= _VAC_IN_GROUP;
6695 } else {
6696 vcp->flags &= ~_VAC_IN_GROUP;
6697 }
6698 }
6699
6700 }
6701 return(error);
6702 }
6703
6704 static int
6705 vauth_dir_owner(vauth_ctx vcp)
6706 {
6707 int result;
6708
6709 if (vcp->flags_valid & _VAC_IS_DIR_OWNER) {
6710 result = (vcp->flags & _VAC_IS_DIR_OWNER) ? 1 : 0;
6711 } else {
6712 result = vauth_node_owner(vcp->dvap, vcp->ctx->vc_ucred);
6713
6714 /* cache our result */
6715 vcp->flags_valid |= _VAC_IS_DIR_OWNER;
6716 if (result) {
6717 vcp->flags |= _VAC_IS_DIR_OWNER;
6718 } else {
6719 vcp->flags &= ~_VAC_IS_DIR_OWNER;
6720 }
6721 }
6722 return(result);
6723 }
6724
6725 /*
6726 * vauth_dir_ingroup
6727 *
6728 * Description: Ask if a user is a member of the group owning the directory
6729 *
6730 * Parameters: vcp The vnode authorization context that
6731 * contains the user and directory info
6732 * vcp->flags_valid Valid flags
6733 * vcp->flags Flags values
6734 * vcp->dvap Dir vnode attributes
6735 * vcp->ctx VFS Context (for user)
6736 * ismember pointer to where to put the answer
6737 * idontknow Return this if we can't get an answer
6738 *
6739 * Returns: 0 Success
6740 * vauth_node_group:? Error from vauth_node_group()
6741 *
6742 * Implicit returns: *ismember 0 The user is not a group member
6743 * 1 The user is a group member
6744 */
6745 static int
6746 vauth_dir_ingroup(vauth_ctx vcp, int *ismember, int idontknow)
6747 {
6748 int error;
6749
6750 /* Check for a cached answer first, to avoid the check if possible */
6751 if (vcp->flags_valid & _VAC_IN_DIR_GROUP) {
6752 *ismember = (vcp->flags & _VAC_IN_DIR_GROUP) ? 1 : 0;
6753 error = 0;
6754 } else {
6755 /* Otherwise, go look for it */
6756 error = vauth_node_group(vcp->dvap, vcp->ctx->vc_ucred, ismember, idontknow);
6757
6758 if (!error) {
6759 /* cache our result */
6760 vcp->flags_valid |= _VAC_IN_DIR_GROUP;
6761 if (*ismember) {
6762 vcp->flags |= _VAC_IN_DIR_GROUP;
6763 } else {
6764 vcp->flags &= ~_VAC_IN_DIR_GROUP;
6765 }
6766 }
6767 }
6768 return(error);
6769 }
6770
6771 /*
6772 * Test the posix permissions in (vap) to determine whether (credential)
6773 * may perform (action)
6774 */
6775 static int
6776 vnode_authorize_posix(vauth_ctx vcp, int action, int on_dir)
6777 {
6778 struct vnode_attr *vap;
6779 int needed, error, owner_ok, group_ok, world_ok, ismember;
6780 #ifdef KAUTH_DEBUG_ENABLE
6781 const char *where = "uninitialized";
6782 # define _SETWHERE(c) where = c;
6783 #else
6784 # define _SETWHERE(c)
6785 #endif
6786
6787 /* checking file or directory? */
6788 if (on_dir) {
6789 vap = vcp->dvap;
6790 } else {
6791 vap = vcp->vap;
6792 }
6793
6794 error = 0;
6795
6796 /*
6797 * We want to do as little work here as possible. So first we check
6798 * which sets of permissions grant us the access we need, and avoid checking
6799 * whether specific permissions grant access when more generic ones would.
6800 */
6801
6802 /* owner permissions */
6803 needed = 0;
6804 if (action & VREAD)
6805 needed |= S_IRUSR;
6806 if (action & VWRITE)
6807 needed |= S_IWUSR;
6808 if (action & VEXEC)
6809 needed |= S_IXUSR;
6810 owner_ok = (needed & vap->va_mode) == needed;
6811
6812 /* group permissions */
6813 needed = 0;
6814 if (action & VREAD)
6815 needed |= S_IRGRP;
6816 if (action & VWRITE)
6817 needed |= S_IWGRP;
6818 if (action & VEXEC)
6819 needed |= S_IXGRP;
6820 group_ok = (needed & vap->va_mode) == needed;
6821
6822 /* world permissions */
6823 needed = 0;
6824 if (action & VREAD)
6825 needed |= S_IROTH;
6826 if (action & VWRITE)
6827 needed |= S_IWOTH;
6828 if (action & VEXEC)
6829 needed |= S_IXOTH;
6830 world_ok = (needed & vap->va_mode) == needed;
6831
6832 /* If granted/denied by all three, we're done */
6833 if (owner_ok && group_ok && world_ok) {
6834 _SETWHERE("all");
6835 goto out;
6836 }
6837 if (!owner_ok && !group_ok && !world_ok) {
6838 _SETWHERE("all");
6839 error = EACCES;
6840 goto out;
6841 }
6842
6843 /* Check ownership (relatively cheap) */
6844 if ((on_dir && vauth_dir_owner(vcp)) ||
6845 (!on_dir && vauth_file_owner(vcp))) {
6846 _SETWHERE("user");
6847 if (!owner_ok)
6848 error = EACCES;
6849 goto out;
6850 }
6851
6852 /* Not owner; if group and world both grant it we're done */
6853 if (group_ok && world_ok) {
6854 _SETWHERE("group/world");
6855 goto out;
6856 }
6857 if (!group_ok && !world_ok) {
6858 _SETWHERE("group/world");
6859 error = EACCES;
6860 goto out;
6861 }
6862
6863 /* Check group membership (most expensive) */
6864 ismember = 0; /* Default to allow, if the target has no group owner */
6865
6866 /*
6867 * In the case we can't get an answer about the user from the call to
6868 * vauth_dir_ingroup() or vauth_file_ingroup(), we want to fail on
6869 * the side of caution, rather than simply granting access, or we will
6870 * fail to correctly implement exclusion groups, so we set the third
6871 * parameter on the basis of the state of 'group_ok'.
6872 */
6873 if (on_dir) {
6874 error = vauth_dir_ingroup(vcp, &ismember, (!group_ok ? EACCES : 0));
6875 } else {
6876 error = vauth_file_ingroup(vcp, &ismember, (!group_ok ? EACCES : 0));
6877 }
6878 if (error) {
6879 if (!group_ok)
6880 ismember = 1;
6881 error = 0;
6882 }
6883 if (ismember) {
6884 _SETWHERE("group");
6885 if (!group_ok)
6886 error = EACCES;
6887 goto out;
6888 }
6889
6890 /* Not owner, not in group, use world result */
6891 _SETWHERE("world");
6892 if (!world_ok)
6893 error = EACCES;
6894
6895 /* FALLTHROUGH */
6896
6897 out:
6898 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",
6899 vcp->vp, (error == 0) ? "ALLOWED" : "DENIED", where,
6900 (action & VREAD) ? "r" : "-",
6901 (action & VWRITE) ? "w" : "-",
6902 (action & VEXEC) ? "x" : "-",
6903 needed,
6904 (vap->va_mode & S_IRUSR) ? "r" : "-",
6905 (vap->va_mode & S_IWUSR) ? "w" : "-",
6906 (vap->va_mode & S_IXUSR) ? "x" : "-",
6907 (vap->va_mode & S_IRGRP) ? "r" : "-",
6908 (vap->va_mode & S_IWGRP) ? "w" : "-",
6909 (vap->va_mode & S_IXGRP) ? "x" : "-",
6910 (vap->va_mode & S_IROTH) ? "r" : "-",
6911 (vap->va_mode & S_IWOTH) ? "w" : "-",
6912 (vap->va_mode & S_IXOTH) ? "x" : "-",
6913 kauth_cred_getuid(vcp->ctx->vc_ucred),
6914 on_dir ? vcp->dvap->va_uid : vcp->vap->va_uid,
6915 on_dir ? vcp->dvap->va_gid : vcp->vap->va_gid);
6916 return(error);
6917 }
6918
6919 /*
6920 * Authorize the deletion of the node vp from the directory dvp.
6921 *
6922 * We assume that:
6923 * - Neither the node nor the directory are immutable.
6924 * - The user is not the superuser.
6925 *
6926 * The precedence of factors for authorizing or denying delete for a credential
6927 *
6928 * 1) Explicit ACE on the node. (allow or deny DELETE)
6929 * 2) Explicit ACE on the directory (allow or deny DELETE_CHILD).
6930 *
6931 * If there are conflicting ACEs on the node and the directory, the node
6932 * ACE wins.
6933 *
6934 * 3) Sticky bit on the directory.
6935 * Deletion is not permitted if the directory is sticky and the caller is
6936 * not owner of the node or directory. The sticky bit rules are like a deny
6937 * delete ACE except lower in priority than ACL's either allowing or denying
6938 * delete.
6939 *
6940 * 4) POSIX permisions on the directory.
6941 *
6942 * As an optimization, we cache whether or not delete child is permitted
6943 * on directories. This enables us to skip directory ACL and POSIX checks
6944 * as we already have the result from those checks. However, we always check the
6945 * node ACL and, if the directory has the sticky bit set, we always check its
6946 * ACL (even for a directory with an authorized delete child). Furthermore,
6947 * caching the delete child authorization is independent of the sticky bit
6948 * being set as it is only applicable in determining whether the node can be
6949 * deleted or not.
6950 */
6951 static int
6952 vnode_authorize_delete(vauth_ctx vcp, boolean_t cached_delete_child)
6953 {
6954 struct vnode_attr *vap = vcp->vap;
6955 struct vnode_attr *dvap = vcp->dvap;
6956 kauth_cred_t cred = vcp->ctx->vc_ucred;
6957 struct kauth_acl_eval eval;
6958 int error, ismember;
6959
6960 /* Check the ACL on the node first */
6961 if (VATTR_IS_NOT(vap, va_acl, NULL)) {
6962 eval.ae_requested = KAUTH_VNODE_DELETE;
6963 eval.ae_acl = &vap->va_acl->acl_ace[0];
6964 eval.ae_count = vap->va_acl->acl_entrycount;
6965 eval.ae_options = 0;
6966 if (vauth_file_owner(vcp))
6967 eval.ae_options |= KAUTH_AEVAL_IS_OWNER;
6968 /*
6969 * We use ENOENT as a marker to indicate we could not get
6970 * information in order to delay evaluation until after we
6971 * have the ACL evaluation answer. Previously, we would
6972 * always deny the operation at this point.
6973 */
6974 if ((error = vauth_file_ingroup(vcp, &ismember, ENOENT)) != 0 && error != ENOENT)
6975 return (error);
6976 if (error == ENOENT)
6977 eval.ae_options |= KAUTH_AEVAL_IN_GROUP_UNKNOWN;
6978 else if (ismember)
6979 eval.ae_options |= KAUTH_AEVAL_IN_GROUP;
6980 eval.ae_exp_gall = KAUTH_VNODE_GENERIC_ALL_BITS;
6981 eval.ae_exp_gread = KAUTH_VNODE_GENERIC_READ_BITS;
6982 eval.ae_exp_gwrite = KAUTH_VNODE_GENERIC_WRITE_BITS;
6983 eval.ae_exp_gexec = KAUTH_VNODE_GENERIC_EXECUTE_BITS;
6984
6985 if ((error = kauth_acl_evaluate(cred, &eval)) != 0) {
6986 KAUTH_DEBUG("%p ERROR during ACL processing - %d", vcp->vp, error);
6987 return (error);
6988 }
6989
6990 switch(eval.ae_result) {
6991 case KAUTH_RESULT_DENY:
6992 KAUTH_DEBUG("%p DENIED - denied by ACL", vcp->vp);
6993 return (EACCES);
6994 case KAUTH_RESULT_ALLOW:
6995 KAUTH_DEBUG("%p ALLOWED - granted by ACL", vcp->vp);
6996 return (0);
6997 case KAUTH_RESULT_DEFER:
6998 default:
6999 /* Defer to directory */
7000 KAUTH_DEBUG("%p DEFERRED - by file ACL", vcp->vp);
7001 break;
7002 }
7003 }
7004
7005 /*
7006 * Without a sticky bit, a previously authorized delete child is
7007 * sufficient to authorize this delete.
7008 *
7009 * If the sticky bit is set, a directory ACL which allows delete child
7010 * overrides a (potential) sticky bit deny. The authorized delete child
7011 * cannot tell us if it was authorized because of an explicit delete
7012 * child allow ACE or because of POSIX permisions so we have to check
7013 * the directory ACL everytime if the directory has a sticky bit.
7014 */
7015 if (!(dvap->va_mode & S_ISTXT) && cached_delete_child) {
7016 KAUTH_DEBUG("%p ALLOWED - granted by directory ACL or POSIX permissions and no sticky bit on directory", vcp->vp);
7017 return (0);
7018 }
7019
7020 /* check the ACL on the directory */
7021 if (VATTR_IS_NOT(dvap, va_acl, NULL)) {
7022 eval.ae_requested = KAUTH_VNODE_DELETE_CHILD;
7023 eval.ae_acl = &dvap->va_acl->acl_ace[0];
7024 eval.ae_count = dvap->va_acl->acl_entrycount;
7025 eval.ae_options = 0;
7026 if (vauth_dir_owner(vcp))
7027 eval.ae_options |= KAUTH_AEVAL_IS_OWNER;
7028 /*
7029 * We use ENOENT as a marker to indicate we could not get
7030 * information in order to delay evaluation until after we
7031 * have the ACL evaluation answer. Previously, we would
7032 * always deny the operation at this point.
7033 */
7034 if ((error = vauth_dir_ingroup(vcp, &ismember, ENOENT)) != 0 && error != ENOENT)
7035 return(error);
7036 if (error == ENOENT)
7037 eval.ae_options |= KAUTH_AEVAL_IN_GROUP_UNKNOWN;
7038 else if (ismember)
7039 eval.ae_options |= KAUTH_AEVAL_IN_GROUP;
7040 eval.ae_exp_gall = KAUTH_VNODE_GENERIC_ALL_BITS;
7041 eval.ae_exp_gread = KAUTH_VNODE_GENERIC_READ_BITS;
7042 eval.ae_exp_gwrite = KAUTH_VNODE_GENERIC_WRITE_BITS;
7043 eval.ae_exp_gexec = KAUTH_VNODE_GENERIC_EXECUTE_BITS;
7044
7045 /*
7046 * If there is no entry, we are going to defer to other
7047 * authorization mechanisms.
7048 */
7049 error = kauth_acl_evaluate(cred, &eval);
7050
7051 if (error != 0) {
7052 KAUTH_DEBUG("%p ERROR during ACL processing - %d", vcp->vp, error);
7053 return (error);
7054 }
7055 switch(eval.ae_result) {
7056 case KAUTH_RESULT_DENY:
7057 KAUTH_DEBUG("%p DENIED - denied by directory ACL", vcp->vp);
7058 return (EACCES);
7059 case KAUTH_RESULT_ALLOW:
7060 KAUTH_DEBUG("%p ALLOWED - granted by directory ACL", vcp->vp);
7061 if (!cached_delete_child && vcp->dvp) {
7062 vnode_cache_authorized_action(vcp->dvp,
7063 vcp->ctx, KAUTH_VNODE_DELETE_CHILD);
7064 }
7065 return (0);
7066 case KAUTH_RESULT_DEFER:
7067 default:
7068 /* Deferred by directory ACL */
7069 KAUTH_DEBUG("%p DEFERRED - directory ACL", vcp->vp);
7070 break;
7071 }
7072 }
7073
7074 /*
7075 * From this point, we can't explicitly allow and if we reach the end
7076 * of the function without a denial, then the delete is authorized.
7077 */
7078 if (!cached_delete_child) {
7079 if (vnode_authorize_posix(vcp, VWRITE, 1 /* on_dir */) != 0) {
7080 KAUTH_DEBUG("%p DENIED - denied by posix permisssions", vcp->vp);
7081 return (EACCES);
7082 }
7083 /*
7084 * Cache the authorized action on the vnode if allowed by the
7085 * directory ACL or POSIX permissions. It is correct to cache
7086 * this action even if sticky bit would deny deleting the node.
7087 */
7088 if (vcp->dvp) {
7089 vnode_cache_authorized_action(vcp->dvp, vcp->ctx,
7090 KAUTH_VNODE_DELETE_CHILD);
7091 }
7092 }
7093
7094 /* enforce sticky bit behaviour */
7095 if ((dvap->va_mode & S_ISTXT) && !vauth_file_owner(vcp) && !vauth_dir_owner(vcp)) {
7096 KAUTH_DEBUG("%p DENIED - sticky bit rules (user %d file %d dir %d)",
7097 vcp->vp, cred->cr_posix.cr_uid, vap->va_uid, dvap->va_uid);
7098 return (EACCES);
7099 }
7100
7101 /* not denied, must be OK */
7102 return (0);
7103 }
7104
7105
7106 /*
7107 * Authorize an operation based on the node's attributes.
7108 */
7109 static int
7110 vnode_authorize_simple(vauth_ctx vcp, kauth_ace_rights_t acl_rights, kauth_ace_rights_t preauth_rights, boolean_t *found_deny)
7111 {
7112 struct vnode_attr *vap = vcp->vap;
7113 kauth_cred_t cred = vcp->ctx->vc_ucred;
7114 struct kauth_acl_eval eval;
7115 int error, ismember;
7116 mode_t posix_action;
7117
7118 /*
7119 * If we are the file owner, we automatically have some rights.
7120 *
7121 * Do we need to expand this to support group ownership?
7122 */
7123 if (vauth_file_owner(vcp))
7124 acl_rights &= ~(KAUTH_VNODE_WRITE_SECURITY);
7125
7126 /*
7127 * If we are checking both TAKE_OWNERSHIP and WRITE_SECURITY, we can
7128 * mask the latter. If TAKE_OWNERSHIP is requested the caller is about to
7129 * change ownership to themselves, and WRITE_SECURITY is implicitly
7130 * granted to the owner. We need to do this because at this point
7131 * WRITE_SECURITY may not be granted as the caller is not currently
7132 * the owner.
7133 */
7134 if ((acl_rights & KAUTH_VNODE_TAKE_OWNERSHIP) &&
7135 (acl_rights & KAUTH_VNODE_WRITE_SECURITY))
7136 acl_rights &= ~KAUTH_VNODE_WRITE_SECURITY;
7137
7138 if (acl_rights == 0) {
7139 KAUTH_DEBUG("%p ALLOWED - implicit or no rights required", vcp->vp);
7140 return(0);
7141 }
7142
7143 /* if we have an ACL, evaluate it */
7144 if (VATTR_IS_NOT(vap, va_acl, NULL)) {
7145 eval.ae_requested = acl_rights;
7146 eval.ae_acl = &vap->va_acl->acl_ace[0];
7147 eval.ae_count = vap->va_acl->acl_entrycount;
7148 eval.ae_options = 0;
7149 if (vauth_file_owner(vcp))
7150 eval.ae_options |= KAUTH_AEVAL_IS_OWNER;
7151 /*
7152 * We use ENOENT as a marker to indicate we could not get
7153 * information in order to delay evaluation until after we
7154 * have the ACL evaluation answer. Previously, we would
7155 * always deny the operation at this point.
7156 */
7157 if ((error = vauth_file_ingroup(vcp, &ismember, ENOENT)) != 0 && error != ENOENT)
7158 return(error);
7159 if (error == ENOENT)
7160 eval.ae_options |= KAUTH_AEVAL_IN_GROUP_UNKNOWN;
7161 else if (ismember)
7162 eval.ae_options |= KAUTH_AEVAL_IN_GROUP;
7163 eval.ae_exp_gall = KAUTH_VNODE_GENERIC_ALL_BITS;
7164 eval.ae_exp_gread = KAUTH_VNODE_GENERIC_READ_BITS;
7165 eval.ae_exp_gwrite = KAUTH_VNODE_GENERIC_WRITE_BITS;
7166 eval.ae_exp_gexec = KAUTH_VNODE_GENERIC_EXECUTE_BITS;
7167
7168 if ((error = kauth_acl_evaluate(cred, &eval)) != 0) {
7169 KAUTH_DEBUG("%p ERROR during ACL processing - %d", vcp->vp, error);
7170 return(error);
7171 }
7172
7173 switch(eval.ae_result) {
7174 case KAUTH_RESULT_DENY:
7175 KAUTH_DEBUG("%p DENIED - by ACL", vcp->vp);
7176 return(EACCES); /* deny, deny, counter-allege */
7177 case KAUTH_RESULT_ALLOW:
7178 KAUTH_DEBUG("%p ALLOWED - all rights granted by ACL", vcp->vp);
7179 return(0);
7180 case KAUTH_RESULT_DEFER:
7181 default:
7182 /* Effectively the same as !delete_child_denied */
7183 KAUTH_DEBUG("%p DEFERRED - directory ACL", vcp->vp);
7184 break;
7185 }
7186
7187 *found_deny = eval.ae_found_deny;
7188
7189 /* fall through and evaluate residual rights */
7190 } else {
7191 /* no ACL, everything is residual */
7192 eval.ae_residual = acl_rights;
7193 }
7194
7195 /*
7196 * Grant residual rights that have been pre-authorized.
7197 */
7198 eval.ae_residual &= ~preauth_rights;
7199
7200 /*
7201 * We grant WRITE_ATTRIBUTES to the owner if it hasn't been denied.
7202 */
7203 if (vauth_file_owner(vcp))
7204 eval.ae_residual &= ~KAUTH_VNODE_WRITE_ATTRIBUTES;
7205
7206 if (eval.ae_residual == 0) {
7207 KAUTH_DEBUG("%p ALLOWED - rights already authorized", vcp->vp);
7208 return(0);
7209 }
7210
7211 /*
7212 * Bail if we have residual rights that can't be granted by posix permissions,
7213 * or aren't presumed granted at this point.
7214 *
7215 * XXX these can be collapsed for performance
7216 */
7217 if (eval.ae_residual & KAUTH_VNODE_CHANGE_OWNER) {
7218 KAUTH_DEBUG("%p DENIED - CHANGE_OWNER not permitted", vcp->vp);
7219 return(EACCES);
7220 }
7221 if (eval.ae_residual & KAUTH_VNODE_WRITE_SECURITY) {
7222 KAUTH_DEBUG("%p DENIED - WRITE_SECURITY not permitted", vcp->vp);
7223 return(EACCES);
7224 }
7225
7226 #if DIAGNOSTIC
7227 if (eval.ae_residual & KAUTH_VNODE_DELETE)
7228 panic("vnode_authorize: can't be checking delete permission here");
7229 #endif
7230
7231 /*
7232 * Compute the fallback posix permissions that will satisfy the remaining
7233 * rights.
7234 */
7235 posix_action = 0;
7236 if (eval.ae_residual & (KAUTH_VNODE_READ_DATA |
7237 KAUTH_VNODE_LIST_DIRECTORY |
7238 KAUTH_VNODE_READ_EXTATTRIBUTES))
7239 posix_action |= VREAD;
7240 if (eval.ae_residual & (KAUTH_VNODE_WRITE_DATA |
7241 KAUTH_VNODE_ADD_FILE |
7242 KAUTH_VNODE_ADD_SUBDIRECTORY |
7243 KAUTH_VNODE_DELETE_CHILD |
7244 KAUTH_VNODE_WRITE_ATTRIBUTES |
7245 KAUTH_VNODE_WRITE_EXTATTRIBUTES))
7246 posix_action |= VWRITE;
7247 if (eval.ae_residual & (KAUTH_VNODE_EXECUTE |
7248 KAUTH_VNODE_SEARCH))
7249 posix_action |= VEXEC;
7250
7251 if (posix_action != 0) {
7252 return(vnode_authorize_posix(vcp, posix_action, 0 /* !on_dir */));
7253 } else {
7254 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",
7255 vcp->vp,
7256 (eval.ae_residual & KAUTH_VNODE_READ_DATA)
7257 ? vnode_isdir(vcp->vp) ? " LIST_DIRECTORY" : " READ_DATA" : "",
7258 (eval.ae_residual & KAUTH_VNODE_WRITE_DATA)
7259 ? vnode_isdir(vcp->vp) ? " ADD_FILE" : " WRITE_DATA" : "",
7260 (eval.ae_residual & KAUTH_VNODE_EXECUTE)
7261 ? vnode_isdir(vcp->vp) ? " SEARCH" : " EXECUTE" : "",
7262 (eval.ae_residual & KAUTH_VNODE_DELETE)
7263 ? " DELETE" : "",
7264 (eval.ae_residual & KAUTH_VNODE_APPEND_DATA)
7265 ? vnode_isdir(vcp->vp) ? " ADD_SUBDIRECTORY" : " APPEND_DATA" : "",
7266 (eval.ae_residual & KAUTH_VNODE_DELETE_CHILD)
7267 ? " DELETE_CHILD" : "",
7268 (eval.ae_residual & KAUTH_VNODE_READ_ATTRIBUTES)
7269 ? " READ_ATTRIBUTES" : "",
7270 (eval.ae_residual & KAUTH_VNODE_WRITE_ATTRIBUTES)
7271 ? " WRITE_ATTRIBUTES" : "",
7272 (eval.ae_residual & KAUTH_VNODE_READ_EXTATTRIBUTES)
7273 ? " READ_EXTATTRIBUTES" : "",
7274 (eval.ae_residual & KAUTH_VNODE_WRITE_EXTATTRIBUTES)
7275 ? " WRITE_EXTATTRIBUTES" : "",
7276 (eval.ae_residual & KAUTH_VNODE_READ_SECURITY)
7277 ? " READ_SECURITY" : "",
7278 (eval.ae_residual & KAUTH_VNODE_WRITE_SECURITY)
7279 ? " WRITE_SECURITY" : "",
7280 (eval.ae_residual & KAUTH_VNODE_CHECKIMMUTABLE)
7281 ? " CHECKIMMUTABLE" : "",
7282 (eval.ae_residual & KAUTH_VNODE_CHANGE_OWNER)
7283 ? " CHANGE_OWNER" : "");
7284 }
7285
7286 /*
7287 * Lack of required Posix permissions implies no reason to deny access.
7288 */
7289 return(0);
7290 }
7291
7292 /*
7293 * Check for file immutability.
7294 */
7295 static int
7296 vnode_authorize_checkimmutable(mount_t mp, struct vnode_attr *vap, int rights, int ignore)
7297 {
7298 int error;
7299 int append;
7300
7301 /*
7302 * Perform immutability checks for operations that change data.
7303 *
7304 * Sockets, fifos and devices require special handling.
7305 */
7306 switch(vap->va_type) {
7307 case VSOCK:
7308 case VFIFO:
7309 case VBLK:
7310 case VCHR:
7311 /*
7312 * Writing to these nodes does not change the filesystem data,
7313 * so forget that it's being tried.
7314 */
7315 rights &= ~KAUTH_VNODE_WRITE_DATA;
7316 break;
7317 default:
7318 break;
7319 }
7320
7321 error = 0;
7322 if (rights & KAUTH_VNODE_WRITE_RIGHTS) {
7323
7324 /* check per-filesystem options if possible */
7325 if (mp != NULL) {
7326
7327 /* check for no-EA filesystems */
7328 if ((rights & KAUTH_VNODE_WRITE_EXTATTRIBUTES) &&
7329 (vfs_flags(mp) & MNT_NOUSERXATTR)) {
7330 KAUTH_DEBUG("%p DENIED - filesystem disallowed extended attributes", vp);
7331 error = EACCES; /* User attributes disabled */
7332 goto out;
7333 }
7334 }
7335
7336 /*
7337 * check for file immutability. first, check if the requested rights are
7338 * allowable for a UF_APPEND file.
7339 */
7340 append = 0;
7341 if (vap->va_type == VDIR) {
7342 if ((rights & (KAUTH_VNODE_ADD_FILE | KAUTH_VNODE_ADD_SUBDIRECTORY | KAUTH_VNODE_WRITE_EXTATTRIBUTES)) == rights)
7343 append = 1;
7344 } else {
7345 if ((rights & (KAUTH_VNODE_APPEND_DATA | KAUTH_VNODE_WRITE_EXTATTRIBUTES)) == rights)
7346 append = 1;
7347 }
7348 if ((error = vnode_immutable(vap, append, ignore)) != 0) {
7349 KAUTH_DEBUG("%p DENIED - file is immutable", vp);
7350 goto out;
7351 }
7352 }
7353 out:
7354 return(error);
7355 }
7356
7357 /*
7358 * Handle authorization actions for filesystems that advertise that the
7359 * server will be enforcing.
7360 *
7361 * Returns: 0 Authorization should be handled locally
7362 * 1 Authorization was handled by the FS
7363 *
7364 * Note: Imputed returns will only occur if the authorization request
7365 * was handled by the FS.
7366 *
7367 * Imputed: *resultp, modified Return code from FS when the request is
7368 * handled by the FS.
7369 * VNOP_ACCESS:???
7370 * VNOP_OPEN:???
7371 */
7372 static int
7373 vnode_authorize_opaque(vnode_t vp, int *resultp, kauth_action_t action, vfs_context_t ctx)
7374 {
7375 int error;
7376
7377 /*
7378 * If the vp is a device node, socket or FIFO it actually represents a local
7379 * endpoint, so we need to handle it locally.
7380 */
7381 switch(vp->v_type) {
7382 case VBLK:
7383 case VCHR:
7384 case VSOCK:
7385 case VFIFO:
7386 return(0);
7387 default:
7388 break;
7389 }
7390
7391 /*
7392 * In the advisory request case, if the filesystem doesn't think it's reliable
7393 * we will attempt to formulate a result ourselves based on VNOP_GETATTR data.
7394 */
7395 if ((action & KAUTH_VNODE_ACCESS) && !vfs_authopaqueaccess(vp->v_mount))
7396 return(0);
7397
7398 /*
7399 * Let the filesystem have a say in the matter. It's OK for it to not implemnent
7400 * VNOP_ACCESS, as most will authorise inline with the actual request.
7401 */
7402 if ((error = VNOP_ACCESS(vp, action, ctx)) != ENOTSUP) {
7403 *resultp = error;
7404 KAUTH_DEBUG("%p DENIED - opaque filesystem VNOP_ACCESS denied access", vp);
7405 return(1);
7406 }
7407
7408 /*
7409 * Typically opaque filesystems do authorisation in-line, but exec is a special case. In
7410 * order to be reasonably sure that exec will be permitted, we try a bit harder here.
7411 */
7412 if ((action & KAUTH_VNODE_EXECUTE) && (vp->v_type == VREG)) {
7413 /* try a VNOP_OPEN for readonly access */
7414 if ((error = VNOP_OPEN(vp, FREAD, ctx)) != 0) {
7415 *resultp = error;
7416 KAUTH_DEBUG("%p DENIED - EXECUTE denied because file could not be opened readonly", vp);
7417 return(1);
7418 }
7419 VNOP_CLOSE(vp, FREAD, ctx);
7420 }
7421
7422 /*
7423 * We don't have any reason to believe that the request has to be denied at this point,
7424 * so go ahead and allow it.
7425 */
7426 *resultp = 0;
7427 KAUTH_DEBUG("%p ALLOWED - bypassing access check for non-local filesystem", vp);
7428 return(1);
7429 }
7430
7431
7432
7433
7434 /*
7435 * Returns: KAUTH_RESULT_ALLOW
7436 * KAUTH_RESULT_DENY
7437 *
7438 * Imputed: *arg3, modified Error code in the deny case
7439 * EROFS Read-only file system
7440 * EACCES Permission denied
7441 * EPERM Operation not permitted [no execute]
7442 * vnode_getattr:ENOMEM Not enough space [only if has filesec]
7443 * vnode_getattr:???
7444 * vnode_authorize_opaque:*arg2 ???
7445 * vnode_authorize_checkimmutable:???
7446 * vnode_authorize_delete:???
7447 * vnode_authorize_simple:???
7448 */
7449
7450
7451 static int
7452 vnode_authorize_callback(__unused kauth_cred_t cred, __unused void *idata,
7453 kauth_action_t action, uintptr_t arg0, uintptr_t arg1, uintptr_t arg2,
7454 uintptr_t arg3)
7455 {
7456 vfs_context_t ctx;
7457 vnode_t cvp = NULLVP;
7458 vnode_t vp, dvp;
7459 int result = KAUTH_RESULT_DENY;
7460 int parent_iocount = 0;
7461 int parent_action; /* In case we need to use namedstream's data fork for cached rights*/
7462
7463 ctx = (vfs_context_t)arg0;
7464 vp = (vnode_t)arg1;
7465 dvp = (vnode_t)arg2;
7466
7467 /*
7468 * if there are 2 vnodes passed in, we don't know at
7469 * this point which rights to look at based on the
7470 * combined action being passed in... defer until later...
7471 * otherwise check the kauth 'rights' cache hung
7472 * off of the vnode we're interested in... if we've already
7473 * been granted the right we're currently interested in,
7474 * we can just return success... otherwise we'll go through
7475 * the process of authorizing the requested right(s)... if that
7476 * succeeds, we'll add the right(s) to the cache.
7477 * VNOP_SETATTR and VNOP_SETXATTR will invalidate this cache
7478 */
7479 if (dvp && vp)
7480 goto defer;
7481 if (dvp) {
7482 cvp = dvp;
7483 } else {
7484 /*
7485 * For named streams on local-authorization volumes, rights are cached on the parent;
7486 * authorization is determined by looking at the parent's properties anyway, so storing
7487 * on the parent means that we don't recompute for the named stream and that if
7488 * we need to flush rights (e.g. on VNOP_SETATTR()) we don't need to track down the
7489 * stream to flush its cache separately. If we miss in the cache, then we authorize
7490 * as if there were no cached rights (passing the named stream vnode and desired rights to
7491 * vnode_authorize_callback_int()).
7492 *
7493 * On an opaquely authorized volume, we don't know the relationship between the
7494 * data fork's properties and the rights granted on a stream. Thus, named stream vnodes
7495 * on such a volume are authorized directly (rather than using the parent) and have their
7496 * own caches. When a named stream vnode is created, we mark the parent as having a named
7497 * stream. On a VNOP_SETATTR() for the parent that may invalidate cached authorization, we
7498 * find the stream and flush its cache.
7499 */
7500 if (vnode_isnamedstream(vp) && (!vfs_authopaque(vp->v_mount))) {
7501 cvp = vnode_getparent(vp);
7502 if (cvp != NULLVP) {
7503 parent_iocount = 1;
7504 } else {
7505 cvp = NULL;
7506 goto defer; /* If we can't use the parent, take the slow path */
7507 }
7508
7509 /* Have to translate some actions */
7510 parent_action = action;
7511 if (parent_action & KAUTH_VNODE_READ_DATA) {
7512 parent_action &= ~KAUTH_VNODE_READ_DATA;
7513 parent_action |= KAUTH_VNODE_READ_EXTATTRIBUTES;
7514 }
7515 if (parent_action & KAUTH_VNODE_WRITE_DATA) {
7516 parent_action &= ~KAUTH_VNODE_WRITE_DATA;
7517 parent_action |= KAUTH_VNODE_WRITE_EXTATTRIBUTES;
7518 }
7519
7520 } else {
7521 cvp = vp;
7522 }
7523 }
7524
7525 if (vnode_cache_is_authorized(cvp, ctx, parent_iocount ? parent_action : action) == TRUE) {
7526 result = KAUTH_RESULT_ALLOW;
7527 goto out;
7528 }
7529 defer:
7530 result = vnode_authorize_callback_int(action, ctx, vp, dvp, (int *)arg3);
7531
7532 if (result == KAUTH_RESULT_ALLOW && cvp != NULLVP) {
7533 KAUTH_DEBUG("%p - caching action = %x", cvp, action);
7534 vnode_cache_authorized_action(cvp, ctx, action);
7535 }
7536
7537 out:
7538 if (parent_iocount) {
7539 vnode_put(cvp);
7540 }
7541
7542 return result;
7543 }
7544
7545 static int
7546 vnode_attr_authorize_internal(vauth_ctx vcp, mount_t mp,
7547 kauth_ace_rights_t rights, int is_suser, boolean_t *found_deny,
7548 int noimmutable, int parent_authorized_for_delete_child)
7549 {
7550 int result;
7551
7552 /*
7553 * Check for immutability.
7554 *
7555 * In the deletion case, parent directory immutability vetoes specific
7556 * file rights.
7557 */
7558 if ((result = vnode_authorize_checkimmutable(mp, vcp->vap, rights,
7559 noimmutable)) != 0)
7560 goto out;
7561
7562 if ((rights & KAUTH_VNODE_DELETE) &&
7563 !parent_authorized_for_delete_child) {
7564 result = vnode_authorize_checkimmutable(mp, vcp->dvap,
7565 KAUTH_VNODE_DELETE_CHILD, 0);
7566 if (result)
7567 goto out;
7568 }
7569
7570 /*
7571 * Clear rights that have been authorized by reaching this point, bail if nothing left to
7572 * check.
7573 */
7574 rights &= ~(KAUTH_VNODE_LINKTARGET | KAUTH_VNODE_CHECKIMMUTABLE);
7575 if (rights == 0)
7576 goto out;
7577
7578 /*
7579 * If we're not the superuser, authorize based on file properties;
7580 * note that even if parent_authorized_for_delete_child is TRUE, we
7581 * need to check on the node itself.
7582 */
7583 if (!is_suser) {
7584 /* process delete rights */
7585 if ((rights & KAUTH_VNODE_DELETE) &&
7586 ((result = vnode_authorize_delete(vcp, parent_authorized_for_delete_child)) != 0))
7587 goto out;
7588
7589 /* process remaining rights */
7590 if ((rights & ~KAUTH_VNODE_DELETE) &&
7591 (result = vnode_authorize_simple(vcp, rights, rights & KAUTH_VNODE_DELETE, found_deny)) != 0)
7592 goto out;
7593 } else {
7594 /*
7595 * Execute is only granted to root if one of the x bits is set. This check only
7596 * makes sense if the posix mode bits are actually supported.
7597 */
7598 if ((rights & KAUTH_VNODE_EXECUTE) &&
7599 (vcp->vap->va_type == VREG) &&
7600 VATTR_IS_SUPPORTED(vcp->vap, va_mode) &&
7601 !(vcp->vap->va_mode & (S_IXUSR | S_IXGRP | S_IXOTH))) {
7602 result = EPERM;
7603 KAUTH_DEBUG("%p DENIED - root execute requires at least one x bit in 0x%x", vp, va.va_mode);
7604 goto out;
7605 }
7606
7607 /* Assume that there were DENYs so we don't wrongly cache KAUTH_VNODE_SEARCHBYANYONE */
7608 *found_deny = TRUE;
7609
7610 KAUTH_DEBUG("%p ALLOWED - caller is superuser", vp);
7611 }
7612 out:
7613 return (result);
7614 }
7615
7616 static int
7617 vnode_authorize_callback_int(kauth_action_t action, vfs_context_t ctx,
7618 vnode_t vp, vnode_t dvp, int *errorp)
7619 {
7620 struct _vnode_authorize_context auth_context;
7621 vauth_ctx vcp;
7622 kauth_cred_t cred;
7623 kauth_ace_rights_t rights;
7624 struct vnode_attr va, dva;
7625 int result;
7626 int noimmutable;
7627 boolean_t parent_authorized_for_delete_child = FALSE;
7628 boolean_t found_deny = FALSE;
7629 boolean_t parent_ref= FALSE;
7630 boolean_t is_suser = FALSE;
7631
7632 vcp = &auth_context;
7633 vcp->ctx = ctx;
7634 vcp->vp = vp;
7635 vcp->dvp = dvp;
7636 /*
7637 * Note that we authorize against the context, not the passed cred
7638 * (the same thing anyway)
7639 */
7640 cred = ctx->vc_ucred;
7641
7642 VATTR_INIT(&va);
7643 vcp->vap = &va;
7644 VATTR_INIT(&dva);
7645 vcp->dvap = &dva;
7646
7647 vcp->flags = vcp->flags_valid = 0;
7648
7649 #if DIAGNOSTIC
7650 if ((ctx == NULL) || (vp == NULL) || (cred == NULL))
7651 panic("vnode_authorize: bad arguments (context %p vp %p cred %p)", ctx, vp, cred);
7652 #endif
7653
7654 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)",
7655 vp, vfs_context_proc(ctx)->p_comm,
7656 (action & KAUTH_VNODE_ACCESS) ? "access" : "auth",
7657 (action & KAUTH_VNODE_READ_DATA) ? vnode_isdir(vp) ? " LIST_DIRECTORY" : " READ_DATA" : "",
7658 (action & KAUTH_VNODE_WRITE_DATA) ? vnode_isdir(vp) ? " ADD_FILE" : " WRITE_DATA" : "",
7659 (action & KAUTH_VNODE_EXECUTE) ? vnode_isdir(vp) ? " SEARCH" : " EXECUTE" : "",
7660 (action & KAUTH_VNODE_DELETE) ? " DELETE" : "",
7661 (action & KAUTH_VNODE_APPEND_DATA) ? vnode_isdir(vp) ? " ADD_SUBDIRECTORY" : " APPEND_DATA" : "",
7662 (action & KAUTH_VNODE_DELETE_CHILD) ? " DELETE_CHILD" : "",
7663 (action & KAUTH_VNODE_READ_ATTRIBUTES) ? " READ_ATTRIBUTES" : "",
7664 (action & KAUTH_VNODE_WRITE_ATTRIBUTES) ? " WRITE_ATTRIBUTES" : "",
7665 (action & KAUTH_VNODE_READ_EXTATTRIBUTES) ? " READ_EXTATTRIBUTES" : "",
7666 (action & KAUTH_VNODE_WRITE_EXTATTRIBUTES) ? " WRITE_EXTATTRIBUTES" : "",
7667 (action & KAUTH_VNODE_READ_SECURITY) ? " READ_SECURITY" : "",
7668 (action & KAUTH_VNODE_WRITE_SECURITY) ? " WRITE_SECURITY" : "",
7669 (action & KAUTH_VNODE_CHANGE_OWNER) ? " CHANGE_OWNER" : "",
7670 (action & KAUTH_VNODE_NOIMMUTABLE) ? " (noimmutable)" : "",
7671 vnode_isdir(vp) ? "directory" : "file",
7672 vp->v_name ? vp->v_name : "<NULL>", action, vp, dvp);
7673
7674 /*
7675 * Extract the control bits from the action, everything else is
7676 * requested rights.
7677 */
7678 noimmutable = (action & KAUTH_VNODE_NOIMMUTABLE) ? 1 : 0;
7679 rights = action & ~(KAUTH_VNODE_ACCESS | KAUTH_VNODE_NOIMMUTABLE);
7680
7681 if (rights & KAUTH_VNODE_DELETE) {
7682 #if DIAGNOSTIC
7683 if (dvp == NULL)
7684 panic("vnode_authorize: KAUTH_VNODE_DELETE test requires a directory");
7685 #endif
7686 /*
7687 * check to see if we've already authorized the parent
7688 * directory for deletion of its children... if so, we
7689 * can skip a whole bunch of work... we will still have to
7690 * authorize that this specific child can be removed
7691 */
7692 if (vnode_cache_is_authorized(dvp, ctx, KAUTH_VNODE_DELETE_CHILD) == TRUE)
7693 parent_authorized_for_delete_child = TRUE;
7694 } else {
7695 vcp->dvp = NULLVP;
7696 vcp->dvap = NULL;
7697 }
7698
7699 /*
7700 * Check for read-only filesystems.
7701 */
7702 if ((rights & KAUTH_VNODE_WRITE_RIGHTS) &&
7703 (vp->v_mount->mnt_flag & MNT_RDONLY) &&
7704 ((vp->v_type == VREG) || (vp->v_type == VDIR) ||
7705 (vp->v_type == VLNK) || (vp->v_type == VCPLX) ||
7706 (rights & KAUTH_VNODE_DELETE) || (rights & KAUTH_VNODE_DELETE_CHILD))) {
7707 result = EROFS;
7708 goto out;
7709 }
7710
7711 /*
7712 * Check for noexec filesystems.
7713 */
7714 if ((rights & KAUTH_VNODE_EXECUTE) && (vp->v_type == VREG) && (vp->v_mount->mnt_flag & MNT_NOEXEC)) {
7715 result = EACCES;
7716 goto out;
7717 }
7718
7719 /*
7720 * Handle cases related to filesystems with non-local enforcement.
7721 * This call can return 0, in which case we will fall through to perform a
7722 * check based on VNOP_GETATTR data. Otherwise it returns 1 and sets
7723 * an appropriate result, at which point we can return immediately.
7724 */
7725 if ((vp->v_mount->mnt_kern_flag & MNTK_AUTH_OPAQUE) && vnode_authorize_opaque(vp, &result, action, ctx))
7726 goto out;
7727
7728 /*
7729 * If the vnode is a namedstream (extended attribute) data vnode (eg.
7730 * a resource fork), *_DATA becomes *_EXTATTRIBUTES.
7731 */
7732 if (vnode_isnamedstream(vp)) {
7733 if (rights & KAUTH_VNODE_READ_DATA) {
7734 rights &= ~KAUTH_VNODE_READ_DATA;
7735 rights |= KAUTH_VNODE_READ_EXTATTRIBUTES;
7736 }
7737 if (rights & KAUTH_VNODE_WRITE_DATA) {
7738 rights &= ~KAUTH_VNODE_WRITE_DATA;
7739 rights |= KAUTH_VNODE_WRITE_EXTATTRIBUTES;
7740 }
7741
7742 /*
7743 * Point 'vp' to the namedstream's parent for ACL checking
7744 */
7745 if ((vp->v_parent != NULL) &&
7746 (vget_internal(vp->v_parent, 0, VNODE_NODEAD | VNODE_DRAINO) == 0)) {
7747 parent_ref = TRUE;
7748 vcp->vp = vp = vp->v_parent;
7749 }
7750 }
7751
7752 if (vfs_context_issuser(ctx)) {
7753 /*
7754 * if we're not asking for execute permissions or modifications,
7755 * then we're done, this action is authorized.
7756 */
7757 if (!(rights & (KAUTH_VNODE_EXECUTE | KAUTH_VNODE_WRITE_RIGHTS)))
7758 goto success;
7759
7760 is_suser = TRUE;
7761 }
7762
7763 /*
7764 * Get vnode attributes and extended security information for the vnode
7765 * and directory if required.
7766 *
7767 * If we're root we only want mode bits and flags for checking
7768 * execute and immutability.
7769 */
7770 VATTR_WANTED(&va, va_mode);
7771 VATTR_WANTED(&va, va_flags);
7772 if (!is_suser) {
7773 VATTR_WANTED(&va, va_uid);
7774 VATTR_WANTED(&va, va_gid);
7775 VATTR_WANTED(&va, va_acl);
7776 }
7777 if ((result = vnode_getattr(vp, &va, ctx)) != 0) {
7778 KAUTH_DEBUG("%p ERROR - failed to get vnode attributes - %d", vp, result);
7779 goto out;
7780 }
7781 VATTR_WANTED(&va, va_type);
7782 VATTR_RETURN(&va, va_type, vnode_vtype(vp));
7783
7784 if (vcp->dvp) {
7785 VATTR_WANTED(&dva, va_mode);
7786 VATTR_WANTED(&dva, va_flags);
7787 if (!is_suser) {
7788 VATTR_WANTED(&dva, va_uid);
7789 VATTR_WANTED(&dva, va_gid);
7790 VATTR_WANTED(&dva, va_acl);
7791 }
7792 if ((result = vnode_getattr(vcp->dvp, &dva, ctx)) != 0) {
7793 KAUTH_DEBUG("%p ERROR - failed to get directory vnode attributes - %d", vp, result);
7794 goto out;
7795 }
7796 VATTR_WANTED(&dva, va_type);
7797 VATTR_RETURN(&dva, va_type, vnode_vtype(vcp->dvp));
7798 }
7799
7800 result = vnode_attr_authorize_internal(vcp, vp->v_mount, rights, is_suser,
7801 &found_deny, noimmutable, parent_authorized_for_delete_child);
7802 out:
7803 if (VATTR_IS_SUPPORTED(&va, va_acl) && (va.va_acl != NULL))
7804 kauth_acl_free(va.va_acl);
7805 if (VATTR_IS_SUPPORTED(&dva, va_acl) && (dva.va_acl != NULL))
7806 kauth_acl_free(dva.va_acl);
7807
7808 if (result) {
7809 if (parent_ref)
7810 vnode_put(vp);
7811 *errorp = result;
7812 KAUTH_DEBUG("%p DENIED - auth denied", vp);
7813 return(KAUTH_RESULT_DENY);
7814 }
7815 if ((rights & KAUTH_VNODE_SEARCH) && found_deny == FALSE && vp->v_type == VDIR) {
7816 /*
7817 * if we were successfully granted the right to search this directory
7818 * and there were NO ACL DENYs for search and the posix permissions also don't
7819 * deny execute, we can synthesize a global right that allows anyone to
7820 * traverse this directory during a pathname lookup without having to
7821 * match the credential associated with this cache of rights.
7822 *
7823 * Note that we can correctly cache KAUTH_VNODE_SEARCHBYANYONE
7824 * only if we actually check ACLs which we don't for root. As
7825 * a workaround, the lookup fast path checks for root.
7826 */
7827 if (!VATTR_IS_SUPPORTED(&va, va_mode) ||
7828 ((va.va_mode & (S_IXUSR | S_IXGRP | S_IXOTH)) ==
7829 (S_IXUSR | S_IXGRP | S_IXOTH))) {
7830 vnode_cache_authorized_action(vp, ctx, KAUTH_VNODE_SEARCHBYANYONE);
7831 }
7832 }
7833 success:
7834 if (parent_ref)
7835 vnode_put(vp);
7836
7837 /*
7838 * Note that this implies that we will allow requests for no rights, as well as
7839 * for rights that we do not recognise. There should be none of these.
7840 */
7841 KAUTH_DEBUG("%p ALLOWED - auth granted", vp);
7842 return(KAUTH_RESULT_ALLOW);
7843 }
7844
7845 int
7846 vnode_attr_authorize_init(struct vnode_attr *vap, struct vnode_attr *dvap,
7847 kauth_action_t action, vfs_context_t ctx)
7848 {
7849 VATTR_INIT(vap);
7850 VATTR_WANTED(vap, va_type);
7851 VATTR_WANTED(vap, va_mode);
7852 VATTR_WANTED(vap, va_flags);
7853 if (dvap) {
7854 VATTR_INIT(dvap);
7855 if (action & KAUTH_VNODE_DELETE) {
7856 VATTR_WANTED(dvap, va_type);
7857 VATTR_WANTED(dvap, va_mode);
7858 VATTR_WANTED(dvap, va_flags);
7859 }
7860 } else if (action & KAUTH_VNODE_DELETE) {
7861 return (EINVAL);
7862 }
7863
7864 if (!vfs_context_issuser(ctx)) {
7865 VATTR_WANTED(vap, va_uid);
7866 VATTR_WANTED(vap, va_gid);
7867 VATTR_WANTED(vap, va_acl);
7868 if (dvap && (action & KAUTH_VNODE_DELETE)) {
7869 VATTR_WANTED(dvap, va_uid);
7870 VATTR_WANTED(dvap, va_gid);
7871 VATTR_WANTED(dvap, va_acl);
7872 }
7873 }
7874
7875 return (0);
7876 }
7877
7878 int
7879 vnode_attr_authorize(struct vnode_attr *vap, struct vnode_attr *dvap, mount_t mp,
7880 kauth_action_t action, vfs_context_t ctx)
7881 {
7882 struct _vnode_authorize_context auth_context;
7883 vauth_ctx vcp;
7884 kauth_ace_rights_t rights;
7885 int noimmutable;
7886 boolean_t found_deny;
7887 boolean_t is_suser = FALSE;
7888 int result = 0;
7889
7890 vcp = &auth_context;
7891 vcp->ctx = ctx;
7892 vcp->vp = NULLVP;
7893 vcp->vap = vap;
7894 vcp->dvp = NULLVP;
7895 vcp->dvap = dvap;
7896 vcp->flags = vcp->flags_valid = 0;
7897
7898 noimmutable = (action & KAUTH_VNODE_NOIMMUTABLE) ? 1 : 0;
7899 rights = action & ~(KAUTH_VNODE_ACCESS | KAUTH_VNODE_NOIMMUTABLE);
7900
7901 /*
7902 * Check for read-only filesystems.
7903 */
7904 if ((rights & KAUTH_VNODE_WRITE_RIGHTS) &&
7905 mp && (mp->mnt_flag & MNT_RDONLY) &&
7906 ((vap->va_type == VREG) || (vap->va_type == VDIR) ||
7907 (vap->va_type == VLNK) || (rights & KAUTH_VNODE_DELETE) ||
7908 (rights & KAUTH_VNODE_DELETE_CHILD))) {
7909 result = EROFS;
7910 goto out;
7911 }
7912
7913 /*
7914 * Check for noexec filesystems.
7915 */
7916 if ((rights & KAUTH_VNODE_EXECUTE) &&
7917 (vap->va_type == VREG) && mp && (mp->mnt_flag & MNT_NOEXEC)) {
7918 result = EACCES;
7919 goto out;
7920 }
7921
7922 if (vfs_context_issuser(ctx)) {
7923 /*
7924 * if we're not asking for execute permissions or modifications,
7925 * then we're done, this action is authorized.
7926 */
7927 if (!(rights & (KAUTH_VNODE_EXECUTE | KAUTH_VNODE_WRITE_RIGHTS)))
7928 goto out;
7929 is_suser = TRUE;
7930 } else {
7931 if (!VATTR_IS_SUPPORTED(vap, va_uid) ||
7932 !VATTR_IS_SUPPORTED(vap, va_gid) ||
7933 (mp && vfs_extendedsecurity(mp) && !VATTR_IS_SUPPORTED(vap, va_acl))) {
7934 panic("vnode attrs not complete for vnode_attr_authorize\n");
7935 }
7936 }
7937
7938 result = vnode_attr_authorize_internal(vcp, mp, rights, is_suser,
7939 &found_deny, noimmutable, FALSE);
7940
7941 if (result == EPERM)
7942 result = EACCES;
7943 out:
7944 return (result);
7945 }
7946
7947
7948 int
7949 vnode_authattr_new(vnode_t dvp, struct vnode_attr *vap, int noauth, vfs_context_t ctx)
7950 {
7951 return vnode_authattr_new_internal(dvp, vap, noauth, NULL, ctx);
7952 }
7953
7954 /*
7955 * Check that the attribute information in vattr can be legally applied to
7956 * a new file by the context.
7957 */
7958 static int
7959 vnode_authattr_new_internal(vnode_t dvp, struct vnode_attr *vap, int noauth, uint32_t *defaulted_fieldsp, vfs_context_t ctx)
7960 {
7961 int error;
7962 int has_priv_suser, ismember, defaulted_owner, defaulted_group, defaulted_mode, inherit_restricted;
7963 kauth_cred_t cred;
7964 guid_t changer;
7965 mount_t dmp;
7966 struct vnode_attr dva;
7967
7968 error = 0;
7969
7970 if (defaulted_fieldsp) {
7971 *defaulted_fieldsp = 0;
7972 }
7973
7974 defaulted_owner = defaulted_group = defaulted_mode = 0;
7975
7976 inherit_restricted = 0;
7977
7978 /*
7979 * Require that the filesystem support extended security to apply any.
7980 */
7981 if (!vfs_extendedsecurity(dvp->v_mount) &&
7982 (VATTR_IS_ACTIVE(vap, va_acl) || VATTR_IS_ACTIVE(vap, va_uuuid) || VATTR_IS_ACTIVE(vap, va_guuid))) {
7983 error = EINVAL;
7984 goto out;
7985 }
7986
7987 /*
7988 * Default some fields.
7989 */
7990 dmp = dvp->v_mount;
7991
7992 /*
7993 * If the filesystem is mounted IGNORE_OWNERSHIP and an explicit owner is set, that
7994 * owner takes ownership of all new files.
7995 */
7996 if ((dmp->mnt_flag & MNT_IGNORE_OWNERSHIP) && (dmp->mnt_fsowner != KAUTH_UID_NONE)) {
7997 VATTR_SET(vap, va_uid, dmp->mnt_fsowner);
7998 defaulted_owner = 1;
7999 } else {
8000 if (!VATTR_IS_ACTIVE(vap, va_uid)) {
8001 /* default owner is current user */
8002 VATTR_SET(vap, va_uid, kauth_cred_getuid(vfs_context_ucred(ctx)));
8003 defaulted_owner = 1;
8004 }
8005 }
8006
8007 /*
8008 * We need the dvp's va_flags and *may* need the gid of the directory,
8009 * we ask for both here.
8010 */
8011 VATTR_INIT(&dva);
8012 VATTR_WANTED(&dva, va_gid);
8013 VATTR_WANTED(&dva, va_flags);
8014 if ((error = vnode_getattr(dvp, &dva, ctx)) != 0)
8015 goto out;
8016
8017 /*
8018 * If the filesystem is mounted IGNORE_OWNERSHIP and an explicit grouo is set, that
8019 * group takes ownership of all new files.
8020 */
8021 if ((dmp->mnt_flag & MNT_IGNORE_OWNERSHIP) && (dmp->mnt_fsgroup != KAUTH_GID_NONE)) {
8022 VATTR_SET(vap, va_gid, dmp->mnt_fsgroup);
8023 defaulted_group = 1;
8024 } else {
8025 if (!VATTR_IS_ACTIVE(vap, va_gid)) {
8026 /* default group comes from parent object, fallback to current user */
8027 if (VATTR_IS_SUPPORTED(&dva, va_gid)) {
8028 VATTR_SET(vap, va_gid, dva.va_gid);
8029 } else {
8030 VATTR_SET(vap, va_gid, kauth_cred_getgid(vfs_context_ucred(ctx)));
8031 }
8032 defaulted_group = 1;
8033 }
8034 }
8035
8036 if (!VATTR_IS_ACTIVE(vap, va_flags))
8037 VATTR_SET(vap, va_flags, 0);
8038
8039 /* Determine if SF_RESTRICTED should be inherited from the parent
8040 * directory. */
8041 if (VATTR_IS_SUPPORTED(&dva, va_flags) &&
8042 (dva.va_flags & SF_RESTRICTED)) {
8043 inherit_restricted = 1;
8044 }
8045
8046 /* default mode is everything, masked with current umask */
8047 if (!VATTR_IS_ACTIVE(vap, va_mode)) {
8048 VATTR_SET(vap, va_mode, ACCESSPERMS & ~vfs_context_proc(ctx)->p_fd->fd_cmask);
8049 KAUTH_DEBUG("ATTR - defaulting new file mode to %o from umask %o", vap->va_mode, vfs_context_proc(ctx)->p_fd->fd_cmask);
8050 defaulted_mode = 1;
8051 }
8052 /* set timestamps to now */
8053 if (!VATTR_IS_ACTIVE(vap, va_create_time)) {
8054 nanotime(&vap->va_create_time);
8055 VATTR_SET_ACTIVE(vap, va_create_time);
8056 }
8057
8058 /*
8059 * Check for attempts to set nonsensical fields.
8060 */
8061 if (vap->va_active & ~VNODE_ATTR_NEWOBJ) {
8062 error = EINVAL;
8063 KAUTH_DEBUG("ATTR - ERROR - attempt to set unsupported new-file attributes %llx",
8064 vap->va_active & ~VNODE_ATTR_NEWOBJ);
8065 goto out;
8066 }
8067
8068 /*
8069 * Quickly check for the applicability of any enforcement here.
8070 * Tests below maintain the integrity of the local security model.
8071 */
8072 if (vfs_authopaque(dvp->v_mount))
8073 goto out;
8074
8075 /*
8076 * We need to know if the caller is the superuser, or if the work is
8077 * otherwise already authorised.
8078 */
8079 cred = vfs_context_ucred(ctx);
8080 if (noauth) {
8081 /* doing work for the kernel */
8082 has_priv_suser = 1;
8083 } else {
8084 has_priv_suser = vfs_context_issuser(ctx);
8085 }
8086
8087
8088 if (VATTR_IS_ACTIVE(vap, va_flags)) {
8089 if (has_priv_suser) {
8090 if ((vap->va_flags & (UF_SETTABLE | SF_SETTABLE)) != vap->va_flags) {
8091 error = EPERM;
8092 KAUTH_DEBUG(" DENIED - superuser attempt to set illegal flag(s)");
8093 goto out;
8094 }
8095 } else {
8096 if ((vap->va_flags & UF_SETTABLE) != vap->va_flags) {
8097 error = EPERM;
8098 KAUTH_DEBUG(" DENIED - user attempt to set illegal flag(s)");
8099 goto out;
8100 }
8101 }
8102 }
8103
8104 /* if not superuser, validate legality of new-item attributes */
8105 if (!has_priv_suser) {
8106 if (!defaulted_mode && VATTR_IS_ACTIVE(vap, va_mode)) {
8107 /* setgid? */
8108 if (vap->va_mode & S_ISGID) {
8109 if ((error = kauth_cred_ismember_gid(cred, vap->va_gid, &ismember)) != 0) {
8110 KAUTH_DEBUG("ATTR - ERROR: got %d checking for membership in %d", error, vap->va_gid);
8111 goto out;
8112 }
8113 if (!ismember) {
8114 KAUTH_DEBUG(" DENIED - can't set SGID bit, not a member of %d", vap->va_gid);
8115 error = EPERM;
8116 goto out;
8117 }
8118 }
8119
8120 /* setuid? */
8121 if ((vap->va_mode & S_ISUID) && (vap->va_uid != kauth_cred_getuid(cred))) {
8122 KAUTH_DEBUG("ATTR - ERROR: illegal attempt to set the setuid bit");
8123 error = EPERM;
8124 goto out;
8125 }
8126 }
8127 if (!defaulted_owner && (vap->va_uid != kauth_cred_getuid(cred))) {
8128 KAUTH_DEBUG(" DENIED - cannot create new item owned by %d", vap->va_uid);
8129 error = EPERM;
8130 goto out;
8131 }
8132 if (!defaulted_group) {
8133 if ((error = kauth_cred_ismember_gid(cred, vap->va_gid, &ismember)) != 0) {
8134 KAUTH_DEBUG(" ERROR - got %d checking for membership in %d", error, vap->va_gid);
8135 goto out;
8136 }
8137 if (!ismember) {
8138 KAUTH_DEBUG(" DENIED - cannot create new item with group %d - not a member", vap->va_gid);
8139 error = EPERM;
8140 goto out;
8141 }
8142 }
8143
8144 /* initialising owner/group UUID */
8145 if (VATTR_IS_ACTIVE(vap, va_uuuid)) {
8146 if ((error = kauth_cred_getguid(cred, &changer)) != 0) {
8147 KAUTH_DEBUG(" ERROR - got %d trying to get caller UUID", error);
8148 /* XXX ENOENT here - no GUID - should perhaps become EPERM */
8149 goto out;
8150 }
8151 if (!kauth_guid_equal(&vap->va_uuuid, &changer)) {
8152 KAUTH_DEBUG(" ERROR - cannot create item with supplied owner UUID - not us");
8153 error = EPERM;
8154 goto out;
8155 }
8156 }
8157 if (VATTR_IS_ACTIVE(vap, va_guuid)) {
8158 if ((error = kauth_cred_ismember_guid(cred, &vap->va_guuid, &ismember)) != 0) {
8159 KAUTH_DEBUG(" ERROR - got %d trying to check group membership", error);
8160 goto out;
8161 }
8162 if (!ismember) {
8163 KAUTH_DEBUG(" ERROR - cannot create item with supplied group UUID - not a member");
8164 error = EPERM;
8165 goto out;
8166 }
8167 }
8168 }
8169 out:
8170 if (inherit_restricted) {
8171 /* Apply SF_RESTRICTED to the file if its parent directory was
8172 * restricted. This is done at the end so that root is not
8173 * required if this flag is only set due to inheritance. */
8174 VATTR_SET(vap, va_flags, (vap->va_flags | SF_RESTRICTED));
8175 }
8176 if (defaulted_fieldsp) {
8177 if (defaulted_mode) {
8178 *defaulted_fieldsp |= VATTR_PREPARE_DEFAULTED_MODE;
8179 }
8180 if (defaulted_group) {
8181 *defaulted_fieldsp |= VATTR_PREPARE_DEFAULTED_GID;
8182 }
8183 if (defaulted_owner) {
8184 *defaulted_fieldsp |= VATTR_PREPARE_DEFAULTED_UID;
8185 }
8186 }
8187 return(error);
8188 }
8189
8190 /*
8191 * Check that the attribute information in vap can be legally written by the
8192 * context.
8193 *
8194 * Call this when you're not sure about the vnode_attr; either its contents
8195 * have come from an unknown source, or when they are variable.
8196 *
8197 * Returns errno, or zero and sets *actionp to the KAUTH_VNODE_* actions that
8198 * must be authorized to be permitted to write the vattr.
8199 */
8200 int
8201 vnode_authattr(vnode_t vp, struct vnode_attr *vap, kauth_action_t *actionp, vfs_context_t ctx)
8202 {
8203 struct vnode_attr ova;
8204 kauth_action_t required_action;
8205 int error, has_priv_suser, ismember, chowner, chgroup, clear_suid, clear_sgid;
8206 guid_t changer;
8207 gid_t group;
8208 uid_t owner;
8209 mode_t newmode;
8210 kauth_cred_t cred;
8211 uint32_t fdelta;
8212
8213 VATTR_INIT(&ova);
8214 required_action = 0;
8215 error = 0;
8216
8217 /*
8218 * Quickly check for enforcement applicability.
8219 */
8220 if (vfs_authopaque(vp->v_mount))
8221 goto out;
8222
8223 /*
8224 * Check for attempts to set nonsensical fields.
8225 */
8226 if (vap->va_active & VNODE_ATTR_RDONLY) {
8227 KAUTH_DEBUG("ATTR - ERROR: attempt to set readonly attribute(s)");
8228 error = EINVAL;
8229 goto out;
8230 }
8231
8232 /*
8233 * We need to know if the caller is the superuser.
8234 */
8235 cred = vfs_context_ucred(ctx);
8236 has_priv_suser = kauth_cred_issuser(cred);
8237
8238 /*
8239 * If any of the following are changing, we need information from the old file:
8240 * va_uid
8241 * va_gid
8242 * va_mode
8243 * va_uuuid
8244 * va_guuid
8245 */
8246 if (VATTR_IS_ACTIVE(vap, va_uid) ||
8247 VATTR_IS_ACTIVE(vap, va_gid) ||
8248 VATTR_IS_ACTIVE(vap, va_mode) ||
8249 VATTR_IS_ACTIVE(vap, va_uuuid) ||
8250 VATTR_IS_ACTIVE(vap, va_guuid)) {
8251 VATTR_WANTED(&ova, va_mode);
8252 VATTR_WANTED(&ova, va_uid);
8253 VATTR_WANTED(&ova, va_gid);
8254 VATTR_WANTED(&ova, va_uuuid);
8255 VATTR_WANTED(&ova, va_guuid);
8256 KAUTH_DEBUG("ATTR - security information changing, fetching existing attributes");
8257 }
8258
8259 /*
8260 * If timestamps are being changed, we need to know who the file is owned
8261 * by.
8262 */
8263 if (VATTR_IS_ACTIVE(vap, va_create_time) ||
8264 VATTR_IS_ACTIVE(vap, va_change_time) ||
8265 VATTR_IS_ACTIVE(vap, va_modify_time) ||
8266 VATTR_IS_ACTIVE(vap, va_access_time) ||
8267 VATTR_IS_ACTIVE(vap, va_backup_time)) {
8268
8269 VATTR_WANTED(&ova, va_uid);
8270 #if 0 /* enable this when we support UUIDs as official owners */
8271 VATTR_WANTED(&ova, va_uuuid);
8272 #endif
8273 KAUTH_DEBUG("ATTR - timestamps changing, fetching uid and GUID");
8274 }
8275
8276 /*
8277 * If flags are being changed, we need the old flags.
8278 */
8279 if (VATTR_IS_ACTIVE(vap, va_flags)) {
8280 KAUTH_DEBUG("ATTR - flags changing, fetching old flags");
8281 VATTR_WANTED(&ova, va_flags);
8282 }
8283
8284 /*
8285 * If ACLs are being changed, we need the old ACLs.
8286 */
8287 if (VATTR_IS_ACTIVE(vap, va_acl)) {
8288 KAUTH_DEBUG("ATTR - acl changing, fetching old flags");
8289 VATTR_WANTED(&ova, va_acl);
8290 }
8291
8292 /*
8293 * If the size is being set, make sure it's not a directory.
8294 */
8295 if (VATTR_IS_ACTIVE(vap, va_data_size)) {
8296 /* size is only meaningful on regular files, don't permit otherwise */
8297 if (!vnode_isreg(vp)) {
8298 KAUTH_DEBUG("ATTR - ERROR: size change requested on non-file");
8299 error = vnode_isdir(vp) ? EISDIR : EINVAL;
8300 goto out;
8301 }
8302 }
8303
8304 /*
8305 * Get old data.
8306 */
8307 KAUTH_DEBUG("ATTR - fetching old attributes %016llx", ova.va_active);
8308 if ((error = vnode_getattr(vp, &ova, ctx)) != 0) {
8309 KAUTH_DEBUG(" ERROR - got %d trying to get attributes", error);
8310 goto out;
8311 }
8312
8313 /*
8314 * Size changes require write access to the file data.
8315 */
8316 if (VATTR_IS_ACTIVE(vap, va_data_size)) {
8317 /* if we can't get the size, or it's different, we need write access */
8318 KAUTH_DEBUG("ATTR - size change, requiring WRITE_DATA");
8319 required_action |= KAUTH_VNODE_WRITE_DATA;
8320 }
8321
8322 /*
8323 * Changing timestamps?
8324 *
8325 * Note that we are only called to authorize user-requested time changes;
8326 * side-effect time changes are not authorized. Authorisation is only
8327 * required for existing files.
8328 *
8329 * Non-owners are not permitted to change the time on an existing
8330 * file to anything other than the current time.
8331 */
8332 if (VATTR_IS_ACTIVE(vap, va_create_time) ||
8333 VATTR_IS_ACTIVE(vap, va_change_time) ||
8334 VATTR_IS_ACTIVE(vap, va_modify_time) ||
8335 VATTR_IS_ACTIVE(vap, va_access_time) ||
8336 VATTR_IS_ACTIVE(vap, va_backup_time)) {
8337 /*
8338 * The owner and root may set any timestamps they like,
8339 * provided that the file is not immutable. The owner still needs
8340 * WRITE_ATTRIBUTES (implied by ownership but still deniable).
8341 */
8342 if (has_priv_suser || vauth_node_owner(&ova, cred)) {
8343 KAUTH_DEBUG("ATTR - root or owner changing timestamps");
8344 required_action |= KAUTH_VNODE_CHECKIMMUTABLE | KAUTH_VNODE_WRITE_ATTRIBUTES;
8345 } else {
8346 /* just setting the current time? */
8347 if (vap->va_vaflags & VA_UTIMES_NULL) {
8348 KAUTH_DEBUG("ATTR - non-root/owner changing timestamps, requiring WRITE_ATTRIBUTES");
8349 required_action |= KAUTH_VNODE_WRITE_ATTRIBUTES;
8350 } else {
8351 KAUTH_DEBUG("ATTR - ERROR: illegal timestamp modification attempted");
8352 error = EACCES;
8353 goto out;
8354 }
8355 }
8356 }
8357
8358 /*
8359 * Changing file mode?
8360 */
8361 if (VATTR_IS_ACTIVE(vap, va_mode) && VATTR_IS_SUPPORTED(&ova, va_mode) && (ova.va_mode != vap->va_mode)) {
8362 KAUTH_DEBUG("ATTR - mode change from %06o to %06o", ova.va_mode, vap->va_mode);
8363
8364 /*
8365 * Mode changes always have the same basic auth requirements.
8366 */
8367 if (has_priv_suser) {
8368 KAUTH_DEBUG("ATTR - superuser mode change, requiring immutability check");
8369 required_action |= KAUTH_VNODE_CHECKIMMUTABLE;
8370 } else {
8371 /* need WRITE_SECURITY */
8372 KAUTH_DEBUG("ATTR - non-superuser mode change, requiring WRITE_SECURITY");
8373 required_action |= KAUTH_VNODE_WRITE_SECURITY;
8374 }
8375
8376 /*
8377 * Can't set the setgid bit if you're not in the group and not root. Have to have
8378 * existing group information in the case we're not setting it right now.
8379 */
8380 if (vap->va_mode & S_ISGID) {
8381 required_action |= KAUTH_VNODE_CHECKIMMUTABLE; /* always required */
8382 if (!has_priv_suser) {
8383 if (VATTR_IS_ACTIVE(vap, va_gid)) {
8384 group = vap->va_gid;
8385 } else if (VATTR_IS_SUPPORTED(&ova, va_gid)) {
8386 group = ova.va_gid;
8387 } else {
8388 KAUTH_DEBUG("ATTR - ERROR: setgid but no gid available");
8389 error = EINVAL;
8390 goto out;
8391 }
8392 /*
8393 * This might be too restrictive; WRITE_SECURITY might be implied by
8394 * membership in this case, rather than being an additional requirement.
8395 */
8396 if ((error = kauth_cred_ismember_gid(cred, group, &ismember)) != 0) {
8397 KAUTH_DEBUG("ATTR - ERROR: got %d checking for membership in %d", error, vap->va_gid);
8398 goto out;
8399 }
8400 if (!ismember) {
8401 KAUTH_DEBUG(" DENIED - can't set SGID bit, not a member of %d", group);
8402 error = EPERM;
8403 goto out;
8404 }
8405 }
8406 }
8407
8408 /*
8409 * Can't set the setuid bit unless you're root or the file's owner.
8410 */
8411 if (vap->va_mode & S_ISUID) {
8412 required_action |= KAUTH_VNODE_CHECKIMMUTABLE; /* always required */
8413 if (!has_priv_suser) {
8414 if (VATTR_IS_ACTIVE(vap, va_uid)) {
8415 owner = vap->va_uid;
8416 } else if (VATTR_IS_SUPPORTED(&ova, va_uid)) {
8417 owner = ova.va_uid;
8418 } else {
8419 KAUTH_DEBUG("ATTR - ERROR: setuid but no uid available");
8420 error = EINVAL;
8421 goto out;
8422 }
8423 if (owner != kauth_cred_getuid(cred)) {
8424 /*
8425 * We could allow this if WRITE_SECURITY is permitted, perhaps.
8426 */
8427 KAUTH_DEBUG("ATTR - ERROR: illegal attempt to set the setuid bit");
8428 error = EPERM;
8429 goto out;
8430 }
8431 }
8432 }
8433 }
8434
8435 /*
8436 * Validate/mask flags changes. This checks that only the flags in
8437 * the UF_SETTABLE mask are being set, and preserves the flags in
8438 * the SF_SETTABLE case.
8439 *
8440 * Since flags changes may be made in conjunction with other changes,
8441 * we will ask the auth code to ignore immutability in the case that
8442 * the SF_* flags are not set and we are only manipulating the file flags.
8443 *
8444 */
8445 if (VATTR_IS_ACTIVE(vap, va_flags)) {
8446 /* compute changing flags bits */
8447 if (VATTR_IS_SUPPORTED(&ova, va_flags)) {
8448 fdelta = vap->va_flags ^ ova.va_flags;
8449 } else {
8450 fdelta = vap->va_flags;
8451 }
8452
8453 if (fdelta != 0) {
8454 KAUTH_DEBUG("ATTR - flags changing, requiring WRITE_SECURITY");
8455 required_action |= KAUTH_VNODE_WRITE_SECURITY;
8456
8457 /* check that changing bits are legal */
8458 if (has_priv_suser) {
8459 /*
8460 * The immutability check will prevent us from clearing the SF_*
8461 * flags unless the system securelevel permits it, so just check
8462 * for legal flags here.
8463 */
8464 if (fdelta & ~(UF_SETTABLE | SF_SETTABLE)) {
8465 error = EPERM;
8466 KAUTH_DEBUG(" DENIED - superuser attempt to set illegal flag(s)");
8467 goto out;
8468 }
8469 } else {
8470 if (fdelta & ~UF_SETTABLE) {
8471 error = EPERM;
8472 KAUTH_DEBUG(" DENIED - user attempt to set illegal flag(s)");
8473 goto out;
8474 }
8475 }
8476 /*
8477 * If the caller has the ability to manipulate file flags,
8478 * security is not reduced by ignoring them for this operation.
8479 *
8480 * A more complete test here would consider the 'after' states of the flags
8481 * to determine whether it would permit the operation, but this becomes
8482 * very complex.
8483 *
8484 * Ignoring immutability is conditional on securelevel; this does not bypass
8485 * the SF_* flags if securelevel > 0.
8486 */
8487 required_action |= KAUTH_VNODE_NOIMMUTABLE;
8488 }
8489 }
8490
8491 /*
8492 * Validate ownership information.
8493 */
8494 chowner = 0;
8495 chgroup = 0;
8496 clear_suid = 0;
8497 clear_sgid = 0;
8498
8499 /*
8500 * uid changing
8501 * Note that if the filesystem didn't give us a UID, we expect that it doesn't
8502 * support them in general, and will ignore it if/when we try to set it.
8503 * We might want to clear the uid out of vap completely here.
8504 */
8505 if (VATTR_IS_ACTIVE(vap, va_uid)) {
8506 if (VATTR_IS_SUPPORTED(&ova, va_uid) && (vap->va_uid != ova.va_uid)) {
8507 if (!has_priv_suser && (kauth_cred_getuid(cred) != vap->va_uid)) {
8508 KAUTH_DEBUG(" DENIED - non-superuser cannot change ownershipt to a third party");
8509 error = EPERM;
8510 goto out;
8511 }
8512 chowner = 1;
8513 }
8514 clear_suid = 1;
8515 }
8516
8517 /*
8518 * gid changing
8519 * Note that if the filesystem didn't give us a GID, we expect that it doesn't
8520 * support them in general, and will ignore it if/when we try to set it.
8521 * We might want to clear the gid out of vap completely here.
8522 */
8523 if (VATTR_IS_ACTIVE(vap, va_gid)) {
8524 if (VATTR_IS_SUPPORTED(&ova, va_gid) && (vap->va_gid != ova.va_gid)) {
8525 if (!has_priv_suser) {
8526 if ((error = kauth_cred_ismember_gid(cred, vap->va_gid, &ismember)) != 0) {
8527 KAUTH_DEBUG(" ERROR - got %d checking for membership in %d", error, vap->va_gid);
8528 goto out;
8529 }
8530 if (!ismember) {
8531 KAUTH_DEBUG(" DENIED - group change from %d to %d but not a member of target group",
8532 ova.va_gid, vap->va_gid);
8533 error = EPERM;
8534 goto out;
8535 }
8536 }
8537 chgroup = 1;
8538 }
8539 clear_sgid = 1;
8540 }
8541
8542 /*
8543 * Owner UUID being set or changed.
8544 */
8545 if (VATTR_IS_ACTIVE(vap, va_uuuid)) {
8546 /* if the owner UUID is not actually changing ... */
8547 if (VATTR_IS_SUPPORTED(&ova, va_uuuid)) {
8548 if (kauth_guid_equal(&vap->va_uuuid, &ova.va_uuuid))
8549 goto no_uuuid_change;
8550
8551 /*
8552 * If the current owner UUID is a null GUID, check
8553 * it against the UUID corresponding to the owner UID.
8554 */
8555 if (kauth_guid_equal(&ova.va_uuuid, &kauth_null_guid) &&
8556 VATTR_IS_SUPPORTED(&ova, va_uid)) {
8557 guid_t uid_guid;
8558
8559 if (kauth_cred_uid2guid(ova.va_uid, &uid_guid) == 0 &&
8560 kauth_guid_equal(&vap->va_uuuid, &uid_guid))
8561 goto no_uuuid_change;
8562 }
8563 }
8564
8565 /*
8566 * The owner UUID cannot be set by a non-superuser to anything other than
8567 * their own or a null GUID (to "unset" the owner UUID).
8568 * Note that file systems must be prepared to handle the
8569 * null UUID case in a manner appropriate for that file
8570 * system.
8571 */
8572 if (!has_priv_suser) {
8573 if ((error = kauth_cred_getguid(cred, &changer)) != 0) {
8574 KAUTH_DEBUG(" ERROR - got %d trying to get caller UUID", error);
8575 /* XXX ENOENT here - no UUID - should perhaps become EPERM */
8576 goto out;
8577 }
8578 if (!kauth_guid_equal(&vap->va_uuuid, &changer) &&
8579 !kauth_guid_equal(&vap->va_uuuid, &kauth_null_guid)) {
8580 KAUTH_DEBUG(" ERROR - cannot set supplied owner UUID - not us / null");
8581 error = EPERM;
8582 goto out;
8583 }
8584 }
8585 chowner = 1;
8586 clear_suid = 1;
8587 }
8588 no_uuuid_change:
8589 /*
8590 * Group UUID being set or changed.
8591 */
8592 if (VATTR_IS_ACTIVE(vap, va_guuid)) {
8593 /* if the group UUID is not actually changing ... */
8594 if (VATTR_IS_SUPPORTED(&ova, va_guuid)) {
8595 if (kauth_guid_equal(&vap->va_guuid, &ova.va_guuid))
8596 goto no_guuid_change;
8597
8598 /*
8599 * If the current group UUID is a null UUID, check
8600 * it against the UUID corresponding to the group GID.
8601 */
8602 if (kauth_guid_equal(&ova.va_guuid, &kauth_null_guid) &&
8603 VATTR_IS_SUPPORTED(&ova, va_gid)) {
8604 guid_t gid_guid;
8605
8606 if (kauth_cred_gid2guid(ova.va_gid, &gid_guid) == 0 &&
8607 kauth_guid_equal(&vap->va_guuid, &gid_guid))
8608 goto no_guuid_change;
8609 }
8610 }
8611
8612 /*
8613 * The group UUID cannot be set by a non-superuser to anything other than
8614 * one of which they are a member or a null GUID (to "unset"
8615 * the group UUID).
8616 * Note that file systems must be prepared to handle the
8617 * null UUID case in a manner appropriate for that file
8618 * system.
8619 */
8620 if (!has_priv_suser) {
8621 if (kauth_guid_equal(&vap->va_guuid, &kauth_null_guid))
8622 ismember = 1;
8623 else if ((error = kauth_cred_ismember_guid(cred, &vap->va_guuid, &ismember)) != 0) {
8624 KAUTH_DEBUG(" ERROR - got %d trying to check group membership", error);
8625 goto out;
8626 }
8627 if (!ismember) {
8628 KAUTH_DEBUG(" ERROR - cannot set supplied group UUID - not a member / null");
8629 error = EPERM;
8630 goto out;
8631 }
8632 }
8633 chgroup = 1;
8634 }
8635 no_guuid_change:
8636
8637 /*
8638 * Compute authorisation for group/ownership changes.
8639 */
8640 if (chowner || chgroup || clear_suid || clear_sgid) {
8641 if (has_priv_suser) {
8642 KAUTH_DEBUG("ATTR - superuser changing file owner/group, requiring immutability check");
8643 required_action |= KAUTH_VNODE_CHECKIMMUTABLE;
8644 } else {
8645 if (chowner) {
8646 KAUTH_DEBUG("ATTR - ownership change, requiring TAKE_OWNERSHIP");
8647 required_action |= KAUTH_VNODE_TAKE_OWNERSHIP;
8648 }
8649 if (chgroup && !chowner) {
8650 KAUTH_DEBUG("ATTR - group change, requiring WRITE_SECURITY");
8651 required_action |= KAUTH_VNODE_WRITE_SECURITY;
8652 }
8653
8654 }
8655
8656 /*
8657 * clear set-uid and set-gid bits. POSIX only requires this for
8658 * non-privileged processes but we do it even for root.
8659 */
8660 if (VATTR_IS_ACTIVE(vap, va_mode)) {
8661 newmode = vap->va_mode;
8662 } else if (VATTR_IS_SUPPORTED(&ova, va_mode)) {
8663 newmode = ova.va_mode;
8664 } else {
8665 KAUTH_DEBUG("CHOWN - trying to change owner but cannot get mode from filesystem to mask setugid bits");
8666 newmode = 0;
8667 }
8668
8669 /* chown always clears setuid/gid bits. An exception is made for
8670 * setattrlist executed by a root process to set <uid, gid, mode> on a file:
8671 * setattrlist is allowed to set the new mode on the file and change (chown)
8672 * uid/gid.
8673 */
8674 if (newmode & (S_ISUID | S_ISGID)) {
8675 if (!VATTR_IS_ACTIVE(vap, va_mode) || !has_priv_suser) {
8676 KAUTH_DEBUG("CHOWN - masking setugid bits from mode %o to %o",
8677 newmode, newmode & ~(S_ISUID | S_ISGID));
8678 newmode &= ~(S_ISUID | S_ISGID);
8679 }
8680 VATTR_SET(vap, va_mode, newmode);
8681 }
8682 }
8683
8684 /*
8685 * Authorise changes in the ACL.
8686 */
8687 if (VATTR_IS_ACTIVE(vap, va_acl)) {
8688
8689 /* no existing ACL */
8690 if (!VATTR_IS_ACTIVE(&ova, va_acl) || (ova.va_acl == NULL)) {
8691
8692 /* adding an ACL */
8693 if (vap->va_acl != NULL) {
8694 required_action |= KAUTH_VNODE_WRITE_SECURITY;
8695 KAUTH_DEBUG("CHMOD - adding ACL");
8696 }
8697
8698 /* removing an existing ACL */
8699 } else if (vap->va_acl == NULL) {
8700 required_action |= KAUTH_VNODE_WRITE_SECURITY;
8701 KAUTH_DEBUG("CHMOD - removing ACL");
8702
8703 /* updating an existing ACL */
8704 } else {
8705 if (vap->va_acl->acl_entrycount != ova.va_acl->acl_entrycount) {
8706 /* entry count changed, must be different */
8707 required_action |= KAUTH_VNODE_WRITE_SECURITY;
8708 KAUTH_DEBUG("CHMOD - adding/removing ACL entries");
8709 } else if (vap->va_acl->acl_entrycount > 0) {
8710 /* both ACLs have the same ACE count, said count is 1 or more, bitwise compare ACLs */
8711 if (memcmp(&vap->va_acl->acl_ace[0], &ova.va_acl->acl_ace[0],
8712 sizeof(struct kauth_ace) * vap->va_acl->acl_entrycount)) {
8713 required_action |= KAUTH_VNODE_WRITE_SECURITY;
8714 KAUTH_DEBUG("CHMOD - changing ACL entries");
8715 }
8716 }
8717 }
8718 }
8719
8720 /*
8721 * Other attributes that require authorisation.
8722 */
8723 if (VATTR_IS_ACTIVE(vap, va_encoding))
8724 required_action |= KAUTH_VNODE_WRITE_ATTRIBUTES;
8725
8726 out:
8727 if (VATTR_IS_SUPPORTED(&ova, va_acl) && (ova.va_acl != NULL))
8728 kauth_acl_free(ova.va_acl);
8729 if (error == 0)
8730 *actionp = required_action;
8731 return(error);
8732 }
8733
8734 static int
8735 setlocklocal_callback(struct vnode *vp, __unused void *cargs)
8736 {
8737 vnode_lock_spin(vp);
8738 vp->v_flag |= VLOCKLOCAL;
8739 vnode_unlock(vp);
8740
8741 return (VNODE_RETURNED);
8742 }
8743
8744 void
8745 vfs_setlocklocal(mount_t mp)
8746 {
8747 mount_lock_spin(mp);
8748 mp->mnt_kern_flag |= MNTK_LOCK_LOCAL;
8749 mount_unlock(mp);
8750
8751 /*
8752 * The number of active vnodes is expected to be
8753 * very small when vfs_setlocklocal is invoked.
8754 */
8755 vnode_iterate(mp, 0, setlocklocal_callback, NULL);
8756 }
8757
8758 void
8759 vfs_setcompoundopen(mount_t mp)
8760 {
8761 mount_lock_spin(mp);
8762 mp->mnt_compound_ops |= COMPOUND_VNOP_OPEN;
8763 mount_unlock(mp);
8764 }
8765
8766
8767 void
8768 vnode_setswapmount(vnode_t vp)
8769 {
8770 mount_lock(vp->v_mount);
8771 vp->v_mount->mnt_kern_flag |= MNTK_SWAP_MOUNT;
8772 mount_unlock(vp->v_mount);
8773 }
8774
8775
8776 int64_t
8777 vnode_getswappin_avail(vnode_t vp)
8778 {
8779 int64_t max_swappin_avail = 0;
8780
8781 mount_lock(vp->v_mount);
8782 if (vp->v_mount->mnt_ioflags & MNT_IOFLAGS_SWAPPIN_SUPPORTED)
8783 max_swappin_avail = vp->v_mount->mnt_max_swappin_available;
8784 mount_unlock(vp->v_mount);
8785
8786 return (max_swappin_avail);
8787 }
8788
8789
8790 void
8791 vn_setunionwait(vnode_t vp)
8792 {
8793 vnode_lock_spin(vp);
8794 vp->v_flag |= VISUNION;
8795 vnode_unlock(vp);
8796 }
8797
8798
8799 void
8800 vn_checkunionwait(vnode_t vp)
8801 {
8802 vnode_lock_spin(vp);
8803 while ((vp->v_flag & VISUNION) == VISUNION)
8804 msleep((caddr_t)&vp->v_flag, &vp->v_lock, 0, 0, 0);
8805 vnode_unlock(vp);
8806 }
8807
8808 void
8809 vn_clearunionwait(vnode_t vp, int locked)
8810 {
8811 if (!locked)
8812 vnode_lock_spin(vp);
8813 if((vp->v_flag & VISUNION) == VISUNION) {
8814 vp->v_flag &= ~VISUNION;
8815 wakeup((caddr_t)&vp->v_flag);
8816 }
8817 if (!locked)
8818 vnode_unlock(vp);
8819 }
8820
8821 /*
8822 * Removes orphaned apple double files during a rmdir
8823 * Works by:
8824 * 1. vnode_suspend().
8825 * 2. Call VNOP_READDIR() till the end of directory is reached.
8826 * 3. Check if the directory entries returned are regular files with name starting with "._". If not, return ENOTEMPTY.
8827 * 4. Continue (2) and (3) till end of directory is reached.
8828 * 5. If all the entries in the directory were files with "._" name, delete all the files.
8829 * 6. vnode_resume()
8830 * 7. If deletion of all files succeeded, call VNOP_RMDIR() again.
8831 */
8832
8833 errno_t rmdir_remove_orphaned_appleDouble(vnode_t vp , vfs_context_t ctx, int * restart_flag)
8834 {
8835
8836 #define UIO_BUFF_SIZE 2048
8837 uio_t auio = NULL;
8838 int eofflag, siz = UIO_BUFF_SIZE, nentries = 0;
8839 int open_flag = 0, full_erase_flag = 0;
8840 char uio_buf[ UIO_SIZEOF(1) ];
8841 char *rbuf = NULL;
8842 void *dir_pos;
8843 void *dir_end;
8844 struct dirent *dp;
8845 errno_t error;
8846
8847 error = vnode_suspend(vp);
8848
8849 /*
8850 * restart_flag is set so that the calling rmdir sleeps and resets
8851 */
8852 if (error == EBUSY)
8853 *restart_flag = 1;
8854 if (error != 0)
8855 return (error);
8856
8857 /*
8858 * set up UIO
8859 */
8860 MALLOC(rbuf, caddr_t, siz, M_TEMP, M_WAITOK);
8861 if (rbuf)
8862 auio = uio_createwithbuffer(1, 0, UIO_SYSSPACE, UIO_READ,
8863 &uio_buf[0], sizeof(uio_buf));
8864 if (!rbuf || !auio) {
8865 error = ENOMEM;
8866 goto outsc;
8867 }
8868
8869 uio_setoffset(auio,0);
8870
8871 eofflag = 0;
8872
8873 if ((error = VNOP_OPEN(vp, FREAD, ctx)))
8874 goto outsc;
8875 else
8876 open_flag = 1;
8877
8878 /*
8879 * First pass checks if all files are appleDouble files.
8880 */
8881
8882 do {
8883 siz = UIO_BUFF_SIZE;
8884 uio_reset(auio, uio_offset(auio), UIO_SYSSPACE, UIO_READ);
8885 uio_addiov(auio, CAST_USER_ADDR_T(rbuf), UIO_BUFF_SIZE);
8886
8887 if((error = VNOP_READDIR(vp, auio, 0, &eofflag, &nentries, ctx)))
8888 goto outsc;
8889
8890 if (uio_resid(auio) != 0)
8891 siz -= uio_resid(auio);
8892
8893 /*
8894 * Iterate through directory
8895 */
8896 dir_pos = (void*) rbuf;
8897 dir_end = (void*) (rbuf + siz);
8898 dp = (struct dirent*) (dir_pos);
8899
8900 if (dir_pos == dir_end)
8901 eofflag = 1;
8902
8903 while (dir_pos < dir_end) {
8904 /*
8905 * Check for . and .. as well as directories
8906 */
8907 if (dp->d_ino != 0 &&
8908 !((dp->d_namlen == 1 && dp->d_name[0] == '.') ||
8909 (dp->d_namlen == 2 && dp->d_name[0] == '.' && dp->d_name[1] == '.'))) {
8910 /*
8911 * Check for irregular files and ._ files
8912 * If there is a ._._ file abort the op
8913 */
8914 if ( dp->d_namlen < 2 ||
8915 strncmp(dp->d_name,"._",2) ||
8916 (dp->d_namlen >= 4 && !strncmp(&(dp->d_name[2]), "._",2))) {
8917 error = ENOTEMPTY;
8918 goto outsc;
8919 }
8920 }
8921 dir_pos = (void*) ((uint8_t*)dir_pos + dp->d_reclen);
8922 dp = (struct dirent*)dir_pos;
8923 }
8924
8925 /*
8926 * workaround for HFS/NFS setting eofflag before end of file
8927 */
8928 if (vp->v_tag == VT_HFS && nentries > 2)
8929 eofflag=0;
8930
8931 if (vp->v_tag == VT_NFS) {
8932 if (eofflag && !full_erase_flag) {
8933 full_erase_flag = 1;
8934 eofflag = 0;
8935 uio_reset(auio, 0, UIO_SYSSPACE, UIO_READ);
8936 }
8937 else if (!eofflag && full_erase_flag)
8938 full_erase_flag = 0;
8939 }
8940
8941 } while (!eofflag);
8942 /*
8943 * If we've made it here all the files in the dir are ._ files.
8944 * We can delete the files even though the node is suspended
8945 * because we are the owner of the file.
8946 */
8947
8948 uio_reset(auio, 0, UIO_SYSSPACE, UIO_READ);
8949 eofflag = 0;
8950 full_erase_flag = 0;
8951
8952 do {
8953 siz = UIO_BUFF_SIZE;
8954 uio_reset(auio, uio_offset(auio), UIO_SYSSPACE, UIO_READ);
8955 uio_addiov(auio, CAST_USER_ADDR_T(rbuf), UIO_BUFF_SIZE);
8956
8957 error = VNOP_READDIR(vp, auio, 0, &eofflag, &nentries, ctx);
8958
8959 if (error != 0)
8960 goto outsc;
8961
8962 if (uio_resid(auio) != 0)
8963 siz -= uio_resid(auio);
8964
8965 /*
8966 * Iterate through directory
8967 */
8968 dir_pos = (void*) rbuf;
8969 dir_end = (void*) (rbuf + siz);
8970 dp = (struct dirent*) dir_pos;
8971
8972 if (dir_pos == dir_end)
8973 eofflag = 1;
8974
8975 while (dir_pos < dir_end) {
8976 /*
8977 * Check for . and .. as well as directories
8978 */
8979 if (dp->d_ino != 0 &&
8980 !((dp->d_namlen == 1 && dp->d_name[0] == '.') ||
8981 (dp->d_namlen == 2 && dp->d_name[0] == '.' && dp->d_name[1] == '.'))
8982 ) {
8983
8984 error = unlink1(ctx, vp,
8985 CAST_USER_ADDR_T(dp->d_name), UIO_SYSSPACE,
8986 VNODE_REMOVE_SKIP_NAMESPACE_EVENT |
8987 VNODE_REMOVE_NO_AUDIT_PATH);
8988
8989 if (error && error != ENOENT) {
8990 goto outsc;
8991 }
8992
8993 }
8994 dir_pos = (void*) ((uint8_t*)dir_pos + dp->d_reclen);
8995 dp = (struct dirent*)dir_pos;
8996 }
8997
8998 /*
8999 * workaround for HFS/NFS setting eofflag before end of file
9000 */
9001 if (vp->v_tag == VT_HFS && nentries > 2)
9002 eofflag=0;
9003
9004 if (vp->v_tag == VT_NFS) {
9005 if (eofflag && !full_erase_flag) {
9006 full_erase_flag = 1;
9007 eofflag = 0;
9008 uio_reset(auio, 0, UIO_SYSSPACE, UIO_READ);
9009 }
9010 else if (!eofflag && full_erase_flag)
9011 full_erase_flag = 0;
9012 }
9013
9014 } while (!eofflag);
9015
9016
9017 error = 0;
9018
9019 outsc:
9020 if (open_flag)
9021 VNOP_CLOSE(vp, FREAD, ctx);
9022
9023 if (auio)
9024 uio_free(auio);
9025 FREE(rbuf, M_TEMP);
9026
9027 vnode_resume(vp);
9028
9029
9030 return(error);
9031
9032 }
9033
9034
9035 void
9036 lock_vnode_and_post(vnode_t vp, int kevent_num)
9037 {
9038 /* Only take the lock if there's something there! */
9039 if (vp->v_knotes.slh_first != NULL) {
9040 vnode_lock(vp);
9041 KNOTE(&vp->v_knotes, kevent_num);
9042 vnode_unlock(vp);
9043 }
9044 }
9045
9046 void panic_print_vnodes(void);
9047
9048 /* define PANIC_PRINTS_VNODES only if investigation is required. */
9049 #ifdef PANIC_PRINTS_VNODES
9050
9051 static const char *__vtype(uint16_t vtype)
9052 {
9053 switch (vtype) {
9054 case VREG:
9055 return "R";
9056 case VDIR:
9057 return "D";
9058 case VBLK:
9059 return "B";
9060 case VCHR:
9061 return "C";
9062 case VLNK:
9063 return "L";
9064 case VSOCK:
9065 return "S";
9066 case VFIFO:
9067 return "F";
9068 case VBAD:
9069 return "x";
9070 case VSTR:
9071 return "T";
9072 case VCPLX:
9073 return "X";
9074 default:
9075 return "?";
9076 }
9077 }
9078
9079 /*
9080 * build a path from the bottom up
9081 * NOTE: called from the panic path - no alloc'ing of memory and no locks!
9082 */
9083 static char *__vpath(vnode_t vp, char *str, int len, int depth)
9084 {
9085 int vnm_len;
9086 const char *src;
9087 char *dst;
9088
9089 if (len <= 0)
9090 return str;
9091 /* str + len is the start of the string we created */
9092 if (!vp->v_name)
9093 return str + len;
9094
9095 /* follow mount vnodes to get the full path */
9096 if ((vp->v_flag & VROOT)) {
9097 if (vp->v_mount != NULL && vp->v_mount->mnt_vnodecovered) {
9098 return __vpath(vp->v_mount->mnt_vnodecovered,
9099 str, len, depth+1);
9100 }
9101 return str + len;
9102 }
9103
9104 src = vp->v_name;
9105 vnm_len = strlen(src);
9106 if (vnm_len > len) {
9107 /* truncate the name to fit in the string */
9108 src += (vnm_len - len);
9109 vnm_len = len;
9110 }
9111
9112 /* start from the back and copy just characters (no NULLs) */
9113
9114 /* this will chop off leaf path (file) names */
9115 if (depth > 0) {
9116 dst = str + len - vnm_len;
9117 memcpy(dst, src, vnm_len);
9118 len -= vnm_len;
9119 } else {
9120 dst = str + len;
9121 }
9122
9123 if (vp->v_parent && len > 1) {
9124 /* follow parents up the chain */
9125 len--;
9126 *(dst-1) = '/';
9127 return __vpath(vp->v_parent, str, len, depth + 1);
9128 }
9129
9130 return dst;
9131 }
9132
9133 extern int kdb_printf(const char *format, ...) __printflike(1,2);
9134
9135 #define SANE_VNODE_PRINT_LIMIT 5000
9136 void panic_print_vnodes(void)
9137 {
9138 mount_t mnt;
9139 vnode_t vp;
9140 int nvnodes = 0;
9141 const char *type;
9142 char *nm;
9143 char vname[257];
9144
9145 kdb_printf("\n***** VNODES *****\n"
9146 "TYPE UREF ICNT PATH\n");
9147
9148 /* NULL-terminate the path name */
9149 vname[sizeof(vname)-1] = '\0';
9150
9151 /*
9152 * iterate all vnodelist items in all mounts (mntlist) -> mnt_vnodelist
9153 */
9154 TAILQ_FOREACH(mnt, &mountlist, mnt_list) {
9155
9156 if (!ml_validate_nofault((vm_offset_t)mnt, sizeof(mount_t))) {
9157 kdb_printf("Unable to iterate the mount list %p - encountered an invalid mount pointer %p \n",
9158 &mountlist, mnt);
9159 break;
9160 }
9161
9162 TAILQ_FOREACH(vp, &mnt->mnt_vnodelist, v_mntvnodes) {
9163
9164 if (!ml_validate_nofault((vm_offset_t)vp, sizeof(vnode_t))) {
9165 kdb_printf("Unable to iterate the vnode list %p - encountered an invalid vnode pointer %p \n",
9166 &mnt->mnt_vnodelist, vp);
9167 break;
9168 }
9169
9170 if (++nvnodes > SANE_VNODE_PRINT_LIMIT)
9171 return;
9172 type = __vtype(vp->v_type);
9173 nm = __vpath(vp, vname, sizeof(vname)-1, 0);
9174 kdb_printf("%s %0d %0d %s\n",
9175 type, vp->v_usecount, vp->v_iocount, nm);
9176 }
9177 }
9178 }
9179
9180 #else /* !PANIC_PRINTS_VNODES */
9181 void panic_print_vnodes(void)
9182 {
9183 return;
9184 }
9185 #endif
9186
9187
9188 #ifdef JOE_DEBUG
9189 static void record_vp(vnode_t vp, int count) {
9190 struct uthread *ut;
9191
9192 #if CONFIG_TRIGGERS
9193 if (vp->v_resolve)
9194 return;
9195 #endif
9196 if ((vp->v_flag & VSYSTEM))
9197 return;
9198
9199 ut = get_bsdthread_info(current_thread());
9200 ut->uu_iocount += count;
9201
9202 if (count == 1) {
9203 if (ut->uu_vpindex < 32) {
9204 OSBacktrace((void **)&ut->uu_pcs[ut->uu_vpindex][0], 10);
9205
9206 ut->uu_vps[ut->uu_vpindex] = vp;
9207 ut->uu_vpindex++;
9208 }
9209 }
9210 }
9211 #endif
9212
9213
9214 #if CONFIG_TRIGGERS
9215
9216 #define TRIG_DEBUG 0
9217
9218 #if TRIG_DEBUG
9219 #define TRIG_LOG(...) do { printf("%s: ", __FUNCTION__); printf(__VA_ARGS__); } while (0)
9220 #else
9221 #define TRIG_LOG(...)
9222 #endif
9223
9224 /*
9225 * Resolver result functions
9226 */
9227
9228 resolver_result_t
9229 vfs_resolver_result(uint32_t seq, enum resolver_status stat, int aux)
9230 {
9231 /*
9232 * |<--- 32 --->|<--- 28 --->|<- 4 ->|
9233 * sequence auxiliary status
9234 */
9235 return (((uint64_t)seq) << 32) |
9236 (((uint64_t)(aux & 0x0fffffff)) << 4) |
9237 (uint64_t)(stat & 0x0000000F);
9238 }
9239
9240 enum resolver_status
9241 vfs_resolver_status(resolver_result_t result)
9242 {
9243 /* lower 4 bits is status */
9244 return (result & 0x0000000F);
9245 }
9246
9247 uint32_t
9248 vfs_resolver_sequence(resolver_result_t result)
9249 {
9250 /* upper 32 bits is sequence */
9251 return (uint32_t)(result >> 32);
9252 }
9253
9254 int
9255 vfs_resolver_auxiliary(resolver_result_t result)
9256 {
9257 /* 28 bits of auxiliary */
9258 return (int)(((uint32_t)(result & 0xFFFFFFF0)) >> 4);
9259 }
9260
9261 /*
9262 * SPI
9263 * Call in for resolvers to update vnode trigger state
9264 */
9265 int
9266 vnode_trigger_update(vnode_t vp, resolver_result_t result)
9267 {
9268 vnode_resolve_t rp;
9269 uint32_t seq;
9270 enum resolver_status stat;
9271
9272 if (vp->v_resolve == NULL) {
9273 return (EINVAL);
9274 }
9275
9276 stat = vfs_resolver_status(result);
9277 seq = vfs_resolver_sequence(result);
9278
9279 if ((stat != RESOLVER_RESOLVED) && (stat != RESOLVER_UNRESOLVED)) {
9280 return (EINVAL);
9281 }
9282
9283 rp = vp->v_resolve;
9284 lck_mtx_lock(&rp->vr_lock);
9285
9286 if (seq > rp->vr_lastseq) {
9287 if (stat == RESOLVER_RESOLVED)
9288 rp->vr_flags |= VNT_RESOLVED;
9289 else
9290 rp->vr_flags &= ~VNT_RESOLVED;
9291
9292 rp->vr_lastseq = seq;
9293 }
9294
9295 lck_mtx_unlock(&rp->vr_lock);
9296
9297 return (0);
9298 }
9299
9300 static int
9301 vnode_resolver_attach(vnode_t vp, vnode_resolve_t rp, boolean_t ref)
9302 {
9303 int error;
9304
9305 vnode_lock_spin(vp);
9306 if (vp->v_resolve != NULL) {
9307 vnode_unlock(vp);
9308 return EINVAL;
9309 } else {
9310 vp->v_resolve = rp;
9311 }
9312 vnode_unlock(vp);
9313
9314 if (ref) {
9315 error = vnode_ref_ext(vp, O_EVTONLY, VNODE_REF_FORCE);
9316 if (error != 0) {
9317 panic("VNODE_REF_FORCE didn't help...");
9318 }
9319 }
9320
9321 return 0;
9322 }
9323
9324 /*
9325 * VFS internal interfaces for vnode triggers
9326 *
9327 * vnode must already have an io count on entry
9328 * v_resolve is stable when io count is non-zero
9329 */
9330 static int
9331 vnode_resolver_create(mount_t mp, vnode_t vp, struct vnode_trigger_param *tinfo, boolean_t external)
9332 {
9333 vnode_resolve_t rp;
9334 int result;
9335 char byte;
9336
9337 #if 1
9338 /* minimum pointer test (debugging) */
9339 if (tinfo->vnt_data)
9340 byte = *((char *)tinfo->vnt_data);
9341 #endif
9342 MALLOC(rp, vnode_resolve_t, sizeof(*rp), M_TEMP, M_WAITOK);
9343 if (rp == NULL)
9344 return (ENOMEM);
9345
9346 lck_mtx_init(&rp->vr_lock, trigger_vnode_lck_grp, trigger_vnode_lck_attr);
9347
9348 rp->vr_resolve_func = tinfo->vnt_resolve_func;
9349 rp->vr_unresolve_func = tinfo->vnt_unresolve_func;
9350 rp->vr_rearm_func = tinfo->vnt_rearm_func;
9351 rp->vr_reclaim_func = tinfo->vnt_reclaim_func;
9352 rp->vr_data = tinfo->vnt_data;
9353 rp->vr_lastseq = 0;
9354 rp->vr_flags = tinfo->vnt_flags & VNT_VALID_MASK;
9355 if (external) {
9356 rp->vr_flags |= VNT_EXTERNAL;
9357 }
9358
9359 result = vnode_resolver_attach(vp, rp, external);
9360 if (result != 0) {
9361 goto out;
9362 }
9363
9364 if (mp) {
9365 OSAddAtomic(1, &mp->mnt_numtriggers);
9366 }
9367
9368 return (result);
9369
9370 out:
9371 FREE(rp, M_TEMP);
9372 return result;
9373 }
9374
9375 static void
9376 vnode_resolver_release(vnode_resolve_t rp)
9377 {
9378 /*
9379 * Give them a chance to free any private data
9380 */
9381 if (rp->vr_data && rp->vr_reclaim_func) {
9382 rp->vr_reclaim_func(NULLVP, rp->vr_data);
9383 }
9384
9385 lck_mtx_destroy(&rp->vr_lock, trigger_vnode_lck_grp);
9386 FREE(rp, M_TEMP);
9387
9388 }
9389
9390 /* Called after the vnode has been drained */
9391 static void
9392 vnode_resolver_detach(vnode_t vp)
9393 {
9394 vnode_resolve_t rp;
9395 mount_t mp;
9396
9397 mp = vnode_mount(vp);
9398
9399 vnode_lock(vp);
9400 rp = vp->v_resolve;
9401 vp->v_resolve = NULL;
9402 vnode_unlock(vp);
9403
9404 if ((rp->vr_flags & VNT_EXTERNAL) != 0) {
9405 vnode_rele_ext(vp, O_EVTONLY, 1);
9406 }
9407
9408 vnode_resolver_release(rp);
9409
9410 /* Keep count of active trigger vnodes per mount */
9411 OSAddAtomic(-1, &mp->mnt_numtriggers);
9412 }
9413
9414 __private_extern__
9415 void
9416 vnode_trigger_rearm(vnode_t vp, vfs_context_t ctx)
9417 {
9418 vnode_resolve_t rp;
9419 resolver_result_t result;
9420 enum resolver_status status;
9421 uint32_t seq;
9422
9423 if ((vp->v_resolve == NULL) ||
9424 (vp->v_resolve->vr_rearm_func == NULL) ||
9425 (vp->v_resolve->vr_flags & VNT_AUTO_REARM) == 0) {
9426 return;
9427 }
9428
9429 rp = vp->v_resolve;
9430 lck_mtx_lock(&rp->vr_lock);
9431
9432 /*
9433 * Check if VFS initiated this unmount. If so, we'll catch it after the unresolve completes.
9434 */
9435 if (rp->vr_flags & VNT_VFS_UNMOUNTED) {
9436 lck_mtx_unlock(&rp->vr_lock);
9437 return;
9438 }
9439
9440 /* Check if this vnode is already armed */
9441 if ((rp->vr_flags & VNT_RESOLVED) == 0) {
9442 lck_mtx_unlock(&rp->vr_lock);
9443 return;
9444 }
9445
9446 lck_mtx_unlock(&rp->vr_lock);
9447
9448 result = rp->vr_rearm_func(vp, 0, rp->vr_data, ctx);
9449 status = vfs_resolver_status(result);
9450 seq = vfs_resolver_sequence(result);
9451
9452 lck_mtx_lock(&rp->vr_lock);
9453 if (seq > rp->vr_lastseq) {
9454 if (status == RESOLVER_UNRESOLVED)
9455 rp->vr_flags &= ~VNT_RESOLVED;
9456 rp->vr_lastseq = seq;
9457 }
9458 lck_mtx_unlock(&rp->vr_lock);
9459 }
9460
9461 __private_extern__
9462 int
9463 vnode_trigger_resolve(vnode_t vp, struct nameidata *ndp, vfs_context_t ctx)
9464 {
9465 vnode_resolve_t rp;
9466 enum path_operation op;
9467 resolver_result_t result;
9468 enum resolver_status status;
9469 uint32_t seq;
9470
9471 /* Only trigger on topmost vnodes */
9472 if ((vp->v_resolve == NULL) ||
9473 (vp->v_resolve->vr_resolve_func == NULL) ||
9474 (vp->v_mountedhere != NULL)) {
9475 return (0);
9476 }
9477
9478 rp = vp->v_resolve;
9479 lck_mtx_lock(&rp->vr_lock);
9480
9481 /* Check if this vnode is already resolved */
9482 if (rp->vr_flags & VNT_RESOLVED) {
9483 lck_mtx_unlock(&rp->vr_lock);
9484 return (0);
9485 }
9486
9487 lck_mtx_unlock(&rp->vr_lock);
9488
9489 /*
9490 * XXX
9491 * assumes that resolver will not access this trigger vnode (otherwise the kernel will deadlock)
9492 * is there anyway to know this???
9493 * there can also be other legitimate lookups in parallel
9494 *
9495 * XXX - should we call this on a separate thread with a timeout?
9496 *
9497 * XXX - should we use ISLASTCN to pick the op value??? Perhaps only leafs should
9498 * get the richer set and non-leafs should get generic OP_LOOKUP? TBD
9499 */
9500 op = (ndp->ni_op < OP_MAXOP) ? ndp->ni_op: OP_LOOKUP;
9501
9502 result = rp->vr_resolve_func(vp, &ndp->ni_cnd, op, 0, rp->vr_data, ctx);
9503 status = vfs_resolver_status(result);
9504 seq = vfs_resolver_sequence(result);
9505
9506 lck_mtx_lock(&rp->vr_lock);
9507 if (seq > rp->vr_lastseq) {
9508 if (status == RESOLVER_RESOLVED)
9509 rp->vr_flags |= VNT_RESOLVED;
9510 rp->vr_lastseq = seq;
9511 }
9512 lck_mtx_unlock(&rp->vr_lock);
9513
9514 /* On resolver errors, propagate the error back up */
9515 return (status == RESOLVER_ERROR ? vfs_resolver_auxiliary(result) : 0);
9516 }
9517
9518 static int
9519 vnode_trigger_unresolve(vnode_t vp, int flags, vfs_context_t ctx)
9520 {
9521 vnode_resolve_t rp;
9522 resolver_result_t result;
9523 enum resolver_status status;
9524 uint32_t seq;
9525
9526 if ((vp->v_resolve == NULL) || (vp->v_resolve->vr_unresolve_func == NULL)) {
9527 return (0);
9528 }
9529
9530 rp = vp->v_resolve;
9531 lck_mtx_lock(&rp->vr_lock);
9532
9533 /* Check if this vnode is already resolved */
9534 if ((rp->vr_flags & VNT_RESOLVED) == 0) {
9535 printf("vnode_trigger_unresolve: not currently resolved\n");
9536 lck_mtx_unlock(&rp->vr_lock);
9537 return (0);
9538 }
9539
9540 rp->vr_flags |= VNT_VFS_UNMOUNTED;
9541
9542 lck_mtx_unlock(&rp->vr_lock);
9543
9544 /*
9545 * XXX
9546 * assumes that resolver will not access this trigger vnode (otherwise the kernel will deadlock)
9547 * there can also be other legitimate lookups in parallel
9548 *
9549 * XXX - should we call this on a separate thread with a timeout?
9550 */
9551
9552 result = rp->vr_unresolve_func(vp, flags, rp->vr_data, ctx);
9553 status = vfs_resolver_status(result);
9554 seq = vfs_resolver_sequence(result);
9555
9556 lck_mtx_lock(&rp->vr_lock);
9557 if (seq > rp->vr_lastseq) {
9558 if (status == RESOLVER_UNRESOLVED)
9559 rp->vr_flags &= ~VNT_RESOLVED;
9560 rp->vr_lastseq = seq;
9561 }
9562 rp->vr_flags &= ~VNT_VFS_UNMOUNTED;
9563 lck_mtx_unlock(&rp->vr_lock);
9564
9565 /* On resolver errors, propagate the error back up */
9566 return (status == RESOLVER_ERROR ? vfs_resolver_auxiliary(result) : 0);
9567 }
9568
9569 static int
9570 triggerisdescendant(mount_t mp, mount_t rmp)
9571 {
9572 int match = FALSE;
9573
9574 /*
9575 * walk up vnode covered chain looking for a match
9576 */
9577 name_cache_lock_shared();
9578
9579 while (1) {
9580 vnode_t vp;
9581
9582 /* did we encounter "/" ? */
9583 if (mp->mnt_flag & MNT_ROOTFS)
9584 break;
9585
9586 vp = mp->mnt_vnodecovered;
9587 if (vp == NULLVP)
9588 break;
9589
9590 mp = vp->v_mount;
9591 if (mp == rmp) {
9592 match = TRUE;
9593 break;
9594 }
9595 }
9596
9597 name_cache_unlock();
9598
9599 return (match);
9600 }
9601
9602 struct trigger_unmount_info {
9603 vfs_context_t ctx;
9604 mount_t top_mp;
9605 vnode_t trigger_vp;
9606 mount_t trigger_mp;
9607 uint32_t trigger_vid;
9608 int flags;
9609 };
9610
9611 static int
9612 trigger_unmount_callback(mount_t mp, void * arg)
9613 {
9614 struct trigger_unmount_info * infop = (struct trigger_unmount_info *)arg;
9615 boolean_t mountedtrigger = FALSE;
9616
9617 /*
9618 * When we encounter the top level mount we're done
9619 */
9620 if (mp == infop->top_mp)
9621 return (VFS_RETURNED_DONE);
9622
9623 if ((mp->mnt_vnodecovered == NULL) ||
9624 (vnode_getwithref(mp->mnt_vnodecovered) != 0)) {
9625 return (VFS_RETURNED);
9626 }
9627
9628 if ((mp->mnt_vnodecovered->v_mountedhere == mp) &&
9629 (mp->mnt_vnodecovered->v_resolve != NULL) &&
9630 (mp->mnt_vnodecovered->v_resolve->vr_flags & VNT_RESOLVED)) {
9631 mountedtrigger = TRUE;
9632 }
9633 vnode_put(mp->mnt_vnodecovered);
9634
9635 /*
9636 * When we encounter a mounted trigger, check if its under the top level mount
9637 */
9638 if ( !mountedtrigger || !triggerisdescendant(mp, infop->top_mp) )
9639 return (VFS_RETURNED);
9640
9641 /*
9642 * Process any pending nested mount (now that its not referenced)
9643 */
9644 if ((infop->trigger_vp != NULLVP) &&
9645 (vnode_getwithvid(infop->trigger_vp, infop->trigger_vid) == 0)) {
9646 vnode_t vp = infop->trigger_vp;
9647 int error;
9648
9649 infop->trigger_vp = NULLVP;
9650
9651 if (mp == vp->v_mountedhere) {
9652 vnode_put(vp);
9653 printf("trigger_unmount_callback: unexpected match '%s'\n",
9654 mp->mnt_vfsstat.f_mntonname);
9655 return (VFS_RETURNED);
9656 }
9657 if (infop->trigger_mp != vp->v_mountedhere) {
9658 vnode_put(vp);
9659 printf("trigger_unmount_callback: trigger mnt changed! (%p != %p)\n",
9660 infop->trigger_mp, vp->v_mountedhere);
9661 goto savenext;
9662 }
9663
9664 error = vnode_trigger_unresolve(vp, infop->flags, infop->ctx);
9665 vnode_put(vp);
9666 if (error) {
9667 printf("unresolving: '%s', err %d\n",
9668 vp->v_mountedhere ? vp->v_mountedhere->mnt_vfsstat.f_mntonname :
9669 "???", error);
9670 return (VFS_RETURNED_DONE); /* stop iteration on errors */
9671 }
9672 }
9673 savenext:
9674 /*
9675 * We can't call resolver here since we hold a mount iter
9676 * ref on mp so save its covered vp for later processing
9677 */
9678 infop->trigger_vp = mp->mnt_vnodecovered;
9679 if ((infop->trigger_vp != NULLVP) &&
9680 (vnode_getwithref(infop->trigger_vp) == 0)) {
9681 if (infop->trigger_vp->v_mountedhere == mp) {
9682 infop->trigger_vid = infop->trigger_vp->v_id;
9683 infop->trigger_mp = mp;
9684 }
9685 vnode_put(infop->trigger_vp);
9686 }
9687
9688 return (VFS_RETURNED);
9689 }
9690
9691 /*
9692 * Attempt to unmount any trigger mounts nested underneath a mount.
9693 * This is a best effort attempt and no retries are performed here.
9694 *
9695 * Note: mp->mnt_rwlock is held exclusively on entry (so be carefull)
9696 */
9697 __private_extern__
9698 void
9699 vfs_nested_trigger_unmounts(mount_t mp, int flags, vfs_context_t ctx)
9700 {
9701 struct trigger_unmount_info info;
9702
9703 /* Must have trigger vnodes */
9704 if (mp->mnt_numtriggers == 0) {
9705 return;
9706 }
9707 /* Avoid recursive requests (by checking covered vnode) */
9708 if ((mp->mnt_vnodecovered != NULL) &&
9709 (vnode_getwithref(mp->mnt_vnodecovered) == 0)) {
9710 boolean_t recursive = FALSE;
9711
9712 if ((mp->mnt_vnodecovered->v_mountedhere == mp) &&
9713 (mp->mnt_vnodecovered->v_resolve != NULL) &&
9714 (mp->mnt_vnodecovered->v_resolve->vr_flags & VNT_VFS_UNMOUNTED)) {
9715 recursive = TRUE;
9716 }
9717 vnode_put(mp->mnt_vnodecovered);
9718 if (recursive)
9719 return;
9720 }
9721
9722 /*
9723 * Attempt to unmount any nested trigger mounts (best effort)
9724 */
9725 info.ctx = ctx;
9726 info.top_mp = mp;
9727 info.trigger_vp = NULLVP;
9728 info.trigger_vid = 0;
9729 info.trigger_mp = NULL;
9730 info.flags = flags;
9731
9732 (void) vfs_iterate(VFS_ITERATE_TAIL_FIRST, trigger_unmount_callback, &info);
9733
9734 /*
9735 * Process remaining nested mount (now that its not referenced)
9736 */
9737 if ((info.trigger_vp != NULLVP) &&
9738 (vnode_getwithvid(info.trigger_vp, info.trigger_vid) == 0)) {
9739 vnode_t vp = info.trigger_vp;
9740
9741 if (info.trigger_mp == vp->v_mountedhere) {
9742 (void) vnode_trigger_unresolve(vp, flags, ctx);
9743 }
9744 vnode_put(vp);
9745 }
9746 }
9747
9748 int
9749 vfs_addtrigger(mount_t mp, const char *relpath, struct vnode_trigger_info *vtip, vfs_context_t ctx)
9750 {
9751 struct nameidata nd;
9752 int res;
9753 vnode_t rvp, vp;
9754 struct vnode_trigger_param vtp;
9755
9756 /*
9757 * Must be called for trigger callback, wherein rwlock is held
9758 */
9759 lck_rw_assert(&mp->mnt_rwlock, LCK_RW_ASSERT_HELD);
9760
9761 TRIG_LOG("Adding trigger at %s\n", relpath);
9762 TRIG_LOG("Trying VFS_ROOT\n");
9763
9764 /*
9765 * We do a lookup starting at the root of the mountpoint, unwilling
9766 * to cross into other mountpoints.
9767 */
9768 res = VFS_ROOT(mp, &rvp, ctx);
9769 if (res != 0) {
9770 goto out;
9771 }
9772
9773 TRIG_LOG("Trying namei\n");
9774
9775 NDINIT(&nd, LOOKUP, OP_LOOKUP, USEDVP | NOCROSSMOUNT | FOLLOW, UIO_SYSSPACE,
9776 CAST_USER_ADDR_T(relpath), ctx);
9777 nd.ni_dvp = rvp;
9778 res = namei(&nd);
9779 if (res != 0) {
9780 vnode_put(rvp);
9781 goto out;
9782 }
9783
9784 vp = nd.ni_vp;
9785 nameidone(&nd);
9786 vnode_put(rvp);
9787
9788 TRIG_LOG("Trying vnode_resolver_create()\n");
9789
9790 /*
9791 * Set up blob. vnode_create() takes a larger structure
9792 * with creation info, and we needed something different
9793 * for this case. One needs to win, or we need to munge both;
9794 * vnode_create() wins.
9795 */
9796 bzero(&vtp, sizeof(vtp));
9797 vtp.vnt_resolve_func = vtip->vti_resolve_func;
9798 vtp.vnt_unresolve_func = vtip->vti_unresolve_func;
9799 vtp.vnt_rearm_func = vtip->vti_rearm_func;
9800 vtp.vnt_reclaim_func = vtip->vti_reclaim_func;
9801 vtp.vnt_reclaim_func = vtip->vti_reclaim_func;
9802 vtp.vnt_data = vtip->vti_data;
9803 vtp.vnt_flags = vtip->vti_flags;
9804
9805 res = vnode_resolver_create(mp, vp, &vtp, TRUE);
9806 vnode_put(vp);
9807 out:
9808 TRIG_LOG("Returning %d\n", res);
9809 return res;
9810 }
9811
9812 #endif /* CONFIG_TRIGGERS */
9813
9814 vm_offset_t kdebug_vnode(vnode_t vp)
9815 {
9816 return VM_KERNEL_ADDRPERM(vp);
9817 }
9818
9819 static int flush_cache_on_write = 0;
9820 SYSCTL_INT (_kern, OID_AUTO, flush_cache_on_write,
9821 CTLFLAG_RW | CTLFLAG_LOCKED, &flush_cache_on_write, 0,
9822 "always flush the drive cache on writes to uncached files");
9823
9824 int vnode_should_flush_after_write(vnode_t vp, int ioflag)
9825 {
9826 return (flush_cache_on_write
9827 && (ISSET(ioflag, IO_NOCACHE) || vnode_isnocache(vp)));
9828 }
9829
9830 /*
9831 * sysctl for use by disk I/O tracing tools to get the list of existing
9832 * vnodes' paths
9833 */
9834
9835 struct vnode_trace_paths_context {
9836 uint64_t count;
9837 long path[MAXPATHLEN / sizeof (long) + 1]; /* + 1 in case sizeof (long) does not divide MAXPATHLEN */
9838 };
9839
9840 static int vnode_trace_path_callback(struct vnode *vp, void *arg) {
9841 int len, rv;
9842 struct vnode_trace_paths_context *ctx;
9843
9844 ctx = arg;
9845
9846 len = sizeof (ctx->path);
9847 rv = vn_getpath(vp, (char *)ctx->path, &len);
9848 /* vn_getpath() NUL-terminates, and len includes the NUL */
9849
9850 if (!rv) {
9851 kdebug_lookup_gen_events(ctx->path, len, vp, TRUE);
9852
9853 if (++(ctx->count) == 1000) {
9854 thread_yield_to_preemption();
9855 ctx->count = 0;
9856 }
9857 }
9858
9859 return VNODE_RETURNED;
9860 }
9861
9862 static int vfs_trace_paths_callback(mount_t mp, void *arg) {
9863 if (mp->mnt_flag & MNT_LOCAL)
9864 vnode_iterate(mp, VNODE_ITERATE_ALL, vnode_trace_path_callback, arg);
9865
9866 return VFS_RETURNED;
9867 }
9868
9869 static int sysctl_vfs_trace_paths SYSCTL_HANDLER_ARGS {
9870 struct vnode_trace_paths_context ctx;
9871
9872 (void)oidp;
9873 (void)arg1;
9874 (void)arg2;
9875 (void)req;
9876
9877 if (!kauth_cred_issuser(kauth_cred_get()))
9878 return EPERM;
9879
9880 if (!kdebug_enable || !kdebug_debugid_enabled(VFS_LOOKUP))
9881 return EINVAL;
9882
9883 bzero(&ctx, sizeof (struct vnode_trace_paths_context));
9884
9885 vfs_iterate(0, vfs_trace_paths_callback, &ctx);
9886
9887 return 0;
9888 }
9889
9890 SYSCTL_PROC(_vfs_generic, OID_AUTO, trace_paths, CTLFLAG_RD | CTLFLAG_LOCKED | CTLFLAG_MASKED, NULL, 0, &sysctl_vfs_trace_paths, "-", "trace_paths");