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