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