Remove the priority part of the priority|flags argument to tsleep(). Only
[dragonfly.git] / sys / kern / vfs_cache.c
1 /*
2  * Copyright (c) 1989, 1993, 1995
3  *      The Regents of the University of California.  All rights reserved.
4  *
5  * This code is derived from software contributed to Berkeley by
6  * Poul-Henning Kamp of the FreeBSD Project.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
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 the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. All advertising materials mentioning features or use of this software
17  *    must display the following acknowledgement:
18  *      This product includes software developed by the University of
19  *      California, Berkeley and its contributors.
20  * 4. Neither the name of the University nor the names of its contributors
21  *    may be used to endorse or promote products derived from this software
22  *    without specific prior written permission.
23  *
24  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34  * SUCH DAMAGE.
35  *
36  *      @(#)vfs_cache.c 8.5 (Berkeley) 3/22/95
37  * $FreeBSD: src/sys/kern/vfs_cache.c,v 1.42.2.6 2001/10/05 20:07:03 dillon Exp $
38  * $DragonFly: src/sys/kern/vfs_cache.c,v 1.4 2003/06/25 03:55:57 dillon Exp $
39  */
40
41 #include <sys/param.h>
42 #include <sys/systm.h>
43 #include <sys/kernel.h>
44 #include <sys/sysctl.h>
45 #include <sys/mount.h>
46 #include <sys/vnode.h>
47 #include <sys/malloc.h>
48 #include <sys/sysproto.h>
49 #include <sys/proc.h>
50 #include <sys/namei.h>
51 #include <sys/filedesc.h>
52 #include <sys/fnv_hash.h>
53
54 /*
55  * This structure describes the elements in the cache of recent
56  * names looked up by namei.
57  */
58
59 struct  namecache {
60         LIST_ENTRY(namecache) nc_hash;  /* hash chain */
61         LIST_ENTRY(namecache) nc_src;   /* source vnode list */
62         TAILQ_ENTRY(namecache) nc_dst;  /* destination vnode list */
63         struct  vnode *nc_dvp;          /* vnode of parent of name */
64         struct  vnode *nc_vp;           /* vnode the name refers to */
65         u_char  nc_flag;                /* flag bits */
66         u_char  nc_nlen;                /* length of name */
67         char    nc_name[0];             /* segment name */
68 };
69
70 /*
71  * Name caching works as follows:
72  *
73  * Names found by directory scans are retained in a cache
74  * for future reference.  It is managed LRU, so frequently
75  * used names will hang around.  Cache is indexed by hash value
76  * obtained from (vp, name) where vp refers to the directory
77  * containing name.
78  *
79  * If it is a "negative" entry, (i.e. for a name that is known NOT to
80  * exist) the vnode pointer will be NULL.
81  *
82  * Upon reaching the last segment of a path, if the reference
83  * is for DELETE, or NOCACHE is set (rewrite), and the
84  * name is located in the cache, it will be dropped.
85  */
86
87 /*
88  * Structures associated with name cacheing.
89  */
90 #define NCHHASH(hash) \
91         (&nchashtbl[(hash) & nchash])
92 static LIST_HEAD(nchashhead, namecache) *nchashtbl;     /* Hash Table */
93 static TAILQ_HEAD(, namecache) ncneg;   /* Hash Table */
94 static u_long   nchash;                 /* size of hash table */
95 SYSCTL_ULONG(_debug, OID_AUTO, nchash, CTLFLAG_RD, &nchash, 0, "");
96 static u_long   ncnegfactor = 16;       /* ratio of negative entries */
97 SYSCTL_ULONG(_debug, OID_AUTO, ncnegfactor, CTLFLAG_RW, &ncnegfactor, 0, "");
98 static u_long   numneg;         /* number of cache entries allocated */
99 SYSCTL_ULONG(_debug, OID_AUTO, numneg, CTLFLAG_RD, &numneg, 0, "");
100 static u_long   numcache;               /* number of cache entries allocated */
101 SYSCTL_ULONG(_debug, OID_AUTO, numcache, CTLFLAG_RD, &numcache, 0, "");
102 struct  nchstats nchstats;              /* cache effectiveness statistics */
103
104 static int      doingcache = 1;         /* 1 => enable the cache */
105 SYSCTL_INT(_debug, OID_AUTO, vfscache, CTLFLAG_RW, &doingcache, 0, "");
106 SYSCTL_INT(_debug, OID_AUTO, vnsize, CTLFLAG_RD, 0, sizeof(struct vnode), "");
107 SYSCTL_INT(_debug, OID_AUTO, ncsize, CTLFLAG_RD, 0, sizeof(struct namecache), "");
108
109 /*
110  * The new name cache statistics
111  */
112 SYSCTL_NODE(_vfs, OID_AUTO, cache, CTLFLAG_RW, 0, "Name cache statistics");
113 #define STATNODE(mode, name, var) \
114         SYSCTL_ULONG(_vfs_cache, OID_AUTO, name, mode, var, 0, "");
115 STATNODE(CTLFLAG_RD, numneg, &numneg);
116 STATNODE(CTLFLAG_RD, numcache, &numcache);
117 static u_long numcalls; STATNODE(CTLFLAG_RD, numcalls, &numcalls);
118 static u_long dothits; STATNODE(CTLFLAG_RD, dothits, &dothits);
119 static u_long dotdothits; STATNODE(CTLFLAG_RD, dotdothits, &dotdothits);
120 static u_long numchecks; STATNODE(CTLFLAG_RD, numchecks, &numchecks);
121 static u_long nummiss; STATNODE(CTLFLAG_RD, nummiss, &nummiss);
122 static u_long nummisszap; STATNODE(CTLFLAG_RD, nummisszap, &nummisszap);
123 static u_long numposzaps; STATNODE(CTLFLAG_RD, numposzaps, &numposzaps);
124 static u_long numposhits; STATNODE(CTLFLAG_RD, numposhits, &numposhits);
125 static u_long numnegzaps; STATNODE(CTLFLAG_RD, numnegzaps, &numnegzaps);
126 static u_long numneghits; STATNODE(CTLFLAG_RD, numneghits, &numneghits);
127
128
129 static void cache_zap __P((struct namecache *ncp));
130
131 MALLOC_DEFINE(M_VFSCACHE, "vfscache", "VFS name cache entries");
132
133 /*
134  * Flags in namecache.nc_flag
135  */
136 #define NCF_WHITE       1
137 /*
138  * Delete an entry from its hash list and move it to the front
139  * of the LRU list for immediate reuse.
140  */
141 static void
142 cache_zap(ncp)
143         struct namecache *ncp;
144 {
145         LIST_REMOVE(ncp, nc_hash);
146         LIST_REMOVE(ncp, nc_src);
147         if (LIST_EMPTY(&ncp->nc_dvp->v_cache_src)) 
148                 vdrop(ncp->nc_dvp);
149         if (ncp->nc_vp) {
150                 TAILQ_REMOVE(&ncp->nc_vp->v_cache_dst, ncp, nc_dst);
151         } else {
152                 TAILQ_REMOVE(&ncneg, ncp, nc_dst);
153                 numneg--;
154         }
155         numcache--;
156         free(ncp, M_VFSCACHE);
157 }
158
159 /*
160  * Lookup an entry in the cache
161  *
162  * We don't do this if the segment name is long, simply so the cache
163  * can avoid holding long names (which would either waste space, or
164  * add greatly to the complexity).
165  *
166  * Lookup is called with dvp pointing to the directory to search,
167  * cnp pointing to the name of the entry being sought. If the lookup
168  * succeeds, the vnode is returned in *vpp, and a status of -1 is
169  * returned. If the lookup determines that the name does not exist
170  * (negative cacheing), a status of ENOENT is returned. If the lookup
171  * fails, a status of zero is returned.
172  */
173
174 int
175 cache_lookup(dvp, vpp, cnp)
176         struct vnode *dvp;
177         struct vnode **vpp;
178         struct componentname *cnp;
179 {
180         struct namecache *ncp;
181         u_int32_t hash;
182
183         if (!doingcache) {
184                 cnp->cn_flags &= ~MAKEENTRY;
185                 return (0);
186         }
187
188         numcalls++;
189
190         if (cnp->cn_nameptr[0] == '.') {
191                 if (cnp->cn_namelen == 1) {
192                         *vpp = dvp;
193                         dothits++;
194                         return (-1);
195                 }
196                 if (cnp->cn_namelen == 2 && cnp->cn_nameptr[1] == '.') {
197                         dotdothits++;
198                         if (dvp->v_dd->v_id != dvp->v_ddid ||
199                             (cnp->cn_flags & MAKEENTRY) == 0) {
200                                 dvp->v_ddid = 0;
201                                 return (0);
202                         }
203                         *vpp = dvp->v_dd;
204                         return (-1);
205                 }
206         }
207
208         hash = fnv_32_buf(cnp->cn_nameptr, cnp->cn_namelen, FNV1_32_INIT);
209         hash = fnv_32_buf(&dvp->v_id, sizeof(dvp->v_id), hash);
210         LIST_FOREACH(ncp, (NCHHASH(hash)), nc_hash) {
211                 numchecks++;
212                 if (ncp->nc_dvp == dvp && ncp->nc_nlen == cnp->cn_namelen &&
213                     !bcmp(ncp->nc_name, cnp->cn_nameptr, ncp->nc_nlen))
214                         break;
215         }
216
217         /* We failed to find an entry */
218         if (ncp == 0) {
219                 if ((cnp->cn_flags & MAKEENTRY) == 0) {
220                         nummisszap++;
221                 } else {
222                         nummiss++;
223                 }
224                 nchstats.ncs_miss++;
225                 return (0);
226         }
227
228         /* We don't want to have an entry, so dump it */
229         if ((cnp->cn_flags & MAKEENTRY) == 0) {
230                 numposzaps++;
231                 nchstats.ncs_badhits++;
232                 cache_zap(ncp);
233                 return (0);
234         }
235
236         /* We found a "positive" match, return the vnode */
237         if (ncp->nc_vp) {
238                 numposhits++;
239                 nchstats.ncs_goodhits++;
240                 *vpp = ncp->nc_vp;
241                 return (-1);
242         }
243
244         /* We found a negative match, and want to create it, so purge */
245         if (cnp->cn_nameiop == CREATE) {
246                 numnegzaps++;
247                 nchstats.ncs_badhits++;
248                 cache_zap(ncp);
249                 return (0);
250         }
251
252         numneghits++;
253         /*
254          * We found a "negative" match, ENOENT notifies client of this match.
255          * The nc_vpid field records whether this is a whiteout.
256          */
257         TAILQ_REMOVE(&ncneg, ncp, nc_dst);
258         TAILQ_INSERT_TAIL(&ncneg, ncp, nc_dst);
259         nchstats.ncs_neghits++;
260         if (ncp->nc_flag & NCF_WHITE)
261                 cnp->cn_flags |= ISWHITEOUT;
262         return (ENOENT);
263 }
264
265 /*
266  * Add an entry to the cache.
267  */
268 void
269 cache_enter(dvp, vp, cnp)
270         struct vnode *dvp;
271         struct vnode *vp;
272         struct componentname *cnp;
273 {
274         struct namecache *ncp;
275         struct nchashhead *ncpp;
276         u_int32_t hash;
277         int len;
278
279         if (!doingcache)
280                 return;
281
282         if (cnp->cn_nameptr[0] == '.') {
283                 if (cnp->cn_namelen == 1) {
284                         return;
285                 }
286                 if (cnp->cn_namelen == 2 && cnp->cn_nameptr[1] == '.') {
287                         if (vp) {
288                                 dvp->v_dd = vp;
289                                 dvp->v_ddid = vp->v_id;
290                         } else {
291                                 dvp->v_dd = dvp;
292                                 dvp->v_ddid = 0;
293                         }
294                         return;
295                 }
296         }
297          
298         ncp = (struct namecache *)
299                 malloc(sizeof *ncp + cnp->cn_namelen, M_VFSCACHE, M_WAITOK);
300         bzero((char *)ncp, sizeof *ncp);
301         numcache++;
302         if (!vp) {
303                 numneg++;
304                 ncp->nc_flag = cnp->cn_flags & ISWHITEOUT ? NCF_WHITE : 0;
305         } else if (vp->v_type == VDIR) {
306                 vp->v_dd = dvp;
307                 vp->v_ddid = dvp->v_id;
308         }
309
310         /*
311          * Fill in cache info, if vp is NULL this is a "negative" cache entry.
312          * For negative entries, we have to record whether it is a whiteout.
313          * the whiteout flag is stored in the nc_vpid field which is
314          * otherwise unused.
315          */
316         ncp->nc_vp = vp;
317         ncp->nc_dvp = dvp;
318         len = ncp->nc_nlen = cnp->cn_namelen;
319         hash = fnv_32_buf(cnp->cn_nameptr, len, FNV1_32_INIT);
320         bcopy(cnp->cn_nameptr, ncp->nc_name, len);
321         hash = fnv_32_buf(&dvp->v_id, sizeof(dvp->v_id), hash);
322         ncpp = NCHHASH(hash);
323         LIST_INSERT_HEAD(ncpp, ncp, nc_hash);
324         if (LIST_EMPTY(&dvp->v_cache_src))
325                 vhold(dvp);
326         LIST_INSERT_HEAD(&dvp->v_cache_src, ncp, nc_src);
327         if (vp) {
328                 TAILQ_INSERT_HEAD(&vp->v_cache_dst, ncp, nc_dst);
329         } else {
330                 TAILQ_INSERT_TAIL(&ncneg, ncp, nc_dst);
331         }
332         if (numneg * ncnegfactor > numcache) {
333                 ncp = TAILQ_FIRST(&ncneg);
334                 cache_zap(ncp);
335         }
336 }
337
338 /*
339  * Name cache initialization, from vfs_init() when we are booting
340  */
341 void
342 nchinit()
343 {
344
345         TAILQ_INIT(&ncneg);
346         nchashtbl = hashinit(desiredvnodes*2, M_VFSCACHE, &nchash);
347 }
348
349 /*
350  * Invalidate all entries to a particular vnode.
351  *
352  * Remove all entries in the namecache relating to this vnode and
353  * change the v_id.  We take the v_id from a global counter, since
354  * it becomes a handy sequence number in crash-dumps that way.
355  * No valid vnode will ever have (v_id == 0).
356  *
357  * XXX: Only time and the size of v_id prevents this from failing:
358  * XXX: In theory we should hunt down all (struct vnode*, v_id)
359  * XXX: soft references and nuke them, at least on the global
360  * XXX: v_id wraparound.  The period of resistance can be extended
361  * XXX: by incrementing each vnodes v_id individually instead of
362  * XXX: using the global v_id.
363  */
364
365 void
366 cache_purge(vp)
367         struct vnode *vp;
368 {
369         static u_long nextid;
370
371         while (!LIST_EMPTY(&vp->v_cache_src)) 
372                 cache_zap(LIST_FIRST(&vp->v_cache_src));
373         while (!TAILQ_EMPTY(&vp->v_cache_dst)) 
374                 cache_zap(TAILQ_FIRST(&vp->v_cache_dst));
375
376         do
377                 nextid++;
378         while (nextid == vp->v_id || !nextid);
379         vp->v_id = nextid;
380         vp->v_dd = vp;
381         vp->v_ddid = 0;
382 }
383
384 /*
385  * Flush all entries referencing a particular filesystem.
386  *
387  * Since we need to check it anyway, we will flush all the invalid
388  * entries at the same time.
389  */
390 void
391 cache_purgevfs(mp)
392         struct mount *mp;
393 {
394         struct nchashhead *ncpp;
395         struct namecache *ncp, *nnp;
396
397         /* Scan hash tables for applicable entries */
398         for (ncpp = &nchashtbl[nchash]; ncpp >= nchashtbl; ncpp--) {
399                 for (ncp = LIST_FIRST(ncpp); ncp != 0; ncp = nnp) {
400                         nnp = LIST_NEXT(ncp, nc_hash);
401                         if (ncp->nc_dvp->v_mount == mp) {
402                                 cache_zap(ncp);
403                         }
404                 }
405         }
406 }
407
408 /*
409  * cache_leaf_test()
410  *
411  *      Test whether this (directory) vnode's namei cache entry contains
412  *      subdirectories or not.  Used to determine whether the directory is
413  *      a leaf in the namei cache or not.  Note: the directory may still
414  *      contain files in the namei cache.
415  *
416  *      Returns 0 if the directory is a leaf, -1 if it isn't.
417  */
418 int
419 cache_leaf_test(struct vnode *vp)
420 {
421         struct namecache *ncpc;
422
423         for (ncpc = LIST_FIRST(&vp->v_cache_src);
424              ncpc != NULL;
425              ncpc = LIST_NEXT(ncpc, nc_src)
426         ) {
427                 if (ncpc->nc_vp != NULL && ncpc->nc_vp->v_type == VDIR)
428                         return(-1);
429         }
430         return(0);
431 }
432
433 /*
434  * Perform canonical checks and cache lookup and pass on to filesystem
435  * through the vop_cachedlookup only if needed.
436  */
437
438 int
439 vfs_cache_lookup(ap)
440         struct vop_lookup_args /* {
441                 struct vnode *a_dvp;
442                 struct vnode **a_vpp;
443                 struct componentname *a_cnp;
444         } */ *ap;
445 {
446         struct vnode *dvp, *vp;
447         int lockparent;
448         int error;
449         struct vnode **vpp = ap->a_vpp;
450         struct componentname *cnp = ap->a_cnp;
451         struct ucred *cred = cnp->cn_cred;
452         int flags = cnp->cn_flags;
453         struct thread *td = cnp->cn_td;
454         u_long vpid;    /* capability number of vnode */
455
456         *vpp = NULL;
457         dvp = ap->a_dvp;
458         lockparent = flags & LOCKPARENT;
459
460         if (dvp->v_type != VDIR)
461                 return (ENOTDIR);
462
463         if ((flags & ISLASTCN) && (dvp->v_mount->mnt_flag & MNT_RDONLY) &&
464             (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME))
465                 return (EROFS);
466
467         error = VOP_ACCESS(dvp, VEXEC, cred, td);
468
469         if (error)
470                 return (error);
471
472         error = cache_lookup(dvp, vpp, cnp);
473
474         if (!error) 
475                 return (VOP_CACHEDLOOKUP(dvp, vpp, cnp));
476
477         if (error == ENOENT)
478                 return (error);
479
480         vp = *vpp;
481         vpid = vp->v_id;
482         cnp->cn_flags &= ~PDIRUNLOCK;
483         if (dvp == vp) {   /* lookup on "." */
484                 VREF(vp);
485                 error = 0;
486         } else if (flags & ISDOTDOT) {
487                 VOP_UNLOCK(dvp, 0, td);
488                 cnp->cn_flags |= PDIRUNLOCK;
489                 error = vget(vp, LK_EXCLUSIVE, td);
490                 if (!error && lockparent && (flags & ISLASTCN)) {
491                         if ((error = vn_lock(dvp, LK_EXCLUSIVE, td)) == 0)
492                                 cnp->cn_flags &= ~PDIRUNLOCK;
493                 }
494         } else {
495                 error = vget(vp, LK_EXCLUSIVE, td);
496                 if (!lockparent || error || !(flags & ISLASTCN)) {
497                         VOP_UNLOCK(dvp, 0, td);
498                         cnp->cn_flags |= PDIRUNLOCK;
499                 }
500         }
501         /*
502          * Check that the capability number did not change
503          * while we were waiting for the lock.
504          */
505         if (!error) {
506                 if (vpid == vp->v_id)
507                         return (0);
508                 vput(vp);
509                 if (lockparent && dvp != vp && (flags & ISLASTCN)) {
510                         VOP_UNLOCK(dvp, 0, td);
511                         cnp->cn_flags |= PDIRUNLOCK;
512                 }
513         }
514         if (cnp->cn_flags & PDIRUNLOCK) {
515                 error = vn_lock(dvp, LK_EXCLUSIVE, td);
516                 if (error)
517                         return (error);
518                 cnp->cn_flags &= ~PDIRUNLOCK;
519         }
520         return (VOP_CACHEDLOOKUP(dvp, vpp, cnp));
521 }
522
523
524 #ifndef _SYS_SYSPROTO_H_
525 struct  __getcwd_args {
526         u_char  *buf;
527         u_int   buflen;
528 };
529 #endif
530
531 static int disablecwd;
532 SYSCTL_INT(_debug, OID_AUTO, disablecwd, CTLFLAG_RW, &disablecwd, 0, "");
533
534 static u_long numcwdcalls; STATNODE(CTLFLAG_RD, numcwdcalls, &numcwdcalls);
535 static u_long numcwdfail1; STATNODE(CTLFLAG_RD, numcwdfail1, &numcwdfail1);
536 static u_long numcwdfail2; STATNODE(CTLFLAG_RD, numcwdfail2, &numcwdfail2);
537 static u_long numcwdfail3; STATNODE(CTLFLAG_RD, numcwdfail3, &numcwdfail3);
538 static u_long numcwdfail4; STATNODE(CTLFLAG_RD, numcwdfail4, &numcwdfail4);
539 static u_long numcwdfound; STATNODE(CTLFLAG_RD, numcwdfound, &numcwdfound);
540
541 int
542 __getcwd(struct __getcwd_args *uap)
543 {
544         struct proc *p = curproc;
545         char *bp, *buf;
546         int error, i, slash_prefixed;
547         struct filedesc *fdp;
548         struct namecache *ncp;
549         struct vnode *vp;
550
551         numcwdcalls++;
552         if (disablecwd)
553                 return (ENODEV);
554         if (uap->buflen < 2)
555                 return (EINVAL);
556         if (uap->buflen > MAXPATHLEN)
557                 uap->buflen = MAXPATHLEN;
558         buf = bp = malloc(uap->buflen, M_TEMP, M_WAITOK);
559         bp += uap->buflen - 1;
560         *bp = '\0';
561         fdp = p->p_fd;
562         slash_prefixed = 0;
563         for (vp = fdp->fd_cdir; vp != fdp->fd_rdir && vp != rootvnode;) {
564                 if (vp->v_flag & VROOT) {
565                         if (vp->v_mount == NULL) {      /* forced unmount */
566                                 free(buf, M_TEMP);
567                                 return (EBADF);
568                         }
569                         vp = vp->v_mount->mnt_vnodecovered;
570                         continue;
571                 }
572                 if (vp->v_dd->v_id != vp->v_ddid) {
573                         numcwdfail1++;
574                         free(buf, M_TEMP);
575                         return (ENOTDIR);
576                 }
577                 ncp = TAILQ_FIRST(&vp->v_cache_dst);
578                 if (!ncp) {
579                         numcwdfail2++;
580                         free(buf, M_TEMP);
581                         return (ENOENT);
582                 }
583                 if (ncp->nc_dvp != vp->v_dd) {
584                         numcwdfail3++;
585                         free(buf, M_TEMP);
586                         return (EBADF);
587                 }
588                 for (i = ncp->nc_nlen - 1; i >= 0; i--) {
589                         if (bp == buf) {
590                                 numcwdfail4++;
591                                 free(buf, M_TEMP);
592                                 return (ENOMEM);
593                         }
594                         *--bp = ncp->nc_name[i];
595                 }
596                 if (bp == buf) {
597                         numcwdfail4++;
598                         free(buf, M_TEMP);
599                         return (ENOMEM);
600                 }
601                 *--bp = '/';
602                 slash_prefixed = 1;
603                 vp = vp->v_dd;
604         }
605         if (!slash_prefixed) {
606                 if (bp == buf) {
607                         numcwdfail4++;
608                         free(buf, M_TEMP);
609                         return (ENOMEM);
610                 }
611                 *--bp = '/';
612         }
613         numcwdfound++;
614         error = copyout(bp, uap->buf, strlen(bp) + 1);
615         free(buf, M_TEMP);
616         return (error);
617 }
618
619 /*
620  * Thus begins the fullpath magic.
621  */
622
623 #undef STATNODE
624 #define STATNODE(name)                                                  \
625         static u_int name;                                              \
626         SYSCTL_UINT(_vfs_cache, OID_AUTO, name, CTLFLAG_RD, &name, 0, "")
627
628 static int disablefullpath;
629 SYSCTL_INT(_debug, OID_AUTO, disablefullpath, CTLFLAG_RW,
630     &disablefullpath, 0, "");
631
632 STATNODE(numfullpathcalls);
633 STATNODE(numfullpathfail1);
634 STATNODE(numfullpathfail2);
635 STATNODE(numfullpathfail3);
636 STATNODE(numfullpathfail4);
637 STATNODE(numfullpathfound);
638
639 int
640 textvp_fullpath(struct proc *p, char **retbuf, char **retfreebuf) {
641         char *bp, *buf;
642         int i, slash_prefixed;
643         struct filedesc *fdp;
644         struct namecache *ncp;
645         struct vnode *vp, *textvp;
646
647         numfullpathcalls++;
648         if (disablefullpath)
649                 return (ENODEV);
650         textvp = p->p_textvp;
651         if (textvp == NULL)
652                 return (EINVAL);
653         buf = malloc(MAXPATHLEN, M_TEMP, M_WAITOK);
654         bp = buf + MAXPATHLEN - 1;
655         *bp = '\0';
656         fdp = p->p_fd;
657         slash_prefixed = 0;
658         for (vp = textvp; vp != fdp->fd_rdir && vp != rootvnode;) {
659                 if (vp->v_flag & VROOT) {
660                         if (vp->v_mount == NULL) {      /* forced unmount */
661                                 free(buf, M_TEMP);
662                                 return (EBADF);
663                         }
664                         vp = vp->v_mount->mnt_vnodecovered;
665                         continue;
666                 }
667                 if (vp != textvp && vp->v_dd->v_id != vp->v_ddid) {
668                         numfullpathfail1++;
669                         free(buf, M_TEMP);
670                         return (ENOTDIR);
671                 }
672                 ncp = TAILQ_FIRST(&vp->v_cache_dst);
673                 if (!ncp) {
674                         numfullpathfail2++;
675                         free(buf, M_TEMP);
676                         return (ENOENT);
677                 }
678                 if (vp != textvp && ncp->nc_dvp != vp->v_dd) {
679                         numfullpathfail3++;
680                         free(buf, M_TEMP);
681                         return (EBADF);
682                 }
683                 for (i = ncp->nc_nlen - 1; i >= 0; i--) {
684                         if (bp == buf) {
685                                 numfullpathfail4++;
686                                 free(buf, M_TEMP);
687                                 return (ENOMEM);
688                         }
689                         *--bp = ncp->nc_name[i];
690                 }
691                 if (bp == buf) {
692                         numfullpathfail4++;
693                         free(buf, M_TEMP);
694                         return (ENOMEM);
695                 }
696                 *--bp = '/';
697                 slash_prefixed = 1;
698                 vp = ncp->nc_dvp;
699         }
700         if (!slash_prefixed) {
701                 if (bp == buf) {
702                         numfullpathfail4++;
703                         free(buf, M_TEMP);
704                         return (ENOMEM);
705                 }
706                 *--bp = '/';
707         }
708         numfullpathfound++;
709         *retbuf = bp; 
710         *retfreebuf = buf;
711         return (0);
712 }