]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/kern/vfs_lookup.c
MFV: Import atf-0.20.
[FreeBSD/FreeBSD.git] / sys / kern / vfs_lookup.c
1 /*-
2  * Copyright (c) 1982, 1986, 1989, 1993
3  *      The Regents of the University of California.  All rights reserved.
4  * (c) UNIX System Laboratories, Inc.
5  * All or some portions of this file are derived from material licensed
6  * to the University of California by American Telephone and Telegraph
7  * Co. or Unix System Laboratories, Inc. and are reproduced herein with
8  * the permission of UNIX System Laboratories, Inc.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
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  * 4. Neither the name of the University nor the names of its contributors
19  *    may be used to endorse or promote products derived from this software
20  *    without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32  * SUCH DAMAGE.
33  *
34  *      @(#)vfs_lookup.c        8.4 (Berkeley) 2/16/94
35  */
36
37 #include <sys/cdefs.h>
38 __FBSDID("$FreeBSD$");
39
40 #include "opt_capsicum.h"
41 #include "opt_ktrace.h"
42
43 #include <sys/param.h>
44 #include <sys/systm.h>
45 #include <sys/kernel.h>
46 #include <sys/capability.h>
47 #include <sys/fcntl.h>
48 #include <sys/jail.h>
49 #include <sys/lock.h>
50 #include <sys/mutex.h>
51 #include <sys/namei.h>
52 #include <sys/vnode.h>
53 #include <sys/mount.h>
54 #include <sys/filedesc.h>
55 #include <sys/proc.h>
56 #include <sys/sdt.h>
57 #include <sys/syscallsubr.h>
58 #include <sys/sysctl.h>
59 #ifdef KTRACE
60 #include <sys/ktrace.h>
61 #endif
62
63 #include <security/audit/audit.h>
64 #include <security/mac/mac_framework.h>
65
66 #include <vm/uma.h>
67
68 #define NAMEI_DIAGNOSTIC 1
69 #undef NAMEI_DIAGNOSTIC
70
71 SDT_PROVIDER_DECLARE(vfs);
72 SDT_PROBE_DEFINE3(vfs, namei, lookup, entry, "struct vnode *", "char *",
73     "unsigned long");
74 SDT_PROBE_DEFINE2(vfs, namei, lookup, return, "int", "struct vnode *");
75
76 /*
77  * Allocation zone for namei
78  */
79 uma_zone_t namei_zone;
80 /*
81  * Placeholder vnode for mp traversal
82  */
83 static struct vnode *vp_crossmp;
84
85 static void
86 nameiinit(void *dummy __unused)
87 {
88
89         namei_zone = uma_zcreate("NAMEI", MAXPATHLEN, NULL, NULL, NULL, NULL,
90             UMA_ALIGN_PTR, 0);
91         getnewvnode("crossmp", NULL, &dead_vnodeops, &vp_crossmp);
92         vn_lock(vp_crossmp, LK_EXCLUSIVE);
93         VN_LOCK_ASHARE(vp_crossmp);
94         VOP_UNLOCK(vp_crossmp, 0);
95 }
96 SYSINIT(vfs, SI_SUB_VFS, SI_ORDER_SECOND, nameiinit, NULL);
97
98 static int lookup_shared = 1;
99 SYSCTL_INT(_vfs, OID_AUTO, lookup_shared, CTLFLAG_RW, &lookup_shared, 0,
100     "Enables/Disables shared locks for path name translation");
101 TUNABLE_INT("vfs.lookup_shared", &lookup_shared);
102
103 /*
104  * Convert a pathname into a pointer to a locked vnode.
105  *
106  * The FOLLOW flag is set when symbolic links are to be followed
107  * when they occur at the end of the name translation process.
108  * Symbolic links are always followed for all other pathname
109  * components other than the last.
110  *
111  * The segflg defines whether the name is to be copied from user
112  * space or kernel space.
113  *
114  * Overall outline of namei:
115  *
116  *      copy in name
117  *      get starting directory
118  *      while (!done && !error) {
119  *              call lookup to search path.
120  *              if symbolic link, massage name in buffer and continue
121  *      }
122  */
123 int
124 namei(struct nameidata *ndp)
125 {
126         struct filedesc *fdp;   /* pointer to file descriptor state */
127         char *cp;               /* pointer into pathname argument */
128         struct vnode *dp;       /* the directory we are searching */
129         struct iovec aiov;              /* uio for reading symbolic links */
130         struct uio auio;
131         int error, linklen;
132         struct componentname *cnp = &ndp->ni_cnd;
133         struct thread *td = cnp->cn_thread;
134         struct proc *p = td->td_proc;
135
136         ndp->ni_cnd.cn_cred = ndp->ni_cnd.cn_thread->td_ucred;
137         KASSERT(cnp->cn_cred && p, ("namei: bad cred/proc"));
138         KASSERT((cnp->cn_nameiop & (~OPMASK)) == 0,
139             ("namei: nameiop contaminated with flags"));
140         KASSERT((cnp->cn_flags & OPMASK) == 0,
141             ("namei: flags contaminated with nameiops"));
142         if (!lookup_shared)
143                 cnp->cn_flags &= ~LOCKSHARED;
144         fdp = p->p_fd;
145
146         /* We will set this ourselves if we need it. */
147         cnp->cn_flags &= ~TRAILINGSLASH;
148
149         /*
150          * Get a buffer for the name to be translated, and copy the
151          * name into the buffer.
152          */
153         if ((cnp->cn_flags & HASBUF) == 0)
154                 cnp->cn_pnbuf = uma_zalloc(namei_zone, M_WAITOK);
155         if (ndp->ni_segflg == UIO_SYSSPACE)
156                 error = copystr(ndp->ni_dirp, cnp->cn_pnbuf,
157                             MAXPATHLEN, (size_t *)&ndp->ni_pathlen);
158         else
159                 error = copyinstr(ndp->ni_dirp, cnp->cn_pnbuf,
160                             MAXPATHLEN, (size_t *)&ndp->ni_pathlen);
161
162         /*
163          * Don't allow empty pathnames.
164          */
165         if (!error && *cnp->cn_pnbuf == '\0')
166                 error = ENOENT;
167
168 #ifdef CAPABILITY_MODE
169         /*
170          * In capability mode, lookups must be "strictly relative" (i.e.
171          * not an absolute path, and not containing '..' components) to
172          * a real file descriptor, not the pseudo-descriptor AT_FDCWD.
173          */
174         if (error == 0 && IN_CAPABILITY_MODE(td) &&
175             (cnp->cn_flags & NOCAPCHECK) == 0) {
176                 ndp->ni_strictrelative = 1;
177                 if (ndp->ni_dirfd == AT_FDCWD) {
178 #ifdef KTRACE
179                         if (KTRPOINT(td, KTR_CAPFAIL))
180                                 ktrcapfail(CAPFAIL_LOOKUP, NULL, NULL);
181 #endif
182                         error = ECAPMODE;
183                 }
184         }
185 #endif
186         if (error) {
187                 uma_zfree(namei_zone, cnp->cn_pnbuf);
188 #ifdef DIAGNOSTIC
189                 cnp->cn_pnbuf = NULL;
190                 cnp->cn_nameptr = NULL;
191 #endif
192                 ndp->ni_vp = NULL;
193                 return (error);
194         }
195         ndp->ni_loopcnt = 0;
196 #ifdef KTRACE
197         if (KTRPOINT(td, KTR_NAMEI)) {
198                 KASSERT(cnp->cn_thread == curthread,
199                     ("namei not using curthread"));
200                 ktrnamei(cnp->cn_pnbuf);
201         }
202 #endif
203         /*
204          * Get starting point for the translation.
205          */
206         FILEDESC_SLOCK(fdp);
207         ndp->ni_rootdir = fdp->fd_rdir;
208         ndp->ni_topdir = fdp->fd_jdir;
209
210         /*
211          * If we are auditing the kernel pathname, save the user pathname.
212          */
213         if (cnp->cn_flags & AUDITVNODE1)
214                 AUDIT_ARG_UPATH1(td, ndp->ni_dirfd, cnp->cn_pnbuf);
215         if (cnp->cn_flags & AUDITVNODE2)
216                 AUDIT_ARG_UPATH2(td, ndp->ni_dirfd, cnp->cn_pnbuf);
217
218         dp = NULL;
219         if (cnp->cn_pnbuf[0] != '/') {
220                 if (ndp->ni_startdir != NULL) {
221                         dp = ndp->ni_startdir;
222                         error = 0;
223                 } else if (ndp->ni_dirfd != AT_FDCWD) {
224                         cap_rights_t rights;
225
226                         rights = ndp->ni_rightsneeded;
227                         cap_rights_set(&rights, CAP_LOOKUP);
228
229                         if (cnp->cn_flags & AUDITVNODE1)
230                                 AUDIT_ARG_ATFD1(ndp->ni_dirfd);
231                         if (cnp->cn_flags & AUDITVNODE2)
232                                 AUDIT_ARG_ATFD2(ndp->ni_dirfd);
233                         error = fgetvp_rights(td, ndp->ni_dirfd,
234                             &rights, &ndp->ni_filecaps, &dp);
235 #ifdef CAPABILITIES
236                         /*
237                          * If file descriptor doesn't have all rights,
238                          * all lookups relative to it must also be
239                          * strictly relative.
240                          */
241                         CAP_ALL(&rights);
242                         if (!cap_rights_contains(&ndp->ni_filecaps.fc_rights,
243                             &rights) ||
244                             ndp->ni_filecaps.fc_fcntls != CAP_FCNTL_ALL ||
245                             ndp->ni_filecaps.fc_nioctls != -1) {
246                                 ndp->ni_strictrelative = 1;
247                         }
248 #endif
249                 }
250                 if (error != 0 || dp != NULL) {
251                         FILEDESC_SUNLOCK(fdp);
252                         if (error == 0 && dp->v_type != VDIR) {
253                                 vrele(dp);
254                                 error = ENOTDIR;
255                         }
256                 }
257                 if (error) {
258                         uma_zfree(namei_zone, cnp->cn_pnbuf);
259 #ifdef DIAGNOSTIC
260                         cnp->cn_pnbuf = NULL;
261                         cnp->cn_nameptr = NULL;
262 #endif
263                         return (error);
264                 }
265         }
266         if (dp == NULL) {
267                 dp = fdp->fd_cdir;
268                 VREF(dp);
269                 FILEDESC_SUNLOCK(fdp);
270                 if (ndp->ni_startdir != NULL)
271                         vrele(ndp->ni_startdir);
272         }
273         SDT_PROBE(vfs, namei, lookup, entry, dp, cnp->cn_pnbuf,
274             cnp->cn_flags, 0, 0);
275         for (;;) {
276                 /*
277                  * Check if root directory should replace current directory.
278                  * Done at start of translation and after symbolic link.
279                  */
280                 cnp->cn_nameptr = cnp->cn_pnbuf;
281                 if (*(cnp->cn_nameptr) == '/') {
282                         vrele(dp);
283                         if (ndp->ni_strictrelative != 0) {
284 #ifdef KTRACE
285                                 if (KTRPOINT(curthread, KTR_CAPFAIL))
286                                         ktrcapfail(CAPFAIL_LOOKUP, NULL, NULL);
287 #endif
288                                 return (ENOTCAPABLE);
289                         }
290                         while (*(cnp->cn_nameptr) == '/') {
291                                 cnp->cn_nameptr++;
292                                 ndp->ni_pathlen--;
293                         }
294                         dp = ndp->ni_rootdir;
295                         VREF(dp);
296                 }
297                 ndp->ni_startdir = dp;
298                 error = lookup(ndp);
299                 if (error) {
300                         uma_zfree(namei_zone, cnp->cn_pnbuf);
301 #ifdef DIAGNOSTIC
302                         cnp->cn_pnbuf = NULL;
303                         cnp->cn_nameptr = NULL;
304 #endif
305                         SDT_PROBE(vfs, namei, lookup, return, error, NULL, 0,
306                             0, 0);
307                         return (error);
308                 }
309                 /*
310                  * If not a symbolic link, we're done.
311                  */
312                 if ((cnp->cn_flags & ISSYMLINK) == 0) {
313                         if ((cnp->cn_flags & (SAVENAME | SAVESTART)) == 0) {
314                                 uma_zfree(namei_zone, cnp->cn_pnbuf);
315 #ifdef DIAGNOSTIC
316                                 cnp->cn_pnbuf = NULL;
317                                 cnp->cn_nameptr = NULL;
318 #endif
319                         } else
320                                 cnp->cn_flags |= HASBUF;
321
322                         SDT_PROBE(vfs, namei, lookup, return, 0, ndp->ni_vp,
323                             0, 0, 0);
324                         return (0);
325                 }
326                 if (ndp->ni_loopcnt++ >= MAXSYMLINKS) {
327                         error = ELOOP;
328                         break;
329                 }
330 #ifdef MAC
331                 if ((cnp->cn_flags & NOMACCHECK) == 0) {
332                         error = mac_vnode_check_readlink(td->td_ucred,
333                             ndp->ni_vp);
334                         if (error)
335                                 break;
336                 }
337 #endif
338                 if (ndp->ni_pathlen > 1)
339                         cp = uma_zalloc(namei_zone, M_WAITOK);
340                 else
341                         cp = cnp->cn_pnbuf;
342                 aiov.iov_base = cp;
343                 aiov.iov_len = MAXPATHLEN;
344                 auio.uio_iov = &aiov;
345                 auio.uio_iovcnt = 1;
346                 auio.uio_offset = 0;
347                 auio.uio_rw = UIO_READ;
348                 auio.uio_segflg = UIO_SYSSPACE;
349                 auio.uio_td = td;
350                 auio.uio_resid = MAXPATHLEN;
351                 error = VOP_READLINK(ndp->ni_vp, &auio, cnp->cn_cred);
352                 if (error) {
353                         if (ndp->ni_pathlen > 1)
354                                 uma_zfree(namei_zone, cp);
355                         break;
356                 }
357                 linklen = MAXPATHLEN - auio.uio_resid;
358                 if (linklen == 0) {
359                         if (ndp->ni_pathlen > 1)
360                                 uma_zfree(namei_zone, cp);
361                         error = ENOENT;
362                         break;
363                 }
364                 if (linklen + ndp->ni_pathlen >= MAXPATHLEN) {
365                         if (ndp->ni_pathlen > 1)
366                                 uma_zfree(namei_zone, cp);
367                         error = ENAMETOOLONG;
368                         break;
369                 }
370                 if (ndp->ni_pathlen > 1) {
371                         bcopy(ndp->ni_next, cp + linklen, ndp->ni_pathlen);
372                         uma_zfree(namei_zone, cnp->cn_pnbuf);
373                         cnp->cn_pnbuf = cp;
374                 } else
375                         cnp->cn_pnbuf[linklen] = '\0';
376                 ndp->ni_pathlen += linklen;
377                 vput(ndp->ni_vp);
378                 dp = ndp->ni_dvp;
379         }
380         uma_zfree(namei_zone, cnp->cn_pnbuf);
381 #ifdef DIAGNOSTIC
382         cnp->cn_pnbuf = NULL;
383         cnp->cn_nameptr = NULL;
384 #endif
385         vput(ndp->ni_vp);
386         ndp->ni_vp = NULL;
387         vrele(ndp->ni_dvp);
388         SDT_PROBE(vfs, namei, lookup, return, error, NULL, 0, 0, 0);
389         return (error);
390 }
391
392 static int
393 compute_cn_lkflags(struct mount *mp, int lkflags, int cnflags)
394 {
395
396         if (mp == NULL || ((lkflags & LK_SHARED) &&
397             (!(mp->mnt_kern_flag & MNTK_LOOKUP_SHARED) ||
398             ((cnflags & ISDOTDOT) &&
399             (mp->mnt_kern_flag & MNTK_LOOKUP_EXCL_DOTDOT))))) {
400                 lkflags &= ~LK_SHARED;
401                 lkflags |= LK_EXCLUSIVE;
402         }
403         return (lkflags);
404 }
405
406 static __inline int
407 needs_exclusive_leaf(struct mount *mp, int flags)
408 {
409
410         /*
411          * Intermediate nodes can use shared locks, we only need to
412          * force an exclusive lock for leaf nodes.
413          */
414         if ((flags & (ISLASTCN | LOCKLEAF)) != (ISLASTCN | LOCKLEAF))
415                 return (0);
416
417         /* Always use exclusive locks if LOCKSHARED isn't set. */
418         if (!(flags & LOCKSHARED))
419                 return (1);
420
421         /*
422          * For lookups during open(), if the mount point supports
423          * extended shared operations, then use a shared lock for the
424          * leaf node, otherwise use an exclusive lock.
425          */
426         if ((flags & ISOPEN) != 0)
427                 return (!MNT_EXTENDED_SHARED(mp));
428
429         /*
430          * Lookup requests outside of open() that specify LOCKSHARED
431          * only need a shared lock on the leaf vnode.
432          */
433         return (0);
434 }
435
436 /*
437  * Search a pathname.
438  * This is a very central and rather complicated routine.
439  *
440  * The pathname is pointed to by ni_ptr and is of length ni_pathlen.
441  * The starting directory is taken from ni_startdir. The pathname is
442  * descended until done, or a symbolic link is encountered. The variable
443  * ni_more is clear if the path is completed; it is set to one if a
444  * symbolic link needing interpretation is encountered.
445  *
446  * The flag argument is LOOKUP, CREATE, RENAME, or DELETE depending on
447  * whether the name is to be looked up, created, renamed, or deleted.
448  * When CREATE, RENAME, or DELETE is specified, information usable in
449  * creating, renaming, or deleting a directory entry may be calculated.
450  * If flag has LOCKPARENT or'ed into it, the parent directory is returned
451  * locked. If flag has WANTPARENT or'ed into it, the parent directory is
452  * returned unlocked. Otherwise the parent directory is not returned. If
453  * the target of the pathname exists and LOCKLEAF is or'ed into the flag
454  * the target is returned locked, otherwise it is returned unlocked.
455  * When creating or renaming and LOCKPARENT is specified, the target may not
456  * be ".".  When deleting and LOCKPARENT is specified, the target may be ".".
457  *
458  * Overall outline of lookup:
459  *
460  * dirloop:
461  *      identify next component of name at ndp->ni_ptr
462  *      handle degenerate case where name is null string
463  *      if .. and crossing mount points and on mounted filesys, find parent
464  *      call VOP_LOOKUP routine for next component name
465  *          directory vnode returned in ni_dvp, unlocked unless LOCKPARENT set
466  *          component vnode returned in ni_vp (if it exists), locked.
467  *      if result vnode is mounted on and crossing mount points,
468  *          find mounted on vnode
469  *      if more components of name, do next level at dirloop
470  *      return the answer in ni_vp, locked if LOCKLEAF set
471  *          if LOCKPARENT set, return locked parent in ni_dvp
472  *          if WANTPARENT set, return unlocked parent in ni_dvp
473  */
474 int
475 lookup(struct nameidata *ndp)
476 {
477         char *cp;               /* pointer into pathname argument */
478         struct vnode *dp = 0;   /* the directory we are searching */
479         struct vnode *tdp;              /* saved dp */
480         struct mount *mp;               /* mount table entry */
481         struct prison *pr;
482         int docache;                    /* == 0 do not cache last component */
483         int wantparent;                 /* 1 => wantparent or lockparent flag */
484         int rdonly;                     /* lookup read-only flag bit */
485         int error = 0;
486         int dpunlocked = 0;             /* dp has already been unlocked */
487         struct componentname *cnp = &ndp->ni_cnd;
488         int lkflags_save;
489         int ni_dvp_unlocked;
490         
491         /*
492          * Setup: break out flag bits into variables.
493          */
494         ni_dvp_unlocked = 0;
495         wantparent = cnp->cn_flags & (LOCKPARENT | WANTPARENT);
496         KASSERT(cnp->cn_nameiop == LOOKUP || wantparent,
497             ("CREATE, DELETE, RENAME require LOCKPARENT or WANTPARENT."));
498         docache = (cnp->cn_flags & NOCACHE) ^ NOCACHE;
499         if (cnp->cn_nameiop == DELETE ||
500             (wantparent && cnp->cn_nameiop != CREATE &&
501              cnp->cn_nameiop != LOOKUP))
502                 docache = 0;
503         rdonly = cnp->cn_flags & RDONLY;
504         cnp->cn_flags &= ~ISSYMLINK;
505         ndp->ni_dvp = NULL;
506         /*
507          * We use shared locks until we hit the parent of the last cn then
508          * we adjust based on the requesting flags.
509          */
510         if (lookup_shared)
511                 cnp->cn_lkflags = LK_SHARED;
512         else
513                 cnp->cn_lkflags = LK_EXCLUSIVE;
514         dp = ndp->ni_startdir;
515         ndp->ni_startdir = NULLVP;
516         vn_lock(dp,
517             compute_cn_lkflags(dp->v_mount, cnp->cn_lkflags | LK_RETRY,
518             cnp->cn_flags));
519
520 dirloop:
521         /*
522          * Search a new directory.
523          *
524          * The last component of the filename is left accessible via
525          * cnp->cn_nameptr for callers that need the name. Callers needing
526          * the name set the SAVENAME flag. When done, they assume
527          * responsibility for freeing the pathname buffer.
528          */
529         cnp->cn_consume = 0;
530         for (cp = cnp->cn_nameptr; *cp != 0 && *cp != '/'; cp++)
531                 continue;
532         cnp->cn_namelen = cp - cnp->cn_nameptr;
533         if (cnp->cn_namelen > NAME_MAX) {
534                 error = ENAMETOOLONG;
535                 goto bad;
536         }
537 #ifdef NAMEI_DIAGNOSTIC
538         { char c = *cp;
539         *cp = '\0';
540         printf("{%s}: ", cnp->cn_nameptr);
541         *cp = c; }
542 #endif
543         ndp->ni_pathlen -= cnp->cn_namelen;
544         ndp->ni_next = cp;
545
546         /*
547          * Replace multiple slashes by a single slash and trailing slashes
548          * by a null.  This must be done before VOP_LOOKUP() because some
549          * fs's don't know about trailing slashes.  Remember if there were
550          * trailing slashes to handle symlinks, existing non-directories
551          * and non-existing files that won't be directories specially later.
552          */
553         while (*cp == '/' && (cp[1] == '/' || cp[1] == '\0')) {
554                 cp++;
555                 ndp->ni_pathlen--;
556                 if (*cp == '\0') {
557                         *ndp->ni_next = '\0';
558                         cnp->cn_flags |= TRAILINGSLASH;
559                 }
560         }
561         ndp->ni_next = cp;
562
563         cnp->cn_flags |= MAKEENTRY;
564         if (*cp == '\0' && docache == 0)
565                 cnp->cn_flags &= ~MAKEENTRY;
566         if (cnp->cn_namelen == 2 &&
567             cnp->cn_nameptr[1] == '.' && cnp->cn_nameptr[0] == '.')
568                 cnp->cn_flags |= ISDOTDOT;
569         else
570                 cnp->cn_flags &= ~ISDOTDOT;
571         if (*ndp->ni_next == 0)
572                 cnp->cn_flags |= ISLASTCN;
573         else
574                 cnp->cn_flags &= ~ISLASTCN;
575
576         if ((cnp->cn_flags & ISLASTCN) != 0 &&
577             cnp->cn_namelen == 1 && cnp->cn_nameptr[0] == '.' &&
578             (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME)) {
579                 error = EINVAL;
580                 goto bad;
581         }
582
583         /*
584          * Check for degenerate name (e.g. / or "")
585          * which is a way of talking about a directory,
586          * e.g. like "/." or ".".
587          */
588         if (cnp->cn_nameptr[0] == '\0') {
589                 if (dp->v_type != VDIR) {
590                         error = ENOTDIR;
591                         goto bad;
592                 }
593                 if (cnp->cn_nameiop != LOOKUP) {
594                         error = EISDIR;
595                         goto bad;
596                 }
597                 if (wantparent) {
598                         ndp->ni_dvp = dp;
599                         VREF(dp);
600                 }
601                 ndp->ni_vp = dp;
602
603                 if (cnp->cn_flags & AUDITVNODE1)
604                         AUDIT_ARG_VNODE1(dp);
605                 else if (cnp->cn_flags & AUDITVNODE2)
606                         AUDIT_ARG_VNODE2(dp);
607
608                 if (!(cnp->cn_flags & (LOCKPARENT | LOCKLEAF)))
609                         VOP_UNLOCK(dp, 0);
610                 /* XXX This should probably move to the top of function. */
611                 if (cnp->cn_flags & SAVESTART)
612                         panic("lookup: SAVESTART");
613                 goto success;
614         }
615
616         /*
617          * Handle "..": five special cases.
618          * 0. If doing a capability lookup, return ENOTCAPABLE (this is a
619          *    fairly conservative design choice, but it's the only one that we
620          *    are satisfied guarantees the property we're looking for).
621          * 1. Return an error if this is the last component of
622          *    the name and the operation is DELETE or RENAME.
623          * 2. If at root directory (e.g. after chroot)
624          *    or at absolute root directory
625          *    then ignore it so can't get out.
626          * 3. If this vnode is the root of a mounted
627          *    filesystem, then replace it with the
628          *    vnode which was mounted on so we take the
629          *    .. in the other filesystem.
630          * 4. If the vnode is the top directory of
631          *    the jail or chroot, don't let them out.
632          */
633         if (cnp->cn_flags & ISDOTDOT) {
634                 if (ndp->ni_strictrelative != 0) {
635 #ifdef KTRACE
636                         if (KTRPOINT(curthread, KTR_CAPFAIL))
637                                 ktrcapfail(CAPFAIL_LOOKUP, NULL, NULL);
638 #endif
639                         error = ENOTCAPABLE;
640                         goto bad;
641                 }
642                 if ((cnp->cn_flags & ISLASTCN) != 0 &&
643                     (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME)) {
644                         error = EINVAL;
645                         goto bad;
646                 }
647                 for (;;) {
648                         for (pr = cnp->cn_cred->cr_prison; pr != NULL;
649                              pr = pr->pr_parent)
650                                 if (dp == pr->pr_root)
651                                         break;
652                         if (dp == ndp->ni_rootdir || 
653                             dp == ndp->ni_topdir || 
654                             dp == rootvnode ||
655                             pr != NULL ||
656                             ((dp->v_vflag & VV_ROOT) != 0 &&
657                              (cnp->cn_flags & NOCROSSMOUNT) != 0)) {
658                                 ndp->ni_dvp = dp;
659                                 ndp->ni_vp = dp;
660                                 VREF(dp);
661                                 goto nextname;
662                         }
663                         if ((dp->v_vflag & VV_ROOT) == 0)
664                                 break;
665                         if (dp->v_iflag & VI_DOOMED) {  /* forced unmount */
666                                 error = ENOENT;
667                                 goto bad;
668                         }
669                         tdp = dp;
670                         dp = dp->v_mount->mnt_vnodecovered;
671                         VREF(dp);
672                         vput(tdp);
673                         vn_lock(dp,
674                             compute_cn_lkflags(dp->v_mount, cnp->cn_lkflags |
675                             LK_RETRY, ISDOTDOT));
676                 }
677         }
678
679         /*
680          * We now have a segment name to search for, and a directory to search.
681          */
682 unionlookup:
683 #ifdef MAC
684         if ((cnp->cn_flags & NOMACCHECK) == 0) {
685                 error = mac_vnode_check_lookup(cnp->cn_thread->td_ucred, dp,
686                     cnp);
687                 if (error)
688                         goto bad;
689         }
690 #endif
691         ndp->ni_dvp = dp;
692         ndp->ni_vp = NULL;
693         ASSERT_VOP_LOCKED(dp, "lookup");
694         /*
695          * If we have a shared lock we may need to upgrade the lock for the
696          * last operation.
697          */
698         if (dp != vp_crossmp &&
699             VOP_ISLOCKED(dp) == LK_SHARED &&
700             (cnp->cn_flags & ISLASTCN) && (cnp->cn_flags & LOCKPARENT))
701                 vn_lock(dp, LK_UPGRADE|LK_RETRY);
702         if ((dp->v_iflag & VI_DOOMED) != 0) {
703                 error = ENOENT;
704                 goto bad;
705         }
706         /*
707          * If we're looking up the last component and we need an exclusive
708          * lock, adjust our lkflags.
709          */
710         if (needs_exclusive_leaf(dp->v_mount, cnp->cn_flags))
711                 cnp->cn_lkflags = LK_EXCLUSIVE;
712 #ifdef NAMEI_DIAGNOSTIC
713         vprint("lookup in", dp);
714 #endif
715         lkflags_save = cnp->cn_lkflags;
716         cnp->cn_lkflags = compute_cn_lkflags(dp->v_mount, cnp->cn_lkflags,
717             cnp->cn_flags);
718         if ((error = VOP_LOOKUP(dp, &ndp->ni_vp, cnp)) != 0) {
719                 cnp->cn_lkflags = lkflags_save;
720                 KASSERT(ndp->ni_vp == NULL, ("leaf should be empty"));
721 #ifdef NAMEI_DIAGNOSTIC
722                 printf("not found\n");
723 #endif
724                 if ((error == ENOENT) &&
725                     (dp->v_vflag & VV_ROOT) && (dp->v_mount != NULL) &&
726                     (dp->v_mount->mnt_flag & MNT_UNION)) {
727                         tdp = dp;
728                         dp = dp->v_mount->mnt_vnodecovered;
729                         VREF(dp);
730                         vput(tdp);
731                         vn_lock(dp,
732                             compute_cn_lkflags(dp->v_mount, cnp->cn_lkflags |
733                             LK_RETRY, cnp->cn_flags));
734                         goto unionlookup;
735                 }
736
737                 if (error != EJUSTRETURN)
738                         goto bad;
739                 /*
740                  * At this point, we know we're at the end of the
741                  * pathname.  If creating / renaming, we can consider
742                  * allowing the file or directory to be created / renamed,
743                  * provided we're not on a read-only filesystem.
744                  */
745                 if (rdonly) {
746                         error = EROFS;
747                         goto bad;
748                 }
749                 /* trailing slash only allowed for directories */
750                 if ((cnp->cn_flags & TRAILINGSLASH) &&
751                     !(cnp->cn_flags & WILLBEDIR)) {
752                         error = ENOENT;
753                         goto bad;
754                 }
755                 if ((cnp->cn_flags & LOCKPARENT) == 0)
756                         VOP_UNLOCK(dp, 0);
757                 /*
758                  * We return with ni_vp NULL to indicate that the entry
759                  * doesn't currently exist, leaving a pointer to the
760                  * (possibly locked) directory vnode in ndp->ni_dvp.
761                  */
762                 if (cnp->cn_flags & SAVESTART) {
763                         ndp->ni_startdir = ndp->ni_dvp;
764                         VREF(ndp->ni_startdir);
765                 }
766                 goto success;
767         } else
768                 cnp->cn_lkflags = lkflags_save;
769 #ifdef NAMEI_DIAGNOSTIC
770         printf("found\n");
771 #endif
772         /*
773          * Take into account any additional components consumed by
774          * the underlying filesystem.
775          */
776         if (cnp->cn_consume > 0) {
777                 cnp->cn_nameptr += cnp->cn_consume;
778                 ndp->ni_next += cnp->cn_consume;
779                 ndp->ni_pathlen -= cnp->cn_consume;
780                 cnp->cn_consume = 0;
781         }
782
783         dp = ndp->ni_vp;
784
785         /*
786          * Check to see if the vnode has been mounted on;
787          * if so find the root of the mounted filesystem.
788          */
789         while (dp->v_type == VDIR && (mp = dp->v_mountedhere) &&
790                (cnp->cn_flags & NOCROSSMOUNT) == 0) {
791                 if (vfs_busy(mp, 0))
792                         continue;
793                 vput(dp);
794                 if (dp != ndp->ni_dvp)
795                         vput(ndp->ni_dvp);
796                 else
797                         vrele(ndp->ni_dvp);
798                 vref(vp_crossmp);
799                 ndp->ni_dvp = vp_crossmp;
800                 error = VFS_ROOT(mp, compute_cn_lkflags(mp, cnp->cn_lkflags,
801                     cnp->cn_flags), &tdp);
802                 vfs_unbusy(mp);
803                 if (vn_lock(vp_crossmp, LK_SHARED | LK_NOWAIT))
804                         panic("vp_crossmp exclusively locked or reclaimed");
805                 if (error) {
806                         dpunlocked = 1;
807                         goto bad2;
808                 }
809                 ndp->ni_vp = dp = tdp;
810         }
811
812         /*
813          * Check for symbolic link
814          */
815         if ((dp->v_type == VLNK) &&
816             ((cnp->cn_flags & FOLLOW) || (cnp->cn_flags & TRAILINGSLASH) ||
817              *ndp->ni_next == '/')) {
818                 cnp->cn_flags |= ISSYMLINK;
819                 if (dp->v_iflag & VI_DOOMED) {
820                         /*
821                          * We can't know whether the directory was mounted with
822                          * NOSYMFOLLOW, so we can't follow safely.
823                          */
824                         error = ENOENT;
825                         goto bad2;
826                 }
827                 if (dp->v_mount->mnt_flag & MNT_NOSYMFOLLOW) {
828                         error = EACCES;
829                         goto bad2;
830                 }
831                 /*
832                  * Symlink code always expects an unlocked dvp.
833                  */
834                 if (ndp->ni_dvp != ndp->ni_vp) {
835                         VOP_UNLOCK(ndp->ni_dvp, 0);
836                         ni_dvp_unlocked = 1;
837                 }
838                 goto success;
839         }
840
841 nextname:
842         /*
843          * Not a symbolic link that we will follow.  Continue with the
844          * next component if there is any; otherwise, we're done.
845          */
846         KASSERT((cnp->cn_flags & ISLASTCN) || *ndp->ni_next == '/',
847             ("lookup: invalid path state."));
848         if (*ndp->ni_next == '/') {
849                 cnp->cn_nameptr = ndp->ni_next;
850                 while (*cnp->cn_nameptr == '/') {
851                         cnp->cn_nameptr++;
852                         ndp->ni_pathlen--;
853                 }
854                 if (ndp->ni_dvp != dp)
855                         vput(ndp->ni_dvp);
856                 else
857                         vrele(ndp->ni_dvp);
858                 goto dirloop;
859         }
860         /*
861          * If we're processing a path with a trailing slash,
862          * check that the end result is a directory.
863          */
864         if ((cnp->cn_flags & TRAILINGSLASH) && dp->v_type != VDIR) {
865                 error = ENOTDIR;
866                 goto bad2;
867         }
868         /*
869          * Disallow directory write attempts on read-only filesystems.
870          */
871         if (rdonly &&
872             (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME)) {
873                 error = EROFS;
874                 goto bad2;
875         }
876         if (cnp->cn_flags & SAVESTART) {
877                 ndp->ni_startdir = ndp->ni_dvp;
878                 VREF(ndp->ni_startdir);
879         }
880         if (!wantparent) {
881                 ni_dvp_unlocked = 2;
882                 if (ndp->ni_dvp != dp)
883                         vput(ndp->ni_dvp);
884                 else
885                         vrele(ndp->ni_dvp);
886         } else if ((cnp->cn_flags & LOCKPARENT) == 0 && ndp->ni_dvp != dp) {
887                 VOP_UNLOCK(ndp->ni_dvp, 0);
888                 ni_dvp_unlocked = 1;
889         }
890
891         if (cnp->cn_flags & AUDITVNODE1)
892                 AUDIT_ARG_VNODE1(dp);
893         else if (cnp->cn_flags & AUDITVNODE2)
894                 AUDIT_ARG_VNODE2(dp);
895
896         if ((cnp->cn_flags & LOCKLEAF) == 0)
897                 VOP_UNLOCK(dp, 0);
898 success:
899         /*
900          * Because of lookup_shared we may have the vnode shared locked, but
901          * the caller may want it to be exclusively locked.
902          */
903         if (needs_exclusive_leaf(dp->v_mount, cnp->cn_flags) &&
904             VOP_ISLOCKED(dp) != LK_EXCLUSIVE) {
905                 vn_lock(dp, LK_UPGRADE | LK_RETRY);
906                 if (dp->v_iflag & VI_DOOMED) {
907                         error = ENOENT;
908                         goto bad2;
909                 }
910         }
911         return (0);
912
913 bad2:
914         if (ni_dvp_unlocked != 2) {
915                 if (dp != ndp->ni_dvp && !ni_dvp_unlocked)
916                         vput(ndp->ni_dvp);
917                 else
918                         vrele(ndp->ni_dvp);
919         }
920 bad:
921         if (!dpunlocked)
922                 vput(dp);
923         ndp->ni_vp = NULL;
924         return (error);
925 }
926
927 /*
928  * relookup - lookup a path name component
929  *    Used by lookup to re-acquire things.
930  */
931 int
932 relookup(struct vnode *dvp, struct vnode **vpp, struct componentname *cnp)
933 {
934         struct vnode *dp = 0;           /* the directory we are searching */
935         int wantparent;                 /* 1 => wantparent or lockparent flag */
936         int rdonly;                     /* lookup read-only flag bit */
937         int error = 0;
938
939         KASSERT(cnp->cn_flags & ISLASTCN,
940             ("relookup: Not given last component."));
941         /*
942          * Setup: break out flag bits into variables.
943          */
944         wantparent = cnp->cn_flags & (LOCKPARENT|WANTPARENT);
945         KASSERT(wantparent, ("relookup: parent not wanted."));
946         rdonly = cnp->cn_flags & RDONLY;
947         cnp->cn_flags &= ~ISSYMLINK;
948         dp = dvp;
949         cnp->cn_lkflags = LK_EXCLUSIVE;
950         vn_lock(dp, LK_EXCLUSIVE | LK_RETRY);
951
952         /*
953          * Search a new directory.
954          *
955          * The last component of the filename is left accessible via
956          * cnp->cn_nameptr for callers that need the name. Callers needing
957          * the name set the SAVENAME flag. When done, they assume
958          * responsibility for freeing the pathname buffer.
959          */
960 #ifdef NAMEI_DIAGNOSTIC
961         printf("{%s}: ", cnp->cn_nameptr);
962 #endif
963
964         /*
965          * Check for "" which represents the root directory after slash
966          * removal.
967          */
968         if (cnp->cn_nameptr[0] == '\0') {
969                 /*
970                  * Support only LOOKUP for "/" because lookup()
971                  * can't succeed for CREATE, DELETE and RENAME.
972                  */
973                 KASSERT(cnp->cn_nameiop == LOOKUP, ("nameiop must be LOOKUP"));
974                 KASSERT(dp->v_type == VDIR, ("dp is not a directory"));
975
976                 if (!(cnp->cn_flags & LOCKLEAF))
977                         VOP_UNLOCK(dp, 0);
978                 *vpp = dp;
979                 /* XXX This should probably move to the top of function. */
980                 if (cnp->cn_flags & SAVESTART)
981                         panic("lookup: SAVESTART");
982                 return (0);
983         }
984
985         if (cnp->cn_flags & ISDOTDOT)
986                 panic ("relookup: lookup on dot-dot");
987
988         /*
989          * We now have a segment name to search for, and a directory to search.
990          */
991 #ifdef NAMEI_DIAGNOSTIC
992         vprint("search in:", dp);
993 #endif
994         if ((error = VOP_LOOKUP(dp, vpp, cnp)) != 0) {
995                 KASSERT(*vpp == NULL, ("leaf should be empty"));
996                 if (error != EJUSTRETURN)
997                         goto bad;
998                 /*
999                  * If creating and at end of pathname, then can consider
1000                  * allowing file to be created.
1001                  */
1002                 if (rdonly) {
1003                         error = EROFS;
1004                         goto bad;
1005                 }
1006                 /* ASSERT(dvp == ndp->ni_startdir) */
1007                 if (cnp->cn_flags & SAVESTART)
1008                         VREF(dvp);
1009                 if ((cnp->cn_flags & LOCKPARENT) == 0)
1010                         VOP_UNLOCK(dp, 0);
1011                 /*
1012                  * We return with ni_vp NULL to indicate that the entry
1013                  * doesn't currently exist, leaving a pointer to the
1014                  * (possibly locked) directory vnode in ndp->ni_dvp.
1015                  */
1016                 return (0);
1017         }
1018
1019         dp = *vpp;
1020
1021         /*
1022          * Disallow directory write attempts on read-only filesystems.
1023          */
1024         if (rdonly &&
1025             (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME)) {
1026                 if (dvp == dp)
1027                         vrele(dvp);
1028                 else
1029                         vput(dvp);
1030                 error = EROFS;
1031                 goto bad;
1032         }
1033         /*
1034          * Set the parent lock/ref state to the requested state.
1035          */
1036         if ((cnp->cn_flags & LOCKPARENT) == 0 && dvp != dp) {
1037                 if (wantparent)
1038                         VOP_UNLOCK(dvp, 0);
1039                 else
1040                         vput(dvp);
1041         } else if (!wantparent)
1042                 vrele(dvp);
1043         /*
1044          * Check for symbolic link
1045          */
1046         KASSERT(dp->v_type != VLNK || !(cnp->cn_flags & FOLLOW),
1047             ("relookup: symlink found.\n"));
1048
1049         /* ASSERT(dvp == ndp->ni_startdir) */
1050         if (cnp->cn_flags & SAVESTART)
1051                 VREF(dvp);
1052         
1053         if ((cnp->cn_flags & LOCKLEAF) == 0)
1054                 VOP_UNLOCK(dp, 0);
1055         return (0);
1056 bad:
1057         vput(dp);
1058         *vpp = NULL;
1059         return (error);
1060 }
1061
1062 void
1063 NDINIT_ALL(struct nameidata *ndp, u_long op, u_long flags, enum uio_seg segflg,
1064     const char *namep, int dirfd, struct vnode *startdir, cap_rights_t *rightsp,
1065     struct thread *td)
1066 {
1067
1068         ndp->ni_cnd.cn_nameiop = op;
1069         ndp->ni_cnd.cn_flags = flags;
1070         ndp->ni_segflg = segflg;
1071         ndp->ni_dirp = namep;
1072         ndp->ni_dirfd = dirfd;
1073         ndp->ni_startdir = startdir;
1074         ndp->ni_strictrelative = 0;
1075         if (rightsp != NULL)
1076                 ndp->ni_rightsneeded = *rightsp;
1077         else
1078                 cap_rights_init(&ndp->ni_rightsneeded);
1079         filecaps_init(&ndp->ni_filecaps);
1080         ndp->ni_cnd.cn_thread = td;
1081 }
1082
1083 /*
1084  * Free data allocated by namei(); see namei(9) for details.
1085  */
1086 void
1087 NDFREE(struct nameidata *ndp, const u_int flags)
1088 {
1089         int unlock_dvp;
1090         int unlock_vp;
1091
1092         unlock_dvp = 0;
1093         unlock_vp = 0;
1094
1095         if (!(flags & NDF_NO_FREE_PNBUF) &&
1096             (ndp->ni_cnd.cn_flags & HASBUF)) {
1097                 uma_zfree(namei_zone, ndp->ni_cnd.cn_pnbuf);
1098                 ndp->ni_cnd.cn_flags &= ~HASBUF;
1099         }
1100         if (!(flags & NDF_NO_VP_UNLOCK) &&
1101             (ndp->ni_cnd.cn_flags & LOCKLEAF) && ndp->ni_vp)
1102                 unlock_vp = 1;
1103         if (!(flags & NDF_NO_VP_RELE) && ndp->ni_vp) {
1104                 if (unlock_vp) {
1105                         vput(ndp->ni_vp);
1106                         unlock_vp = 0;
1107                 } else
1108                         vrele(ndp->ni_vp);
1109                 ndp->ni_vp = NULL;
1110         }
1111         if (unlock_vp)
1112                 VOP_UNLOCK(ndp->ni_vp, 0);
1113         if (!(flags & NDF_NO_DVP_UNLOCK) &&
1114             (ndp->ni_cnd.cn_flags & LOCKPARENT) &&
1115             ndp->ni_dvp != ndp->ni_vp)
1116                 unlock_dvp = 1;
1117         if (!(flags & NDF_NO_DVP_RELE) &&
1118             (ndp->ni_cnd.cn_flags & (LOCKPARENT|WANTPARENT))) {
1119                 if (unlock_dvp) {
1120                         vput(ndp->ni_dvp);
1121                         unlock_dvp = 0;
1122                 } else
1123                         vrele(ndp->ni_dvp);
1124                 ndp->ni_dvp = NULL;
1125         }
1126         if (unlock_dvp)
1127                 VOP_UNLOCK(ndp->ni_dvp, 0);
1128         if (!(flags & NDF_NO_STARTDIR_RELE) &&
1129             (ndp->ni_cnd.cn_flags & SAVESTART)) {
1130                 vrele(ndp->ni_startdir);
1131                 ndp->ni_startdir = NULL;
1132         }
1133 }
1134
1135 /*
1136  * Determine if there is a suitable alternate filename under the specified
1137  * prefix for the specified path.  If the create flag is set, then the
1138  * alternate prefix will be used so long as the parent directory exists.
1139  * This is used by the various compatiblity ABIs so that Linux binaries prefer
1140  * files under /compat/linux for example.  The chosen path (whether under
1141  * the prefix or under /) is returned in a kernel malloc'd buffer pointed
1142  * to by pathbuf.  The caller is responsible for free'ing the buffer from
1143  * the M_TEMP bucket if one is returned.
1144  */
1145 int
1146 kern_alternate_path(struct thread *td, const char *prefix, const char *path,
1147     enum uio_seg pathseg, char **pathbuf, int create, int dirfd)
1148 {
1149         struct nameidata nd, ndroot;
1150         char *ptr, *buf, *cp;
1151         size_t len, sz;
1152         int error;
1153
1154         buf = (char *) malloc(MAXPATHLEN, M_TEMP, M_WAITOK);
1155         *pathbuf = buf;
1156
1157         /* Copy the prefix into the new pathname as a starting point. */
1158         len = strlcpy(buf, prefix, MAXPATHLEN);
1159         if (len >= MAXPATHLEN) {
1160                 *pathbuf = NULL;
1161                 free(buf, M_TEMP);
1162                 return (EINVAL);
1163         }
1164         sz = MAXPATHLEN - len;
1165         ptr = buf + len;
1166
1167         /* Append the filename to the prefix. */
1168         if (pathseg == UIO_SYSSPACE)
1169                 error = copystr(path, ptr, sz, &len);
1170         else
1171                 error = copyinstr(path, ptr, sz, &len);
1172
1173         if (error) {
1174                 *pathbuf = NULL;
1175                 free(buf, M_TEMP);
1176                 return (error);
1177         }
1178
1179         /* Only use a prefix with absolute pathnames. */
1180         if (*ptr != '/') {
1181                 error = EINVAL;
1182                 goto keeporig;
1183         }
1184
1185         if (dirfd != AT_FDCWD) {
1186                 /*
1187                  * We want the original because the "prefix" is
1188                  * included in the already opened dirfd.
1189                  */
1190                 bcopy(ptr, buf, len);
1191                 return (0);
1192         }
1193
1194         /*
1195          * We know that there is a / somewhere in this pathname.
1196          * Search backwards for it, to find the file's parent dir
1197          * to see if it exists in the alternate tree. If it does,
1198          * and we want to create a file (cflag is set). We don't
1199          * need to worry about the root comparison in this case.
1200          */
1201
1202         if (create) {
1203                 for (cp = &ptr[len] - 1; *cp != '/'; cp--);
1204                 *cp = '\0';
1205
1206                 NDINIT(&nd, LOOKUP, FOLLOW, UIO_SYSSPACE, buf, td);
1207                 error = namei(&nd);
1208                 *cp = '/';
1209                 if (error != 0)
1210                         goto keeporig;
1211         } else {
1212                 NDINIT(&nd, LOOKUP, FOLLOW, UIO_SYSSPACE, buf, td);
1213
1214                 error = namei(&nd);
1215                 if (error != 0)
1216                         goto keeporig;
1217
1218                 /*
1219                  * We now compare the vnode of the prefix to the one
1220                  * vnode asked. If they resolve to be the same, then we
1221                  * ignore the match so that the real root gets used.
1222                  * This avoids the problem of traversing "../.." to find the
1223                  * root directory and never finding it, because "/" resolves
1224                  * to the emulation root directory. This is expensive :-(
1225                  */
1226                 NDINIT(&ndroot, LOOKUP, FOLLOW, UIO_SYSSPACE, prefix,
1227                     td);
1228
1229                 /* We shouldn't ever get an error from this namei(). */
1230                 error = namei(&ndroot);
1231                 if (error == 0) {
1232                         if (nd.ni_vp == ndroot.ni_vp)
1233                                 error = ENOENT;
1234
1235                         NDFREE(&ndroot, NDF_ONLY_PNBUF);
1236                         vrele(ndroot.ni_vp);
1237                 }
1238         }
1239
1240         NDFREE(&nd, NDF_ONLY_PNBUF);
1241         vrele(nd.ni_vp);
1242
1243 keeporig:
1244         /* If there was an error, use the original path name. */
1245         if (error)
1246                 bcopy(ptr, buf, len);
1247         return (error);
1248 }