]> git.saurik.com Git - apple/xnu.git/blob - bsd/vfs/vfs_cache.c
xnu-1228.15.4.tar.gz
[apple/xnu.git] / bsd / vfs / vfs_cache.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, 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 <sys/namei.h>
79 #include <sys/errno.h>
80 #include <sys/malloc.h>
81 #include <sys/kauth.h>
82 #include <sys/user.h>
83 #include <sys/paths.h>
84
85 #if CONFIG_MACF
86 #include <security/mac_framework.h>
87 #endif
88
89 /*
90 * Name caching works as follows:
91 *
92 * Names found by directory scans are retained in a cache
93 * for future reference. It is managed LRU, so frequently
94 * used names will hang around. Cache is indexed by hash value
95 * obtained from (vp, name) where vp refers to the directory
96 * containing name.
97 *
98 * If it is a "negative" entry, (i.e. for a name that is known NOT to
99 * exist) the vnode pointer will be NULL.
100 *
101 * Upon reaching the last segment of a path, if the reference
102 * is for DELETE, or NOCACHE is set (rewrite), and the
103 * name is located in the cache, it will be dropped.
104 */
105
106 /*
107 * Structures associated with name cacheing.
108 */
109
110 LIST_HEAD(nchashhead, namecache) *nchashtbl; /* Hash Table */
111 u_long nchashmask;
112 u_long nchash; /* size of hash table - 1 */
113 long numcache; /* number of cache entries allocated */
114 int desiredNodes;
115 int desiredNegNodes;
116 int ncs_negtotal;
117 TAILQ_HEAD(, namecache) nchead; /* chain of all name cache entries */
118 TAILQ_HEAD(, namecache) neghead; /* chain of only negative cache entries */
119
120
121 #if COLLECT_STATS
122
123 struct nchstats nchstats; /* cache effectiveness statistics */
124
125 #define NCHSTAT(v) { \
126 nchstats.v++; \
127 }
128 #define NAME_CACHE_LOCK() name_cache_lock()
129 #define NAME_CACHE_UNLOCK() name_cache_unlock()
130 #define NAME_CACHE_LOCK_SHARED() name_cache_lock()
131
132 #else
133
134 #define NCHSTAT(v)
135 #define NAME_CACHE_LOCK() name_cache_lock()
136 #define NAME_CACHE_UNLOCK() name_cache_unlock()
137 #define NAME_CACHE_LOCK_SHARED() name_cache_lock_shared()
138
139 #endif
140
141
142 /* vars for name cache list lock */
143 lck_grp_t * namecache_lck_grp;
144 lck_grp_attr_t * namecache_lck_grp_attr;
145 lck_attr_t * namecache_lck_attr;
146 lck_rw_t * namecache_rw_lock;
147
148 static vnode_t cache_lookup_locked(vnode_t dvp, struct componentname *cnp);
149 static int remove_name_locked(const char *);
150 static const char *add_name_locked(const char *, size_t, u_int, u_int);
151 static void init_string_table(void) __attribute__((section("__TEXT, initcode")));
152 static void cache_delete(struct namecache *, int);
153 static void cache_enter_locked(vnode_t dvp, vnode_t vp, struct componentname *cnp);
154
155 #ifdef DUMP_STRING_TABLE
156 /*
157 * Internal dump function used for debugging
158 */
159 void dump_string_table(void);
160 #endif /* DUMP_STRING_TABLE */
161
162 static void init_crc32(void) __attribute__((section("__TEXT, initcode")));
163 static unsigned int crc32tab[256];
164
165
166 #define NCHHASH(dvp, hash_val) \
167 (&nchashtbl[(dvp->v_id ^ (hash_val)) & nchashmask])
168
169
170
171 //
172 // This function builds the path to a filename in "buff". The
173 // length of the buffer *INCLUDING* the trailing zero byte is
174 // returned in outlen. NOTE: the length includes the trailing
175 // zero byte and thus the length is one greater than what strlen
176 // would return. This is important and lots of code elsewhere
177 // in the kernel assumes this behavior.
178 //
179 // This function can call vnop in file system if the parent vnode
180 // does not exist or when called for hardlinks via volfs path.
181 // If BUILDPATH_NO_FS_ENTER is set in flags, it only uses values present
182 // in the name cache and does not enter the file system.
183 //
184 int
185 build_path(vnode_t first_vp, char *buff, int buflen, int *outlen, int flags, vfs_context_t ctx)
186 {
187 vnode_t vp;
188 vnode_t proc_root_dir_vp;
189 char *end;
190 const char *str;
191 int len;
192 int ret = 0;
193 int fixhardlink;
194
195 if (first_vp == NULLVP) {
196 return (EINVAL);
197 }
198 /* Grab the process fd so we can evaluate fd_rdir. */
199 if (vfs_context_proc(ctx)->p_fd) {
200 proc_root_dir_vp = vfs_context_proc(ctx)->p_fd->fd_rdir;
201 } else {
202 proc_root_dir_vp = NULL;
203 }
204 again:
205 vp = first_vp;
206 end = &buff[buflen-1];
207 *end = '\0';
208
209 /* Check if this is the root of a file system. */
210 while (vp && vp->v_flag & VROOT) {
211 if (vp->v_mount == NULL) {
212 return (EINVAL);
213 }
214 if ((vp->v_mount->mnt_flag & MNT_ROOTFS) || (vp == proc_root_dir_vp)) {
215 /*
216 * It's the root of the root file system, so it's
217 * just "/".
218 */
219 *--end = '/';
220 goto out;
221 } else {
222 vp = vp->v_mount->mnt_vnodecovered;
223 }
224 }
225 NAME_CACHE_LOCK_SHARED();
226
227 while ((vp != NULLVP) && (vp->v_parent != vp)) {
228 /*
229 * For hardlinks the v_name may be stale, so if its OK
230 * to enter a file system, ask the file system for the
231 * name and parent (below).
232 */
233 fixhardlink = (vp->v_flag & VISHARDLINK) &&
234 (vp->v_mount->mnt_kern_flag & MNTK_PATH_FROM_ID) &&
235 !(flags & BUILDPATH_NO_FS_ENTER);
236 if (!fixhardlink) {
237 str = vp->v_name;
238 if (str == NULL || *str == '\0') {
239 if (vp->v_parent != NULL) {
240 ret = EINVAL;
241 } else {
242 ret = ENOENT;
243 }
244 break;
245 }
246 len = strlen(str);
247 /*
248 * Check that there's enough space (including space for the '/')
249 */
250 if ((end - buff) < (len + 1)) {
251 ret = ENOSPC;
252 break;
253 }
254 /* Copy the name backwards. */
255 str += len;
256
257 for (; len > 0; len--) {
258 *--end = *--str;
259 }
260 /* Add a path separator. */
261 *--end = '/';
262 }
263
264 /*
265 * Walk up the parent chain.
266 */
267 if (((vp->v_parent != NULLVP) && !fixhardlink) ||
268 (flags & BUILDPATH_NO_FS_ENTER)) {
269 vp = vp->v_parent;
270
271 // if the vnode we have in hand isn't a directory and it
272 // has a v_parent, then we started with the resource fork
273 // so skip up to avoid getting a duplicate copy of the
274 // file name in the path.
275 if (vp && !vnode_isdir(vp) && vp->v_parent) {
276 vp = vp->v_parent;
277 }
278 } else /* No parent, go get it if supported. */ {
279 struct vnode_attr va;
280 vnode_t dvp;
281 int vid;
282
283 /* Make sure file system supports obtaining a path from id. */
284 if (!(vp->v_mount->mnt_kern_flag & MNTK_PATH_FROM_ID)) {
285 ret = ENOENT;
286 break;
287 }
288 vid = vp->v_id;
289 NAME_CACHE_UNLOCK();
290
291 if (vnode_getwithvid(vp, vid) != 0) {
292 /* vnode was recycled, so start over. */
293 goto again;
294 }
295
296 VATTR_INIT(&va);
297 VATTR_WANTED(&va, va_parentid);
298 if (fixhardlink) {
299 VATTR_WANTED(&va, va_name);
300 MALLOC_ZONE(va.va_name, caddr_t, MAXPATHLEN, M_NAMEI, M_WAITOK);
301 } else {
302 va.va_name = NULL;
303 }
304 /* Ask the file system for its parent id and for its name (optional). */
305 ret = vnode_getattr(vp, &va, ctx);
306
307 if (fixhardlink) {
308 if ((ret == 0) && (VATTR_IS_SUPPORTED(&va, va_name))) {
309 str = va.va_name;
310 } else if (vp->v_name) {
311 str = vp->v_name;
312 ret = 0;
313 } else {
314 ret = ENOENT;
315 goto bad_news;
316 }
317 len = strlen(str);
318
319 /* Check that there's enough space. */
320 if ((end - buff) < (len + 1)) {
321 ret = ENOSPC;
322 } else {
323 /* Copy the name backwards. */
324 str += len;
325
326 for (; len > 0; len--) {
327 *--end = *--str;
328 }
329 /* Add a path separator. */
330 *--end = '/';
331 }
332 bad_news:
333 FREE_ZONE(va.va_name, MAXPATHLEN, M_NAMEI);
334 }
335 if (ret || !VATTR_IS_SUPPORTED(&va, va_parentid)) {
336 vnode_put(vp);
337 ret = ENOENT;
338 goto out;
339 }
340 /* Ask the file system for the parent vnode. */
341 ret = VFS_VGET(vp->v_mount, (ino64_t)va.va_parentid, &dvp, ctx);
342 if (ret) {
343 vnode_put(vp);
344 goto out;
345 }
346 if (!fixhardlink && (vp->v_parent != dvp)) {
347 vnode_update_identity(vp, dvp, NULL, 0, 0, VNODE_UPDATE_PARENT);
348 }
349 vnode_put(vp);
350 vp = dvp;
351 /*
352 * We are no longer under the name cache lock here.
353 * So to avoid a race for vnode termination, take a
354 * reference on the vnode and drop that reference
355 * after reacquiring the name cache lock. We use the
356 * vnode_rele_ext call with the dont_reenter flag
357 * set to avoid re-entering the file system which
358 * could possibly re-enter the name cache.
359 */
360 if (vnode_ref(dvp) != 0) {
361 dvp = NULLVP;
362 }
363 vnode_put(vp);
364 NAME_CACHE_LOCK_SHARED();
365
366 if (dvp) {
367 vnode_rele_ext(dvp, 0, 1);
368 }
369
370 // if the vnode we have in hand isn't a directory and it
371 // has a v_parent, then we started with the resource fork
372 // so skip up to avoid getting a duplicate copy of the
373 // file name in the path.
374 if (vp && !vnode_isdir(vp) && vp->v_parent) {
375 vp = vp->v_parent;
376 }
377 }
378 /*
379 * When a mount point is crossed switch the vp.
380 * Continue until we find the root or we find
381 * a vnode that's not the root of a mounted
382 * file system.
383 */
384 while (vp) {
385 if (vp == proc_root_dir_vp) {
386 NAME_CACHE_UNLOCK();
387 goto out; /* encountered the root */
388 }
389 if (!(vp->v_flag & VROOT) || !vp->v_mount)
390 break; /* not the root of a mounted FS */
391 vp = vp->v_mount->mnt_vnodecovered;
392 }
393 }
394 NAME_CACHE_UNLOCK();
395 out:
396 /* Slide the name down to the beginning of the buffer. */
397 memmove(buff, end, &buff[buflen] - end);
398
399 *outlen = &buff[buflen] - end; /* length includes the trailing zero byte */
400
401 return (ret);
402 }
403
404
405 /*
406 * return NULLVP if vp's parent doesn't
407 * exist, or we can't get a valid iocount
408 * else return the parent of vp
409 */
410 vnode_t
411 vnode_getparent(vnode_t vp)
412 {
413 vnode_t pvp = NULLVP;
414 int pvid;
415
416 NAME_CACHE_LOCK_SHARED();
417 /*
418 * v_parent is stable behind the name_cache lock
419 * however, the only thing we can really guarantee
420 * is that we've grabbed a valid iocount on the
421 * parent of 'vp' at the time we took the name_cache lock...
422 * once we drop the lock, vp could get re-parented
423 */
424 if ( (pvp = vp->v_parent) != NULLVP ) {
425 pvid = pvp->v_id;
426
427 NAME_CACHE_UNLOCK();
428
429 if (vnode_getwithvid(pvp, pvid) != 0)
430 pvp = NULL;
431 } else
432 NAME_CACHE_UNLOCK();
433 return (pvp);
434 }
435
436 const char *
437 vnode_getname(vnode_t vp)
438 {
439 const char *name = NULL;
440
441 NAME_CACHE_LOCK();
442
443 if (vp->v_name)
444 name = add_name_locked(vp->v_name, strlen(vp->v_name), 0, 0);
445 NAME_CACHE_UNLOCK();
446
447 return (name);
448 }
449
450 void
451 vnode_putname(const char *name)
452 {
453 NAME_CACHE_LOCK();
454
455 remove_name_locked(name);
456
457 NAME_CACHE_UNLOCK();
458 }
459
460
461 /*
462 * if VNODE_UPDATE_PARENT, and we can take
463 * a reference on dvp, then update vp with
464 * it's new parent... if vp already has a parent,
465 * then drop the reference vp held on it
466 *
467 * if VNODE_UPDATE_NAME,
468 * then drop string ref on v_name if it exists, and if name is non-NULL
469 * then pick up a string reference on name and record it in v_name...
470 * optionally pass in the length and hashval of name if known
471 *
472 * if VNODE_UPDATE_CACHE, flush the name cache entries associated with vp
473 */
474 void
475 vnode_update_identity(vnode_t vp, vnode_t dvp, const char *name, int name_len, int name_hashval, int flags)
476 {
477 struct namecache *ncp;
478 vnode_t old_parentvp = NULLVP;
479 #if NAMEDSTREAMS
480 int isstream = (vp->v_flag & VISNAMEDSTREAM);
481 int kusecountbumped = 0;
482 #endif
483
484 if (flags & VNODE_UPDATE_PARENT) {
485 if (dvp && vnode_ref(dvp) != 0) {
486 dvp = NULLVP;
487 }
488 #if NAMEDSTREAMS
489 /* Don't count a stream's parent ref during unmounts */
490 if (isstream && dvp && (dvp != vp) && (dvp != vp->v_parent) && (dvp->v_type == VREG)) {
491 vnode_lock_spin(dvp);
492 ++dvp->v_kusecount;
493 kusecountbumped = 1;
494 vnode_unlock(dvp);
495 }
496 #endif
497 } else {
498 dvp = NULLVP;
499 }
500 NAME_CACHE_LOCK();
501
502 if ( (flags & VNODE_UPDATE_NAME) && (name != vp->v_name) ) {
503 if (vp->v_name != NULL) {
504 remove_name_locked(vp->v_name);
505 vp->v_name = NULL;
506 }
507 if (name && *name) {
508 if (name_len == 0)
509 name_len = strlen(name);
510 vp->v_name = add_name_locked(name, name_len, name_hashval, 0);
511 }
512 }
513 if (flags & VNODE_UPDATE_PARENT) {
514 if (dvp != vp && dvp != vp->v_parent) {
515 old_parentvp = vp->v_parent;
516 vp->v_parent = dvp;
517 dvp = NULLVP;
518
519 if (old_parentvp)
520 flags |= VNODE_UPDATE_CACHE;
521 }
522 }
523 if (flags & VNODE_UPDATE_CACHE) {
524 while ( (ncp = LIST_FIRST(&vp->v_nclinks)) )
525 cache_delete(ncp, 1);
526 }
527 NAME_CACHE_UNLOCK();
528
529 if (dvp != NULLVP) {
530 #if NAMEDSTREAMS
531 /* Back-out the ref we took if we lost a race for vp->v_parent. */
532 if (kusecountbumped) {
533 vnode_lock_spin(dvp);
534 if (dvp->v_kusecount > 0)
535 --dvp->v_kusecount;
536 vnode_unlock(dvp);
537 }
538 #endif
539 vnode_rele(dvp);
540 }
541 if (old_parentvp) {
542 struct uthread *ut;
543
544 #if NAMEDSTREAMS
545 if (isstream) {
546 vnode_lock_spin(old_parentvp);
547 if ((old_parentvp->v_type != VDIR) && (old_parentvp->v_kusecount > 0))
548 --old_parentvp->v_kusecount;
549 vnode_unlock(old_parentvp);
550 }
551 #endif
552 ut = get_bsdthread_info(current_thread());
553
554 /*
555 * indicated to vnode_rele that it shouldn't do a
556 * vnode_reclaim at this time... instead it will
557 * chain the vnode to the uu_vreclaims list...
558 * we'll be responsible for calling vnode_reclaim
559 * on each of the vnodes in this list...
560 */
561 ut->uu_defer_reclaims = 1;
562 ut->uu_vreclaims = NULLVP;
563
564 while ( (vp = old_parentvp) != NULLVP ) {
565
566 vnode_lock(vp);
567 vnode_rele_internal(vp, 0, 0, 1);
568
569 /*
570 * check to see if the vnode is now in the state
571 * that would have triggered a vnode_reclaim in vnode_rele
572 * if it is, we save it's parent pointer and then NULL
573 * out the v_parent field... we'll drop the reference
574 * that was held on the next iteration of this loop...
575 * this short circuits a potential deep recursion if we
576 * have a long chain of parents in this state...
577 * we'll sit in this loop until we run into
578 * a parent in this chain that is not in this state
579 *
580 * make our check and the node_rele atomic
581 * with respect to the current vnode we're working on
582 * by holding the vnode lock
583 * if vnode_rele deferred the vnode_reclaim and has put
584 * this vnode on the list to be reaped by us, than
585 * it has left this vnode with an iocount == 1
586 */
587 if ( (vp->v_iocount == 1) && (vp->v_usecount == 0) &&
588 ((vp->v_lflag & (VL_MARKTERM | VL_TERMINATE | VL_DEAD)) == VL_MARKTERM)) {
589 /*
590 * vnode_rele wanted to do a vnode_reclaim on this vnode
591 * it should be sitting on the head of the uu_vreclaims chain
592 * pull the parent pointer now so that when we do the
593 * vnode_reclaim for each of the vnodes in the uu_vreclaims
594 * list, we won't recurse back through here
595 *
596 * need to do a convert here in case vnode_rele_internal
597 * returns with the lock held in the spin mode... it
598 * can drop and retake the lock under certain circumstances
599 */
600 vnode_lock_convert(vp);
601
602 NAME_CACHE_LOCK();
603 old_parentvp = vp->v_parent;
604 vp->v_parent = NULLVP;
605 NAME_CACHE_UNLOCK();
606 } else {
607 /*
608 * we're done... we ran into a vnode that isn't
609 * being terminated
610 */
611 old_parentvp = NULLVP;
612 }
613 vnode_unlock(vp);
614 }
615 ut->uu_defer_reclaims = 0;
616
617 while ( (vp = ut->uu_vreclaims) != NULLVP) {
618 ut->uu_vreclaims = vp->v_defer_reclaimlist;
619
620 /*
621 * vnode_put will drive the vnode_reclaim if
622 * we are still the only reference on this vnode
623 */
624 vnode_put(vp);
625 }
626 }
627 }
628
629
630 /*
631 * Mark a vnode as having multiple hard links. HFS makes use of this
632 * because it keeps track of each link separately, and wants to know
633 * which link was actually used.
634 *
635 * This will cause the name cache to force a VNOP_LOOKUP on the vnode
636 * so that HFS can post-process the lookup. Also, volfs will call
637 * VNOP_GETATTR2 to determine the parent, instead of using v_parent.
638 */
639 void vnode_setmultipath(vnode_t vp)
640 {
641 vnode_lock_spin(vp);
642
643 /*
644 * In theory, we're changing the vnode's identity as far as the
645 * name cache is concerned, so we ought to grab the name cache lock
646 * here. However, there is already a race, and grabbing the name
647 * cache lock only makes the race window slightly smaller.
648 *
649 * The race happens because the vnode already exists in the name
650 * cache, and could be found by one thread before another thread
651 * can set the hard link flag.
652 */
653
654 vp->v_flag |= VISHARDLINK;
655
656 vnode_unlock(vp);
657 }
658
659
660
661 /*
662 * backwards compatibility
663 */
664 void vnode_uncache_credentials(vnode_t vp)
665 {
666 vnode_uncache_authorized_action(vp, KAUTH_INVALIDATE_CACHED_RIGHTS);
667 }
668
669
670 /*
671 * use the exclusive form of NAME_CACHE_LOCK to protect the update of the
672 * following fields in the vnode: v_cred_timestamp, v_cred, v_authorized_actions
673 * we use this lock so that we can look at the v_cred and v_authorized_actions
674 * atomically while behind the NAME_CACHE_LOCK in shared mode in 'cache_lookup_path',
675 * which is the super-hot path... if we are updating the authorized actions for this
676 * vnode, we are already in the super-slow and far less frequented path so its not
677 * that bad that we take the lock exclusive for this case... of course we strive
678 * to hold it for the minimum amount of time possible
679 */
680
681 void vnode_uncache_authorized_action(vnode_t vp, kauth_action_t action)
682 {
683 kauth_cred_t tcred = NOCRED;
684
685 NAME_CACHE_LOCK();
686
687 vp->v_authorized_actions &= ~action;
688
689 if (action == KAUTH_INVALIDATE_CACHED_RIGHTS &&
690 IS_VALID_CRED(vp->v_cred)) {
691 /*
692 * Use a temp variable to avoid kauth_cred_unref() while NAME_CACHE_LOCK is held
693 */
694 tcred = vp->v_cred;
695 vp->v_cred = NOCRED;
696 }
697 NAME_CACHE_UNLOCK();
698
699 if (tcred != NOCRED)
700 kauth_cred_unref(&tcred);
701 }
702
703
704 boolean_t vnode_cache_is_authorized(vnode_t vp, vfs_context_t ctx, kauth_action_t action)
705 {
706 kauth_cred_t ucred;
707 boolean_t retval = FALSE;
708
709 if ( (vp->v_mount->mnt_kern_flag & (MNTK_AUTH_OPAQUE | MNTK_AUTH_CACHE_TTL)) ) {
710 /*
711 * a TTL is enabled on the rights cache... handle it here
712 * a TTL of 0 indicates that no rights should be cached
713 */
714 if (vp->v_mount->mnt_authcache_ttl) {
715 if ( !(vp->v_mount->mnt_kern_flag & MNTK_AUTH_CACHE_TTL) ) {
716 /*
717 * For filesystems marked only MNTK_AUTH_OPAQUE (generally network ones),
718 * we will only allow a SEARCH right on a directory to be cached...
719 * that cached right always has a default TTL associated with it
720 */
721 if (action != KAUTH_VNODE_SEARCH || vp->v_type != VDIR)
722 vp = NULLVP;
723 }
724 if (vp != NULLVP && vnode_cache_is_stale(vp) == TRUE) {
725 vnode_uncache_authorized_action(vp, vp->v_authorized_actions);
726 vp = NULLVP;
727 }
728 } else
729 vp = NULLVP;
730 }
731 if (vp != NULLVP) {
732 ucred = vfs_context_ucred(ctx);
733
734 NAME_CACHE_LOCK_SHARED();
735
736 if (vp->v_cred == ucred && (vp->v_authorized_actions & action) == action)
737 retval = TRUE;
738
739 NAME_CACHE_UNLOCK();
740 }
741 return retval;
742 }
743
744
745 void vnode_cache_authorized_action(vnode_t vp, vfs_context_t ctx, kauth_action_t action)
746 {
747 kauth_cred_t tcred = NOCRED;
748 kauth_cred_t ucred;
749 struct timeval tv;
750 boolean_t ttl_active = FALSE;
751
752 ucred = vfs_context_ucred(ctx);
753
754 if (!IS_VALID_CRED(ucred) || action == 0)
755 return;
756
757 if ( (vp->v_mount->mnt_kern_flag & (MNTK_AUTH_OPAQUE | MNTK_AUTH_CACHE_TTL)) ) {
758 /*
759 * a TTL is enabled on the rights cache... handle it here
760 * a TTL of 0 indicates that no rights should be cached
761 */
762 if (vp->v_mount->mnt_authcache_ttl == 0)
763 return;
764
765 if ( !(vp->v_mount->mnt_kern_flag & MNTK_AUTH_CACHE_TTL) ) {
766 /*
767 * only cache SEARCH action for filesystems marked
768 * MNTK_AUTH_OPAQUE on VDIRs...
769 * the lookup_path code will time these out
770 */
771 if ( (action & ~KAUTH_VNODE_SEARCH) || vp->v_type != VDIR )
772 return;
773 }
774 ttl_active = TRUE;
775
776 microuptime(&tv);
777 }
778 NAME_CACHE_LOCK();
779
780 if (vp->v_cred != ucred) {
781 kauth_cred_ref(ucred);
782 /*
783 * Use a temp variable to avoid kauth_cred_unref() while NAME_CACHE_LOCK is held
784 */
785 tcred = vp->v_cred;
786 vp->v_cred = ucred;
787 vp->v_authorized_actions = 0;
788 }
789 if (ttl_active == TRUE && vp->v_authorized_actions == 0) {
790 /*
791 * only reset the timestamnp on the
792 * first authorization cached after the previous
793 * timer has expired or we're switching creds...
794 * 'vnode_cache_is_authorized' will clear the
795 * authorized actions if the TTL is active and
796 * it has expired
797 */
798 vp->v_cred_timestamp = tv.tv_sec;
799 }
800 vp->v_authorized_actions |= action;
801
802 NAME_CACHE_UNLOCK();
803
804 if (IS_VALID_CRED(tcred))
805 kauth_cred_unref(&tcred);
806 }
807
808
809 boolean_t vnode_cache_is_stale(vnode_t vp)
810 {
811 struct timeval tv;
812 boolean_t retval;
813
814 microuptime(&tv);
815
816 if ((tv.tv_sec - vp->v_cred_timestamp) > vp->v_mount->mnt_authcache_ttl)
817 retval = TRUE;
818 else
819 retval = FALSE;
820
821 return retval;
822 }
823
824
825
826 /*
827 * Returns: 0 Success
828 * ERECYCLE vnode was recycled from underneath us. Force lookup to be re-driven from namei.
829 * This errno value should not be seen by anyone outside of the kernel.
830 */
831 int
832 cache_lookup_path(struct nameidata *ndp, struct componentname *cnp, vnode_t dp,
833 vfs_context_t ctx, int *trailing_slash, int *dp_authorized, vnode_t last_dp)
834 {
835 char *cp; /* pointer into pathname argument */
836 int vid;
837 int vvid = 0; /* protected by vp != NULLVP */
838 vnode_t vp = NULLVP;
839 vnode_t tdp = NULLVP;
840 kauth_cred_t ucred;
841 boolean_t ttl_enabled = FALSE;
842 struct timeval tv;
843 mount_t mp;
844 unsigned int hash;
845 int error = 0;
846
847 ucred = vfs_context_ucred(ctx);
848 *trailing_slash = 0;
849
850 NAME_CACHE_LOCK_SHARED();
851
852 if ( dp->v_mount && (dp->v_mount->mnt_kern_flag & (MNTK_AUTH_OPAQUE | MNTK_AUTH_CACHE_TTL)) ) {
853 ttl_enabled = TRUE;
854 microuptime(&tv);
855 }
856 for (;;) {
857 /*
858 * Search a directory.
859 *
860 * The cn_hash value is for use by cache_lookup
861 * The last component of the filename is left accessible via
862 * cnp->cn_nameptr for callers that need the name.
863 */
864 hash = 0;
865 cp = cnp->cn_nameptr;
866
867 while (*cp && (*cp != '/')) {
868 hash ^= crc32tab[((hash >> 24) ^ (unsigned char)*cp++)];
869 }
870 /*
871 * the crc generator can legitimately generate
872 * a 0... however, 0 for us means that we
873 * haven't computed a hash, so use 1 instead
874 */
875 if (hash == 0)
876 hash = 1;
877 cnp->cn_hash = hash;
878 cnp->cn_namelen = cp - cnp->cn_nameptr;
879
880 ndp->ni_pathlen -= cnp->cn_namelen;
881 ndp->ni_next = cp;
882
883 /*
884 * Replace multiple slashes by a single slash and trailing slashes
885 * by a null. This must be done before VNOP_LOOKUP() because some
886 * fs's don't know about trailing slashes. Remember if there were
887 * trailing slashes to handle symlinks, existing non-directories
888 * and non-existing files that won't be directories specially later.
889 */
890 while (*cp == '/' && (cp[1] == '/' || cp[1] == '\0')) {
891 cp++;
892 ndp->ni_pathlen--;
893
894 if (*cp == '\0') {
895 *trailing_slash = 1;
896 *ndp->ni_next = '\0';
897 }
898 }
899 ndp->ni_next = cp;
900
901 cnp->cn_flags &= ~(MAKEENTRY | ISLASTCN | ISDOTDOT);
902
903 if (*cp == '\0')
904 cnp->cn_flags |= ISLASTCN;
905
906 if (cnp->cn_namelen == 2 && cnp->cn_nameptr[1] == '.' && cnp->cn_nameptr[0] == '.')
907 cnp->cn_flags |= ISDOTDOT;
908
909 *dp_authorized = 0;
910 #if NAMEDRSRCFORK
911 /*
912 * Process a request for a file's resource fork.
913 *
914 * Consume the _PATH_RSRCFORKSPEC suffix and tag the path.
915 */
916 if ((ndp->ni_pathlen == sizeof(_PATH_RSRCFORKSPEC)) &&
917 (cp[1] == '.' && cp[2] == '.') &&
918 bcmp(cp, _PATH_RSRCFORKSPEC, sizeof(_PATH_RSRCFORKSPEC)) == 0) {
919 /* Skip volfs file systems that don't support native streams. */
920 if ((dp->v_mount != NULL) &&
921 (dp->v_mount->mnt_flag & MNT_DOVOLFS) &&
922 (dp->v_mount->mnt_kern_flag & MNTK_NAMED_STREAMS) == 0) {
923 goto skiprsrcfork;
924 }
925 cnp->cn_flags |= CN_WANTSRSRCFORK;
926 cnp->cn_flags |= ISLASTCN;
927 ndp->ni_next[0] = '\0';
928 ndp->ni_pathlen = 1;
929 }
930 skiprsrcfork:
931 #endif
932
933 #if CONFIG_MACF
934
935 /*
936 * Name cache provides authorization caching (see below)
937 * that will short circuit MAC checks in lookup().
938 * We must perform MAC check here. On denial
939 * dp_authorized will remain 0 and second check will
940 * be perfomed in lookup().
941 */
942 if (!(cnp->cn_flags & DONOTAUTH)) {
943 error = mac_vnode_check_lookup(ctx, dp, cnp);
944 if (error) {
945 name_cache_unlock();
946 goto errorout;
947 }
948 }
949 #endif /* MAC */
950 if (ttl_enabled && ((tv.tv_sec - dp->v_cred_timestamp) > dp->v_mount->mnt_authcache_ttl))
951 break;
952
953 /*
954 * NAME_CACHE_LOCK holds these fields stable
955 */
956 if ((dp->v_cred != ucred || !(dp->v_authorized_actions & KAUTH_VNODE_SEARCH)) &&
957 !(dp->v_authorized_actions & KAUTH_VNODE_SEARCHBYANYONE))
958 break;
959
960 /*
961 * indicate that we're allowed to traverse this directory...
962 * even if we fail the cache lookup or decide to bail for
963 * some other reason, this information is valid and is used
964 * to avoid doing a vnode_authorize before the call to VNOP_LOOKUP
965 */
966 *dp_authorized = 1;
967
968 if ( (cnp->cn_flags & (ISLASTCN | ISDOTDOT)) ) {
969 if (cnp->cn_nameiop != LOOKUP)
970 break;
971 if (cnp->cn_flags & (LOCKPARENT | NOCACHE))
972 break;
973 if (cnp->cn_flags & ISDOTDOT) {
974 /*
975 * Force directory hardlinks to go to
976 * file system for ".." requests.
977 */
978 if (dp && (dp->v_flag & VISHARDLINK)) {
979 break;
980 }
981 /*
982 * Quit here only if we can't use
983 * the parent directory pointer or
984 * don't have one. Otherwise, we'll
985 * use it below.
986 */
987 if ((dp->v_flag & VROOT) ||
988 dp == ndp->ni_rootdir ||
989 dp->v_parent == NULLVP)
990 break;
991 }
992 }
993
994 /*
995 * "." and ".." aren't supposed to be cached, so check
996 * for them before checking the cache.
997 */
998 if (cnp->cn_namelen == 1 && cnp->cn_nameptr[0] == '.')
999 vp = dp;
1000 else if ((cnp->cn_flags & ISDOTDOT) && dp->v_parent)
1001 vp = dp->v_parent;
1002 else {
1003 if ( (vp = cache_lookup_locked(dp, cnp)) == NULLVP)
1004 break;
1005 }
1006
1007 if ( (cnp->cn_flags & ISLASTCN) )
1008 break;
1009
1010 if (vp->v_type != VDIR) {
1011 if (vp->v_type != VLNK)
1012 vp = NULL;
1013 break;
1014 }
1015 if ( (mp = vp->v_mountedhere) && ((cnp->cn_flags & NOCROSSMOUNT) == 0)) {
1016
1017 if (mp->mnt_realrootvp == NULLVP || mp->mnt_generation != mount_generation ||
1018 mp->mnt_realrootvp_vid != mp->mnt_realrootvp->v_id)
1019 break;
1020 vp = mp->mnt_realrootvp;
1021 }
1022 dp = vp;
1023 vp = NULLVP;
1024
1025 cnp->cn_nameptr = ndp->ni_next + 1;
1026 ndp->ni_pathlen--;
1027 while (*cnp->cn_nameptr == '/') {
1028 cnp->cn_nameptr++;
1029 ndp->ni_pathlen--;
1030 }
1031 }
1032 if (vp != NULLVP)
1033 vvid = vp->v_id;
1034 vid = dp->v_id;
1035
1036 NAME_CACHE_UNLOCK();
1037
1038 if ((vp != NULLVP) && (vp->v_type != VLNK) &&
1039 ((cnp->cn_flags & (ISLASTCN | LOCKPARENT | WANTPARENT | SAVESTART)) == ISLASTCN)) {
1040 /*
1041 * if we've got a child and it's the last component, and
1042 * the lookup doesn't need to return the parent then we
1043 * can skip grabbing an iocount on the parent, since all
1044 * we're going to do with it is a vnode_put just before
1045 * we return from 'lookup'. If it's a symbolic link,
1046 * we need the parent in case the link happens to be
1047 * a relative pathname.
1048 */
1049 tdp = dp;
1050 dp = NULLVP;
1051 } else {
1052 need_dp:
1053 /*
1054 * return the last directory we looked at
1055 * with an io reference held. If it was the one passed
1056 * in as a result of the last iteration of VNOP_LOOKUP,
1057 * it should already hold an io ref. No need to increase ref.
1058 */
1059 if (last_dp != dp){
1060
1061 if (dp == ndp->ni_usedvp) {
1062 /*
1063 * if this vnode matches the one passed in via USEDVP
1064 * than this context already holds an io_count... just
1065 * use vnode_get to get an extra ref for lookup to play
1066 * with... can't use the getwithvid variant here because
1067 * it will block behind a vnode_drain which would result
1068 * in a deadlock (since we already own an io_count that the
1069 * vnode_drain is waiting on)... vnode_get grabs the io_count
1070 * immediately w/o waiting... it always succeeds
1071 */
1072 vnode_get(dp);
1073 } else if ( (vnode_getwithvid(dp, vid)) ) {
1074 /*
1075 * failure indicates the vnode
1076 * changed identity or is being
1077 * TERMINATED... in either case
1078 * punt this lookup.
1079 *
1080 * don't necessarily return ENOENT, though, because
1081 * we really want to go back to disk and make sure it's
1082 * there or not if someone else is changing this
1083 * vnode.
1084 */
1085 error = ERECYCLE;
1086 goto errorout;
1087 }
1088 }
1089 }
1090 if (vp != NULLVP) {
1091 if ( (vnode_getwithvid(vp, vvid)) ) {
1092 vp = NULLVP;
1093
1094 /*
1095 * can't get reference on the vp we'd like
1096 * to return... if we didn't grab a reference
1097 * on the directory (due to fast path bypass),
1098 * then we need to do it now... we can't return
1099 * with both ni_dvp and ni_vp NULL, and no
1100 * error condition
1101 */
1102 if (dp == NULLVP) {
1103 dp = tdp;
1104 goto need_dp;
1105 }
1106 }
1107 }
1108 ndp->ni_dvp = dp;
1109 ndp->ni_vp = vp;
1110
1111 errorout:
1112 /*
1113 * If we came into cache_lookup_path after an iteration of the lookup loop that
1114 * resulted in a call to VNOP_LOOKUP, then VNOP_LOOKUP returned a vnode with a io ref
1115 * on it. It is now the job of cache_lookup_path to drop the ref on this vnode
1116 * when it is no longer needed. If we get to this point, and last_dp is not NULL
1117 * and it is ALSO not the dvp we want to return to caller of this function, it MUST be
1118 * the case that we got to a subsequent path component and this previous vnode is
1119 * no longer needed. We can then drop the io ref on it.
1120 */
1121 if ((last_dp != NULLVP) && (last_dp != ndp->ni_dvp)){
1122 vnode_put(last_dp);
1123 }
1124
1125 //initialized to 0, should be the same if no error cases occurred.
1126 return error;
1127 }
1128
1129
1130 static vnode_t
1131 cache_lookup_locked(vnode_t dvp, struct componentname *cnp)
1132 {
1133 struct namecache *ncp;
1134 struct nchashhead *ncpp;
1135 long namelen = cnp->cn_namelen;
1136 char *nameptr = cnp->cn_nameptr;
1137 unsigned int hashval = (cnp->cn_hash & NCHASHMASK);
1138 vnode_t vp;
1139
1140 ncpp = NCHHASH(dvp, cnp->cn_hash);
1141 LIST_FOREACH(ncp, ncpp, nc_hash) {
1142 if ((ncp->nc_dvp == dvp) && (ncp->nc_hashval == hashval)) {
1143 if (memcmp(ncp->nc_name, nameptr, namelen) == 0 && ncp->nc_name[namelen] == 0)
1144 break;
1145 }
1146 }
1147 if (ncp == 0) {
1148 /*
1149 * We failed to find an entry
1150 */
1151 NCHSTAT(ncs_miss);
1152 return (NULL);
1153 }
1154 NCHSTAT(ncs_goodhits);
1155
1156 vp = ncp->nc_vp;
1157 if (vp && (vp->v_flag & VISHARDLINK)) {
1158 /*
1159 * The file system wants a VNOP_LOOKUP on this vnode
1160 */
1161 vp = NULL;
1162 }
1163
1164 return (vp);
1165 }
1166
1167
1168 //
1169 // Have to take a len argument because we may only need to
1170 // hash part of a componentname.
1171 //
1172 static unsigned int
1173 hash_string(const char *cp, int len)
1174 {
1175 unsigned hash = 0;
1176
1177 if (len) {
1178 while (len--) {
1179 hash ^= crc32tab[((hash >> 24) ^ (unsigned char)*cp++)];
1180 }
1181 } else {
1182 while (*cp != '\0') {
1183 hash ^= crc32tab[((hash >> 24) ^ (unsigned char)*cp++)];
1184 }
1185 }
1186 /*
1187 * the crc generator can legitimately generate
1188 * a 0... however, 0 for us means that we
1189 * haven't computed a hash, so use 1 instead
1190 */
1191 if (hash == 0)
1192 hash = 1;
1193 return hash;
1194 }
1195
1196
1197 /*
1198 * Lookup an entry in the cache
1199 *
1200 * We don't do this if the segment name is long, simply so the cache
1201 * can avoid holding long names (which would either waste space, or
1202 * add greatly to the complexity).
1203 *
1204 * Lookup is called with dvp pointing to the directory to search,
1205 * cnp pointing to the name of the entry being sought. If the lookup
1206 * succeeds, the vnode is returned in *vpp, and a status of -1 is
1207 * returned. If the lookup determines that the name does not exist
1208 * (negative cacheing), a status of ENOENT is returned. If the lookup
1209 * fails, a status of zero is returned.
1210 */
1211
1212 int
1213 cache_lookup(struct vnode *dvp, struct vnode **vpp, struct componentname *cnp)
1214 {
1215 struct namecache *ncp;
1216 struct nchashhead *ncpp;
1217 long namelen = cnp->cn_namelen;
1218 char *nameptr = cnp->cn_nameptr;
1219 unsigned int hashval = (cnp->cn_hash & NCHASHMASK);
1220 boolean_t have_exclusive = FALSE;
1221 uint32_t vid;
1222 vnode_t vp;
1223
1224 NAME_CACHE_LOCK_SHARED();
1225
1226 ncpp = NCHHASH(dvp, cnp->cn_hash);
1227 relook:
1228 LIST_FOREACH(ncp, ncpp, nc_hash) {
1229 if ((ncp->nc_dvp == dvp) && (ncp->nc_hashval == hashval)) {
1230 if (memcmp(ncp->nc_name, nameptr, namelen) == 0 && ncp->nc_name[namelen] == 0)
1231 break;
1232 }
1233 }
1234 /* We failed to find an entry */
1235 if (ncp == 0) {
1236 NCHSTAT(ncs_miss);
1237 NAME_CACHE_UNLOCK();
1238 return (0);
1239 }
1240
1241 /* We don't want to have an entry, so dump it */
1242 if ((cnp->cn_flags & MAKEENTRY) == 0) {
1243 if (have_exclusive == TRUE) {
1244 NCHSTAT(ncs_badhits);
1245 cache_delete(ncp, 1);
1246 NAME_CACHE_UNLOCK();
1247 return (0);
1248 }
1249 NAME_CACHE_UNLOCK();
1250 NAME_CACHE_LOCK();
1251 have_exclusive = TRUE;
1252 goto relook;
1253 }
1254 vp = ncp->nc_vp;
1255
1256 /* We found a "positive" match, return the vnode */
1257 if (vp) {
1258 NCHSTAT(ncs_goodhits);
1259
1260 vid = vp->v_id;
1261 NAME_CACHE_UNLOCK();
1262
1263 if (vnode_getwithvid(vp, vid)) {
1264 #if COLLECT_STATS
1265 NAME_CACHE_LOCK();
1266 NCHSTAT(ncs_badvid);
1267 NAME_CACHE_UNLOCK();
1268 #endif
1269 return (0);
1270 }
1271 *vpp = vp;
1272 return (-1);
1273 }
1274
1275 /* We found a negative match, and want to create it, so purge */
1276 if (cnp->cn_nameiop == CREATE || cnp->cn_nameiop == RENAME) {
1277 if (have_exclusive == TRUE) {
1278 NCHSTAT(ncs_badhits);
1279 cache_delete(ncp, 1);
1280 NAME_CACHE_UNLOCK();
1281 return (0);
1282 }
1283 NAME_CACHE_UNLOCK();
1284 NAME_CACHE_LOCK();
1285 have_exclusive = TRUE;
1286 goto relook;
1287 }
1288
1289 /*
1290 * We found a "negative" match, ENOENT notifies client of this match.
1291 * The nc_whiteout field records whether this is a whiteout.
1292 */
1293 NCHSTAT(ncs_neghits);
1294
1295 if (ncp->nc_whiteout)
1296 cnp->cn_flags |= ISWHITEOUT;
1297 NAME_CACHE_UNLOCK();
1298 return (ENOENT);
1299 }
1300
1301
1302 /*
1303 * Add an entry to the cache...
1304 * but first check to see if the directory
1305 * that this entry is to be associated with has
1306 * had any cache_purges applied since we took
1307 * our identity snapshot... this check needs to
1308 * be done behind the name cache lock
1309 */
1310 void
1311 cache_enter_with_gen(struct vnode *dvp, struct vnode *vp, struct componentname *cnp, int gen)
1312 {
1313
1314 if (cnp->cn_hash == 0)
1315 cnp->cn_hash = hash_string(cnp->cn_nameptr, cnp->cn_namelen);
1316
1317 NAME_CACHE_LOCK();
1318
1319 if (dvp->v_nc_generation == gen)
1320 cache_enter_locked(dvp, vp, cnp);
1321
1322 NAME_CACHE_UNLOCK();
1323 }
1324
1325
1326 /*
1327 * Add an entry to the cache.
1328 */
1329 void
1330 cache_enter(struct vnode *dvp, struct vnode *vp, struct componentname *cnp)
1331 {
1332 if (cnp->cn_hash == 0)
1333 cnp->cn_hash = hash_string(cnp->cn_nameptr, cnp->cn_namelen);
1334
1335 NAME_CACHE_LOCK();
1336
1337 cache_enter_locked(dvp, vp, cnp);
1338
1339 NAME_CACHE_UNLOCK();
1340 }
1341
1342
1343 static void
1344 cache_enter_locked(struct vnode *dvp, struct vnode *vp, struct componentname *cnp)
1345 {
1346 struct namecache *ncp, *negp;
1347 struct nchashhead *ncpp;
1348
1349 /*
1350 * if the entry is for -ve caching vp is null
1351 */
1352 if ((vp != NULLVP) && (LIST_FIRST(&vp->v_nclinks))) {
1353 /*
1354 * someone beat us to the punch..
1355 * this vnode is already in the cache
1356 */
1357 return;
1358 }
1359 /*
1360 * We allocate a new entry if we are less than the maximum
1361 * allowed and the one at the front of the list is in use.
1362 * Otherwise we use the one at the front of the list.
1363 */
1364 if (numcache < desiredNodes &&
1365 ((ncp = nchead.tqh_first) == NULL ||
1366 ncp->nc_hash.le_prev != 0)) {
1367 /*
1368 * Allocate one more entry
1369 */
1370 ncp = (struct namecache *)_MALLOC_ZONE((u_long)sizeof *ncp, M_CACHE, M_WAITOK);
1371 numcache++;
1372 } else {
1373 /*
1374 * reuse an old entry
1375 */
1376 ncp = TAILQ_FIRST(&nchead);
1377 TAILQ_REMOVE(&nchead, ncp, nc_entry);
1378
1379 if (ncp->nc_hash.le_prev != 0) {
1380 /*
1381 * still in use... we need to
1382 * delete it before re-using it
1383 */
1384 NCHSTAT(ncs_stolen);
1385 cache_delete(ncp, 0);
1386 }
1387 }
1388 NCHSTAT(ncs_enters);
1389
1390 /*
1391 * Fill in cache info, if vp is NULL this is a "negative" cache entry.
1392 */
1393 ncp->nc_vp = vp;
1394 ncp->nc_dvp = dvp;
1395 ncp->nc_hashval = cnp->cn_hash;
1396 ncp->nc_whiteout = FALSE;
1397 ncp->nc_name = add_name_locked(cnp->cn_nameptr, cnp->cn_namelen, cnp->cn_hash, 0);
1398
1399 /*
1400 * make us the newest entry in the cache
1401 * i.e. we'll be the last to be stolen
1402 */
1403 TAILQ_INSERT_TAIL(&nchead, ncp, nc_entry);
1404
1405 ncpp = NCHHASH(dvp, cnp->cn_hash);
1406 #if DIAGNOSTIC
1407 {
1408 struct namecache *p;
1409
1410 for (p = ncpp->lh_first; p != 0; p = p->nc_hash.le_next)
1411 if (p == ncp)
1412 panic("cache_enter: duplicate");
1413 }
1414 #endif
1415 /*
1416 * make us available to be found via lookup
1417 */
1418 LIST_INSERT_HEAD(ncpp, ncp, nc_hash);
1419
1420 if (vp) {
1421 /*
1422 * add to the list of name cache entries
1423 * that point at vp
1424 */
1425 LIST_INSERT_HEAD(&vp->v_nclinks, ncp, nc_un.nc_link);
1426 } else {
1427 /*
1428 * this is a negative cache entry (vp == NULL)
1429 * stick it on the negative cache list
1430 * and record the whiteout state
1431 */
1432 TAILQ_INSERT_TAIL(&neghead, ncp, nc_un.nc_negentry);
1433
1434 if (cnp->cn_flags & ISWHITEOUT)
1435 ncp->nc_whiteout = TRUE;
1436 ncs_negtotal++;
1437
1438 if (ncs_negtotal > desiredNegNodes) {
1439 /*
1440 * if we've reached our desired limit
1441 * of negative cache entries, delete
1442 * the oldest
1443 */
1444 negp = TAILQ_FIRST(&neghead);
1445 TAILQ_REMOVE(&neghead, negp, nc_un.nc_negentry);
1446
1447 cache_delete(negp, 1);
1448 }
1449 }
1450 /*
1451 * add us to the list of name cache entries that
1452 * are children of dvp
1453 */
1454 LIST_INSERT_HEAD(&dvp->v_ncchildren, ncp, nc_child);
1455 }
1456
1457
1458 /*
1459 * Initialize CRC-32 remainder table.
1460 */
1461 static void init_crc32(void)
1462 {
1463 /*
1464 * the CRC-32 generator polynomial is:
1465 * x^32 + x^26 + x^23 + x^22 + x^16 + x^12 + x^10
1466 * + x^8 + x^7 + x^5 + x^4 + x^2 + x + 1
1467 */
1468 unsigned int crc32_polynomial = 0x04c11db7;
1469 unsigned int i,j;
1470
1471 /*
1472 * pre-calculate the CRC-32 remainder for each possible octet encoding
1473 */
1474 for (i = 0; i < 256; i++) {
1475 unsigned int crc_rem = i << 24;
1476
1477 for (j = 0; j < 8; j++) {
1478 if (crc_rem & 0x80000000)
1479 crc_rem = (crc_rem << 1) ^ crc32_polynomial;
1480 else
1481 crc_rem = (crc_rem << 1);
1482 }
1483 crc32tab[i] = crc_rem;
1484 }
1485 }
1486
1487
1488 /*
1489 * Name cache initialization, from vfs_init() when we are booting
1490 */
1491 void
1492 nchinit(void)
1493 {
1494 desiredNegNodes = (desiredvnodes / 10);
1495 desiredNodes = desiredvnodes + desiredNegNodes;
1496
1497 TAILQ_INIT(&nchead);
1498 TAILQ_INIT(&neghead);
1499
1500 init_crc32();
1501
1502 nchashtbl = hashinit(MAX(CONFIG_NC_HASH, (2 *desiredNodes)), M_CACHE, &nchash);
1503 nchashmask = nchash;
1504 nchash++;
1505
1506 init_string_table();
1507
1508 /* Allocate mount list lock group attribute and group */
1509 namecache_lck_grp_attr= lck_grp_attr_alloc_init();
1510
1511 namecache_lck_grp = lck_grp_alloc_init("Name Cache", namecache_lck_grp_attr);
1512
1513 /* Allocate mount list lock attribute */
1514 namecache_lck_attr = lck_attr_alloc_init();
1515
1516 /* Allocate mount list lock */
1517 namecache_rw_lock = lck_rw_alloc_init(namecache_lck_grp, namecache_lck_attr);
1518
1519
1520 }
1521
1522 void
1523 name_cache_lock_shared(void)
1524 {
1525 lck_rw_lock_shared(namecache_rw_lock);
1526 }
1527
1528 void
1529 name_cache_lock(void)
1530 {
1531 lck_rw_lock_exclusive(namecache_rw_lock);
1532 }
1533
1534 void
1535 name_cache_unlock(void)
1536 {
1537 lck_rw_done(namecache_rw_lock);
1538 }
1539
1540
1541 int
1542 resize_namecache(u_int newsize)
1543 {
1544 struct nchashhead *new_table;
1545 struct nchashhead *old_table;
1546 struct nchashhead *old_head, *head;
1547 struct namecache *entry, *next;
1548 uint32_t i, hashval;
1549 int dNodes, dNegNodes;
1550 u_long new_size, old_size;
1551
1552 dNegNodes = (newsize / 10);
1553 dNodes = newsize + dNegNodes;
1554
1555 // we don't support shrinking yet
1556 if (dNodes < desiredNodes) {
1557 return 0;
1558 }
1559 new_table = hashinit(2 * dNodes, M_CACHE, &nchashmask);
1560 new_size = nchashmask + 1;
1561
1562 if (new_table == NULL) {
1563 return ENOMEM;
1564 }
1565
1566 NAME_CACHE_LOCK();
1567 // do the switch!
1568 old_table = nchashtbl;
1569 nchashtbl = new_table;
1570 old_size = nchash;
1571 nchash = new_size;
1572
1573 // walk the old table and insert all the entries into
1574 // the new table
1575 //
1576 for(i=0; i < old_size; i++) {
1577 old_head = &old_table[i];
1578 for (entry=old_head->lh_first; entry != NULL; entry=next) {
1579 //
1580 // XXXdbg - Beware: this assumes that hash_string() does
1581 // the same thing as what happens in
1582 // lookup() over in vfs_lookup.c
1583 hashval = hash_string(entry->nc_name, 0);
1584 entry->nc_hashval = hashval;
1585 head = NCHHASH(entry->nc_dvp, hashval);
1586
1587 next = entry->nc_hash.le_next;
1588 LIST_INSERT_HEAD(head, entry, nc_hash);
1589 }
1590 }
1591 desiredNodes = dNodes;
1592 desiredNegNodes = dNegNodes;
1593
1594 NAME_CACHE_UNLOCK();
1595 FREE(old_table, M_CACHE);
1596
1597 return 0;
1598 }
1599
1600 static void
1601 cache_delete(struct namecache *ncp, int age_entry)
1602 {
1603 NCHSTAT(ncs_deletes);
1604
1605 if (ncp->nc_vp) {
1606 LIST_REMOVE(ncp, nc_un.nc_link);
1607 } else {
1608 TAILQ_REMOVE(&neghead, ncp, nc_un.nc_negentry);
1609 ncs_negtotal--;
1610 }
1611 LIST_REMOVE(ncp, nc_child);
1612
1613 LIST_REMOVE(ncp, nc_hash);
1614 /*
1615 * this field is used to indicate
1616 * that the entry is in use and
1617 * must be deleted before it can
1618 * be reused...
1619 */
1620 ncp->nc_hash.le_prev = NULL;
1621
1622 if (age_entry) {
1623 /*
1624 * make it the next one available
1625 * for cache_enter's use
1626 */
1627 TAILQ_REMOVE(&nchead, ncp, nc_entry);
1628 TAILQ_INSERT_HEAD(&nchead, ncp, nc_entry);
1629 }
1630 remove_name_locked(ncp->nc_name);
1631 ncp->nc_name = NULL;
1632 }
1633
1634
1635 /*
1636 * purge the entry associated with the
1637 * specified vnode from the name cache
1638 */
1639 void
1640 cache_purge(vnode_t vp)
1641 {
1642 struct namecache *ncp;
1643 kauth_cred_t tcred = NULL;
1644
1645 if ((LIST_FIRST(&vp->v_nclinks) == NULL) && (LIST_FIRST(&vp->v_ncchildren) == NULL) && (vp->v_cred == NOCRED))
1646 return;
1647
1648 NAME_CACHE_LOCK();
1649
1650 if (vp->v_parent)
1651 vp->v_parent->v_nc_generation++;
1652
1653 while ( (ncp = LIST_FIRST(&vp->v_nclinks)) )
1654 cache_delete(ncp, 1);
1655
1656 while ( (ncp = LIST_FIRST(&vp->v_ncchildren)) )
1657 cache_delete(ncp, 1);
1658
1659 /*
1660 * Use a temp variable to avoid kauth_cred_unref() while NAME_CACHE_LOCK is held
1661 */
1662 tcred = vp->v_cred;
1663 vp->v_cred = NOCRED;
1664 vp->v_authorized_actions = 0;
1665
1666 NAME_CACHE_UNLOCK();
1667
1668 if (IS_VALID_CRED(tcred))
1669 kauth_cred_unref(&tcred);
1670 }
1671
1672 /*
1673 * Purge all negative cache entries that are children of the
1674 * given vnode. A case-insensitive file system (or any file
1675 * system that has multiple equivalent names for the same
1676 * directory entry) can use this when creating or renaming
1677 * to remove negative entries that may no longer apply.
1678 */
1679 void
1680 cache_purge_negatives(vnode_t vp)
1681 {
1682 struct namecache *ncp;
1683
1684 NAME_CACHE_LOCK();
1685
1686 LIST_FOREACH(ncp, &vp->v_ncchildren, nc_child)
1687 if (ncp->nc_vp == NULL)
1688 cache_delete(ncp , 1);
1689
1690 NAME_CACHE_UNLOCK();
1691 }
1692
1693 /*
1694 * Flush all entries referencing a particular filesystem.
1695 *
1696 * Since we need to check it anyway, we will flush all the invalid
1697 * entries at the same time.
1698 */
1699 void
1700 cache_purgevfs(struct mount *mp)
1701 {
1702 struct nchashhead *ncpp;
1703 struct namecache *ncp;
1704
1705 NAME_CACHE_LOCK();
1706 /* Scan hash tables for applicable entries */
1707 for (ncpp = &nchashtbl[nchash - 1]; ncpp >= nchashtbl; ncpp--) {
1708 restart:
1709 for (ncp = ncpp->lh_first; ncp != 0; ncp = ncp->nc_hash.le_next) {
1710 if (ncp->nc_dvp->v_mount == mp) {
1711 cache_delete(ncp, 0);
1712 goto restart;
1713 }
1714 }
1715 }
1716 NAME_CACHE_UNLOCK();
1717 }
1718
1719
1720
1721 //
1722 // String ref routines
1723 //
1724 static LIST_HEAD(stringhead, string_t) *string_ref_table;
1725 static u_long string_table_mask;
1726 static uint32_t max_chain_len=0;
1727 static struct stringhead *long_chain_head=NULL;
1728 static uint32_t filled_buckets=0;
1729 static uint32_t num_dups=0;
1730 static uint32_t nstrings=0;
1731
1732 typedef struct string_t {
1733 LIST_ENTRY(string_t) hash_chain;
1734 const char *str;
1735 uint32_t refcount;
1736 } string_t;
1737
1738
1739
1740 static int
1741 resize_string_ref_table(void)
1742 {
1743 struct stringhead *new_table;
1744 struct stringhead *old_table;
1745 struct stringhead *old_head, *head;
1746 string_t *entry, *next;
1747 uint32_t i, hashval;
1748 u_long new_mask, old_mask;
1749
1750 new_table = hashinit((string_table_mask + 1) * 2, M_CACHE, &new_mask);
1751 if (new_table == NULL) {
1752 return ENOMEM;
1753 }
1754
1755 // do the switch!
1756 old_table = string_ref_table;
1757 string_ref_table = new_table;
1758 old_mask = string_table_mask;
1759 string_table_mask = new_mask;
1760
1761 printf("resize: max chain len %d, new table size %lu\n",
1762 max_chain_len, new_mask + 1);
1763 max_chain_len = 0;
1764 long_chain_head = NULL;
1765 filled_buckets = 0;
1766
1767 // walk the old table and insert all the entries into
1768 // the new table
1769 //
1770 for(i=0; i <= old_mask; i++) {
1771 old_head = &old_table[i];
1772 for (entry=old_head->lh_first; entry != NULL; entry=next) {
1773 hashval = hash_string((const char *)entry->str, 0);
1774 head = &string_ref_table[hashval & string_table_mask];
1775 if (head->lh_first == NULL) {
1776 filled_buckets++;
1777 }
1778
1779 next = entry->hash_chain.le_next;
1780 LIST_INSERT_HEAD(head, entry, hash_chain);
1781 }
1782 }
1783
1784 FREE(old_table, M_CACHE);
1785
1786 return 0;
1787 }
1788
1789
1790 static void
1791 init_string_table(void)
1792 {
1793 string_ref_table = hashinit(CONFIG_VFS_NAMES, M_CACHE, &string_table_mask);
1794 }
1795
1796
1797 const char *
1798 vfs_addname(const char *name, size_t len, u_int hashval, u_int flags)
1799 {
1800 const char * ptr;
1801
1802 NAME_CACHE_LOCK();
1803 ptr = add_name_locked(name, len, hashval, flags);
1804 NAME_CACHE_UNLOCK();
1805
1806 return(ptr);
1807 }
1808
1809 static const char *
1810 add_name_locked(const char *name, size_t len, u_int hashval, __unused u_int flags)
1811 {
1812 struct stringhead *head;
1813 string_t *entry;
1814 uint32_t chain_len = 0;
1815 char *ptr;
1816
1817 //
1818 // If the table gets more than 3/4 full, resize it
1819 //
1820 if (4*filled_buckets >= ((string_table_mask + 1) * 3)) {
1821 if (resize_string_ref_table() != 0) {
1822 printf("failed to resize the hash table.\n");
1823 }
1824 }
1825 if (hashval == 0) {
1826 hashval = hash_string(name, 0);
1827 }
1828
1829 //
1830 // if the length already accounts for the null-byte, then
1831 // subtract one so later on we don't index past the end
1832 // of the string.
1833 //
1834 if (len > 0 && name[len-1] == '\0') {
1835 len--;
1836 }
1837
1838 head = &string_ref_table[hashval & string_table_mask];
1839 for (entry=head->lh_first; entry != NULL; chain_len++, entry=entry->hash_chain.le_next) {
1840 if (memcmp(entry->str, name, len) == 0 && entry->str[len] == '\0') {
1841 entry->refcount++;
1842 num_dups++;
1843 break;
1844 }
1845 }
1846
1847 if (entry == NULL) {
1848 // it wasn't already there so add it.
1849 MALLOC(entry, string_t *, sizeof(string_t) + len + 1, M_TEMP, M_WAITOK);
1850
1851 // have to get "head" again because we could have blocked
1852 // in malloc and thus head could have changed.
1853 //
1854 head = &string_ref_table[hashval & string_table_mask];
1855 if (head->lh_first == NULL) {
1856 filled_buckets++;
1857 }
1858
1859 ptr = (char *)((char *)entry + sizeof(string_t));
1860 strncpy(ptr, name, len);
1861 ptr[len] = '\0';
1862 entry->str = ptr;
1863 entry->refcount = 1;
1864 LIST_INSERT_HEAD(head, entry, hash_chain);
1865
1866 if (chain_len > max_chain_len) {
1867 max_chain_len = chain_len;
1868 long_chain_head = head;
1869 }
1870
1871 nstrings++;
1872 }
1873
1874 return (const char *)entry->str;
1875 }
1876
1877 int
1878 vfs_removename(const char *nameref)
1879 {
1880 int i;
1881
1882 NAME_CACHE_LOCK();
1883 i = remove_name_locked(nameref);
1884 NAME_CACHE_UNLOCK();
1885
1886 return(i);
1887
1888 }
1889
1890
1891 static int
1892 remove_name_locked(const char *nameref)
1893 {
1894 struct stringhead *head;
1895 string_t *entry;
1896 uint32_t hashval;
1897 const char *ptr;
1898
1899 hashval = hash_string(nameref, 0);
1900 head = &string_ref_table[hashval & string_table_mask];
1901 for (entry=head->lh_first; entry != NULL; entry=entry->hash_chain.le_next) {
1902 if (entry->str == nameref) {
1903 entry->refcount--;
1904 if (entry->refcount == 0) {
1905 LIST_REMOVE(entry, hash_chain);
1906 if (head->lh_first == NULL) {
1907 filled_buckets--;
1908 }
1909 ptr = entry->str;
1910 entry->str = NULL;
1911 nstrings--;
1912
1913 FREE(entry, M_TEMP);
1914 } else {
1915 num_dups--;
1916 }
1917
1918 return 0;
1919 }
1920 }
1921
1922 return ENOENT;
1923 }
1924
1925
1926 #ifdef DUMP_STRING_TABLE
1927 void
1928 dump_string_table(void)
1929 {
1930 struct stringhead *head;
1931 string_t *entry;
1932 u_long i;
1933
1934 NAME_CACHE_LOCK_SHARED();
1935
1936 for (i = 0; i <= string_table_mask; i++) {
1937 head = &string_ref_table[i];
1938 for (entry=head->lh_first; entry != NULL; entry=entry->hash_chain.le_next) {
1939 printf("%6d - %s\n", entry->refcount, entry->str);
1940 }
1941 }
1942 NAME_CACHE_UNLOCK();
1943 }
1944 #endif /* DUMP_STRING_TABLE */