vfs_cache.c revision 74384
1266077Sdes/* 2266077Sdes * Copyright (c) 1989, 1993, 1995 3266077Sdes * The Regents of the University of California. All rights reserved. 4266077Sdes * 5266077Sdes * This code is derived from software contributed to Berkeley by 6266077Sdes * Poul-Henning Kamp of the FreeBSD Project. 7266077Sdes * 8266077Sdes * Redistribution and use in source and binary forms, with or without 9266077Sdes * modification, are permitted provided that the following conditions 10266077Sdes * are met: 11266077Sdes * 1. Redistributions of source code must retain the above copyright 12266077Sdes * notice, this list of conditions and the following disclaimer. 13266077Sdes * 2. Redistributions in binary form must reproduce the above copyright 14266077Sdes * notice, this list of conditions and the following disclaimer in the 15266077Sdes * documentation and/or other materials provided with the distribution. 16266077Sdes * 3. All advertising materials mentioning features or use of this software 17266077Sdes * must display the following acknowledgement: 18266077Sdes * This product includes software developed by the University of 19266077Sdes * California, Berkeley and its contributors. 20266077Sdes * 4. Neither the name of the University nor the names of its contributors 21266077Sdes * may be used to endorse or promote products derived from this software 22266077Sdes * without specific prior written permission. 23266077Sdes * 24266077Sdes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 25266077Sdes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 26266077Sdes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 27266077Sdes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 28266077Sdes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 29266077Sdes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 30266077Sdes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 31266077Sdes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 32266077Sdes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 33266077Sdes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 34266077Sdes * SUCH DAMAGE. 35266077Sdes * 36266077Sdes * @(#)vfs_cache.c 8.5 (Berkeley) 3/22/95 37266077Sdes * $FreeBSD: head/sys/kern/vfs_cache.c 74384 2001-03-17 09:31:06Z peter $ 38266077Sdes */ 39266077Sdes 40266077Sdes#include <sys/param.h> 41266077Sdes#include <sys/systm.h> 42266077Sdes#include <sys/kernel.h> 43266077Sdes#include <sys/sysctl.h> 44266077Sdes#include <sys/mount.h> 45266077Sdes#include <sys/vnode.h> 46266077Sdes#include <sys/namei.h> 47266077Sdes#include <sys/malloc.h> 48266077Sdes#include <sys/sysproto.h> 49266077Sdes#include <sys/proc.h> 50266077Sdes#include <sys/filedesc.h> 51266077Sdes#include <sys/fnv_hash.h> 52266077Sdes 53266077Sdes/* 54266077Sdes * This structure describes the elements in the cache of recent 55266077Sdes * names looked up by namei. 56266077Sdes */ 57266077Sdes 58266077Sdesstruct namecache { 59266077Sdes LIST_ENTRY(namecache) nc_hash; /* hash chain */ 60266077Sdes LIST_ENTRY(namecache) nc_src; /* source vnode list */ 61266077Sdes TAILQ_ENTRY(namecache) nc_dst; /* destination vnode list */ 62266077Sdes struct vnode *nc_dvp; /* vnode of parent of name */ 63266077Sdes struct vnode *nc_vp; /* vnode the name refers to */ 64266077Sdes u_char nc_flag; /* flag bits */ 65266077Sdes u_char nc_nlen; /* length of name */ 66266077Sdes char nc_name[0]; /* segment name */ 67266077Sdes}; 68266077Sdes 69266077Sdes/* 70266077Sdes * Name caching works as follows: 71266077Sdes * 72266077Sdes * Names found by directory scans are retained in a cache 73266077Sdes * for future reference. It is managed LRU, so frequently 74266077Sdes * used names will hang around. Cache is indexed by hash value 75266077Sdes * obtained from (vp, name) where vp refers to the directory 76266077Sdes * containing name. 77266077Sdes * 78266077Sdes * If it is a "negative" entry, (i.e. for a name that is known NOT to 79266077Sdes * exist) the vnode pointer will be NULL. 80266077Sdes * 81266077Sdes * Upon reaching the last segment of a path, if the reference 82266077Sdes * is for DELETE, or NOCACHE is set (rewrite), and the 83266077Sdes * name is located in the cache, it will be dropped. 84266077Sdes */ 85266077Sdes 86266077Sdes/* 87266077Sdes * Structures associated with name cacheing. 88266077Sdes */ 89266077Sdes#define NCHHASH(dvp, hash) \ 90266077Sdes (&nchashtbl[((dvp)->v_id + (hash)) & nchash]) 91266077Sdesstatic LIST_HEAD(nchashhead, namecache) *nchashtbl; /* Hash Table */ 92266077Sdesstatic TAILQ_HEAD(, namecache) ncneg; /* Hash Table */ 93266077Sdesstatic u_long nchash; /* size of hash table */ 94266077SdesSYSCTL_ULONG(_debug, OID_AUTO, nchash, CTLFLAG_RD, &nchash, 0, ""); 95266077Sdesstatic u_long ncnegfactor = 16; /* ratio of negative entries */ 96266077SdesSYSCTL_ULONG(_debug, OID_AUTO, ncnegfactor, CTLFLAG_RW, &ncnegfactor, 0, ""); 97266077Sdesstatic u_long numneg; /* number of cache entries allocated */ 98266077SdesSYSCTL_ULONG(_debug, OID_AUTO, numneg, CTLFLAG_RD, &numneg, 0, ""); 99266077Sdesstatic u_long numcache; /* number of cache entries allocated */ 100266077SdesSYSCTL_ULONG(_debug, OID_AUTO, numcache, CTLFLAG_RD, &numcache, 0, ""); 101266077Sdesstruct nchstats nchstats; /* cache effectiveness statistics */ 102266077Sdes 103266077Sdesstatic int doingcache = 1; /* 1 => enable the cache */ 104266077SdesSYSCTL_INT(_debug, OID_AUTO, vfscache, CTLFLAG_RW, &doingcache, 0, ""); 105266077SdesSYSCTL_INT(_debug, OID_AUTO, vnsize, CTLFLAG_RD, 0, sizeof(struct vnode), ""); 106266077SdesSYSCTL_INT(_debug, OID_AUTO, ncsize, CTLFLAG_RD, 0, sizeof(struct namecache), ""); 107266077Sdes 108266077Sdes/* 109266077Sdes * The new name cache statistics 110266077Sdes */ 111266077SdesSYSCTL_NODE(_vfs, OID_AUTO, cache, CTLFLAG_RW, 0, "Name cache statistics"); 112266077Sdes#define STATNODE(mode, name, var) \ 113266077Sdes SYSCTL_ULONG(_vfs_cache, OID_AUTO, name, mode, var, 0, ""); 114266077SdesSTATNODE(CTLFLAG_RD, numneg, &numneg); 115266077SdesSTATNODE(CTLFLAG_RD, numcache, &numcache); 116266077Sdesstatic u_long numcalls; STATNODE(CTLFLAG_RD, numcalls, &numcalls); 117266077Sdesstatic u_long dothits; STATNODE(CTLFLAG_RD, dothits, &dothits); 118266077Sdesstatic u_long dotdothits; STATNODE(CTLFLAG_RD, dotdothits, &dotdothits); 119266077Sdesstatic u_long numchecks; STATNODE(CTLFLAG_RD, numchecks, &numchecks); 120266077Sdesstatic u_long nummiss; STATNODE(CTLFLAG_RD, nummiss, &nummiss); 121266077Sdesstatic u_long nummisszap; STATNODE(CTLFLAG_RD, nummisszap, &nummisszap); 122266077Sdesstatic u_long numposzaps; STATNODE(CTLFLAG_RD, numposzaps, &numposzaps); 123266077Sdesstatic u_long numposhits; STATNODE(CTLFLAG_RD, numposhits, &numposhits); 124266077Sdesstatic u_long numnegzaps; STATNODE(CTLFLAG_RD, numnegzaps, &numnegzaps); 125266077Sdesstatic u_long numneghits; STATNODE(CTLFLAG_RD, numneghits, &numneghits); 126266077Sdes 127266077SdesSYSCTL_OPAQUE(_vfs_cache, OID_AUTO, nchstats, CTLFLAG_RD, &nchstats, 128266077Sdes sizeof(nchstats), "LU", "VFS cache effectiveness statistics"); 129266077Sdes 130266077Sdes 131266077Sdes 132266077Sdesstatic void cache_zap __P((struct namecache *ncp)); 133266077Sdes 134266077Sdesstatic MALLOC_DEFINE(M_VFSCACHE, "vfscache", "VFS name cache entries"); 135266077Sdes 136266077Sdes/* 137266077Sdes * Flags in namecache.nc_flag 138266077Sdes */ 139266077Sdes#define NCF_WHITE 1 140266077Sdes/* 141266077Sdes * Delete an entry from its hash list and move it to the front 142266077Sdes * of the LRU list for immediate reuse. 143266077Sdes */ 144266077Sdesstatic void 145266077Sdescache_zap(ncp) 146266077Sdes struct namecache *ncp; 147266077Sdes{ 148266077Sdes LIST_REMOVE(ncp, nc_hash); 149266077Sdes LIST_REMOVE(ncp, nc_src); 150266077Sdes if (LIST_EMPTY(&ncp->nc_dvp->v_cache_src)) 151266077Sdes vdrop(ncp->nc_dvp); 152266077Sdes if (ncp->nc_vp) { 153266077Sdes TAILQ_REMOVE(&ncp->nc_vp->v_cache_dst, ncp, nc_dst); 154266077Sdes } else { 155266077Sdes TAILQ_REMOVE(&ncneg, ncp, nc_dst); 156266077Sdes numneg--; 157266077Sdes } 158266077Sdes numcache--; 159266077Sdes free(ncp, M_VFSCACHE); 160266077Sdes} 161266077Sdes 162266077Sdes/* 163266077Sdes * Lookup an entry in the cache 164266077Sdes * 165266077Sdes * We don't do this if the segment name is long, simply so the cache 166266077Sdes * can avoid holding long names (which would either waste space, or 167266077Sdes * add greatly to the complexity). 168266077Sdes * 169266077Sdes * Lookup is called with dvp pointing to the directory to search, 170266077Sdes * cnp pointing to the name of the entry being sought. If the lookup 171266077Sdes * succeeds, the vnode is returned in *vpp, and a status of -1 is 172266077Sdes * returned. If the lookup determines that the name does not exist 173266077Sdes * (negative cacheing), a status of ENOENT is returned. If the lookup 174266077Sdes * fails, a status of zero is returned. 175266077Sdes */ 176266077Sdes 177266077Sdesint 178266077Sdescache_lookup(dvp, vpp, cnp) 179266077Sdes struct vnode *dvp; 180266077Sdes struct vnode **vpp; 181266077Sdes struct componentname *cnp; 182266077Sdes{ 183266077Sdes struct namecache *ncp; 184266077Sdes u_int32_t hash; 185266077Sdes 186266077Sdes if (!doingcache) { 187266077Sdes cnp->cn_flags &= ~MAKEENTRY; 188266077Sdes return (0); 189266077Sdes } 190266077Sdes 191266077Sdes numcalls++; 192266077Sdes 193266077Sdes if (cnp->cn_nameptr[0] == '.') { 194266077Sdes if (cnp->cn_namelen == 1) { 195266077Sdes *vpp = dvp; 196266077Sdes dothits++; 197266077Sdes return (-1); 198266077Sdes } 199266077Sdes if (cnp->cn_namelen == 2 && cnp->cn_nameptr[1] == '.') { 200266077Sdes dotdothits++; 201266077Sdes if (dvp->v_dd->v_id != dvp->v_ddid || 202266077Sdes (cnp->cn_flags & MAKEENTRY) == 0) { 203266077Sdes dvp->v_ddid = 0; 204266077Sdes return (0); 205266077Sdes } 206266077Sdes *vpp = dvp->v_dd; 207266077Sdes return (-1); 208266077Sdes } 209266077Sdes } 210266077Sdes 211266077Sdes hash = fnv32_hashbuf(cnp->cn_nameptr, cnp->cn_namelen); 212266077Sdes LIST_FOREACH(ncp, (NCHHASH(dvp, hash)), nc_hash) { 213266077Sdes numchecks++; 214266077Sdes if (ncp->nc_dvp == dvp && ncp->nc_nlen == cnp->cn_namelen && 215266077Sdes !bcmp(ncp->nc_name, cnp->cn_nameptr, ncp->nc_nlen)) 216266077Sdes break; 217266077Sdes } 218266077Sdes 219266077Sdes /* We failed to find an entry */ 220266077Sdes if (ncp == 0) { 221266077Sdes if ((cnp->cn_flags & MAKEENTRY) == 0) { 222266077Sdes nummisszap++; 223266077Sdes } else { 224266077Sdes nummiss++; 225266077Sdes } 226266077Sdes nchstats.ncs_miss++; 227266077Sdes return (0); 228266077Sdes } 229266077Sdes 230266077Sdes /* We don't want to have an entry, so dump it */ 231266077Sdes if ((cnp->cn_flags & MAKEENTRY) == 0) { 232266077Sdes numposzaps++; 233266077Sdes nchstats.ncs_badhits++; 234266077Sdes cache_zap(ncp); 235266077Sdes return (0); 236266077Sdes } 237266077Sdes 238266077Sdes /* We found a "positive" match, return the vnode */ 239266077Sdes if (ncp->nc_vp) { 240266077Sdes numposhits++; 241266077Sdes nchstats.ncs_goodhits++; 242266077Sdes *vpp = ncp->nc_vp; 243266077Sdes return (-1); 244266077Sdes } 245266077Sdes 246266077Sdes /* We found a negative match, and want to create it, so purge */ 247266077Sdes if (cnp->cn_nameiop == CREATE) { 248266077Sdes numnegzaps++; 249266077Sdes nchstats.ncs_badhits++; 250266077Sdes cache_zap(ncp); 251266077Sdes return (0); 252266077Sdes } 253266077Sdes 254266077Sdes numneghits++; 255266077Sdes /* 256266077Sdes * We found a "negative" match, ENOENT notifies client of this match. 257266077Sdes * The nc_vpid field records whether this is a whiteout. 258266077Sdes */ 259266077Sdes TAILQ_REMOVE(&ncneg, ncp, nc_dst); 260266077Sdes TAILQ_INSERT_TAIL(&ncneg, ncp, nc_dst); 261266077Sdes nchstats.ncs_neghits++; 262266077Sdes if (ncp->nc_flag & NCF_WHITE) 263266077Sdes cnp->cn_flags |= ISWHITEOUT; 264266077Sdes return (ENOENT); 265266077Sdes} 266266077Sdes 267266077Sdes/* 268266077Sdes * Add an entry to the cache. 269266077Sdes */ 270266077Sdesvoid 271266077Sdescache_enter(dvp, vp, cnp) 272266077Sdes struct vnode *dvp; 273266077Sdes struct vnode *vp; 274266077Sdes struct componentname *cnp; 275266077Sdes{ 276266077Sdes struct namecache *ncp; 277266077Sdes struct nchashhead *ncpp; 278266077Sdes u_int32_t hash; 279266077Sdes int len; 280266077Sdes 281266077Sdes if (!doingcache) 282266077Sdes return; 283266077Sdes 284266077Sdes if (cnp->cn_nameptr[0] == '.') { 285266077Sdes if (cnp->cn_namelen == 1) { 286266077Sdes return; 287266077Sdes } 288266077Sdes if (cnp->cn_namelen == 2 && cnp->cn_nameptr[1] == '.') { 289266077Sdes if (vp) { 290266077Sdes dvp->v_dd = vp; 291266077Sdes dvp->v_ddid = vp->v_id; 292266077Sdes } else { 293266077Sdes dvp->v_dd = dvp; 294266077Sdes dvp->v_ddid = 0; 295266077Sdes } 296266077Sdes return; 297266077Sdes } 298266077Sdes } 299266077Sdes 300266077Sdes ncp = (struct namecache *) 301266077Sdes malloc(sizeof *ncp + cnp->cn_namelen, M_VFSCACHE, M_WAITOK); 302266077Sdes bzero((char *)ncp, sizeof *ncp); 303266077Sdes numcache++; 304266077Sdes if (!vp) { 305266077Sdes numneg++; 306266077Sdes ncp->nc_flag = cnp->cn_flags & ISWHITEOUT ? NCF_WHITE : 0; 307266077Sdes } else if (vp->v_type == VDIR) { 308266077Sdes vp->v_dd = dvp; 309266077Sdes vp->v_ddid = dvp->v_id; 310266077Sdes } 311266077Sdes 312266077Sdes /* 313266077Sdes * Fill in cache info, if vp is NULL this is a "negative" cache entry. 314266077Sdes * For negative entries, we have to record whether it is a whiteout. 315266077Sdes * the whiteout flag is stored in the nc_vpid field which is 316266077Sdes * otherwise unused. 317266077Sdes */ 318266077Sdes ncp->nc_vp = vp; 319266077Sdes ncp->nc_dvp = dvp; 320266077Sdes len = ncp->nc_nlen = cnp->cn_namelen; 321266077Sdes hash = fnv32_hashbuf(cnp->cn_nameptr, len); 322266077Sdes bcopy(cnp->cn_nameptr, ncp->nc_name, len); 323266077Sdes ncpp = NCHHASH(dvp, hash); 324266077Sdes LIST_INSERT_HEAD(ncpp, ncp, nc_hash); 325266077Sdes if (LIST_EMPTY(&dvp->v_cache_src)) 326266077Sdes vhold(dvp); 327266077Sdes LIST_INSERT_HEAD(&dvp->v_cache_src, ncp, nc_src); 328266077Sdes if (vp) { 329266077Sdes TAILQ_INSERT_HEAD(&vp->v_cache_dst, ncp, nc_dst); 330266077Sdes } else { 331266077Sdes TAILQ_INSERT_TAIL(&ncneg, ncp, nc_dst); 332266077Sdes } 333266077Sdes if (numneg * ncnegfactor > numcache) { 334266077Sdes ncp = TAILQ_FIRST(&ncneg); 335266077Sdes cache_zap(ncp); 336266077Sdes } 337266077Sdes} 338266077Sdes 339266077Sdes/* 340266077Sdes * Name cache initialization, from vfs_init() when we are booting 341266077Sdes */ 342266077Sdesstatic void 343266077Sdesnchinit(void *dummy __unused) 344266077Sdes{ 345266077Sdes 346266077Sdes TAILQ_INIT(&ncneg); 347266077Sdes nchashtbl = hashinit(desiredvnodes * 2, M_VFSCACHE, &nchash); 348266077Sdes} 349266077SdesSYSINIT(vfs, SI_SUB_VFS, SI_ORDER_SECOND, nchinit, NULL) 350266077Sdes 351266077Sdes 352266077Sdes/* 353266077Sdes * Invalidate all entries to a particular vnode. 354266077Sdes * 355266077Sdes * Remove all entries in the namecache relating to this vnode and 356266077Sdes * change the v_id. We take the v_id from a global counter, since 357266077Sdes * it becomes a handy sequence number in crash-dumps that way. 358266077Sdes * No valid vnode will ever have (v_id == 0). 359266077Sdes * 360266077Sdes * XXX: Only time and the size of v_id prevents this from failing: 361266077Sdes * XXX: In theory we should hunt down all (struct vnode*, v_id) 362266077Sdes * XXX: soft references and nuke them, at least on the global 363266077Sdes * XXX: v_id wraparound. The period of resistance can be extended 364285206Sdes * XXX: by incrementing each vnodes v_id individually instead of 365285206Sdes * XXX: using the global v_id. 366266077Sdes */ 367266077Sdes 368266077Sdesvoid 369266077Sdescache_purge(vp) 370266077Sdes struct vnode *vp; 371266077Sdes{ 372266077Sdes static u_long nextid; 373266077Sdes 374266077Sdes while (!LIST_EMPTY(&vp->v_cache_src)) 375266077Sdes cache_zap(LIST_FIRST(&vp->v_cache_src)); 376266077Sdes while (!TAILQ_EMPTY(&vp->v_cache_dst)) 377266077Sdes cache_zap(TAILQ_FIRST(&vp->v_cache_dst)); 378266077Sdes 379266077Sdes do 380266077Sdes nextid++; 381266077Sdes while (nextid == vp->v_id || !nextid); 382266077Sdes vp->v_id = nextid; 383266077Sdes vp->v_dd = vp; 384266077Sdes vp->v_ddid = 0; 385266077Sdes} 386266077Sdes 387266077Sdes/* 388266077Sdes * Flush all entries referencing a particular filesystem. 389266077Sdes * 390266077Sdes * Since we need to check it anyway, we will flush all the invalid 391266077Sdes * entries at the same time. 392266077Sdes */ 393266077Sdesvoid 394266077Sdescache_purgevfs(mp) 395266077Sdes struct mount *mp; 396266077Sdes{ 397266077Sdes struct nchashhead *ncpp; 398266077Sdes struct namecache *ncp, *nnp; 399266077Sdes 400266077Sdes /* Scan hash tables for applicable entries */ 401266077Sdes for (ncpp = &nchashtbl[nchash]; ncpp >= nchashtbl; ncpp--) { 402266077Sdes for (ncp = LIST_FIRST(ncpp); ncp != 0; ncp = nnp) { 403266077Sdes nnp = LIST_NEXT(ncp, nc_hash); 404266077Sdes if (ncp->nc_dvp->v_mount == mp) { 405266077Sdes cache_zap(ncp); 406266077Sdes } 407266077Sdes } 408266077Sdes } 409266077Sdes} 410266077Sdes 411266077Sdes/* 412266077Sdes * Perform canonical checks and cache lookup and pass on to filesystem 413266077Sdes * through the vop_cachedlookup only if needed. 414266077Sdes */ 415266077Sdes 416266077Sdesint 417266077Sdesvfs_cache_lookup(ap) 418266077Sdes struct vop_lookup_args /* { 419266077Sdes struct vnode *a_dvp; 420266077Sdes struct vnode **a_vpp; 421266077Sdes struct componentname *a_cnp; 422266077Sdes } */ *ap; 423266077Sdes{ 424266077Sdes struct vnode *dvp, *vp; 425266077Sdes int lockparent; 426266077Sdes int error; 427266077Sdes struct vnode **vpp = ap->a_vpp; 428266077Sdes struct componentname *cnp = ap->a_cnp; 429266077Sdes struct ucred *cred = cnp->cn_cred; 430266077Sdes int flags = cnp->cn_flags; 431266077Sdes struct proc *p = cnp->cn_proc; 432266077Sdes u_long vpid; /* capability number of vnode */ 433266077Sdes 434266077Sdes *vpp = NULL; 435266077Sdes dvp = ap->a_dvp; 436266077Sdes lockparent = flags & LOCKPARENT; 437266077Sdes 438266077Sdes if (dvp->v_type != VDIR) 439266077Sdes return (ENOTDIR); 440266077Sdes 441266077Sdes if ((flags & ISLASTCN) && (dvp->v_mount->mnt_flag & MNT_RDONLY) && 442266077Sdes (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME)) 443266077Sdes return (EROFS); 444266077Sdes 445266077Sdes error = VOP_ACCESS(dvp, VEXEC, cred, p); 446266077Sdes 447266077Sdes if (error) 448266077Sdes return (error); 449266077Sdes 450266077Sdes error = cache_lookup(dvp, vpp, cnp); 451266077Sdes 452266077Sdes if (!error) 453266077Sdes return (VOP_CACHEDLOOKUP(dvp, vpp, cnp)); 454266077Sdes 455266077Sdes if (error == ENOENT) 456266077Sdes return (error); 457266077Sdes 458266077Sdes vp = *vpp; 459266077Sdes vpid = vp->v_id; 460266077Sdes cnp->cn_flags &= ~PDIRUNLOCK; 461266077Sdes if (dvp == vp) { /* lookup on "." */ 462266077Sdes VREF(vp); 463266077Sdes error = 0; 464266077Sdes } else if (flags & ISDOTDOT) { 465266077Sdes VOP_UNLOCK(dvp, 0, p); 466266077Sdes cnp->cn_flags |= PDIRUNLOCK; 467266077Sdes error = vget(vp, LK_EXCLUSIVE, p); 468266077Sdes if (!error && lockparent && (flags & ISLASTCN)) { 469266077Sdes if ((error = vn_lock(dvp, LK_EXCLUSIVE, p)) == 0) 470266077Sdes cnp->cn_flags &= ~PDIRUNLOCK; 471266077Sdes } 472266077Sdes } else { 473266077Sdes error = vget(vp, LK_EXCLUSIVE, p); 474266077Sdes if (!lockparent || error || !(flags & ISLASTCN)) { 475266077Sdes VOP_UNLOCK(dvp, 0, p); 476266077Sdes cnp->cn_flags |= PDIRUNLOCK; 477266077Sdes } 478266077Sdes } 479266077Sdes /* 480266077Sdes * Check that the capability number did not change 481266077Sdes * while we were waiting for the lock. 482266077Sdes */ 483266077Sdes if (!error) { 484266077Sdes if (vpid == vp->v_id) 485266077Sdes return (0); 486266077Sdes vput(vp); 487266077Sdes if (lockparent && dvp != vp && (flags & ISLASTCN)) { 488266077Sdes VOP_UNLOCK(dvp, 0, p); 489266077Sdes cnp->cn_flags |= PDIRUNLOCK; 490266077Sdes } 491266077Sdes } 492266077Sdes if (cnp->cn_flags & PDIRUNLOCK) { 493266077Sdes error = vn_lock(dvp, LK_EXCLUSIVE, p); 494266077Sdes if (error) 495266077Sdes return (error); 496266077Sdes cnp->cn_flags &= ~PDIRUNLOCK; 497266077Sdes } 498266077Sdes return (VOP_CACHEDLOOKUP(dvp, vpp, cnp)); 499266077Sdes} 500266077Sdes 501266077Sdes 502266077Sdes#ifndef _SYS_SYSPROTO_H_ 503266077Sdesstruct __getcwd_args { 504266077Sdes u_char *buf; 505266077Sdes u_int buflen; 506266077Sdes}; 507266077Sdes#endif 508266077Sdes 509266077Sdesstatic int disablecwd; 510266077SdesSYSCTL_INT(_debug, OID_AUTO, disablecwd, CTLFLAG_RW, &disablecwd, 0, ""); 511266077Sdes 512266077Sdesstatic u_long numcwdcalls; STATNODE(CTLFLAG_RD, numcwdcalls, &numcwdcalls); 513266077Sdesstatic u_long numcwdfail1; STATNODE(CTLFLAG_RD, numcwdfail1, &numcwdfail1); 514266077Sdesstatic u_long numcwdfail2; STATNODE(CTLFLAG_RD, numcwdfail2, &numcwdfail2); 515266077Sdesstatic u_long numcwdfail3; STATNODE(CTLFLAG_RD, numcwdfail3, &numcwdfail3); 516266077Sdesstatic u_long numcwdfail4; STATNODE(CTLFLAG_RD, numcwdfail4, &numcwdfail4); 517266077Sdesstatic u_long numcwdfound; STATNODE(CTLFLAG_RD, numcwdfound, &numcwdfound); 518266077Sdesint 519266077Sdes__getcwd(p, uap) 520266077Sdes struct proc *p; 521266077Sdes struct __getcwd_args *uap; 522266077Sdes{ 523266077Sdes char *bp, *buf; 524266077Sdes int error, i, slash_prefixed; 525266077Sdes struct filedesc *fdp; 526266077Sdes struct namecache *ncp; 527266077Sdes struct vnode *vp; 528266077Sdes 529266077Sdes numcwdcalls++; 530266077Sdes if (disablecwd) 531266077Sdes return (ENODEV); 532266077Sdes if (uap->buflen < 2) 533266077Sdes return (EINVAL); 534266077Sdes if (uap->buflen > MAXPATHLEN) 535266077Sdes uap->buflen = MAXPATHLEN; 536266077Sdes buf = bp = malloc(uap->buflen, M_TEMP, M_WAITOK); 537266077Sdes bp += uap->buflen - 1; 538266077Sdes *bp = '\0'; 539266077Sdes fdp = p->p_fd; 540266077Sdes slash_prefixed = 0; 541266077Sdes for (vp = fdp->fd_cdir; vp != fdp->fd_rdir && vp != rootvnode;) { 542266077Sdes if (vp->v_flag & VROOT) { 543266077Sdes if (vp->v_mount == NULL) /* forced unmount */ 544266077Sdes return (EBADF); 545266077Sdes vp = vp->v_mount->mnt_vnodecovered; 546266077Sdes continue; 547266077Sdes } 548266077Sdes if (vp->v_dd->v_id != vp->v_ddid) { 549266077Sdes numcwdfail1++; 550266077Sdes free(buf, M_TEMP); 551266077Sdes return (ENOTDIR); 552266077Sdes } 553266077Sdes ncp = TAILQ_FIRST(&vp->v_cache_dst); 554266077Sdes if (!ncp) { 555266077Sdes numcwdfail2++; 556266077Sdes free(buf, M_TEMP); 557266077Sdes return (ENOENT); 558266077Sdes } 559266077Sdes if (ncp->nc_dvp != vp->v_dd) { 560266077Sdes numcwdfail3++; 561266077Sdes free(buf, M_TEMP); 562266077Sdes return (EBADF); 563266077Sdes } 564266077Sdes for (i = ncp->nc_nlen - 1; i >= 0; i--) { 565266077Sdes if (bp == buf) { 566266077Sdes numcwdfail4++; 567266077Sdes free(buf, M_TEMP); 568266077Sdes return (ENOMEM); 569266077Sdes } 570266077Sdes *--bp = ncp->nc_name[i]; 571266077Sdes } 572266077Sdes if (bp == buf) { 573266077Sdes numcwdfail4++; 574266077Sdes free(buf, M_TEMP); 575266077Sdes return (ENOMEM); 576266077Sdes } 577266077Sdes *--bp = '/'; 578266077Sdes slash_prefixed = 1; 579266077Sdes vp = vp->v_dd; 580266077Sdes } 581266077Sdes if (!slash_prefixed) { 582266077Sdes if (bp == buf) { 583266077Sdes numcwdfail4++; 584266077Sdes free(buf, M_TEMP); 585266077Sdes return (ENOMEM); 586266077Sdes } 587266077Sdes *--bp = '/'; 588266077Sdes } 589266077Sdes numcwdfound++; 590266077Sdes error = copyout(bp, uap->buf, strlen(bp) + 1); 591266077Sdes free(buf, M_TEMP); 592266077Sdes return (error); 593266077Sdes} 594266077Sdes 595266077Sdes/* 596266077Sdes * Thus begins the fullpath magic. 597266077Sdes */ 598266077Sdes 599266077Sdes#undef STATNODE 600266077Sdes#define STATNODE(name) \ 601266077Sdes static u_int name; \ 602266077Sdes SYSCTL_UINT(_vfs_cache, OID_AUTO, name, CTLFLAG_RD, &name, 0, "") 603266077Sdes 604266077Sdesstatic int disablefullpath; 605266077SdesSYSCTL_INT(_debug, OID_AUTO, disablefullpath, CTLFLAG_RW, 606266077Sdes &disablefullpath, 0, ""); 607266077Sdes 608266077SdesSTATNODE(numfullpathcalls); 609266077SdesSTATNODE(numfullpathfail1); 610266077SdesSTATNODE(numfullpathfail2); 611266077SdesSTATNODE(numfullpathfail3); 612266077SdesSTATNODE(numfullpathfail4); 613266077SdesSTATNODE(numfullpathfound); 614266077Sdes 615266077Sdesint 616266077Sdestextvp_fullpath(struct proc *p, char **retbuf, char **retfreebuf) { 617266077Sdes char *bp, *buf; 618266077Sdes int i, slash_prefixed; 619266077Sdes struct filedesc *fdp; 620266077Sdes struct namecache *ncp; 621266077Sdes struct vnode *vp, *textvp; 622266077Sdes 623266077Sdes numfullpathcalls++; 624266077Sdes if (disablefullpath) 625266077Sdes return (ENODEV); 626266077Sdes textvp = p->p_textvp; 627266077Sdes if (textvp == NULL) 628266077Sdes return (EINVAL); 629266077Sdes buf = malloc(MAXPATHLEN, M_TEMP, M_WAITOK); 630266077Sdes bp = buf + MAXPATHLEN - 1; 631266077Sdes *bp = '\0'; 632266077Sdes fdp = p->p_fd; 633266077Sdes slash_prefixed = 0; 634266077Sdes for (vp = textvp; vp != fdp->fd_rdir && vp != rootvnode;) { 635266077Sdes if (vp->v_flag & VROOT) { 636266077Sdes if (vp->v_mount == NULL) { /* forced unmount */ 637266077Sdes free(buf, M_TEMP); 638266077Sdes return (EBADF); 639266077Sdes } 640266077Sdes vp = vp->v_mount->mnt_vnodecovered; 641266077Sdes continue; 642266077Sdes } 643266077Sdes if (vp != textvp && vp->v_dd->v_id != vp->v_ddid) { 644266077Sdes numfullpathfail1++; 645266077Sdes free(buf, M_TEMP); 646266077Sdes return (ENOTDIR); 647266077Sdes } 648266077Sdes ncp = TAILQ_FIRST(&vp->v_cache_dst); 649266077Sdes if (!ncp) { 650266077Sdes numfullpathfail2++; 651266077Sdes free(buf, M_TEMP); 652266077Sdes return (ENOENT); 653266077Sdes } 654266077Sdes if (vp != textvp && ncp->nc_dvp != vp->v_dd) { 655266077Sdes numfullpathfail3++; 656266077Sdes free(buf, M_TEMP); 657266077Sdes return (EBADF); 658266077Sdes } 659266077Sdes for (i = ncp->nc_nlen - 1; i >= 0; i--) { 660266077Sdes if (bp == buf) { 661266077Sdes numfullpathfail4++; 662266077Sdes free(buf, M_TEMP); 663266077Sdes return (ENOMEM); 664266077Sdes } 665266077Sdes *--bp = ncp->nc_name[i]; 666266077Sdes } 667266077Sdes if (bp == buf) { 668266077Sdes numfullpathfail4++; 669266077Sdes free(buf, M_TEMP); 670266077Sdes return (ENOMEM); 671266077Sdes } 672266077Sdes *--bp = '/'; 673266077Sdes slash_prefixed = 1; 674266077Sdes vp = ncp->nc_dvp; 675266077Sdes } 676266077Sdes if (!slash_prefixed) { 677266077Sdes if (bp == buf) { 678266077Sdes numfullpathfail4++; 679266077Sdes free(buf, M_TEMP); 680266077Sdes return (ENOMEM); 681266077Sdes } 682266077Sdes *--bp = '/'; 683266077Sdes } 684266077Sdes numfullpathfound++; 685266077Sdes *retbuf = bp; 686266077Sdes *retfreebuf = buf; 687266077Sdes return (0); 688266077Sdes} 689266077Sdes