2 * Copyright (c) 2003,2004 The DragonFly Project. All rights reserved.
4 * This code is derived from software contributed to The DragonFly Project
5 * by Matthew Dillon <dillon@backplane.com>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
17 * 3. Neither the name of The DragonFly Project nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific, prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
27 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
29 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
31 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * Copyright (c) 1989, 1993, 1995
35 * The Regents of the University of California. All rights reserved.
37 * This code is derived from software contributed to Berkeley by
38 * Poul-Henning Kamp of the FreeBSD Project.
40 * Redistribution and use in source and binary forms, with or without
41 * modification, are permitted provided that the following conditions
43 * 1. Redistributions of source code must retain the above copyright
44 * notice, this list of conditions and the following disclaimer.
45 * 2. Redistributions in binary form must reproduce the above copyright
46 * notice, this list of conditions and the following disclaimer in the
47 * documentation and/or other materials provided with the distribution.
48 * 3. All advertising materials mentioning features or use of this software
49 * must display the following acknowledgement:
50 * This product includes software developed by the University of
51 * California, Berkeley and its contributors.
52 * 4. Neither the name of the University nor the names of its contributors
53 * may be used to endorse or promote products derived from this software
54 * without specific prior written permission.
56 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
57 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
58 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
59 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
60 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
61 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
62 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
63 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
64 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
65 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
68 * @(#)vfs_cache.c 8.5 (Berkeley) 3/22/95
69 * $FreeBSD: src/sys/kern/vfs_cache.c,v 1.42.2.6 2001/10/05 20:07:03 dillon Exp $
70 * $DragonFly: src/sys/kern/vfs_cache.c,v 1.25 2004/07/16 05:51:10 dillon Exp $
73 #include <sys/param.h>
74 #include <sys/systm.h>
75 #include <sys/kernel.h>
76 #include <sys/sysctl.h>
77 #include <sys/mount.h>
78 #include <sys/vnode.h>
79 #include <sys/malloc.h>
80 #include <sys/sysproto.h>
82 #include <sys/namei.h>
83 #include <sys/filedesc.h>
84 #include <sys/fnv_hash.h>
85 #include <sys/globaldata.h>
86 #include <sys/kern_syscall.h>
89 * Random lookups in the cache are accomplished with a hash table using
90 * a hash key of (nc_src_vp, name).
92 * Negative entries may exist and correspond to structures where nc_vp
93 * is NULL. In a negative entry, NCF_WHITEOUT will be set if the entry
94 * corresponds to a whited-out directory entry (verses simply not finding the
97 * Upon reaching the last segment of a path, if the reference is for DELETE,
98 * or NOCACHE is set (rewrite), and the name is located in the cache, it
103 * Structures associated with name cacheing.
105 #define NCHHASH(hash) (&nchashtbl[(hash) & nchash])
108 MALLOC_DEFINE(M_VFSCACHE, "vfscache", "VFS name cache entries");
110 static LIST_HEAD(nchashhead, namecache) *nchashtbl; /* Hash Table */
111 static struct namecache_list ncneglist; /* instead of vnode */
112 static struct namecache rootnamecache; /* Dummy node */
114 static int nczapcheck; /* panic on bad release */
115 SYSCTL_INT(_debug, OID_AUTO, nczapcheck, CTLFLAG_RW, &nczapcheck, 0, "");
117 static u_long nchash; /* size of hash table */
118 SYSCTL_ULONG(_debug, OID_AUTO, nchash, CTLFLAG_RD, &nchash, 0, "");
120 static u_long ncnegfactor = 16; /* ratio of negative entries */
121 SYSCTL_ULONG(_debug, OID_AUTO, ncnegfactor, CTLFLAG_RW, &ncnegfactor, 0, "");
123 static u_long numneg; /* number of cache entries allocated */
124 SYSCTL_ULONG(_debug, OID_AUTO, numneg, CTLFLAG_RD, &numneg, 0, "");
126 static u_long numcache; /* number of cache entries allocated */
127 SYSCTL_ULONG(_debug, OID_AUTO, numcache, CTLFLAG_RD, &numcache, 0, "");
129 static u_long numunres; /* number of unresolved entries */
130 SYSCTL_ULONG(_debug, OID_AUTO, numunres, CTLFLAG_RD, &numunres, 0, "");
132 SYSCTL_INT(_debug, OID_AUTO, vnsize, CTLFLAG_RD, 0, sizeof(struct vnode), "");
133 SYSCTL_INT(_debug, OID_AUTO, ncsize, CTLFLAG_RD, 0, sizeof(struct namecache), "");
136 * The new name cache statistics
138 SYSCTL_NODE(_vfs, OID_AUTO, cache, CTLFLAG_RW, 0, "Name cache statistics");
139 #define STATNODE(mode, name, var) \
140 SYSCTL_ULONG(_vfs_cache, OID_AUTO, name, mode, var, 0, "");
141 STATNODE(CTLFLAG_RD, numneg, &numneg);
142 STATNODE(CTLFLAG_RD, numcache, &numcache);
143 static u_long numcalls; STATNODE(CTLFLAG_RD, numcalls, &numcalls);
144 static u_long dothits; STATNODE(CTLFLAG_RD, dothits, &dothits);
145 static u_long dotdothits; STATNODE(CTLFLAG_RD, dotdothits, &dotdothits);
146 static u_long numchecks; STATNODE(CTLFLAG_RD, numchecks, &numchecks);
147 static u_long nummiss; STATNODE(CTLFLAG_RD, nummiss, &nummiss);
148 static u_long nummisszap; STATNODE(CTLFLAG_RD, nummisszap, &nummisszap);
149 static u_long numposzaps; STATNODE(CTLFLAG_RD, numposzaps, &numposzaps);
150 static u_long numposhits; STATNODE(CTLFLAG_RD, numposhits, &numposhits);
151 static u_long numnegzaps; STATNODE(CTLFLAG_RD, numnegzaps, &numnegzaps);
152 static u_long numneghits; STATNODE(CTLFLAG_RD, numneghits, &numneghits);
154 struct nchstats nchstats[SMP_MAXCPU];
156 * Export VFS cache effectiveness statistics to user-land.
158 * The statistics are left for aggregation to user-land so
159 * neat things can be achieved, like observing per-CPU cache
163 sysctl_nchstats(SYSCTL_HANDLER_ARGS)
165 struct globaldata *gd;
169 for (i = 0; i < ncpus; ++i) {
170 gd = globaldata_find(i);
171 if ((error = SYSCTL_OUT(req, (void *)&(*gd->gd_nchstats),
172 sizeof(struct nchstats))))
178 SYSCTL_PROC(_vfs_cache, OID_AUTO, nchstats, CTLTYPE_OPAQUE|CTLFLAG_RD,
179 0, 0, sysctl_nchstats, "S,nchstats", "VFS cache effectiveness statistics");
181 static void cache_zap(struct namecache *ncp);
184 * cache_hold() and cache_drop() prevent the premature deletion of a
185 * namecache entry but do not prevent operations (such as zapping) on
186 * that namecache entry.
190 _cache_hold(struct namecache *ncp)
198 _cache_drop(struct namecache *ncp)
200 KKASSERT(ncp->nc_refs > 0);
201 if (ncp->nc_refs == 1 &&
202 (ncp->nc_flag & NCF_UNRESOLVED) &&
203 TAILQ_EMPTY(&ncp->nc_list)
212 cache_hold(struct namecache *ncp)
214 return(_cache_hold(ncp));
218 cache_drop(struct namecache *ncp)
224 cache_link_parent(struct namecache *ncp, struct namecache *par)
226 KKASSERT(ncp->nc_parent == NULL);
227 ncp->nc_parent = par;
228 if (TAILQ_EMPTY(&par->nc_list)) {
232 TAILQ_INSERT_HEAD(&par->nc_list, ncp, nc_entry);
236 cache_unlink_parent(struct namecache *ncp)
238 struct namecache *par;
240 if ((par = ncp->nc_parent) != NULL) {
241 ncp->nc_parent = NULL;
242 par = cache_hold(par);
243 TAILQ_REMOVE(&par->nc_list, ncp, nc_entry);
244 if (par->nc_vp && TAILQ_EMPTY(&par->nc_list))
250 static struct namecache *
251 cache_alloc(struct vnode *vp)
253 struct namecache *ncp;
255 ncp = malloc(sizeof(*ncp), M_VFSCACHE, M_WAITOK|M_ZERO);
256 TAILQ_INIT(&ncp->nc_list);
259 TAILQ_INSERT_HEAD(&vp->v_namecache, ncp, nc_vnode);
262 TAILQ_INSERT_TAIL(&ncneglist, ncp, nc_vnode);
269 * Try to destroy a namecache entry. The entry is disassociated from its
270 * vnode or ncneglist and reverted to an UNRESOLVED state.
272 * Then, if there are no additional references to the ncp and we can
273 * successfully delete the children, the entry is also removed from the
274 * namecache hashlist / topology.
276 * References or undeletable children will prevent the entry from being
277 * removed from the topology. The entry may be revalidated (typically
278 * by cache_enter()) at a later time. Children remain because:
280 * + we have tried to delete a node rather then a leaf in the topology.
281 * + the presence of negative entries (we try to scrap these).
282 * + an entry or child has a non-zero ref count and cannot be scrapped.
284 * This function must be called with the ncp held and will drop the ref
285 * count during zapping.
288 cache_zap(struct namecache *ncp)
290 struct namecache *par;
294 * Disassociate the vnode or negative cache ref and set NCF_UNRESOLVED.
296 if ((ncp->nc_flag & NCF_UNRESOLVED) == 0) {
297 ncp->nc_flag |= NCF_UNRESOLVED;
299 if ((vp = ncp->nc_vp) != NULL) {
301 ncp->nc_vp = NULL; /* safety */
302 TAILQ_REMOVE(&vp->v_namecache, ncp, nc_vnode);
303 if (!TAILQ_EMPTY(&ncp->nc_list))
306 TAILQ_REMOVE(&ncneglist, ncp, nc_vnode);
312 * Try to scrap the entry and possibly tail-recurse on its parent.
313 * We only scrap unref'd (other then our ref) unresolved entries,
314 * we do not scrap 'live' entries.
316 while (ncp->nc_flag & NCF_UNRESOLVED) {
318 * Someone other then us has a ref, stop.
320 if (ncp->nc_refs > 1)
324 * We have children, stop.
326 if (!TAILQ_EMPTY(&ncp->nc_list))
330 * Ok, we can completely destroy and free this entry. Sanity
331 * check it against our static rootnamecache structure,
332 * then remove it from the hash.
334 KKASSERT(ncp != &rootnamecache);
336 if (ncp->nc_flag & NCF_HASHED) {
337 ncp->nc_flag &= ~NCF_HASHED;
338 LIST_REMOVE(ncp, nc_hash);
342 * Unlink from its parent and free, then loop on the
343 * parent. XXX temp hack, in stage-3 parent is never NULL
345 if ((par = ncp->nc_parent) != NULL) {
346 par = cache_hold(par);
347 TAILQ_REMOVE(&par->nc_list, ncp, nc_entry);
348 if (par->nc_vp && TAILQ_EMPTY(&par->nc_list))
352 ncp->nc_refs = -1; /* safety */
353 ncp->nc_parent = NULL; /* safety */
355 free(ncp->nc_name, M_VFSCACHE);
356 free(ncp, M_VFSCACHE);
358 if (par == NULL) /* temp hack */
359 return; /* temp hack */
366 * Lookup an entry in the cache
368 * Lookup is called with dvp pointing to the directory to search,
369 * cnp pointing to the name of the entry being sought.
371 * If the lookup succeeds, the vnode is returned in *vpp, and a
372 * status of -1 is returned.
374 * If the lookup determines that the name does not exist (negative cacheing),
375 * a status of ENOENT is returned.
377 * If the lookup fails, a status of zero is returned.
379 * Note that UNRESOLVED entries are ignored. They are not negative cache
383 cache_lookup(struct vnode *dvp, struct namecache *par, struct vnode **vpp,
384 struct namecache **ncpp, struct componentname *cnp)
386 struct namecache *ncp;
388 globaldata_t gd = mycpu;
393 * Obtain the namecache entry associated with dvp, creating one if
394 * necessary. If we have to create one we have insufficient
395 * information to hash it or even supply the name, but we still
396 * need one so we can link it in.
398 * NOTE: in this stage of development, the passed 'par' is
399 * almost always NULL.
402 if ((par = TAILQ_FIRST(&dvp->v_namecache)) == NULL)
403 par = cache_alloc(dvp);
407 * Deal with "." and "..". In this stage of code development we leave
408 * the returned ncpp NULL. Note that if the namecache is disjoint,
409 * we won't find a vnode for "..".
411 if (cnp->cn_nameptr[0] == '.') {
412 if (cnp->cn_namelen == 1) {
415 numposhits++; /* include in total statistics */
418 if (cnp->cn_namelen == 2 && cnp->cn_nameptr[1] == '.') {
420 numposhits++; /* include in total statistics */
421 if ((cnp->cn_flags & CNP_MAKEENTRY) == 0)
423 if (par->nc_parent == NULL ||
424 par->nc_parent->nc_vp == NULL) {
427 *vpp = par->nc_parent->nc_vp;
433 * Try to locate an existing entry
435 hash = fnv_32_buf(cnp->cn_nameptr, cnp->cn_namelen, FNV1_32_INIT);
436 hash = fnv_32_buf(&par, sizeof(par), hash);
438 printf("DVP %p/%p %08x %*.*s\n", dvp, par, hash, (int)cnp->cn_namelen, (int)cnp->cn_namelen, cnp->cn_nameptr);
440 LIST_FOREACH(ncp, (NCHHASH(hash)), nc_hash) {
442 if (nczapcheck > 1) {
443 printf("TEST ncp par=%p %*.*s\n",
444 ncp->nc_parent, ncp->nc_nlen, ncp->nc_nlen,
449 * Zap entries that have timed out.
451 if (ncp->nc_timeout &&
452 (int)(ncp->nc_timeout - ticks) < 0
456 cache_zap(cache_hold(ncp));
461 * Break out if we find a matching entry. UNRESOLVED entries
462 * never match (they are in the middle of being destroyed).
464 if ((ncp->nc_flag & NCF_UNRESOLVED) == 0 &&
465 ncp->nc_parent == par &&
466 ncp->nc_nlen == cnp->cn_namelen &&
467 bcmp(ncp->nc_name, cnp->cn_nameptr, ncp->nc_nlen) == 0
477 * If we failed to locate an entry, return 0 (indicates failure).
480 if ((cnp->cn_flags & CNP_MAKEENTRY) == 0) {
485 gd->gd_nchstats->ncs_miss++;
487 printf("MISS %p/%p %*.*s/%*.*s\n", dvp, par,
488 par->nc_nlen, par->nc_nlen, (par->nc_name ? par->nc_name : ""),
489 (int)cnp->cn_namelen, (int)cnp->cn_namelen, cnp->cn_nameptr);
495 * If we found an entry, but we don't want to have one, we zap it.
497 if ((cnp->cn_flags & CNP_MAKEENTRY) == 0) {
499 gd->gd_nchstats->ncs_badhits++;
505 * If the vnode is not NULL then return the positive match.
509 gd->gd_nchstats->ncs_goodhits++;
516 * If the vnode is NULL we found a negative match. If we want to
517 * create it, purge the negative match and return failure (as if
518 * we hadn't found a match in the first place).
520 if (cnp->cn_nameiop == NAMEI_CREATE) {
522 gd->gd_nchstats->ncs_badhits++;
530 * We found a "negative" match, ENOENT notifies client of this match.
531 * The nc_flag field records whether this is a whiteout. Since there
532 * is no vnode we can use the vnode tailq link field with ncneglist.
534 TAILQ_REMOVE(&ncneglist, ncp, nc_vnode);
535 TAILQ_INSERT_TAIL(&ncneglist, ncp, nc_vnode);
536 gd->gd_nchstats->ncs_neghits++;
537 if (ncp->nc_flag & NCF_WHITEOUT)
538 cnp->cn_flags |= CNP_ISWHITEOUT;
544 * Generate a special linkage between the mount point and the root of the
545 * mounted filesystem in order to maintain the namecache topology across
546 * a mount point. The special linkage has a 0-length name component
547 * and sets NCF_MOUNTPT.
550 cache_mount(struct vnode *dvp, struct vnode *tvp)
552 struct namecache *ncp;
553 struct namecache *par;
554 struct nchashhead *nchpp;
558 * If a linkage already exists we do not have to do anything.
560 hash = fnv_32_buf("", 0, FNV1_32_INIT);
561 hash = fnv_32_buf(&dvp->v_id, sizeof(dvp->v_id), hash);
562 LIST_FOREACH(ncp, (NCHHASH(hash)), nc_hash) {
564 if (ncp->nc_vp == tvp &&
567 ncp->nc_parent->nc_vp == dvp
573 if ((par = TAILQ_FIRST(&dvp->v_namecache)) == NULL)
574 par = cache_alloc(dvp);
577 * Otherwise create a new linkage.
579 ncp = cache_alloc(tvp);
580 ncp->nc_flag = NCF_MOUNTPT;
581 cache_link_parent(ncp, par);
586 hash = fnv_32_buf("", 0, FNV1_32_INIT);
587 hash = fnv_32_buf(&dvp->v_id, sizeof(dvp->v_id), hash);
588 nchpp = NCHHASH(hash);
589 LIST_INSERT_HEAD(nchpp, ncp, nc_hash);
591 ncp->nc_flag |= NCF_HASHED;
595 * Add an entry to the cache.
598 cache_enter(struct vnode *dvp, struct namecache *par, struct vnode *vp, struct componentname *cnp)
600 struct namecache *ncp;
601 struct namecache *bpar;
602 struct nchashhead *nchpp;
607 * If the directory has no namecache entry we must associate one with
608 * it. The name of the entry is not known so it isn't hashed.
611 if ((par = TAILQ_FIRST(&dvp->v_namecache)) == NULL)
612 par = cache_alloc(dvp);
616 * This may be a bit confusing. "." and ".." are 'virtual' entries.
617 * We do not actually create a namecache entry representing either.
618 * However, the ".." case is used to linkup a potentially disjoint
619 * directory with its parent, to disconnect a directory from its
620 * parent, or to change an existing linkage that may no longer be
621 * correct (as might occur when a subdirectory is renamed).
624 if (cnp->cn_namelen == 1 && cnp->cn_nameptr[0] == '.')
626 if (cnp->cn_namelen == 2 && cnp->cn_nameptr[0] == '.' &&
627 cnp->cn_nameptr[1] == '.'
631 cache_unlink_parent(par);
633 if ((ncp = TAILQ_FIRST(&vp->v_namecache)) == NULL)
634 ncp = cache_alloc(vp);
637 cache_unlink_parent(par);
638 cache_link_parent(par, ncp); /* ncp is parent of par */
645 * Locate other entries associated with this vnode and zap them,
646 * because the purge code may not be able to find them due to
647 * the topology not yet being consistent. This is a temporary
652 TAILQ_FOREACH(ncp, &vp->v_namecache, nc_vnode) {
653 if ((ncp->nc_flag & NCF_UNRESOLVED) == 0) {
654 cache_zap(cache_hold(ncp));
660 hash = fnv_32_buf(cnp->cn_nameptr, cnp->cn_namelen, FNV1_32_INIT);
662 hash = fnv_32_buf(&bpar, sizeof(bpar), hash);
665 printf("ENTER %p/%p %08x '%*.*s' %p ", dvp, par, hash, (int)cnp->cn_namelen, (int)cnp->cn_namelen, cnp->cn_nameptr, vp);
668 name = malloc(cnp->cn_namelen, M_VFSCACHE, M_WAITOK);
669 ncp = cache_alloc(vp);
676 if (cnp->cn_flags & CNP_CACHETIMEOUT) {
677 if ((ncp->nc_timeout = ticks + cnp->cn_timeout) == 0)
682 * Linkup the parent pointer, bump the parent vnode's hold
683 * count when we go from 0->1 children.
685 cache_link_parent(ncp, par);
688 * Add to the hash table
691 ncp->nc_nlen = cnp->cn_namelen;
692 bcopy(cnp->cn_nameptr, ncp->nc_name, cnp->cn_namelen);
693 nchpp = NCHHASH(hash);
694 LIST_INSERT_HEAD(nchpp, ncp, nc_hash);
695 ncp->nc_flag |= NCF_HASHED;
698 * If the target vnode is NULL if this is to be a negative cache
702 ncp->nc_flag &= ~NCF_WHITEOUT;
703 if (cnp->cn_flags & CNP_ISWHITEOUT)
704 ncp->nc_flag |= NCF_WHITEOUT;
708 * Don't cache too many negative hits
710 if (numneg > MINNEG && numneg * ncnegfactor > numcache) {
711 ncp = TAILQ_FIRST(&ncneglist);
712 KKASSERT(ncp != NULL);
713 cache_zap(cache_hold(ncp));
718 * Name cache initialization, from vfsinit() when we are booting
720 * rootnamecache is initialized such that it cannot be recursively deleted.
728 /* initialise per-cpu namecache effectiveness statistics. */
729 for (i = 0; i < ncpus; ++i) {
730 gd = globaldata_find(i);
731 gd->gd_nchstats = &nchstats[i];
734 TAILQ_INIT(&ncneglist);
735 nchashtbl = hashinit(desiredvnodes*2, M_VFSCACHE, &nchash);
736 TAILQ_INIT(&rootnamecache.nc_list);
737 rootnamecache.nc_flag |= NCF_HASHED | NCF_ROOT | NCF_UNRESOLVED;
738 rootnamecache.nc_refs = 1;
742 * vfs_cache_setroot()
744 * Create an association between the root of our namecache and
745 * the root vnode. This routine may be called several times during
749 vfs_cache_setroot(struct vnode *nvp)
751 KKASSERT(rootnamecache.nc_refs > 0); /* don't accidently free */
752 cache_zap(cache_hold(&rootnamecache));
754 rootnamecache.nc_vp = nvp;
755 rootnamecache.nc_flag &= ~NCF_UNRESOLVED;
758 if (!TAILQ_EMPTY(&rootnamecache.nc_list))
760 TAILQ_INSERT_HEAD(&nvp->v_namecache, &rootnamecache, nc_vnode);
763 TAILQ_INSERT_TAIL(&ncneglist, &rootnamecache, nc_vnode);
764 rootnamecache.nc_flag &= ~NCF_WHITEOUT;
769 * Invalidate all namecache entries to a particular vnode as well as
770 * any direct children of that vnode in the namecache. This is a
771 * 'catch all' purge used by filesystems that do not know any better.
773 * A new vnode v_id is generated. Note that no vnode will ever have a
776 * Note that the linkage between the vnode and its namecache entries will
777 * be removed, but the namecache entries themselves might stay put due to
778 * active references from elsewhere in the system or due to the existance of
779 * the children. The namecache topology is left intact even if we do not
780 * know what the vnode association is. Such entries will be marked
783 * XXX: Only time and the size of v_id prevents this from failing:
784 * XXX: In theory we should hunt down all (struct vnode*, v_id)
785 * XXX: soft references and nuke them, at least on the global
786 * XXX: v_id wraparound. The period of resistance can be extended
787 * XXX: by incrementing each vnodes v_id individually instead of
788 * XXX: using the global v_id.
791 cache_purge(struct vnode *vp)
793 static u_long nextid;
794 struct namecache *ncp;
795 struct namecache *scan;
798 * Disassociate the vnode from its namecache entries along with
799 * (for historical reasons) any direct children.
801 while ((ncp = TAILQ_FIRST(&vp->v_namecache)) != NULL) {
804 restart: /* YYY hack, fix me */
805 TAILQ_FOREACH(scan, &ncp->nc_list, nc_entry) {
806 if ((scan->nc_flag & NCF_UNRESOLVED) == 0) {
807 cache_zap(cache_hold(scan));
815 * Calculate a new unique id for ".." handling
819 } while (nextid == vp->v_id || nextid == 0);
824 * Flush all entries referencing a particular filesystem.
826 * Since we need to check it anyway, we will flush all the invalid
827 * entries at the same time.
830 cache_purgevfs(struct mount *mp)
832 struct nchashhead *nchpp;
833 struct namecache *ncp, *nnp;
836 * Scan hash tables for applicable entries.
838 for (nchpp = &nchashtbl[nchash]; nchpp >= nchashtbl; nchpp--) {
839 ncp = LIST_FIRST(nchpp);
843 nnp = LIST_NEXT(ncp, nc_hash);
846 if (ncp->nc_vp && ncp->nc_vp->v_mount == mp)
858 * Test whether the vnode is at a leaf in the nameicache tree.
860 * Returns 0 if it is a leaf, -1 if it isn't.
863 cache_leaf_test(struct vnode *vp)
865 struct namecache *scan;
866 struct namecache *ncp;
868 TAILQ_FOREACH(scan, &vp->v_namecache, nc_vnode) {
869 TAILQ_FOREACH(ncp, &scan->nc_list, nc_entry) {
870 /* YYY && ncp->nc_vp->v_type == VDIR ? */
871 if (ncp->nc_vp != NULL)
879 * Perform canonical checks and cache lookup and pass on to filesystem
880 * through the vop_cachedlookup only if needed.
883 * struct vnode a_dvp;
884 * struct namecache *a_ncp;
885 * struct vnode **a_vpp;
886 * struct namecache **a_ncpp;
887 * struct componentname *a_cnp;
891 vfs_cache_lookup(struct vop_lookup_args *ap)
893 struct vnode *dvp, *vp;
896 struct namecache *par = ap->a_par;
897 struct vnode **vpp = ap->a_vpp;
898 struct namecache **ncpp = ap->a_ncpp;
899 struct componentname *cnp = ap->a_cnp;
900 struct ucred *cred = cnp->cn_cred;
901 int flags = cnp->cn_flags;
902 struct thread *td = cnp->cn_td;
903 u_long vpid; /* capability number of vnode */
909 lockparent = flags & CNP_LOCKPARENT;
911 if (dvp->v_type != VDIR)
914 if ((flags & CNP_ISLASTCN) && (dvp->v_mount->mnt_flag & MNT_RDONLY) &&
915 (cnp->cn_nameiop == NAMEI_DELETE || cnp->cn_nameiop == NAMEI_RENAME)) {
919 error = VOP_ACCESS(dvp, VEXEC, cred, td);
924 error = cache_lookup(dvp, par, vpp, ncpp, cnp);
927 return (VOP_CACHEDLOOKUP(dvp, par, vpp, ncpp, cnp));
934 cnp->cn_flags &= ~CNP_PDIRUNLOCK;
935 if (dvp == vp) { /* lookup on "." */
938 } else if (flags & CNP_ISDOTDOT) {
939 VOP_UNLOCK(dvp, NULL, 0, td);
940 cnp->cn_flags |= CNP_PDIRUNLOCK;
941 error = vget(vp, NULL, LK_EXCLUSIVE, td);
942 if (!error && lockparent && (flags & CNP_ISLASTCN)) {
943 if ((error = vn_lock(dvp, NULL, LK_EXCLUSIVE, td)) == 0)
944 cnp->cn_flags &= ~CNP_PDIRUNLOCK;
947 error = vget(vp, NULL, LK_EXCLUSIVE, td);
948 if (!lockparent || error || !(flags & CNP_ISLASTCN)) {
949 VOP_UNLOCK(dvp, NULL, 0, td);
950 cnp->cn_flags |= CNP_PDIRUNLOCK;
954 * Check that the capability number did not change
955 * while we were waiting for the lock.
958 if (vpid == vp->v_id)
961 if (lockparent && dvp != vp && (flags & CNP_ISLASTCN)) {
962 VOP_UNLOCK(dvp, NULL, 0, td);
963 cnp->cn_flags |= CNP_PDIRUNLOCK;
966 if (cnp->cn_flags & CNP_PDIRUNLOCK) {
967 error = vn_lock(dvp, NULL, LK_EXCLUSIVE, td);
970 cnp->cn_flags &= ~CNP_PDIRUNLOCK;
972 return (VOP_CACHEDLOOKUP(dvp, par, vpp, ncpp, cnp));
975 static int disablecwd;
976 SYSCTL_INT(_debug, OID_AUTO, disablecwd, CTLFLAG_RW, &disablecwd, 0, "");
978 static u_long numcwdcalls; STATNODE(CTLFLAG_RD, numcwdcalls, &numcwdcalls);
979 static u_long numcwdfail1; STATNODE(CTLFLAG_RD, numcwdfail1, &numcwdfail1);
980 static u_long numcwdfail2; STATNODE(CTLFLAG_RD, numcwdfail2, &numcwdfail2);
981 static u_long numcwdfail3; STATNODE(CTLFLAG_RD, numcwdfail3, &numcwdfail3);
982 static u_long numcwdfail4; STATNODE(CTLFLAG_RD, numcwdfail4, &numcwdfail4);
983 static u_long numcwdfound; STATNODE(CTLFLAG_RD, numcwdfound, &numcwdfound);
986 __getcwd(struct __getcwd_args *uap)
996 buflen = uap->buflen;
999 if (buflen > MAXPATHLEN)
1000 buflen = MAXPATHLEN;
1002 buf = malloc(buflen, M_TEMP, M_WAITOK);
1003 bp = kern_getcwd(buf, buflen, &error);
1005 error = copyout(bp, uap->buf, strlen(bp) + 1);
1011 kern_getcwd(char *buf, size_t buflen, int *error)
1013 struct proc *p = curproc;
1015 int i, slash_prefixed;
1016 struct filedesc *fdp;
1017 struct namecache *ncp;
1026 for (vp = fdp->fd_cdir; vp != fdp->fd_rdir && vp != rootvnode;) {
1027 if (vp->v_flag & VROOT) {
1028 if (vp->v_mount == NULL) { /* forced unmount */
1032 vp = vp->v_mount->mnt_vnodecovered;
1035 TAILQ_FOREACH(ncp, &vp->v_namecache, nc_vnode) {
1036 if (ncp->nc_parent && ncp->nc_parent->nc_vp &&
1046 for (i = ncp->nc_nlen - 1; i >= 0; i--) {
1052 *--bp = ncp->nc_name[i];
1061 vp = ncp->nc_parent->nc_vp;
1063 if (!slash_prefixed) {
1077 * Thus begins the fullpath magic.
1081 #define STATNODE(name) \
1082 static u_int name; \
1083 SYSCTL_UINT(_vfs_cache, OID_AUTO, name, CTLFLAG_RD, &name, 0, "")
1085 static int disablefullpath;
1086 SYSCTL_INT(_debug, OID_AUTO, disablefullpath, CTLFLAG_RW,
1087 &disablefullpath, 0, "");
1089 STATNODE(numfullpathcalls);
1090 STATNODE(numfullpathfail1);
1091 STATNODE(numfullpathfail2);
1092 STATNODE(numfullpathfail3);
1093 STATNODE(numfullpathfail4);
1094 STATNODE(numfullpathfound);
1097 vn_fullpath(struct proc *p, struct vnode *vn, char **retbuf, char **freebuf)
1100 int i, slash_prefixed;
1101 struct filedesc *fdp;
1102 struct namecache *ncp;
1106 if (disablefullpath)
1112 /* vn is NULL, client wants us to use p->p_textvp */
1114 if ((vn = p->p_textvp) == NULL)
1118 buf = malloc(MAXPATHLEN, M_TEMP, M_WAITOK);
1119 bp = buf + MAXPATHLEN - 1;
1123 for (vp = vn; vp != fdp->fd_rdir && vp != rootvnode;) {
1124 if (vp->v_flag & VROOT) {
1125 if (vp->v_mount == NULL) { /* forced unmount */
1129 vp = vp->v_mount->mnt_vnodecovered;
1132 TAILQ_FOREACH(ncp, &vp->v_namecache, nc_vnode) {
1133 if (ncp->nc_parent && ncp->nc_parent->nc_vp &&
1143 for (i = ncp->nc_nlen - 1; i >= 0; i--) {
1149 *--bp = ncp->nc_name[i];
1158 vp = ncp->nc_parent->nc_vp;
1160 if (!slash_prefixed) {