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