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