]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/kern/vfs_lookup.c
This commit was generated by cvs2svn to compensate for changes in r147455,
[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_ktrace.h"
41 #include "opt_mac.h"
42 #include "opt_vfs.h"
43
44 #include <sys/param.h>
45 #include <sys/systm.h>
46 #include <sys/kernel.h>
47 #include <sys/lock.h>
48 #include <sys/mac.h>
49 #include <sys/mutex.h>
50 #include <sys/namei.h>
51 #include <sys/vnode.h>
52 #include <sys/mount.h>
53 #include <sys/filedesc.h>
54 #include <sys/proc.h>
55 #include <sys/syscallsubr.h>
56 #include <sys/sysctl.h>
57 #ifdef KTRACE
58 #include <sys/ktrace.h>
59 #endif
60
61 #include <vm/uma.h>
62
63 #define NAMEI_DIAGNOSTIC 1
64 #undef NAMEI_DIAGNOSTIC
65
66 /*
67  * Allocation zone for namei
68  */
69 uma_zone_t namei_zone;
70
71 static void
72 nameiinit(void *dummy __unused)
73 {
74         namei_zone = uma_zcreate("NAMEI", MAXPATHLEN, NULL, NULL, NULL, NULL,
75             UMA_ALIGN_PTR, 0);
76
77 }
78 SYSINIT(vfs, SI_SUB_VFS, SI_ORDER_SECOND, nameiinit, NULL)
79
80 #ifdef LOOKUP_SHARED
81 static int lookup_shared = 1;
82 #else
83 static int lookup_shared = 0;
84 #endif
85 SYSCTL_INT(_vfs, OID_AUTO, lookup_shared, CTLFLAG_RW, &lookup_shared, 0,
86     "Enables/Disables shared locks for path name translation");
87
88 /*
89  * Convert a pathname into a pointer to a locked inode.
90  *
91  * The FOLLOW flag is set when symbolic links are to be followed
92  * when they occur at the end of the name translation process.
93  * Symbolic links are always followed for all other pathname
94  * components other than the last.
95  *
96  * The segflg defines whether the name is to be copied from user
97  * space or kernel space.
98  *
99  * Overall outline of namei:
100  *
101  *      copy in name
102  *      get starting directory
103  *      while (!done && !error) {
104  *              call lookup to search path.
105  *              if symbolic link, massage name in buffer and continue
106  *      }
107  */
108 int
109 namei(ndp)
110         register struct nameidata *ndp;
111 {
112         register struct filedesc *fdp;  /* pointer to file descriptor state */
113         register char *cp;              /* pointer into pathname argument */
114         register struct vnode *dp;      /* the directory we are searching */
115         struct iovec aiov;              /* uio for reading symbolic links */
116         struct uio auio;
117         int error, linklen;
118         struct componentname *cnp = &ndp->ni_cnd;
119         struct thread *td = cnp->cn_thread;
120         struct proc *p = td->td_proc;
121         int vfslocked;
122
123         ndp->ni_cnd.cn_cred = ndp->ni_cnd.cn_thread->td_ucred;
124         KASSERT(cnp->cn_cred && p, ("namei: bad cred/proc"));
125         KASSERT((cnp->cn_nameiop & (~OPMASK)) == 0,
126             ("namei: nameiop contaminated with flags"));
127         KASSERT((cnp->cn_flags & OPMASK) == 0,
128             ("namei: flags contaminated with nameiops"));
129         if (!lookup_shared)
130                 cnp->cn_flags &= ~LOCKSHARED;
131         fdp = p->p_fd;
132
133         /*
134          * Get a buffer for the name to be translated, and copy the
135          * name into the buffer.
136          */
137         if ((cnp->cn_flags & HASBUF) == 0)
138                 cnp->cn_pnbuf = uma_zalloc(namei_zone, M_WAITOK);
139         if (ndp->ni_segflg == UIO_SYSSPACE)
140                 error = copystr(ndp->ni_dirp, cnp->cn_pnbuf,
141                             MAXPATHLEN, (size_t *)&ndp->ni_pathlen);
142         else
143                 error = copyinstr(ndp->ni_dirp, cnp->cn_pnbuf,
144                             MAXPATHLEN, (size_t *)&ndp->ni_pathlen);
145
146         /*
147          * Don't allow empty pathnames.
148          */
149         if (!error && *cnp->cn_pnbuf == '\0')
150                 error = ENOENT;
151
152         if (error) {
153                 uma_zfree(namei_zone, cnp->cn_pnbuf);
154 #ifdef DIAGNOSTIC
155                 cnp->cn_pnbuf = NULL;
156                 cnp->cn_nameptr = NULL;
157 #endif
158                 ndp->ni_vp = NULL;
159                 return (error);
160         }
161         ndp->ni_loopcnt = 0;
162 #ifdef KTRACE
163         if (KTRPOINT(td, KTR_NAMEI)) {
164                 KASSERT(cnp->cn_thread == curthread,
165                     ("namei not using curthread"));
166                 ktrnamei(cnp->cn_pnbuf);
167         }
168 #endif
169
170         /*
171          * Get starting point for the translation.
172          */
173         FILEDESC_LOCK(fdp);
174         ndp->ni_rootdir = fdp->fd_rdir;
175         ndp->ni_topdir = fdp->fd_jdir;
176
177         dp = fdp->fd_cdir;
178         vfslocked = VFS_LOCK_GIANT(dp->v_mount);
179         VREF(dp);
180         FILEDESC_UNLOCK(fdp);
181         for (;;) {
182                 /*
183                  * Check if root directory should replace current directory.
184                  * Done at start of translation and after symbolic link.
185                  */
186                 cnp->cn_nameptr = cnp->cn_pnbuf;
187                 if (*(cnp->cn_nameptr) == '/') {
188                         vrele(dp);
189                         VFS_UNLOCK_GIANT(vfslocked);
190                         while (*(cnp->cn_nameptr) == '/') {
191                                 cnp->cn_nameptr++;
192                                 ndp->ni_pathlen--;
193                         }
194                         dp = ndp->ni_rootdir;
195                         vfslocked = VFS_LOCK_GIANT(dp->v_mount);
196                         VREF(dp);
197                 }
198                 if (vfslocked)
199                         ndp->ni_cnd.cn_flags |= GIANTHELD;
200                 ndp->ni_startdir = dp;
201                 error = lookup(ndp);
202                 if (error) {
203                         uma_zfree(namei_zone, cnp->cn_pnbuf);
204 #ifdef DIAGNOSTIC
205                         cnp->cn_pnbuf = NULL;
206                         cnp->cn_nameptr = NULL;
207 #endif
208                         return (error);
209                 }
210                 vfslocked = (ndp->ni_cnd.cn_flags & GIANTHELD) != 0;
211                 ndp->ni_cnd.cn_flags &= ~GIANTHELD;
212                 /*
213                  * Check for symbolic link
214                  */
215                 if ((cnp->cn_flags & ISSYMLINK) == 0) {
216                         if ((cnp->cn_flags & (SAVENAME | SAVESTART)) == 0) {
217                                 uma_zfree(namei_zone, cnp->cn_pnbuf);
218 #ifdef DIAGNOSTIC
219                                 cnp->cn_pnbuf = NULL;
220                                 cnp->cn_nameptr = NULL;
221 #endif
222                         } else
223                                 cnp->cn_flags |= HASBUF;
224
225                         if ((cnp->cn_flags & MPSAFE) == 0) {
226                                 VFS_UNLOCK_GIANT(vfslocked);
227                         } else if (vfslocked)
228                                 ndp->ni_cnd.cn_flags |= GIANTHELD;
229                         return (0);
230                 }
231                 if (ndp->ni_loopcnt++ >= MAXSYMLINKS) {
232                         error = ELOOP;
233                         break;
234                 }
235 #ifdef MAC
236                 if ((cnp->cn_flags & NOMACCHECK) == 0) {
237                         error = mac_check_vnode_readlink(td->td_ucred,
238                             ndp->ni_vp);
239                         if (error)
240                                 break;
241                 }
242 #endif
243                 if (ndp->ni_pathlen > 1)
244                         cp = uma_zalloc(namei_zone, M_WAITOK);
245                 else
246                         cp = cnp->cn_pnbuf;
247                 aiov.iov_base = cp;
248                 aiov.iov_len = MAXPATHLEN;
249                 auio.uio_iov = &aiov;
250                 auio.uio_iovcnt = 1;
251                 auio.uio_offset = 0;
252                 auio.uio_rw = UIO_READ;
253                 auio.uio_segflg = UIO_SYSSPACE;
254                 auio.uio_td = (struct thread *)0;
255                 auio.uio_resid = MAXPATHLEN;
256                 error = VOP_READLINK(ndp->ni_vp, &auio, cnp->cn_cred);
257                 if (error) {
258                         if (ndp->ni_pathlen > 1)
259                                 uma_zfree(namei_zone, cp);
260                         break;
261                 }
262                 linklen = MAXPATHLEN - auio.uio_resid;
263                 if (linklen == 0) {
264                         if (ndp->ni_pathlen > 1)
265                                 uma_zfree(namei_zone, cp);
266                         error = ENOENT;
267                         break;
268                 }
269                 if (linklen + ndp->ni_pathlen >= MAXPATHLEN) {
270                         if (ndp->ni_pathlen > 1)
271                                 uma_zfree(namei_zone, cp);
272                         error = ENAMETOOLONG;
273                         break;
274                 }
275                 if (ndp->ni_pathlen > 1) {
276                         bcopy(ndp->ni_next, cp + linklen, ndp->ni_pathlen);
277                         uma_zfree(namei_zone, cnp->cn_pnbuf);
278                         cnp->cn_pnbuf = cp;
279                 } else
280                         cnp->cn_pnbuf[linklen] = '\0';
281                 ndp->ni_pathlen += linklen;
282                 vput(ndp->ni_vp);
283                 dp = ndp->ni_dvp;
284         }
285         uma_zfree(namei_zone, cnp->cn_pnbuf);
286 #ifdef DIAGNOSTIC
287         cnp->cn_pnbuf = NULL;
288         cnp->cn_nameptr = NULL;
289 #endif
290         vput(ndp->ni_vp);
291         ndp->ni_vp = NULL;
292         vrele(ndp->ni_dvp);
293         VFS_UNLOCK_GIANT(vfslocked);
294         return (error);
295 }
296
297 /*
298  * Search a pathname.
299  * This is a very central and rather complicated routine.
300  *
301  * The pathname is pointed to by ni_ptr and is of length ni_pathlen.
302  * The starting directory is taken from ni_startdir. The pathname is
303  * descended until done, or a symbolic link is encountered. The variable
304  * ni_more is clear if the path is completed; it is set to one if a
305  * symbolic link needing interpretation is encountered.
306  *
307  * The flag argument is LOOKUP, CREATE, RENAME, or DELETE depending on
308  * whether the name is to be looked up, created, renamed, or deleted.
309  * When CREATE, RENAME, or DELETE is specified, information usable in
310  * creating, renaming, or deleting a directory entry may be calculated.
311  * If flag has LOCKPARENT or'ed into it, the parent directory is returned
312  * locked. If flag has WANTPARENT or'ed into it, the parent directory is
313  * returned unlocked. Otherwise the parent directory is not returned. If
314  * the target of the pathname exists and LOCKLEAF is or'ed into the flag
315  * the target is returned locked, otherwise it is returned unlocked.
316  * When creating or renaming and LOCKPARENT is specified, the target may not
317  * be ".".  When deleting and LOCKPARENT is specified, the target may be ".".
318  *
319  * Overall outline of lookup:
320  *
321  * dirloop:
322  *      identify next component of name at ndp->ni_ptr
323  *      handle degenerate case where name is null string
324  *      if .. and crossing mount points and on mounted filesys, find parent
325  *      call VOP_LOOKUP routine for next component name
326  *          directory vnode returned in ni_dvp, unlocked unless LOCKPARENT set
327  *          component vnode returned in ni_vp (if it exists), locked.
328  *      if result vnode is mounted on and crossing mount points,
329  *          find mounted on vnode
330  *      if more components of name, do next level at dirloop
331  *      return the answer in ni_vp, locked if LOCKLEAF set
332  *          if LOCKPARENT set, return locked parent in ni_dvp
333  *          if WANTPARENT set, return unlocked parent in ni_dvp
334  */
335 int
336 lookup(ndp)
337         register struct nameidata *ndp;
338 {
339         register char *cp;              /* pointer into pathname argument */
340         register struct vnode *dp = 0;  /* the directory we are searching */
341         struct vnode *tdp;              /* saved dp */
342         struct mount *mp;               /* mount table entry */
343         int docache;                    /* == 0 do not cache last component */
344         int wantparent;                 /* 1 => wantparent or lockparent flag */
345         int rdonly;                     /* lookup read-only flag bit */
346         int trailing_slash;
347         int error = 0;
348         int dpunlocked = 0;             /* dp has already been unlocked */
349         struct componentname *cnp = &ndp->ni_cnd;
350         struct thread *td = cnp->cn_thread;
351         int vfslocked;
352         int tvfslocked;
353
354         /*
355          * Setup: break out flag bits into variables.
356          */
357         vfslocked = (ndp->ni_cnd.cn_flags & GIANTHELD) != 0;
358         ndp->ni_cnd.cn_flags &= ~GIANTHELD;
359         wantparent = cnp->cn_flags & (LOCKPARENT | WANTPARENT);
360         KASSERT(cnp->cn_nameiop == LOOKUP || wantparent,
361             ("CREATE, DELETE, RENAME require LOCKPARENT or WANTPARENT."));
362         docache = (cnp->cn_flags & NOCACHE) ^ NOCACHE;
363         if (cnp->cn_nameiop == DELETE ||
364             (wantparent && cnp->cn_nameiop != CREATE &&
365              cnp->cn_nameiop != LOOKUP))
366                 docache = 0;
367         rdonly = cnp->cn_flags & RDONLY;
368         cnp->cn_flags &= ~ISSYMLINK;
369         ndp->ni_dvp = NULL;
370         /*
371          * We use shared locks until we hit the parent of the last cn then
372          * we adjust based on the requesting flags.
373          */
374         if (lookup_shared)
375                 cnp->cn_lkflags = LK_SHARED;
376         else
377                 cnp->cn_lkflags = LK_EXCLUSIVE;
378         dp = ndp->ni_startdir;
379         ndp->ni_startdir = NULLVP;
380         vn_lock(dp, cnp->cn_lkflags | LK_RETRY, td);
381
382 dirloop:
383         /*
384          * Search a new directory.
385          *
386          * The last component of the filename is left accessible via
387          * cnp->cn_nameptr for callers that need the name. Callers needing
388          * the name set the SAVENAME flag. When done, they assume
389          * responsibility for freeing the pathname buffer.
390          */
391         cnp->cn_consume = 0;
392         for (cp = cnp->cn_nameptr; *cp != 0 && *cp != '/'; cp++)
393                 continue;
394         cnp->cn_namelen = cp - cnp->cn_nameptr;
395         if (cnp->cn_namelen > NAME_MAX) {
396                 error = ENAMETOOLONG;
397                 goto bad;
398         }
399 #ifdef NAMEI_DIAGNOSTIC
400         { char c = *cp;
401         *cp = '\0';
402         printf("{%s}: ", cnp->cn_nameptr);
403         *cp = c; }
404 #endif
405         ndp->ni_pathlen -= cnp->cn_namelen;
406         ndp->ni_next = cp;
407
408         /*
409          * Replace multiple slashes by a single slash and trailing slashes
410          * by a null.  This must be done before VOP_LOOKUP() because some
411          * fs's don't know about trailing slashes.  Remember if there were
412          * trailing slashes to handle symlinks, existing non-directories
413          * and non-existing files that won't be directories specially later.
414          */
415         trailing_slash = 0;
416         while (*cp == '/' && (cp[1] == '/' || cp[1] == '\0')) {
417                 cp++;
418                 ndp->ni_pathlen--;
419                 if (*cp == '\0') {
420                         trailing_slash = 1;
421                         *ndp->ni_next = '\0';   /* XXX for direnter() ... */
422                 }
423         }
424         ndp->ni_next = cp;
425
426         cnp->cn_flags |= MAKEENTRY;
427         if (*cp == '\0' && docache == 0)
428                 cnp->cn_flags &= ~MAKEENTRY;
429         if (cnp->cn_namelen == 2 &&
430             cnp->cn_nameptr[1] == '.' && cnp->cn_nameptr[0] == '.')
431                 cnp->cn_flags |= ISDOTDOT;
432         else
433                 cnp->cn_flags &= ~ISDOTDOT;
434         if (*ndp->ni_next == 0)
435                 cnp->cn_flags |= ISLASTCN;
436         else
437                 cnp->cn_flags &= ~ISLASTCN;
438
439
440         /*
441          * Check for degenerate name (e.g. / or "")
442          * which is a way of talking about a directory,
443          * e.g. like "/." or ".".
444          */
445         if (cnp->cn_nameptr[0] == '\0') {
446                 if (dp->v_type != VDIR) {
447                         error = ENOTDIR;
448                         goto bad;
449                 }
450                 if (cnp->cn_nameiop != LOOKUP) {
451                         error = EISDIR;
452                         goto bad;
453                 }
454                 if (wantparent) {
455                         ndp->ni_dvp = dp;
456                         VREF(dp);
457                 }
458                 ndp->ni_vp = dp;
459                 if (!(cnp->cn_flags & (LOCKPARENT | LOCKLEAF)))
460                         VOP_UNLOCK(dp, 0, td);
461                 /* XXX This should probably move to the top of function. */
462                 if (cnp->cn_flags & SAVESTART)
463                         panic("lookup: SAVESTART");
464                 goto success;
465         }
466
467         /*
468          * Handle "..": two special cases.
469          * 1. If at root directory (e.g. after chroot)
470          *    or at absolute root directory
471          *    then ignore it so can't get out.
472          * 2. If this vnode is the root of a mounted
473          *    filesystem, then replace it with the
474          *    vnode which was mounted on so we take the
475          *    .. in the other filesystem.
476          * 3. If the vnode is the top directory of
477          *    the jail or chroot, don't let them out.
478          */
479         if (cnp->cn_flags & ISDOTDOT) {
480                 for (;;) {
481                         if (dp == ndp->ni_rootdir || 
482                             dp == ndp->ni_topdir || 
483                             dp == rootvnode) {
484                                 ndp->ni_dvp = dp;
485                                 ndp->ni_vp = dp;
486                                 VREF(dp);
487                                 goto nextname;
488                         }
489                         if ((dp->v_vflag & VV_ROOT) == 0 ||
490                             (cnp->cn_flags & NOCROSSMOUNT))
491                                 break;
492                         if (dp->v_mount == NULL) {      /* forced unmount */
493                                 error = EBADF;
494                                 goto bad;
495                         }
496                         tdp = dp;
497                         dp = dp->v_mount->mnt_vnodecovered;
498                         tvfslocked = vfslocked;
499                         vfslocked = VFS_LOCK_GIANT(dp->v_mount);
500                         VREF(dp);
501                         vput(tdp);
502                         VFS_UNLOCK_GIANT(tvfslocked);
503                         vn_lock(dp, cnp->cn_lkflags | LK_RETRY, td);
504                 }
505         }
506
507         /*
508          * We now have a segment name to search for, and a directory to search.
509          */
510 unionlookup:
511 #ifdef MAC
512         if ((cnp->cn_flags & NOMACCHECK) == 0) {
513                 error = mac_check_vnode_lookup(td->td_ucred, dp, cnp);
514                 if (error)
515                         goto bad;
516         }
517 #endif
518         ndp->ni_dvp = dp;
519         ndp->ni_vp = NULL;
520         ASSERT_VOP_LOCKED(dp, "lookup");
521         /*
522          * If we have a shared lock we may need to upgrade the lock for the
523          * last operation.
524          */
525         if (VOP_ISLOCKED(dp, td) == LK_SHARED &&
526             (cnp->cn_flags & ISLASTCN) && (cnp->cn_flags & LOCKPARENT))
527                 vn_lock(dp, LK_UPGRADE|LK_RETRY, td);
528         /*
529          * If we're looking up the last component and we need an exclusive
530          * lock, adjust our lkflags.
531          */
532         if ((cnp->cn_flags & (ISLASTCN|LOCKSHARED|LOCKLEAF)) ==
533             (ISLASTCN|LOCKLEAF))
534                 cnp->cn_lkflags = LK_EXCLUSIVE;
535 #ifdef NAMEI_DIAGNOSTIC
536         vprint("lookup in", dp);
537 #endif
538         if ((error = VOP_LOOKUP(dp, &ndp->ni_vp, cnp)) != 0) {
539                 KASSERT(ndp->ni_vp == NULL, ("leaf should be empty"));
540 #ifdef NAMEI_DIAGNOSTIC
541                 printf("not found\n");
542 #endif
543                 if ((error == ENOENT) &&
544                     (dp->v_vflag & VV_ROOT) && (dp->v_mount != NULL) &&
545                     (dp->v_mount->mnt_flag & MNT_UNION)) {
546                         tdp = dp;
547                         dp = dp->v_mount->mnt_vnodecovered;
548                         tvfslocked = vfslocked;
549                         vfslocked = VFS_LOCK_GIANT(dp->v_mount);
550                         VREF(dp);
551                         vput(tdp);
552                         VFS_UNLOCK_GIANT(tvfslocked);
553                         vn_lock(dp, cnp->cn_lkflags | LK_RETRY, td);
554                         goto unionlookup;
555                 }
556
557                 if (error != EJUSTRETURN)
558                         goto bad;
559                 /*
560                  * If creating and at end of pathname, then can consider
561                  * allowing file to be created.
562                  */
563                 if (rdonly) {
564                         error = EROFS;
565                         goto bad;
566                 }
567                 if (*cp == '\0' && trailing_slash &&
568                      !(cnp->cn_flags & WILLBEDIR)) {
569                         error = ENOENT;
570                         goto bad;
571                 }
572                 if ((cnp->cn_flags & LOCKPARENT) == 0)
573                         VOP_UNLOCK(dp, 0, td);
574                 /*
575                  * This is a temporary assert to make sure I know what the
576                  * behavior here was.
577                  */
578                 KASSERT((cnp->cn_flags & (WANTPARENT|LOCKPARENT)) != 0,
579                    ("lookup: Unhandled case."));
580                 /*
581                  * We return with ni_vp NULL to indicate that the entry
582                  * doesn't currently exist, leaving a pointer to the
583                  * (possibly locked) directory inode in ndp->ni_dvp.
584                  */
585                 if (cnp->cn_flags & SAVESTART) {
586                         ndp->ni_startdir = ndp->ni_dvp;
587                         VREF(ndp->ni_startdir);
588                 }
589                 goto success;
590         }
591 #ifdef NAMEI_DIAGNOSTIC
592         printf("found\n");
593 #endif
594         /*
595          * Take into account any additional components consumed by
596          * the underlying filesystem.
597          */
598         if (cnp->cn_consume > 0) {
599                 cnp->cn_nameptr += cnp->cn_consume;
600                 ndp->ni_next += cnp->cn_consume;
601                 ndp->ni_pathlen -= cnp->cn_consume;
602                 cnp->cn_consume = 0;
603         }
604
605         dp = ndp->ni_vp;
606
607         /*
608          * Check to see if the vnode has been mounted on;
609          * if so find the root of the mounted filesystem.
610          */
611         while (dp->v_type == VDIR && (mp = dp->v_mountedhere) &&
612                (cnp->cn_flags & NOCROSSMOUNT) == 0) {
613                 KASSERT(dp != ndp->ni_dvp, ("XXX"));
614                 if (vfs_busy(mp, 0, 0, td))
615                         continue;
616                 vput(dp);
617                 tvfslocked = VFS_LOCK_GIANT(mp);
618                 VFS_UNLOCK_GIANT(vfslocked);
619                 vfslocked = tvfslocked;
620                 error = VFS_ROOT(mp, cnp->cn_lkflags, &tdp, td);
621                 vfs_unbusy(mp, td);
622                 if (error) {
623                         dpunlocked = 1;
624                         goto bad2;
625                 }
626                 ndp->ni_vp = dp = tdp;
627         }
628
629         /*
630          * Check for symbolic link
631          */
632         if ((dp->v_type == VLNK) &&
633             ((cnp->cn_flags & FOLLOW) || trailing_slash ||
634              *ndp->ni_next == '/')) {
635                 cnp->cn_flags |= ISSYMLINK;
636                 if (dp->v_mount == NULL) {
637                         /* We can't know whether the directory was mounted with
638                          * NOSYMFOLLOW, so we can't follow safely. */
639                         error = EBADF;
640                         goto bad2;
641                 }
642                 if (dp->v_mount->mnt_flag & MNT_NOSYMFOLLOW) {
643                         error = EACCES;
644                         goto bad2;
645                 }
646                 /*
647                  * Symlink code always expects an unlocked dvp.
648                  */
649                 if (ndp->ni_dvp != ndp->ni_vp)
650                         VOP_UNLOCK(ndp->ni_dvp, 0, td);
651                 goto success;
652         }
653
654         /*
655          * Check for bogus trailing slashes.
656          */
657         if (trailing_slash && dp->v_type != VDIR) {
658                 error = ENOTDIR;
659                 goto bad2;
660         }
661
662 nextname:
663         /*
664          * Not a symbolic link.  If more pathname,
665          * continue at next component, else return.
666          */
667         KASSERT((cnp->cn_flags & ISLASTCN) || *ndp->ni_next == '/',
668             ("lookup: invalid path state."));
669         if (*ndp->ni_next == '/') {
670                 cnp->cn_nameptr = ndp->ni_next;
671                 while (*cnp->cn_nameptr == '/') {
672                         cnp->cn_nameptr++;
673                         ndp->ni_pathlen--;
674                 }
675                 if (ndp->ni_dvp != dp)
676                         vput(ndp->ni_dvp);
677                 else
678                         vrele(ndp->ni_dvp);
679                 goto dirloop;
680         }
681         /*
682          * Disallow directory write attempts on read-only filesystems.
683          */
684         if (rdonly &&
685             (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME)) {
686                 error = EROFS;
687                 goto bad2;
688         }
689         if (cnp->cn_flags & SAVESTART) {
690                 ndp->ni_startdir = ndp->ni_dvp;
691                 VREF(ndp->ni_startdir);
692         }
693         if (!wantparent) {
694                 if (ndp->ni_dvp != dp)
695                         vput(ndp->ni_dvp);
696                 else
697                         vrele(ndp->ni_dvp);
698         } else if ((cnp->cn_flags & LOCKPARENT) == 0 && ndp->ni_dvp != dp)
699                 VOP_UNLOCK(ndp->ni_dvp, 0, td);
700
701         if ((cnp->cn_flags & LOCKLEAF) == 0)
702                 VOP_UNLOCK(dp, 0, td);
703 success:
704         if (vfslocked)
705                 ndp->ni_cnd.cn_flags |= GIANTHELD;
706         return (0);
707
708 bad2:
709         if (dp != ndp->ni_dvp)
710                 vput(ndp->ni_dvp);
711         else
712                 vrele(ndp->ni_dvp);
713 bad:
714         if (!dpunlocked)
715                 vput(dp);
716         VFS_UNLOCK_GIANT(vfslocked);
717         ndp->ni_cnd.cn_flags &= ~GIANTHELD;
718         ndp->ni_vp = NULL;
719         return (error);
720 }
721
722 /*
723  * relookup - lookup a path name component
724  *    Used by lookup to re-aquire things.
725  */
726 int
727 relookup(dvp, vpp, cnp)
728         struct vnode *dvp, **vpp;
729         struct componentname *cnp;
730 {
731         struct thread *td = cnp->cn_thread;
732         struct vnode *dp = 0;           /* the directory we are searching */
733         int wantparent;                 /* 1 => wantparent or lockparent flag */
734         int rdonly;                     /* lookup read-only flag bit */
735         int error = 0;
736
737         KASSERT(cnp->cn_flags & ISLASTCN,
738             ("relookup: Not given last component."));
739         /*
740          * Setup: break out flag bits into variables.
741          */
742         wantparent = cnp->cn_flags & (LOCKPARENT|WANTPARENT);
743         KASSERT(wantparent, ("relookup: parent not wanted."));
744         rdonly = cnp->cn_flags & RDONLY;
745         cnp->cn_flags &= ~ISSYMLINK;
746         dp = dvp;
747         cnp->cn_lkflags = LK_EXCLUSIVE;
748         vn_lock(dp, LK_EXCLUSIVE | LK_RETRY, td);
749
750         /*
751          * Search a new directory.
752          *
753          * The last component of the filename is left accessible via
754          * cnp->cn_nameptr for callers that need the name. Callers needing
755          * the name set the SAVENAME flag. When done, they assume
756          * responsibility for freeing the pathname buffer.
757          */
758 #ifdef NAMEI_DIAGNOSTIC
759         printf("{%s}: ", cnp->cn_nameptr);
760 #endif
761
762         /*
763          * Check for degenerate name (e.g. / or "")
764          * which is a way of talking about a directory,
765          * e.g. like "/." or ".".
766          */
767         if (cnp->cn_nameptr[0] == '\0') {
768                 if (cnp->cn_nameiop != LOOKUP || wantparent) {
769                         error = EISDIR;
770                         goto bad;
771                 }
772                 if (dp->v_type != VDIR) {
773                         error = ENOTDIR;
774                         goto bad;
775                 }
776                 if (!(cnp->cn_flags & LOCKLEAF))
777                         VOP_UNLOCK(dp, 0, td);
778                 *vpp = dp;
779                 /* XXX This should probably move to the top of function. */
780                 if (cnp->cn_flags & SAVESTART)
781                         panic("lookup: SAVESTART");
782                 return (0);
783         }
784
785         if (cnp->cn_flags & ISDOTDOT)
786                 panic ("relookup: lookup on dot-dot");
787
788         /*
789          * We now have a segment name to search for, and a directory to search.
790          */
791 #ifdef NAMEI_DIAGNOSTIC
792         vprint("search in:", dp);
793 #endif
794         if ((error = VOP_LOOKUP(dp, vpp, cnp)) != 0) {
795                 KASSERT(*vpp == NULL, ("leaf should be empty"));
796                 if (error != EJUSTRETURN)
797                         goto bad;
798                 /*
799                  * If creating and at end of pathname, then can consider
800                  * allowing file to be created.
801                  */
802                 if (rdonly) {
803                         error = EROFS;
804                         goto bad;
805                 }
806                 /* ASSERT(dvp == ndp->ni_startdir) */
807                 if (cnp->cn_flags & SAVESTART)
808                         VREF(dvp);
809                 if ((cnp->cn_flags & LOCKPARENT) == 0)
810                         VOP_UNLOCK(dp, 0, td);
811                 /*
812                  * This is a temporary assert to make sure I know what the
813                  * behavior here was.
814                  */
815                 KASSERT((cnp->cn_flags & (WANTPARENT|LOCKPARENT)) != 0,
816                    ("relookup: Unhandled case."));
817                 /*
818                  * We return with ni_vp NULL to indicate that the entry
819                  * doesn't currently exist, leaving a pointer to the
820                  * (possibly locked) directory inode in ndp->ni_dvp.
821                  */
822                 return (0);
823         }
824         dp = *vpp;
825
826         /*
827          * Disallow directory write attempts on read-only filesystems.
828          */
829         if (rdonly &&
830             (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME)) {
831                 if (dvp == dp)
832                         vrele(dvp);
833                 else
834                         vput(dvp);
835                 error = EROFS;
836                 goto bad;
837         }
838         /*
839          * Set the parent lock/ref state to the requested state.
840          */
841         if ((cnp->cn_flags & LOCKPARENT) == 0 && dvp != dp) {
842                 if (wantparent)
843                         VOP_UNLOCK(dvp, 0, td);
844                 else
845                         vput(dvp);
846         } else if (!wantparent)
847                 vrele(dvp);
848         /*
849          * Check for symbolic link
850          */
851         KASSERT(dp->v_type != VLNK || !(cnp->cn_flags & FOLLOW),
852             ("relookup: symlink found.\n"));
853
854         /* ASSERT(dvp == ndp->ni_startdir) */
855         if (cnp->cn_flags & SAVESTART)
856                 VREF(dvp);
857         
858         if ((cnp->cn_flags & LOCKLEAF) == 0)
859                 VOP_UNLOCK(dp, 0, td);
860         return (0);
861 bad:
862         vput(dp);
863         *vpp = NULL;
864         return (error);
865 }
866
867 /*
868  * Free data allocated by namei(); see namei(9) for details.
869  */
870 void
871 NDFREE(ndp, flags)
872      struct nameidata *ndp;
873      const u_int flags;
874 {
875         int unlock_dvp;
876         int unlock_vp;
877
878         unlock_dvp = 0;
879         unlock_vp = 0;
880
881         if (!(flags & NDF_NO_FREE_PNBUF) &&
882             (ndp->ni_cnd.cn_flags & HASBUF)) {
883                 uma_zfree(namei_zone, ndp->ni_cnd.cn_pnbuf);
884                 ndp->ni_cnd.cn_flags &= ~HASBUF;
885         }
886         if (!(flags & NDF_NO_VP_UNLOCK) &&
887             (ndp->ni_cnd.cn_flags & LOCKLEAF) && ndp->ni_vp)
888                 unlock_vp = 1;
889         if (!(flags & NDF_NO_VP_RELE) && ndp->ni_vp) {
890                 if (unlock_vp) {
891                         vput(ndp->ni_vp);
892                         unlock_vp = 0;
893                 } else
894                         vrele(ndp->ni_vp);
895                 ndp->ni_vp = NULL;
896         }
897         if (unlock_vp)
898                 VOP_UNLOCK(ndp->ni_vp, 0, ndp->ni_cnd.cn_thread);
899         if (!(flags & NDF_NO_DVP_UNLOCK) &&
900             (ndp->ni_cnd.cn_flags & LOCKPARENT) &&
901             ndp->ni_dvp != ndp->ni_vp)
902                 unlock_dvp = 1;
903         if (!(flags & NDF_NO_DVP_RELE) &&
904             (ndp->ni_cnd.cn_flags & (LOCKPARENT|WANTPARENT))) {
905                 if (unlock_dvp) {
906                         vput(ndp->ni_dvp);
907                         unlock_dvp = 0;
908                 } else
909                         vrele(ndp->ni_dvp);
910                 ndp->ni_dvp = NULL;
911         }
912         if (unlock_dvp)
913                 VOP_UNLOCK(ndp->ni_dvp, 0, ndp->ni_cnd.cn_thread);
914         if (!(flags & NDF_NO_STARTDIR_RELE) &&
915             (ndp->ni_cnd.cn_flags & SAVESTART)) {
916                 vrele(ndp->ni_startdir);
917                 ndp->ni_startdir = NULL;
918         }
919 }
920
921 /*
922  * Determine if there is a suitable alternate filename under the specified
923  * prefix for the specified path.  If the create flag is set, then the
924  * alternate prefix will be used so long as the parent directory exists.
925  * This is used by the various compatiblity ABIs so that Linux binaries prefer
926  * files under /compat/linux for example.  The chosen path (whether under
927  * the prefix or under /) is returned in a kernel malloc'd buffer pointed
928  * to by pathbuf.  The caller is responsible for free'ing the buffer from
929  * the M_TEMP bucket if one is returned.
930  */
931 int
932 kern_alternate_path(struct thread *td, const char *prefix, char *path,
933     enum uio_seg pathseg, char **pathbuf, int create)
934 {
935         struct nameidata nd, ndroot;
936         char *ptr, *buf, *cp;
937         size_t len, sz;
938         int error;
939
940         buf = (char *) malloc(MAXPATHLEN, M_TEMP, M_WAITOK);
941         *pathbuf = buf;
942
943         /* Copy the prefix into the new pathname as a starting point. */
944         len = strlcpy(buf, prefix, MAXPATHLEN);
945         if (len >= MAXPATHLEN) {
946                 *pathbuf = NULL;
947                 free(buf, M_TEMP);
948                 return (EINVAL);
949         }
950         sz = MAXPATHLEN - len;
951         ptr = buf + len;
952
953         /* Append the filename to the prefix. */
954         if (pathseg == UIO_SYSSPACE)
955                 error = copystr(path, ptr, sz, &len);
956         else
957                 error = copyinstr(path, ptr, sz, &len);
958
959         if (error) {
960                 *pathbuf = NULL;
961                 free(buf, M_TEMP);
962                 return (error);
963         }
964
965         /* Only use a prefix with absolute pathnames. */
966         if (*ptr != '/') {
967                 error = EINVAL;
968                 goto keeporig;
969         }
970
971         /* XXX: VFS_LOCK_GIANT? */
972         mtx_lock(&Giant);
973
974         /*
975          * We know that there is a / somewhere in this pathname.
976          * Search backwards for it, to find the file's parent dir
977          * to see if it exists in the alternate tree. If it does,
978          * and we want to create a file (cflag is set). We don't
979          * need to worry about the root comparison in this case.
980          */
981
982         if (create) {
983                 for (cp = &ptr[len] - 1; *cp != '/'; cp--);
984                 *cp = '\0';
985
986                 NDINIT(&nd, LOOKUP, FOLLOW, UIO_SYSSPACE, buf, td);
987                 error = namei(&nd);
988                 *cp = '/';
989                 if (error != 0)
990                         goto nd_failed;
991         } else {
992                 NDINIT(&nd, LOOKUP, FOLLOW, UIO_SYSSPACE, buf, td);
993
994                 error = namei(&nd);
995                 if (error != 0)
996                         goto nd_failed;
997
998                 /*
999                  * We now compare the vnode of the prefix to the one
1000                  * vnode asked. If they resolve to be the same, then we
1001                  * ignore the match so that the real root gets used.
1002                  * This avoids the problem of traversing "../.." to find the
1003                  * root directory and never finding it, because "/" resolves
1004                  * to the emulation root directory. This is expensive :-(
1005                  */
1006                 NDINIT(&ndroot, LOOKUP, FOLLOW, UIO_SYSSPACE, prefix, td);
1007
1008                 /* We shouldn't ever get an error from this namei(). */
1009                 error = namei(&ndroot);
1010                 if (error == 0) {
1011                         if (nd.ni_vp == ndroot.ni_vp)
1012                                 error = ENOENT;
1013
1014                         NDFREE(&ndroot, NDF_ONLY_PNBUF);
1015                         vrele(ndroot.ni_vp);
1016                 }
1017         }
1018
1019         NDFREE(&nd, NDF_ONLY_PNBUF);
1020         vrele(nd.ni_vp);
1021
1022 nd_failed:
1023         /* XXX: VFS_UNLOCK_GIANT? */
1024         mtx_unlock(&Giant);
1025
1026 keeporig:
1027         /* If there was an error, use the original path name. */
1028         if (error)
1029                 bcopy(ptr, buf, len);
1030         return (error);
1031 }