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