]> CyberLeo.Net >> Repos - FreeBSD/releng/10.2.git/blob - sys/kern/vfs_lookup.c
- Copy stable/10@285827 to releng/10.2 in preparation for 10.2-RC1
[FreeBSD/releng/10.2.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/capsicum.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         lkflags |= LK_NODDLKTREAT;
396         return (lkflags);
397 }
398
399 static __inline int
400 needs_exclusive_leaf(struct mount *mp, int flags)
401 {
402
403         /*
404          * Intermediate nodes can use shared locks, we only need to
405          * force an exclusive lock for leaf nodes.
406          */
407         if ((flags & (ISLASTCN | LOCKLEAF)) != (ISLASTCN | LOCKLEAF))
408                 return (0);
409
410         /* Always use exclusive locks if LOCKSHARED isn't set. */
411         if (!(flags & LOCKSHARED))
412                 return (1);
413
414         /*
415          * For lookups during open(), if the mount point supports
416          * extended shared operations, then use a shared lock for the
417          * leaf node, otherwise use an exclusive lock.
418          */
419         if ((flags & ISOPEN) != 0)
420                 return (!MNT_EXTENDED_SHARED(mp));
421
422         /*
423          * Lookup requests outside of open() that specify LOCKSHARED
424          * only need a shared lock on the leaf vnode.
425          */
426         return (0);
427 }
428
429 /*
430  * Search a pathname.
431  * This is a very central and rather complicated routine.
432  *
433  * The pathname is pointed to by ni_ptr and is of length ni_pathlen.
434  * The starting directory is taken from ni_startdir. The pathname is
435  * descended until done, or a symbolic link is encountered. The variable
436  * ni_more is clear if the path is completed; it is set to one if a
437  * symbolic link needing interpretation is encountered.
438  *
439  * The flag argument is LOOKUP, CREATE, RENAME, or DELETE depending on
440  * whether the name is to be looked up, created, renamed, or deleted.
441  * When CREATE, RENAME, or DELETE is specified, information usable in
442  * creating, renaming, or deleting a directory entry may be calculated.
443  * If flag has LOCKPARENT or'ed into it, the parent directory is returned
444  * locked. If flag has WANTPARENT or'ed into it, the parent directory is
445  * returned unlocked. Otherwise the parent directory is not returned. If
446  * the target of the pathname exists and LOCKLEAF is or'ed into the flag
447  * the target is returned locked, otherwise it is returned unlocked.
448  * When creating or renaming and LOCKPARENT is specified, the target may not
449  * be ".".  When deleting and LOCKPARENT is specified, the target may be ".".
450  *
451  * Overall outline of lookup:
452  *
453  * dirloop:
454  *      identify next component of name at ndp->ni_ptr
455  *      handle degenerate case where name is null string
456  *      if .. and crossing mount points and on mounted filesys, find parent
457  *      call VOP_LOOKUP routine for next component name
458  *          directory vnode returned in ni_dvp, unlocked unless LOCKPARENT set
459  *          component vnode returned in ni_vp (if it exists), locked.
460  *      if result vnode is mounted on and crossing mount points,
461  *          find mounted on vnode
462  *      if more components of name, do next level at dirloop
463  *      return the answer in ni_vp, locked if LOCKLEAF set
464  *          if LOCKPARENT set, return locked parent in ni_dvp
465  *          if WANTPARENT set, return unlocked parent in ni_dvp
466  */
467 int
468 lookup(struct nameidata *ndp)
469 {
470         char *cp;               /* pointer into pathname argument */
471         struct vnode *dp = 0;   /* the directory we are searching */
472         struct vnode *tdp;              /* saved dp */
473         struct mount *mp;               /* mount table entry */
474         struct prison *pr;
475         int docache;                    /* == 0 do not cache last component */
476         int wantparent;                 /* 1 => wantparent or lockparent flag */
477         int rdonly;                     /* lookup read-only flag bit */
478         int error = 0;
479         int dpunlocked = 0;             /* dp has already been unlocked */
480         struct componentname *cnp = &ndp->ni_cnd;
481         int lkflags_save;
482         int ni_dvp_unlocked;
483         
484         /*
485          * Setup: break out flag bits into variables.
486          */
487         ni_dvp_unlocked = 0;
488         wantparent = cnp->cn_flags & (LOCKPARENT | WANTPARENT);
489         KASSERT(cnp->cn_nameiop == LOOKUP || wantparent,
490             ("CREATE, DELETE, RENAME require LOCKPARENT or WANTPARENT."));
491         docache = (cnp->cn_flags & NOCACHE) ^ NOCACHE;
492         if (cnp->cn_nameiop == DELETE ||
493             (wantparent && cnp->cn_nameiop != CREATE &&
494              cnp->cn_nameiop != LOOKUP))
495                 docache = 0;
496         rdonly = cnp->cn_flags & RDONLY;
497         cnp->cn_flags &= ~ISSYMLINK;
498         ndp->ni_dvp = NULL;
499         /*
500          * We use shared locks until we hit the parent of the last cn then
501          * we adjust based on the requesting flags.
502          */
503         if (lookup_shared)
504                 cnp->cn_lkflags = LK_SHARED;
505         else
506                 cnp->cn_lkflags = LK_EXCLUSIVE;
507         dp = ndp->ni_startdir;
508         ndp->ni_startdir = NULLVP;
509         vn_lock(dp,
510             compute_cn_lkflags(dp->v_mount, cnp->cn_lkflags | LK_RETRY,
511             cnp->cn_flags));
512
513 dirloop:
514         /*
515          * Search a new directory.
516          *
517          * The last component of the filename is left accessible via
518          * cnp->cn_nameptr for callers that need the name. Callers needing
519          * the name set the SAVENAME flag. When done, they assume
520          * responsibility for freeing the pathname buffer.
521          */
522         cnp->cn_consume = 0;
523         for (cp = cnp->cn_nameptr; *cp != 0 && *cp != '/'; cp++)
524                 continue;
525         cnp->cn_namelen = cp - cnp->cn_nameptr;
526         if (cnp->cn_namelen > NAME_MAX) {
527                 error = ENAMETOOLONG;
528                 goto bad;
529         }
530 #ifdef NAMEI_DIAGNOSTIC
531         { char c = *cp;
532         *cp = '\0';
533         printf("{%s}: ", cnp->cn_nameptr);
534         *cp = c; }
535 #endif
536         ndp->ni_pathlen -= cnp->cn_namelen;
537         ndp->ni_next = cp;
538
539         /*
540          * Replace multiple slashes by a single slash and trailing slashes
541          * by a null.  This must be done before VOP_LOOKUP() because some
542          * fs's don't know about trailing slashes.  Remember if there were
543          * trailing slashes to handle symlinks, existing non-directories
544          * and non-existing files that won't be directories specially later.
545          */
546         while (*cp == '/' && (cp[1] == '/' || cp[1] == '\0')) {
547                 cp++;
548                 ndp->ni_pathlen--;
549                 if (*cp == '\0') {
550                         *ndp->ni_next = '\0';
551                         cnp->cn_flags |= TRAILINGSLASH;
552                 }
553         }
554         ndp->ni_next = cp;
555
556         cnp->cn_flags |= MAKEENTRY;
557         if (*cp == '\0' && docache == 0)
558                 cnp->cn_flags &= ~MAKEENTRY;
559         if (cnp->cn_namelen == 2 &&
560             cnp->cn_nameptr[1] == '.' && cnp->cn_nameptr[0] == '.')
561                 cnp->cn_flags |= ISDOTDOT;
562         else
563                 cnp->cn_flags &= ~ISDOTDOT;
564         if (*ndp->ni_next == 0)
565                 cnp->cn_flags |= ISLASTCN;
566         else
567                 cnp->cn_flags &= ~ISLASTCN;
568
569         if ((cnp->cn_flags & ISLASTCN) != 0 &&
570             cnp->cn_namelen == 1 && cnp->cn_nameptr[0] == '.' &&
571             (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME)) {
572                 error = EINVAL;
573                 goto bad;
574         }
575
576         /*
577          * Check for degenerate name (e.g. / or "")
578          * which is a way of talking about a directory,
579          * e.g. like "/." or ".".
580          */
581         if (cnp->cn_nameptr[0] == '\0') {
582                 if (dp->v_type != VDIR) {
583                         error = ENOTDIR;
584                         goto bad;
585                 }
586                 if (cnp->cn_nameiop != LOOKUP) {
587                         error = EISDIR;
588                         goto bad;
589                 }
590                 if (wantparent) {
591                         ndp->ni_dvp = dp;
592                         VREF(dp);
593                 }
594                 ndp->ni_vp = dp;
595
596                 if (cnp->cn_flags & AUDITVNODE1)
597                         AUDIT_ARG_VNODE1(dp);
598                 else if (cnp->cn_flags & AUDITVNODE2)
599                         AUDIT_ARG_VNODE2(dp);
600
601                 if (!(cnp->cn_flags & (LOCKPARENT | LOCKLEAF)))
602                         VOP_UNLOCK(dp, 0);
603                 /* XXX This should probably move to the top of function. */
604                 if (cnp->cn_flags & SAVESTART)
605                         panic("lookup: SAVESTART");
606                 goto success;
607         }
608
609         /*
610          * Handle "..": five special cases.
611          * 0. If doing a capability lookup, return ENOTCAPABLE (this is a
612          *    fairly conservative design choice, but it's the only one that we
613          *    are satisfied guarantees the property we're looking for).
614          * 1. Return an error if this is the last component of
615          *    the name and the operation is DELETE or RENAME.
616          * 2. If at root directory (e.g. after chroot)
617          *    or at absolute root directory
618          *    then ignore it so can't get out.
619          * 3. If this vnode is the root of a mounted
620          *    filesystem, then replace it with the
621          *    vnode which was mounted on so we take the
622          *    .. in the other filesystem.
623          * 4. If the vnode is the top directory of
624          *    the jail or chroot, don't let them out.
625          */
626         if (cnp->cn_flags & ISDOTDOT) {
627                 if (ndp->ni_strictrelative != 0) {
628 #ifdef KTRACE
629                         if (KTRPOINT(curthread, KTR_CAPFAIL))
630                                 ktrcapfail(CAPFAIL_LOOKUP, NULL, NULL);
631 #endif
632                         error = ENOTCAPABLE;
633                         goto bad;
634                 }
635                 if ((cnp->cn_flags & ISLASTCN) != 0 &&
636                     (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME)) {
637                         error = EINVAL;
638                         goto bad;
639                 }
640                 for (;;) {
641                         for (pr = cnp->cn_cred->cr_prison; pr != NULL;
642                              pr = pr->pr_parent)
643                                 if (dp == pr->pr_root)
644                                         break;
645                         if (dp == ndp->ni_rootdir || 
646                             dp == ndp->ni_topdir || 
647                             dp == rootvnode ||
648                             pr != NULL ||
649                             ((dp->v_vflag & VV_ROOT) != 0 &&
650                              (cnp->cn_flags & NOCROSSMOUNT) != 0)) {
651                                 ndp->ni_dvp = dp;
652                                 ndp->ni_vp = dp;
653                                 VREF(dp);
654                                 goto nextname;
655                         }
656                         if ((dp->v_vflag & VV_ROOT) == 0)
657                                 break;
658                         if (dp->v_iflag & VI_DOOMED) {  /* forced unmount */
659                                 error = ENOENT;
660                                 goto bad;
661                         }
662                         tdp = dp;
663                         dp = dp->v_mount->mnt_vnodecovered;
664                         VREF(dp);
665                         vput(tdp);
666                         vn_lock(dp,
667                             compute_cn_lkflags(dp->v_mount, cnp->cn_lkflags |
668                             LK_RETRY, ISDOTDOT));
669                 }
670         }
671
672         /*
673          * We now have a segment name to search for, and a directory to search.
674          */
675 unionlookup:
676 #ifdef MAC
677         if ((cnp->cn_flags & NOMACCHECK) == 0) {
678                 error = mac_vnode_check_lookup(cnp->cn_thread->td_ucred, dp,
679                     cnp);
680                 if (error)
681                         goto bad;
682         }
683 #endif
684         ndp->ni_dvp = dp;
685         ndp->ni_vp = NULL;
686         ASSERT_VOP_LOCKED(dp, "lookup");
687         /*
688          * If we have a shared lock we may need to upgrade the lock for the
689          * last operation.
690          */
691         if (dp != vp_crossmp &&
692             VOP_ISLOCKED(dp) == LK_SHARED &&
693             (cnp->cn_flags & ISLASTCN) && (cnp->cn_flags & LOCKPARENT))
694                 vn_lock(dp, LK_UPGRADE|LK_RETRY);
695         if ((dp->v_iflag & VI_DOOMED) != 0) {
696                 error = ENOENT;
697                 goto bad;
698         }
699         /*
700          * If we're looking up the last component and we need an exclusive
701          * lock, adjust our lkflags.
702          */
703         if (needs_exclusive_leaf(dp->v_mount, cnp->cn_flags))
704                 cnp->cn_lkflags = LK_EXCLUSIVE;
705 #ifdef NAMEI_DIAGNOSTIC
706         vprint("lookup in", dp);
707 #endif
708         lkflags_save = cnp->cn_lkflags;
709         cnp->cn_lkflags = compute_cn_lkflags(dp->v_mount, cnp->cn_lkflags,
710             cnp->cn_flags);
711         if ((error = VOP_LOOKUP(dp, &ndp->ni_vp, cnp)) != 0) {
712                 cnp->cn_lkflags = lkflags_save;
713                 KASSERT(ndp->ni_vp == NULL, ("leaf should be empty"));
714 #ifdef NAMEI_DIAGNOSTIC
715                 printf("not found\n");
716 #endif
717                 if ((error == ENOENT) &&
718                     (dp->v_vflag & VV_ROOT) && (dp->v_mount != NULL) &&
719                     (dp->v_mount->mnt_flag & MNT_UNION)) {
720                         tdp = dp;
721                         dp = dp->v_mount->mnt_vnodecovered;
722                         VREF(dp);
723                         vput(tdp);
724                         vn_lock(dp,
725                             compute_cn_lkflags(dp->v_mount, cnp->cn_lkflags |
726                             LK_RETRY, cnp->cn_flags));
727                         goto unionlookup;
728                 }
729
730                 if (error != EJUSTRETURN)
731                         goto bad;
732                 /*
733                  * At this point, we know we're at the end of the
734                  * pathname.  If creating / renaming, we can consider
735                  * allowing the file or directory to be created / renamed,
736                  * provided we're not on a read-only filesystem.
737                  */
738                 if (rdonly) {
739                         error = EROFS;
740                         goto bad;
741                 }
742                 /* trailing slash only allowed for directories */
743                 if ((cnp->cn_flags & TRAILINGSLASH) &&
744                     !(cnp->cn_flags & WILLBEDIR)) {
745                         error = ENOENT;
746                         goto bad;
747                 }
748                 if ((cnp->cn_flags & LOCKPARENT) == 0)
749                         VOP_UNLOCK(dp, 0);
750                 /*
751                  * We return with ni_vp NULL to indicate that the entry
752                  * doesn't currently exist, leaving a pointer to the
753                  * (possibly locked) directory vnode in ndp->ni_dvp.
754                  */
755                 if (cnp->cn_flags & SAVESTART) {
756                         ndp->ni_startdir = ndp->ni_dvp;
757                         VREF(ndp->ni_startdir);
758                 }
759                 goto success;
760         } else
761                 cnp->cn_lkflags = lkflags_save;
762 #ifdef NAMEI_DIAGNOSTIC
763         printf("found\n");
764 #endif
765         /*
766          * Take into account any additional components consumed by
767          * the underlying filesystem.
768          */
769         if (cnp->cn_consume > 0) {
770                 cnp->cn_nameptr += cnp->cn_consume;
771                 ndp->ni_next += cnp->cn_consume;
772                 ndp->ni_pathlen -= cnp->cn_consume;
773                 cnp->cn_consume = 0;
774         }
775
776         dp = ndp->ni_vp;
777
778         /*
779          * Check to see if the vnode has been mounted on;
780          * if so find the root of the mounted filesystem.
781          */
782         while (dp->v_type == VDIR && (mp = dp->v_mountedhere) &&
783                (cnp->cn_flags & NOCROSSMOUNT) == 0) {
784                 if (vfs_busy(mp, 0))
785                         continue;
786                 vput(dp);
787                 if (dp != ndp->ni_dvp)
788                         vput(ndp->ni_dvp);
789                 else
790                         vrele(ndp->ni_dvp);
791                 vref(vp_crossmp);
792                 ndp->ni_dvp = vp_crossmp;
793                 error = VFS_ROOT(mp, compute_cn_lkflags(mp, cnp->cn_lkflags,
794                     cnp->cn_flags), &tdp);
795                 vfs_unbusy(mp);
796                 if (vn_lock(vp_crossmp, LK_SHARED | LK_NOWAIT))
797                         panic("vp_crossmp exclusively locked or reclaimed");
798                 if (error) {
799                         dpunlocked = 1;
800                         goto bad2;
801                 }
802                 ndp->ni_vp = dp = tdp;
803         }
804
805         /*
806          * Check for symbolic link
807          */
808         if ((dp->v_type == VLNK) &&
809             ((cnp->cn_flags & FOLLOW) || (cnp->cn_flags & TRAILINGSLASH) ||
810              *ndp->ni_next == '/')) {
811                 cnp->cn_flags |= ISSYMLINK;
812                 if (dp->v_iflag & VI_DOOMED) {
813                         /*
814                          * We can't know whether the directory was mounted with
815                          * NOSYMFOLLOW, so we can't follow safely.
816                          */
817                         error = ENOENT;
818                         goto bad2;
819                 }
820                 if (dp->v_mount->mnt_flag & MNT_NOSYMFOLLOW) {
821                         error = EACCES;
822                         goto bad2;
823                 }
824                 /*
825                  * Symlink code always expects an unlocked dvp.
826                  */
827                 if (ndp->ni_dvp != ndp->ni_vp) {
828                         VOP_UNLOCK(ndp->ni_dvp, 0);
829                         ni_dvp_unlocked = 1;
830                 }
831                 goto success;
832         }
833
834 nextname:
835         /*
836          * Not a symbolic link that we will follow.  Continue with the
837          * next component if there is any; otherwise, we're done.
838          */
839         KASSERT((cnp->cn_flags & ISLASTCN) || *ndp->ni_next == '/',
840             ("lookup: invalid path state."));
841         if (*ndp->ni_next == '/') {
842                 cnp->cn_nameptr = ndp->ni_next;
843                 while (*cnp->cn_nameptr == '/') {
844                         cnp->cn_nameptr++;
845                         ndp->ni_pathlen--;
846                 }
847                 if (ndp->ni_dvp != dp)
848                         vput(ndp->ni_dvp);
849                 else
850                         vrele(ndp->ni_dvp);
851                 goto dirloop;
852         }
853         /*
854          * If we're processing a path with a trailing slash,
855          * check that the end result is a directory.
856          */
857         if ((cnp->cn_flags & TRAILINGSLASH) && dp->v_type != VDIR) {
858                 error = ENOTDIR;
859                 goto bad2;
860         }
861         /*
862          * Disallow directory write attempts on read-only filesystems.
863          */
864         if (rdonly &&
865             (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME)) {
866                 error = EROFS;
867                 goto bad2;
868         }
869         if (cnp->cn_flags & SAVESTART) {
870                 ndp->ni_startdir = ndp->ni_dvp;
871                 VREF(ndp->ni_startdir);
872         }
873         if (!wantparent) {
874                 ni_dvp_unlocked = 2;
875                 if (ndp->ni_dvp != dp)
876                         vput(ndp->ni_dvp);
877                 else
878                         vrele(ndp->ni_dvp);
879         } else if ((cnp->cn_flags & LOCKPARENT) == 0 && ndp->ni_dvp != dp) {
880                 VOP_UNLOCK(ndp->ni_dvp, 0);
881                 ni_dvp_unlocked = 1;
882         }
883
884         if (cnp->cn_flags & AUDITVNODE1)
885                 AUDIT_ARG_VNODE1(dp);
886         else if (cnp->cn_flags & AUDITVNODE2)
887                 AUDIT_ARG_VNODE2(dp);
888
889         if ((cnp->cn_flags & LOCKLEAF) == 0)
890                 VOP_UNLOCK(dp, 0);
891 success:
892         /*
893          * Because of lookup_shared we may have the vnode shared locked, but
894          * the caller may want it to be exclusively locked.
895          */
896         if (needs_exclusive_leaf(dp->v_mount, cnp->cn_flags) &&
897             VOP_ISLOCKED(dp) != LK_EXCLUSIVE) {
898                 vn_lock(dp, LK_UPGRADE | LK_RETRY);
899                 if (dp->v_iflag & VI_DOOMED) {
900                         error = ENOENT;
901                         goto bad2;
902                 }
903         }
904         return (0);
905
906 bad2:
907         if (ni_dvp_unlocked != 2) {
908                 if (dp != ndp->ni_dvp && !ni_dvp_unlocked)
909                         vput(ndp->ni_dvp);
910                 else
911                         vrele(ndp->ni_dvp);
912         }
913 bad:
914         if (!dpunlocked)
915                 vput(dp);
916         ndp->ni_vp = NULL;
917         return (error);
918 }
919
920 /*
921  * relookup - lookup a path name component
922  *    Used by lookup to re-acquire things.
923  */
924 int
925 relookup(struct vnode *dvp, struct vnode **vpp, struct componentname *cnp)
926 {
927         struct vnode *dp = 0;           /* the directory we are searching */
928         int wantparent;                 /* 1 => wantparent or lockparent flag */
929         int rdonly;                     /* lookup read-only flag bit */
930         int error = 0;
931
932         KASSERT(cnp->cn_flags & ISLASTCN,
933             ("relookup: Not given last component."));
934         /*
935          * Setup: break out flag bits into variables.
936          */
937         wantparent = cnp->cn_flags & (LOCKPARENT|WANTPARENT);
938         KASSERT(wantparent, ("relookup: parent not wanted."));
939         rdonly = cnp->cn_flags & RDONLY;
940         cnp->cn_flags &= ~ISSYMLINK;
941         dp = dvp;
942         cnp->cn_lkflags = LK_EXCLUSIVE;
943         vn_lock(dp, LK_EXCLUSIVE | LK_RETRY);
944
945         /*
946          * Search a new directory.
947          *
948          * The last component of the filename is left accessible via
949          * cnp->cn_nameptr for callers that need the name. Callers needing
950          * the name set the SAVENAME flag. When done, they assume
951          * responsibility for freeing the pathname buffer.
952          */
953 #ifdef NAMEI_DIAGNOSTIC
954         printf("{%s}: ", cnp->cn_nameptr);
955 #endif
956
957         /*
958          * Check for "" which represents the root directory after slash
959          * removal.
960          */
961         if (cnp->cn_nameptr[0] == '\0') {
962                 /*
963                  * Support only LOOKUP for "/" because lookup()
964                  * can't succeed for CREATE, DELETE and RENAME.
965                  */
966                 KASSERT(cnp->cn_nameiop == LOOKUP, ("nameiop must be LOOKUP"));
967                 KASSERT(dp->v_type == VDIR, ("dp is not a directory"));
968
969                 if (!(cnp->cn_flags & LOCKLEAF))
970                         VOP_UNLOCK(dp, 0);
971                 *vpp = dp;
972                 /* XXX This should probably move to the top of function. */
973                 if (cnp->cn_flags & SAVESTART)
974                         panic("lookup: SAVESTART");
975                 return (0);
976         }
977
978         if (cnp->cn_flags & ISDOTDOT)
979                 panic ("relookup: lookup on dot-dot");
980
981         /*
982          * We now have a segment name to search for, and a directory to search.
983          */
984 #ifdef NAMEI_DIAGNOSTIC
985         vprint("search in:", dp);
986 #endif
987         if ((error = VOP_LOOKUP(dp, vpp, cnp)) != 0) {
988                 KASSERT(*vpp == NULL, ("leaf should be empty"));
989                 if (error != EJUSTRETURN)
990                         goto bad;
991                 /*
992                  * If creating and at end of pathname, then can consider
993                  * allowing file to be created.
994                  */
995                 if (rdonly) {
996                         error = EROFS;
997                         goto bad;
998                 }
999                 /* ASSERT(dvp == ndp->ni_startdir) */
1000                 if (cnp->cn_flags & SAVESTART)
1001                         VREF(dvp);
1002                 if ((cnp->cn_flags & LOCKPARENT) == 0)
1003                         VOP_UNLOCK(dp, 0);
1004                 /*
1005                  * We return with ni_vp NULL to indicate that the entry
1006                  * doesn't currently exist, leaving a pointer to the
1007                  * (possibly locked) directory vnode in ndp->ni_dvp.
1008                  */
1009                 return (0);
1010         }
1011
1012         dp = *vpp;
1013
1014         /*
1015          * Disallow directory write attempts on read-only filesystems.
1016          */
1017         if (rdonly &&
1018             (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME)) {
1019                 if (dvp == dp)
1020                         vrele(dvp);
1021                 else
1022                         vput(dvp);
1023                 error = EROFS;
1024                 goto bad;
1025         }
1026         /*
1027          * Set the parent lock/ref state to the requested state.
1028          */
1029         if ((cnp->cn_flags & LOCKPARENT) == 0 && dvp != dp) {
1030                 if (wantparent)
1031                         VOP_UNLOCK(dvp, 0);
1032                 else
1033                         vput(dvp);
1034         } else if (!wantparent)
1035                 vrele(dvp);
1036         /*
1037          * Check for symbolic link
1038          */
1039         KASSERT(dp->v_type != VLNK || !(cnp->cn_flags & FOLLOW),
1040             ("relookup: symlink found.\n"));
1041
1042         /* ASSERT(dvp == ndp->ni_startdir) */
1043         if (cnp->cn_flags & SAVESTART)
1044                 VREF(dvp);
1045         
1046         if ((cnp->cn_flags & LOCKLEAF) == 0)
1047                 VOP_UNLOCK(dp, 0);
1048         return (0);
1049 bad:
1050         vput(dp);
1051         *vpp = NULL;
1052         return (error);
1053 }
1054
1055 void
1056 NDINIT_ALL(struct nameidata *ndp, u_long op, u_long flags, enum uio_seg segflg,
1057     const char *namep, int dirfd, struct vnode *startdir, cap_rights_t *rightsp,
1058     struct thread *td)
1059 {
1060
1061         ndp->ni_cnd.cn_nameiop = op;
1062         ndp->ni_cnd.cn_flags = flags;
1063         ndp->ni_segflg = segflg;
1064         ndp->ni_dirp = namep;
1065         ndp->ni_dirfd = dirfd;
1066         ndp->ni_startdir = startdir;
1067         ndp->ni_strictrelative = 0;
1068         if (rightsp != NULL)
1069                 ndp->ni_rightsneeded = *rightsp;
1070         else
1071                 cap_rights_init(&ndp->ni_rightsneeded);
1072         filecaps_init(&ndp->ni_filecaps);
1073         ndp->ni_cnd.cn_thread = td;
1074 }
1075
1076 /*
1077  * Free data allocated by namei(); see namei(9) for details.
1078  */
1079 void
1080 NDFREE(struct nameidata *ndp, const u_int flags)
1081 {
1082         int unlock_dvp;
1083         int unlock_vp;
1084
1085         unlock_dvp = 0;
1086         unlock_vp = 0;
1087
1088         if (!(flags & NDF_NO_FREE_PNBUF) &&
1089             (ndp->ni_cnd.cn_flags & HASBUF)) {
1090                 uma_zfree(namei_zone, ndp->ni_cnd.cn_pnbuf);
1091                 ndp->ni_cnd.cn_flags &= ~HASBUF;
1092         }
1093         if (!(flags & NDF_NO_VP_UNLOCK) &&
1094             (ndp->ni_cnd.cn_flags & LOCKLEAF) && ndp->ni_vp)
1095                 unlock_vp = 1;
1096         if (!(flags & NDF_NO_VP_RELE) && ndp->ni_vp) {
1097                 if (unlock_vp) {
1098                         vput(ndp->ni_vp);
1099                         unlock_vp = 0;
1100                 } else
1101                         vrele(ndp->ni_vp);
1102                 ndp->ni_vp = NULL;
1103         }
1104         if (unlock_vp)
1105                 VOP_UNLOCK(ndp->ni_vp, 0);
1106         if (!(flags & NDF_NO_DVP_UNLOCK) &&
1107             (ndp->ni_cnd.cn_flags & LOCKPARENT) &&
1108             ndp->ni_dvp != ndp->ni_vp)
1109                 unlock_dvp = 1;
1110         if (!(flags & NDF_NO_DVP_RELE) &&
1111             (ndp->ni_cnd.cn_flags & (LOCKPARENT|WANTPARENT))) {
1112                 if (unlock_dvp) {
1113                         vput(ndp->ni_dvp);
1114                         unlock_dvp = 0;
1115                 } else
1116                         vrele(ndp->ni_dvp);
1117                 ndp->ni_dvp = NULL;
1118         }
1119         if (unlock_dvp)
1120                 VOP_UNLOCK(ndp->ni_dvp, 0);
1121         if (!(flags & NDF_NO_STARTDIR_RELE) &&
1122             (ndp->ni_cnd.cn_flags & SAVESTART)) {
1123                 vrele(ndp->ni_startdir);
1124                 ndp->ni_startdir = NULL;
1125         }
1126 }
1127
1128 /*
1129  * Determine if there is a suitable alternate filename under the specified
1130  * prefix for the specified path.  If the create flag is set, then the
1131  * alternate prefix will be used so long as the parent directory exists.
1132  * This is used by the various compatiblity ABIs so that Linux binaries prefer
1133  * files under /compat/linux for example.  The chosen path (whether under
1134  * the prefix or under /) is returned in a kernel malloc'd buffer pointed
1135  * to by pathbuf.  The caller is responsible for free'ing the buffer from
1136  * the M_TEMP bucket if one is returned.
1137  */
1138 int
1139 kern_alternate_path(struct thread *td, const char *prefix, const char *path,
1140     enum uio_seg pathseg, char **pathbuf, int create, int dirfd)
1141 {
1142         struct nameidata nd, ndroot;
1143         char *ptr, *buf, *cp;
1144         size_t len, sz;
1145         int error;
1146
1147         buf = (char *) malloc(MAXPATHLEN, M_TEMP, M_WAITOK);
1148         *pathbuf = buf;
1149
1150         /* Copy the prefix into the new pathname as a starting point. */
1151         len = strlcpy(buf, prefix, MAXPATHLEN);
1152         if (len >= MAXPATHLEN) {
1153                 *pathbuf = NULL;
1154                 free(buf, M_TEMP);
1155                 return (EINVAL);
1156         }
1157         sz = MAXPATHLEN - len;
1158         ptr = buf + len;
1159
1160         /* Append the filename to the prefix. */
1161         if (pathseg == UIO_SYSSPACE)
1162                 error = copystr(path, ptr, sz, &len);
1163         else
1164                 error = copyinstr(path, ptr, sz, &len);
1165
1166         if (error) {
1167                 *pathbuf = NULL;
1168                 free(buf, M_TEMP);
1169                 return (error);
1170         }
1171
1172         /* Only use a prefix with absolute pathnames. */
1173         if (*ptr != '/') {
1174                 error = EINVAL;
1175                 goto keeporig;
1176         }
1177
1178         if (dirfd != AT_FDCWD) {
1179                 /*
1180                  * We want the original because the "prefix" is
1181                  * included in the already opened dirfd.
1182                  */
1183                 bcopy(ptr, buf, len);
1184                 return (0);
1185         }
1186
1187         /*
1188          * We know that there is a / somewhere in this pathname.
1189          * Search backwards for it, to find the file's parent dir
1190          * to see if it exists in the alternate tree. If it does,
1191          * and we want to create a file (cflag is set). We don't
1192          * need to worry about the root comparison in this case.
1193          */
1194
1195         if (create) {
1196                 for (cp = &ptr[len] - 1; *cp != '/'; cp--);
1197                 *cp = '\0';
1198
1199                 NDINIT(&nd, LOOKUP, FOLLOW, UIO_SYSSPACE, buf, td);
1200                 error = namei(&nd);
1201                 *cp = '/';
1202                 if (error != 0)
1203                         goto keeporig;
1204         } else {
1205                 NDINIT(&nd, LOOKUP, FOLLOW, UIO_SYSSPACE, buf, td);
1206
1207                 error = namei(&nd);
1208                 if (error != 0)
1209                         goto keeporig;
1210
1211                 /*
1212                  * We now compare the vnode of the prefix to the one
1213                  * vnode asked. If they resolve to be the same, then we
1214                  * ignore the match so that the real root gets used.
1215                  * This avoids the problem of traversing "../.." to find the
1216                  * root directory and never finding it, because "/" resolves
1217                  * to the emulation root directory. This is expensive :-(
1218                  */
1219                 NDINIT(&ndroot, LOOKUP, FOLLOW, UIO_SYSSPACE, prefix,
1220                     td);
1221
1222                 /* We shouldn't ever get an error from this namei(). */
1223                 error = namei(&ndroot);
1224                 if (error == 0) {
1225                         if (nd.ni_vp == ndroot.ni_vp)
1226                                 error = ENOENT;
1227
1228                         NDFREE(&ndroot, NDF_ONLY_PNBUF);
1229                         vrele(ndroot.ni_vp);
1230                 }
1231         }
1232
1233         NDFREE(&nd, NDF_ONLY_PNBUF);
1234         vrele(nd.ni_vp);
1235
1236 keeporig:
1237         /* If there was an error, use the original path name. */
1238         if (error)
1239                 bcopy(ptr, buf, len);
1240         return (error);
1241 }