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