2 * Copyright (c) 1989, 1993, 1995
3 * The Regents of the University of California. All rights reserved.
4 * Copyright (c) 2003 Matthew Dillon <dillon@backplane.com>,
7 * This code is derived from software contributed to Berkeley by
8 * Poul-Henning Kamp of the FreeBSD Project.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the University of
21 * California, Berkeley and its contributors.
22 * 4. Neither the name of the University nor the names of its contributors
23 * may be used to endorse or promote products derived from this software
24 * without specific prior written permission.
26 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38 * @(#)vfs_cache.c 8.5 (Berkeley) 3/22/95
39 * $FreeBSD: src/sys/kern/vfs_cache.c,v 1.42.2.6 2001/10/05 20:07:03 dillon Exp $
40 * $DragonFly: src/sys/kern/vfs_cache.c,v 1.15 2004/04/08 17:56:48 dillon Exp $
43 #include <sys/param.h>
44 #include <sys/systm.h>
45 #include <sys/kernel.h>
46 #include <sys/sysctl.h>
47 #include <sys/mount.h>
48 #include <sys/vnode.h>
49 #include <sys/malloc.h>
50 #include <sys/sysproto.h>
52 #include <sys/namei.h>
53 #include <sys/filedesc.h>
54 #include <sys/fnv_hash.h>
55 #include <sys/globaldata.h>
58 * Random lookups in the cache are accomplished with a hash table using
59 * a hash key of (nc_src_vp, name).
61 * Negative entries may exist and correspond to structures where nc_vp
62 * is NULL. In a negative entry, NCF_WHITEOUT will be set if the entry
63 * corresponds to a whited-out directory entry (verses simply not finding the
66 * Upon reaching the last segment of a path, if the reference is for DELETE,
67 * or NOCACHE is set (rewrite), and the name is located in the cache, it
72 * Structures associated with name cacheing.
74 #define NCHHASH(hash) (&nchashtbl[(hash) & nchash])
77 MALLOC_DEFINE(M_VFSCACHE, "vfscache", "VFS name cache entries");
79 static LIST_HEAD(nchashhead, namecache) *nchashtbl; /* Hash Table */
80 static struct namecache_list ncneglist; /* instead of vnode */
81 static struct namecache rootnamecache; /* Dummy node */
83 static int nczapcheck; /* panic on bad release */
84 SYSCTL_INT(_debug, OID_AUTO, nczapcheck, CTLFLAG_RW, &nczapcheck, 0, "");
86 static u_long nchash; /* size of hash table */
87 SYSCTL_ULONG(_debug, OID_AUTO, nchash, CTLFLAG_RD, &nchash, 0, "");
89 static u_long ncnegfactor = 16; /* ratio of negative entries */
90 SYSCTL_ULONG(_debug, OID_AUTO, ncnegfactor, CTLFLAG_RW, &ncnegfactor, 0, "");
92 static u_long numneg; /* number of cache entries allocated */
93 SYSCTL_ULONG(_debug, OID_AUTO, numneg, CTLFLAG_RD, &numneg, 0, "");
95 static u_long numcache; /* number of cache entries allocated */
96 SYSCTL_ULONG(_debug, OID_AUTO, numcache, CTLFLAG_RD, &numcache, 0, "");
98 static u_long numunres; /* number of unresolved entries */
99 SYSCTL_ULONG(_debug, OID_AUTO, numunres, CTLFLAG_RD, &numunres, 0, "");
101 SYSCTL_INT(_debug, OID_AUTO, vnsize, CTLFLAG_RD, 0, sizeof(struct vnode), "");
102 SYSCTL_INT(_debug, OID_AUTO, ncsize, CTLFLAG_RD, 0, sizeof(struct namecache), "");
105 * The new name cache statistics
107 SYSCTL_NODE(_vfs, OID_AUTO, cache, CTLFLAG_RW, 0, "Name cache statistics");
108 #define STATNODE(mode, name, var) \
109 SYSCTL_ULONG(_vfs_cache, OID_AUTO, name, mode, var, 0, "");
110 STATNODE(CTLFLAG_RD, numneg, &numneg);
111 STATNODE(CTLFLAG_RD, numcache, &numcache);
112 static u_long numcalls; STATNODE(CTLFLAG_RD, numcalls, &numcalls);
113 static u_long dothits; STATNODE(CTLFLAG_RD, dothits, &dothits);
114 static u_long dotdothits; STATNODE(CTLFLAG_RD, dotdothits, &dotdothits);
115 static u_long numchecks; STATNODE(CTLFLAG_RD, numchecks, &numchecks);
116 static u_long nummiss; STATNODE(CTLFLAG_RD, nummiss, &nummiss);
117 static u_long nummisszap; STATNODE(CTLFLAG_RD, nummisszap, &nummisszap);
118 static u_long numposzaps; STATNODE(CTLFLAG_RD, numposzaps, &numposzaps);
119 static u_long numposhits; STATNODE(CTLFLAG_RD, numposhits, &numposhits);
120 static u_long numnegzaps; STATNODE(CTLFLAG_RD, numnegzaps, &numnegzaps);
121 static u_long numneghits; STATNODE(CTLFLAG_RD, numneghits, &numneghits);
123 struct nchstats nchstats[SMP_MAXCPU];
125 * Export VFS cache effectiveness statistics to user-land.
127 * The statistics are left for aggregation to user-land so
128 * neat things can be achieved, like observing per-CPU cache
132 nchstats_agg(SYSCTL_HANDLER_ARGS)
134 struct globaldata *gd;
138 for (i = 0; i < ncpus; ++i) {
139 gd = globaldata_find(i);
140 if ((error = SYSCTL_OUT(req, (void *)&(*gd->gd_nchstats),
141 sizeof(struct nchstats))))
147 SYSCTL_PROC(_vfs_cache, OID_AUTO, nchstats, CTLTYPE_OPAQUE|CTLFLAG_RD,
148 0, 0, nchstats_agg, "S,nchstats", "VFS cache effectiveness statistics");
150 static void cache_zap(struct namecache *ncp);
153 * cache_hold() and cache_drop() prevent the premature deletion of a
154 * namecache entry but do not prevent operations (such as zapping) on
155 * that namecache entry.
159 _cache_hold(struct namecache *ncp)
167 _cache_drop(struct namecache *ncp)
169 KKASSERT(ncp->nc_refs > 0);
170 if (ncp->nc_refs == 1 &&
171 (ncp->nc_flag & NCF_UNRESOLVED) &&
172 TAILQ_EMPTY(&ncp->nc_list)
181 cache_hold(struct namecache *ncp)
183 return(_cache_hold(ncp));
187 cache_drop(struct namecache *ncp)
193 cache_link_parent(struct namecache *ncp, struct namecache *par)
195 KKASSERT(ncp->nc_parent == NULL);
196 ncp->nc_parent = par;
197 if (TAILQ_EMPTY(&par->nc_list)) {
201 TAILQ_INSERT_HEAD(&par->nc_list, ncp, nc_entry);
205 cache_unlink_parent(struct namecache *ncp)
207 struct namecache *par;
209 if ((par = ncp->nc_parent) != NULL) {
210 ncp->nc_parent = NULL;
211 par = cache_hold(par);
212 TAILQ_REMOVE(&par->nc_list, ncp, nc_entry);
213 if (par->nc_vp && TAILQ_EMPTY(&par->nc_list))
219 static struct namecache *
220 cache_alloc(struct vnode *vp)
222 struct namecache *ncp;
224 ncp = malloc(sizeof(*ncp), M_VFSCACHE, M_WAITOK|M_ZERO);
225 TAILQ_INIT(&ncp->nc_list);
228 TAILQ_INSERT_HEAD(&vp->v_namecache, ncp, nc_vnode);
231 TAILQ_INSERT_HEAD(&ncneglist, ncp, nc_vnode);
238 * Try to destroy a namecache entry. The entry is disassociated from its
239 * vnode or ncneglist and reverted to an UNRESOLVED state.
241 * Then, if there are no additional references to the ncp and we can
242 * successfully delete the children, the entry is also removed from the
243 * namecache hashlist / topology.
245 * References or undeletable children will prevent the entry from being
246 * removed from the topology. The entry may be revalidated (typically
247 * by cache_enter()) at a later time. Children remain because:
249 * + we have tried to delete a node rather then a leaf in the topology.
250 * + the presence of negative entries (we try to scrap these).
251 * + an entry or child has a non-zero ref count and cannot be scrapped.
253 * This function must be called with the ncp held and will drop the ref
254 * count during zapping.
257 cache_zap(struct namecache *ncp)
259 struct namecache *par;
263 * Disassociate the vnode or negative cache ref and set NCF_UNRESOLVED.
265 if ((ncp->nc_flag & NCF_UNRESOLVED) == 0) {
266 ncp->nc_flag |= NCF_UNRESOLVED;
268 if ((vp = ncp->nc_vp) != NULL) {
270 ncp->nc_vp = NULL; /* safety */
271 TAILQ_REMOVE(&vp->v_namecache, ncp, nc_vnode);
272 if (!TAILQ_EMPTY(&ncp->nc_list))
275 TAILQ_REMOVE(&ncneglist, ncp, nc_vnode);
281 * Try to scrap the entry and possibly tail-recurse on its parent.
282 * We only scrap unref'd (other then our ref) unresolved entries,
283 * we do not scrap 'live' entries.
285 while (ncp->nc_flag & NCF_UNRESOLVED) {
287 * Someone other then us has a ref, stop.
289 if (ncp->nc_refs > 1)
293 * We have children, stop.
295 if (!TAILQ_EMPTY(&ncp->nc_list))
299 * Ok, we can completely destroy and free this entry. Sanity
300 * check it against our static rootnamecache structure,
301 * then remove it from the hash.
303 KKASSERT(ncp != &rootnamecache);
305 if (ncp->nc_flag & NCF_HASHED) {
306 ncp->nc_flag &= ~NCF_HASHED;
307 LIST_REMOVE(ncp, nc_hash);
311 * Unlink from its parent and free, then loop on the
312 * parent. XXX temp hack, in stage-3 parent is never NULL
314 if ((par = ncp->nc_parent) != NULL) {
315 par = cache_hold(par);
316 TAILQ_REMOVE(&par->nc_list, ncp, nc_entry);
317 if (par->nc_vp && TAILQ_EMPTY(&par->nc_list))
321 ncp->nc_refs = -1; /* safety */
322 ncp->nc_parent = NULL; /* safety */
324 free(ncp->nc_name, M_VFSCACHE);
325 free(ncp, M_VFSCACHE);
327 if (par == NULL) /* temp hack */
328 return; /* temp hack */
335 * Lookup an entry in the cache
337 * Lookup is called with dvp pointing to the directory to search,
338 * cnp pointing to the name of the entry being sought.
340 * If the lookup succeeds, the vnode is returned in *vpp, and a
341 * status of -1 is returned.
343 * If the lookup determines that the name does not exist (negative cacheing),
344 * a status of ENOENT is returned.
346 * If the lookup fails, a status of zero is returned.
348 * Note that UNRESOLVED entries are ignored. They are not negative cache
352 cache_lookup(struct vnode *dvp, struct namecache *par, struct vnode **vpp,
353 struct namecache **ncpp, struct componentname *cnp)
355 struct namecache *ncp;
357 globaldata_t gd = mycpu;
362 * Obtain the namecache entry associated with dvp, creating one if
363 * necessary. If we have to create one we have insufficient
364 * information to hash it or even supply the name, but we still
365 * need one so we can link it in.
367 * NOTE: in this stage of development, the passed 'par' is
368 * almost always NULL.
371 if ((par = TAILQ_FIRST(&dvp->v_namecache)) == NULL)
372 par = cache_alloc(dvp);
376 * Deal with "." and "..". In this stage of code development we leave
377 * the returned ncpp NULL. Note that if the namecache is disjoint,
378 * we won't find a vnode for "..".
380 if (cnp->cn_nameptr[0] == '.') {
381 if (cnp->cn_namelen == 1) {
384 numposhits++; /* include in total statistics */
387 if (cnp->cn_namelen == 2 && cnp->cn_nameptr[1] == '.') {
389 numposhits++; /* include in total statistics */
390 if ((cnp->cn_flags & CNP_MAKEENTRY) == 0)
392 if (par->nc_parent == NULL ||
393 par->nc_parent->nc_vp == NULL) {
396 *vpp = par->nc_parent->nc_vp;
402 * Try to locate an existing entry
404 hash = fnv_32_buf(cnp->cn_nameptr, cnp->cn_namelen, FNV1_32_INIT);
405 hash = fnv_32_buf(&par, sizeof(par), hash);
407 printf("DVP %p/%p %08x %*.*s\n", dvp, par, hash, cnp->cn_namelen, cnp->cn_namelen, cnp->cn_nameptr);
408 LIST_FOREACH(ncp, (NCHHASH(hash)), nc_hash) {
411 printf("TEST ncp par=%p %*.*s\n",
412 ncp->nc_parent, ncp->nc_nlen, ncp->nc_nlen,
415 if ((ncp->nc_flag & NCF_UNRESOLVED) == 0 &&
416 ncp->nc_parent == par &&
417 ncp->nc_nlen == cnp->cn_namelen &&
418 bcmp(ncp->nc_name, cnp->cn_nameptr, ncp->nc_nlen) == 0
428 * If we failed to locate an entry, return 0 (indicates failure).
431 if ((cnp->cn_flags & CNP_MAKEENTRY) == 0) {
436 gd->gd_nchstats->ncs_miss++;
441 * If we found an entry, but we don't want to have one, we zap it.
443 if ((cnp->cn_flags & CNP_MAKEENTRY) == 0) {
445 gd->gd_nchstats->ncs_badhits++;
451 * If the vnode is not NULL then return the positive match.
455 gd->gd_nchstats->ncs_goodhits++;
462 * If the vnode is NULL we found a negative match. If we want to
463 * create it, purge the negative match and return failure (as if
464 * we hadn't found a match in the first place).
466 if (cnp->cn_nameiop == NAMEI_CREATE) {
468 gd->gd_nchstats->ncs_badhits++;
476 * We found a "negative" match, ENOENT notifies client of this match.
477 * The nc_flag field records whether this is a whiteout. Since there
478 * is no vnode we can use the vnode tailq link field with ncneglist.
480 TAILQ_REMOVE(&ncneglist, ncp, nc_vnode);
481 TAILQ_INSERT_TAIL(&ncneglist, ncp, nc_vnode);
482 gd->gd_nchstats->ncs_neghits++;
483 if (ncp->nc_flag & NCF_WHITEOUT)
484 cnp->cn_flags |= CNP_ISWHITEOUT;
490 * Generate a special linkage between the mount point and the root of the
491 * mounted filesystem in order to maintain the namecache topology across
492 * a mount point. The special linkage has a 0-length name component
493 * and sets NCF_MOUNTPT.
496 cache_mount(struct vnode *dvp, struct vnode *tvp)
498 struct namecache *ncp;
499 struct namecache *par;
500 struct nchashhead *nchpp;
504 * If a linkage already exists we do not have to do anything.
506 hash = fnv_32_buf("", 0, FNV1_32_INIT);
507 hash = fnv_32_buf(&dvp->v_id, sizeof(dvp->v_id), hash);
508 LIST_FOREACH(ncp, (NCHHASH(hash)), nc_hash) {
510 if (ncp->nc_vp == tvp &&
513 ncp->nc_parent->nc_vp == dvp
519 if ((par = TAILQ_FIRST(&dvp->v_namecache)) == NULL)
520 par = cache_alloc(dvp);
523 * Otherwise create a new linkage.
525 ncp = cache_alloc(tvp);
526 ncp->nc_flag = NCF_MOUNTPT;
527 cache_link_parent(ncp, par);
532 hash = fnv_32_buf("", 0, FNV1_32_INIT);
533 hash = fnv_32_buf(&dvp->v_id, sizeof(dvp->v_id), hash);
534 nchpp = NCHHASH(hash);
535 LIST_INSERT_HEAD(nchpp, ncp, nc_hash);
537 ncp->nc_flag |= NCF_HASHED;
541 * Add an entry to the cache.
544 cache_enter(struct vnode *dvp, struct namecache *par, struct vnode *vp, struct componentname *cnp)
546 struct namecache *ncp;
547 struct namecache *bpar;
548 struct nchashhead *nchpp;
553 * If the directory has no namecache entry we must associate one with
554 * it. The name of the entry is not known so it isn't hashed.
557 if ((par = TAILQ_FIRST(&dvp->v_namecache)) == NULL)
558 par = cache_alloc(dvp);
562 * This may be a bit confusing. "." and ".." are 'virtual' entries.
563 * We do not actually create a namecache entry representing either.
564 * However, the ".." case is used to linkup a potentially disjoint
565 * directory with its parent, to disconnect a directory from its
566 * parent, or to change an existing linkage that may no longer be
567 * correct (as might occur when a subdirectory is renamed).
570 if (cnp->cn_namelen == 1 && cnp->cn_nameptr[0] == '.')
572 if (cnp->cn_namelen == 2 && cnp->cn_nameptr[0] == '.' &&
573 cnp->cn_nameptr[1] == '.'
577 cache_unlink_parent(par);
579 if ((ncp = TAILQ_FIRST(&vp->v_namecache)) == NULL)
580 ncp = cache_alloc(vp);
583 cache_unlink_parent(par);
584 cache_link_parent(par, ncp); /* ncp is parent of par */
591 * Locate other entries associated with this vnode and zap them,
592 * because the purge code may not be able to find them due to
593 * the topology not yet being consistent. This is a temporary
598 TAILQ_FOREACH(ncp, &vp->v_namecache, nc_vnode) {
599 if ((ncp->nc_flag & NCF_UNRESOLVED) == 0) {
600 cache_zap(cache_hold(ncp));
606 hash = fnv_32_buf(cnp->cn_nameptr, cnp->cn_namelen, FNV1_32_INIT);
608 hash = fnv_32_buf(&bpar, sizeof(bpar), hash);
611 printf("ENTER %p/%p %08x '%*.*s' %p ", dvp, par, hash, (int)cnp->cn_namelen, (int)cnp->cn_namelen, cnp->cn_nameptr, vp);
614 name = malloc(cnp->cn_namelen, M_VFSCACHE, M_WAITOK);
615 ncp = cache_alloc(vp);
620 * Linkup the parent pointer, bump the parent vnode's hold
621 * count when we go from 0->1 children.
623 cache_link_parent(ncp, par);
626 * Add to the hash table
629 ncp->nc_nlen = cnp->cn_namelen;
630 bcopy(cnp->cn_nameptr, ncp->nc_name, cnp->cn_namelen);
631 nchpp = NCHHASH(hash);
632 LIST_INSERT_HEAD(nchpp, ncp, nc_hash);
633 ncp->nc_flag |= NCF_HASHED;
636 * If the target vnode is NULL if this is to be a negative cache
640 ncp->nc_flag &= ~NCF_WHITEOUT;
641 if (cnp->cn_flags & CNP_ISWHITEOUT)
642 ncp->nc_flag |= NCF_WHITEOUT;
646 * Don't cache too many negative hits
648 if (numneg > MINNEG && numneg * ncnegfactor > numcache) {
649 ncp = TAILQ_FIRST(&ncneglist);
650 KKASSERT(ncp != NULL);
651 cache_zap(cache_hold(ncp));
656 * Name cache initialization, from vfsinit() when we are booting
658 * rootnamecache is initialized such that it cannot be recursively deleted.
666 /* initialise per-cpu namecache effectiveness statistics. */
667 for (i = 0; i < ncpus; ++i) {
668 gd = globaldata_find(i);
669 gd->gd_nchstats = &nchstats[i];
672 TAILQ_INIT(&ncneglist);
673 nchashtbl = hashinit(desiredvnodes*2, M_VFSCACHE, &nchash);
674 TAILQ_INIT(&rootnamecache.nc_list);
675 rootnamecache.nc_flag |= NCF_HASHED | NCF_ROOT | NCF_UNRESOLVED;
676 rootnamecache.nc_refs = 1;
680 * vfs_cache_setroot()
682 * Create an association between the root of our namecache and
683 * the root vnode. This routine may be called several times during
687 vfs_cache_setroot(struct vnode *nvp)
689 KKASSERT(rootnamecache.nc_refs > 0); /* don't accidently free */
690 cache_zap(cache_hold(&rootnamecache));
692 rootnamecache.nc_vp = nvp;
693 rootnamecache.nc_flag &= ~NCF_UNRESOLVED;
696 if (!TAILQ_EMPTY(&rootnamecache.nc_list))
698 TAILQ_INSERT_HEAD(&nvp->v_namecache, &rootnamecache, nc_vnode);
701 TAILQ_INSERT_HEAD(&ncneglist, &rootnamecache, nc_vnode);
702 rootnamecache.nc_flag &= ~NCF_WHITEOUT;
707 * Invalidate all namecache entries to a particular vnode as well as
708 * any direct children of that vnode in the namecache. This is a
709 * 'catch all' purge used by filesystems that do not know any better.
711 * A new vnode v_id is generated. Note that no vnode will ever have a
714 * Note that the linkage between the vnode and its namecache entries will
715 * be removed, but the namecache entries themselves might stay put due to
716 * active references from elsewhere in the system or due to the existance of
717 * the children. The namecache topology is left intact even if we do not
718 * know what the vnode association is. Such entries will be marked
721 * XXX: Only time and the size of v_id prevents this from failing:
722 * XXX: In theory we should hunt down all (struct vnode*, v_id)
723 * XXX: soft references and nuke them, at least on the global
724 * XXX: v_id wraparound. The period of resistance can be extended
725 * XXX: by incrementing each vnodes v_id individually instead of
726 * XXX: using the global v_id.
729 cache_purge(struct vnode *vp)
731 static u_long nextid;
732 struct namecache *ncp;
733 struct namecache *scan;
736 * Disassociate the vnode from its namecache entries along with
737 * (for historical reasons) any direct children.
739 while ((ncp = TAILQ_FIRST(&vp->v_namecache)) != NULL) {
742 restart: /* YYY hack, fix me */
743 TAILQ_FOREACH(scan, &ncp->nc_list, nc_entry) {
744 if ((scan->nc_flag & NCF_UNRESOLVED) == 0) {
745 cache_zap(cache_hold(scan));
753 * Calculate a new unique id for ".." handling
757 } while (nextid == vp->v_id || nextid == 0);
762 * Flush all entries referencing a particular filesystem.
764 * Since we need to check it anyway, we will flush all the invalid
765 * entries at the same time.
768 cache_purgevfs(struct mount *mp)
770 struct nchashhead *nchpp;
771 struct namecache *ncp, *nnp;
774 * Scan hash tables for applicable entries.
776 for (nchpp = &nchashtbl[nchash]; nchpp >= nchashtbl; nchpp--) {
777 ncp = LIST_FIRST(nchpp);
781 nnp = LIST_NEXT(ncp, nc_hash);
784 if (ncp->nc_vp && ncp->nc_vp->v_mount == mp)
796 * Test whether the vnode is at a leaf in the nameicache tree.
798 * Returns 0 if it is a leaf, -1 if it isn't.
801 cache_leaf_test(struct vnode *vp)
803 struct namecache *scan;
804 struct namecache *ncp;
806 TAILQ_FOREACH(scan, &vp->v_namecache, nc_vnode) {
807 TAILQ_FOREACH(ncp, &scan->nc_list, nc_entry) {
808 /* YYY && ncp->nc_vp->v_type == VDIR ? */
809 if (ncp->nc_vp != NULL)
817 * Perform canonical checks and cache lookup and pass on to filesystem
818 * through the vop_cachedlookup only if needed.
821 * struct vnode a_dvp;
822 * struct namecache *a_ncp;
823 * struct vnode **a_vpp;
824 * struct namecache **a_ncpp;
825 * struct componentname *a_cnp;
829 vfs_cache_lookup(struct vop_lookup_args *ap)
831 struct vnode *dvp, *vp;
834 struct namecache *par = ap->a_par;
835 struct vnode **vpp = ap->a_vpp;
836 struct namecache **ncpp = ap->a_ncpp;
837 struct componentname *cnp = ap->a_cnp;
838 struct ucred *cred = cnp->cn_cred;
839 int flags = cnp->cn_flags;
840 struct thread *td = cnp->cn_td;
841 u_long vpid; /* capability number of vnode */
847 lockparent = flags & CNP_LOCKPARENT;
849 if (dvp->v_type != VDIR)
852 if ((flags & CNP_ISLASTCN) && (dvp->v_mount->mnt_flag & MNT_RDONLY) &&
853 (cnp->cn_nameiop == NAMEI_DELETE || cnp->cn_nameiop == NAMEI_RENAME)) {
857 error = VOP_ACCESS(dvp, VEXEC, cred, td);
862 error = cache_lookup(dvp, par, vpp, ncpp, cnp);
865 return (VOP_CACHEDLOOKUP(dvp, par, vpp, ncpp, cnp));
872 cnp->cn_flags &= ~CNP_PDIRUNLOCK;
873 if (dvp == vp) { /* lookup on "." */
876 } else if (flags & CNP_ISDOTDOT) {
877 VOP_UNLOCK(dvp, NULL, 0, td);
878 cnp->cn_flags |= CNP_PDIRUNLOCK;
879 error = vget(vp, NULL, LK_EXCLUSIVE, td);
880 if (!error && lockparent && (flags & CNP_ISLASTCN)) {
881 if ((error = vn_lock(dvp, NULL, LK_EXCLUSIVE, td)) == 0)
882 cnp->cn_flags &= ~CNP_PDIRUNLOCK;
885 error = vget(vp, NULL, LK_EXCLUSIVE, td);
886 if (!lockparent || error || !(flags & CNP_ISLASTCN)) {
887 VOP_UNLOCK(dvp, NULL, 0, td);
888 cnp->cn_flags |= CNP_PDIRUNLOCK;
892 * Check that the capability number did not change
893 * while we were waiting for the lock.
896 if (vpid == vp->v_id)
899 if (lockparent && dvp != vp && (flags & CNP_ISLASTCN)) {
900 VOP_UNLOCK(dvp, NULL, 0, td);
901 cnp->cn_flags |= CNP_PDIRUNLOCK;
904 if (cnp->cn_flags & CNP_PDIRUNLOCK) {
905 error = vn_lock(dvp, NULL, LK_EXCLUSIVE, td);
908 cnp->cn_flags &= ~CNP_PDIRUNLOCK;
910 return (VOP_CACHEDLOOKUP(dvp, par, vpp, ncpp, cnp));
913 static int disablecwd;
914 SYSCTL_INT(_debug, OID_AUTO, disablecwd, CTLFLAG_RW, &disablecwd, 0, "");
916 static u_long numcwdcalls; STATNODE(CTLFLAG_RD, numcwdcalls, &numcwdcalls);
917 static u_long numcwdfail1; STATNODE(CTLFLAG_RD, numcwdfail1, &numcwdfail1);
918 static u_long numcwdfail2; STATNODE(CTLFLAG_RD, numcwdfail2, &numcwdfail2);
919 static u_long numcwdfail3; STATNODE(CTLFLAG_RD, numcwdfail3, &numcwdfail3);
920 static u_long numcwdfail4; STATNODE(CTLFLAG_RD, numcwdfail4, &numcwdfail4);
921 static u_long numcwdfound; STATNODE(CTLFLAG_RD, numcwdfound, &numcwdfound);
924 __getcwd(struct __getcwd_args *uap)
926 struct proc *p = curproc;
928 int error, i, slash_prefixed;
929 struct filedesc *fdp;
930 struct namecache *ncp;
938 if (uap->buflen > MAXPATHLEN)
939 uap->buflen = MAXPATHLEN;
940 buf = bp = malloc(uap->buflen, M_TEMP, M_WAITOK);
941 bp += uap->buflen - 1;
945 for (vp = fdp->fd_cdir; vp != fdp->fd_rdir && vp != rootvnode;) {
946 if (vp->v_flag & VROOT) {
947 if (vp->v_mount == NULL) { /* forced unmount */
951 vp = vp->v_mount->mnt_vnodecovered;
954 TAILQ_FOREACH(ncp, &vp->v_namecache, nc_vnode) {
955 if (ncp->nc_parent && ncp->nc_parent->nc_vp &&
965 for (i = ncp->nc_nlen - 1; i >= 0; i--) {
971 *--bp = ncp->nc_name[i];
980 vp = ncp->nc_parent->nc_vp;
982 if (!slash_prefixed) {
991 error = copyout(bp, uap->buf, strlen(bp) + 1);
997 * Thus begins the fullpath magic.
1001 #define STATNODE(name) \
1002 static u_int name; \
1003 SYSCTL_UINT(_vfs_cache, OID_AUTO, name, CTLFLAG_RD, &name, 0, "")
1005 static int disablefullpath;
1006 SYSCTL_INT(_debug, OID_AUTO, disablefullpath, CTLFLAG_RW,
1007 &disablefullpath, 0, "");
1009 STATNODE(numfullpathcalls);
1010 STATNODE(numfullpathfail1);
1011 STATNODE(numfullpathfail2);
1012 STATNODE(numfullpathfail3);
1013 STATNODE(numfullpathfail4);
1014 STATNODE(numfullpathfound);
1017 textvp_fullpath(struct proc *p, char **retbuf, char **retfreebuf)
1020 int i, slash_prefixed;
1021 struct filedesc *fdp;
1022 struct namecache *ncp;
1023 struct vnode *vp, *textvp;
1026 if (disablefullpath)
1028 textvp = p->p_textvp;
1031 buf = malloc(MAXPATHLEN, M_TEMP, M_WAITOK);
1032 bp = buf + MAXPATHLEN - 1;
1036 for (vp = textvp; vp != fdp->fd_rdir && vp != rootvnode;) {
1037 if (vp->v_flag & VROOT) {
1038 if (vp->v_mount == NULL) { /* forced unmount */
1042 vp = vp->v_mount->mnt_vnodecovered;
1045 TAILQ_FOREACH(ncp, &vp->v_namecache, nc_vnode) {
1048 if (ncp->nc_parent && ncp->nc_parent->nc_vp &&
1058 for (i = ncp->nc_nlen - 1; i >= 0; i--) {
1064 *--bp = ncp->nc_name[i];
1073 vp = ncp->nc_parent->nc_vp;
1075 if (!slash_prefixed) {