]> git.saurik.com Git - apple/xnu.git/blob - bsd/vfs/vfs_cache.c
xnu-4570.31.3.tar.gz
[apple/xnu.git] / bsd / vfs / vfs_cache.c
1 /*
2 * Copyright (c) 2000-2015 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, 1995
31 * The Regents of the University of California. All rights reserved.
32 *
33 * This code is derived from software contributed to Berkeley by
34 * Poul-Henning Kamp of the FreeBSD Project.
35 *
36 * Redistribution and use in source and binary forms, with or without
37 * modification, are permitted provided that the following conditions
38 * are met:
39 * 1. Redistributions of source code must retain the above copyright
40 * notice, this list of conditions and the following disclaimer.
41 * 2. Redistributions in binary form must reproduce the above copyright
42 * notice, this list of conditions and the following disclaimer in the
43 * documentation and/or other materials provided with the distribution.
44 * 3. All advertising materials mentioning features or use of this software
45 * must display the following acknowledgement:
46 * This product includes software developed by the University of
47 * California, Berkeley and its contributors.
48 * 4. Neither the name of the University nor the names of its contributors
49 * may be used to endorse or promote products derived from this software
50 * without specific prior written permission.
51 *
52 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
53 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
54 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
55 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
56 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
57 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
58 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
59 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
60 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
61 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
62 * SUCH DAMAGE.
63 *
64 *
65 * @(#)vfs_cache.c 8.5 (Berkeley) 3/22/95
66 */
67 /*
68 * NOTICE: This file was modified by SPARTA, Inc. in 2005 to introduce
69 * support for mandatory and extensible security protections. This notice
70 * is included in support of clause 2.2 (b) of the Apple Public License,
71 * Version 2.0.
72 */
73 #include <sys/param.h>
74 #include <sys/systm.h>
75 #include <sys/time.h>
76 #include <sys/mount_internal.h>
77 #include <sys/vnode_internal.h>
78 #include <miscfs/specfs/specdev.h>
79 #include <sys/namei.h>
80 #include <sys/errno.h>
81 #include <sys/malloc.h>
82 #include <sys/kauth.h>
83 #include <sys/user.h>
84 #include <sys/paths.h>
85
86 #if CONFIG_MACF
87 #include <security/mac_framework.h>
88 #endif
89
90 /*
91 * Name caching works as follows:
92 *
93 * Names found by directory scans are retained in a cache
94 * for future reference. It is managed LRU, so frequently
95 * used names will hang around. Cache is indexed by hash value
96 * obtained from (vp, name) where vp refers to the directory
97 * containing name.
98 *
99 * If it is a "negative" entry, (i.e. for a name that is known NOT to
100 * exist) the vnode pointer will be NULL.
101 *
102 * Upon reaching the last segment of a path, if the reference
103 * is for DELETE, or NOCACHE is set (rewrite), and the
104 * name is located in the cache, it will be dropped.
105 */
106
107 /*
108 * Structures associated with name cacheing.
109 */
110
111 LIST_HEAD(nchashhead, namecache) *nchashtbl; /* Hash Table */
112 u_long nchashmask;
113 u_long nchash; /* size of hash table - 1 */
114 long numcache; /* number of cache entries allocated */
115 int desiredNodes;
116 int desiredNegNodes;
117 int ncs_negtotal;
118 int nc_disabled = 0;
119 TAILQ_HEAD(, namecache) nchead; /* chain of all name cache entries */
120 TAILQ_HEAD(, namecache) neghead; /* chain of only negative cache entries */
121
122
123 #if COLLECT_STATS
124
125 struct nchstats nchstats; /* cache effectiveness statistics */
126
127 #define NCHSTAT(v) { \
128 nchstats.v++; \
129 }
130 #define NAME_CACHE_LOCK() name_cache_lock()
131 #define NAME_CACHE_UNLOCK() name_cache_unlock()
132 #define NAME_CACHE_LOCK_SHARED() name_cache_lock()
133
134 #else
135
136 #define NCHSTAT(v)
137 #define NAME_CACHE_LOCK() name_cache_lock()
138 #define NAME_CACHE_UNLOCK() name_cache_unlock()
139 #define NAME_CACHE_LOCK_SHARED() name_cache_lock_shared()
140
141 #endif
142
143
144 /* vars for name cache list lock */
145 lck_grp_t * namecache_lck_grp;
146 lck_grp_attr_t * namecache_lck_grp_attr;
147 lck_attr_t * namecache_lck_attr;
148
149 lck_grp_t * strcache_lck_grp;
150 lck_grp_attr_t * strcache_lck_grp_attr;
151 lck_attr_t * strcache_lck_attr;
152
153 lck_rw_t * namecache_rw_lock;
154 lck_rw_t * strtable_rw_lock;
155
156 #define NUM_STRCACHE_LOCKS 1024
157
158 lck_mtx_t strcache_mtx_locks[NUM_STRCACHE_LOCKS];
159
160
161 static vnode_t cache_lookup_locked(vnode_t dvp, struct componentname *cnp);
162 static const char *add_name_internal(const char *, uint32_t, u_int, boolean_t, u_int);
163 static void init_string_table(void);
164 static void cache_delete(struct namecache *, int);
165 static void cache_enter_locked(vnode_t dvp, vnode_t vp, struct componentname *cnp, const char *strname);
166
167 #ifdef DUMP_STRING_TABLE
168 /*
169 * Internal dump function used for debugging
170 */
171 void dump_string_table(void);
172 #endif /* DUMP_STRING_TABLE */
173
174 static void init_crc32(void);
175 static unsigned int crc32tab[256];
176
177
178 #define NCHHASH(dvp, hash_val) \
179 (&nchashtbl[(dvp->v_id ^ (hash_val)) & nchashmask])
180
181 /*
182 * This function tries to check if a directory vp is a subdirectory of dvp
183 * only from valid v_parent pointers. It is called with the name cache lock
184 * held and does not drop the lock anytime inside the function.
185 *
186 * It returns a boolean that indicates whether or not it was able to
187 * successfully infer the parent/descendent relationship via the v_parent
188 * pointers, or if it could not infer such relationship and that the decision
189 * must be delegated to the owning filesystem.
190 *
191 * If it does not defer the decision, i.e. it was successfuly able to determine
192 * the parent/descendent relationship, *is_subdir tells the caller if vp is a
193 * subdirectory of dvp.
194 *
195 * If the decision is deferred, *next_vp is where it stopped i.e. *next_vp
196 * is the vnode whose parent is to be determined from the filesystem.
197 * *is_subdir, in this case, is not indicative of anything and should be
198 * ignored.
199 *
200 * The return value and output args should be used as follows :
201 *
202 * defer = cache_check_vnode_issubdir(vp, dvp, is_subdir, next_vp);
203 * if (!defer) {
204 * if (*is_subdir)
205 * vp is subdirectory;
206 * else
207 * vp is not a subdirectory;
208 * } else {
209 * if (*next_vp)
210 * check this vnode's parent from the filesystem
211 * else
212 * error (likely because of forced unmount).
213 * }
214 *
215 */
216 static boolean_t
217 cache_check_vnode_issubdir(vnode_t vp, vnode_t dvp, boolean_t *is_subdir,
218 vnode_t *next_vp)
219 {
220 vnode_t tvp = vp;
221 int defer = FALSE;
222
223 *is_subdir = FALSE;
224 *next_vp = NULLVP;
225 while (1) {
226 mount_t tmp;
227
228 if (tvp == dvp) {
229 *is_subdir = TRUE;
230 break;
231 } else if (tvp == rootvnode) {
232 /* *is_subdir = FALSE */
233 break;
234 }
235
236 tmp = tvp->v_mount;
237 while ((tvp->v_flag & VROOT) && tmp && tmp->mnt_vnodecovered &&
238 tvp != dvp && tvp != rootvnode) {
239 tvp = tmp->mnt_vnodecovered;
240 tmp = tvp->v_mount;
241 }
242
243 /*
244 * If dvp is not at the top of a mount "stack" then
245 * vp is not a subdirectory of dvp either.
246 */
247 if (tvp == dvp || tvp == rootvnode) {
248 /* *is_subdir = FALSE */
249 break;
250 }
251
252 if (!tmp) {
253 defer = TRUE;
254 *next_vp = NULLVP;
255 break;
256 }
257
258 if ((tvp->v_flag & VISHARDLINK) || !(tvp->v_parent)) {
259 defer = TRUE;
260 *next_vp = tvp;
261 break;
262 }
263
264 tvp = tvp->v_parent;
265 }
266
267 return (defer);
268 }
269
270 /* maximum times retry from potentially transient errors in vnode_issubdir */
271 #define MAX_ERROR_RETRY 3
272
273 /*
274 * This function checks if a given directory (vp) is a subdirectory of dvp.
275 * It walks backwards from vp and if it hits dvp in its parent chain,
276 * it is a subdirectory. If it encounters the root directory, it is not
277 * a subdirectory.
278 *
279 * This function returns an error if it is unsuccessful and 0 on success.
280 *
281 * On entry (and exit) vp has an iocount and if this function has to take
282 * any iocounts on other vnodes in the parent chain traversal, it releases them.
283 */
284 int
285 vnode_issubdir(vnode_t vp, vnode_t dvp, int *is_subdir, vfs_context_t ctx)
286 {
287 vnode_t start_vp, tvp;
288 vnode_t vp_with_iocount;
289 int error = 0;
290 char dotdotbuf[] = "..";
291 int error_retry_count = 0; /* retry count for potentially transient
292 errors */
293
294 *is_subdir = FALSE;
295 tvp = start_vp = vp;
296 /*
297 * Anytime we acquire an iocount in this function, we save the vnode
298 * in this variable and release it before exiting.
299 */
300 vp_with_iocount = NULLVP;
301
302 while (1) {
303 boolean_t defer;
304 vnode_t pvp;
305 uint32_t vid;
306 struct componentname cn;
307 boolean_t is_subdir_locked = FALSE;
308
309 if (tvp == dvp) {
310 *is_subdir = TRUE;
311 break;
312 } else if (tvp == rootvnode) {
313 /* *is_subdir = FALSE */
314 break;
315 }
316
317 NAME_CACHE_LOCK_SHARED();
318
319 defer = cache_check_vnode_issubdir(tvp, dvp, &is_subdir_locked,
320 &tvp);
321
322 if (defer && tvp)
323 vid = vnode_vid(tvp);
324
325 NAME_CACHE_UNLOCK();
326
327 if (!defer) {
328 *is_subdir = is_subdir_locked;
329 break;
330 }
331
332 if (!tvp) {
333 if (error_retry_count++ < MAX_ERROR_RETRY) {
334 tvp = vp;
335 continue;
336 }
337 error = ENOENT;
338 break;
339 }
340
341 if (tvp != start_vp) {
342 if (vp_with_iocount) {
343 vnode_put(vp_with_iocount);
344 vp_with_iocount = NULLVP;
345 }
346
347 error = vnode_getwithvid(tvp, vid);
348 if (error) {
349 if (error_retry_count++ < MAX_ERROR_RETRY) {
350 tvp = vp;
351 error = 0;
352 continue;
353 }
354 break;
355 }
356
357 vp_with_iocount = tvp;
358 }
359
360 bzero(&cn, sizeof(cn));
361 cn.cn_nameiop = LOOKUP;
362 cn.cn_flags = ISLASTCN | ISDOTDOT;
363 cn.cn_context = ctx;
364 cn.cn_pnbuf = &dotdotbuf[0];
365 cn.cn_pnlen = sizeof(dotdotbuf);
366 cn.cn_nameptr = cn.cn_pnbuf;
367 cn.cn_namelen = 2;
368
369 pvp = NULLVP;
370 if ((error = VNOP_LOOKUP(tvp, &pvp, &cn, ctx)))
371 break;
372
373 if (!(tvp->v_flag & VISHARDLINK) && tvp->v_parent != pvp) {
374 (void)vnode_update_identity(tvp, pvp, NULL, 0, 0,
375 VNODE_UPDATE_PARENT);
376 }
377
378 if (vp_with_iocount)
379 vnode_put(vp_with_iocount);
380
381 vp_with_iocount = tvp = pvp;
382 }
383
384 if (vp_with_iocount)
385 vnode_put(vp_with_iocount);
386
387 return (error);
388 }
389
390 /*
391 * This function builds the path in "buff" from the supplied vnode.
392 * The length of the buffer *INCLUDING* the trailing zero byte is
393 * returned in outlen. NOTE: the length includes the trailing zero
394 * byte and thus the length is one greater than what strlen would
395 * return. This is important and lots of code elsewhere in the kernel
396 * assumes this behavior.
397 *
398 * This function can call vnop in file system if the parent vnode
399 * does not exist or when called for hardlinks via volfs path.
400 * If BUILDPATH_NO_FS_ENTER is set in flags, it only uses values present
401 * in the name cache and does not enter the file system.
402 *
403 * If BUILDPATH_CHECK_MOVED is set in flags, we return EAGAIN when
404 * we encounter ENOENT during path reconstruction. ENOENT means that
405 * one of the parents moved while we were building the path. The
406 * caller can special handle this case by calling build_path again.
407 *
408 * If BUILDPATH_VOLUME_RELATIVE is set in flags, we return path
409 * that is relative to the nearest mount point, i.e. do not
410 * cross over mount points during building the path.
411 *
412 * passed in vp must have a valid io_count reference
413 *
414 * If parent vnode is non-NULL it also must have an io count. This
415 * allows build_path_with_parent to be safely called for operations
416 * unlink, rmdir and rename that already have io counts on the target
417 * and the directory. In this way build_path_with_parent does not have
418 * to try and obtain an additional io count on the parent. Taking an
419 * io count ont the parent can lead to dead lock if a forced unmount
420 * occures at the right moment. For a fuller explaination on how this
421 * can occur see the comment for vn_getpath_with_parent.
422 *
423 */
424 int
425 build_path_with_parent(vnode_t first_vp, vnode_t parent_vp, char *buff, int buflen, int *outlen, int flags, vfs_context_t ctx)
426 {
427 vnode_t vp, tvp;
428 vnode_t vp_with_iocount;
429 vnode_t proc_root_dir_vp;
430 char *end;
431 const char *str;
432 int len;
433 int ret = 0;
434 int fixhardlink;
435
436 if (first_vp == NULLVP)
437 return (EINVAL);
438
439 if (buflen <= 1)
440 return (ENOSPC);
441
442 /*
443 * Grab the process fd so we can evaluate fd_rdir.
444 */
445 if (vfs_context_proc(ctx)->p_fd)
446 proc_root_dir_vp = vfs_context_proc(ctx)->p_fd->fd_rdir;
447 else
448 proc_root_dir_vp = NULL;
449
450 vp_with_iocount = NULLVP;
451 again:
452 vp = first_vp;
453
454 end = &buff[buflen-1];
455 *end = '\0';
456
457 /*
458 * holding the NAME_CACHE_LOCK in shared mode is
459 * sufficient to stabilize both the vp->v_parent chain
460 * and the 'vp->v_mount->mnt_vnodecovered' chain
461 *
462 * if we need to drop this lock, we must first grab the v_id
463 * from the vnode we're currently working with... if that
464 * vnode doesn't already have an io_count reference (the vp
465 * passed in comes with one), we must grab a reference
466 * after we drop the NAME_CACHE_LOCK via vnode_getwithvid...
467 * deadlocks may result if you call vnode_get while holding
468 * the NAME_CACHE_LOCK... we lazily release the reference
469 * we pick up the next time we encounter a need to drop
470 * the NAME_CACHE_LOCK or before we return from this routine
471 */
472 NAME_CACHE_LOCK_SHARED();
473
474 /*
475 * Check if this is the root of a file system.
476 */
477 while (vp && vp->v_flag & VROOT) {
478 if (vp->v_mount == NULL) {
479 ret = EINVAL;
480 goto out_unlock;
481 }
482 if ((vp->v_mount->mnt_flag & MNT_ROOTFS) || (vp == proc_root_dir_vp)) {
483 /*
484 * It's the root of the root file system, so it's
485 * just "/".
486 */
487 *--end = '/';
488
489 goto out_unlock;
490 } else {
491 /*
492 * This the root of the volume and the caller does not
493 * want to cross mount points. Therefore just return
494 * '/' as the relative path.
495 */
496 if (flags & BUILDPATH_VOLUME_RELATIVE) {
497 *--end = '/';
498 goto out_unlock;
499 } else {
500 vp = vp->v_mount->mnt_vnodecovered;
501 }
502 }
503 }
504
505 while ((vp != NULLVP) && (vp->v_parent != vp)) {
506 int vid;
507
508 /*
509 * For hardlinks the v_name may be stale, so if its OK
510 * to enter a file system, ask the file system for the
511 * name and parent (below).
512 */
513 fixhardlink = (vp->v_flag & VISHARDLINK) &&
514 (vp->v_mount->mnt_kern_flag & MNTK_PATH_FROM_ID) &&
515 !(flags & BUILDPATH_NO_FS_ENTER);
516
517 if (!fixhardlink) {
518 str = vp->v_name;
519
520 if (str == NULL || *str == '\0') {
521 if (vp->v_parent != NULL)
522 ret = EINVAL;
523 else
524 ret = ENOENT;
525 goto out_unlock;
526 }
527 len = strlen(str);
528 /*
529 * Check that there's enough space (including space for the '/')
530 */
531 if ((end - buff) < (len + 1)) {
532 ret = ENOSPC;
533 goto out_unlock;
534 }
535 /*
536 * Copy the name backwards.
537 */
538 str += len;
539
540 for (; len > 0; len--)
541 *--end = *--str;
542 /*
543 * Add a path separator.
544 */
545 *--end = '/';
546 }
547
548 /*
549 * Walk up the parent chain.
550 */
551 if (((vp->v_parent != NULLVP) && !fixhardlink) ||
552 (flags & BUILDPATH_NO_FS_ENTER)) {
553
554 /*
555 * In this if () block we are not allowed to enter the filesystem
556 * to conclusively get the most accurate parent identifier.
557 * As a result, if 'vp' does not identify '/' and it
558 * does not have a valid v_parent, then error out
559 * and disallow further path construction
560 */
561 if ((vp->v_parent == NULLVP) && (rootvnode != vp)) {
562 /*
563 * Only '/' is allowed to have a NULL parent
564 * pointer. Upper level callers should ideally
565 * re-drive name lookup on receiving a ENOENT.
566 */
567 ret = ENOENT;
568
569 /* The code below will exit early if 'tvp = vp' == NULL */
570 }
571 vp = vp->v_parent;
572
573 /*
574 * if the vnode we have in hand isn't a directory and it
575 * has a v_parent, then we started with the resource fork
576 * so skip up to avoid getting a duplicate copy of the
577 * file name in the path.
578 */
579 if (vp && !vnode_isdir(vp) && vp->v_parent) {
580 vp = vp->v_parent;
581 }
582 } else {
583 /*
584 * No parent, go get it if supported.
585 */
586 struct vnode_attr va;
587 vnode_t dvp;
588
589 /*
590 * Make sure file system supports obtaining a path from id.
591 */
592 if (!(vp->v_mount->mnt_kern_flag & MNTK_PATH_FROM_ID)) {
593 ret = ENOENT;
594 goto out_unlock;
595 }
596 vid = vp->v_id;
597
598 NAME_CACHE_UNLOCK();
599
600 if (vp != first_vp && vp != parent_vp && vp != vp_with_iocount) {
601 if (vp_with_iocount) {
602 vnode_put(vp_with_iocount);
603 vp_with_iocount = NULLVP;
604 }
605 if (vnode_getwithvid(vp, vid))
606 goto again;
607 vp_with_iocount = vp;
608 }
609 VATTR_INIT(&va);
610 VATTR_WANTED(&va, va_parentid);
611
612 if (fixhardlink) {
613 VATTR_WANTED(&va, va_name);
614 MALLOC_ZONE(va.va_name, caddr_t, MAXPATHLEN, M_NAMEI, M_WAITOK);
615 } else {
616 va.va_name = NULL;
617 }
618 /*
619 * Ask the file system for its parent id and for its name (optional).
620 */
621 ret = vnode_getattr(vp, &va, ctx);
622
623 if (fixhardlink) {
624 if ((ret == 0) && (VATTR_IS_SUPPORTED(&va, va_name))) {
625 str = va.va_name;
626 vnode_update_identity(vp, NULL, str, strlen(str), 0, VNODE_UPDATE_NAME);
627 } else if (vp->v_name) {
628 str = vp->v_name;
629 ret = 0;
630 } else {
631 ret = ENOENT;
632 goto bad_news;
633 }
634 len = strlen(str);
635
636 /*
637 * Check that there's enough space.
638 */
639 if ((end - buff) < (len + 1)) {
640 ret = ENOSPC;
641 } else {
642 /* Copy the name backwards. */
643 str += len;
644
645 for (; len > 0; len--) {
646 *--end = *--str;
647 }
648 /*
649 * Add a path separator.
650 */
651 *--end = '/';
652 }
653 bad_news:
654 FREE_ZONE(va.va_name, MAXPATHLEN, M_NAMEI);
655 }
656 if (ret || !VATTR_IS_SUPPORTED(&va, va_parentid)) {
657 ret = ENOENT;
658 goto out;
659 }
660 /*
661 * Ask the file system for the parent vnode.
662 */
663 if ((ret = VFS_VGET(vp->v_mount, (ino64_t)va.va_parentid, &dvp, ctx)))
664 goto out;
665
666 if (!fixhardlink && (vp->v_parent != dvp))
667 vnode_update_identity(vp, dvp, NULL, 0, 0, VNODE_UPDATE_PARENT);
668
669 if (vp_with_iocount)
670 vnode_put(vp_with_iocount);
671 vp = dvp;
672 vp_with_iocount = vp;
673
674 NAME_CACHE_LOCK_SHARED();
675
676 /*
677 * if the vnode we have in hand isn't a directory and it
678 * has a v_parent, then we started with the resource fork
679 * so skip up to avoid getting a duplicate copy of the
680 * file name in the path.
681 */
682 if (vp && !vnode_isdir(vp) && vp->v_parent)
683 vp = vp->v_parent;
684 }
685
686 if (vp && (flags & BUILDPATH_CHECKACCESS)) {
687 vid = vp->v_id;
688
689 NAME_CACHE_UNLOCK();
690
691 if (vp != first_vp && vp != parent_vp && vp != vp_with_iocount) {
692 if (vp_with_iocount) {
693 vnode_put(vp_with_iocount);
694 vp_with_iocount = NULLVP;
695 }
696 if (vnode_getwithvid(vp, vid))
697 goto again;
698 vp_with_iocount = vp;
699 }
700 if ((ret = vnode_authorize(vp, NULL, KAUTH_VNODE_SEARCH, ctx)))
701 goto out; /* no peeking */
702
703 NAME_CACHE_LOCK_SHARED();
704 }
705
706 /*
707 * When a mount point is crossed switch the vp.
708 * Continue until we find the root or we find
709 * a vnode that's not the root of a mounted
710 * file system.
711 */
712 tvp = vp;
713
714 while (tvp) {
715 if (tvp == proc_root_dir_vp)
716 goto out_unlock; /* encountered the root */
717
718 if (!(tvp->v_flag & VROOT) || !tvp->v_mount)
719 break; /* not the root of a mounted FS */
720
721 if (flags & BUILDPATH_VOLUME_RELATIVE) {
722 /* Do not cross over mount points */
723 tvp = NULL;
724 } else {
725 tvp = tvp->v_mount->mnt_vnodecovered;
726 }
727 }
728 if (tvp == NULLVP)
729 goto out_unlock;
730 vp = tvp;
731 }
732 out_unlock:
733 NAME_CACHE_UNLOCK();
734 out:
735 if (vp_with_iocount)
736 vnode_put(vp_with_iocount);
737 /*
738 * Slide the name down to the beginning of the buffer.
739 */
740 memmove(buff, end, &buff[buflen] - end);
741
742 /*
743 * length includes the trailing zero byte
744 */
745 *outlen = &buff[buflen] - end;
746
747 /* One of the parents was moved during path reconstruction.
748 * The caller is interested in knowing whether any of the
749 * parents moved via BUILDPATH_CHECK_MOVED, so return EAGAIN.
750 */
751 if ((ret == ENOENT) && (flags & BUILDPATH_CHECK_MOVED)) {
752 ret = EAGAIN;
753 }
754
755 return (ret);
756 }
757
758 int
759 build_path(vnode_t first_vp, char *buff, int buflen, int *outlen, int flags, vfs_context_t ctx)
760 {
761 return (build_path_with_parent(first_vp, NULL, buff, buflen, outlen, flags, ctx));
762 }
763
764 /*
765 * return NULLVP if vp's parent doesn't
766 * exist, or we can't get a valid iocount
767 * else return the parent of vp
768 */
769 vnode_t
770 vnode_getparent(vnode_t vp)
771 {
772 vnode_t pvp = NULLVP;
773 int pvid;
774
775 NAME_CACHE_LOCK_SHARED();
776 /*
777 * v_parent is stable behind the name_cache lock
778 * however, the only thing we can really guarantee
779 * is that we've grabbed a valid iocount on the
780 * parent of 'vp' at the time we took the name_cache lock...
781 * once we drop the lock, vp could get re-parented
782 */
783 if ( (pvp = vp->v_parent) != NULLVP ) {
784 pvid = pvp->v_id;
785
786 NAME_CACHE_UNLOCK();
787
788 if (vnode_getwithvid(pvp, pvid) != 0)
789 pvp = NULL;
790 } else
791 NAME_CACHE_UNLOCK();
792 return (pvp);
793 }
794
795 const char *
796 vnode_getname(vnode_t vp)
797 {
798 const char *name = NULL;
799
800 NAME_CACHE_LOCK_SHARED();
801
802 if (vp->v_name)
803 name = vfs_addname(vp->v_name, strlen(vp->v_name), 0, 0);
804 NAME_CACHE_UNLOCK();
805
806 return (name);
807 }
808
809 void
810 vnode_putname(const char *name)
811 {
812 vfs_removename(name);
813 }
814
815 static const char unknown_vnodename[] = "(unknown vnode name)";
816
817 const char *
818 vnode_getname_printable(vnode_t vp)
819 {
820 const char *name = vnode_getname(vp);
821 if (name != NULL)
822 return name;
823
824 switch (vp->v_type) {
825 case VCHR:
826 case VBLK:
827 {
828 /*
829 * Create an artificial dev name from
830 * major and minor device number
831 */
832 char dev_name[64];
833 (void) snprintf(dev_name, sizeof(dev_name),
834 "%c(%u, %u)", VCHR == vp->v_type ? 'c':'b',
835 major(vp->v_rdev), minor(vp->v_rdev));
836 /*
837 * Add the newly created dev name to the name
838 * cache to allow easier cleanup. Also,
839 * vfs_addname allocates memory for the new name
840 * and returns it.
841 */
842 NAME_CACHE_LOCK_SHARED();
843 name = vfs_addname(dev_name, strlen(dev_name), 0, 0);
844 NAME_CACHE_UNLOCK();
845 return name;
846 }
847 default:
848 return unknown_vnodename;
849 }
850 }
851
852 void
853 vnode_putname_printable(const char *name)
854 {
855 if (name == unknown_vnodename)
856 return;
857 vnode_putname(name);
858 }
859
860
861 /*
862 * if VNODE_UPDATE_PARENT, and we can take
863 * a reference on dvp, then update vp with
864 * it's new parent... if vp already has a parent,
865 * then drop the reference vp held on it
866 *
867 * if VNODE_UPDATE_NAME,
868 * then drop string ref on v_name if it exists, and if name is non-NULL
869 * then pick up a string reference on name and record it in v_name...
870 * optionally pass in the length and hashval of name if known
871 *
872 * if VNODE_UPDATE_CACHE, flush the name cache entries associated with vp
873 */
874 void
875 vnode_update_identity(vnode_t vp, vnode_t dvp, const char *name, int name_len, uint32_t name_hashval, int flags)
876 {
877 struct namecache *ncp;
878 vnode_t old_parentvp = NULLVP;
879 #if NAMEDSTREAMS
880 int isstream = (vp->v_flag & VISNAMEDSTREAM);
881 int kusecountbumped = 0;
882 #endif
883 kauth_cred_t tcred = NULL;
884 const char *vname = NULL;
885 const char *tname = NULL;
886
887 if (flags & VNODE_UPDATE_PARENT) {
888 if (dvp && vnode_ref(dvp) != 0) {
889 dvp = NULLVP;
890 }
891 #if NAMEDSTREAMS
892 /* Don't count a stream's parent ref during unmounts */
893 if (isstream && dvp && (dvp != vp) && (dvp != vp->v_parent) && (dvp->v_type == VREG)) {
894 vnode_lock_spin(dvp);
895 ++dvp->v_kusecount;
896 kusecountbumped = 1;
897 vnode_unlock(dvp);
898 }
899 #endif
900 } else {
901 dvp = NULLVP;
902 }
903 if ( (flags & VNODE_UPDATE_NAME) ) {
904 if (name != vp->v_name) {
905 if (name && *name) {
906 if (name_len == 0)
907 name_len = strlen(name);
908 tname = vfs_addname(name, name_len, name_hashval, 0);
909 }
910 } else
911 flags &= ~VNODE_UPDATE_NAME;
912 }
913 if ( (flags & (VNODE_UPDATE_PURGE | VNODE_UPDATE_PARENT | VNODE_UPDATE_CACHE | VNODE_UPDATE_NAME)) ) {
914
915 NAME_CACHE_LOCK();
916
917 if ( (flags & VNODE_UPDATE_PURGE) ) {
918
919 if (vp->v_parent)
920 vp->v_parent->v_nc_generation++;
921
922 while ( (ncp = LIST_FIRST(&vp->v_nclinks)) )
923 cache_delete(ncp, 1);
924
925 while ( (ncp = TAILQ_FIRST(&vp->v_ncchildren)) )
926 cache_delete(ncp, 1);
927
928 /*
929 * Use a temp variable to avoid kauth_cred_unref() while NAME_CACHE_LOCK is held
930 */
931 tcred = vp->v_cred;
932 vp->v_cred = NOCRED;
933 vp->v_authorized_actions = 0;
934 vp->v_cred_timestamp = 0;
935 }
936 if ( (flags & VNODE_UPDATE_NAME) ) {
937 vname = vp->v_name;
938 vp->v_name = tname;
939 }
940 if (flags & VNODE_UPDATE_PARENT) {
941 if (dvp != vp && dvp != vp->v_parent) {
942 old_parentvp = vp->v_parent;
943 vp->v_parent = dvp;
944 dvp = NULLVP;
945
946 if (old_parentvp)
947 flags |= VNODE_UPDATE_CACHE;
948 }
949 }
950 if (flags & VNODE_UPDATE_CACHE) {
951 while ( (ncp = LIST_FIRST(&vp->v_nclinks)) )
952 cache_delete(ncp, 1);
953 }
954 NAME_CACHE_UNLOCK();
955
956 if (vname != NULL)
957 vfs_removename(vname);
958
959 if (IS_VALID_CRED(tcred))
960 kauth_cred_unref(&tcred);
961 }
962 if (dvp != NULLVP) {
963 #if NAMEDSTREAMS
964 /* Back-out the ref we took if we lost a race for vp->v_parent. */
965 if (kusecountbumped) {
966 vnode_lock_spin(dvp);
967 if (dvp->v_kusecount > 0)
968 --dvp->v_kusecount;
969 vnode_unlock(dvp);
970 }
971 #endif
972 vnode_rele(dvp);
973 }
974 if (old_parentvp) {
975 struct uthread *ut;
976
977 #if NAMEDSTREAMS
978 if (isstream) {
979 vnode_lock_spin(old_parentvp);
980 if ((old_parentvp->v_type != VDIR) && (old_parentvp->v_kusecount > 0))
981 --old_parentvp->v_kusecount;
982 vnode_unlock(old_parentvp);
983 }
984 #endif
985 ut = get_bsdthread_info(current_thread());
986
987 /*
988 * indicated to vnode_rele that it shouldn't do a
989 * vnode_reclaim at this time... instead it will
990 * chain the vnode to the uu_vreclaims list...
991 * we'll be responsible for calling vnode_reclaim
992 * on each of the vnodes in this list...
993 */
994 ut->uu_defer_reclaims = 1;
995 ut->uu_vreclaims = NULLVP;
996
997 while ( (vp = old_parentvp) != NULLVP ) {
998
999 vnode_lock_spin(vp);
1000 vnode_rele_internal(vp, 0, 0, 1);
1001
1002 /*
1003 * check to see if the vnode is now in the state
1004 * that would have triggered a vnode_reclaim in vnode_rele
1005 * if it is, we save it's parent pointer and then NULL
1006 * out the v_parent field... we'll drop the reference
1007 * that was held on the next iteration of this loop...
1008 * this short circuits a potential deep recursion if we
1009 * have a long chain of parents in this state...
1010 * we'll sit in this loop until we run into
1011 * a parent in this chain that is not in this state
1012 *
1013 * make our check and the vnode_rele atomic
1014 * with respect to the current vnode we're working on
1015 * by holding the vnode lock
1016 * if vnode_rele deferred the vnode_reclaim and has put
1017 * this vnode on the list to be reaped by us, than
1018 * it has left this vnode with an iocount == 1
1019 */
1020 if ( (vp->v_iocount == 1) && (vp->v_usecount == 0) &&
1021 ((vp->v_lflag & (VL_MARKTERM | VL_TERMINATE | VL_DEAD)) == VL_MARKTERM)) {
1022 /*
1023 * vnode_rele wanted to do a vnode_reclaim on this vnode
1024 * it should be sitting on the head of the uu_vreclaims chain
1025 * pull the parent pointer now so that when we do the
1026 * vnode_reclaim for each of the vnodes in the uu_vreclaims
1027 * list, we won't recurse back through here
1028 *
1029 * need to do a convert here in case vnode_rele_internal
1030 * returns with the lock held in the spin mode... it
1031 * can drop and retake the lock under certain circumstances
1032 */
1033 vnode_lock_convert(vp);
1034
1035 NAME_CACHE_LOCK();
1036 old_parentvp = vp->v_parent;
1037 vp->v_parent = NULLVP;
1038 NAME_CACHE_UNLOCK();
1039 } else {
1040 /*
1041 * we're done... we ran into a vnode that isn't
1042 * being terminated
1043 */
1044 old_parentvp = NULLVP;
1045 }
1046 vnode_unlock(vp);
1047 }
1048 ut->uu_defer_reclaims = 0;
1049
1050 while ( (vp = ut->uu_vreclaims) != NULLVP) {
1051 ut->uu_vreclaims = vp->v_defer_reclaimlist;
1052
1053 /*
1054 * vnode_put will drive the vnode_reclaim if
1055 * we are still the only reference on this vnode
1056 */
1057 vnode_put(vp);
1058 }
1059 }
1060 }
1061
1062
1063 /*
1064 * Mark a vnode as having multiple hard links. HFS makes use of this
1065 * because it keeps track of each link separately, and wants to know
1066 * which link was actually used.
1067 *
1068 * This will cause the name cache to force a VNOP_LOOKUP on the vnode
1069 * so that HFS can post-process the lookup. Also, volfs will call
1070 * VNOP_GETATTR2 to determine the parent, instead of using v_parent.
1071 */
1072 void vnode_setmultipath(vnode_t vp)
1073 {
1074 vnode_lock_spin(vp);
1075
1076 /*
1077 * In theory, we're changing the vnode's identity as far as the
1078 * name cache is concerned, so we ought to grab the name cache lock
1079 * here. However, there is already a race, and grabbing the name
1080 * cache lock only makes the race window slightly smaller.
1081 *
1082 * The race happens because the vnode already exists in the name
1083 * cache, and could be found by one thread before another thread
1084 * can set the hard link flag.
1085 */
1086
1087 vp->v_flag |= VISHARDLINK;
1088
1089 vnode_unlock(vp);
1090 }
1091
1092
1093
1094 /*
1095 * backwards compatibility
1096 */
1097 void vnode_uncache_credentials(vnode_t vp)
1098 {
1099 vnode_uncache_authorized_action(vp, KAUTH_INVALIDATE_CACHED_RIGHTS);
1100 }
1101
1102
1103 /*
1104 * use the exclusive form of NAME_CACHE_LOCK to protect the update of the
1105 * following fields in the vnode: v_cred_timestamp, v_cred, v_authorized_actions
1106 * we use this lock so that we can look at the v_cred and v_authorized_actions
1107 * atomically while behind the NAME_CACHE_LOCK in shared mode in 'cache_lookup_path',
1108 * which is the super-hot path... if we are updating the authorized actions for this
1109 * vnode, we are already in the super-slow and far less frequented path so its not
1110 * that bad that we take the lock exclusive for this case... of course we strive
1111 * to hold it for the minimum amount of time possible
1112 */
1113
1114 void vnode_uncache_authorized_action(vnode_t vp, kauth_action_t action)
1115 {
1116 kauth_cred_t tcred = NOCRED;
1117
1118 NAME_CACHE_LOCK();
1119
1120 vp->v_authorized_actions &= ~action;
1121
1122 if (action == KAUTH_INVALIDATE_CACHED_RIGHTS &&
1123 IS_VALID_CRED(vp->v_cred)) {
1124 /*
1125 * Use a temp variable to avoid kauth_cred_unref() while NAME_CACHE_LOCK is held
1126 */
1127 tcred = vp->v_cred;
1128 vp->v_cred = NOCRED;
1129 }
1130 NAME_CACHE_UNLOCK();
1131
1132 if (tcred != NOCRED)
1133 kauth_cred_unref(&tcred);
1134 }
1135
1136
1137 extern int bootarg_vnode_cache_defeat; /* default = 0, from bsd_init.c */
1138
1139 boolean_t
1140 vnode_cache_is_authorized(vnode_t vp, vfs_context_t ctx, kauth_action_t action)
1141 {
1142 kauth_cred_t ucred;
1143 boolean_t retval = FALSE;
1144
1145 /* Boot argument to defeat rights caching */
1146 if (bootarg_vnode_cache_defeat)
1147 return FALSE;
1148
1149 if ( (vp->v_mount->mnt_kern_flag & (MNTK_AUTH_OPAQUE | MNTK_AUTH_CACHE_TTL)) ) {
1150 /*
1151 * a TTL is enabled on the rights cache... handle it here
1152 * a TTL of 0 indicates that no rights should be cached
1153 */
1154 if (vp->v_mount->mnt_authcache_ttl) {
1155 if ( !(vp->v_mount->mnt_kern_flag & MNTK_AUTH_CACHE_TTL) ) {
1156 /*
1157 * For filesystems marked only MNTK_AUTH_OPAQUE (generally network ones),
1158 * we will only allow a SEARCH right on a directory to be cached...
1159 * that cached right always has a default TTL associated with it
1160 */
1161 if (action != KAUTH_VNODE_SEARCH || vp->v_type != VDIR)
1162 vp = NULLVP;
1163 }
1164 if (vp != NULLVP && vnode_cache_is_stale(vp) == TRUE) {
1165 vnode_uncache_authorized_action(vp, vp->v_authorized_actions);
1166 vp = NULLVP;
1167 }
1168 } else
1169 vp = NULLVP;
1170 }
1171 if (vp != NULLVP) {
1172 ucred = vfs_context_ucred(ctx);
1173
1174 NAME_CACHE_LOCK_SHARED();
1175
1176 if (vp->v_cred == ucred && (vp->v_authorized_actions & action) == action)
1177 retval = TRUE;
1178
1179 NAME_CACHE_UNLOCK();
1180 }
1181 return retval;
1182 }
1183
1184
1185 void vnode_cache_authorized_action(vnode_t vp, vfs_context_t ctx, kauth_action_t action)
1186 {
1187 kauth_cred_t tcred = NOCRED;
1188 kauth_cred_t ucred;
1189 struct timeval tv;
1190 boolean_t ttl_active = FALSE;
1191
1192 ucred = vfs_context_ucred(ctx);
1193
1194 if (!IS_VALID_CRED(ucred) || action == 0)
1195 return;
1196
1197 if ( (vp->v_mount->mnt_kern_flag & (MNTK_AUTH_OPAQUE | MNTK_AUTH_CACHE_TTL)) ) {
1198 /*
1199 * a TTL is enabled on the rights cache... handle it here
1200 * a TTL of 0 indicates that no rights should be cached
1201 */
1202 if (vp->v_mount->mnt_authcache_ttl == 0)
1203 return;
1204
1205 if ( !(vp->v_mount->mnt_kern_flag & MNTK_AUTH_CACHE_TTL) ) {
1206 /*
1207 * only cache SEARCH action for filesystems marked
1208 * MNTK_AUTH_OPAQUE on VDIRs...
1209 * the lookup_path code will time these out
1210 */
1211 if ( (action & ~KAUTH_VNODE_SEARCH) || vp->v_type != VDIR )
1212 return;
1213 }
1214 ttl_active = TRUE;
1215
1216 microuptime(&tv);
1217 }
1218 NAME_CACHE_LOCK();
1219
1220 if (vp->v_cred != ucred) {
1221 kauth_cred_ref(ucred);
1222 /*
1223 * Use a temp variable to avoid kauth_cred_unref() while NAME_CACHE_LOCK is held
1224 */
1225 tcred = vp->v_cred;
1226 vp->v_cred = ucred;
1227 vp->v_authorized_actions = 0;
1228 }
1229 if (ttl_active == TRUE && vp->v_authorized_actions == 0) {
1230 /*
1231 * only reset the timestamnp on the
1232 * first authorization cached after the previous
1233 * timer has expired or we're switching creds...
1234 * 'vnode_cache_is_authorized' will clear the
1235 * authorized actions if the TTL is active and
1236 * it has expired
1237 */
1238 vp->v_cred_timestamp = tv.tv_sec;
1239 }
1240 vp->v_authorized_actions |= action;
1241
1242 NAME_CACHE_UNLOCK();
1243
1244 if (IS_VALID_CRED(tcred))
1245 kauth_cred_unref(&tcred);
1246 }
1247
1248
1249 boolean_t vnode_cache_is_stale(vnode_t vp)
1250 {
1251 struct timeval tv;
1252 boolean_t retval;
1253
1254 microuptime(&tv);
1255
1256 if ((tv.tv_sec - vp->v_cred_timestamp) > vp->v_mount->mnt_authcache_ttl)
1257 retval = TRUE;
1258 else
1259 retval = FALSE;
1260
1261 return retval;
1262 }
1263
1264
1265
1266 /*
1267 * Returns: 0 Success
1268 * ERECYCLE vnode was recycled from underneath us. Force lookup to be re-driven from namei.
1269 * This errno value should not be seen by anyone outside of the kernel.
1270 */
1271 int
1272 cache_lookup_path(struct nameidata *ndp, struct componentname *cnp, vnode_t dp,
1273 vfs_context_t ctx, int *dp_authorized, vnode_t last_dp)
1274 {
1275 char *cp; /* pointer into pathname argument */
1276 int vid;
1277 int vvid = 0; /* protected by vp != NULLVP */
1278 vnode_t vp = NULLVP;
1279 vnode_t tdp = NULLVP;
1280 kauth_cred_t ucred;
1281 boolean_t ttl_enabled = FALSE;
1282 struct timeval tv;
1283 mount_t mp;
1284 unsigned int hash;
1285 int error = 0;
1286 boolean_t dotdotchecked = FALSE;
1287
1288 #if CONFIG_TRIGGERS
1289 vnode_t trigger_vp;
1290 #endif /* CONFIG_TRIGGERS */
1291
1292 ucred = vfs_context_ucred(ctx);
1293 ndp->ni_flag &= ~(NAMEI_TRAILINGSLASH);
1294
1295 NAME_CACHE_LOCK_SHARED();
1296
1297 if ( dp->v_mount && (dp->v_mount->mnt_kern_flag & (MNTK_AUTH_OPAQUE | MNTK_AUTH_CACHE_TTL)) ) {
1298 ttl_enabled = TRUE;
1299 microuptime(&tv);
1300 }
1301 for (;;) {
1302 /*
1303 * Search a directory.
1304 *
1305 * The cn_hash value is for use by cache_lookup
1306 * The last component of the filename is left accessible via
1307 * cnp->cn_nameptr for callers that need the name.
1308 */
1309 hash = 0;
1310 cp = cnp->cn_nameptr;
1311
1312 while (*cp && (*cp != '/')) {
1313 hash = crc32tab[((hash >> 24) ^ (unsigned char)*cp++)] ^ hash << 8;
1314 }
1315 /*
1316 * the crc generator can legitimately generate
1317 * a 0... however, 0 for us means that we
1318 * haven't computed a hash, so use 1 instead
1319 */
1320 if (hash == 0)
1321 hash = 1;
1322 cnp->cn_hash = hash;
1323 cnp->cn_namelen = cp - cnp->cn_nameptr;
1324
1325 ndp->ni_pathlen -= cnp->cn_namelen;
1326 ndp->ni_next = cp;
1327
1328 /*
1329 * Replace multiple slashes by a single slash and trailing slashes
1330 * by a null. This must be done before VNOP_LOOKUP() because some
1331 * fs's don't know about trailing slashes. Remember if there were
1332 * trailing slashes to handle symlinks, existing non-directories
1333 * and non-existing files that won't be directories specially later.
1334 */
1335 while (*cp == '/' && (cp[1] == '/' || cp[1] == '\0')) {
1336 cp++;
1337 ndp->ni_pathlen--;
1338
1339 if (*cp == '\0') {
1340 ndp->ni_flag |= NAMEI_TRAILINGSLASH;
1341 *ndp->ni_next = '\0';
1342 }
1343 }
1344 ndp->ni_next = cp;
1345
1346 cnp->cn_flags &= ~(MAKEENTRY | ISLASTCN | ISDOTDOT);
1347
1348 if (*cp == '\0')
1349 cnp->cn_flags |= ISLASTCN;
1350
1351 if (cnp->cn_namelen == 2 && cnp->cn_nameptr[1] == '.' && cnp->cn_nameptr[0] == '.')
1352 cnp->cn_flags |= ISDOTDOT;
1353
1354 *dp_authorized = 0;
1355 #if NAMEDRSRCFORK
1356 /*
1357 * Process a request for a file's resource fork.
1358 *
1359 * Consume the _PATH_RSRCFORKSPEC suffix and tag the path.
1360 */
1361 if ((ndp->ni_pathlen == sizeof(_PATH_RSRCFORKSPEC)) &&
1362 (cp[1] == '.' && cp[2] == '.') &&
1363 bcmp(cp, _PATH_RSRCFORKSPEC, sizeof(_PATH_RSRCFORKSPEC)) == 0) {
1364 /* Skip volfs file systems that don't support native streams. */
1365 if ((dp->v_mount != NULL) &&
1366 (dp->v_mount->mnt_flag & MNT_DOVOLFS) &&
1367 (dp->v_mount->mnt_kern_flag & MNTK_NAMED_STREAMS) == 0) {
1368 goto skiprsrcfork;
1369 }
1370 cnp->cn_flags |= CN_WANTSRSRCFORK;
1371 cnp->cn_flags |= ISLASTCN;
1372 ndp->ni_next[0] = '\0';
1373 ndp->ni_pathlen = 1;
1374 }
1375 skiprsrcfork:
1376 #endif
1377
1378 #if CONFIG_MACF
1379
1380 /*
1381 * Name cache provides authorization caching (see below)
1382 * that will short circuit MAC checks in lookup().
1383 * We must perform MAC check here. On denial
1384 * dp_authorized will remain 0 and second check will
1385 * be perfomed in lookup().
1386 */
1387 if (!(cnp->cn_flags & DONOTAUTH)) {
1388 error = mac_vnode_check_lookup(ctx, dp, cnp);
1389 if (error) {
1390 NAME_CACHE_UNLOCK();
1391 goto errorout;
1392 }
1393 }
1394 #endif /* MAC */
1395 if (ttl_enabled &&
1396 (dp->v_mount->mnt_authcache_ttl == 0 ||
1397 ((tv.tv_sec - dp->v_cred_timestamp) > dp->v_mount->mnt_authcache_ttl))) {
1398 break;
1399 }
1400
1401 /*
1402 * NAME_CACHE_LOCK holds these fields stable
1403 *
1404 * We can't cache KAUTH_VNODE_SEARCHBYANYONE for root correctly
1405 * so we make an ugly check for root here. root is always
1406 * allowed and breaking out of here only to find out that is
1407 * authorized by virtue of being root is very very expensive.
1408 * However, the check for not root is valid only for filesystems
1409 * which use local authorization.
1410 *
1411 * XXX: Remove the check for root when we can reliably set
1412 * KAUTH_VNODE_SEARCHBYANYONE as root.
1413 */
1414 if ((dp->v_cred != ucred || !(dp->v_authorized_actions & KAUTH_VNODE_SEARCH)) &&
1415 !(dp->v_authorized_actions & KAUTH_VNODE_SEARCHBYANYONE) &&
1416 (ttl_enabled || !vfs_context_issuser(ctx))) {
1417 break;
1418 }
1419
1420 /*
1421 * indicate that we're allowed to traverse this directory...
1422 * even if we fail the cache lookup or decide to bail for
1423 * some other reason, this information is valid and is used
1424 * to avoid doing a vnode_authorize before the call to VNOP_LOOKUP
1425 */
1426 *dp_authorized = 1;
1427
1428 if ( (cnp->cn_flags & (ISLASTCN | ISDOTDOT)) ) {
1429 if (cnp->cn_nameiop != LOOKUP)
1430 break;
1431 if (cnp->cn_flags & LOCKPARENT)
1432 break;
1433 if (cnp->cn_flags & NOCACHE)
1434 break;
1435 if (cnp->cn_flags & ISDOTDOT) {
1436 /*
1437 * Force directory hardlinks to go to
1438 * file system for ".." requests.
1439 */
1440 if (dp && (dp->v_flag & VISHARDLINK)) {
1441 break;
1442 }
1443 /*
1444 * Quit here only if we can't use
1445 * the parent directory pointer or
1446 * don't have one. Otherwise, we'll
1447 * use it below.
1448 */
1449 if ((dp->v_flag & VROOT) ||
1450 dp == ndp->ni_rootdir ||
1451 dp->v_parent == NULLVP)
1452 break;
1453 }
1454 }
1455
1456 if ((cnp->cn_flags & CN_SKIPNAMECACHE)) {
1457 /*
1458 * Force lookup to go to the filesystem with
1459 * all cnp fields set up.
1460 */
1461 break;
1462 }
1463
1464 /*
1465 * "." and ".." aren't supposed to be cached, so check
1466 * for them before checking the cache.
1467 */
1468 if (cnp->cn_namelen == 1 && cnp->cn_nameptr[0] == '.')
1469 vp = dp;
1470 else if ( (cnp->cn_flags & ISDOTDOT) ) {
1471 /*
1472 * If this is a chrooted process, we need to check if
1473 * the process is trying to break out of its chrooted
1474 * jail. We do that by trying to determine if dp is
1475 * a subdirectory of ndp->ni_rootdir. If we aren't
1476 * able to determine that by the v_parent pointers, we
1477 * will leave the fast path.
1478 *
1479 * Since this function may see dotdot components
1480 * many times and it has the name cache lock held for
1481 * the entire duration, we optimise this by doing this
1482 * check only once per cache_lookup_path call.
1483 * If dotdotchecked is set, it means we've done this
1484 * check once already and don't need to do it again.
1485 */
1486 if (!dotdotchecked && (ndp->ni_rootdir != rootvnode)) {
1487 vnode_t tvp = dp;
1488 boolean_t defer = FALSE;
1489 boolean_t is_subdir = FALSE;
1490
1491 defer = cache_check_vnode_issubdir(tvp,
1492 ndp->ni_rootdir, &is_subdir, &tvp);
1493
1494 if (defer) {
1495 /* defer to Filesystem */
1496 break;
1497 } else if (!is_subdir) {
1498 /*
1499 * This process is trying to break out
1500 * of its chrooted jail, so all its
1501 * dotdot accesses will be translated to
1502 * its root directory.
1503 */
1504 vp = ndp->ni_rootdir;
1505 } else {
1506 /*
1507 * All good, let this dotdot access
1508 * proceed normally
1509 */
1510 vp = dp->v_parent;
1511 }
1512 dotdotchecked = TRUE;
1513 } else {
1514 vp = dp->v_parent;
1515 }
1516 } else {
1517 if ( (vp = cache_lookup_locked(dp, cnp)) == NULLVP)
1518 break;
1519
1520 if ( (vp->v_flag & VISHARDLINK) ) {
1521 /*
1522 * The file system wants a VNOP_LOOKUP on this vnode
1523 */
1524 vp = NULL;
1525 break;
1526 }
1527 }
1528 if ( (cnp->cn_flags & ISLASTCN) )
1529 break;
1530
1531 if (vp->v_type != VDIR) {
1532 if (vp->v_type != VLNK)
1533 vp = NULL;
1534 break;
1535 }
1536
1537 if ( (mp = vp->v_mountedhere) && ((cnp->cn_flags & NOCROSSMOUNT) == 0)) {
1538 vnode_t tmp_vp = mp->mnt_realrootvp;
1539 if (tmp_vp == NULLVP || mp->mnt_generation != mount_generation ||
1540 mp->mnt_realrootvp_vid != tmp_vp->v_id)
1541 break;
1542 vp = tmp_vp;
1543 }
1544
1545 #if CONFIG_TRIGGERS
1546 /*
1547 * After traversing all mountpoints stacked here, if we have a
1548 * trigger in hand, resolve it. Note that we don't need to
1549 * leave the fast path if the mount has already happened.
1550 */
1551 if (vp->v_resolve)
1552 break;
1553 #endif /* CONFIG_TRIGGERS */
1554
1555
1556 dp = vp;
1557 vp = NULLVP;
1558
1559 cnp->cn_nameptr = ndp->ni_next + 1;
1560 ndp->ni_pathlen--;
1561 while (*cnp->cn_nameptr == '/') {
1562 cnp->cn_nameptr++;
1563 ndp->ni_pathlen--;
1564 }
1565 }
1566 if (vp != NULLVP)
1567 vvid = vp->v_id;
1568 vid = dp->v_id;
1569
1570 NAME_CACHE_UNLOCK();
1571
1572 if ((vp != NULLVP) && (vp->v_type != VLNK) &&
1573 ((cnp->cn_flags & (ISLASTCN | LOCKPARENT | WANTPARENT | SAVESTART)) == ISLASTCN)) {
1574 /*
1575 * if we've got a child and it's the last component, and
1576 * the lookup doesn't need to return the parent then we
1577 * can skip grabbing an iocount on the parent, since all
1578 * we're going to do with it is a vnode_put just before
1579 * we return from 'lookup'. If it's a symbolic link,
1580 * we need the parent in case the link happens to be
1581 * a relative pathname.
1582 */
1583 tdp = dp;
1584 dp = NULLVP;
1585 } else {
1586 need_dp:
1587 /*
1588 * return the last directory we looked at
1589 * with an io reference held. If it was the one passed
1590 * in as a result of the last iteration of VNOP_LOOKUP,
1591 * it should already hold an io ref. No need to increase ref.
1592 */
1593 if (last_dp != dp){
1594
1595 if (dp == ndp->ni_usedvp) {
1596 /*
1597 * if this vnode matches the one passed in via USEDVP
1598 * than this context already holds an io_count... just
1599 * use vnode_get to get an extra ref for lookup to play
1600 * with... can't use the getwithvid variant here because
1601 * it will block behind a vnode_drain which would result
1602 * in a deadlock (since we already own an io_count that the
1603 * vnode_drain is waiting on)... vnode_get grabs the io_count
1604 * immediately w/o waiting... it always succeeds
1605 */
1606 vnode_get(dp);
1607 } else if ((error = vnode_getwithvid_drainok(dp, vid))) {
1608 /*
1609 * failure indicates the vnode
1610 * changed identity or is being
1611 * TERMINATED... in either case
1612 * punt this lookup.
1613 *
1614 * don't necessarily return ENOENT, though, because
1615 * we really want to go back to disk and make sure it's
1616 * there or not if someone else is changing this
1617 * vnode. That being said, the one case where we do want
1618 * to return ENOENT is when the vnode's mount point is
1619 * in the process of unmounting and we might cause a deadlock
1620 * in our attempt to take an iocount. An ENODEV error return
1621 * is from vnode_get* is an indication this but we change that
1622 * ENOENT for upper layers.
1623 */
1624 if (error == ENODEV) {
1625 error = ENOENT;
1626 } else {
1627 error = ERECYCLE;
1628 }
1629 goto errorout;
1630 }
1631 }
1632 }
1633 if (vp != NULLVP) {
1634 if ( (vnode_getwithvid_drainok(vp, vvid)) ) {
1635 vp = NULLVP;
1636
1637 /*
1638 * can't get reference on the vp we'd like
1639 * to return... if we didn't grab a reference
1640 * on the directory (due to fast path bypass),
1641 * then we need to do it now... we can't return
1642 * with both ni_dvp and ni_vp NULL, and no
1643 * error condition
1644 */
1645 if (dp == NULLVP) {
1646 dp = tdp;
1647 goto need_dp;
1648 }
1649 }
1650 }
1651
1652 ndp->ni_dvp = dp;
1653 ndp->ni_vp = vp;
1654
1655 #if CONFIG_TRIGGERS
1656 trigger_vp = vp ? vp : dp;
1657 if ((error == 0) && (trigger_vp != NULLVP) && vnode_isdir(trigger_vp)) {
1658 error = vnode_trigger_resolve(trigger_vp, ndp, ctx);
1659 if (error) {
1660 if (vp)
1661 vnode_put(vp);
1662 if (dp)
1663 vnode_put(dp);
1664 goto errorout;
1665 }
1666 }
1667 #endif /* CONFIG_TRIGGERS */
1668
1669 errorout:
1670 /*
1671 * If we came into cache_lookup_path after an iteration of the lookup loop that
1672 * resulted in a call to VNOP_LOOKUP, then VNOP_LOOKUP returned a vnode with a io ref
1673 * on it. It is now the job of cache_lookup_path to drop the ref on this vnode
1674 * when it is no longer needed. If we get to this point, and last_dp is not NULL
1675 * and it is ALSO not the dvp we want to return to caller of this function, it MUST be
1676 * the case that we got to a subsequent path component and this previous vnode is
1677 * no longer needed. We can then drop the io ref on it.
1678 */
1679 if ((last_dp != NULLVP) && (last_dp != ndp->ni_dvp)){
1680 vnode_put(last_dp);
1681 }
1682
1683 //initialized to 0, should be the same if no error cases occurred.
1684 return error;
1685 }
1686
1687
1688 static vnode_t
1689 cache_lookup_locked(vnode_t dvp, struct componentname *cnp)
1690 {
1691 struct namecache *ncp;
1692 struct nchashhead *ncpp;
1693 long namelen = cnp->cn_namelen;
1694 unsigned int hashval = cnp->cn_hash;
1695
1696 if (nc_disabled) {
1697 return NULL;
1698 }
1699
1700 ncpp = NCHHASH(dvp, cnp->cn_hash);
1701 LIST_FOREACH(ncp, ncpp, nc_hash) {
1702 if ((ncp->nc_dvp == dvp) && (ncp->nc_hashval == hashval)) {
1703 if (strncmp(ncp->nc_name, cnp->cn_nameptr, namelen) == 0 && ncp->nc_name[namelen] == 0)
1704 break;
1705 }
1706 }
1707 if (ncp == 0) {
1708 /*
1709 * We failed to find an entry
1710 */
1711 NCHSTAT(ncs_miss);
1712 return (NULL);
1713 }
1714 NCHSTAT(ncs_goodhits);
1715
1716 return (ncp->nc_vp);
1717 }
1718
1719
1720 unsigned int hash_string(const char *cp, int len);
1721 //
1722 // Have to take a len argument because we may only need to
1723 // hash part of a componentname.
1724 //
1725 unsigned int
1726 hash_string(const char *cp, int len)
1727 {
1728 unsigned hash = 0;
1729
1730 if (len) {
1731 while (len--) {
1732 hash = crc32tab[((hash >> 24) ^ (unsigned char)*cp++)] ^ hash << 8;
1733 }
1734 } else {
1735 while (*cp != '\0') {
1736 hash = crc32tab[((hash >> 24) ^ (unsigned char)*cp++)] ^ hash << 8;
1737 }
1738 }
1739 /*
1740 * the crc generator can legitimately generate
1741 * a 0... however, 0 for us means that we
1742 * haven't computed a hash, so use 1 instead
1743 */
1744 if (hash == 0)
1745 hash = 1;
1746 return hash;
1747 }
1748
1749
1750 /*
1751 * Lookup an entry in the cache
1752 *
1753 * We don't do this if the segment name is long, simply so the cache
1754 * can avoid holding long names (which would either waste space, or
1755 * add greatly to the complexity).
1756 *
1757 * Lookup is called with dvp pointing to the directory to search,
1758 * cnp pointing to the name of the entry being sought. If the lookup
1759 * succeeds, the vnode is returned in *vpp, and a status of -1 is
1760 * returned. If the lookup determines that the name does not exist
1761 * (negative cacheing), a status of ENOENT is returned. If the lookup
1762 * fails, a status of zero is returned.
1763 */
1764
1765 int
1766 cache_lookup(struct vnode *dvp, struct vnode **vpp, struct componentname *cnp)
1767 {
1768 struct namecache *ncp;
1769 struct nchashhead *ncpp;
1770 long namelen = cnp->cn_namelen;
1771 unsigned int hashval;
1772 boolean_t have_exclusive = FALSE;
1773 uint32_t vid;
1774 vnode_t vp;
1775
1776 if (cnp->cn_hash == 0)
1777 cnp->cn_hash = hash_string(cnp->cn_nameptr, cnp->cn_namelen);
1778 hashval = cnp->cn_hash;
1779
1780 if (nc_disabled) {
1781 return 0;
1782 }
1783
1784 NAME_CACHE_LOCK_SHARED();
1785
1786 relook:
1787 ncpp = NCHHASH(dvp, cnp->cn_hash);
1788 LIST_FOREACH(ncp, ncpp, nc_hash) {
1789 if ((ncp->nc_dvp == dvp) && (ncp->nc_hashval == hashval)) {
1790 if (strncmp(ncp->nc_name, cnp->cn_nameptr, namelen) == 0 && ncp->nc_name[namelen] == 0)
1791 break;
1792 }
1793 }
1794 /* We failed to find an entry */
1795 if (ncp == 0) {
1796 NCHSTAT(ncs_miss);
1797 NAME_CACHE_UNLOCK();
1798 return (0);
1799 }
1800
1801 /* We don't want to have an entry, so dump it */
1802 if ((cnp->cn_flags & MAKEENTRY) == 0) {
1803 if (have_exclusive == TRUE) {
1804 NCHSTAT(ncs_badhits);
1805 cache_delete(ncp, 1);
1806 NAME_CACHE_UNLOCK();
1807 return (0);
1808 }
1809 NAME_CACHE_UNLOCK();
1810 NAME_CACHE_LOCK();
1811 have_exclusive = TRUE;
1812 goto relook;
1813 }
1814 vp = ncp->nc_vp;
1815
1816 /* We found a "positive" match, return the vnode */
1817 if (vp) {
1818 NCHSTAT(ncs_goodhits);
1819
1820 vid = vp->v_id;
1821 NAME_CACHE_UNLOCK();
1822
1823 if (vnode_getwithvid(vp, vid)) {
1824 #if COLLECT_STATS
1825 NAME_CACHE_LOCK();
1826 NCHSTAT(ncs_badvid);
1827 NAME_CACHE_UNLOCK();
1828 #endif
1829 return (0);
1830 }
1831 *vpp = vp;
1832 return (-1);
1833 }
1834
1835 /* We found a negative match, and want to create it, so purge */
1836 if (cnp->cn_nameiop == CREATE || cnp->cn_nameiop == RENAME) {
1837 if (have_exclusive == TRUE) {
1838 NCHSTAT(ncs_badhits);
1839 cache_delete(ncp, 1);
1840 NAME_CACHE_UNLOCK();
1841 return (0);
1842 }
1843 NAME_CACHE_UNLOCK();
1844 NAME_CACHE_LOCK();
1845 have_exclusive = TRUE;
1846 goto relook;
1847 }
1848
1849 /*
1850 * We found a "negative" match, ENOENT notifies client of this match.
1851 */
1852 NCHSTAT(ncs_neghits);
1853
1854 NAME_CACHE_UNLOCK();
1855 return (ENOENT);
1856 }
1857
1858 const char *
1859 cache_enter_create(vnode_t dvp, vnode_t vp, struct componentname *cnp)
1860 {
1861 const char *strname;
1862
1863 if (cnp->cn_hash == 0)
1864 cnp->cn_hash = hash_string(cnp->cn_nameptr, cnp->cn_namelen);
1865
1866 /*
1867 * grab 2 references on the string entered
1868 * one for the cache_enter_locked to consume
1869 * and the second to be consumed by v_name (vnode_create call point)
1870 */
1871 strname = add_name_internal(cnp->cn_nameptr, cnp->cn_namelen, cnp->cn_hash, TRUE, 0);
1872
1873 NAME_CACHE_LOCK();
1874
1875 cache_enter_locked(dvp, vp, cnp, strname);
1876
1877 NAME_CACHE_UNLOCK();
1878
1879 return (strname);
1880 }
1881
1882
1883 /*
1884 * Add an entry to the cache...
1885 * but first check to see if the directory
1886 * that this entry is to be associated with has
1887 * had any cache_purges applied since we took
1888 * our identity snapshot... this check needs to
1889 * be done behind the name cache lock
1890 */
1891 void
1892 cache_enter_with_gen(struct vnode *dvp, struct vnode *vp, struct componentname *cnp, int gen)
1893 {
1894
1895 if (cnp->cn_hash == 0)
1896 cnp->cn_hash = hash_string(cnp->cn_nameptr, cnp->cn_namelen);
1897
1898 NAME_CACHE_LOCK();
1899
1900 if (dvp->v_nc_generation == gen)
1901 (void)cache_enter_locked(dvp, vp, cnp, NULL);
1902
1903 NAME_CACHE_UNLOCK();
1904 }
1905
1906
1907 /*
1908 * Add an entry to the cache.
1909 */
1910 void
1911 cache_enter(struct vnode *dvp, struct vnode *vp, struct componentname *cnp)
1912 {
1913 const char *strname;
1914
1915 if (cnp->cn_hash == 0)
1916 cnp->cn_hash = hash_string(cnp->cn_nameptr, cnp->cn_namelen);
1917
1918 /*
1919 * grab 1 reference on the string entered
1920 * for the cache_enter_locked to consume
1921 */
1922 strname = add_name_internal(cnp->cn_nameptr, cnp->cn_namelen, cnp->cn_hash, FALSE, 0);
1923
1924 NAME_CACHE_LOCK();
1925
1926 cache_enter_locked(dvp, vp, cnp, strname);
1927
1928 NAME_CACHE_UNLOCK();
1929 }
1930
1931
1932 static void
1933 cache_enter_locked(struct vnode *dvp, struct vnode *vp, struct componentname *cnp, const char *strname)
1934 {
1935 struct namecache *ncp, *negp;
1936 struct nchashhead *ncpp;
1937
1938 if (nc_disabled)
1939 return;
1940
1941 /*
1942 * if the entry is for -ve caching vp is null
1943 */
1944 if ((vp != NULLVP) && (LIST_FIRST(&vp->v_nclinks))) {
1945 /*
1946 * someone beat us to the punch..
1947 * this vnode is already in the cache
1948 */
1949 if (strname != NULL)
1950 vfs_removename(strname);
1951 return;
1952 }
1953 /*
1954 * We allocate a new entry if we are less than the maximum
1955 * allowed and the one at the front of the list is in use.
1956 * Otherwise we use the one at the front of the list.
1957 */
1958 if (numcache < desiredNodes &&
1959 ((ncp = nchead.tqh_first) == NULL ||
1960 ncp->nc_hash.le_prev != 0)) {
1961 /*
1962 * Allocate one more entry
1963 */
1964 ncp = (struct namecache *)_MALLOC_ZONE(sizeof(*ncp), M_CACHE, M_WAITOK);
1965 numcache++;
1966 } else {
1967 /*
1968 * reuse an old entry
1969 */
1970 ncp = TAILQ_FIRST(&nchead);
1971 TAILQ_REMOVE(&nchead, ncp, nc_entry);
1972
1973 if (ncp->nc_hash.le_prev != 0) {
1974 /*
1975 * still in use... we need to
1976 * delete it before re-using it
1977 */
1978 NCHSTAT(ncs_stolen);
1979 cache_delete(ncp, 0);
1980 }
1981 }
1982 NCHSTAT(ncs_enters);
1983
1984 /*
1985 * Fill in cache info, if vp is NULL this is a "negative" cache entry.
1986 */
1987 ncp->nc_vp = vp;
1988 ncp->nc_dvp = dvp;
1989 ncp->nc_hashval = cnp->cn_hash;
1990
1991 if (strname == NULL)
1992 ncp->nc_name = add_name_internal(cnp->cn_nameptr, cnp->cn_namelen, cnp->cn_hash, FALSE, 0);
1993 else
1994 ncp->nc_name = strname;
1995
1996 //
1997 // If the bytes of the name associated with the vnode differ,
1998 // use the name associated with the vnode since the file system
1999 // may have set that explicitly in the case of a lookup on a
2000 // case-insensitive file system where the case of the looked up
2001 // name differs from what is on disk. For more details, see:
2002 // <rdar://problem/8044697> FSEvents doesn't always decompose diacritical unicode chars in the paths of the changed directories
2003 //
2004 const char *vn_name = vp ? vp->v_name : NULL;
2005 unsigned int len = vn_name ? strlen(vn_name) : 0;
2006 if (vn_name && ncp && ncp->nc_name && strncmp(ncp->nc_name, vn_name, len) != 0) {
2007 unsigned int hash = hash_string(vn_name, len);
2008
2009 vfs_removename(ncp->nc_name);
2010 ncp->nc_name = add_name_internal(vn_name, len, hash, FALSE, 0);
2011 ncp->nc_hashval = hash;
2012 }
2013
2014 /*
2015 * make us the newest entry in the cache
2016 * i.e. we'll be the last to be stolen
2017 */
2018 TAILQ_INSERT_TAIL(&nchead, ncp, nc_entry);
2019
2020 ncpp = NCHHASH(dvp, cnp->cn_hash);
2021 #if DIAGNOSTIC
2022 {
2023 struct namecache *p;
2024
2025 for (p = ncpp->lh_first; p != 0; p = p->nc_hash.le_next)
2026 if (p == ncp)
2027 panic("cache_enter: duplicate");
2028 }
2029 #endif
2030 /*
2031 * make us available to be found via lookup
2032 */
2033 LIST_INSERT_HEAD(ncpp, ncp, nc_hash);
2034
2035 if (vp) {
2036 /*
2037 * add to the list of name cache entries
2038 * that point at vp
2039 */
2040 LIST_INSERT_HEAD(&vp->v_nclinks, ncp, nc_un.nc_link);
2041 } else {
2042 /*
2043 * this is a negative cache entry (vp == NULL)
2044 * stick it on the negative cache list.
2045 */
2046 TAILQ_INSERT_TAIL(&neghead, ncp, nc_un.nc_negentry);
2047
2048 ncs_negtotal++;
2049
2050 if (ncs_negtotal > desiredNegNodes) {
2051 /*
2052 * if we've reached our desired limit
2053 * of negative cache entries, delete
2054 * the oldest
2055 */
2056 negp = TAILQ_FIRST(&neghead);
2057 cache_delete(negp, 1);
2058 }
2059 }
2060 /*
2061 * add us to the list of name cache entries that
2062 * are children of dvp
2063 */
2064 if (vp)
2065 TAILQ_INSERT_TAIL(&dvp->v_ncchildren, ncp, nc_child);
2066 else
2067 TAILQ_INSERT_HEAD(&dvp->v_ncchildren, ncp, nc_child);
2068 }
2069
2070
2071 /*
2072 * Initialize CRC-32 remainder table.
2073 */
2074 static void init_crc32(void)
2075 {
2076 /*
2077 * the CRC-32 generator polynomial is:
2078 * x^32 + x^26 + x^23 + x^22 + x^16 + x^12 + x^10
2079 * + x^8 + x^7 + x^5 + x^4 + x^2 + x + 1
2080 */
2081 unsigned int crc32_polynomial = 0x04c11db7;
2082 unsigned int i,j;
2083
2084 /*
2085 * pre-calculate the CRC-32 remainder for each possible octet encoding
2086 */
2087 for (i = 0; i < 256; i++) {
2088 unsigned int crc_rem = i << 24;
2089
2090 for (j = 0; j < 8; j++) {
2091 if (crc_rem & 0x80000000)
2092 crc_rem = (crc_rem << 1) ^ crc32_polynomial;
2093 else
2094 crc_rem = (crc_rem << 1);
2095 }
2096 crc32tab[i] = crc_rem;
2097 }
2098 }
2099
2100
2101 /*
2102 * Name cache initialization, from vfs_init() when we are booting
2103 */
2104 void
2105 nchinit(void)
2106 {
2107 int i;
2108
2109 desiredNegNodes = (desiredvnodes / 10);
2110 desiredNodes = desiredvnodes + desiredNegNodes;
2111
2112 TAILQ_INIT(&nchead);
2113 TAILQ_INIT(&neghead);
2114
2115 init_crc32();
2116
2117 nchashtbl = hashinit(MAX(CONFIG_NC_HASH, (2 *desiredNodes)), M_CACHE, &nchash);
2118 nchashmask = nchash;
2119 nchash++;
2120
2121 init_string_table();
2122
2123 /* Allocate name cache lock group attribute and group */
2124 namecache_lck_grp_attr= lck_grp_attr_alloc_init();
2125
2126 namecache_lck_grp = lck_grp_alloc_init("Name Cache", namecache_lck_grp_attr);
2127
2128 /* Allocate name cache lock attribute */
2129 namecache_lck_attr = lck_attr_alloc_init();
2130
2131 /* Allocate name cache lock */
2132 namecache_rw_lock = lck_rw_alloc_init(namecache_lck_grp, namecache_lck_attr);
2133
2134
2135 /* Allocate string cache lock group attribute and group */
2136 strcache_lck_grp_attr= lck_grp_attr_alloc_init();
2137
2138 strcache_lck_grp = lck_grp_alloc_init("String Cache", strcache_lck_grp_attr);
2139
2140 /* Allocate string cache lock attribute */
2141 strcache_lck_attr = lck_attr_alloc_init();
2142
2143 /* Allocate string cache lock */
2144 strtable_rw_lock = lck_rw_alloc_init(strcache_lck_grp, strcache_lck_attr);
2145
2146 for (i = 0; i < NUM_STRCACHE_LOCKS; i++)
2147 lck_mtx_init(&strcache_mtx_locks[i], strcache_lck_grp, strcache_lck_attr);
2148 }
2149
2150 void
2151 name_cache_lock_shared(void)
2152 {
2153 lck_rw_lock_shared(namecache_rw_lock);
2154 }
2155
2156 void
2157 name_cache_lock(void)
2158 {
2159 lck_rw_lock_exclusive(namecache_rw_lock);
2160 }
2161
2162 void
2163 name_cache_unlock(void)
2164 {
2165 lck_rw_done(namecache_rw_lock);
2166 }
2167
2168
2169 int
2170 resize_namecache(u_int newsize)
2171 {
2172 struct nchashhead *new_table;
2173 struct nchashhead *old_table;
2174 struct nchashhead *old_head, *head;
2175 struct namecache *entry, *next;
2176 uint32_t i, hashval;
2177 int dNodes, dNegNodes;
2178 u_long new_size, old_size;
2179
2180 dNegNodes = (newsize / 10);
2181 dNodes = newsize + dNegNodes;
2182
2183 // we don't support shrinking yet
2184 if (dNodes <= desiredNodes) {
2185 return 0;
2186 }
2187 new_table = hashinit(2 * dNodes, M_CACHE, &nchashmask);
2188 new_size = nchashmask + 1;
2189
2190 if (new_table == NULL) {
2191 return ENOMEM;
2192 }
2193
2194 NAME_CACHE_LOCK();
2195 // do the switch!
2196 old_table = nchashtbl;
2197 nchashtbl = new_table;
2198 old_size = nchash;
2199 nchash = new_size;
2200
2201 // walk the old table and insert all the entries into
2202 // the new table
2203 //
2204 for(i=0; i < old_size; i++) {
2205 old_head = &old_table[i];
2206 for (entry=old_head->lh_first; entry != NULL; entry=next) {
2207 //
2208 // XXXdbg - Beware: this assumes that hash_string() does
2209 // the same thing as what happens in
2210 // lookup() over in vfs_lookup.c
2211 hashval = hash_string(entry->nc_name, 0);
2212 entry->nc_hashval = hashval;
2213 head = NCHHASH(entry->nc_dvp, hashval);
2214
2215 next = entry->nc_hash.le_next;
2216 LIST_INSERT_HEAD(head, entry, nc_hash);
2217 }
2218 }
2219 desiredNodes = dNodes;
2220 desiredNegNodes = dNegNodes;
2221
2222 NAME_CACHE_UNLOCK();
2223 FREE(old_table, M_CACHE);
2224
2225 return 0;
2226 }
2227
2228 static void
2229 cache_delete(struct namecache *ncp, int free_entry)
2230 {
2231 NCHSTAT(ncs_deletes);
2232
2233 if (ncp->nc_vp) {
2234 LIST_REMOVE(ncp, nc_un.nc_link);
2235 } else {
2236 TAILQ_REMOVE(&neghead, ncp, nc_un.nc_negentry);
2237 ncs_negtotal--;
2238 }
2239 TAILQ_REMOVE(&(ncp->nc_dvp->v_ncchildren), ncp, nc_child);
2240
2241 LIST_REMOVE(ncp, nc_hash);
2242 /*
2243 * this field is used to indicate
2244 * that the entry is in use and
2245 * must be deleted before it can
2246 * be reused...
2247 */
2248 ncp->nc_hash.le_prev = NULL;
2249
2250 vfs_removename(ncp->nc_name);
2251 ncp->nc_name = NULL;
2252 if (free_entry) {
2253 TAILQ_REMOVE(&nchead, ncp, nc_entry);
2254 FREE_ZONE(ncp, sizeof(*ncp), M_CACHE);
2255 numcache--;
2256 }
2257 }
2258
2259
2260 /*
2261 * purge the entry associated with the
2262 * specified vnode from the name cache
2263 */
2264 void
2265 cache_purge(vnode_t vp)
2266 {
2267 struct namecache *ncp;
2268 kauth_cred_t tcred = NULL;
2269
2270 if ((LIST_FIRST(&vp->v_nclinks) == NULL) &&
2271 (TAILQ_FIRST(&vp->v_ncchildren) == NULL) &&
2272 (vp->v_cred == NOCRED) &&
2273 (vp->v_parent == NULLVP))
2274 return;
2275
2276 NAME_CACHE_LOCK();
2277
2278 if (vp->v_parent)
2279 vp->v_parent->v_nc_generation++;
2280
2281 while ( (ncp = LIST_FIRST(&vp->v_nclinks)) )
2282 cache_delete(ncp, 1);
2283
2284 while ( (ncp = TAILQ_FIRST(&vp->v_ncchildren)) )
2285 cache_delete(ncp, 1);
2286
2287 /*
2288 * Use a temp variable to avoid kauth_cred_unref() while NAME_CACHE_LOCK is held
2289 */
2290 tcred = vp->v_cred;
2291 vp->v_cred = NOCRED;
2292 vp->v_authorized_actions = 0;
2293
2294 NAME_CACHE_UNLOCK();
2295
2296 if (IS_VALID_CRED(tcred))
2297 kauth_cred_unref(&tcred);
2298 }
2299
2300 /*
2301 * Purge all negative cache entries that are children of the
2302 * given vnode. A case-insensitive file system (or any file
2303 * system that has multiple equivalent names for the same
2304 * directory entry) can use this when creating or renaming
2305 * to remove negative entries that may no longer apply.
2306 */
2307 void
2308 cache_purge_negatives(vnode_t vp)
2309 {
2310 struct namecache *ncp, *next_ncp;
2311
2312 NAME_CACHE_LOCK();
2313
2314 TAILQ_FOREACH_SAFE(ncp, &vp->v_ncchildren, nc_child, next_ncp) {
2315 if (ncp->nc_vp)
2316 break;
2317
2318 cache_delete(ncp, 1);
2319 }
2320
2321 NAME_CACHE_UNLOCK();
2322 }
2323
2324 /*
2325 * Flush all entries referencing a particular filesystem.
2326 *
2327 * Since we need to check it anyway, we will flush all the invalid
2328 * entries at the same time.
2329 */
2330 void
2331 cache_purgevfs(struct mount *mp)
2332 {
2333 struct nchashhead *ncpp;
2334 struct namecache *ncp;
2335
2336 NAME_CACHE_LOCK();
2337 /* Scan hash tables for applicable entries */
2338 for (ncpp = &nchashtbl[nchash - 1]; ncpp >= nchashtbl; ncpp--) {
2339 restart:
2340 for (ncp = ncpp->lh_first; ncp != 0; ncp = ncp->nc_hash.le_next) {
2341 if (ncp->nc_dvp->v_mount == mp) {
2342 cache_delete(ncp, 0);
2343 goto restart;
2344 }
2345 }
2346 }
2347 NAME_CACHE_UNLOCK();
2348 }
2349
2350
2351
2352 //
2353 // String ref routines
2354 //
2355 static LIST_HEAD(stringhead, string_t) *string_ref_table;
2356 static u_long string_table_mask;
2357 static uint32_t filled_buckets=0;
2358
2359
2360 typedef struct string_t {
2361 LIST_ENTRY(string_t) hash_chain;
2362 const char *str;
2363 uint32_t refcount;
2364 } string_t;
2365
2366
2367 static void
2368 resize_string_ref_table(void)
2369 {
2370 struct stringhead *new_table;
2371 struct stringhead *old_table;
2372 struct stringhead *old_head, *head;
2373 string_t *entry, *next;
2374 uint32_t i, hashval;
2375 u_long new_mask, old_mask;
2376
2377 /*
2378 * need to hold the table lock exclusively
2379 * in order to grow the table... need to recheck
2380 * the need to resize again after we've taken
2381 * the lock exclusively in case some other thread
2382 * beat us to the punch
2383 */
2384 lck_rw_lock_exclusive(strtable_rw_lock);
2385
2386 if (4 * filled_buckets < ((string_table_mask + 1) * 3)) {
2387 lck_rw_done(strtable_rw_lock);
2388 return;
2389 }
2390 new_table = hashinit((string_table_mask + 1) * 2, M_CACHE, &new_mask);
2391
2392 if (new_table == NULL) {
2393 printf("failed to resize the hash table.\n");
2394 lck_rw_done(strtable_rw_lock);
2395 return;
2396 }
2397
2398 // do the switch!
2399 old_table = string_ref_table;
2400 string_ref_table = new_table;
2401 old_mask = string_table_mask;
2402 string_table_mask = new_mask;
2403 filled_buckets = 0;
2404
2405 // walk the old table and insert all the entries into
2406 // the new table
2407 //
2408 for (i = 0; i <= old_mask; i++) {
2409 old_head = &old_table[i];
2410 for (entry = old_head->lh_first; entry != NULL; entry = next) {
2411 hashval = hash_string((const char *)entry->str, 0);
2412 head = &string_ref_table[hashval & string_table_mask];
2413 if (head->lh_first == NULL) {
2414 filled_buckets++;
2415 }
2416 next = entry->hash_chain.le_next;
2417 LIST_INSERT_HEAD(head, entry, hash_chain);
2418 }
2419 }
2420 lck_rw_done(strtable_rw_lock);
2421
2422 FREE(old_table, M_CACHE);
2423 }
2424
2425
2426 static void
2427 init_string_table(void)
2428 {
2429 string_ref_table = hashinit(CONFIG_VFS_NAMES, M_CACHE, &string_table_mask);
2430 }
2431
2432
2433 const char *
2434 vfs_addname(const char *name, uint32_t len, u_int hashval, u_int flags)
2435 {
2436 return (add_name_internal(name, len, hashval, FALSE, flags));
2437 }
2438
2439
2440 static const char *
2441 add_name_internal(const char *name, uint32_t len, u_int hashval, boolean_t need_extra_ref, __unused u_int flags)
2442 {
2443 struct stringhead *head;
2444 string_t *entry;
2445 uint32_t chain_len = 0;
2446 uint32_t hash_index;
2447 uint32_t lock_index;
2448 char *ptr;
2449
2450 if (len > MAXPATHLEN)
2451 len = MAXPATHLEN;
2452
2453 /*
2454 * if the length already accounts for the null-byte, then
2455 * subtract one so later on we don't index past the end
2456 * of the string.
2457 */
2458 if (len > 0 && name[len-1] == '\0') {
2459 len--;
2460 }
2461 if (hashval == 0) {
2462 hashval = hash_string(name, len);
2463 }
2464
2465 /*
2466 * take this lock 'shared' to keep the hash stable
2467 * if someone else decides to grow the pool they
2468 * will take this lock exclusively
2469 */
2470 lck_rw_lock_shared(strtable_rw_lock);
2471
2472 /*
2473 * If the table gets more than 3/4 full, resize it
2474 */
2475 if (4 * filled_buckets >= ((string_table_mask + 1) * 3)) {
2476 lck_rw_done(strtable_rw_lock);
2477
2478 resize_string_ref_table();
2479
2480 lck_rw_lock_shared(strtable_rw_lock);
2481 }
2482 hash_index = hashval & string_table_mask;
2483 lock_index = hash_index % NUM_STRCACHE_LOCKS;
2484
2485 head = &string_ref_table[hash_index];
2486
2487 lck_mtx_lock_spin(&strcache_mtx_locks[lock_index]);
2488
2489 for (entry = head->lh_first; entry != NULL; chain_len++, entry = entry->hash_chain.le_next) {
2490 if (strncmp(entry->str, name, len) == 0 && entry->str[len] == 0) {
2491 entry->refcount++;
2492 break;
2493 }
2494 }
2495 if (entry == NULL) {
2496 lck_mtx_convert_spin(&strcache_mtx_locks[lock_index]);
2497 /*
2498 * it wasn't already there so add it.
2499 */
2500 MALLOC(entry, string_t *, sizeof(string_t) + len + 1, M_TEMP, M_WAITOK);
2501
2502 if (head->lh_first == NULL) {
2503 OSAddAtomic(1, &filled_buckets);
2504 }
2505 ptr = (char *)((char *)entry + sizeof(string_t));
2506 strncpy(ptr, name, len);
2507 ptr[len] = '\0';
2508 entry->str = ptr;
2509 entry->refcount = 1;
2510 LIST_INSERT_HEAD(head, entry, hash_chain);
2511 }
2512 if (need_extra_ref == TRUE)
2513 entry->refcount++;
2514
2515 lck_mtx_unlock(&strcache_mtx_locks[lock_index]);
2516 lck_rw_done(strtable_rw_lock);
2517
2518 return (const char *)entry->str;
2519 }
2520
2521
2522 int
2523 vfs_removename(const char *nameref)
2524 {
2525 struct stringhead *head;
2526 string_t *entry;
2527 uint32_t hashval;
2528 uint32_t hash_index;
2529 uint32_t lock_index;
2530 int retval = ENOENT;
2531
2532 hashval = hash_string(nameref, 0);
2533
2534 /*
2535 * take this lock 'shared' to keep the hash stable
2536 * if someone else decides to grow the pool they
2537 * will take this lock exclusively
2538 */
2539 lck_rw_lock_shared(strtable_rw_lock);
2540 /*
2541 * must compute the head behind the table lock
2542 * since the size and location of the table
2543 * can change on the fly
2544 */
2545 hash_index = hashval & string_table_mask;
2546 lock_index = hash_index % NUM_STRCACHE_LOCKS;
2547
2548 head = &string_ref_table[hash_index];
2549
2550 lck_mtx_lock_spin(&strcache_mtx_locks[lock_index]);
2551
2552 for (entry = head->lh_first; entry != NULL; entry = entry->hash_chain.le_next) {
2553 if (entry->str == nameref) {
2554 entry->refcount--;
2555
2556 if (entry->refcount == 0) {
2557 LIST_REMOVE(entry, hash_chain);
2558
2559 if (head->lh_first == NULL) {
2560 OSAddAtomic(-1, &filled_buckets);
2561 }
2562 } else {
2563 entry = NULL;
2564 }
2565 retval = 0;
2566 break;
2567 }
2568 }
2569 lck_mtx_unlock(&strcache_mtx_locks[lock_index]);
2570 lck_rw_done(strtable_rw_lock);
2571
2572 if (entry != NULL)
2573 FREE(entry, M_TEMP);
2574
2575 return retval;
2576 }
2577
2578
2579 #ifdef DUMP_STRING_TABLE
2580 void
2581 dump_string_table(void)
2582 {
2583 struct stringhead *head;
2584 string_t *entry;
2585 u_long i;
2586
2587 lck_rw_lock_shared(strtable_rw_lock);
2588
2589 for (i = 0; i <= string_table_mask; i++) {
2590 head = &string_ref_table[i];
2591 for (entry=head->lh_first; entry != NULL; entry=entry->hash_chain.le_next) {
2592 printf("%6d - %s\n", entry->refcount, entry->str);
2593 }
2594 }
2595 lck_rw_done(strtable_rw_lock);
2596 }
2597 #endif /* DUMP_STRING_TABLE */