]> git.saurik.com Git - apple/xnu.git/blob - bsd/vfs/vfs_subr.c
004292442a17ba3614991bbaff758003b14ce255
[apple/xnu.git] / bsd / vfs / vfs_subr.c
1 /*
2 * Copyright (c) 2006 Apple Computer, Inc. All Rights Reserved.
3 *
4 * @APPLE_LICENSE_OSREFERENCE_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
10 * License may not be used to create, or enable the creation or
11 * redistribution of, unlawful or unlicensed copies of an Apple operating
12 * system, or to circumvent, violate, or enable the circumvention or
13 * violation of, any terms of an Apple operating system software license
14 * agreement.
15 *
16 * Please obtain a copy of the License at
17 * http://www.opensource.apple.com/apsl/ and read it before using this
18 * file.
19 *
20 * The Original Code and all software distributed under the License are
21 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
22 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
23 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
24 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
25 * Please see the License for the specific language governing rights and
26 * limitations under the License.
27 *
28 * @APPLE_LICENSE_OSREFERENCE_HEADER_END@
29 */
30 /* Copyright (c) 1995 NeXT Computer, Inc. All Rights Reserved */
31 /*
32 * Copyright (c) 1989, 1993
33 * The Regents of the University of California. All rights reserved.
34 * (c) UNIX System Laboratories, Inc.
35 * All or some portions of this file are derived from material licensed
36 * to the University of California by American Telephone and Telegraph
37 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
38 * the permission of UNIX System Laboratories, Inc.
39 *
40 * Redistribution and use in source and binary forms, with or without
41 * modification, are permitted provided that the following conditions
42 * are met:
43 * 1. Redistributions of source code must retain the above copyright
44 * notice, this list of conditions and the following disclaimer.
45 * 2. Redistributions in binary form must reproduce the above copyright
46 * notice, this list of conditions and the following disclaimer in the
47 * documentation and/or other materials provided with the distribution.
48 * 3. All advertising materials mentioning features or use of this software
49 * must display the following acknowledgement:
50 * This product includes software developed by the University of
51 * California, Berkeley and its contributors.
52 * 4. Neither the name of the University nor the names of its contributors
53 * may be used to endorse or promote products derived from this software
54 * without specific prior written permission.
55 *
56 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
57 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
58 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
59 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
60 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
61 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
62 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
63 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
64 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
65 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
66 * SUCH DAMAGE.
67 *
68 * @(#)vfs_subr.c 8.31 (Berkeley) 5/26/95
69 */
70
71 /*
72 * External virtual filesystem routines
73 */
74
75 #undef DIAGNOSTIC
76 #define DIAGNOSTIC 1
77
78 #include <sys/param.h>
79 #include <sys/systm.h>
80 #include <sys/proc_internal.h>
81 #include <sys/kauth.h>
82 #include <sys/mount_internal.h>
83 #include <sys/time.h>
84 #include <sys/lock.h>
85 #include <sys/vnode_internal.h>
86 #include <sys/stat.h>
87 #include <sys/namei.h>
88 #include <sys/ucred.h>
89 #include <sys/buf_internal.h>
90 #include <sys/errno.h>
91 #include <sys/malloc.h>
92 #include <sys/domain.h>
93 #include <sys/mbuf.h>
94 #include <sys/syslog.h>
95 #include <sys/ubc_internal.h>
96 #include <sys/vm.h>
97 #include <sys/sysctl.h>
98 #include <sys/filedesc.h>
99 #include <sys/event.h>
100 #include <sys/kdebug.h>
101 #include <sys/kauth.h>
102 #include <sys/user.h>
103 #include <miscfs/fifofs/fifo.h>
104
105 #include <string.h>
106 #include <machine/spl.h>
107
108
109 #include <kern/assert.h>
110
111 #include <miscfs/specfs/specdev.h>
112
113 #include <mach/mach_types.h>
114 #include <mach/memory_object_types.h>
115
116 extern lck_grp_t *vnode_lck_grp;
117 extern lck_attr_t *vnode_lck_attr;
118
119
120 extern lck_mtx_t * mnt_list_mtx_lock;
121
122 enum vtype iftovt_tab[16] = {
123 VNON, VFIFO, VCHR, VNON, VDIR, VNON, VBLK, VNON,
124 VREG, VNON, VLNK, VNON, VSOCK, VNON, VNON, VBAD,
125 };
126 int vttoif_tab[9] = {
127 0, S_IFREG, S_IFDIR, S_IFBLK, S_IFCHR, S_IFLNK,
128 S_IFSOCK, S_IFIFO, S_IFMT,
129 };
130
131 extern int ubc_isinuse_locked(vnode_t, int, int);
132 extern kern_return_t adjust_vm_object_cache(vm_size_t oval, vm_size_t nval);
133
134 static void vnode_list_add(vnode_t);
135 static void vnode_list_remove(vnode_t);
136
137 static errno_t vnode_drain(vnode_t);
138 static void vgone(vnode_t);
139 static void vclean(vnode_t vp, int flag, proc_t p);
140 static void vnode_reclaim_internal(vnode_t, int, int);
141
142 static void vnode_dropiocount (vnode_t, int);
143 static errno_t vnode_getiocount(vnode_t vp, int locked, int vid, int vflags);
144 static int vget_internal(vnode_t, int, int);
145
146 static vnode_t checkalias(vnode_t vp, dev_t nvp_rdev);
147 static int vnode_reload(vnode_t);
148 static int vnode_isinuse_locked(vnode_t, int, int);
149
150 static void insmntque(vnode_t vp, mount_t mp);
151 mount_t mount_list_lookupby_fsid(fsid_t *, int, int);
152 static int mount_getvfscnt(void);
153 static int mount_fillfsids(fsid_t *, int );
154 static void vnode_iterate_setup(mount_t);
155 static int vnode_umount_preflight(mount_t, vnode_t, int);
156 static int vnode_iterate_prepare(mount_t);
157 static int vnode_iterate_reloadq(mount_t);
158 static void vnode_iterate_clear(mount_t);
159
160 TAILQ_HEAD(freelst, vnode) vnode_free_list; /* vnode free list */
161 TAILQ_HEAD(inactivelst, vnode) vnode_inactive_list; /* vnode inactive list */
162 struct mntlist mountlist; /* mounted filesystem list */
163 static int nummounts = 0;
164
165 #if DIAGNOSTIC
166 #define VLISTCHECK(fun, vp, list) \
167 if ((vp)->v_freelist.tqe_prev == (struct vnode **)0xdeadb) \
168 panic("%s: %s vnode not on %slist", (fun), (list), (list));
169
170 #define VINACTIVECHECK(fun, vp, expected) \
171 do { \
172 int __is_inactive = ISSET((vp)->v_flag, VUINACTIVE); \
173 if (__is_inactive ^ expected) \
174 panic("%s: %sinactive vnode, expected %s", (fun), \
175 __is_inactive? "" : "not ", \
176 expected? "inactive": "not inactive"); \
177 } while(0)
178 #else
179 #define VLISTCHECK(fun, vp, list)
180 #define VINACTIVECHECK(fun, vp, expected)
181 #endif /* DIAGNOSTIC */
182
183 #define VLISTNONE(vp) \
184 do { \
185 (vp)->v_freelist.tqe_next = (struct vnode *)0; \
186 (vp)->v_freelist.tqe_prev = (struct vnode **)0xdeadb; \
187 } while(0)
188
189 #define VONLIST(vp) \
190 ((vp)->v_freelist.tqe_prev != (struct vnode **)0xdeadb)
191
192 /* remove a vnode from free vnode list */
193 #define VREMFREE(fun, vp) \
194 do { \
195 VLISTCHECK((fun), (vp), "free"); \
196 TAILQ_REMOVE(&vnode_free_list, (vp), v_freelist); \
197 VLISTNONE((vp)); \
198 freevnodes--; \
199 } while(0)
200
201 /* remove a vnode from inactive vnode list */
202 #define VREMINACTIVE(fun, vp) \
203 do { \
204 VLISTCHECK((fun), (vp), "inactive"); \
205 VINACTIVECHECK((fun), (vp), VUINACTIVE); \
206 TAILQ_REMOVE(&vnode_inactive_list, (vp), v_freelist); \
207 CLR((vp)->v_flag, VUINACTIVE); \
208 VLISTNONE((vp)); \
209 inactivevnodes--; \
210 } while(0)
211
212 /*
213 * Have to declare first two locks as actual data even if !MACH_SLOCKS, since
214 * a pointers to them get passed around.
215 */
216 void * mntvnode_slock;
217 void * mntid_slock;
218 void * spechash_slock;
219
220 /*
221 * vnodetarget is the amount of vnodes we expect to get back
222 * from the the inactive vnode list and VM object cache.
223 * As vnreclaim() is a mainly cpu bound operation for faster
224 * processers this number could be higher.
225 * Having this number too high introduces longer delays in
226 * the execution of new_vnode().
227 */
228 unsigned long vnodetarget; /* target for vnreclaim() */
229 #define VNODE_FREE_TARGET 20 /* Default value for vnodetarget */
230
231 /*
232 * We need quite a few vnodes on the free list to sustain the
233 * rapid stat() the compilation process does, and still benefit from the name
234 * cache. Having too few vnodes on the free list causes serious disk
235 * thrashing as we cycle through them.
236 */
237 #define VNODE_FREE_MIN 300 /* freelist should have at least these many */
238
239 /*
240 * We need to get vnodes back from the VM object cache when a certain #
241 * of vnodes are reused from the freelist. This is essential for the
242 * caching to be effective in the namecache and the buffer cache [for the
243 * metadata].
244 */
245 #define VNODE_TOOMANY_REUSED (VNODE_FREE_MIN/4)
246
247 /*
248 * If we have enough vnodes on the freelist we do not want to reclaim
249 * the vnodes from the VM object cache.
250 */
251 #define VNODE_FREE_ENOUGH (VNODE_FREE_MIN + (VNODE_FREE_MIN/2))
252
253 /*
254 * Initialize the vnode management data structures.
255 */
256 __private_extern__ void
257 vntblinit(void)
258 {
259 TAILQ_INIT(&vnode_free_list);
260 TAILQ_INIT(&vnode_inactive_list);
261 TAILQ_INIT(&mountlist);
262
263 if (!vnodetarget)
264 vnodetarget = VNODE_FREE_TARGET;
265
266 /*
267 * Scale the vm_object_cache to accomodate the vnodes
268 * we want to cache
269 */
270 (void) adjust_vm_object_cache(0, desiredvnodes - VNODE_FREE_MIN);
271 }
272
273 /* Reset the VM Object Cache with the values passed in */
274 __private_extern__ kern_return_t
275 reset_vmobjectcache(unsigned int val1, unsigned int val2)
276 {
277 vm_size_t oval = val1 - VNODE_FREE_MIN;
278 vm_size_t nval;
279
280 if(val2 < VNODE_FREE_MIN)
281 nval = 0;
282 else
283 nval = val2 - VNODE_FREE_MIN;
284
285 return(adjust_vm_object_cache(oval, nval));
286 }
287
288
289 /* the timeout is in 10 msecs */
290 int
291 vnode_waitforwrites(vnode_t vp, int output_target, int slpflag, int slptimeout, char *msg) {
292 int error = 0;
293 struct timespec ts;
294
295 KERNEL_DEBUG(0x3010280 | DBG_FUNC_START, (int)vp, output_target, vp->v_numoutput, 0, 0);
296
297 if (vp->v_numoutput > output_target) {
298
299 slpflag &= ~PDROP;
300
301 vnode_lock(vp);
302
303 while ((vp->v_numoutput > output_target) && error == 0) {
304 if (output_target)
305 vp->v_flag |= VTHROTTLED;
306 else
307 vp->v_flag |= VBWAIT;
308 ts.tv_sec = (slptimeout/100);
309 ts.tv_nsec = (slptimeout % 1000) * 10 * NSEC_PER_USEC * 1000 ;
310 error = msleep((caddr_t)&vp->v_numoutput, &vp->v_lock, (slpflag | (PRIBIO + 1)), msg, &ts);
311 }
312 vnode_unlock(vp);
313 }
314 KERNEL_DEBUG(0x3010280 | DBG_FUNC_END, (int)vp, output_target, vp->v_numoutput, error, 0);
315
316 return error;
317 }
318
319
320 void
321 vnode_startwrite(vnode_t vp) {
322
323 OSAddAtomic(1, &vp->v_numoutput);
324 }
325
326
327 void
328 vnode_writedone(vnode_t vp)
329 {
330 if (vp) {
331 int need_wakeup = 0;
332
333 OSAddAtomic(-1, &vp->v_numoutput);
334
335 vnode_lock(vp);
336
337 if (vp->v_numoutput < 0)
338 panic("vnode_writedone: numoutput < 0");
339
340 if ((vp->v_flag & VTHROTTLED) && (vp->v_numoutput < (VNODE_ASYNC_THROTTLE / 3))) {
341 vp->v_flag &= ~VTHROTTLED;
342 need_wakeup = 1;
343 }
344 if ((vp->v_flag & VBWAIT) && (vp->v_numoutput == 0)) {
345 vp->v_flag &= ~VBWAIT;
346 need_wakeup = 1;
347 }
348 vnode_unlock(vp);
349
350 if (need_wakeup)
351 wakeup((caddr_t)&vp->v_numoutput);
352 }
353 }
354
355
356
357 int
358 vnode_hasdirtyblks(vnode_t vp)
359 {
360 struct cl_writebehind *wbp;
361
362 /*
363 * Not taking the buf_mtxp as there is little
364 * point doing it. Even if the lock is taken the
365 * state can change right after that. If their
366 * needs to be a synchronization, it must be driven
367 * by the caller
368 */
369 if (vp->v_dirtyblkhd.lh_first)
370 return (1);
371
372 if (!UBCINFOEXISTS(vp))
373 return (0);
374
375 wbp = vp->v_ubcinfo->cl_wbehind;
376
377 if (wbp && (wbp->cl_number || wbp->cl_scmap))
378 return (1);
379
380 return (0);
381 }
382
383 int
384 vnode_hascleanblks(vnode_t vp)
385 {
386 /*
387 * Not taking the buf_mtxp as there is little
388 * point doing it. Even if the lock is taken the
389 * state can change right after that. If their
390 * needs to be a synchronization, it must be driven
391 * by the caller
392 */
393 if (vp->v_cleanblkhd.lh_first)
394 return (1);
395 return (0);
396 }
397
398 void
399 vnode_iterate_setup(mount_t mp)
400 {
401 while (mp->mnt_lflag & MNT_LITER) {
402 mp->mnt_lflag |= MNT_LITERWAIT;
403 msleep((caddr_t)mp, &mp->mnt_mlock, PVFS, "vnode_iterate_setup", 0);
404 }
405
406 mp->mnt_lflag |= MNT_LITER;
407
408 }
409
410 static int
411 vnode_umount_preflight(mount_t mp, vnode_t skipvp, int flags)
412 {
413 vnode_t vp;
414
415 TAILQ_FOREACH(vp, &mp->mnt_vnodelist, v_mntvnodes) {
416 if (vp->v_type == VDIR)
417 continue;
418 if (vp == skipvp)
419 continue;
420 if ((flags & SKIPSYSTEM) && ((vp->v_flag & VSYSTEM) ||
421 (vp->v_flag & VNOFLUSH)))
422 continue;
423 if ((flags & SKIPSWAP) && (vp->v_flag & VSWAP))
424 continue;
425 if ((flags & WRITECLOSE) &&
426 (vp->v_writecount == 0 || vp->v_type != VREG))
427 continue;
428 /* Look for busy vnode */
429 if (((vp->v_usecount != 0) &&
430 ((vp->v_usecount - vp->v_kusecount) != 0)))
431 return(1);
432 }
433
434 return(0);
435 }
436
437 /*
438 * This routine prepares iteration by moving all the vnodes to worker queue
439 * called with mount lock held
440 */
441 int
442 vnode_iterate_prepare(mount_t mp)
443 {
444 vnode_t vp;
445
446 if (TAILQ_EMPTY(&mp->mnt_vnodelist)) {
447 /* nothing to do */
448 return (0);
449 }
450
451 vp = TAILQ_FIRST(&mp->mnt_vnodelist);
452 vp->v_mntvnodes.tqe_prev = &(mp->mnt_workerqueue.tqh_first);
453 mp->mnt_workerqueue.tqh_first = mp->mnt_vnodelist.tqh_first;
454 mp->mnt_workerqueue.tqh_last = mp->mnt_vnodelist.tqh_last;
455
456 TAILQ_INIT(&mp->mnt_vnodelist);
457 if (mp->mnt_newvnodes.tqh_first != NULL)
458 panic("vnode_iterate_prepare: newvnode when entering vnode");
459 TAILQ_INIT(&mp->mnt_newvnodes);
460
461 return (1);
462 }
463
464
465 /* called with mount lock held */
466 int
467 vnode_iterate_reloadq(mount_t mp)
468 {
469 int moved = 0;
470
471 /* add the remaining entries in workerq to the end of mount vnode list */
472 if (!TAILQ_EMPTY(&mp->mnt_workerqueue)) {
473 struct vnode * mvp;
474 mvp = TAILQ_LAST(&mp->mnt_vnodelist, vnodelst);
475
476 /* Joining the workerque entities to mount vnode list */
477 if (mvp)
478 mvp->v_mntvnodes.tqe_next = mp->mnt_workerqueue.tqh_first;
479 else
480 mp->mnt_vnodelist.tqh_first = mp->mnt_workerqueue.tqh_first;
481 mp->mnt_workerqueue.tqh_first->v_mntvnodes.tqe_prev = mp->mnt_vnodelist.tqh_last;
482 mp->mnt_vnodelist.tqh_last = mp->mnt_workerqueue.tqh_last;
483 TAILQ_INIT(&mp->mnt_workerqueue);
484 }
485
486 /* add the newvnodes to the head of mount vnode list */
487 if (!TAILQ_EMPTY(&mp->mnt_newvnodes)) {
488 struct vnode * nlvp;
489 nlvp = TAILQ_LAST(&mp->mnt_newvnodes, vnodelst);
490
491 mp->mnt_newvnodes.tqh_first->v_mntvnodes.tqe_prev = &mp->mnt_vnodelist.tqh_first;
492 nlvp->v_mntvnodes.tqe_next = mp->mnt_vnodelist.tqh_first;
493 if(mp->mnt_vnodelist.tqh_first)
494 mp->mnt_vnodelist.tqh_first->v_mntvnodes.tqe_prev = &nlvp->v_mntvnodes.tqe_next;
495 else
496 mp->mnt_vnodelist.tqh_last = mp->mnt_newvnodes.tqh_last;
497 mp->mnt_vnodelist.tqh_first = mp->mnt_newvnodes.tqh_first;
498 TAILQ_INIT(&mp->mnt_newvnodes);
499 moved = 1;
500 }
501
502 return(moved);
503 }
504
505
506 void
507 vnode_iterate_clear(mount_t mp)
508 {
509 mp->mnt_lflag &= ~MNT_LITER;
510 if (mp->mnt_lflag & MNT_LITERWAIT) {
511 mp->mnt_lflag &= ~MNT_LITERWAIT;
512 wakeup(mp);
513 }
514 }
515
516
517 int
518 vnode_iterate(mp, flags, callout, arg)
519 mount_t mp;
520 int flags;
521 int (*callout)(struct vnode *, void *);
522 void * arg;
523 {
524 struct vnode *vp;
525 int vid, retval;
526 int ret = 0;
527
528 mount_lock(mp);
529
530 vnode_iterate_setup(mp);
531
532 /* it is returns 0 then there is nothing to do */
533 retval = vnode_iterate_prepare(mp);
534
535 if (retval == 0) {
536 vnode_iterate_clear(mp);
537 mount_unlock(mp);
538 return(ret);
539 }
540
541 /* iterate over all the vnodes */
542 while (!TAILQ_EMPTY(&mp->mnt_workerqueue)) {
543 vp = TAILQ_FIRST(&mp->mnt_workerqueue);
544 TAILQ_REMOVE(&mp->mnt_workerqueue, vp, v_mntvnodes);
545 TAILQ_INSERT_TAIL(&mp->mnt_vnodelist, vp, v_mntvnodes);
546 vid = vp->v_id;
547 if ((vp->v_data == NULL) || (vp->v_type == VNON) || (vp->v_mount != mp)) {
548 continue;
549 }
550 mount_unlock(mp);
551
552 if ( vget_internal(vp, vid, (flags | VNODE_NODEAD| VNODE_WITHID | VNODE_NOSUSPEND))) {
553 mount_lock(mp);
554 continue;
555 }
556 if (flags & VNODE_RELOAD) {
557 /*
558 * we're reloading the filesystem
559 * cast out any inactive vnodes...
560 */
561 if (vnode_reload(vp)) {
562 /* vnode will be recycled on the refcount drop */
563 vnode_put(vp);
564 mount_lock(mp);
565 continue;
566 }
567 }
568
569 retval = callout(vp, arg);
570
571 switch (retval) {
572 case VNODE_RETURNED:
573 case VNODE_RETURNED_DONE:
574 vnode_put(vp);
575 if (retval == VNODE_RETURNED_DONE) {
576 mount_lock(mp);
577 ret = 0;
578 goto out;
579 }
580 break;
581
582 case VNODE_CLAIMED_DONE:
583 mount_lock(mp);
584 ret = 0;
585 goto out;
586 case VNODE_CLAIMED:
587 default:
588 break;
589 }
590 mount_lock(mp);
591 }
592
593 out:
594 (void)vnode_iterate_reloadq(mp);
595 vnode_iterate_clear(mp);
596 mount_unlock(mp);
597 return (ret);
598 }
599
600 void
601 mount_lock_renames(mount_t mp)
602 {
603 lck_mtx_lock(&mp->mnt_renamelock);
604 }
605
606 void
607 mount_unlock_renames(mount_t mp)
608 {
609 lck_mtx_unlock(&mp->mnt_renamelock);
610 }
611
612 void
613 mount_lock(mount_t mp)
614 {
615 lck_mtx_lock(&mp->mnt_mlock);
616 }
617
618 void
619 mount_unlock(mount_t mp)
620 {
621 lck_mtx_unlock(&mp->mnt_mlock);
622 }
623
624
625 void
626 mount_ref(mount_t mp, int locked)
627 {
628 if ( !locked)
629 mount_lock(mp);
630
631 mp->mnt_count++;
632
633 if ( !locked)
634 mount_unlock(mp);
635 }
636
637
638 void
639 mount_drop(mount_t mp, int locked)
640 {
641 if ( !locked)
642 mount_lock(mp);
643
644 mp->mnt_count--;
645
646 if (mp->mnt_count == 0 && (mp->mnt_lflag & MNT_LDRAIN))
647 wakeup(&mp->mnt_lflag);
648
649 if ( !locked)
650 mount_unlock(mp);
651 }
652
653
654 int
655 mount_iterref(mount_t mp, int locked)
656 {
657 int retval = 0;
658
659 if (!locked)
660 mount_list_lock();
661 if (mp->mnt_iterref < 0) {
662 retval = 1;
663 } else {
664 mp->mnt_iterref++;
665 }
666 if (!locked)
667 mount_list_unlock();
668 return(retval);
669 }
670
671 int
672 mount_isdrained(mount_t mp, int locked)
673 {
674 int retval;
675
676 if (!locked)
677 mount_list_lock();
678 if (mp->mnt_iterref < 0)
679 retval = 1;
680 else
681 retval = 0;
682 if (!locked)
683 mount_list_unlock();
684 return(retval);
685 }
686
687 void
688 mount_iterdrop(mount_t mp)
689 {
690 mount_list_lock();
691 mp->mnt_iterref--;
692 wakeup(&mp->mnt_iterref);
693 mount_list_unlock();
694 }
695
696 void
697 mount_iterdrain(mount_t mp)
698 {
699 mount_list_lock();
700 while (mp->mnt_iterref)
701 msleep((caddr_t)&mp->mnt_iterref, mnt_list_mtx_lock, PVFS, "mount_iterdrain", 0 );
702 /* mount iterations drained */
703 mp->mnt_iterref = -1;
704 mount_list_unlock();
705 }
706 void
707 mount_iterreset(mount_t mp)
708 {
709 mount_list_lock();
710 if (mp->mnt_iterref == -1)
711 mp->mnt_iterref = 0;
712 mount_list_unlock();
713 }
714
715 /* always called with mount lock held */
716 int
717 mount_refdrain(mount_t mp)
718 {
719 if (mp->mnt_lflag & MNT_LDRAIN)
720 panic("already in drain");
721 mp->mnt_lflag |= MNT_LDRAIN;
722
723 while (mp->mnt_count)
724 msleep((caddr_t)&mp->mnt_lflag, &mp->mnt_mlock, PVFS, "mount_drain", 0 );
725
726 if (mp->mnt_vnodelist.tqh_first != NULL)
727 panic("mount_refdrain: dangling vnode");
728
729 mp->mnt_lflag &= ~MNT_LDRAIN;
730
731 return(0);
732 }
733
734
735 /*
736 * Mark a mount point as busy. Used to synchronize access and to delay
737 * unmounting.
738 */
739 int
740 vfs_busy(mount_t mp, int flags)
741 {
742
743 restart:
744 if (mp->mnt_lflag & MNT_LDEAD)
745 return(ENOENT);
746
747 if (mp->mnt_lflag & MNT_LUNMOUNT) {
748 if (flags & LK_NOWAIT)
749 return (ENOENT);
750
751 mount_lock(mp);
752
753 if (mp->mnt_lflag & MNT_LDEAD) {
754 mount_unlock(mp);
755 return(ENOENT);
756 }
757 if (mp->mnt_lflag & MNT_LUNMOUNT) {
758 mp->mnt_lflag |= MNT_LWAIT;
759 /*
760 * Since all busy locks are shared except the exclusive
761 * lock granted when unmounting, the only place that a
762 * wakeup needs to be done is at the release of the
763 * exclusive lock at the end of dounmount.
764 */
765 msleep((caddr_t)mp, &mp->mnt_mlock, (PVFS | PDROP), "vfsbusy", 0 );
766 return (ENOENT);
767 }
768 mount_unlock(mp);
769 }
770
771 lck_rw_lock_shared(&mp->mnt_rwlock);
772
773 /*
774 * until we are granted the rwlock, it's possible for the mount point to
775 * change state, so reevaluate before granting the vfs_busy
776 */
777 if (mp->mnt_lflag & (MNT_LDEAD | MNT_LUNMOUNT)) {
778 lck_rw_done(&mp->mnt_rwlock);
779 goto restart;
780 }
781 return (0);
782 }
783
784 /*
785 * Free a busy filesystem.
786 */
787
788 void
789 vfs_unbusy(mount_t mp)
790 {
791 lck_rw_done(&mp->mnt_rwlock);
792 }
793
794
795
796 static void
797 vfs_rootmountfailed(mount_t mp) {
798
799 mount_list_lock();
800 mp->mnt_vtable->vfc_refcount--;
801 mount_list_unlock();
802
803 vfs_unbusy(mp);
804
805 mount_lock_destroy(mp);
806
807 FREE_ZONE(mp, sizeof(struct mount), M_MOUNT);
808 }
809
810 /*
811 * Lookup a filesystem type, and if found allocate and initialize
812 * a mount structure for it.
813 *
814 * Devname is usually updated by mount(8) after booting.
815 */
816 static mount_t
817 vfs_rootmountalloc_internal(struct vfstable *vfsp, const char *devname)
818 {
819 mount_t mp;
820
821 mp = _MALLOC_ZONE((u_long)sizeof(struct mount), M_MOUNT, M_WAITOK);
822 bzero((char *)mp, (u_long)sizeof(struct mount));
823
824 /* Initialize the default IO constraints */
825 mp->mnt_maxreadcnt = mp->mnt_maxwritecnt = MAXPHYS;
826 mp->mnt_segreadcnt = mp->mnt_segwritecnt = 32;
827 mp->mnt_maxsegreadsize = mp->mnt_maxreadcnt;
828 mp->mnt_maxsegwritesize = mp->mnt_maxwritecnt;
829 mp->mnt_devblocksize = DEV_BSIZE;
830
831 mount_lock_init(mp);
832 (void)vfs_busy(mp, LK_NOWAIT);
833
834 TAILQ_INIT(&mp->mnt_vnodelist);
835 TAILQ_INIT(&mp->mnt_workerqueue);
836 TAILQ_INIT(&mp->mnt_newvnodes);
837
838 mp->mnt_vtable = vfsp;
839 mp->mnt_op = vfsp->vfc_vfsops;
840 mp->mnt_flag = MNT_RDONLY | MNT_ROOTFS;
841 mp->mnt_vnodecovered = NULLVP;
842 //mp->mnt_stat.f_type = vfsp->vfc_typenum;
843 mp->mnt_flag |= vfsp->vfc_flags & MNT_VISFLAGMASK;
844
845 mount_list_lock();
846 vfsp->vfc_refcount++;
847 mount_list_unlock();
848
849 strncpy(mp->mnt_vfsstat.f_fstypename, vfsp->vfc_name, MFSTYPENAMELEN);
850 mp->mnt_vfsstat.f_mntonname[0] = '/';
851 (void) copystr((char *)devname, mp->mnt_vfsstat.f_mntfromname, MAXPATHLEN - 1, 0);
852
853 return (mp);
854 }
855
856 errno_t
857 vfs_rootmountalloc(const char *fstypename, const char *devname, mount_t *mpp)
858 {
859 struct vfstable *vfsp;
860
861 for (vfsp = vfsconf; vfsp; vfsp = vfsp->vfc_next)
862 if (!strcmp(vfsp->vfc_name, fstypename))
863 break;
864 if (vfsp == NULL)
865 return (ENODEV);
866
867 *mpp = vfs_rootmountalloc_internal(vfsp, devname);
868
869 if (*mpp)
870 return (0);
871
872 return (ENOMEM);
873 }
874
875
876 /*
877 * Find an appropriate filesystem to use for the root. If a filesystem
878 * has not been preselected, walk through the list of known filesystems
879 * trying those that have mountroot routines, and try them until one
880 * works or we have tried them all.
881 */
882 extern int (*mountroot)(void);
883
884 int
885 vfs_mountroot()
886 {
887 struct vfstable *vfsp;
888 struct vfs_context context;
889 int error;
890 mount_t mp;
891
892 if (mountroot != NULL) {
893 /*
894 * used for netboot which follows a different set of rules
895 */
896 error = (*mountroot)();
897 return (error);
898 }
899 if ((error = bdevvp(rootdev, &rootvp))) {
900 printf("vfs_mountroot: can't setup bdevvp\n");
901 return (error);
902 }
903 context.vc_proc = current_proc();
904 context.vc_ucred = kauth_cred_get();
905
906 for (vfsp = vfsconf; vfsp; vfsp = vfsp->vfc_next) {
907 if (vfsp->vfc_mountroot == NULL)
908 continue;
909
910 mp = vfs_rootmountalloc_internal(vfsp, "root_device");
911 mp->mnt_devvp = rootvp;
912
913 if ((error = (*vfsp->vfc_mountroot)(mp, rootvp, &context)) == 0) {
914 mp->mnt_devvp->v_specflags |= SI_MOUNTEDON;
915
916 vfs_unbusy(mp);
917
918 mount_list_add(mp);
919
920 /*
921 * cache the IO attributes for the underlying physical media...
922 * an error return indicates the underlying driver doesn't
923 * support all the queries necessary... however, reasonable
924 * defaults will have been set, so no reason to bail or care
925 */
926 vfs_init_io_attributes(rootvp, mp);
927 /*
928 * get rid of iocount reference returned
929 * by bdevvp... it will have also taken
930 * a usecount reference which we want to keep
931 */
932 vnode_put(rootvp);
933
934 return (0);
935 }
936 vfs_rootmountfailed(mp);
937
938 if (error != EINVAL)
939 printf("%s_mountroot failed: %d\n", vfsp->vfc_name, error);
940 }
941 return (ENODEV);
942 }
943
944 /*
945 * Lookup a mount point by filesystem identifier.
946 */
947 extern mount_t vfs_getvfs_locked(fsid_t *);
948
949 struct mount *
950 vfs_getvfs(fsid)
951 fsid_t *fsid;
952 {
953 return (mount_list_lookupby_fsid(fsid, 0, 0));
954 }
955
956 struct mount *
957 vfs_getvfs_locked(fsid)
958 fsid_t *fsid;
959 {
960 return(mount_list_lookupby_fsid(fsid, 1, 0));
961 }
962
963 struct mount *
964 vfs_getvfs_by_mntonname(u_char *path)
965 {
966 mount_t retmp = (mount_t)0;
967 mount_t mp;
968
969 mount_list_lock();
970 TAILQ_FOREACH(mp, &mountlist, mnt_list) {
971 if (!strcmp(mp->mnt_vfsstat.f_mntonname, path)) {
972 retmp = mp;
973 goto out;
974 }
975 }
976 out:
977 mount_list_unlock();
978 return (retmp);
979 }
980
981 /* generation number for creation of new fsids */
982 u_short mntid_gen = 0;
983 /*
984 * Get a new unique fsid
985 */
986 void
987 vfs_getnewfsid(mp)
988 struct mount *mp;
989 {
990
991 fsid_t tfsid;
992 int mtype;
993 mount_t nmp;
994
995 mount_list_lock();
996
997 /* generate a new fsid */
998 mtype = mp->mnt_vtable->vfc_typenum;
999 if (++mntid_gen == 0)
1000 mntid_gen++;
1001 tfsid.val[0] = makedev(nblkdev + mtype, mntid_gen);
1002 tfsid.val[1] = mtype;
1003
1004 TAILQ_FOREACH(nmp, &mountlist, mnt_list) {
1005 while (vfs_getvfs_locked(&tfsid)) {
1006 if (++mntid_gen == 0)
1007 mntid_gen++;
1008 tfsid.val[0] = makedev(nblkdev + mtype, mntid_gen);
1009 }
1010 }
1011 mp->mnt_vfsstat.f_fsid.val[0] = tfsid.val[0];
1012 mp->mnt_vfsstat.f_fsid.val[1] = tfsid.val[1];
1013 mount_list_unlock();
1014 }
1015
1016 /*
1017 * Routines having to do with the management of the vnode table.
1018 */
1019 extern int (**dead_vnodeop_p)(void *);
1020 long numvnodes, freevnodes;
1021 long inactivevnodes;
1022
1023
1024 /*
1025 * Move a vnode from one mount queue to another.
1026 */
1027 static void
1028 insmntque(vnode_t vp, mount_t mp)
1029 {
1030 mount_t lmp;
1031 /*
1032 * Delete from old mount point vnode list, if on one.
1033 */
1034 if ( (lmp = vp->v_mount) != NULL && lmp != dead_mountp) {
1035 if ((vp->v_lflag & VNAMED_MOUNT) == 0)
1036 panic("insmntque: vp not in mount vnode list");
1037 vp->v_lflag &= ~VNAMED_MOUNT;
1038
1039 mount_lock(lmp);
1040
1041 mount_drop(lmp, 1);
1042
1043 if (vp->v_mntvnodes.tqe_next == NULL) {
1044 if (TAILQ_LAST(&lmp->mnt_vnodelist, vnodelst) == vp)
1045 TAILQ_REMOVE(&lmp->mnt_vnodelist, vp, v_mntvnodes);
1046 else if (TAILQ_LAST(&lmp->mnt_newvnodes, vnodelst) == vp)
1047 TAILQ_REMOVE(&lmp->mnt_newvnodes, vp, v_mntvnodes);
1048 else if (TAILQ_LAST(&lmp->mnt_workerqueue, vnodelst) == vp)
1049 TAILQ_REMOVE(&lmp->mnt_workerqueue, vp, v_mntvnodes);
1050 } else {
1051 vp->v_mntvnodes.tqe_next->v_mntvnodes.tqe_prev = vp->v_mntvnodes.tqe_prev;
1052 *vp->v_mntvnodes.tqe_prev = vp->v_mntvnodes.tqe_next;
1053 }
1054 vp->v_mntvnodes.tqe_next = 0;
1055 vp->v_mntvnodes.tqe_prev = 0;
1056 mount_unlock(lmp);
1057 return;
1058 }
1059
1060 /*
1061 * Insert into list of vnodes for the new mount point, if available.
1062 */
1063 if ((vp->v_mount = mp) != NULL) {
1064 mount_lock(mp);
1065 if ((vp->v_mntvnodes.tqe_next != 0) && (vp->v_mntvnodes.tqe_prev != 0))
1066 panic("vp already in mount list");
1067 if (mp->mnt_lflag & MNT_LITER)
1068 TAILQ_INSERT_HEAD(&mp->mnt_newvnodes, vp, v_mntvnodes);
1069 else
1070 TAILQ_INSERT_HEAD(&mp->mnt_vnodelist, vp, v_mntvnodes);
1071 if (vp->v_lflag & VNAMED_MOUNT)
1072 panic("insmntque: vp already in mount vnode list");
1073 if ((vp->v_freelist.tqe_prev != (struct vnode **)0xdeadb))
1074 panic("insmntque: vp on the free list\n");
1075 vp->v_lflag |= VNAMED_MOUNT;
1076 mount_ref(mp, 1);
1077 mount_unlock(mp);
1078 }
1079 }
1080
1081
1082 /*
1083 * Create a vnode for a block device.
1084 * Used for root filesystem, argdev, and swap areas.
1085 * Also used for memory file system special devices.
1086 */
1087 int
1088 bdevvp(dev_t dev, vnode_t *vpp)
1089 {
1090 vnode_t nvp;
1091 int error;
1092 struct vnode_fsparam vfsp;
1093 struct vfs_context context;
1094
1095 if (dev == NODEV) {
1096 *vpp = NULLVP;
1097 return (ENODEV);
1098 }
1099
1100 context.vc_proc = current_proc();
1101 context.vc_ucred = FSCRED;
1102
1103 vfsp.vnfs_mp = (struct mount *)0;
1104 vfsp.vnfs_vtype = VBLK;
1105 vfsp.vnfs_str = "bdevvp";
1106 vfsp.vnfs_dvp = 0;
1107 vfsp.vnfs_fsnode = 0;
1108 vfsp.vnfs_cnp = 0;
1109 vfsp.vnfs_vops = spec_vnodeop_p;
1110 vfsp.vnfs_rdev = dev;
1111 vfsp.vnfs_filesize = 0;
1112
1113 vfsp.vnfs_flags = VNFS_NOCACHE | VNFS_CANTCACHE;
1114
1115 vfsp.vnfs_marksystem = 0;
1116 vfsp.vnfs_markroot = 0;
1117
1118 if ( (error = vnode_create(VNCREATE_FLAVOR, VCREATESIZE, &vfsp, &nvp)) ) {
1119 *vpp = NULLVP;
1120 return (error);
1121 }
1122 if ( (error = vnode_ref(nvp)) ) {
1123 panic("bdevvp failed: vnode_ref");
1124 return (error);
1125 }
1126 if ( (error = VNOP_FSYNC(nvp, MNT_WAIT, &context)) ) {
1127 panic("bdevvp failed: fsync");
1128 return (error);
1129 }
1130 if ( (error = buf_invalidateblks(nvp, BUF_WRITE_DATA, 0, 0)) ) {
1131 panic("bdevvp failed: invalidateblks");
1132 return (error);
1133 }
1134 if ( (error = VNOP_OPEN(nvp, FREAD, &context)) ) {
1135 panic("bdevvp failed: open");
1136 return (error);
1137 }
1138 *vpp = nvp;
1139
1140 return (0);
1141 }
1142
1143 /*
1144 * Check to see if the new vnode represents a special device
1145 * for which we already have a vnode (either because of
1146 * bdevvp() or because of a different vnode representing
1147 * the same block device). If such an alias exists, deallocate
1148 * the existing contents and return the aliased vnode. The
1149 * caller is responsible for filling it with its new contents.
1150 */
1151 static vnode_t
1152 checkalias(nvp, nvp_rdev)
1153 register struct vnode *nvp;
1154 dev_t nvp_rdev;
1155 {
1156 struct vnode *vp;
1157 struct vnode **vpp;
1158 int vid = 0;
1159
1160 vpp = &speclisth[SPECHASH(nvp_rdev)];
1161 loop:
1162 SPECHASH_LOCK();
1163
1164 for (vp = *vpp; vp; vp = vp->v_specnext) {
1165 if (nvp_rdev == vp->v_rdev && nvp->v_type == vp->v_type) {
1166 vid = vp->v_id;
1167 break;
1168 }
1169 }
1170 SPECHASH_UNLOCK();
1171
1172 if (vp) {
1173 if (vnode_getwithvid(vp,vid)) {
1174 goto loop;
1175 }
1176 /*
1177 * Termination state is checked in vnode_getwithvid
1178 */
1179 vnode_lock(vp);
1180
1181 /*
1182 * Alias, but not in use, so flush it out.
1183 */
1184 if ((vp->v_iocount == 1) && (vp->v_usecount == 0)) {
1185 vnode_reclaim_internal(vp, 1, 0);
1186 vnode_unlock(vp);
1187 vnode_put(vp);
1188 goto loop;
1189 }
1190 }
1191 if (vp == NULL || vp->v_tag != VT_NON) {
1192 MALLOC_ZONE(nvp->v_specinfo, struct specinfo *, sizeof(struct specinfo),
1193 M_SPECINFO, M_WAITOK);
1194 bzero(nvp->v_specinfo, sizeof(struct specinfo));
1195 nvp->v_rdev = nvp_rdev;
1196 nvp->v_specflags = 0;
1197 nvp->v_speclastr = -1;
1198
1199 SPECHASH_LOCK();
1200 nvp->v_hashchain = vpp;
1201 nvp->v_specnext = *vpp;
1202 *vpp = nvp;
1203 SPECHASH_UNLOCK();
1204
1205 if (vp != NULLVP) {
1206 nvp->v_flag |= VALIASED;
1207 vp->v_flag |= VALIASED;
1208 vnode_unlock(vp);
1209 vnode_put(vp);
1210 }
1211 return (NULLVP);
1212 }
1213 return (vp);
1214 }
1215
1216
1217 /*
1218 * Get a reference on a particular vnode and lock it if requested.
1219 * If the vnode was on the inactive list, remove it from the list.
1220 * If the vnode was on the free list, remove it from the list and
1221 * move it to inactive list as needed.
1222 * The vnode lock bit is set if the vnode is being eliminated in
1223 * vgone. The process is awakened when the transition is completed,
1224 * and an error returned to indicate that the vnode is no longer
1225 * usable (possibly having been changed to a new file system type).
1226 */
1227 static int
1228 vget_internal(vnode_t vp, int vid, int vflags)
1229 {
1230 int error = 0;
1231 u_long vpid;
1232
1233 vnode_lock(vp);
1234
1235 if (vflags & VNODE_WITHID)
1236 vpid = vid;
1237 else
1238 vpid = vp->v_id; // save off the original v_id
1239
1240 if ((vflags & VNODE_WRITEABLE) && (vp->v_writecount == 0))
1241 /*
1242 * vnode to be returned only if it has writers opened
1243 */
1244 error = EINVAL;
1245 else
1246 error = vnode_getiocount(vp, 1, vpid, vflags);
1247
1248 vnode_unlock(vp);
1249
1250 return (error);
1251 }
1252
1253 int
1254 vnode_ref(vnode_t vp)
1255 {
1256
1257 return (vnode_ref_ext(vp, 0));
1258 }
1259
1260 int
1261 vnode_ref_ext(vnode_t vp, int fmode)
1262 {
1263 int error = 0;
1264
1265 vnode_lock(vp);
1266
1267 /*
1268 * once all the current call sites have been fixed to insure they have
1269 * taken an iocount, we can toughen this assert up and insist that the
1270 * iocount is non-zero... a non-zero usecount doesn't insure correctness
1271 */
1272 if (vp->v_iocount <= 0 && vp->v_usecount <= 0)
1273 panic("vnode_ref_ext: vp %x has no valid reference %d, %d", vp, vp->v_iocount, vp->v_usecount);
1274
1275 /*
1276 * if you are the owner of drain/termination, can acquire usecount
1277 */
1278 if ((vp->v_lflag & (VL_DRAIN | VL_TERMINATE | VL_DEAD))) {
1279 if (vp->v_owner != current_thread()) {
1280 error = ENOENT;
1281 goto out;
1282 }
1283 }
1284 vp->v_usecount++;
1285
1286 if (fmode & FWRITE) {
1287 if (++vp->v_writecount <= 0)
1288 panic("vnode_ref_ext: v_writecount");
1289 }
1290 if (fmode & O_EVTONLY) {
1291 if (++vp->v_kusecount <= 0)
1292 panic("vnode_ref_ext: v_kusecount");
1293 }
1294 out:
1295 vnode_unlock(vp);
1296
1297 return (error);
1298 }
1299
1300
1301 /*
1302 * put the vnode on appropriate free list.
1303 * called with vnode LOCKED
1304 */
1305 static void
1306 vnode_list_add(vnode_t vp)
1307 {
1308
1309 /*
1310 * if it is already on a list or non zero references return
1311 */
1312 if (VONLIST(vp) || (vp->v_usecount != 0) || (vp->v_iocount != 0))
1313 return;
1314 vnode_list_lock();
1315
1316 /*
1317 * insert at tail of LRU list or at head if VAGE or VL_DEAD is set
1318 */
1319 if ((vp->v_flag & VAGE) || (vp->v_lflag & VL_DEAD)) {
1320 TAILQ_INSERT_HEAD(&vnode_free_list, vp, v_freelist);
1321 vp->v_flag &= ~VAGE;
1322 } else {
1323 TAILQ_INSERT_TAIL(&vnode_free_list, vp, v_freelist);
1324 }
1325 freevnodes++;
1326
1327 vnode_list_unlock();
1328 }
1329
1330 /*
1331 * remove the vnode from appropriate free list.
1332 */
1333 static void
1334 vnode_list_remove(vnode_t vp)
1335 {
1336 /*
1337 * we want to avoid taking the list lock
1338 * in the case where we're not on the free
1339 * list... this will be true for most
1340 * directories and any currently in use files
1341 *
1342 * we're guaranteed that we can't go from
1343 * the not-on-list state to the on-list
1344 * state since we hold the vnode lock...
1345 * all calls to vnode_list_add are done
1346 * under the vnode lock... so we can
1347 * check for that condition (the prevelant one)
1348 * without taking the list lock
1349 */
1350 if (VONLIST(vp)) {
1351 vnode_list_lock();
1352 /*
1353 * however, we're not guaranteed that
1354 * we won't go from the on-list state
1355 * to the non-on-list state until we
1356 * hold the vnode_list_lock... this
1357 * is due to new_vnode removing vnodes
1358 * from the free list uder the list_lock
1359 * w/o the vnode lock... so we need to
1360 * check again whether we're currently
1361 * on the free list
1362 */
1363 if (VONLIST(vp)) {
1364 VREMFREE("vnode_list_remove", vp);
1365 VLISTNONE(vp);
1366 }
1367 vnode_list_unlock();
1368 }
1369 }
1370
1371
1372 void
1373 vnode_rele(vnode_t vp)
1374 {
1375 vnode_rele_internal(vp, 0, 0, 0);
1376 }
1377
1378
1379 void
1380 vnode_rele_ext(vnode_t vp, int fmode, int dont_reenter)
1381 {
1382 vnode_rele_internal(vp, fmode, dont_reenter, 0);
1383 }
1384
1385
1386 void
1387 vnode_rele_internal(vnode_t vp, int fmode, int dont_reenter, int locked)
1388 {
1389 struct vfs_context context;
1390
1391 if ( !locked)
1392 vnode_lock(vp);
1393
1394 if (--vp->v_usecount < 0)
1395 panic("vnode_rele_ext: vp %x usecount -ve : %d", vp, vp->v_usecount);
1396
1397 if (fmode & FWRITE) {
1398 if (--vp->v_writecount < 0)
1399 panic("vnode_rele_ext: vp %x writecount -ve : %d", vp, vp->v_writecount);
1400 }
1401 if (fmode & O_EVTONLY) {
1402 if (--vp->v_kusecount < 0)
1403 panic("vnode_rele_ext: vp %x kusecount -ve : %d", vp, vp->v_kusecount);
1404 }
1405 if ((vp->v_iocount > 0) || (vp->v_usecount > 0)) {
1406 /*
1407 * vnode is still busy... if we're the last
1408 * usecount, mark for a future call to VNOP_INACTIVE
1409 * when the iocount finally drops to 0
1410 */
1411 if (vp->v_usecount == 0) {
1412 vp->v_lflag |= VL_NEEDINACTIVE;
1413 vp->v_flag &= ~(VNOCACHE_DATA | VRAOFF);
1414 }
1415 if ( !locked)
1416 vnode_unlock(vp);
1417 return;
1418 }
1419 vp->v_flag &= ~(VNOCACHE_DATA | VRAOFF);
1420
1421 if ( (vp->v_lflag & (VL_TERMINATE | VL_DEAD)) || dont_reenter) {
1422 /*
1423 * vnode is being cleaned, or
1424 * we've requested that we don't reenter
1425 * the filesystem on this release... in
1426 * this case, we'll mark the vnode aged
1427 * if it's been marked for termination
1428 */
1429 if (dont_reenter) {
1430 if ( !(vp->v_lflag & (VL_TERMINATE | VL_DEAD | VL_MARKTERM)) )
1431 vp->v_lflag |= VL_NEEDINACTIVE;
1432 vp->v_flag |= VAGE;
1433 }
1434 vnode_list_add(vp);
1435 if ( !locked)
1436 vnode_unlock(vp);
1437 return;
1438 }
1439 /*
1440 * at this point both the iocount and usecount
1441 * are zero
1442 * pick up an iocount so that we can call
1443 * VNOP_INACTIVE with the vnode lock unheld
1444 */
1445 vp->v_iocount++;
1446 #ifdef JOE_DEBUG
1447 record_vp(vp, 1);
1448 #endif
1449 vp->v_lflag &= ~VL_NEEDINACTIVE;
1450 vnode_unlock(vp);
1451
1452 context.vc_proc = current_proc();
1453 context.vc_ucred = kauth_cred_get();
1454 VNOP_INACTIVE(vp, &context);
1455
1456 vnode_lock(vp);
1457 /*
1458 * because we dropped the vnode lock to call VNOP_INACTIVE
1459 * the state of the vnode may have changed... we may have
1460 * picked up an iocount, usecount or the MARKTERM may have
1461 * been set... we need to reevaluate the reference counts
1462 * to determine if we can call vnode_reclaim_internal at
1463 * this point... if the reference counts are up, we'll pick
1464 * up the MARKTERM state when they get subsequently dropped
1465 */
1466 if ( (vp->v_iocount == 1) && (vp->v_usecount == 0) &&
1467 ((vp->v_lflag & (VL_MARKTERM | VL_TERMINATE | VL_DEAD)) == VL_MARKTERM)) {
1468 struct uthread *ut;
1469
1470 ut = get_bsdthread_info(current_thread());
1471
1472 if (ut->uu_defer_reclaims) {
1473 vp->v_defer_reclaimlist = ut->uu_vreclaims;
1474 ut->uu_vreclaims = vp;
1475 goto defer_reclaim;
1476 }
1477 vnode_reclaim_internal(vp, 1, 0);
1478 }
1479 vnode_dropiocount(vp, 1);
1480 vnode_list_add(vp);
1481 defer_reclaim:
1482 if ( !locked)
1483 vnode_unlock(vp);
1484 return;
1485 }
1486
1487 /*
1488 * Remove any vnodes in the vnode table belonging to mount point mp.
1489 *
1490 * If MNT_NOFORCE is specified, there should not be any active ones,
1491 * return error if any are found (nb: this is a user error, not a
1492 * system error). If MNT_FORCE is specified, detach any active vnodes
1493 * that are found.
1494 */
1495 #if DIAGNOSTIC
1496 int busyprt = 0; /* print out busy vnodes */
1497 #if 0
1498 struct ctldebug debug1 = { "busyprt", &busyprt };
1499 #endif /* 0 */
1500 #endif
1501
1502 int
1503 vflush(mp, skipvp, flags)
1504 struct mount *mp;
1505 struct vnode *skipvp;
1506 int flags;
1507 {
1508 struct proc *p = current_proc();
1509 struct vnode *vp;
1510 int busy = 0;
1511 int reclaimed = 0;
1512 int vid, retval;
1513
1514 mount_lock(mp);
1515 vnode_iterate_setup(mp);
1516 /*
1517 * On regular unmounts(not forced) do a
1518 * quick check for vnodes to be in use. This
1519 * preserves the caching of vnodes. automounter
1520 * tries unmounting every so often to see whether
1521 * it is still busy or not.
1522 */
1523 if ((flags & FORCECLOSE)==0) {
1524 if (vnode_umount_preflight(mp, skipvp, flags)) {
1525 vnode_iterate_clear(mp);
1526 mount_unlock(mp);
1527 return(EBUSY);
1528 }
1529 }
1530 loop:
1531 /* it is returns 0 then there is nothing to do */
1532 retval = vnode_iterate_prepare(mp);
1533
1534 if (retval == 0) {
1535 vnode_iterate_clear(mp);
1536 mount_unlock(mp);
1537 return(retval);
1538 }
1539
1540 /* iterate over all the vnodes */
1541 while (!TAILQ_EMPTY(&mp->mnt_workerqueue)) {
1542 vp = TAILQ_FIRST(&mp->mnt_workerqueue);
1543 TAILQ_REMOVE(&mp->mnt_workerqueue, vp, v_mntvnodes);
1544 TAILQ_INSERT_TAIL(&mp->mnt_vnodelist, vp, v_mntvnodes);
1545 if ( (vp->v_mount != mp) || (vp == skipvp)) {
1546 continue;
1547 }
1548 vid = vp->v_id;
1549 mount_unlock(mp);
1550 vnode_lock(vp);
1551
1552 if ((vp->v_id != vid) || ((vp->v_lflag & (VL_DEAD | VL_TERMINATE)))) {
1553 vnode_unlock(vp);
1554 mount_lock(mp);
1555 continue;
1556 }
1557
1558 /*
1559 * If requested, skip over vnodes marked VSYSTEM.
1560 * Skip over all vnodes marked VNOFLUSH.
1561 */
1562 if ((flags & SKIPSYSTEM) && ((vp->v_flag & VSYSTEM) ||
1563 (vp->v_flag & VNOFLUSH))) {
1564 vnode_unlock(vp);
1565 mount_lock(mp);
1566 continue;
1567 }
1568 /*
1569 * If requested, skip over vnodes marked VSWAP.
1570 */
1571 if ((flags & SKIPSWAP) && (vp->v_flag & VSWAP)) {
1572 vnode_unlock(vp);
1573 mount_lock(mp);
1574 continue;
1575 }
1576 /*
1577 * If requested, skip over vnodes marked VSWAP.
1578 */
1579 if ((flags & SKIPROOT) && (vp->v_flag & VROOT)) {
1580 vnode_unlock(vp);
1581 mount_lock(mp);
1582 continue;
1583 }
1584 /*
1585 * If WRITECLOSE is set, only flush out regular file
1586 * vnodes open for writing.
1587 */
1588 if ((flags & WRITECLOSE) &&
1589 (vp->v_writecount == 0 || vp->v_type != VREG)) {
1590 vnode_unlock(vp);
1591 mount_lock(mp);
1592 continue;
1593 }
1594 /*
1595 * If the real usecount is 0, all we need to do is clear
1596 * out the vnode data structures and we are done.
1597 */
1598 if (((vp->v_usecount == 0) ||
1599 ((vp->v_usecount - vp->v_kusecount) == 0))) {
1600 vp->v_iocount++; /* so that drain waits for * other iocounts */
1601 #ifdef JOE_DEBUG
1602 record_vp(vp, 1);
1603 #endif
1604 vnode_reclaim_internal(vp, 1, 0);
1605 vnode_dropiocount(vp, 1);
1606 vnode_list_add(vp);
1607
1608 vnode_unlock(vp);
1609 reclaimed++;
1610 mount_lock(mp);
1611 continue;
1612 }
1613 /*
1614 * If FORCECLOSE is set, forcibly close the vnode.
1615 * For block or character devices, revert to an
1616 * anonymous device. For all other files, just kill them.
1617 */
1618 if (flags & FORCECLOSE) {
1619 if (vp->v_type != VBLK && vp->v_type != VCHR) {
1620 vp->v_iocount++; /* so that drain waits * for other iocounts */
1621 #ifdef JOE_DEBUG
1622 record_vp(vp, 1);
1623 #endif
1624 vnode_reclaim_internal(vp, 1, 0);
1625 vnode_dropiocount(vp, 1);
1626 vnode_list_add(vp);
1627 vnode_unlock(vp);
1628 } else {
1629 vclean(vp, 0, p);
1630 vp->v_lflag &= ~VL_DEAD;
1631 vp->v_op = spec_vnodeop_p;
1632 vnode_unlock(vp);
1633 }
1634 mount_lock(mp);
1635 continue;
1636 }
1637 #if DIAGNOSTIC
1638 if (busyprt)
1639 vprint("vflush: busy vnode", vp);
1640 #endif
1641 vnode_unlock(vp);
1642 mount_lock(mp);
1643 busy++;
1644 }
1645
1646 /* At this point the worker queue is completed */
1647 if (busy && ((flags & FORCECLOSE)==0) && reclaimed) {
1648 busy = 0;
1649 reclaimed = 0;
1650 (void)vnode_iterate_reloadq(mp);
1651 /* returned with mount lock held */
1652 goto loop;
1653 }
1654
1655 /* if new vnodes were created in between retry the reclaim */
1656 if ( vnode_iterate_reloadq(mp) != 0) {
1657 if (!(busy && ((flags & FORCECLOSE)==0)))
1658 goto loop;
1659 }
1660 vnode_iterate_clear(mp);
1661 mount_unlock(mp);
1662
1663 if (busy && ((flags & FORCECLOSE)==0))
1664 return (EBUSY);
1665 return (0);
1666 }
1667
1668 int num_recycledvnodes=0;
1669 /*
1670 * Disassociate the underlying file system from a vnode.
1671 * The vnode lock is held on entry.
1672 */
1673 static void
1674 vclean(vnode_t vp, int flags, proc_t p)
1675 {
1676 struct vfs_context context;
1677 int active;
1678 int need_inactive;
1679 int already_terminating;
1680 kauth_cred_t ucred = NULL;
1681
1682 context.vc_proc = p;
1683 context.vc_ucred = kauth_cred_get();
1684
1685 /*
1686 * Check to see if the vnode is in use.
1687 * If so we have to reference it before we clean it out
1688 * so that its count cannot fall to zero and generate a
1689 * race against ourselves to recycle it.
1690 */
1691 active = vp->v_usecount;
1692
1693 /*
1694 * just in case we missed sending a needed
1695 * VNOP_INACTIVE, we'll do it now
1696 */
1697 need_inactive = (vp->v_lflag & VL_NEEDINACTIVE);
1698
1699 vp->v_lflag &= ~VL_NEEDINACTIVE;
1700
1701 /*
1702 * Prevent the vnode from being recycled or
1703 * brought into use while we clean it out.
1704 */
1705 already_terminating = (vp->v_lflag & VL_TERMINATE);
1706
1707 vp->v_lflag |= VL_TERMINATE;
1708
1709 /*
1710 * remove the vnode from any mount list
1711 * it might be on...
1712 */
1713 insmntque(vp, (struct mount *)0);
1714
1715 ucred = vp->v_cred;
1716 vp->v_cred = NULL;
1717
1718 vnode_unlock(vp);
1719
1720 if (ucred)
1721 kauth_cred_rele(ucred);
1722
1723 OSAddAtomic(1, &num_recycledvnodes);
1724 /*
1725 * purge from the name cache as early as possible...
1726 */
1727 cache_purge(vp);
1728
1729 if (active && (flags & DOCLOSE))
1730 VNOP_CLOSE(vp, IO_NDELAY, &context);
1731
1732 /*
1733 * Clean out any buffers associated with the vnode.
1734 */
1735 if (flags & DOCLOSE) {
1736 #if NFSCLIENT
1737 if (vp->v_tag == VT_NFS)
1738 nfs_vinvalbuf(vp, V_SAVE, NOCRED, p, 0);
1739 else
1740 #endif
1741 {
1742 VNOP_FSYNC(vp, MNT_WAIT, &context);
1743 buf_invalidateblks(vp, BUF_WRITE_DATA, 0, 0);
1744 }
1745 if (UBCINFOEXISTS(vp))
1746 /*
1747 * Clean the pages in VM.
1748 */
1749 (void)ubc_sync_range(vp, (off_t)0, ubc_getsize(vp), UBC_PUSHALL);
1750 }
1751 if (UBCINFOEXISTS(vp))
1752 cluster_release(vp->v_ubcinfo);
1753
1754 if (active || need_inactive)
1755 VNOP_INACTIVE(vp, &context);
1756
1757 /* Destroy ubc named reference */
1758 ubc_destroy_named(vp);
1759
1760 /*
1761 * Reclaim the vnode.
1762 */
1763 if (VNOP_RECLAIM(vp, &context))
1764 panic("vclean: cannot reclaim");
1765
1766 // make sure the name & parent ptrs get cleaned out!
1767 vnode_update_identity(vp, NULLVP, NULL, 0, 0, VNODE_UPDATE_PARENT | VNODE_UPDATE_NAME);
1768
1769 vnode_lock(vp);
1770
1771 vp->v_mount = dead_mountp;
1772 vp->v_op = dead_vnodeop_p;
1773 vp->v_tag = VT_NON;
1774 vp->v_data = NULL;
1775
1776 vp->v_lflag |= VL_DEAD;
1777
1778 if (already_terminating == 0) {
1779 vp->v_lflag &= ~VL_TERMINATE;
1780 /*
1781 * Done with purge, notify sleepers of the grim news.
1782 */
1783 if (vp->v_lflag & VL_TERMWANT) {
1784 vp->v_lflag &= ~VL_TERMWANT;
1785 wakeup(&vp->v_lflag);
1786 }
1787 }
1788 }
1789
1790 /*
1791 * Eliminate all activity associated with the requested vnode
1792 * and with all vnodes aliased to the requested vnode.
1793 */
1794 int
1795 vn_revoke(vnode_t vp, int flags, __unused vfs_context_t a_context)
1796 {
1797 struct vnode *vq;
1798 int vid;
1799
1800 #if DIAGNOSTIC
1801 if ((flags & REVOKEALL) == 0)
1802 panic("vnop_revoke");
1803 #endif
1804
1805 if (vp->v_flag & VALIASED) {
1806 /*
1807 * If a vgone (or vclean) is already in progress,
1808 * wait until it is done and return.
1809 */
1810 vnode_lock(vp);
1811 if (vp->v_lflag & VL_TERMINATE) {
1812 vnode_unlock(vp);
1813 return(ENOENT);
1814 }
1815 vnode_unlock(vp);
1816 /*
1817 * Ensure that vp will not be vgone'd while we
1818 * are eliminating its aliases.
1819 */
1820 SPECHASH_LOCK();
1821 while (vp->v_flag & VALIASED) {
1822 for (vq = *vp->v_hashchain; vq; vq = vq->v_specnext) {
1823 if (vq->v_rdev != vp->v_rdev ||
1824 vq->v_type != vp->v_type || vp == vq)
1825 continue;
1826 vid = vq->v_id;
1827 SPECHASH_UNLOCK();
1828 if (vnode_getwithvid(vq,vid)){
1829 SPECHASH_LOCK();
1830 break;
1831 }
1832 vnode_reclaim_internal(vq, 0, 0);
1833 vnode_put(vq);
1834 SPECHASH_LOCK();
1835 break;
1836 }
1837 }
1838 SPECHASH_UNLOCK();
1839 }
1840 vnode_reclaim_internal(vp, 0, 0);
1841
1842 return (0);
1843 }
1844
1845 /*
1846 * Recycle an unused vnode to the front of the free list.
1847 * Release the passed interlock if the vnode will be recycled.
1848 */
1849 int
1850 vnode_recycle(vp)
1851 struct vnode *vp;
1852 {
1853 vnode_lock(vp);
1854
1855 if (vp->v_iocount || vp->v_usecount) {
1856 vp->v_lflag |= VL_MARKTERM;
1857 vnode_unlock(vp);
1858 return(0);
1859 }
1860 vnode_reclaim_internal(vp, 1, 0);
1861 vnode_unlock(vp);
1862
1863 return (1);
1864 }
1865
1866 static int
1867 vnode_reload(vnode_t vp)
1868 {
1869 vnode_lock(vp);
1870
1871 if ((vp->v_iocount > 1) || vp->v_usecount) {
1872 vnode_unlock(vp);
1873 return(0);
1874 }
1875 if (vp->v_iocount <= 0)
1876 panic("vnode_reload with no iocount %d", vp->v_iocount);
1877
1878 /* mark for release when iocount is dopped */
1879 vp->v_lflag |= VL_MARKTERM;
1880 vnode_unlock(vp);
1881
1882 return (1);
1883 }
1884
1885
1886 static void
1887 vgone(vnode_t vp)
1888 {
1889 struct vnode *vq;
1890 struct vnode *vx;
1891
1892 /*
1893 * Clean out the filesystem specific data.
1894 * vclean also takes care of removing the
1895 * vnode from any mount list it might be on
1896 */
1897 vclean(vp, DOCLOSE, current_proc());
1898
1899 /*
1900 * If special device, remove it from special device alias list
1901 * if it is on one.
1902 */
1903 if ((vp->v_type == VBLK || vp->v_type == VCHR) && vp->v_specinfo != 0) {
1904 SPECHASH_LOCK();
1905 if (*vp->v_hashchain == vp) {
1906 *vp->v_hashchain = vp->v_specnext;
1907 } else {
1908 for (vq = *vp->v_hashchain; vq; vq = vq->v_specnext) {
1909 if (vq->v_specnext != vp)
1910 continue;
1911 vq->v_specnext = vp->v_specnext;
1912 break;
1913 }
1914 if (vq == NULL)
1915 panic("missing bdev");
1916 }
1917 if (vp->v_flag & VALIASED) {
1918 vx = NULL;
1919 for (vq = *vp->v_hashchain; vq; vq = vq->v_specnext) {
1920 if (vq->v_rdev != vp->v_rdev ||
1921 vq->v_type != vp->v_type)
1922 continue;
1923 if (vx)
1924 break;
1925 vx = vq;
1926 }
1927 if (vx == NULL)
1928 panic("missing alias");
1929 if (vq == NULL)
1930 vx->v_flag &= ~VALIASED;
1931 vp->v_flag &= ~VALIASED;
1932 }
1933 SPECHASH_UNLOCK();
1934 {
1935 struct specinfo *tmp = vp->v_specinfo;
1936 vp->v_specinfo = NULL;
1937 FREE_ZONE((void *)tmp, sizeof(struct specinfo), M_SPECINFO);
1938 }
1939 }
1940 }
1941
1942 /*
1943 * Lookup a vnode by device number.
1944 */
1945 int
1946 check_mountedon(dev_t dev, enum vtype type, int *errorp)
1947 {
1948 vnode_t vp;
1949 int rc = 0;
1950 int vid;
1951
1952 loop:
1953 SPECHASH_LOCK();
1954 for (vp = speclisth[SPECHASH(dev)]; vp; vp = vp->v_specnext) {
1955 if (dev != vp->v_rdev || type != vp->v_type)
1956 continue;
1957 vid = vp->v_id;
1958 SPECHASH_UNLOCK();
1959 if (vnode_getwithvid(vp,vid))
1960 goto loop;
1961 vnode_lock(vp);
1962 if ((vp->v_usecount > 0) || (vp->v_iocount > 1)) {
1963 vnode_unlock(vp);
1964 if ((*errorp = vfs_mountedon(vp)) != 0)
1965 rc = 1;
1966 } else
1967 vnode_unlock(vp);
1968 vnode_put(vp);
1969 return(rc);
1970 }
1971 SPECHASH_UNLOCK();
1972 return (0);
1973 }
1974
1975 /*
1976 * Calculate the total number of references to a special device.
1977 */
1978 int
1979 vcount(vnode_t vp)
1980 {
1981 vnode_t vq, vnext;
1982 int count;
1983 int vid;
1984
1985 loop:
1986 if ((vp->v_flag & VALIASED) == 0)
1987 return (vp->v_usecount - vp->v_kusecount);
1988
1989 SPECHASH_LOCK();
1990 for (count = 0, vq = *vp->v_hashchain; vq; vq = vnext) {
1991 vnext = vq->v_specnext;
1992 if (vq->v_rdev != vp->v_rdev || vq->v_type != vp->v_type)
1993 continue;
1994 vid = vq->v_id;
1995 SPECHASH_UNLOCK();
1996
1997 if (vnode_getwithvid(vq, vid)) {
1998 goto loop;
1999 }
2000 /*
2001 * Alias, but not in use, so flush it out.
2002 */
2003 vnode_lock(vq);
2004 if ((vq->v_usecount == 0) && (vq->v_iocount == 1) && vq != vp) {
2005 vnode_reclaim_internal(vq, 1, 0);
2006 vnode_unlock(vq);
2007 vnode_put(vq);
2008 goto loop;
2009 }
2010 count += (vq->v_usecount - vq->v_kusecount);
2011 vnode_unlock(vq);
2012 vnode_put(vq);
2013
2014 SPECHASH_LOCK();
2015 }
2016 SPECHASH_UNLOCK();
2017
2018 return (count);
2019 }
2020
2021 int prtactive = 0; /* 1 => print out reclaim of active vnodes */
2022
2023 /*
2024 * Print out a description of a vnode.
2025 */
2026 static char *typename[] =
2027 { "VNON", "VREG", "VDIR", "VBLK", "VCHR", "VLNK", "VSOCK", "VFIFO", "VBAD" };
2028
2029 void
2030 vprint(const char *label, struct vnode *vp)
2031 {
2032 char sbuf[64];
2033
2034 if (label != NULL)
2035 printf("%s: ", label);
2036 printf("type %s, usecount %d, writecount %d",
2037 typename[vp->v_type], vp->v_usecount, vp->v_writecount);
2038 sbuf[0] = '\0';
2039 if (vp->v_flag & VROOT)
2040 strcat(sbuf, "|VROOT");
2041 if (vp->v_flag & VTEXT)
2042 strcat(sbuf, "|VTEXT");
2043 if (vp->v_flag & VSYSTEM)
2044 strcat(sbuf, "|VSYSTEM");
2045 if (vp->v_flag & VNOFLUSH)
2046 strcat(sbuf, "|VNOFLUSH");
2047 if (vp->v_flag & VBWAIT)
2048 strcat(sbuf, "|VBWAIT");
2049 if (vp->v_flag & VALIASED)
2050 strcat(sbuf, "|VALIASED");
2051 if (sbuf[0] != '\0')
2052 printf(" flags (%s)", &sbuf[1]);
2053 }
2054
2055
2056 int
2057 vn_getpath(struct vnode *vp, char *pathbuf, int *len)
2058 {
2059 return build_path(vp, pathbuf, *len, len);
2060 }
2061
2062
2063 static char *extension_table=NULL;
2064 static int nexts;
2065 static int max_ext_width;
2066
2067 static int
2068 extension_cmp(void *a, void *b)
2069 {
2070 return (strlen((char *)a) - strlen((char *)b));
2071 }
2072
2073
2074 //
2075 // This is the api LaunchServices uses to inform the kernel
2076 // the list of package extensions to ignore.
2077 //
2078 // Internally we keep the list sorted by the length of the
2079 // the extension (from longest to shortest). We sort the
2080 // list of extensions so that we can speed up our searches
2081 // when comparing file names -- we only compare extensions
2082 // that could possibly fit into the file name, not all of
2083 // them (i.e. a short 8 character name can't have an 8
2084 // character extension).
2085 //
2086 __private_extern__ int
2087 set_package_extensions_table(void *data, int nentries, int maxwidth)
2088 {
2089 char *new_exts, *ptr;
2090 int error, i, len;
2091
2092 if (nentries <= 0 || nentries > 1024 || maxwidth <= 0 || maxwidth > 255) {
2093 return EINVAL;
2094 }
2095
2096 MALLOC(new_exts, char *, nentries * maxwidth, M_TEMP, M_WAITOK);
2097
2098 error = copyin(CAST_USER_ADDR_T(data), new_exts, nentries * maxwidth);
2099 if (error) {
2100 FREE(new_exts, M_TEMP);
2101 return error;
2102 }
2103
2104 if (extension_table) {
2105 FREE(extension_table, M_TEMP);
2106 }
2107 extension_table = new_exts;
2108 nexts = nentries;
2109 max_ext_width = maxwidth;
2110
2111 qsort(extension_table, nexts, maxwidth, extension_cmp);
2112
2113 return 0;
2114 }
2115
2116
2117 __private_extern__ int
2118 is_package_name(char *name, int len)
2119 {
2120 int i, extlen;
2121 char *ptr, *name_ext;
2122
2123 if (len <= 3) {
2124 return 0;
2125 }
2126
2127 name_ext = NULL;
2128 for(ptr=name; *ptr != '\0'; ptr++) {
2129 if (*ptr == '.') {
2130 name_ext = ptr;
2131 }
2132 }
2133
2134 // if there is no "." extension, it can't match
2135 if (name_ext == NULL) {
2136 return 0;
2137 }
2138
2139 // advance over the "."
2140 name_ext++;
2141
2142 // now iterate over all the extensions to see if any match
2143 ptr = &extension_table[0];
2144 for(i=0; i < nexts; i++, ptr+=max_ext_width) {
2145 extlen = strlen(ptr);
2146 if (strncasecmp(name_ext, ptr, extlen) == 0 && name_ext[extlen] == '\0') {
2147 // aha, a match!
2148 return 1;
2149 }
2150 }
2151
2152 // if we get here, no extension matched
2153 return 0;
2154 }
2155
2156 int
2157 vn_path_package_check(__unused vnode_t vp, char *path, int pathlen, int *component)
2158 {
2159 char *ptr, *end;
2160 int comp=0;
2161
2162 *component = -1;
2163 if (*path != '/') {
2164 return EINVAL;
2165 }
2166
2167 end = path + 1;
2168 while(end < path + pathlen && *end != '\0') {
2169 while(end < path + pathlen && *end == '/' && *end != '\0') {
2170 end++;
2171 }
2172
2173 ptr = end;
2174
2175 while(end < path + pathlen && *end != '/' && *end != '\0') {
2176 end++;
2177 }
2178
2179 if (end > path + pathlen) {
2180 // hmm, string wasn't null terminated
2181 return EINVAL;
2182 }
2183
2184 *end = '\0';
2185 if (is_package_name(ptr, end - ptr)) {
2186 *component = comp;
2187 break;
2188 }
2189
2190 end++;
2191 comp++;
2192 }
2193
2194 return 0;
2195 }
2196
2197
2198 /*
2199 * Top level filesystem related information gathering.
2200 */
2201 extern unsigned int vfs_nummntops;
2202
2203 int
2204 vfs_sysctl(int *name, u_int namelen, user_addr_t oldp, size_t *oldlenp,
2205 user_addr_t newp, size_t newlen, struct proc *p)
2206 {
2207 struct vfstable *vfsp;
2208 int *username;
2209 u_int usernamelen;
2210 int error;
2211 struct vfsconf *vfsc;
2212
2213 /*
2214 * The VFS_NUMMNTOPS shouldn't be at name[0] since
2215 * is a VFS generic variable. So now we must check
2216 * namelen so we don't end up covering any UFS
2217 * variables (sinc UFS vfc_typenum is 1).
2218 *
2219 * It should have been:
2220 * name[0]: VFS_GENERIC
2221 * name[1]: VFS_NUMMNTOPS
2222 */
2223 if (namelen == 1 && name[0] == VFS_NUMMNTOPS) {
2224 return (sysctl_rdint(oldp, oldlenp, newp, vfs_nummntops));
2225 }
2226
2227 /* all sysctl names at this level are at least name and field */
2228 if (namelen < 2)
2229 return (EISDIR); /* overloaded */
2230 if (name[0] != VFS_GENERIC) {
2231 struct vfs_context context;
2232
2233 for (vfsp = vfsconf; vfsp; vfsp = vfsp->vfc_next)
2234 if (vfsp->vfc_typenum == name[0])
2235 break;
2236 if (vfsp == NULL)
2237 return (ENOTSUP);
2238 context.vc_proc = p;
2239 context.vc_ucred = kauth_cred_get();
2240
2241 return ((*vfsp->vfc_vfsops->vfs_sysctl)(&name[1], namelen - 1,
2242 oldp, oldlenp, newp, newlen, &context));
2243 }
2244 switch (name[1]) {
2245 case VFS_MAXTYPENUM:
2246 return (sysctl_rdint(oldp, oldlenp, newp, maxvfsconf));
2247 case VFS_CONF:
2248 if (namelen < 3)
2249 return (ENOTDIR); /* overloaded */
2250 for (vfsp = vfsconf; vfsp; vfsp = vfsp->vfc_next)
2251 if (vfsp->vfc_typenum == name[2])
2252 break;
2253 if (vfsp == NULL)
2254 return (ENOTSUP);
2255 vfsc = (struct vfsconf *)vfsp;
2256 if (proc_is64bit(p)) {
2257 struct user_vfsconf usr_vfsc;
2258 usr_vfsc.vfc_vfsops = CAST_USER_ADDR_T(vfsc->vfc_vfsops);
2259 bcopy(vfsc->vfc_name, usr_vfsc.vfc_name, sizeof(usr_vfsc.vfc_name));
2260 usr_vfsc.vfc_typenum = vfsc->vfc_typenum;
2261 usr_vfsc.vfc_refcount = vfsc->vfc_refcount;
2262 usr_vfsc.vfc_flags = vfsc->vfc_flags;
2263 usr_vfsc.vfc_mountroot = CAST_USER_ADDR_T(vfsc->vfc_mountroot);
2264 usr_vfsc.vfc_next = CAST_USER_ADDR_T(vfsc->vfc_next);
2265 return (sysctl_rdstruct(oldp, oldlenp, newp, &usr_vfsc,
2266 sizeof(usr_vfsc)));
2267 }
2268 else {
2269 return (sysctl_rdstruct(oldp, oldlenp, newp, vfsc,
2270 sizeof(struct vfsconf)));
2271 }
2272
2273 case VFS_SET_PACKAGE_EXTS:
2274 return set_package_extensions_table((void *)name[1], name[2], name[3]);
2275 }
2276 /*
2277 * We need to get back into the general MIB, so we need to re-prepend
2278 * CTL_VFS to our name and try userland_sysctl().
2279 */
2280 usernamelen = namelen + 1;
2281 MALLOC(username, int *, usernamelen * sizeof(*username),
2282 M_TEMP, M_WAITOK);
2283 bcopy(name, username + 1, namelen * sizeof(*name));
2284 username[0] = CTL_VFS;
2285 error = userland_sysctl(p, username, usernamelen, oldp,
2286 oldlenp, 1, newp, newlen, oldlenp);
2287 FREE(username, M_TEMP);
2288 return (error);
2289 }
2290
2291 int kinfo_vdebug = 1;
2292 #define KINFO_VNODESLOP 10
2293 /*
2294 * Dump vnode list (via sysctl).
2295 * Copyout address of vnode followed by vnode.
2296 */
2297 /* ARGSUSED */
2298 int
2299 sysctl_vnode(__unused user_addr_t where, __unused size_t *sizep)
2300 {
2301 #if 0
2302 struct mount *mp, *nmp;
2303 struct vnode *nvp, *vp;
2304 char *bp = where, *savebp;
2305 char *ewhere;
2306 int error;
2307
2308 #define VPTRSZ sizeof (struct vnode *)
2309 #define VNODESZ sizeof (struct vnode)
2310 if (where == NULL) {
2311 *sizep = (numvnodes + KINFO_VNODESLOP) * (VPTRSZ + VNODESZ);
2312 return (0);
2313 }
2314 ewhere = where + *sizep;
2315
2316 for (mp = mountlist.cqh_first; mp != (void *)&mountlist; mp = nmp) {
2317 if (vfs_busy(mp, LK_NOWAIT)) {
2318 nmp = mp->mnt_list.cqe_next;
2319 continue;
2320 }
2321 savebp = bp;
2322 again:
2323 TAILQ_FOREACH(vp, &mp->mnt_vnodelist, v_mntvnodes) {
2324 /*
2325 * Check that the vp is still associated with
2326 * this filesystem. RACE: could have been
2327 * recycled onto the same filesystem.
2328 */
2329 if (vp->v_mount != mp) {
2330 if (kinfo_vdebug)
2331 printf("kinfo: vp changed\n");
2332 bp = savebp;
2333 goto again;
2334 }
2335 if (bp + VPTRSZ + VNODESZ > ewhere) {
2336 vfs_unbusy(mp);
2337 *sizep = bp - where;
2338 return (ENOMEM);
2339 }
2340 if ((error = copyout((caddr_t)&vp, bp, VPTRSZ)) ||
2341 (error = copyout((caddr_t)vp, bp + VPTRSZ, VNODESZ))) {
2342 vfs_unbusy(mp);
2343 return (error);
2344 }
2345 bp += VPTRSZ + VNODESZ;
2346 }
2347 nmp = mp->mnt_list.cqe_next;
2348 vfs_unbusy(mp);
2349 }
2350
2351 *sizep = bp - where;
2352 return (0);
2353 #else
2354 return(EINVAL);
2355 #endif
2356 }
2357
2358 /*
2359 * Check to see if a filesystem is mounted on a block device.
2360 */
2361 int
2362 vfs_mountedon(vp)
2363 struct vnode *vp;
2364 {
2365 struct vnode *vq;
2366 int error = 0;
2367
2368 SPECHASH_LOCK();
2369 if (vp->v_specflags & SI_MOUNTEDON) {
2370 error = EBUSY;
2371 goto out;
2372 }
2373 if (vp->v_flag & VALIASED) {
2374 for (vq = *vp->v_hashchain; vq; vq = vq->v_specnext) {
2375 if (vq->v_rdev != vp->v_rdev ||
2376 vq->v_type != vp->v_type)
2377 continue;
2378 if (vq->v_specflags & SI_MOUNTEDON) {
2379 error = EBUSY;
2380 break;
2381 }
2382 }
2383 }
2384 out:
2385 SPECHASH_UNLOCK();
2386 return (error);
2387 }
2388
2389 /*
2390 * Unmount all filesystems. The list is traversed in reverse order
2391 * of mounting to avoid dependencies.
2392 */
2393 __private_extern__ void
2394 vfs_unmountall()
2395 {
2396 struct mount *mp;
2397 struct proc *p = current_proc();
2398 int error;
2399
2400 /*
2401 * Since this only runs when rebooting, it is not interlocked.
2402 */
2403 mount_list_lock();
2404 while(!TAILQ_EMPTY(&mountlist)) {
2405 mp = TAILQ_LAST(&mountlist, mntlist);
2406 mount_list_unlock();
2407 error = dounmount(mp, MNT_FORCE, p);
2408 if (error) {
2409 mount_list_lock();
2410 TAILQ_REMOVE(&mountlist, mp, mnt_list);
2411 printf("unmount of %s failed (", mp->mnt_vfsstat.f_mntonname);
2412 if (error == EBUSY)
2413 printf("BUSY)\n");
2414 else
2415 printf("%d)\n", error);
2416 continue;
2417 }
2418 mount_list_lock();
2419 }
2420 mount_list_unlock();
2421 }
2422
2423
2424 /*
2425 * This routine is called from vnode_pager_no_senders()
2426 * which in turn can be called with vnode locked by vnode_uncache()
2427 * But it could also get called as a result of vm_object_cache_trim().
2428 * In that case lock state is unknown.
2429 * AGE the vnode so that it gets recycled quickly.
2430 */
2431 __private_extern__ void
2432 vnode_pager_vrele(struct vnode *vp)
2433 {
2434 vnode_lock(vp);
2435
2436 if (!ISSET(vp->v_lflag, VL_TERMINATE))
2437 panic("vnode_pager_vrele: vp not in termination");
2438 vp->v_lflag &= ~VNAMED_UBC;
2439
2440 if (UBCINFOEXISTS(vp)) {
2441 struct ubc_info *uip = vp->v_ubcinfo;
2442
2443 if (ISSET(uip->ui_flags, UI_WASMAPPED))
2444 SET(vp->v_flag, VWASMAPPED);
2445 vp->v_ubcinfo = UBC_INFO_NULL;
2446
2447 ubc_info_deallocate(uip);
2448 } else {
2449 panic("NO ubcinfo in vnode_pager_vrele");
2450 }
2451 vnode_unlock(vp);
2452
2453 wakeup(&vp->v_lflag);
2454 }
2455
2456
2457 #include <sys/disk.h>
2458
2459 errno_t
2460 vfs_init_io_attributes(vnode_t devvp, mount_t mp)
2461 {
2462 int error;
2463 off_t readblockcnt;
2464 off_t writeblockcnt;
2465 off_t readmaxcnt;
2466 off_t writemaxcnt;
2467 off_t readsegcnt;
2468 off_t writesegcnt;
2469 off_t readsegsize;
2470 off_t writesegsize;
2471 u_long blksize;
2472 u_int64_t temp;
2473 struct vfs_context context;
2474
2475 proc_t p = current_proc();
2476
2477 context.vc_proc = p;
2478 context.vc_ucred = kauth_cred_get();
2479
2480 int isvirtual = 0;
2481 /*
2482 * determine if this mount point exists on the same device as the root
2483 * partition... if so, then it comes under the hard throttle control
2484 */
2485 int thisunit = -1;
2486 static int rootunit = -1;
2487
2488 if (rootunit == -1) {
2489 if (VNOP_IOCTL(rootvp, DKIOCGETBSDUNIT, (caddr_t)&rootunit, 0, &context))
2490 rootunit = -1;
2491 else if (rootvp == devvp)
2492 mp->mnt_kern_flag |= MNTK_ROOTDEV;
2493 }
2494 if (devvp != rootvp && rootunit != -1) {
2495 if (VNOP_IOCTL(devvp, DKIOCGETBSDUNIT, (caddr_t)&thisunit, 0, &context) == 0) {
2496 if (thisunit == rootunit)
2497 mp->mnt_kern_flag |= MNTK_ROOTDEV;
2498 }
2499 }
2500 /*
2501 * force the spec device to re-cache
2502 * the underlying block size in case
2503 * the filesystem overrode the initial value
2504 */
2505 set_fsblocksize(devvp);
2506
2507
2508 if ((error = VNOP_IOCTL(devvp, DKIOCGETBLOCKSIZE,
2509 (caddr_t)&blksize, 0, &context)))
2510 return (error);
2511
2512 mp->mnt_devblocksize = blksize;
2513
2514 if (VNOP_IOCTL(devvp, DKIOCISVIRTUAL, (caddr_t)&isvirtual, 0, &context) == 0) {
2515 if (isvirtual)
2516 mp->mnt_kern_flag |= MNTK_VIRTUALDEV;
2517 }
2518
2519 if ((error = VNOP_IOCTL(devvp, DKIOCGETMAXBLOCKCOUNTREAD,
2520 (caddr_t)&readblockcnt, 0, &context)))
2521 return (error);
2522
2523 if ((error = VNOP_IOCTL(devvp, DKIOCGETMAXBLOCKCOUNTWRITE,
2524 (caddr_t)&writeblockcnt, 0, &context)))
2525 return (error);
2526
2527 if ((error = VNOP_IOCTL(devvp, DKIOCGETMAXBYTECOUNTREAD,
2528 (caddr_t)&readmaxcnt, 0, &context)))
2529 return (error);
2530
2531 if ((error = VNOP_IOCTL(devvp, DKIOCGETMAXBYTECOUNTWRITE,
2532 (caddr_t)&writemaxcnt, 0, &context)))
2533 return (error);
2534
2535 if ((error = VNOP_IOCTL(devvp, DKIOCGETMAXSEGMENTCOUNTREAD,
2536 (caddr_t)&readsegcnt, 0, &context)))
2537 return (error);
2538
2539 if ((error = VNOP_IOCTL(devvp, DKIOCGETMAXSEGMENTCOUNTWRITE,
2540 (caddr_t)&writesegcnt, 0, &context)))
2541 return (error);
2542
2543 if ((error = VNOP_IOCTL(devvp, DKIOCGETMAXSEGMENTBYTECOUNTREAD,
2544 (caddr_t)&readsegsize, 0, &context)))
2545 return (error);
2546
2547 if ((error = VNOP_IOCTL(devvp, DKIOCGETMAXSEGMENTBYTECOUNTWRITE,
2548 (caddr_t)&writesegsize, 0, &context)))
2549 return (error);
2550
2551 if (readmaxcnt)
2552 temp = (readmaxcnt > UINT32_MAX) ? UINT32_MAX : readmaxcnt;
2553 else {
2554 if (readblockcnt) {
2555 temp = readblockcnt * blksize;
2556 temp = (temp > UINT32_MAX) ? UINT32_MAX : temp;
2557 } else
2558 temp = MAXPHYS;
2559 }
2560 mp->mnt_maxreadcnt = (u_int32_t)temp;
2561
2562 if (writemaxcnt)
2563 temp = (writemaxcnt > UINT32_MAX) ? UINT32_MAX : writemaxcnt;
2564 else {
2565 if (writeblockcnt) {
2566 temp = writeblockcnt * blksize;
2567 temp = (temp > UINT32_MAX) ? UINT32_MAX : temp;
2568 } else
2569 temp = MAXPHYS;
2570 }
2571 mp->mnt_maxwritecnt = (u_int32_t)temp;
2572
2573 if (readsegcnt) {
2574 temp = (readsegcnt > UINT16_MAX) ? UINT16_MAX : readsegcnt;
2575 mp->mnt_segreadcnt = (u_int16_t)temp;
2576 }
2577 if (writesegcnt) {
2578 temp = (writesegcnt > UINT16_MAX) ? UINT16_MAX : writesegcnt;
2579 mp->mnt_segwritecnt = (u_int16_t)temp;
2580 }
2581 if (readsegsize)
2582 temp = (readsegsize > UINT32_MAX) ? UINT32_MAX : readsegsize;
2583 else
2584 temp = mp->mnt_maxreadcnt;
2585 mp->mnt_maxsegreadsize = (u_int32_t)temp;
2586
2587 if (writesegsize)
2588 temp = (writesegsize > UINT32_MAX) ? UINT32_MAX : writesegsize;
2589 else
2590 temp = mp->mnt_maxwritecnt;
2591 mp->mnt_maxsegwritesize = (u_int32_t)temp;
2592
2593 return (error);
2594 }
2595
2596 static struct klist fs_klist;
2597
2598 void
2599 vfs_event_init(void)
2600 {
2601
2602 klist_init(&fs_klist);
2603 }
2604
2605 void
2606 vfs_event_signal(__unused fsid_t *fsid, u_int32_t event, __unused intptr_t data)
2607 {
2608
2609 KNOTE(&fs_klist, event);
2610 }
2611
2612 /*
2613 * return the number of mounted filesystems.
2614 */
2615 static int
2616 sysctl_vfs_getvfscnt(void)
2617 {
2618 return(mount_getvfscnt());
2619 }
2620
2621
2622 static int
2623 mount_getvfscnt(void)
2624 {
2625 int ret;
2626
2627 mount_list_lock();
2628 ret = nummounts;
2629 mount_list_unlock();
2630 return (ret);
2631
2632 }
2633
2634
2635
2636 static int
2637 mount_fillfsids(fsid_t *fsidlst, int count)
2638 {
2639 struct mount *mp;
2640 int actual=0;
2641
2642 actual = 0;
2643 mount_list_lock();
2644 TAILQ_FOREACH(mp, &mountlist, mnt_list) {
2645 if (actual <= count) {
2646 fsidlst[actual] = mp->mnt_vfsstat.f_fsid;
2647 actual++;
2648 }
2649 }
2650 mount_list_unlock();
2651 return (actual);
2652
2653 }
2654
2655 /*
2656 * fill in the array of fsid_t's up to a max of 'count', the actual
2657 * number filled in will be set in '*actual'. If there are more fsid_t's
2658 * than room in fsidlst then ENOMEM will be returned and '*actual' will
2659 * have the actual count.
2660 * having *actual filled out even in the error case is depended upon.
2661 */
2662 static int
2663 sysctl_vfs_getvfslist(fsid_t *fsidlst, int count, int *actual)
2664 {
2665 struct mount *mp;
2666
2667 *actual = 0;
2668 mount_list_lock();
2669 TAILQ_FOREACH(mp, &mountlist, mnt_list) {
2670 (*actual)++;
2671 if (*actual <= count)
2672 fsidlst[(*actual) - 1] = mp->mnt_vfsstat.f_fsid;
2673 }
2674 mount_list_unlock();
2675 return (*actual <= count ? 0 : ENOMEM);
2676 }
2677
2678 static int
2679 sysctl_vfs_vfslist SYSCTL_HANDLER_ARGS
2680 {
2681 int actual, error;
2682 size_t space;
2683 fsid_t *fsidlst;
2684
2685 /* This is a readonly node. */
2686 if (req->newptr != USER_ADDR_NULL)
2687 return (EPERM);
2688
2689 /* they are querying us so just return the space required. */
2690 if (req->oldptr == USER_ADDR_NULL) {
2691 req->oldidx = sysctl_vfs_getvfscnt() * sizeof(fsid_t);
2692 return 0;
2693 }
2694 again:
2695 /*
2696 * Retrieve an accurate count of the amount of space required to copy
2697 * out all the fsids in the system.
2698 */
2699 space = req->oldlen;
2700 req->oldlen = sysctl_vfs_getvfscnt() * sizeof(fsid_t);
2701
2702 /* they didn't give us enough space. */
2703 if (space < req->oldlen)
2704 return (ENOMEM);
2705
2706 MALLOC(fsidlst, fsid_t *, req->oldlen, M_TEMP, M_WAITOK);
2707 error = sysctl_vfs_getvfslist(fsidlst, req->oldlen / sizeof(fsid_t),
2708 &actual);
2709 /*
2710 * If we get back ENOMEM, then another mount has been added while we
2711 * slept in malloc above. If this is the case then try again.
2712 */
2713 if (error == ENOMEM) {
2714 FREE(fsidlst, M_TEMP);
2715 req->oldlen = space;
2716 goto again;
2717 }
2718 if (error == 0) {
2719 error = SYSCTL_OUT(req, fsidlst, actual * sizeof(fsid_t));
2720 }
2721 FREE(fsidlst, M_TEMP);
2722 return (error);
2723 }
2724
2725 /*
2726 * Do a sysctl by fsid.
2727 */
2728 static int
2729 sysctl_vfs_ctlbyfsid SYSCTL_HANDLER_ARGS
2730 {
2731 struct vfsidctl vc;
2732 struct user_vfsidctl user_vc;
2733 struct mount *mp;
2734 struct vfsstatfs *sp;
2735 struct proc *p;
2736 int *name;
2737 int error, flags, namelen;
2738 struct vfs_context context;
2739 boolean_t is_64_bit;
2740
2741 name = arg1;
2742 namelen = arg2;
2743 p = req->p;
2744 context.vc_proc = p;
2745 context.vc_ucred = kauth_cred_get();
2746 is_64_bit = proc_is64bit(p);
2747
2748 if (is_64_bit) {
2749 error = SYSCTL_IN(req, &user_vc, sizeof(user_vc));
2750 if (error)
2751 return (error);
2752 if (user_vc.vc_vers != VFS_CTL_VERS1)
2753 return (EINVAL);
2754 mp = mount_list_lookupby_fsid(&user_vc.vc_fsid, 0, 0);
2755 }
2756 else {
2757 error = SYSCTL_IN(req, &vc, sizeof(vc));
2758 if (error)
2759 return (error);
2760 if (vc.vc_vers != VFS_CTL_VERS1)
2761 return (EINVAL);
2762 mp = mount_list_lookupby_fsid(&vc.vc_fsid, 0, 0);
2763 }
2764 if (mp == NULL)
2765 return (ENOENT);
2766 /* reset so that the fs specific code can fetch it. */
2767 req->newidx = 0;
2768 /*
2769 * Note if this is a VFS_CTL then we pass the actual sysctl req
2770 * in for "oldp" so that the lower layer can DTRT and use the
2771 * SYSCTL_IN/OUT routines.
2772 */
2773 if (mp->mnt_op->vfs_sysctl != NULL) {
2774 if (is_64_bit) {
2775 if (vfs_64bitready(mp)) {
2776 error = mp->mnt_op->vfs_sysctl(name, namelen,
2777 CAST_USER_ADDR_T(req),
2778 NULL, USER_ADDR_NULL, 0,
2779 &context);
2780 }
2781 else {
2782 error = ENOTSUP;
2783 }
2784 }
2785 else {
2786 error = mp->mnt_op->vfs_sysctl(name, namelen,
2787 CAST_USER_ADDR_T(req),
2788 NULL, USER_ADDR_NULL, 0,
2789 &context);
2790 }
2791 if (error != ENOTSUP)
2792 return (error);
2793 }
2794 switch (name[0]) {
2795 case VFS_CTL_UMOUNT:
2796 req->newidx = 0;
2797 if (is_64_bit) {
2798 req->newptr = user_vc.vc_ptr;
2799 req->newlen = (size_t)user_vc.vc_len;
2800 }
2801 else {
2802 req->newptr = CAST_USER_ADDR_T(vc.vc_ptr);
2803 req->newlen = vc.vc_len;
2804 }
2805 error = SYSCTL_IN(req, &flags, sizeof(flags));
2806 if (error)
2807 break;
2808 error = safedounmount(mp, flags, p);
2809 break;
2810 case VFS_CTL_STATFS:
2811 req->newidx = 0;
2812 if (is_64_bit) {
2813 req->newptr = user_vc.vc_ptr;
2814 req->newlen = (size_t)user_vc.vc_len;
2815 }
2816 else {
2817 req->newptr = CAST_USER_ADDR_T(vc.vc_ptr);
2818 req->newlen = vc.vc_len;
2819 }
2820 error = SYSCTL_IN(req, &flags, sizeof(flags));
2821 if (error)
2822 break;
2823 sp = &mp->mnt_vfsstat;
2824 if (((flags & MNT_NOWAIT) == 0 || (flags & MNT_WAIT)) &&
2825 (error = vfs_update_vfsstat(mp, &context)))
2826 return (error);
2827 if (is_64_bit) {
2828 struct user_statfs sfs;
2829 bzero(&sfs, sizeof(sfs));
2830 sfs.f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
2831 sfs.f_type = mp->mnt_vtable->vfc_typenum;
2832 sfs.f_bsize = (user_long_t)sp->f_bsize;
2833 sfs.f_iosize = (user_long_t)sp->f_iosize;
2834 sfs.f_blocks = (user_long_t)sp->f_blocks;
2835 sfs.f_bfree = (user_long_t)sp->f_bfree;
2836 sfs.f_bavail = (user_long_t)sp->f_bavail;
2837 sfs.f_files = (user_long_t)sp->f_files;
2838 sfs.f_ffree = (user_long_t)sp->f_ffree;
2839 sfs.f_fsid = sp->f_fsid;
2840 sfs.f_owner = sp->f_owner;
2841
2842 strncpy(&sfs.f_fstypename, &sp->f_fstypename, MFSNAMELEN-1);
2843 strncpy(&sfs.f_mntonname, &sp->f_mntonname, MNAMELEN-1);
2844 strncpy(&sfs.f_mntfromname, &sp->f_mntfromname, MNAMELEN-1);
2845
2846 error = SYSCTL_OUT(req, &sfs, sizeof(sfs));
2847 }
2848 else {
2849 struct statfs sfs;
2850 bzero(&sfs, sizeof(struct statfs));
2851 sfs.f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
2852 sfs.f_type = mp->mnt_vtable->vfc_typenum;
2853
2854 /*
2855 * It's possible for there to be more than 2^^31 blocks in the filesystem, so we
2856 * have to fudge the numbers here in that case. We inflate the blocksize in order
2857 * to reflect the filesystem size as best we can.
2858 */
2859 if (sp->f_blocks > LONG_MAX) {
2860 int shift;
2861
2862 /*
2863 * Work out how far we have to shift the block count down to make it fit.
2864 * Note that it's possible to have to shift so far that the resulting
2865 * blocksize would be unreportably large. At that point, we will clip
2866 * any values that don't fit.
2867 *
2868 * For safety's sake, we also ensure that f_iosize is never reported as
2869 * being smaller than f_bsize.
2870 */
2871 for (shift = 0; shift < 32; shift++) {
2872 if ((sp->f_blocks >> shift) <= LONG_MAX)
2873 break;
2874 if ((sp->f_bsize << (shift + 1)) > LONG_MAX)
2875 break;
2876 }
2877 #define __SHIFT_OR_CLIP(x, s) ((((x) >> (s)) > LONG_MAX) ? LONG_MAX : ((x) >> (s)))
2878 sfs.f_blocks = (long)__SHIFT_OR_CLIP(sp->f_blocks, shift);
2879 sfs.f_bfree = (long)__SHIFT_OR_CLIP(sp->f_bfree, shift);
2880 sfs.f_bavail = (long)__SHIFT_OR_CLIP(sp->f_bavail, shift);
2881 #undef __SHIFT_OR_CLIP
2882 sfs.f_bsize = (long)(sp->f_bsize << shift);
2883 sfs.f_iosize = lmax(sp->f_iosize, sp->f_bsize);
2884 } else {
2885 sfs.f_bsize = (long)sp->f_bsize;
2886 sfs.f_iosize = (long)sp->f_iosize;
2887 sfs.f_blocks = (long)sp->f_blocks;
2888 sfs.f_bfree = (long)sp->f_bfree;
2889 sfs.f_bavail = (long)sp->f_bavail;
2890 }
2891 sfs.f_files = (long)sp->f_files;
2892 sfs.f_ffree = (long)sp->f_ffree;
2893 sfs.f_fsid = sp->f_fsid;
2894 sfs.f_owner = sp->f_owner;
2895
2896 strncpy(&sfs.f_fstypename, &sp->f_fstypename, MFSNAMELEN-1);
2897 strncpy(&sfs.f_mntonname, &sp->f_mntonname, MNAMELEN-1);
2898 strncpy(&sfs.f_mntfromname, &sp->f_mntfromname, MNAMELEN-1);
2899
2900 error = SYSCTL_OUT(req, &sfs, sizeof(sfs));
2901 }
2902 break;
2903 default:
2904 return (ENOTSUP);
2905 }
2906 return (error);
2907 }
2908
2909 static int filt_fsattach(struct knote *kn);
2910 static void filt_fsdetach(struct knote *kn);
2911 static int filt_fsevent(struct knote *kn, long hint);
2912
2913 struct filterops fs_filtops =
2914 { 0, filt_fsattach, filt_fsdetach, filt_fsevent };
2915
2916 static int
2917 filt_fsattach(struct knote *kn)
2918 {
2919
2920 kn->kn_flags |= EV_CLEAR;
2921 KNOTE_ATTACH(&fs_klist, kn);
2922 return (0);
2923 }
2924
2925 static void
2926 filt_fsdetach(struct knote *kn)
2927 {
2928
2929 KNOTE_DETACH(&fs_klist, kn);
2930 }
2931
2932 static int
2933 filt_fsevent(struct knote *kn, long hint)
2934 {
2935
2936 kn->kn_fflags |= hint;
2937 return (kn->kn_fflags != 0);
2938 }
2939
2940 static int
2941 sysctl_vfs_noremotehang SYSCTL_HANDLER_ARGS
2942 {
2943 int out, error;
2944 pid_t pid;
2945 size_t space;
2946 struct proc *p;
2947
2948 /* We need a pid. */
2949 if (req->newptr == USER_ADDR_NULL)
2950 return (EINVAL);
2951
2952 error = SYSCTL_IN(req, &pid, sizeof(pid));
2953 if (error)
2954 return (error);
2955
2956 p = pfind(pid < 0 ? -pid : pid);
2957 if (p == NULL)
2958 return (ESRCH);
2959
2960 /*
2961 * Fetching the value is ok, but we only fetch if the old
2962 * pointer is given.
2963 */
2964 if (req->oldptr != USER_ADDR_NULL) {
2965 out = !((p->p_flag & P_NOREMOTEHANG) == 0);
2966 error = SYSCTL_OUT(req, &out, sizeof(out));
2967 return (error);
2968 }
2969
2970 /* XXX req->p->p_ucred -> kauth_cred_get() ??? */
2971 /* cansignal offers us enough security. */
2972 if (p != req->p && suser(req->p->p_ucred, &req->p->p_acflag) != 0)
2973 return (EPERM);
2974
2975 if (pid < 0)
2976 p->p_flag &= ~P_NOREMOTEHANG;
2977 else
2978 p->p_flag |= P_NOREMOTEHANG;
2979
2980 return (0);
2981 }
2982 /* the vfs.generic. branch. */
2983 SYSCTL_NODE(_vfs, VFS_GENERIC, generic, CTLFLAG_RW, 0, "vfs generic hinge");
2984 /* retreive a list of mounted filesystem fsid_t */
2985 SYSCTL_PROC(_vfs_generic, OID_AUTO, vfsidlist, CTLFLAG_RD,
2986 0, 0, sysctl_vfs_vfslist, "S,fsid", "List of mounted filesystem ids");
2987 /* perform operations on filesystem via fsid_t */
2988 SYSCTL_NODE(_vfs_generic, OID_AUTO, ctlbyfsid, CTLFLAG_RW,
2989 sysctl_vfs_ctlbyfsid, "ctlbyfsid");
2990 SYSCTL_PROC(_vfs_generic, OID_AUTO, noremotehang, CTLFLAG_RW,
2991 0, 0, sysctl_vfs_noremotehang, "I", "noremotehang");
2992
2993
2994 int num_reusedvnodes=0;
2995
2996 static int
2997 new_vnode(vnode_t *vpp)
2998 {
2999 vnode_t vp;
3000 int retries = 0; /* retry incase of tablefull */
3001 int vpid;
3002 struct timespec ts;
3003
3004 retry:
3005 vnode_list_lock();
3006
3007 if ( !TAILQ_EMPTY(&vnode_free_list)) {
3008 /*
3009 * Pick the first vp for possible reuse
3010 */
3011 vp = TAILQ_FIRST(&vnode_free_list);
3012
3013 if (vp->v_lflag & VL_DEAD)
3014 goto steal_this_vp;
3015 } else
3016 vp = NULL;
3017
3018 /*
3019 * we're either empty, or the next guy on the
3020 * list is a valid vnode... if we're under the
3021 * limit, we'll create a new vnode
3022 */
3023 if (numvnodes < desiredvnodes) {
3024 numvnodes++;
3025 vnode_list_unlock();
3026 MALLOC_ZONE(vp, struct vnode *, sizeof *vp, M_VNODE, M_WAITOK);
3027 bzero((char *)vp, sizeof *vp);
3028 VLISTNONE(vp); /* avoid double queue removal */
3029 lck_mtx_init(&vp->v_lock, vnode_lck_grp, vnode_lck_attr);
3030
3031 nanouptime(&ts);
3032 vp->v_id = ts.tv_nsec;
3033 vp->v_flag = VSTANDARD;
3034
3035 goto done;
3036 }
3037 if (vp == NULL) {
3038 /*
3039 * we've reached the system imposed maximum number of vnodes
3040 * but there isn't a single one available
3041 * wait a bit and then retry... if we can't get a vnode
3042 * after 100 retries, than log a complaint
3043 */
3044 if (++retries <= 100) {
3045 vnode_list_unlock();
3046 IOSleep(1);
3047 goto retry;
3048 }
3049
3050 vnode_list_unlock();
3051 tablefull("vnode");
3052 log(LOG_EMERG, "%d desired, %d numvnodes, "
3053 "%d free, %d inactive\n",
3054 desiredvnodes, numvnodes, freevnodes, inactivevnodes);
3055 *vpp = 0;
3056 return (ENFILE);
3057 }
3058 steal_this_vp:
3059 vpid = vp->v_id;
3060
3061 VREMFREE("new_vnode", vp);
3062 VLISTNONE(vp);
3063
3064 vnode_list_unlock();
3065 vnode_lock(vp);
3066
3067 /*
3068 * We could wait for the vnode_lock after removing the vp from the freelist
3069 * and the vid is bumped only at the very end of reclaim. So it is possible
3070 * that we are looking at a vnode that is being terminated. If so skip it.
3071 */
3072 if ((vpid != vp->v_id) || (vp->v_usecount != 0) || (vp->v_iocount != 0) ||
3073 VONLIST(vp) || (vp->v_lflag & VL_TERMINATE)) {
3074 /*
3075 * we lost the race between dropping the list lock
3076 * and picking up the vnode_lock... someone else
3077 * used this vnode and it is now in a new state
3078 * so we need to go back and try again
3079 */
3080 vnode_unlock(vp);
3081 goto retry;
3082 }
3083 if ( (vp->v_lflag & (VL_NEEDINACTIVE | VL_MARKTERM)) == VL_NEEDINACTIVE ) {
3084 /*
3085 * we did a vnode_rele_ext that asked for
3086 * us not to reenter the filesystem during
3087 * the release even though VL_NEEDINACTIVE was
3088 * set... we'll do it here by doing a
3089 * vnode_get/vnode_put
3090 *
3091 * pick up an iocount so that we can call
3092 * vnode_put and drive the VNOP_INACTIVE...
3093 * vnode_put will either leave us off
3094 * the freelist if a new ref comes in,
3095 * or put us back on the end of the freelist
3096 * or recycle us if we were marked for termination...
3097 * so we'll just go grab a new candidate
3098 */
3099 vp->v_iocount++;
3100 #ifdef JOE_DEBUG
3101 record_vp(vp, 1);
3102 #endif
3103 vnode_put_locked(vp);
3104 vnode_unlock(vp);
3105 goto retry;
3106 }
3107 OSAddAtomic(1, &num_reusedvnodes);
3108
3109 /* Checks for anyone racing us for recycle */
3110 if (vp->v_type != VBAD) {
3111 if (vp->v_lflag & VL_DEAD)
3112 panic("new_vnode: the vnode is VL_DEAD but not VBAD");
3113
3114 (void)vnode_reclaim_internal(vp, 1, 1);
3115
3116 if ((VONLIST(vp)))
3117 panic("new_vnode: vp on list ");
3118 if (vp->v_usecount || vp->v_iocount || vp->v_kusecount ||
3119 (vp->v_lflag & (VNAMED_UBC | VNAMED_MOUNT | VNAMED_FSHASH)))
3120 panic("new_vnode: free vnode still referenced\n");
3121 if ((vp->v_mntvnodes.tqe_prev != 0) && (vp->v_mntvnodes.tqe_next != 0))
3122 panic("new_vnode: vnode seems to be on mount list ");
3123 if ( !LIST_EMPTY(&vp->v_nclinks) || !LIST_EMPTY(&vp->v_ncchildren))
3124 panic("new_vnode: vnode still hooked into the name cache");
3125 }
3126 if (vp->v_unsafefs) {
3127 lck_mtx_destroy(&vp->v_unsafefs->fsnodelock, vnode_lck_grp);
3128 FREE_ZONE((void *)vp->v_unsafefs, sizeof(struct unsafe_fsnode), M_UNSAFEFS);
3129 vp->v_unsafefs = (struct unsafe_fsnode *)NULL;
3130 }
3131 vp->v_lflag = 0;
3132 vp->v_writecount = 0;
3133 vp->v_references = 0;
3134 vp->v_iterblkflags = 0;
3135 vp->v_flag = VSTANDARD;
3136 /* vbad vnodes can point to dead_mountp */
3137 vp->v_mount = 0;
3138 vp->v_defer_reclaimlist = (vnode_t)0;
3139
3140 vnode_unlock(vp);
3141 done:
3142 *vpp = vp;
3143
3144 return (0);
3145 }
3146
3147 void
3148 vnode_lock(vnode_t vp)
3149 {
3150 lck_mtx_lock(&vp->v_lock);
3151 }
3152
3153 void
3154 vnode_unlock(vnode_t vp)
3155 {
3156 lck_mtx_unlock(&vp->v_lock);
3157 }
3158
3159
3160
3161 int
3162 vnode_get(struct vnode *vp)
3163 {
3164 vnode_lock(vp);
3165
3166 if ( (vp->v_iocount == 0) && (vp->v_lflag & (VL_TERMINATE | VL_DEAD)) ) {
3167 vnode_unlock(vp);
3168 return(ENOENT);
3169 }
3170 vp->v_iocount++;
3171 #ifdef JOE_DEBUG
3172 record_vp(vp, 1);
3173 #endif
3174 vnode_unlock(vp);
3175
3176 return(0);
3177 }
3178
3179 int
3180 vnode_getwithvid(vnode_t vp, int vid)
3181 {
3182 return(vget_internal(vp, vid, ( VNODE_NODEAD| VNODE_WITHID)));
3183 }
3184
3185 int
3186 vnode_getwithref(vnode_t vp)
3187 {
3188 return(vget_internal(vp, 0, 0));
3189 }
3190
3191
3192 int
3193 vnode_put(vnode_t vp)
3194 {
3195 int retval;
3196
3197 vnode_lock(vp);
3198 retval = vnode_put_locked(vp);
3199 vnode_unlock(vp);
3200
3201 return(retval);
3202 }
3203
3204 int
3205 vnode_put_locked(vnode_t vp)
3206 {
3207 struct vfs_context context;
3208
3209 retry:
3210 if (vp->v_iocount < 1)
3211 panic("vnode_put(%x): iocount < 1", vp);
3212
3213 if ((vp->v_usecount > 0) || (vp->v_iocount > 1)) {
3214 vnode_dropiocount(vp, 1);
3215 return(0);
3216 }
3217 if ((vp->v_lflag & (VL_MARKTERM | VL_TERMINATE | VL_DEAD | VL_NEEDINACTIVE)) == VL_NEEDINACTIVE) {
3218
3219 vp->v_lflag &= ~VL_NEEDINACTIVE;
3220 vnode_unlock(vp);
3221
3222 context.vc_proc = current_proc();
3223 context.vc_ucred = kauth_cred_get();
3224 VNOP_INACTIVE(vp, &context);
3225
3226 vnode_lock(vp);
3227 /*
3228 * because we had to drop the vnode lock before calling
3229 * VNOP_INACTIVE, the state of this vnode may have changed...
3230 * we may pick up both VL_MARTERM and either
3231 * an iocount or a usecount while in the VNOP_INACTIVE call
3232 * we don't want to call vnode_reclaim_internal on a vnode
3233 * that has active references on it... so loop back around
3234 * and reevaluate the state
3235 */
3236 goto retry;
3237 }
3238 vp->v_lflag &= ~VL_NEEDINACTIVE;
3239
3240 if ((vp->v_lflag & (VL_MARKTERM | VL_TERMINATE | VL_DEAD)) == VL_MARKTERM)
3241 vnode_reclaim_internal(vp, 1, 0);
3242
3243 vnode_dropiocount(vp, 1);
3244 vnode_list_add(vp);
3245
3246 return(0);
3247 }
3248
3249 /* is vnode_t in use by others? */
3250 int
3251 vnode_isinuse(vnode_t vp, int refcnt)
3252 {
3253 return(vnode_isinuse_locked(vp, refcnt, 0));
3254 }
3255
3256
3257 static int
3258 vnode_isinuse_locked(vnode_t vp, int refcnt, int locked)
3259 {
3260 int retval = 0;
3261
3262 if (!locked)
3263 vnode_lock(vp);
3264 if ((vp->v_type != VREG) && (vp->v_usecount > refcnt)) {
3265 retval = 1;
3266 goto out;
3267 }
3268 if (vp->v_type == VREG) {
3269 retval = ubc_isinuse_locked(vp, refcnt, 1);
3270 }
3271
3272 out:
3273 if (!locked)
3274 vnode_unlock(vp);
3275 return(retval);
3276 }
3277
3278
3279 /* resume vnode_t */
3280 errno_t
3281 vnode_resume(vnode_t vp)
3282 {
3283
3284 vnode_lock(vp);
3285
3286 if (vp->v_owner == current_thread()) {
3287 vp->v_lflag &= ~VL_SUSPENDED;
3288 vp->v_owner = 0;
3289 vnode_unlock(vp);
3290 wakeup(&vp->v_iocount);
3291 } else
3292 vnode_unlock(vp);
3293
3294 return(0);
3295 }
3296
3297 static errno_t
3298 vnode_drain(vnode_t vp)
3299 {
3300
3301 if (vp->v_lflag & VL_DRAIN) {
3302 panic("vnode_drain: recursuve drain");
3303 return(ENOENT);
3304 }
3305 vp->v_lflag |= VL_DRAIN;
3306 vp->v_owner = current_thread();
3307
3308 while (vp->v_iocount > 1)
3309 msleep(&vp->v_iocount, &vp->v_lock, PVFS, "vnode_drain", 0);
3310 return(0);
3311 }
3312
3313
3314 /*
3315 * if the number of recent references via vnode_getwithvid or vnode_getwithref
3316 * exceeds this threshhold, than 'UN-AGE' the vnode by removing it from
3317 * the LRU list if it's currently on it... once the iocount and usecount both drop
3318 * to 0, it will get put back on the end of the list, effectively making it younger
3319 * this allows us to keep actively referenced vnodes in the list without having
3320 * to constantly remove and add to the list each time a vnode w/o a usecount is
3321 * referenced which costs us taking and dropping a global lock twice.
3322 */
3323 #define UNAGE_THRESHHOLD 10
3324
3325 errno_t
3326 vnode_getiocount(vnode_t vp, int locked, int vid, int vflags)
3327 {
3328 int nodead = vflags & VNODE_NODEAD;
3329 int nosusp = vflags & VNODE_NOSUSPEND;
3330
3331 if (!locked)
3332 vnode_lock(vp);
3333
3334 for (;;) {
3335 /*
3336 * if it is a dead vnode with deadfs
3337 */
3338 if (nodead && (vp->v_lflag & VL_DEAD) && ((vp->v_type == VBAD) || (vp->v_data == 0))) {
3339 if (!locked)
3340 vnode_unlock(vp);
3341 return(ENOENT);
3342 }
3343 /*
3344 * will return VL_DEAD ones
3345 */
3346 if ((vp->v_lflag & (VL_SUSPENDED | VL_DRAIN | VL_TERMINATE)) == 0 ) {
3347 break;
3348 }
3349 /*
3350 * if suspended vnodes are to be failed
3351 */
3352 if (nosusp && (vp->v_lflag & VL_SUSPENDED)) {
3353 if (!locked)
3354 vnode_unlock(vp);
3355 return(ENOENT);
3356 }
3357 /*
3358 * if you are the owner of drain/suspend/termination , can acquire iocount
3359 * check for VL_TERMINATE; it does not set owner
3360 */
3361 if ((vp->v_lflag & (VL_DRAIN | VL_SUSPENDED | VL_TERMINATE)) &&
3362 (vp->v_owner == current_thread())) {
3363 break;
3364 }
3365 if (vp->v_lflag & VL_TERMINATE) {
3366 vp->v_lflag |= VL_TERMWANT;
3367
3368 msleep(&vp->v_lflag, &vp->v_lock, PVFS, "vnode getiocount", 0);
3369 } else
3370 msleep(&vp->v_iocount, &vp->v_lock, PVFS, "vnode_getiocount", 0);
3371 }
3372 if (vid != vp->v_id) {
3373 if (!locked)
3374 vnode_unlock(vp);
3375 return(ENOENT);
3376 }
3377 if (++vp->v_references >= UNAGE_THRESHHOLD) {
3378 vp->v_references = 0;
3379 vnode_list_remove(vp);
3380 }
3381 vp->v_iocount++;
3382 #ifdef JOE_DEBUG
3383 record_vp(vp, 1);
3384 #endif
3385 if (!locked)
3386 vnode_unlock(vp);
3387 return(0);
3388 }
3389
3390 static void
3391 vnode_dropiocount (vnode_t vp, int locked)
3392 {
3393 if (!locked)
3394 vnode_lock(vp);
3395 if (vp->v_iocount < 1)
3396 panic("vnode_dropiocount(%x): v_iocount < 1", vp);
3397
3398 vp->v_iocount--;
3399 #ifdef JOE_DEBUG
3400 record_vp(vp, -1);
3401 #endif
3402 if ((vp->v_lflag & (VL_DRAIN | VL_SUSPENDED)) && (vp->v_iocount <= 1))
3403 wakeup(&vp->v_iocount);
3404
3405 if (!locked)
3406 vnode_unlock(vp);
3407 }
3408
3409
3410 void
3411 vnode_reclaim(struct vnode * vp)
3412 {
3413 vnode_reclaim_internal(vp, 0, 0);
3414 }
3415
3416 __private_extern__
3417 void
3418 vnode_reclaim_internal(struct vnode * vp, int locked, int reuse)
3419 {
3420 int isfifo = 0;
3421
3422 if (!locked)
3423 vnode_lock(vp);
3424
3425 if (vp->v_lflag & VL_TERMINATE) {
3426 panic("vnode reclaim in progress");
3427 }
3428 vp->v_lflag |= VL_TERMINATE;
3429
3430 if (vnode_drain(vp)) {
3431 panic("vnode drain failed");
3432 vnode_unlock(vp);
3433 return;
3434 }
3435 isfifo = (vp->v_type == VFIFO);
3436
3437 if (vp->v_type != VBAD)
3438 vgone(vp); /* clean and reclaim the vnode */
3439
3440 /*
3441 * give the vnode a new identity so
3442 * that vnode_getwithvid will fail
3443 * on any stale cache accesses
3444 */
3445 vp->v_id++;
3446 if (isfifo) {
3447 struct fifoinfo * fip;
3448
3449 fip = vp->v_fifoinfo;
3450 vp->v_fifoinfo = NULL;
3451 FREE(fip, M_TEMP);
3452 }
3453
3454 vp->v_type = VBAD;
3455
3456 if (vp->v_data)
3457 panic("vnode_reclaim_internal: cleaned vnode isn't");
3458 if (vp->v_numoutput)
3459 panic("vnode_reclaim_internal: Clean vnode has pending I/O's");
3460 if (UBCINFOEXISTS(vp))
3461 panic("vnode_reclaim_internal: ubcinfo not cleaned");
3462 if (vp->v_parent)
3463 panic("vnode_reclaim_internal: vparent not removed");
3464 if (vp->v_name)
3465 panic("vnode_reclaim_internal: vname not removed");
3466
3467 vp->v_socket = 0;
3468
3469 vp->v_lflag &= ~VL_TERMINATE;
3470 vp->v_lflag &= ~VL_DRAIN;
3471 vp->v_owner = 0;
3472
3473 if (vp->v_lflag & VL_TERMWANT) {
3474 vp->v_lflag &= ~VL_TERMWANT;
3475 wakeup(&vp->v_lflag);
3476 }
3477 if (!reuse && vp->v_usecount == 0)
3478 vnode_list_add(vp);
3479 if (!locked)
3480 vnode_unlock(vp);
3481 }
3482
3483 /* USAGE:
3484 * The following api creates a vnode and associates all the parameter specified in vnode_fsparam
3485 * structure and returns a vnode handle with a reference. device aliasing is handled here so checkalias
3486 * is obsoleted by this.
3487 * vnode_create(int flavor, size_t size, void * param, vnode_t *vp)
3488 */
3489 int
3490 vnode_create(int flavor, size_t size, void *data, vnode_t *vpp)
3491 {
3492 int error;
3493 int insert = 1;
3494 vnode_t vp;
3495 vnode_t nvp;
3496 vnode_t dvp;
3497 struct componentname *cnp;
3498 struct vnode_fsparam *param = (struct vnode_fsparam *)data;
3499
3500 if (flavor == VNCREATE_FLAVOR && (size == VCREATESIZE) && param) {
3501 if ( (error = new_vnode(&vp)) ) {
3502 return(error);
3503 } else {
3504 dvp = param->vnfs_dvp;
3505 cnp = param->vnfs_cnp;
3506
3507 vp->v_op = param->vnfs_vops;
3508 vp->v_type = param->vnfs_vtype;
3509 vp->v_data = param->vnfs_fsnode;
3510 vp->v_iocount = 1;
3511
3512 if (param->vnfs_markroot)
3513 vp->v_flag |= VROOT;
3514 if (param->vnfs_marksystem)
3515 vp->v_flag |= VSYSTEM;
3516 else if (vp->v_type == VREG) {
3517 /*
3518 * only non SYSTEM vp
3519 */
3520 error = ubc_info_init_withsize(vp, param->vnfs_filesize);
3521 if (error) {
3522 #ifdef JOE_DEBUG
3523 record_vp(vp, 1);
3524 #endif
3525 vp->v_mount = 0;
3526 vp->v_op = dead_vnodeop_p;
3527 vp->v_tag = VT_NON;
3528 vp->v_data = NULL;
3529 vp->v_type = VBAD;
3530 vp->v_lflag |= VL_DEAD;
3531
3532 vnode_put(vp);
3533 return(error);
3534 }
3535 }
3536 #ifdef JOE_DEBUG
3537 record_vp(vp, 1);
3538 #endif
3539 if (vp->v_type == VCHR || vp->v_type == VBLK) {
3540
3541 if ( (nvp = checkalias(vp, param->vnfs_rdev)) ) {
3542 /*
3543 * if checkalias returns a vnode, it will be locked
3544 *
3545 * first get rid of the unneeded vnode we acquired
3546 */
3547 vp->v_data = NULL;
3548 vp->v_op = spec_vnodeop_p;
3549 vp->v_type = VBAD;
3550 vp->v_lflag = VL_DEAD;
3551 vp->v_data = NULL;
3552 vp->v_tag = VT_NON;
3553 vnode_put(vp);
3554
3555 /*
3556 * switch to aliased vnode and finish
3557 * preparing it
3558 */
3559 vp = nvp;
3560
3561 vclean(vp, 0, current_proc());
3562 vp->v_op = param->vnfs_vops;
3563 vp->v_type = param->vnfs_vtype;
3564 vp->v_data = param->vnfs_fsnode;
3565 vp->v_lflag = 0;
3566 vp->v_mount = NULL;
3567 insmntque(vp, param->vnfs_mp);
3568 insert = 0;
3569 vnode_unlock(vp);
3570 }
3571 }
3572
3573 if (vp->v_type == VFIFO) {
3574 struct fifoinfo *fip;
3575
3576 MALLOC(fip, struct fifoinfo *,
3577 sizeof(*fip), M_TEMP, M_WAITOK);
3578 bzero(fip, sizeof(struct fifoinfo ));
3579 vp->v_fifoinfo = fip;
3580 }
3581 /* The file systems usually pass the address of the location where
3582 * where there store the vnode pointer. When we add the vnode in mount
3583 * point and name cache they are discoverable. So the file system node
3584 * will have the connection to vnode setup by then
3585 */
3586 *vpp = vp;
3587
3588 if (param->vnfs_mp) {
3589 if (param->vnfs_mp->mnt_kern_flag & MNTK_LOCK_LOCAL)
3590 vp->v_flag |= VLOCKLOCAL;
3591 if (insert) {
3592 /*
3593 * enter in mount vnode list
3594 */
3595 insmntque(vp, param->vnfs_mp);
3596 }
3597 #ifdef INTERIM_FSNODE_LOCK
3598 if (param->vnfs_mp->mnt_vtable->vfc_threadsafe == 0) {
3599 MALLOC_ZONE(vp->v_unsafefs, struct unsafe_fsnode *,
3600 sizeof(struct unsafe_fsnode), M_UNSAFEFS, M_WAITOK);
3601 vp->v_unsafefs->fsnode_count = 0;
3602 vp->v_unsafefs->fsnodeowner = (void *)NULL;
3603 lck_mtx_init(&vp->v_unsafefs->fsnodelock, vnode_lck_grp, vnode_lck_attr);
3604 }
3605 #endif /* INTERIM_FSNODE_LOCK */
3606 }
3607 if (dvp && vnode_ref(dvp) == 0) {
3608 vp->v_parent = dvp;
3609 }
3610 if (cnp) {
3611 if (dvp && ((param->vnfs_flags & (VNFS_NOCACHE | VNFS_CANTCACHE)) == 0)) {
3612 /*
3613 * enter into name cache
3614 * we've got the info to enter it into the name cache now
3615 */
3616 cache_enter(dvp, vp, cnp);
3617 }
3618 vp->v_name = vfs_addname(cnp->cn_nameptr, cnp->cn_namelen, cnp->cn_hash, 0);
3619 }
3620 if ((param->vnfs_flags & VNFS_CANTCACHE) == 0) {
3621 /*
3622 * this vnode is being created as cacheable in the name cache
3623 * this allows us to re-enter it in the cache
3624 */
3625 vp->v_flag |= VNCACHEABLE;
3626 }
3627 if ((vp->v_flag & VSYSTEM) && (vp->v_type != VREG))
3628 panic("incorrect vnode setup");
3629
3630 return(0);
3631 }
3632 }
3633 return (EINVAL);
3634 }
3635
3636 int
3637 vnode_addfsref(vnode_t vp)
3638 {
3639 vnode_lock(vp);
3640 if (vp->v_lflag & VNAMED_FSHASH)
3641 panic("add_fsref: vp already has named reference");
3642 if ((vp->v_freelist.tqe_prev != (struct vnode **)0xdeadb))
3643 panic("addfsref: vp on the free list\n");
3644 vp->v_lflag |= VNAMED_FSHASH;
3645 vnode_unlock(vp);
3646 return(0);
3647
3648 }
3649 int
3650 vnode_removefsref(vnode_t vp)
3651 {
3652 vnode_lock(vp);
3653 if ((vp->v_lflag & VNAMED_FSHASH) == 0)
3654 panic("remove_fsref: no named reference");
3655 vp->v_lflag &= ~VNAMED_FSHASH;
3656 vnode_unlock(vp);
3657 return(0);
3658
3659 }
3660
3661
3662 int
3663 vfs_iterate(__unused int flags, int (*callout)(mount_t, void *), void *arg)
3664 {
3665 mount_t mp;
3666 int ret = 0;
3667 fsid_t * fsid_list;
3668 int count, actualcount, i;
3669 void * allocmem;
3670
3671 count = mount_getvfscnt();
3672 count += 10;
3673
3674 fsid_list = (fsid_t *)kalloc(count * sizeof(fsid_t));
3675 allocmem = (void *)fsid_list;
3676
3677 actualcount = mount_fillfsids(fsid_list, count);
3678
3679 for (i=0; i< actualcount; i++) {
3680
3681 /* obtain the mount point with iteration reference */
3682 mp = mount_list_lookupby_fsid(&fsid_list[i], 0, 1);
3683
3684 if(mp == (struct mount *)0)
3685 continue;
3686 mount_lock(mp);
3687 if (mp->mnt_lflag & (MNT_LDEAD | MNT_LUNMOUNT)) {
3688 mount_unlock(mp);
3689 mount_iterdrop(mp);
3690 continue;
3691
3692 }
3693 mount_unlock(mp);
3694
3695 /* iterate over all the vnodes */
3696 ret = callout(mp, arg);
3697
3698 mount_iterdrop(mp);
3699
3700 switch (ret) {
3701 case VFS_RETURNED:
3702 case VFS_RETURNED_DONE:
3703 if (ret == VFS_RETURNED_DONE) {
3704 ret = 0;
3705 goto out;
3706 }
3707 break;
3708
3709 case VFS_CLAIMED_DONE:
3710 ret = 0;
3711 goto out;
3712 case VFS_CLAIMED:
3713 default:
3714 break;
3715 }
3716 ret = 0;
3717 }
3718
3719 out:
3720 kfree(allocmem, (count * sizeof(fsid_t)));
3721 return (ret);
3722 }
3723
3724 /*
3725 * Update the vfsstatfs structure in the mountpoint.
3726 */
3727 int
3728 vfs_update_vfsstat(mount_t mp, vfs_context_t ctx)
3729 {
3730 struct vfs_attr va;
3731 int error;
3732
3733 /*
3734 * Request the attributes we want to propagate into
3735 * the per-mount vfsstat structure.
3736 */
3737 VFSATTR_INIT(&va);
3738 VFSATTR_WANTED(&va, f_iosize);
3739 VFSATTR_WANTED(&va, f_blocks);
3740 VFSATTR_WANTED(&va, f_bfree);
3741 VFSATTR_WANTED(&va, f_bavail);
3742 VFSATTR_WANTED(&va, f_bused);
3743 VFSATTR_WANTED(&va, f_files);
3744 VFSATTR_WANTED(&va, f_ffree);
3745 VFSATTR_WANTED(&va, f_bsize);
3746 VFSATTR_WANTED(&va, f_fssubtype);
3747 if ((error = vfs_getattr(mp, &va, ctx)) != 0) {
3748 KAUTH_DEBUG("STAT - filesystem returned error %d", error);
3749 return(error);
3750 }
3751
3752 /*
3753 * Unpack into the per-mount structure.
3754 *
3755 * We only overwrite these fields, which are likely to change:
3756 * f_blocks
3757 * f_bfree
3758 * f_bavail
3759 * f_bused
3760 * f_files
3761 * f_ffree
3762 *
3763 * And these which are not, but which the FS has no other way
3764 * of providing to us:
3765 * f_bsize
3766 * f_iosize
3767 * f_fssubtype
3768 *
3769 */
3770 if (VFSATTR_IS_SUPPORTED(&va, f_bsize)) {
3771 mp->mnt_vfsstat.f_bsize = va.f_bsize;
3772 } else {
3773 mp->mnt_vfsstat.f_bsize = mp->mnt_devblocksize; /* default from the device block size */
3774 }
3775 if (VFSATTR_IS_SUPPORTED(&va, f_iosize)) {
3776 mp->mnt_vfsstat.f_iosize = va.f_iosize;
3777 } else {
3778 mp->mnt_vfsstat.f_iosize = 1024 * 1024; /* 1MB sensible I/O size */
3779 }
3780 if (VFSATTR_IS_SUPPORTED(&va, f_blocks))
3781 mp->mnt_vfsstat.f_blocks = va.f_blocks;
3782 if (VFSATTR_IS_SUPPORTED(&va, f_bfree))
3783 mp->mnt_vfsstat.f_bfree = va.f_bfree;
3784 if (VFSATTR_IS_SUPPORTED(&va, f_bavail))
3785 mp->mnt_vfsstat.f_bavail = va.f_bavail;
3786 if (VFSATTR_IS_SUPPORTED(&va, f_bused))
3787 mp->mnt_vfsstat.f_bused = va.f_bused;
3788 if (VFSATTR_IS_SUPPORTED(&va, f_files))
3789 mp->mnt_vfsstat.f_files = va.f_files;
3790 if (VFSATTR_IS_SUPPORTED(&va, f_ffree))
3791 mp->mnt_vfsstat.f_ffree = va.f_ffree;
3792
3793 /* this is unlikely to change, but has to be queried for */
3794 if (VFSATTR_IS_SUPPORTED(&va, f_fssubtype))
3795 mp->mnt_vfsstat.f_fssubtype = va.f_fssubtype;
3796
3797 return(0);
3798 }
3799
3800 void
3801 mount_list_add(mount_t mp)
3802 {
3803 mount_list_lock();
3804 TAILQ_INSERT_TAIL(&mountlist, mp, mnt_list);
3805 nummounts++;
3806 mount_list_unlock();
3807 }
3808
3809 void
3810 mount_list_remove(mount_t mp)
3811 {
3812 mount_list_lock();
3813 TAILQ_REMOVE(&mountlist, mp, mnt_list);
3814 nummounts--;
3815 mp->mnt_list.tqe_next = 0;
3816 mp->mnt_list.tqe_prev = 0;
3817 mount_list_unlock();
3818 }
3819
3820 mount_t
3821 mount_lookupby_volfsid(int volfs_id, int withref)
3822 {
3823 mount_t cur_mount = (mount_t)0;
3824 mount_t mp ;
3825
3826 mount_list_lock();
3827 TAILQ_FOREACH(mp, &mountlist, mnt_list) {
3828 if (validfsnode(mp) && mp->mnt_vfsstat.f_fsid.val[0] == volfs_id) {
3829 cur_mount = mp;
3830 if (withref) {
3831 if (mount_iterref(cur_mount, 1)) {
3832 cur_mount = (mount_t)0;
3833 mount_list_unlock();
3834 goto out;
3835 }
3836 }
3837 break;
3838 }
3839 }
3840 mount_list_unlock();
3841 if (withref && (cur_mount != (mount_t)0)) {
3842 mp = cur_mount;
3843 if (vfs_busy(mp, LK_NOWAIT) != 0) {
3844 cur_mount = (mount_t)0;
3845 }
3846 mount_iterdrop(mp);
3847 }
3848 out:
3849 return(cur_mount);
3850 }
3851
3852
3853 mount_t
3854 mount_list_lookupby_fsid(fsid, locked, withref)
3855 fsid_t *fsid;
3856 int locked;
3857 int withref;
3858 {
3859 mount_t retmp = (mount_t)0;
3860 mount_t mp;
3861
3862 if (!locked)
3863 mount_list_lock();
3864 TAILQ_FOREACH(mp, &mountlist, mnt_list)
3865 if (mp->mnt_vfsstat.f_fsid.val[0] == fsid->val[0] &&
3866 mp->mnt_vfsstat.f_fsid.val[1] == fsid->val[1]) {
3867 retmp = mp;
3868 if (withref) {
3869 if (mount_iterref(retmp, 1))
3870 retmp = (mount_t)0;
3871 }
3872 goto out;
3873 }
3874 out:
3875 if (!locked)
3876 mount_list_unlock();
3877 return (retmp);
3878 }
3879
3880 errno_t
3881 vnode_lookup(const char *path, int flags, vnode_t *vpp, vfs_context_t context)
3882 {
3883 struct nameidata nd;
3884 int error;
3885 struct vfs_context context2;
3886 vfs_context_t ctx = context;
3887 u_long ndflags = 0;
3888
3889 if (context == NULL) { /* XXX technically an error */
3890 context2.vc_proc = current_proc();
3891 context2.vc_ucred = kauth_cred_get();
3892 ctx = &context2;
3893 }
3894
3895 if (flags & VNODE_LOOKUP_NOFOLLOW)
3896 ndflags = NOFOLLOW;
3897 else
3898 ndflags = FOLLOW;
3899
3900 if (flags & VNODE_LOOKUP_NOCROSSMOUNT)
3901 ndflags |= NOCROSSMOUNT;
3902 if (flags & VNODE_LOOKUP_DOWHITEOUT)
3903 ndflags |= DOWHITEOUT;
3904
3905 /* XXX AUDITVNPATH1 needed ? */
3906 NDINIT(&nd, LOOKUP, ndflags, UIO_SYSSPACE, CAST_USER_ADDR_T(path), ctx);
3907
3908 if ((error = namei(&nd)))
3909 return (error);
3910 *vpp = nd.ni_vp;
3911 nameidone(&nd);
3912
3913 return (0);
3914 }
3915
3916 errno_t
3917 vnode_open(const char *path, int fmode, int cmode, int flags, vnode_t *vpp, vfs_context_t context)
3918 {
3919 struct nameidata nd;
3920 int error;
3921 struct vfs_context context2;
3922 vfs_context_t ctx = context;
3923 u_long ndflags = 0;
3924 int lflags = flags;
3925
3926 if (context == NULL) { /* XXX technically an error */
3927 context2.vc_proc = current_proc();
3928 context2.vc_ucred = kauth_cred_get();
3929 ctx = &context2;
3930 }
3931
3932 if (fmode & O_NOFOLLOW)
3933 lflags |= VNODE_LOOKUP_NOFOLLOW;
3934
3935 if (lflags & VNODE_LOOKUP_NOFOLLOW)
3936 ndflags = NOFOLLOW;
3937 else
3938 ndflags = FOLLOW;
3939
3940 if (lflags & VNODE_LOOKUP_NOCROSSMOUNT)
3941 ndflags |= NOCROSSMOUNT;
3942 if (lflags & VNODE_LOOKUP_DOWHITEOUT)
3943 ndflags |= DOWHITEOUT;
3944
3945 /* XXX AUDITVNPATH1 needed ? */
3946 NDINIT(&nd, LOOKUP, ndflags, UIO_SYSSPACE, CAST_USER_ADDR_T(path), ctx);
3947
3948 if ((error = vn_open(&nd, fmode, cmode)))
3949 *vpp = NULL;
3950 else
3951 *vpp = nd.ni_vp;
3952
3953 return (error);
3954 }
3955
3956 errno_t
3957 vnode_close(vnode_t vp, int flags, vfs_context_t context)
3958 {
3959 kauth_cred_t cred;
3960 struct proc *p;
3961 int error;
3962
3963 if (context) {
3964 p = context->vc_proc;
3965 cred = context->vc_ucred;
3966 } else {
3967 p = current_proc();
3968 cred = kauth_cred_get();
3969 }
3970
3971 error = vn_close(vp, flags, cred, p);
3972 vnode_put(vp);
3973 return (error);
3974 }
3975
3976 errno_t
3977 vnode_size(vnode_t vp, off_t *sizep, vfs_context_t ctx)
3978 {
3979 struct vnode_attr va;
3980 int error;
3981
3982 VATTR_INIT(&va);
3983 VATTR_WANTED(&va, va_data_size);
3984 error = vnode_getattr(vp, &va, ctx);
3985 if (!error)
3986 *sizep = va.va_data_size;
3987 return(error);
3988 }
3989
3990 errno_t
3991 vnode_setsize(vnode_t vp, off_t size, int ioflag, vfs_context_t ctx)
3992 {
3993 struct vnode_attr va;
3994
3995 VATTR_INIT(&va);
3996 VATTR_SET(&va, va_data_size, size);
3997 va.va_vaflags = ioflag & 0xffff;
3998 return(vnode_setattr(vp, &va, ctx));
3999 }
4000
4001 errno_t
4002 vn_create(vnode_t dvp, vnode_t *vpp, struct componentname *cnp, struct vnode_attr *vap, int flags, vfs_context_t ctx)
4003 {
4004 kauth_acl_t oacl, nacl;
4005 int initial_acl;
4006 errno_t error;
4007 vnode_t vp = (vnode_t)0;
4008
4009 error = 0;
4010 oacl = nacl = NULL;
4011 initial_acl = 0;
4012
4013 KAUTH_DEBUG("%p CREATE - '%s'", dvp, cnp->cn_nameptr);
4014
4015 /*
4016 * Handle ACL inheritance.
4017 */
4018 if (!(flags & VN_CREATE_NOINHERIT) && vfs_extendedsecurity(dvp->v_mount)) {
4019 /* save the original filesec */
4020 if (VATTR_IS_ACTIVE(vap, va_acl)) {
4021 initial_acl = 1;
4022 oacl = vap->va_acl;
4023 }
4024
4025 vap->va_acl = NULL;
4026 if ((error = kauth_acl_inherit(dvp,
4027 oacl,
4028 &nacl,
4029 vap->va_type == VDIR,
4030 ctx)) != 0) {
4031 KAUTH_DEBUG("%p CREATE - error %d processing inheritance", dvp, error);
4032 return(error);
4033 }
4034
4035 /*
4036 * If the generated ACL is NULL, then we can save ourselves some effort
4037 * by clearing the active bit.
4038 */
4039 if (nacl == NULL) {
4040 VATTR_CLEAR_ACTIVE(vap, va_acl);
4041 } else {
4042 VATTR_SET(vap, va_acl, nacl);
4043 }
4044 }
4045
4046 /*
4047 * Check and default new attributes.
4048 * This will set va_uid, va_gid, va_mode and va_create_time at least, if the caller
4049 * hasn't supplied them.
4050 */
4051 if ((error = vnode_authattr_new(dvp, vap, flags & VN_CREATE_NOAUTH, ctx)) != 0) {
4052 KAUTH_DEBUG("%p CREATE - error %d handing/defaulting attributes", dvp, error);
4053 goto out;
4054 }
4055
4056
4057 /*
4058 * Create the requested node.
4059 */
4060 switch(vap->va_type) {
4061 case VREG:
4062 error = VNOP_CREATE(dvp, vpp, cnp, vap, ctx);
4063 break;
4064 case VDIR:
4065 error = VNOP_MKDIR(dvp, vpp, cnp, vap, ctx);
4066 break;
4067 case VSOCK:
4068 case VFIFO:
4069 case VBLK:
4070 case VCHR:
4071 error = VNOP_MKNOD(dvp, vpp, cnp, vap, ctx);
4072 break;
4073 default:
4074 panic("vnode_create: unknown vtype %d", vap->va_type);
4075 }
4076 if (error != 0) {
4077 KAUTH_DEBUG("%p CREATE - error %d returned by filesystem", dvp, error);
4078 goto out;
4079 }
4080
4081 vp = *vpp;
4082 /*
4083 * If some of the requested attributes weren't handled by the VNOP,
4084 * use our fallback code.
4085 */
4086 if (!VATTR_ALL_SUPPORTED(vap) && *vpp) {
4087 KAUTH_DEBUG(" CREATE - doing fallback with ACL %p", vap->va_acl);
4088 error = vnode_setattr_fallback(*vpp, vap, ctx);
4089 }
4090 if ((error != 0 ) && (vp != (vnode_t)0)) {
4091 *vpp = (vnode_t) 0;
4092 vnode_put(vp);
4093 }
4094
4095 out:
4096 /*
4097 * If the caller supplied a filesec in vap, it has been replaced
4098 * now by the post-inheritance copy. We need to put the original back
4099 * and free the inherited product.
4100 */
4101 if (initial_acl) {
4102 VATTR_SET(vap, va_acl, oacl);
4103 } else {
4104 VATTR_CLEAR_ACTIVE(vap, va_acl);
4105 }
4106 if (nacl != NULL)
4107 kauth_acl_free(nacl);
4108
4109 return(error);
4110 }
4111
4112 static kauth_scope_t vnode_scope;
4113 static int vnode_authorize_callback(kauth_cred_t credential, __unused void *idata, kauth_action_t action,
4114 uintptr_t arg0, uintptr_t arg1, uintptr_t arg2, uintptr_t arg3);
4115
4116 typedef struct _vnode_authorize_context {
4117 vnode_t vp;
4118 struct vnode_attr *vap;
4119 vnode_t dvp;
4120 struct vnode_attr *dvap;
4121 vfs_context_t ctx;
4122 int flags;
4123 int flags_valid;
4124 #define _VAC_IS_OWNER (1<<0)
4125 #define _VAC_IN_GROUP (1<<1)
4126 #define _VAC_IS_DIR_OWNER (1<<2)
4127 #define _VAC_IN_DIR_GROUP (1<<3)
4128 } *vauth_ctx;
4129
4130 void
4131 vnode_authorize_init(void)
4132 {
4133 vnode_scope = kauth_register_scope(KAUTH_SCOPE_VNODE, vnode_authorize_callback, NULL);
4134 }
4135
4136 /*
4137 * Authorize an operation on a vnode.
4138 *
4139 * This is KPI, but here because it needs vnode_scope.
4140 */
4141 int
4142 vnode_authorize(vnode_t vp, vnode_t dvp, kauth_action_t action, vfs_context_t context)
4143 {
4144 int error, result;
4145
4146 /*
4147 * We can't authorize against a dead vnode; allow all operations through so that
4148 * the correct error can be returned.
4149 */
4150 if (vp->v_type == VBAD)
4151 return(0);
4152
4153 error = 0;
4154 result = kauth_authorize_action(vnode_scope, vfs_context_ucred(context), action,
4155 (uintptr_t)context, (uintptr_t)vp, (uintptr_t)dvp, (uintptr_t)&error);
4156 if (result == EPERM) /* traditional behaviour */
4157 result = EACCES;
4158 /* did the lower layers give a better error return? */
4159 if ((result != 0) && (error != 0))
4160 return(error);
4161 return(result);
4162 }
4163
4164 /*
4165 * Test for vnode immutability.
4166 *
4167 * The 'append' flag is set when the authorization request is constrained
4168 * to operations which only request the right to append to a file.
4169 *
4170 * The 'ignore' flag is set when an operation modifying the immutability flags
4171 * is being authorized. We check the system securelevel to determine which
4172 * immutability flags we can ignore.
4173 */
4174 static int
4175 vnode_immutable(struct vnode_attr *vap, int append, int ignore)
4176 {
4177 int mask;
4178
4179 /* start with all bits precluding the operation */
4180 mask = IMMUTABLE | APPEND;
4181
4182 /* if appending only, remove the append-only bits */
4183 if (append)
4184 mask &= ~APPEND;
4185
4186 /* ignore only set when authorizing flags changes */
4187 if (ignore) {
4188 if (securelevel <= 0) {
4189 /* in insecure state, flags do not inhibit changes */
4190 mask = 0;
4191 } else {
4192 /* in secure state, user flags don't inhibit */
4193 mask &= ~(UF_IMMUTABLE | UF_APPEND);
4194 }
4195 }
4196 KAUTH_DEBUG("IMMUTABLE - file flags 0x%x mask 0x%x append = %d ignore = %d", vap->va_flags, mask, append, ignore);
4197 if ((vap->va_flags & mask) != 0)
4198 return(EPERM);
4199 return(0);
4200 }
4201
4202 static int
4203 vauth_node_owner(struct vnode_attr *vap, kauth_cred_t cred)
4204 {
4205 int result;
4206
4207 /* default assumption is not-owner */
4208 result = 0;
4209
4210 /*
4211 * If the filesystem has given us a UID, we treat this as authoritative.
4212 */
4213 if (vap && VATTR_IS_SUPPORTED(vap, va_uid)) {
4214 result = (vap->va_uid == kauth_cred_getuid(cred)) ? 1 : 0;
4215 }
4216 /* we could test the owner UUID here if we had a policy for it */
4217
4218 return(result);
4219 }
4220
4221 static int
4222 vauth_node_group(struct vnode_attr *vap, kauth_cred_t cred, int *ismember)
4223 {
4224 int error;
4225 int result;
4226
4227 error = 0;
4228 result = 0;
4229
4230 /* the caller is expected to have asked the filesystem for a group at some point */
4231 if (vap && VATTR_IS_SUPPORTED(vap, va_gid)) {
4232 error = kauth_cred_ismember_gid(cred, vap->va_gid, &result);
4233 }
4234 /* we could test the group UUID here if we had a policy for it */
4235
4236 if (!error)
4237 *ismember = result;
4238 return(error);
4239 }
4240
4241 static int
4242 vauth_file_owner(vauth_ctx vcp)
4243 {
4244 int result;
4245
4246 if (vcp->flags_valid & _VAC_IS_OWNER) {
4247 result = (vcp->flags & _VAC_IS_OWNER) ? 1 : 0;
4248 } else {
4249 result = vauth_node_owner(vcp->vap, vcp->ctx->vc_ucred);
4250
4251 /* cache our result */
4252 vcp->flags_valid |= _VAC_IS_OWNER;
4253 if (result) {
4254 vcp->flags |= _VAC_IS_OWNER;
4255 } else {
4256 vcp->flags &= ~_VAC_IS_OWNER;
4257 }
4258 }
4259 return(result);
4260 }
4261
4262 static int
4263 vauth_file_ingroup(vauth_ctx vcp, int *ismember)
4264 {
4265 int error;
4266
4267 if (vcp->flags_valid & _VAC_IN_GROUP) {
4268 *ismember = (vcp->flags & _VAC_IN_GROUP) ? 1 : 0;
4269 error = 0;
4270 } else {
4271 error = vauth_node_group(vcp->vap, vcp->ctx->vc_ucred, ismember);
4272
4273 if (!error) {
4274 /* cache our result */
4275 vcp->flags_valid |= _VAC_IN_GROUP;
4276 if (*ismember) {
4277 vcp->flags |= _VAC_IN_GROUP;
4278 } else {
4279 vcp->flags &= ~_VAC_IN_GROUP;
4280 }
4281 }
4282
4283 }
4284 return(error);
4285 }
4286
4287 static int
4288 vauth_dir_owner(vauth_ctx vcp)
4289 {
4290 int result;
4291
4292 if (vcp->flags_valid & _VAC_IS_DIR_OWNER) {
4293 result = (vcp->flags & _VAC_IS_DIR_OWNER) ? 1 : 0;
4294 } else {
4295 result = vauth_node_owner(vcp->dvap, vcp->ctx->vc_ucred);
4296
4297 /* cache our result */
4298 vcp->flags_valid |= _VAC_IS_DIR_OWNER;
4299 if (result) {
4300 vcp->flags |= _VAC_IS_DIR_OWNER;
4301 } else {
4302 vcp->flags &= ~_VAC_IS_DIR_OWNER;
4303 }
4304 }
4305 return(result);
4306 }
4307
4308 static int
4309 vauth_dir_ingroup(vauth_ctx vcp, int *ismember)
4310 {
4311 int error;
4312
4313 if (vcp->flags_valid & _VAC_IN_DIR_GROUP) {
4314 *ismember = (vcp->flags & _VAC_IN_DIR_GROUP) ? 1 : 0;
4315 error = 0;
4316 } else {
4317 error = vauth_node_group(vcp->dvap, vcp->ctx->vc_ucred, ismember);
4318
4319 if (!error) {
4320 /* cache our result */
4321 vcp->flags_valid |= _VAC_IN_DIR_GROUP;
4322 if (*ismember) {
4323 vcp->flags |= _VAC_IN_DIR_GROUP;
4324 } else {
4325 vcp->flags &= ~_VAC_IN_DIR_GROUP;
4326 }
4327 }
4328 }
4329 return(error);
4330 }
4331
4332 /*
4333 * Test the posix permissions in (vap) to determine whether (credential)
4334 * may perform (action)
4335 */
4336 static int
4337 vnode_authorize_posix(vauth_ctx vcp, int action, int on_dir)
4338 {
4339 struct vnode_attr *vap;
4340 int needed, error, owner_ok, group_ok, world_ok, ismember;
4341 #ifdef KAUTH_DEBUG_ENABLE
4342 const char *where;
4343 # define _SETWHERE(c) where = c;
4344 #else
4345 # define _SETWHERE(c)
4346 #endif
4347
4348 /* checking file or directory? */
4349 if (on_dir) {
4350 vap = vcp->dvap;
4351 } else {
4352 vap = vcp->vap;
4353 }
4354
4355 error = 0;
4356
4357 /*
4358 * We want to do as little work here as possible. So first we check
4359 * which sets of permissions grant us the access we need, and avoid checking
4360 * whether specific permissions grant access when more generic ones would.
4361 */
4362
4363 /* owner permissions */
4364 needed = 0;
4365 if (action & VREAD)
4366 needed |= S_IRUSR;
4367 if (action & VWRITE)
4368 needed |= S_IWUSR;
4369 if (action & VEXEC)
4370 needed |= S_IXUSR;
4371 owner_ok = (needed & vap->va_mode) == needed;
4372
4373 /* group permissions */
4374 needed = 0;
4375 if (action & VREAD)
4376 needed |= S_IRGRP;
4377 if (action & VWRITE)
4378 needed |= S_IWGRP;
4379 if (action & VEXEC)
4380 needed |= S_IXGRP;
4381 group_ok = (needed & vap->va_mode) == needed;
4382
4383 /* world permissions */
4384 needed = 0;
4385 if (action & VREAD)
4386 needed |= S_IROTH;
4387 if (action & VWRITE)
4388 needed |= S_IWOTH;
4389 if (action & VEXEC)
4390 needed |= S_IXOTH;
4391 world_ok = (needed & vap->va_mode) == needed;
4392
4393 /* If granted/denied by all three, we're done */
4394 if (owner_ok && group_ok && world_ok) {
4395 _SETWHERE("all");
4396 goto out;
4397 }
4398 if (!owner_ok && !group_ok && !world_ok) {
4399 _SETWHERE("all");
4400 error = EACCES;
4401 goto out;
4402 }
4403
4404 /* Check ownership (relatively cheap) */
4405 if ((on_dir && vauth_dir_owner(vcp)) ||
4406 (!on_dir && vauth_file_owner(vcp))) {
4407 _SETWHERE("user");
4408 if (!owner_ok)
4409 error = EACCES;
4410 goto out;
4411 }
4412
4413 /* Not owner; if group and world both grant it we're done */
4414 if (group_ok && world_ok) {
4415 _SETWHERE("group/world");
4416 goto out;
4417 }
4418 if (!group_ok && !world_ok) {
4419 _SETWHERE("group/world");
4420 error = EACCES;
4421 goto out;
4422 }
4423
4424 /* Check group membership (most expensive) */
4425 ismember = 0;
4426 if (on_dir) {
4427 error = vauth_dir_ingroup(vcp, &ismember);
4428 } else {
4429 error = vauth_file_ingroup(vcp, &ismember);
4430 }
4431 if (error)
4432 goto out;
4433 if (ismember) {
4434 _SETWHERE("group");
4435 if (!group_ok)
4436 error = EACCES;
4437 goto out;
4438 }
4439
4440 /* Not owner, not in group, use world result */
4441 _SETWHERE("world");
4442 if (!world_ok)
4443 error = EACCES;
4444
4445 /* FALLTHROUGH */
4446
4447 out:
4448 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",
4449 vcp->vp, (error == 0) ? "ALLOWED" : "DENIED", where,
4450 (action & VREAD) ? "r" : "-",
4451 (action & VWRITE) ? "w" : "-",
4452 (action & VEXEC) ? "x" : "-",
4453 needed,
4454 (vap->va_mode & S_IRUSR) ? "r" : "-",
4455 (vap->va_mode & S_IWUSR) ? "w" : "-",
4456 (vap->va_mode & S_IXUSR) ? "x" : "-",
4457 (vap->va_mode & S_IRGRP) ? "r" : "-",
4458 (vap->va_mode & S_IWGRP) ? "w" : "-",
4459 (vap->va_mode & S_IXGRP) ? "x" : "-",
4460 (vap->va_mode & S_IROTH) ? "r" : "-",
4461 (vap->va_mode & S_IWOTH) ? "w" : "-",
4462 (vap->va_mode & S_IXOTH) ? "x" : "-",
4463 kauth_cred_getuid(vcp->ctx->vc_ucred),
4464 on_dir ? vcp->dvap->va_uid : vcp->vap->va_uid,
4465 on_dir ? vcp->dvap->va_gid : vcp->vap->va_gid);
4466 return(error);
4467 }
4468
4469 /*
4470 * Authorize the deletion of the node vp from the directory dvp.
4471 *
4472 * We assume that:
4473 * - Neither the node nor the directory are immutable.
4474 * - The user is not the superuser.
4475 *
4476 * Deletion is not permitted if the directory is sticky and the caller is not owner of the
4477 * node or directory.
4478 *
4479 * If either the node grants DELETE, or the directory grants DELETE_CHILD, the node may be
4480 * deleted. If neither denies the permission, and the caller has Posix write access to the
4481 * directory, then the node may be deleted.
4482 */
4483 static int
4484 vnode_authorize_delete(vauth_ctx vcp)
4485 {
4486 struct vnode_attr *vap = vcp->vap;
4487 struct vnode_attr *dvap = vcp->dvap;
4488 kauth_cred_t cred = vcp->ctx->vc_ucred;
4489 struct kauth_acl_eval eval;
4490 int error, delete_denied, delete_child_denied, ismember;
4491
4492 /* check the ACL on the directory */
4493 delete_child_denied = 0;
4494 if (VATTR_IS_NOT(dvap, va_acl, NULL)) {
4495 eval.ae_requested = KAUTH_VNODE_DELETE_CHILD;
4496 eval.ae_acl = &dvap->va_acl->acl_ace[0];
4497 eval.ae_count = dvap->va_acl->acl_entrycount;
4498 eval.ae_options = 0;
4499 if (vauth_dir_owner(vcp))
4500 eval.ae_options |= KAUTH_AEVAL_IS_OWNER;
4501 if ((error = vauth_dir_ingroup(vcp, &ismember)) != 0)
4502 return(error);
4503 if (ismember)
4504 eval.ae_options |= KAUTH_AEVAL_IN_GROUP;
4505 eval.ae_exp_gall = KAUTH_VNODE_GENERIC_ALL_BITS;
4506 eval.ae_exp_gread = KAUTH_VNODE_GENERIC_READ_BITS;
4507 eval.ae_exp_gwrite = KAUTH_VNODE_GENERIC_WRITE_BITS;
4508 eval.ae_exp_gexec = KAUTH_VNODE_GENERIC_EXECUTE_BITS;
4509
4510 error = kauth_acl_evaluate(cred, &eval);
4511
4512 if (error != 0) {
4513 KAUTH_DEBUG("%p ERROR during ACL processing - %d", vcp->vp, error);
4514 return(error);
4515 }
4516 if (eval.ae_result == KAUTH_RESULT_DENY)
4517 delete_child_denied = 1;
4518 if (eval.ae_result == KAUTH_RESULT_ALLOW) {
4519 KAUTH_DEBUG("%p ALLOWED - granted by directory ACL", vcp->vp);
4520 return(0);
4521 }
4522 }
4523
4524 /* check the ACL on the node */
4525 delete_denied = 0;
4526 if (VATTR_IS_NOT(vap, va_acl, NULL)) {
4527 eval.ae_requested = KAUTH_VNODE_DELETE;
4528 eval.ae_acl = &vap->va_acl->acl_ace[0];
4529 eval.ae_count = vap->va_acl->acl_entrycount;
4530 eval.ae_options = 0;
4531 if (vauth_file_owner(vcp))
4532 eval.ae_options |= KAUTH_AEVAL_IS_OWNER;
4533 if ((error = vauth_file_ingroup(vcp, &ismember)) != 0)
4534 return(error);
4535 if (ismember)
4536 eval.ae_options |= KAUTH_AEVAL_IN_GROUP;
4537 eval.ae_exp_gall = KAUTH_VNODE_GENERIC_ALL_BITS;
4538 eval.ae_exp_gread = KAUTH_VNODE_GENERIC_READ_BITS;
4539 eval.ae_exp_gwrite = KAUTH_VNODE_GENERIC_WRITE_BITS;
4540 eval.ae_exp_gexec = KAUTH_VNODE_GENERIC_EXECUTE_BITS;
4541
4542 if ((error = kauth_acl_evaluate(cred, &eval)) != 0) {
4543 KAUTH_DEBUG("%p ERROR during ACL processing - %d", vcp->vp, error);
4544 return(error);
4545 }
4546 if (eval.ae_result == KAUTH_RESULT_DENY)
4547 delete_denied = 1;
4548 if (eval.ae_result == KAUTH_RESULT_ALLOW) {
4549 KAUTH_DEBUG("%p ALLOWED - granted by file ACL", vcp->vp);
4550 return(0);
4551 }
4552 }
4553
4554 /* if denied by ACL on directory or node, return denial */
4555 if (delete_denied || delete_child_denied) {
4556 KAUTH_DEBUG("%p ALLOWED - denied by ACL", vcp->vp);
4557 return(EACCES);
4558 }
4559
4560 /* enforce sticky bit behaviour */
4561 if ((dvap->va_mode & S_ISTXT) && !vauth_file_owner(vcp) && !vauth_dir_owner(vcp)) {
4562 KAUTH_DEBUG("%p DENIED - sticky bit rules (user %d file %d dir %d)",
4563 vcp->vp, cred->cr_uid, vap->va_uid, dvap->va_uid);
4564 return(EACCES);
4565 }
4566
4567 /* check the directory */
4568 if ((error = vnode_authorize_posix(vcp, VWRITE, 1 /* on_dir */)) != 0) {
4569 KAUTH_DEBUG("%p ALLOWED - granted by posix permisssions", vcp->vp);
4570 return(error);
4571 }
4572
4573 /* not denied, must be OK */
4574 return(0);
4575 }
4576
4577
4578 /*
4579 * Authorize an operation based on the node's attributes.
4580 */
4581 static int
4582 vnode_authorize_simple(vauth_ctx vcp, kauth_ace_rights_t acl_rights, kauth_ace_rights_t preauth_rights)
4583 {
4584 struct vnode_attr *vap = vcp->vap;
4585 kauth_cred_t cred = vcp->ctx->vc_ucred;
4586 struct kauth_acl_eval eval;
4587 int error, ismember;
4588 mode_t posix_action;
4589
4590 /*
4591 * If we are the file owner, we automatically have some rights.
4592 *
4593 * Do we need to expand this to support group ownership?
4594 */
4595 if (vauth_file_owner(vcp))
4596 acl_rights &= ~(KAUTH_VNODE_WRITE_SECURITY);
4597
4598 /*
4599 * If we are checking both TAKE_OWNERSHIP and WRITE_SECURITY, we can
4600 * mask the latter. If TAKE_OWNERSHIP is requested the caller is about to
4601 * change ownership to themselves, and WRITE_SECURITY is implicitly
4602 * granted to the owner. We need to do this because at this point
4603 * WRITE_SECURITY may not be granted as the caller is not currently
4604 * the owner.
4605 */
4606 if ((acl_rights & KAUTH_VNODE_TAKE_OWNERSHIP) &&
4607 (acl_rights & KAUTH_VNODE_WRITE_SECURITY))
4608 acl_rights &= ~KAUTH_VNODE_WRITE_SECURITY;
4609
4610 if (acl_rights == 0) {
4611 KAUTH_DEBUG("%p ALLOWED - implicit or no rights required", vcp->vp);
4612 return(0);
4613 }
4614
4615 /* if we have an ACL, evaluate it */
4616 if (VATTR_IS_NOT(vap, va_acl, NULL)) {
4617 eval.ae_requested = acl_rights;
4618 eval.ae_acl = &vap->va_acl->acl_ace[0];
4619 eval.ae_count = vap->va_acl->acl_entrycount;
4620 eval.ae_options = 0;
4621 if (vauth_file_owner(vcp))
4622 eval.ae_options |= KAUTH_AEVAL_IS_OWNER;
4623 if ((error = vauth_file_ingroup(vcp, &ismember)) != 0)
4624 return(error);
4625 if (ismember)
4626 eval.ae_options |= KAUTH_AEVAL_IN_GROUP;
4627 eval.ae_exp_gall = KAUTH_VNODE_GENERIC_ALL_BITS;
4628 eval.ae_exp_gread = KAUTH_VNODE_GENERIC_READ_BITS;
4629 eval.ae_exp_gwrite = KAUTH_VNODE_GENERIC_WRITE_BITS;
4630 eval.ae_exp_gexec = KAUTH_VNODE_GENERIC_EXECUTE_BITS;
4631
4632 if ((error = kauth_acl_evaluate(cred, &eval)) != 0) {
4633 KAUTH_DEBUG("%p ERROR during ACL processing - %d", vcp->vp, error);
4634 return(error);
4635 }
4636
4637 if (eval.ae_result == KAUTH_RESULT_DENY) {
4638 KAUTH_DEBUG("%p DENIED - by ACL", vcp->vp);
4639 return(EACCES); /* deny, deny, counter-allege */
4640 }
4641 if (eval.ae_result == KAUTH_RESULT_ALLOW) {
4642 KAUTH_DEBUG("%p ALLOWED - all rights granted by ACL", vcp->vp);
4643 return(0);
4644 }
4645 /* fall through and evaluate residual rights */
4646 } else {
4647 /* no ACL, everything is residual */
4648 eval.ae_residual = acl_rights;
4649 }
4650
4651 /*
4652 * Grant residual rights that have been pre-authorized.
4653 */
4654 eval.ae_residual &= ~preauth_rights;
4655
4656 /*
4657 * We grant WRITE_ATTRIBUTES to the owner if it hasn't been denied.
4658 */
4659 if (vauth_file_owner(vcp))
4660 eval.ae_residual &= ~KAUTH_VNODE_WRITE_ATTRIBUTES;
4661
4662 if (eval.ae_residual == 0) {
4663 KAUTH_DEBUG("%p ALLOWED - rights already authorized", vcp->vp);
4664 return(0);
4665 }
4666
4667 /*
4668 * Bail if we have residual rights that can't be granted by posix permissions,
4669 * or aren't presumed granted at this point.
4670 *
4671 * XXX these can be collapsed for performance
4672 */
4673 if (eval.ae_residual & KAUTH_VNODE_CHANGE_OWNER) {
4674 KAUTH_DEBUG("%p DENIED - CHANGE_OWNER not permitted", vcp->vp);
4675 return(EACCES);
4676 }
4677 if (eval.ae_residual & KAUTH_VNODE_WRITE_SECURITY) {
4678 KAUTH_DEBUG("%p DENIED - WRITE_SECURITY not permitted", vcp->vp);
4679 return(EACCES);
4680 }
4681
4682 #if DIAGNOSTIC
4683 if (eval.ae_residual & KAUTH_VNODE_DELETE)
4684 panic("vnode_authorize: can't be checking delete permission here");
4685 #endif
4686
4687 /*
4688 * Compute the fallback posix permissions that will satisfy the remaining
4689 * rights.
4690 */
4691 posix_action = 0;
4692 if (eval.ae_residual & (KAUTH_VNODE_READ_DATA |
4693 KAUTH_VNODE_LIST_DIRECTORY |
4694 KAUTH_VNODE_READ_EXTATTRIBUTES))
4695 posix_action |= VREAD;
4696 if (eval.ae_residual & (KAUTH_VNODE_WRITE_DATA |
4697 KAUTH_VNODE_ADD_FILE |
4698 KAUTH_VNODE_ADD_SUBDIRECTORY |
4699 KAUTH_VNODE_DELETE_CHILD |
4700 KAUTH_VNODE_WRITE_ATTRIBUTES |
4701 KAUTH_VNODE_WRITE_EXTATTRIBUTES))
4702 posix_action |= VWRITE;
4703 if (eval.ae_residual & (KAUTH_VNODE_EXECUTE |
4704 KAUTH_VNODE_SEARCH))
4705 posix_action |= VEXEC;
4706
4707 if (posix_action != 0) {
4708 return(vnode_authorize_posix(vcp, posix_action, 0 /* !on_dir */));
4709 } else {
4710 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",
4711 vcp->vp,
4712 (eval.ae_residual & KAUTH_VNODE_READ_DATA)
4713 ? vnode_isdir(vcp->vp) ? " LIST_DIRECTORY" : " READ_DATA" : "",
4714 (eval.ae_residual & KAUTH_VNODE_WRITE_DATA)
4715 ? vnode_isdir(vcp->vp) ? " ADD_FILE" : " WRITE_DATA" : "",
4716 (eval.ae_residual & KAUTH_VNODE_EXECUTE)
4717 ? vnode_isdir(vcp->vp) ? " SEARCH" : " EXECUTE" : "",
4718 (eval.ae_residual & KAUTH_VNODE_DELETE)
4719 ? " DELETE" : "",
4720 (eval.ae_residual & KAUTH_VNODE_APPEND_DATA)
4721 ? vnode_isdir(vcp->vp) ? " ADD_SUBDIRECTORY" : " APPEND_DATA" : "",
4722 (eval.ae_residual & KAUTH_VNODE_DELETE_CHILD)
4723 ? " DELETE_CHILD" : "",
4724 (eval.ae_residual & KAUTH_VNODE_READ_ATTRIBUTES)
4725 ? " READ_ATTRIBUTES" : "",
4726 (eval.ae_residual & KAUTH_VNODE_WRITE_ATTRIBUTES)
4727 ? " WRITE_ATTRIBUTES" : "",
4728 (eval.ae_residual & KAUTH_VNODE_READ_EXTATTRIBUTES)
4729 ? " READ_EXTATTRIBUTES" : "",
4730 (eval.ae_residual & KAUTH_VNODE_WRITE_EXTATTRIBUTES)
4731 ? " WRITE_EXTATTRIBUTES" : "",
4732 (eval.ae_residual & KAUTH_VNODE_READ_SECURITY)
4733 ? " READ_SECURITY" : "",
4734 (eval.ae_residual & KAUTH_VNODE_WRITE_SECURITY)
4735 ? " WRITE_SECURITY" : "",
4736 (eval.ae_residual & KAUTH_VNODE_CHECKIMMUTABLE)
4737 ? " CHECKIMMUTABLE" : "",
4738 (eval.ae_residual & KAUTH_VNODE_CHANGE_OWNER)
4739 ? " CHANGE_OWNER" : "");
4740 }
4741
4742 /*
4743 * Lack of required Posix permissions implies no reason to deny access.
4744 */
4745 return(0);
4746 }
4747
4748 /*
4749 * Check for file immutability.
4750 */
4751 static int
4752 vnode_authorize_checkimmutable(vnode_t vp, struct vnode_attr *vap, int rights, int ignore)
4753 {
4754 mount_t mp;
4755 int error;
4756 int append;
4757
4758 /*
4759 * Perform immutability checks for operations that change data.
4760 *
4761 * Sockets, fifos and devices require special handling.
4762 */
4763 switch(vp->v_type) {
4764 case VSOCK:
4765 case VFIFO:
4766 case VBLK:
4767 case VCHR:
4768 /*
4769 * Writing to these nodes does not change the filesystem data,
4770 * so forget that it's being tried.
4771 */
4772 rights &= ~KAUTH_VNODE_WRITE_DATA;
4773 break;
4774 default:
4775 break;
4776 }
4777
4778 error = 0;
4779 if (rights & KAUTH_VNODE_WRITE_RIGHTS) {
4780
4781 /* check per-filesystem options if possible */
4782 mp = vnode_mount(vp);
4783 if (mp != NULL) {
4784
4785 /* check for no-EA filesystems */
4786 if ((rights & KAUTH_VNODE_WRITE_EXTATTRIBUTES) &&
4787 (vfs_flags(mp) & MNT_NOUSERXATTR)) {
4788 KAUTH_DEBUG("%p DENIED - filesystem disallowed extended attributes", vp);
4789 error = EACCES; /* User attributes disabled */
4790 goto out;
4791 }
4792 }
4793
4794 /* check for file immutability */
4795 append = 0;
4796 if (vp->v_type == VDIR) {
4797 if ((rights & (KAUTH_VNODE_ADD_FILE | KAUTH_VNODE_ADD_SUBDIRECTORY)) == rights)
4798 append = 1;
4799 } else {
4800 if ((rights & KAUTH_VNODE_APPEND_DATA) == rights)
4801 append = 1;
4802 }
4803 if ((error = vnode_immutable(vap, append, ignore)) != 0) {
4804 KAUTH_DEBUG("%p DENIED - file is immutable", vp);
4805 goto out;
4806 }
4807 }
4808 out:
4809 return(error);
4810 }
4811
4812 /*
4813 * Handle authorization actions for filesystems that advertise that the server will
4814 * be enforcing.
4815 */
4816 static int
4817 vnode_authorize_opaque(vnode_t vp, int *resultp, kauth_action_t action, vfs_context_t ctx)
4818 {
4819 int error;
4820
4821 /*
4822 * If the vp is a device node, socket or FIFO it actually represents a local
4823 * endpoint, so we need to handle it locally.
4824 */
4825 switch(vp->v_type) {
4826 case VBLK:
4827 case VCHR:
4828 case VSOCK:
4829 case VFIFO:
4830 return(0);
4831 default:
4832 break;
4833 }
4834
4835 /*
4836 * In the advisory request case, if the filesystem doesn't think it's reliable
4837 * we will attempt to formulate a result ourselves based on VNOP_GETATTR data.
4838 */
4839 if ((action & KAUTH_VNODE_ACCESS) && !vfs_authopaqueaccess(vnode_mount(vp)))
4840 return(0);
4841
4842 /*
4843 * Let the filesystem have a say in the matter. It's OK for it to not implemnent
4844 * VNOP_ACCESS, as most will authorise inline with the actual request.
4845 */
4846 if ((error = VNOP_ACCESS(vp, action, ctx)) != ENOTSUP) {
4847 *resultp = error;
4848 KAUTH_DEBUG("%p DENIED - opaque filesystem VNOP_ACCESS denied access", vp);
4849 return(1);
4850 }
4851
4852 /*
4853 * Typically opaque filesystems do authorisation in-line, but exec is a special case. In
4854 * order to be reasonably sure that exec will be permitted, we try a bit harder here.
4855 */
4856 if ((action & KAUTH_VNODE_EXECUTE) && vnode_isreg(vp)) {
4857 /* try a VNOP_OPEN for readonly access */
4858 if ((error = VNOP_OPEN(vp, FREAD, ctx)) != 0) {
4859 *resultp = error;
4860 KAUTH_DEBUG("%p DENIED - EXECUTE denied because file could not be opened readonly", vp);
4861 return(1);
4862 }
4863 VNOP_CLOSE(vp, FREAD, ctx);
4864 }
4865
4866 /*
4867 * We don't have any reason to believe that the request has to be denied at this point,
4868 * so go ahead and allow it.
4869 */
4870 *resultp = 0;
4871 KAUTH_DEBUG("%p ALLOWED - bypassing access check for non-local filesystem", vp);
4872 return(1);
4873 }
4874
4875 static int
4876 vnode_authorize_callback(__unused kauth_cred_t unused_cred, __unused void *idata, kauth_action_t action,
4877 uintptr_t arg0, uintptr_t arg1, uintptr_t arg2, uintptr_t arg3)
4878 {
4879 struct _vnode_authorize_context auth_context;
4880 vauth_ctx vcp;
4881 vfs_context_t ctx;
4882 vnode_t vp, dvp;
4883 kauth_cred_t cred;
4884 kauth_ace_rights_t rights;
4885 struct vnode_attr va, dva;
4886 int result;
4887 int *errorp;
4888 int noimmutable;
4889
4890 vcp = &auth_context;
4891 ctx = vcp->ctx = (vfs_context_t)arg0;
4892 vp = vcp->vp = (vnode_t)arg1;
4893 dvp = vcp->dvp = (vnode_t)arg2;
4894 errorp = (int *)arg3;
4895 /* note that we authorize against the context, not the passed cred (the same thing anyway) */
4896 cred = ctx->vc_ucred;
4897
4898 VATTR_INIT(&va);
4899 vcp->vap = &va;
4900 VATTR_INIT(&dva);
4901 vcp->dvap = &dva;
4902
4903 vcp->flags = vcp->flags_valid = 0;
4904
4905 #if DIAGNOSTIC
4906 if ((ctx == NULL) || (vp == NULL) || (cred == NULL))
4907 panic("vnode_authorize: bad arguments (context %p vp %p cred %p)", ctx, vp, cred);
4908 #endif
4909
4910 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)",
4911 vp, vfs_context_proc(ctx)->p_comm,
4912 (action & KAUTH_VNODE_ACCESS) ? "access" : "auth",
4913 (action & KAUTH_VNODE_READ_DATA) ? vnode_isdir(vp) ? " LIST_DIRECTORY" : " READ_DATA" : "",
4914 (action & KAUTH_VNODE_WRITE_DATA) ? vnode_isdir(vp) ? " ADD_FILE" : " WRITE_DATA" : "",
4915 (action & KAUTH_VNODE_EXECUTE) ? vnode_isdir(vp) ? " SEARCH" : " EXECUTE" : "",
4916 (action & KAUTH_VNODE_DELETE) ? " DELETE" : "",
4917 (action & KAUTH_VNODE_APPEND_DATA) ? vnode_isdir(vp) ? " ADD_SUBDIRECTORY" : " APPEND_DATA" : "",
4918 (action & KAUTH_VNODE_DELETE_CHILD) ? " DELETE_CHILD" : "",
4919 (action & KAUTH_VNODE_READ_ATTRIBUTES) ? " READ_ATTRIBUTES" : "",
4920 (action & KAUTH_VNODE_WRITE_ATTRIBUTES) ? " WRITE_ATTRIBUTES" : "",
4921 (action & KAUTH_VNODE_READ_EXTATTRIBUTES) ? " READ_EXTATTRIBUTES" : "",
4922 (action & KAUTH_VNODE_WRITE_EXTATTRIBUTES) ? " WRITE_EXTATTRIBUTES" : "",
4923 (action & KAUTH_VNODE_READ_SECURITY) ? " READ_SECURITY" : "",
4924 (action & KAUTH_VNODE_WRITE_SECURITY) ? " WRITE_SECURITY" : "",
4925 (action & KAUTH_VNODE_CHANGE_OWNER) ? " CHANGE_OWNER" : "",
4926 (action & KAUTH_VNODE_NOIMMUTABLE) ? " (noimmutable)" : "",
4927 vnode_isdir(vp) ? "directory" : "file",
4928 vp->v_name ? vp->v_name : "<NULL>", action, vp, dvp);
4929
4930 /*
4931 * Extract the control bits from the action, everything else is
4932 * requested rights.
4933 */
4934 noimmutable = (action & KAUTH_VNODE_NOIMMUTABLE) ? 1 : 0;
4935 rights = action & ~(KAUTH_VNODE_ACCESS | KAUTH_VNODE_NOIMMUTABLE);
4936
4937 if (rights & KAUTH_VNODE_DELETE) {
4938 #if DIAGNOSTIC
4939 if (dvp == NULL)
4940 panic("vnode_authorize: KAUTH_VNODE_DELETE test requires a directory");
4941 #endif
4942 } else {
4943 dvp = NULL;
4944 }
4945
4946 /*
4947 * Check for read-only filesystems.
4948 */
4949 if ((rights & KAUTH_VNODE_WRITE_RIGHTS) &&
4950 (vp->v_mount->mnt_flag & MNT_RDONLY) &&
4951 ((vp->v_type == VREG) || (vp->v_type == VDIR) ||
4952 (vp->v_type == VLNK) || (vp->v_type == VCPLX) ||
4953 (rights & KAUTH_VNODE_DELETE) || (rights & KAUTH_VNODE_DELETE_CHILD))) {
4954 result = EROFS;
4955 goto out;
4956 }
4957
4958 /*
4959 * Check for noexec filesystems.
4960 */
4961 if ((rights & KAUTH_VNODE_EXECUTE) && vnode_isreg(vp) && (vp->v_mount->mnt_flag & MNT_NOEXEC)) {
4962 result = EACCES;
4963 goto out;
4964 }
4965
4966 /*
4967 * Handle cases related to filesystems with non-local enforcement.
4968 * This call can return 0, in which case we will fall through to perform a
4969 * check based on VNOP_GETATTR data. Otherwise it returns 1 and sets
4970 * an appropriate result, at which point we can return immediately.
4971 */
4972 if (vfs_authopaque(vp->v_mount) && vnode_authorize_opaque(vp, &result, action, ctx))
4973 goto out;
4974
4975 /*
4976 * Get vnode attributes and extended security information for the vnode
4977 * and directory if required.
4978 */
4979 VATTR_WANTED(&va, va_mode);
4980 VATTR_WANTED(&va, va_uid);
4981 VATTR_WANTED(&va, va_gid);
4982 VATTR_WANTED(&va, va_flags);
4983 VATTR_WANTED(&va, va_acl);
4984 if ((result = vnode_getattr(vp, &va, ctx)) != 0) {
4985 KAUTH_DEBUG("%p ERROR - failed to get vnode attributes - %d", vp, result);
4986 goto out;
4987 }
4988 if (dvp) {
4989 VATTR_WANTED(&dva, va_mode);
4990 VATTR_WANTED(&dva, va_uid);
4991 VATTR_WANTED(&dva, va_gid);
4992 VATTR_WANTED(&dva, va_flags);
4993 VATTR_WANTED(&dva, va_acl);
4994 if ((result = vnode_getattr(dvp, &dva, ctx)) != 0) {
4995 KAUTH_DEBUG("%p ERROR - failed to get directory vnode attributes - %d", vp, result);
4996 goto out;
4997 }
4998 }
4999
5000 /*
5001 * If the vnode is an extended attribute data vnode (eg. a resource fork), *_DATA becomes
5002 * *_EXTATTRIBUTES.
5003 */
5004 if (S_ISXATTR(va.va_mode)) {
5005 if (rights & KAUTH_VNODE_READ_DATA) {
5006 rights &= ~KAUTH_VNODE_READ_DATA;
5007 rights |= KAUTH_VNODE_READ_EXTATTRIBUTES;
5008 }
5009 if (rights & KAUTH_VNODE_WRITE_DATA) {
5010 rights &= ~KAUTH_VNODE_WRITE_DATA;
5011 rights |= KAUTH_VNODE_WRITE_EXTATTRIBUTES;
5012 }
5013 }
5014
5015 /*
5016 * Check for immutability.
5017 *
5018 * In the deletion case, parent directory immutability vetoes specific
5019 * file rights.
5020 */
5021 if ((result = vnode_authorize_checkimmutable(vp, &va, rights, noimmutable)) != 0)
5022 goto out;
5023 if ((rights & KAUTH_VNODE_DELETE) &&
5024 ((result = vnode_authorize_checkimmutable(dvp, &dva, KAUTH_VNODE_DELETE_CHILD, 0)) != 0))
5025 goto out;
5026
5027 /*
5028 * Clear rights that have been authorized by reaching this point, bail if nothing left to
5029 * check.
5030 */
5031 rights &= ~(KAUTH_VNODE_LINKTARGET | KAUTH_VNODE_CHECKIMMUTABLE);
5032 if (rights == 0)
5033 goto out;
5034
5035 /*
5036 * If we're not the superuser, authorize based on file properties.
5037 */
5038 if (!vfs_context_issuser(ctx)) {
5039 /* process delete rights */
5040 if ((rights & KAUTH_VNODE_DELETE) &&
5041 ((result = vnode_authorize_delete(vcp)) != 0))
5042 goto out;
5043
5044 /* process remaining rights */
5045 if ((rights & ~KAUTH_VNODE_DELETE) &&
5046 ((result = vnode_authorize_simple(vcp, rights, rights & KAUTH_VNODE_DELETE)) != 0))
5047 goto out;
5048 } else {
5049
5050 /*
5051 * Execute is only granted to root if one of the x bits is set. This check only
5052 * makes sense if the posix mode bits are actually supported.
5053 */
5054 if ((rights & KAUTH_VNODE_EXECUTE) &&
5055 (vp->v_type == VREG) &&
5056 VATTR_IS_SUPPORTED(&va, va_mode) &&
5057 !(va.va_mode & (S_IXUSR | S_IXGRP | S_IXOTH))) {
5058 result = EPERM;
5059 KAUTH_DEBUG("%p DENIED - root execute requires at least one x bit in 0x%x", vp, va.va_mode);
5060 goto out;
5061 }
5062
5063 KAUTH_DEBUG("%p ALLOWED - caller is superuser", vp);
5064 }
5065
5066 out:
5067 if (VATTR_IS_SUPPORTED(&va, va_acl) && (va.va_acl != NULL))
5068 kauth_acl_free(va.va_acl);
5069 if (VATTR_IS_SUPPORTED(&dva, va_acl) && (dva.va_acl != NULL))
5070 kauth_acl_free(dva.va_acl);
5071 if (result) {
5072 *errorp = result;
5073 KAUTH_DEBUG("%p DENIED - auth denied", vp);
5074 return(KAUTH_RESULT_DENY);
5075 }
5076
5077 /*
5078 * Note that this implies that we will allow requests for no rights, as well as
5079 * for rights that we do not recognise. There should be none of these.
5080 */
5081 KAUTH_DEBUG("%p ALLOWED - auth granted", vp);
5082 return(KAUTH_RESULT_ALLOW);
5083 }
5084
5085 /*
5086 * Check that the attribute information in vattr can be legally applied to
5087 * a new file by the context.
5088 */
5089 int
5090 vnode_authattr_new(vnode_t dvp, struct vnode_attr *vap, int noauth, vfs_context_t ctx)
5091 {
5092 int error;
5093 int is_suser, ismember, defaulted_owner, defaulted_group, defaulted_mode;
5094 kauth_cred_t cred;
5095 guid_t changer;
5096 mount_t dmp;
5097
5098 error = 0;
5099 defaulted_owner = defaulted_group = defaulted_mode = 0;
5100
5101 /*
5102 * Require that the filesystem support extended security to apply any.
5103 */
5104 if (!vfs_extendedsecurity(dvp->v_mount) &&
5105 (VATTR_IS_ACTIVE(vap, va_acl) || VATTR_IS_ACTIVE(vap, va_uuuid) || VATTR_IS_ACTIVE(vap, va_guuid))) {
5106 error = EINVAL;
5107 goto out;
5108 }
5109
5110 /*
5111 * Default some fields.
5112 */
5113 dmp = dvp->v_mount;
5114
5115 /*
5116 * If the filesystem is mounted IGNORE_OWNERSHIP and an explicit owner is set, that
5117 * owner takes ownership of all new files.
5118 */
5119 if ((dmp->mnt_flag & MNT_IGNORE_OWNERSHIP) && (dmp->mnt_fsowner != KAUTH_UID_NONE)) {
5120 VATTR_SET(vap, va_uid, dmp->mnt_fsowner);
5121 defaulted_owner = 1;
5122 } else {
5123 if (!VATTR_IS_ACTIVE(vap, va_uid)) {
5124 /* default owner is current user */
5125 VATTR_SET(vap, va_uid, kauth_cred_getuid(vfs_context_ucred(ctx)));
5126 defaulted_owner = 1;
5127 }
5128 }
5129
5130 /*
5131 * If the filesystem is mounted IGNORE_OWNERSHIP and an explicit grouo is set, that
5132 * group takes ownership of all new files.
5133 */
5134 if ((dmp->mnt_flag & MNT_IGNORE_OWNERSHIP) && (dmp->mnt_fsgroup != KAUTH_GID_NONE)) {
5135 VATTR_SET(vap, va_gid, dmp->mnt_fsgroup);
5136 defaulted_group = 1;
5137 } else {
5138 if (!VATTR_IS_ACTIVE(vap, va_gid)) {
5139 /* default group comes from parent object, fallback to current user */
5140 struct vnode_attr dva;
5141 VATTR_INIT(&dva);
5142 VATTR_WANTED(&dva, va_gid);
5143 if ((error = vnode_getattr(dvp, &dva, ctx)) != 0)
5144 goto out;
5145 if (VATTR_IS_SUPPORTED(&dva, va_gid)) {
5146 VATTR_SET(vap, va_gid, dva.va_gid);
5147 } else {
5148 VATTR_SET(vap, va_gid, kauth_cred_getgid(vfs_context_ucred(ctx)));
5149 }
5150 defaulted_group = 1;
5151 }
5152 }
5153
5154 if (!VATTR_IS_ACTIVE(vap, va_flags))
5155 VATTR_SET(vap, va_flags, 0);
5156
5157 /* default mode is everything, masked with current umask */
5158 if (!VATTR_IS_ACTIVE(vap, va_mode)) {
5159 VATTR_SET(vap, va_mode, ACCESSPERMS & ~vfs_context_proc(ctx)->p_fd->fd_cmask);
5160 KAUTH_DEBUG("ATTR - defaulting new file mode to %o from umask %o", vap->va_mode, vfs_context_proc(ctx)->p_fd->fd_cmask);
5161 defaulted_mode = 1;
5162 }
5163 /* set timestamps to now */
5164 if (!VATTR_IS_ACTIVE(vap, va_create_time)) {
5165 nanotime(&vap->va_create_time);
5166 VATTR_SET_ACTIVE(vap, va_create_time);
5167 }
5168
5169 /*
5170 * Check for attempts to set nonsensical fields.
5171 */
5172 if (vap->va_active & ~VNODE_ATTR_NEWOBJ) {
5173 error = EINVAL;
5174 KAUTH_DEBUG("ATTR - ERROR - attempt to set unsupported new-file attributes %llx",
5175 vap->va_active & ~VNODE_ATTR_NEWOBJ);
5176 goto out;
5177 }
5178
5179 /*
5180 * Quickly check for the applicability of any enforcement here.
5181 * Tests below maintain the integrity of the local security model.
5182 */
5183 if (vfs_authopaque(vnode_mount(dvp)))
5184 goto out;
5185
5186 /*
5187 * We need to know if the caller is the superuser, or if the work is
5188 * otherwise already authorised.
5189 */
5190 cred = vfs_context_ucred(ctx);
5191 if (noauth) {
5192 /* doing work for the kernel */
5193 is_suser = 1;
5194 } else {
5195 is_suser = vfs_context_issuser(ctx);
5196 }
5197
5198
5199 if (VATTR_IS_ACTIVE(vap, va_flags)) {
5200 if (is_suser) {
5201 if ((vap->va_flags & (UF_SETTABLE | SF_SETTABLE)) != vap->va_flags) {
5202 error = EPERM;
5203 KAUTH_DEBUG(" DENIED - superuser attempt to set illegal flag(s)");
5204 goto out;
5205 }
5206 } else {
5207 if ((vap->va_flags & UF_SETTABLE) != vap->va_flags) {
5208 error = EPERM;
5209 KAUTH_DEBUG(" DENIED - user attempt to set illegal flag(s)");
5210 goto out;
5211 }
5212 }
5213 }
5214
5215 /* if not superuser, validate legality of new-item attributes */
5216 if (!is_suser) {
5217 if (!defaulted_mode && VATTR_IS_ACTIVE(vap, va_mode)) {
5218 /* setgid? */
5219 if (vap->va_mode & S_ISGID) {
5220 if ((error = kauth_cred_ismember_gid(cred, vap->va_gid, &ismember)) != 0) {
5221 KAUTH_DEBUG("ATTR - ERROR: got %d checking for membership in %d", error, vap->va_gid);
5222 goto out;
5223 }
5224 if (!ismember) {
5225 KAUTH_DEBUG(" DENIED - can't set SGID bit, not a member of %d", vap->va_gid);
5226 error = EPERM;
5227 goto out;
5228 }
5229 }
5230
5231 /* setuid? */
5232 if ((vap->va_mode & S_ISUID) && (vap->va_uid != kauth_cred_getuid(cred))) {
5233 KAUTH_DEBUG("ATTR - ERROR: illegal attempt to set the setuid bit");
5234 error = EPERM;
5235 goto out;
5236 }
5237 }
5238 if (!defaulted_owner && (vap->va_uid != kauth_cred_getuid(cred))) {
5239 KAUTH_DEBUG(" DENIED - cannot create new item owned by %d", vap->va_uid);
5240 error = EPERM;
5241 goto out;
5242 }
5243 if (!defaulted_group) {
5244 if ((error = kauth_cred_ismember_gid(cred, vap->va_gid, &ismember)) != 0) {
5245 KAUTH_DEBUG(" ERROR - got %d checking for membership in %d", error, vap->va_gid);
5246 goto out;
5247 }
5248 if (!ismember) {
5249 KAUTH_DEBUG(" DENIED - cannot create new item with group %d - not a member", vap->va_gid);
5250 error = EPERM;
5251 goto out;
5252 }
5253 }
5254
5255 /* initialising owner/group UUID */
5256 if (VATTR_IS_ACTIVE(vap, va_uuuid)) {
5257 if ((error = kauth_cred_getguid(cred, &changer)) != 0) {
5258 KAUTH_DEBUG(" ERROR - got %d trying to get caller UUID", error);
5259 /* XXX ENOENT here - no GUID - should perhaps become EPERM */
5260 goto out;
5261 }
5262 if (!kauth_guid_equal(&vap->va_uuuid, &changer)) {
5263 KAUTH_DEBUG(" ERROR - cannot create item with supplied owner UUID - not us");
5264 error = EPERM;
5265 goto out;
5266 }
5267 }
5268 if (VATTR_IS_ACTIVE(vap, va_guuid)) {
5269 if ((error = kauth_cred_ismember_guid(cred, &vap->va_guuid, &ismember)) != 0) {
5270 KAUTH_DEBUG(" ERROR - got %d trying to check group membership", error);
5271 goto out;
5272 }
5273 if (!ismember) {
5274 KAUTH_DEBUG(" ERROR - cannot create item with supplied group UUID - not a member");
5275 error = EPERM;
5276 goto out;
5277 }
5278 }
5279 }
5280 out:
5281 return(error);
5282 }
5283
5284 /*
5285 * Check that the attribute information in vap can be legally written by the context.
5286 *
5287 * Call this when you're not sure about the vnode_attr; either its contents have come
5288 * from an unknown source, or when they are variable.
5289 *
5290 * Returns errno, or zero and sets *actionp to the KAUTH_VNODE_* actions that
5291 * must be authorized to be permitted to write the vattr.
5292 */
5293 int
5294 vnode_authattr(vnode_t vp, struct vnode_attr *vap, kauth_action_t *actionp, vfs_context_t ctx)
5295 {
5296 struct vnode_attr ova;
5297 kauth_action_t required_action;
5298 int error, is_suser, ismember, chowner, chgroup;
5299 guid_t changer;
5300 gid_t group;
5301 uid_t owner;
5302 mode_t newmode;
5303 kauth_cred_t cred;
5304 uint32_t fdelta;
5305
5306 VATTR_INIT(&ova);
5307 required_action = 0;
5308 error = 0;
5309
5310 /*
5311 * Quickly check for enforcement applicability.
5312 */
5313 if (vfs_authopaque(vnode_mount(vp)))
5314 goto out;
5315
5316 /*
5317 * Check for attempts to set nonsensical fields.
5318 */
5319 if (vap->va_active & VNODE_ATTR_RDONLY) {
5320 KAUTH_DEBUG("ATTR - ERROR: attempt to set readonly attribute(s)");
5321 error = EINVAL;
5322 goto out;
5323 }
5324
5325 /*
5326 * We need to know if the caller is the superuser.
5327 */
5328 cred = vfs_context_ucred(ctx);
5329 is_suser = kauth_cred_issuser(cred);
5330
5331 /*
5332 * If any of the following are changing, we need information from the old file:
5333 * va_uid
5334 * va_gid
5335 * va_mode
5336 * va_uuuid
5337 * va_guuid
5338 */
5339 if (VATTR_IS_ACTIVE(vap, va_uid) ||
5340 VATTR_IS_ACTIVE(vap, va_gid) ||
5341 VATTR_IS_ACTIVE(vap, va_mode) ||
5342 VATTR_IS_ACTIVE(vap, va_uuuid) ||
5343 VATTR_IS_ACTIVE(vap, va_guuid)) {
5344 VATTR_WANTED(&ova, va_mode);
5345 VATTR_WANTED(&ova, va_uid);
5346 VATTR_WANTED(&ova, va_gid);
5347 VATTR_WANTED(&ova, va_uuuid);
5348 VATTR_WANTED(&ova, va_guuid);
5349 KAUTH_DEBUG("ATTR - security information changing, fetching existing attributes");
5350 }
5351
5352 /*
5353 * If timestamps are being changed, we need to know who the file is owned
5354 * by.
5355 */
5356 if (VATTR_IS_ACTIVE(vap, va_create_time) ||
5357 VATTR_IS_ACTIVE(vap, va_change_time) ||
5358 VATTR_IS_ACTIVE(vap, va_modify_time) ||
5359 VATTR_IS_ACTIVE(vap, va_access_time) ||
5360 VATTR_IS_ACTIVE(vap, va_backup_time)) {
5361
5362 VATTR_WANTED(&ova, va_uid);
5363 #if 0 /* enable this when we support UUIDs as official owners */
5364 VATTR_WANTED(&ova, va_uuuid);
5365 #endif
5366 KAUTH_DEBUG("ATTR - timestamps changing, fetching uid and GUID");
5367 }
5368
5369 /*
5370 * If flags are being changed, we need the old flags.
5371 */
5372 if (VATTR_IS_ACTIVE(vap, va_flags)) {
5373 KAUTH_DEBUG("ATTR - flags changing, fetching old flags");
5374 VATTR_WANTED(&ova, va_flags);
5375 }
5376
5377 /*
5378 * If the size is being set, make sure it's not a directory.
5379 */
5380 if (VATTR_IS_ACTIVE(vap, va_data_size)) {
5381 /* size is meaningless on a directory, don't permit this */
5382 if (vnode_isdir(vp)) {
5383 KAUTH_DEBUG("ATTR - ERROR: size change requested on a directory");
5384 error = EISDIR;
5385 goto out;
5386 }
5387 }
5388
5389 /*
5390 * Get old data.
5391 */
5392 KAUTH_DEBUG("ATTR - fetching old attributes %016llx", ova.va_active);
5393 if ((error = vnode_getattr(vp, &ova, ctx)) != 0) {
5394 KAUTH_DEBUG(" ERROR - got %d trying to get attributes", error);
5395 goto out;
5396 }
5397
5398 /*
5399 * Size changes require write access to the file data.
5400 */
5401 if (VATTR_IS_ACTIVE(vap, va_data_size)) {
5402 /* if we can't get the size, or it's different, we need write access */
5403 KAUTH_DEBUG("ATTR - size change, requiring WRITE_DATA");
5404 required_action |= KAUTH_VNODE_WRITE_DATA;
5405 }
5406
5407 /*
5408 * Changing timestamps?
5409 *
5410 * Note that we are only called to authorize user-requested time changes;
5411 * side-effect time changes are not authorized. Authorisation is only
5412 * required for existing files.
5413 *
5414 * Non-owners are not permitted to change the time on an existing
5415 * file to anything other than the current time.
5416 */
5417 if (VATTR_IS_ACTIVE(vap, va_create_time) ||
5418 VATTR_IS_ACTIVE(vap, va_change_time) ||
5419 VATTR_IS_ACTIVE(vap, va_modify_time) ||
5420 VATTR_IS_ACTIVE(vap, va_access_time) ||
5421 VATTR_IS_ACTIVE(vap, va_backup_time)) {
5422 /*
5423 * The owner and root may set any timestamps they like,
5424 * provided that the file is not immutable. The owner still needs
5425 * WRITE_ATTRIBUTES (implied by ownership but still deniable).
5426 */
5427 if (is_suser || vauth_node_owner(&ova, cred)) {
5428 KAUTH_DEBUG("ATTR - root or owner changing timestamps");
5429 required_action |= KAUTH_VNODE_CHECKIMMUTABLE | KAUTH_VNODE_WRITE_ATTRIBUTES;
5430 } else {
5431 /* just setting the current time? */
5432 if (vap->va_vaflags & VA_UTIMES_NULL) {
5433 KAUTH_DEBUG("ATTR - non-root/owner changing timestamps, requiring WRITE_ATTRIBUTES");
5434 required_action |= KAUTH_VNODE_WRITE_ATTRIBUTES;
5435 } else {
5436 KAUTH_DEBUG("ATTR - ERROR: illegal timestamp modification attempted");
5437 error = EACCES;
5438 goto out;
5439 }
5440 }
5441 }
5442
5443 /*
5444 * Changing file mode?
5445 */
5446 if (VATTR_IS_ACTIVE(vap, va_mode) && VATTR_IS_SUPPORTED(&ova, va_mode) && (ova.va_mode != vap->va_mode)) {
5447 KAUTH_DEBUG("ATTR - mode change from %06o to %06o", ova.va_mode, vap->va_mode);
5448
5449 /*
5450 * Mode changes always have the same basic auth requirements.
5451 */
5452 if (is_suser) {
5453 KAUTH_DEBUG("ATTR - superuser mode change, requiring immutability check");
5454 required_action |= KAUTH_VNODE_CHECKIMMUTABLE;
5455 } else {
5456 /* need WRITE_SECURITY */
5457 KAUTH_DEBUG("ATTR - non-superuser mode change, requiring WRITE_SECURITY");
5458 required_action |= KAUTH_VNODE_WRITE_SECURITY;
5459 }
5460
5461 /*
5462 * Can't set the setgid bit if you're not in the group and not root. Have to have
5463 * existing group information in the case we're not setting it right now.
5464 */
5465 if (vap->va_mode & S_ISGID) {
5466 required_action |= KAUTH_VNODE_CHECKIMMUTABLE; /* always required */
5467 if (!is_suser) {
5468 if (VATTR_IS_ACTIVE(vap, va_gid)) {
5469 group = vap->va_gid;
5470 } else if (VATTR_IS_SUPPORTED(&ova, va_gid)) {
5471 group = ova.va_gid;
5472 } else {
5473 KAUTH_DEBUG("ATTR - ERROR: setgid but no gid available");
5474 error = EINVAL;
5475 goto out;
5476 }
5477 /*
5478 * This might be too restrictive; WRITE_SECURITY might be implied by
5479 * membership in this case, rather than being an additional requirement.
5480 */
5481 if ((error = kauth_cred_ismember_gid(cred, group, &ismember)) != 0) {
5482 KAUTH_DEBUG("ATTR - ERROR: got %d checking for membership in %d", error, vap->va_gid);
5483 goto out;
5484 }
5485 if (!ismember) {
5486 KAUTH_DEBUG(" DENIED - can't set SGID bit, not a member of %d", group);
5487 error = EPERM;
5488 goto out;
5489 }
5490 }
5491 }
5492
5493 /*
5494 * Can't set the setuid bit unless you're root or the file's owner.
5495 */
5496 if (vap->va_mode & S_ISUID) {
5497 required_action |= KAUTH_VNODE_CHECKIMMUTABLE; /* always required */
5498 if (!is_suser) {
5499 if (VATTR_IS_ACTIVE(vap, va_uid)) {
5500 owner = vap->va_uid;
5501 } else if (VATTR_IS_SUPPORTED(&ova, va_uid)) {
5502 owner = ova.va_uid;
5503 } else {
5504 KAUTH_DEBUG("ATTR - ERROR: setuid but no uid available");
5505 error = EINVAL;
5506 goto out;
5507 }
5508 if (owner != kauth_cred_getuid(cred)) {
5509 /*
5510 * We could allow this if WRITE_SECURITY is permitted, perhaps.
5511 */
5512 KAUTH_DEBUG("ATTR - ERROR: illegal attempt to set the setuid bit");
5513 error = EPERM;
5514 goto out;
5515 }
5516 }
5517 }
5518 }
5519
5520 /*
5521 * Validate/mask flags changes. This checks that only the flags in
5522 * the UF_SETTABLE mask are being set, and preserves the flags in
5523 * the SF_SETTABLE case.
5524 *
5525 * Since flags changes may be made in conjunction with other changes,
5526 * we will ask the auth code to ignore immutability in the case that
5527 * the SF_* flags are not set and we are only manipulating the file flags.
5528 *
5529 */
5530 if (VATTR_IS_ACTIVE(vap, va_flags)) {
5531 /* compute changing flags bits */
5532 if (VATTR_IS_SUPPORTED(&ova, va_flags)) {
5533 fdelta = vap->va_flags ^ ova.va_flags;
5534 } else {
5535 fdelta = vap->va_flags;
5536 }
5537
5538 if (fdelta != 0) {
5539 KAUTH_DEBUG("ATTR - flags changing, requiring WRITE_SECURITY");
5540 required_action |= KAUTH_VNODE_WRITE_SECURITY;
5541
5542 /* check that changing bits are legal */
5543 if (is_suser) {
5544 /*
5545 * The immutability check will prevent us from clearing the SF_*
5546 * flags unless the system securelevel permits it, so just check
5547 * for legal flags here.
5548 */
5549 if (fdelta & ~(UF_SETTABLE | SF_SETTABLE)) {
5550 error = EPERM;
5551 KAUTH_DEBUG(" DENIED - superuser attempt to set illegal flag(s)");
5552 goto out;
5553 }
5554 } else {
5555 if (fdelta & ~UF_SETTABLE) {
5556 error = EPERM;
5557 KAUTH_DEBUG(" DENIED - user attempt to set illegal flag(s)");
5558 goto out;
5559 }
5560 }
5561 /*
5562 * If the caller has the ability to manipulate file flags,
5563 * security is not reduced by ignoring them for this operation.
5564 *
5565 * A more complete test here would consider the 'after' states of the flags
5566 * to determine whether it would permit the operation, but this becomes
5567 * very complex.
5568 *
5569 * Ignoring immutability is conditional on securelevel; this does not bypass
5570 * the SF_* flags if securelevel > 0.
5571 */
5572 required_action |= KAUTH_VNODE_NOIMMUTABLE;
5573 }
5574 }
5575
5576 /*
5577 * Validate ownership information.
5578 */
5579 chowner = 0;
5580 chgroup = 0;
5581
5582 /*
5583 * uid changing
5584 * Note that if the filesystem didn't give us a UID, we expect that it doesn't
5585 * support them in general, and will ignore it if/when we try to set it.
5586 * We might want to clear the uid out of vap completely here.
5587 */
5588 if (VATTR_IS_ACTIVE(vap, va_uid) && VATTR_IS_SUPPORTED(&ova, va_uid) && (vap->va_uid != ova.va_uid)) {
5589 if (!is_suser && (kauth_cred_getuid(cred) != vap->va_uid)) {
5590 KAUTH_DEBUG(" DENIED - non-superuser cannot change ownershipt to a third party");
5591 error = EPERM;
5592 goto out;
5593 }
5594 chowner = 1;
5595 }
5596
5597 /*
5598 * gid changing
5599 * Note that if the filesystem didn't give us a GID, we expect that it doesn't
5600 * support them in general, and will ignore it if/when we try to set it.
5601 * We might want to clear the gid out of vap completely here.
5602 */
5603 if (VATTR_IS_ACTIVE(vap, va_gid) && VATTR_IS_SUPPORTED(&ova, va_gid) && (vap->va_gid != ova.va_gid)) {
5604 if (!is_suser) {
5605 if ((error = kauth_cred_ismember_gid(cred, vap->va_gid, &ismember)) != 0) {
5606 KAUTH_DEBUG(" ERROR - got %d checking for membership in %d", error, vap->va_gid);
5607 goto out;
5608 }
5609 if (!ismember) {
5610 KAUTH_DEBUG(" DENIED - group change from %d to %d but not a member of target group",
5611 ova.va_gid, vap->va_gid);
5612 error = EPERM;
5613 goto out;
5614 }
5615 }
5616 chgroup = 1;
5617 }
5618
5619 /*
5620 * Owner UUID being set or changed.
5621 */
5622 if (VATTR_IS_ACTIVE(vap, va_uuuid)) {
5623 /* if the owner UUID is not actually changing ... */
5624 if (VATTR_IS_SUPPORTED(&ova, va_uuuid) && kauth_guid_equal(&vap->va_uuuid, &ova.va_uuuid))
5625 goto no_uuuid_change;
5626
5627 /*
5628 * The owner UUID cannot be set by a non-superuser to anything other than
5629 * their own.
5630 */
5631 if (!is_suser) {
5632 if ((error = kauth_cred_getguid(cred, &changer)) != 0) {
5633 KAUTH_DEBUG(" ERROR - got %d trying to get caller UUID", error);
5634 /* XXX ENOENT here - no UUID - should perhaps become EPERM */
5635 goto out;
5636 }
5637 if (!kauth_guid_equal(&vap->va_uuuid, &changer)) {
5638 KAUTH_DEBUG(" ERROR - cannot set supplied owner UUID - not us");
5639 error = EPERM;
5640 goto out;
5641 }
5642 }
5643 chowner = 1;
5644 }
5645 no_uuuid_change:
5646 /*
5647 * Group UUID being set or changed.
5648 */
5649 if (VATTR_IS_ACTIVE(vap, va_guuid)) {
5650 /* if the group UUID is not actually changing ... */
5651 if (VATTR_IS_SUPPORTED(&ova, va_guuid) && kauth_guid_equal(&vap->va_guuid, &ova.va_guuid))
5652 goto no_guuid_change;
5653
5654 /*
5655 * The group UUID cannot be set by a non-superuser to anything other than
5656 * one of which they are a member.
5657 */
5658 if (!is_suser) {
5659 if ((error = kauth_cred_ismember_guid(cred, &vap->va_guuid, &ismember)) != 0) {
5660 KAUTH_DEBUG(" ERROR - got %d trying to check group membership", error);
5661 goto out;
5662 }
5663 if (!ismember) {
5664 KAUTH_DEBUG(" ERROR - cannot create item with supplied group UUID - not a member");
5665 error = EPERM;
5666 goto out;
5667 }
5668 }
5669 chgroup = 1;
5670 }
5671 no_guuid_change:
5672
5673 /*
5674 * Compute authorisation for group/ownership changes.
5675 */
5676 if (chowner || chgroup) {
5677 if (is_suser) {
5678 KAUTH_DEBUG("ATTR - superuser changing file owner/group, requiring immutability check");
5679 required_action |= KAUTH_VNODE_CHECKIMMUTABLE;
5680 } else {
5681 if (chowner) {
5682 KAUTH_DEBUG("ATTR - ownership change, requiring TAKE_OWNERSHIP");
5683 required_action |= KAUTH_VNODE_TAKE_OWNERSHIP;
5684 }
5685 if (chgroup && !chowner) {
5686 KAUTH_DEBUG("ATTR - group change, requiring WRITE_SECURITY");
5687 required_action |= KAUTH_VNODE_WRITE_SECURITY;
5688 }
5689
5690 /* clear set-uid and set-gid bits as required by Posix */
5691 if (VATTR_IS_ACTIVE(vap, va_mode)) {
5692 newmode = vap->va_mode;
5693 } else if (VATTR_IS_SUPPORTED(&ova, va_mode)) {
5694 newmode = ova.va_mode;
5695 } else {
5696 KAUTH_DEBUG("CHOWN - trying to change owner but cannot get mode from filesystem to mask setugid bits");
5697 newmode = 0;
5698 }
5699 if (newmode & (S_ISUID | S_ISGID)) {
5700 VATTR_SET(vap, va_mode, newmode & ~(S_ISUID | S_ISGID));
5701 KAUTH_DEBUG("CHOWN - masking setugid bits from mode %o to %o", newmode, vap->va_mode);
5702 }
5703 }
5704 }
5705
5706 /*
5707 * Authorise changes in the ACL.
5708 */
5709 if (VATTR_IS_ACTIVE(vap, va_acl)) {
5710
5711 /* no existing ACL */
5712 if (!VATTR_IS_ACTIVE(&ova, va_acl) || (ova.va_acl == NULL)) {
5713
5714 /* adding an ACL */
5715 if (vap->va_acl != NULL) {
5716 required_action |= KAUTH_VNODE_WRITE_SECURITY;
5717 KAUTH_DEBUG("CHMOD - adding ACL");
5718 }
5719
5720 /* removing an existing ACL */
5721 } else if (vap->va_acl == NULL) {
5722 required_action |= KAUTH_VNODE_WRITE_SECURITY;
5723 KAUTH_DEBUG("CHMOD - removing ACL");
5724
5725 /* updating an existing ACL */
5726 } else {
5727 if (vap->va_acl->acl_entrycount != ova.va_acl->acl_entrycount) {
5728 /* entry count changed, must be different */
5729 required_action |= KAUTH_VNODE_WRITE_SECURITY;
5730 KAUTH_DEBUG("CHMOD - adding/removing ACL entries");
5731 } else if (vap->va_acl->acl_entrycount > 0) {
5732 /* both ACLs have the same ACE count, said count is 1 or more, bitwise compare ACLs */
5733 if (!memcmp(&vap->va_acl->acl_ace[0], &ova.va_acl->acl_ace[0],
5734 sizeof(struct kauth_ace) * vap->va_acl->acl_entrycount)) {
5735 required_action |= KAUTH_VNODE_WRITE_SECURITY;
5736 KAUTH_DEBUG("CHMOD - changing ACL entries");
5737 }
5738 }
5739 }
5740 }
5741
5742 /*
5743 * Other attributes that require authorisation.
5744 */
5745 if (VATTR_IS_ACTIVE(vap, va_encoding))
5746 required_action |= KAUTH_VNODE_WRITE_ATTRIBUTES;
5747
5748 out:
5749 if (VATTR_IS_SUPPORTED(&ova, va_acl) && (ova.va_acl != NULL))
5750 kauth_acl_free(ova.va_acl);
5751 if (error == 0)
5752 *actionp = required_action;
5753 return(error);
5754 }
5755
5756
5757 void
5758 vfs_setlocklocal(mount_t mp)
5759 {
5760 vnode_t vp;
5761
5762 mount_lock(mp);
5763 mp->mnt_kern_flag |= MNTK_LOCK_LOCAL;
5764
5765 /*
5766 * We do not expect anyone to be using any vnodes at the
5767 * time this routine is called. So no need for vnode locking
5768 */
5769 TAILQ_FOREACH(vp, &mp->mnt_vnodelist, v_mntvnodes) {
5770 vp->v_flag |= VLOCKLOCAL;
5771 }
5772 TAILQ_FOREACH(vp, &mp->mnt_workerqueue, v_mntvnodes) {
5773 vp->v_flag |= VLOCKLOCAL;
5774 }
5775 TAILQ_FOREACH(vp, &mp->mnt_newvnodes, v_mntvnodes) {
5776 vp->v_flag |= VLOCKLOCAL;
5777 }
5778 mount_unlock(mp);
5779 }
5780
5781
5782 #ifdef JOE_DEBUG
5783
5784 record_vp(vnode_t vp, int count) {
5785 struct uthread *ut;
5786 int i;
5787
5788 if ((vp->v_flag & VSYSTEM))
5789 return;
5790
5791 ut = get_bsdthread_info(current_thread());
5792 ut->uu_iocount += count;
5793
5794 if (ut->uu_vpindex < 32) {
5795 for (i = 0; i < ut->uu_vpindex; i++) {
5796 if (ut->uu_vps[i] == vp)
5797 return;
5798 }
5799 ut->uu_vps[ut->uu_vpindex] = vp;
5800 ut->uu_vpindex++;
5801 }
5802 }
5803 #endif