vfs_lookup.c revision 32011
11541Srgrimes/* 21541Srgrimes * Copyright (c) 1982, 1986, 1989, 1993 31541Srgrimes * The Regents of the University of California. All rights reserved. 41541Srgrimes * (c) UNIX System Laboratories, Inc. 51541Srgrimes * All or some portions of this file are derived from material licensed 61541Srgrimes * to the University of California by American Telephone and Telegraph 71541Srgrimes * Co. or Unix System Laboratories, Inc. and are reproduced herein with 81541Srgrimes * the permission of UNIX System Laboratories, Inc. 91541Srgrimes * 101541Srgrimes * Redistribution and use in source and binary forms, with or without 111541Srgrimes * modification, are permitted provided that the following conditions 121541Srgrimes * are met: 131541Srgrimes * 1. Redistributions of source code must retain the above copyright 141541Srgrimes * notice, this list of conditions and the following disclaimer. 151541Srgrimes * 2. Redistributions in binary form must reproduce the above copyright 161541Srgrimes * notice, this list of conditions and the following disclaimer in the 171541Srgrimes * documentation and/or other materials provided with the distribution. 181541Srgrimes * 3. All advertising materials mentioning features or use of this software 191541Srgrimes * must display the following acknowledgement: 201541Srgrimes * This product includes software developed by the University of 211541Srgrimes * California, Berkeley and its contributors. 221541Srgrimes * 4. Neither the name of the University nor the names of its contributors 231541Srgrimes * may be used to endorse or promote products derived from this software 241541Srgrimes * without specific prior written permission. 251541Srgrimes * 261541Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 271541Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 281541Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 291541Srgrimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 301541Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 311541Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 321541Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 331541Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 341541Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 351541Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 361541Srgrimes * SUCH DAMAGE. 371541Srgrimes * 381541Srgrimes * @(#)vfs_lookup.c 8.4 (Berkeley) 2/16/94 3932011Sbde * $Id: vfs_lookup.c,v 1.20 1997/09/21 04:23:01 dyson Exp $ 401541Srgrimes */ 411541Srgrimes 4213203Swollman#include "opt_ktrace.h" 4313203Swollman 441541Srgrimes#include <sys/param.h> 452112Swollman#include <sys/systm.h> 461541Srgrimes#include <sys/namei.h> 471541Srgrimes#include <sys/vnode.h> 481541Srgrimes#include <sys/mount.h> 491541Srgrimes#include <sys/filedesc.h> 501541Srgrimes#include <sys/proc.h> 511541Srgrimes 521541Srgrimes#ifdef KTRACE 531541Srgrimes#include <sys/ktrace.h> 541541Srgrimes#endif 551541Srgrimes 5632011Sbde#include <vm/vm_zone.h> 5732011Sbde 581541Srgrimes/* 591541Srgrimes * Convert a pathname into a pointer to a locked inode. 601541Srgrimes * 611541Srgrimes * The FOLLOW flag is set when symbolic links are to be followed 621541Srgrimes * when they occur at the end of the name translation process. 631541Srgrimes * Symbolic links are always followed for all other pathname 641541Srgrimes * components other than the last. 651541Srgrimes * 661541Srgrimes * The segflg defines whether the name is to be copied from user 671541Srgrimes * space or kernel space. 681541Srgrimes * 691541Srgrimes * Overall outline of namei: 701541Srgrimes * 711541Srgrimes * copy in name 721541Srgrimes * get starting directory 731541Srgrimes * while (!done && !error) { 741541Srgrimes * call lookup to search path. 751541Srgrimes * if symbolic link, massage name in buffer and continue 761541Srgrimes * } 771541Srgrimes */ 781541Srgrimesint 791541Srgrimesnamei(ndp) 801541Srgrimes register struct nameidata *ndp; 811541Srgrimes{ 821541Srgrimes register struct filedesc *fdp; /* pointer to file descriptor state */ 831541Srgrimes register char *cp; /* pointer into pathname argument */ 841541Srgrimes register struct vnode *dp; /* the directory we are searching */ 851541Srgrimes struct iovec aiov; /* uio for reading symbolic links */ 861541Srgrimes struct uio auio; 871541Srgrimes int error, linklen; 881541Srgrimes struct componentname *cnp = &ndp->ni_cnd; 8922521Sdyson struct proc *p = cnp->cn_proc; 901541Srgrimes 911541Srgrimes ndp->ni_cnd.cn_cred = ndp->ni_cnd.cn_proc->p_ucred; 921541Srgrimes#ifdef DIAGNOSTIC 931541Srgrimes if (!cnp->cn_cred || !cnp->cn_proc) 941541Srgrimes panic ("namei: bad cred/proc"); 951541Srgrimes if (cnp->cn_nameiop & (~OPMASK)) 961541Srgrimes panic ("namei: nameiop contaminated with flags"); 971541Srgrimes if (cnp->cn_flags & OPMASK) 981541Srgrimes panic ("namei: flags contaminated with nameiops"); 991541Srgrimes#endif 1001541Srgrimes fdp = cnp->cn_proc->p_fd; 1011541Srgrimes 1021541Srgrimes /* 1031541Srgrimes * Get a buffer for the name to be translated, and copy the 1041541Srgrimes * name into the buffer. 1051541Srgrimes */ 1061541Srgrimes if ((cnp->cn_flags & HASBUF) == 0) 10729653Sdyson cnp->cn_pnbuf = zalloc(namei_zone); 1081541Srgrimes if (ndp->ni_segflg == UIO_SYSSPACE) 1091541Srgrimes error = copystr(ndp->ni_dirp, cnp->cn_pnbuf, 1102142Sdg MAXPATHLEN, (u_int *)&ndp->ni_pathlen); 1111541Srgrimes else 1121541Srgrimes error = copyinstr(ndp->ni_dirp, cnp->cn_pnbuf, 1132142Sdg MAXPATHLEN, (u_int *)&ndp->ni_pathlen); 11420069Sbde 11520069Sbde /* 11620069Sbde * Don't allow empty pathnames. 11720069Sbde */ 11820069Sbde if (!error && *cnp->cn_pnbuf == '\0') 11920069Sbde error = ENOENT; 12020069Sbde 1211541Srgrimes if (error) { 12229653Sdyson zfree(namei_zone, cnp->cn_pnbuf); 1231541Srgrimes ndp->ni_vp = NULL; 1241541Srgrimes return (error); 1251541Srgrimes } 1261541Srgrimes ndp->ni_loopcnt = 0; 1271541Srgrimes#ifdef KTRACE 1281541Srgrimes if (KTRPOINT(cnp->cn_proc, KTR_NAMEI)) 1291541Srgrimes ktrnamei(cnp->cn_proc->p_tracep, cnp->cn_pnbuf); 1301541Srgrimes#endif 1311541Srgrimes 1321541Srgrimes /* 1331541Srgrimes * Get starting point for the translation. 1341541Srgrimes */ 1351541Srgrimes if ((ndp->ni_rootdir = fdp->fd_rdir) == NULL) 1361541Srgrimes ndp->ni_rootdir = rootvnode; 1371541Srgrimes dp = fdp->fd_cdir; 1381541Srgrimes VREF(dp); 1391541Srgrimes for (;;) { 1401541Srgrimes /* 1411541Srgrimes * Check if root directory should replace current directory. 1421541Srgrimes * Done at start of translation and after symbolic link. 1431541Srgrimes */ 1441541Srgrimes cnp->cn_nameptr = cnp->cn_pnbuf; 1451541Srgrimes if (*(cnp->cn_nameptr) == '/') { 1461541Srgrimes vrele(dp); 1471541Srgrimes while (*(cnp->cn_nameptr) == '/') { 1481541Srgrimes cnp->cn_nameptr++; 1491541Srgrimes ndp->ni_pathlen--; 1501541Srgrimes } 1511541Srgrimes dp = ndp->ni_rootdir; 1521541Srgrimes VREF(dp); 1531541Srgrimes } 1541541Srgrimes ndp->ni_startdir = dp; 1553148Sphk error = lookup(ndp); 1563148Sphk if (error) { 15729653Sdyson zfree(namei_zone, cnp->cn_pnbuf); 1581541Srgrimes return (error); 1591541Srgrimes } 1601541Srgrimes /* 1611541Srgrimes * Check for symbolic link 1621541Srgrimes */ 1631541Srgrimes if ((cnp->cn_flags & ISSYMLINK) == 0) { 1641541Srgrimes if ((cnp->cn_flags & (SAVENAME | SAVESTART)) == 0) 16529653Sdyson zfree(namei_zone, cnp->cn_pnbuf); 1661541Srgrimes else 1671541Srgrimes cnp->cn_flags |= HASBUF; 1681541Srgrimes return (0); 1691541Srgrimes } 1701541Srgrimes if ((cnp->cn_flags & LOCKPARENT) && ndp->ni_pathlen == 1) 17122521Sdyson VOP_UNLOCK(ndp->ni_dvp, 0, p); 1721541Srgrimes if (ndp->ni_loopcnt++ >= MAXSYMLINKS) { 1731541Srgrimes error = ELOOP; 1741541Srgrimes break; 1751541Srgrimes } 1761541Srgrimes if (ndp->ni_pathlen > 1) 17729653Sdyson cp = zalloc(namei_zone); 1781541Srgrimes else 1791541Srgrimes cp = cnp->cn_pnbuf; 1801541Srgrimes aiov.iov_base = cp; 1811541Srgrimes aiov.iov_len = MAXPATHLEN; 1821541Srgrimes auio.uio_iov = &aiov; 1831541Srgrimes auio.uio_iovcnt = 1; 1841541Srgrimes auio.uio_offset = 0; 1851541Srgrimes auio.uio_rw = UIO_READ; 1861541Srgrimes auio.uio_segflg = UIO_SYSSPACE; 1871541Srgrimes auio.uio_procp = (struct proc *)0; 1881541Srgrimes auio.uio_resid = MAXPATHLEN; 1893148Sphk error = VOP_READLINK(ndp->ni_vp, &auio, cnp->cn_cred); 1903148Sphk if (error) { 1911541Srgrimes if (ndp->ni_pathlen > 1) 19229653Sdyson zfree(namei_zone, cp); 1931541Srgrimes break; 1941541Srgrimes } 1951541Srgrimes linklen = MAXPATHLEN - auio.uio_resid; 1961541Srgrimes if (linklen + ndp->ni_pathlen >= MAXPATHLEN) { 1971541Srgrimes if (ndp->ni_pathlen > 1) 19829653Sdyson zfree(namei_zone, cp); 1991541Srgrimes error = ENAMETOOLONG; 2001541Srgrimes break; 2011541Srgrimes } 2021541Srgrimes if (ndp->ni_pathlen > 1) { 2031541Srgrimes bcopy(ndp->ni_next, cp + linklen, ndp->ni_pathlen); 20429653Sdyson zfree(namei_zone, cnp->cn_pnbuf); 2051541Srgrimes cnp->cn_pnbuf = cp; 2061541Srgrimes } else 2071541Srgrimes cnp->cn_pnbuf[linklen] = '\0'; 2081541Srgrimes ndp->ni_pathlen += linklen; 2091541Srgrimes vput(ndp->ni_vp); 2101541Srgrimes dp = ndp->ni_dvp; 2111541Srgrimes } 21229653Sdyson zfree(namei_zone, cnp->cn_pnbuf); 2131541Srgrimes vrele(ndp->ni_dvp); 2141541Srgrimes vput(ndp->ni_vp); 2151541Srgrimes ndp->ni_vp = NULL; 2161541Srgrimes return (error); 2171541Srgrimes} 2181541Srgrimes 2191541Srgrimes/* 2201541Srgrimes * Search a pathname. 2211541Srgrimes * This is a very central and rather complicated routine. 2221541Srgrimes * 2231541Srgrimes * The pathname is pointed to by ni_ptr and is of length ni_pathlen. 2241541Srgrimes * The starting directory is taken from ni_startdir. The pathname is 2251541Srgrimes * descended until done, or a symbolic link is encountered. The variable 2261541Srgrimes * ni_more is clear if the path is completed; it is set to one if a 2271541Srgrimes * symbolic link needing interpretation is encountered. 2281541Srgrimes * 2291541Srgrimes * The flag argument is LOOKUP, CREATE, RENAME, or DELETE depending on 2301541Srgrimes * whether the name is to be looked up, created, renamed, or deleted. 2311541Srgrimes * When CREATE, RENAME, or DELETE is specified, information usable in 2321541Srgrimes * creating, renaming, or deleting a directory entry may be calculated. 2331541Srgrimes * If flag has LOCKPARENT or'ed into it, the parent directory is returned 2341541Srgrimes * locked. If flag has WANTPARENT or'ed into it, the parent directory is 2351541Srgrimes * returned unlocked. Otherwise the parent directory is not returned. If 2361541Srgrimes * the target of the pathname exists and LOCKLEAF is or'ed into the flag 2371541Srgrimes * the target is returned locked, otherwise it is returned unlocked. 2381541Srgrimes * When creating or renaming and LOCKPARENT is specified, the target may not 2391541Srgrimes * be ".". When deleting and LOCKPARENT is specified, the target may be ".". 2408876Srgrimes * 2411541Srgrimes * Overall outline of lookup: 2421541Srgrimes * 2431541Srgrimes * dirloop: 2441541Srgrimes * identify next component of name at ndp->ni_ptr 2451541Srgrimes * handle degenerate case where name is null string 2461541Srgrimes * if .. and crossing mount points and on mounted filesys, find parent 2471541Srgrimes * call VOP_LOOKUP routine for next component name 2481541Srgrimes * directory vnode returned in ni_dvp, unlocked unless LOCKPARENT set 2491541Srgrimes * component vnode returned in ni_vp (if it exists), locked. 2501541Srgrimes * if result vnode is mounted on and crossing mount points, 2511541Srgrimes * find mounted on vnode 2521541Srgrimes * if more components of name, do next level at dirloop 2531541Srgrimes * return the answer in ni_vp, locked if LOCKLEAF set 2541541Srgrimes * if LOCKPARENT set, return locked parent in ni_dvp 2551541Srgrimes * if WANTPARENT set, return unlocked parent in ni_dvp 2561541Srgrimes */ 2571541Srgrimesint 2581541Srgrimeslookup(ndp) 2591541Srgrimes register struct nameidata *ndp; 2601541Srgrimes{ 2611541Srgrimes register char *cp; /* pointer into pathname argument */ 2621541Srgrimes register struct vnode *dp = 0; /* the directory we are searching */ 2631541Srgrimes struct vnode *tdp; /* saved dp */ 2641541Srgrimes struct mount *mp; /* mount table entry */ 2651541Srgrimes int docache; /* == 0 do not cache last component */ 2661541Srgrimes int wantparent; /* 1 => wantparent or lockparent flag */ 2671541Srgrimes int rdonly; /* lookup read-only flag bit */ 2689804Sbde int trailing_slash; 2691541Srgrimes int error = 0; 2701541Srgrimes struct componentname *cnp = &ndp->ni_cnd; 27122521Sdyson struct proc *p = cnp->cn_proc; 2721541Srgrimes 2731541Srgrimes /* 2741541Srgrimes * Setup: break out flag bits into variables. 2751541Srgrimes */ 2761541Srgrimes wantparent = cnp->cn_flags & (LOCKPARENT | WANTPARENT); 2771541Srgrimes docache = (cnp->cn_flags & NOCACHE) ^ NOCACHE; 2781541Srgrimes if (cnp->cn_nameiop == DELETE || 27922874Sbde (wantparent && cnp->cn_nameiop != CREATE && 28022874Sbde cnp->cn_nameiop != LOOKUP)) 2811541Srgrimes docache = 0; 2821541Srgrimes rdonly = cnp->cn_flags & RDONLY; 2831541Srgrimes ndp->ni_dvp = NULL; 2841541Srgrimes cnp->cn_flags &= ~ISSYMLINK; 2851541Srgrimes dp = ndp->ni_startdir; 2861541Srgrimes ndp->ni_startdir = NULLVP; 28722521Sdyson vn_lock(dp, LK_EXCLUSIVE | LK_RETRY, p); 2881541Srgrimes 2891541Srgrimesdirloop: 2901541Srgrimes /* 2911541Srgrimes * Search a new directory. 2921541Srgrimes * 2931541Srgrimes * The cn_hash value is for use by vfs_cache. 2941541Srgrimes * The last component of the filename is left accessible via 2951541Srgrimes * cnp->cn_nameptr for callers that need the name. Callers needing 2961541Srgrimes * the name set the SAVENAME flag. When done, they assume 2971541Srgrimes * responsibility for freeing the pathname buffer. 2981541Srgrimes */ 2991541Srgrimes cnp->cn_consume = 0; 3001541Srgrimes cnp->cn_hash = 0; 3011541Srgrimes for (cp = cnp->cn_nameptr; *cp != 0 && *cp != '/'; cp++) 3021541Srgrimes cnp->cn_hash += (unsigned char)*cp; 3031541Srgrimes cnp->cn_namelen = cp - cnp->cn_nameptr; 3041541Srgrimes if (cnp->cn_namelen > NAME_MAX) { 3051541Srgrimes error = ENAMETOOLONG; 3061541Srgrimes goto bad; 3071541Srgrimes } 3081541Srgrimes#ifdef NAMEI_DIAGNOSTIC 3091541Srgrimes { char c = *cp; 3101541Srgrimes *cp = '\0'; 3111541Srgrimes printf("{%s}: ", cnp->cn_nameptr); 3121541Srgrimes *cp = c; } 3131541Srgrimes#endif 3141541Srgrimes ndp->ni_pathlen -= cnp->cn_namelen; 3151541Srgrimes ndp->ni_next = cp; 3169804Sbde 3179804Sbde /* 3189804Sbde * Replace multiple slashes by a single slash and trailing slashes 3199804Sbde * by a null. This must be done before VOP_LOOKUP() because some 3209804Sbde * fs's don't know about trailing slashes. Remember if there were 3219804Sbde * trailing slashes to handle symlinks, existing non-directories 3229804Sbde * and non-existing files that won't be directories specially later. 3239804Sbde */ 3249804Sbde trailing_slash = 0; 3259804Sbde while (*cp == '/' && (cp[1] == '/' || cp[1] == '\0')) { 3269804Sbde cp++; 3279804Sbde ndp->ni_pathlen--; 3289804Sbde if (*cp == '\0') { 3299804Sbde trailing_slash = 1; 3309804Sbde *ndp->ni_next = '\0'; /* XXX for direnter() ... */ 3319804Sbde } 3329804Sbde } 3339804Sbde ndp->ni_next = cp; 3349804Sbde 3351541Srgrimes cnp->cn_flags |= MAKEENTRY; 3361541Srgrimes if (*cp == '\0' && docache == 0) 3371541Srgrimes cnp->cn_flags &= ~MAKEENTRY; 3381541Srgrimes if (cnp->cn_namelen == 2 && 3391541Srgrimes cnp->cn_nameptr[1] == '.' && cnp->cn_nameptr[0] == '.') 3401541Srgrimes cnp->cn_flags |= ISDOTDOT; 3411541Srgrimes else 3421541Srgrimes cnp->cn_flags &= ~ISDOTDOT; 3431541Srgrimes if (*ndp->ni_next == 0) 3441541Srgrimes cnp->cn_flags |= ISLASTCN; 3451541Srgrimes else 3461541Srgrimes cnp->cn_flags &= ~ISLASTCN; 3471541Srgrimes 3481541Srgrimes 3491541Srgrimes /* 3501541Srgrimes * Check for degenerate name (e.g. / or "") 3511541Srgrimes * which is a way of talking about a directory, 3521541Srgrimes * e.g. like "/." or ".". 3531541Srgrimes */ 3541541Srgrimes if (cnp->cn_nameptr[0] == '\0') { 35522521Sdyson if (dp->v_type != VDIR) { 35622521Sdyson error = ENOTDIR; 35722521Sdyson goto bad; 35822521Sdyson } 3591541Srgrimes if (cnp->cn_nameiop != LOOKUP) { 3601541Srgrimes error = EISDIR; 3611541Srgrimes goto bad; 3621541Srgrimes } 3631541Srgrimes if (wantparent) { 3641541Srgrimes ndp->ni_dvp = dp; 3651541Srgrimes VREF(dp); 3661541Srgrimes } 3671541Srgrimes ndp->ni_vp = dp; 3681541Srgrimes if (!(cnp->cn_flags & (LOCKPARENT | LOCKLEAF))) 36922521Sdyson VOP_UNLOCK(dp, 0, p); 3701541Srgrimes if (cnp->cn_flags & SAVESTART) 3711541Srgrimes panic("lookup: SAVESTART"); 3721541Srgrimes return (0); 3731541Srgrimes } 3741541Srgrimes 3751541Srgrimes /* 3761541Srgrimes * Handle "..": two special cases. 3771541Srgrimes * 1. If at root directory (e.g. after chroot) 3781541Srgrimes * or at absolute root directory 3791541Srgrimes * then ignore it so can't get out. 3801541Srgrimes * 2. If this vnode is the root of a mounted 3811541Srgrimes * filesystem, then replace it with the 3821541Srgrimes * vnode which was mounted on so we take the 3831541Srgrimes * .. in the other file system. 3841541Srgrimes */ 3851541Srgrimes if (cnp->cn_flags & ISDOTDOT) { 3861541Srgrimes for (;;) { 3871541Srgrimes if (dp == ndp->ni_rootdir || dp == rootvnode) { 3881541Srgrimes ndp->ni_dvp = dp; 3891541Srgrimes ndp->ni_vp = dp; 3901541Srgrimes VREF(dp); 3911541Srgrimes goto nextname; 3921541Srgrimes } 3931541Srgrimes if ((dp->v_flag & VROOT) == 0 || 3941541Srgrimes (cnp->cn_flags & NOCROSSMOUNT)) 3951541Srgrimes break; 3961541Srgrimes tdp = dp; 3971541Srgrimes dp = dp->v_mount->mnt_vnodecovered; 3981541Srgrimes vput(tdp); 3991541Srgrimes VREF(dp); 40022521Sdyson vn_lock(dp, LK_EXCLUSIVE | LK_RETRY, p); 4011541Srgrimes } 4021541Srgrimes } 4031541Srgrimes 4041541Srgrimes /* 4051541Srgrimes * We now have a segment name to search for, and a directory to search. 4061541Srgrimes */ 4071541Srgrimesunionlookup: 4081541Srgrimes ndp->ni_dvp = dp; 40922521Sdyson ndp->ni_vp = NULL; 41024624Sdfr ASSERT_VOP_LOCKED(dp, "lookup"); 41122521Sdyson if (error = VOP_LOOKUP(dp, &ndp->ni_vp, cnp)) { 4121541Srgrimes#ifdef DIAGNOSTIC 4131541Srgrimes if (ndp->ni_vp != NULL) 4141541Srgrimes panic("leaf should be empty"); 4151541Srgrimes#endif 4161541Srgrimes#ifdef NAMEI_DIAGNOSTIC 4171541Srgrimes printf("not found\n"); 4181541Srgrimes#endif 4191541Srgrimes if ((error == ENOENT) && 4201541Srgrimes (dp->v_flag & VROOT) && 4211541Srgrimes (dp->v_mount->mnt_flag & MNT_UNION)) { 4221541Srgrimes tdp = dp; 4231541Srgrimes dp = dp->v_mount->mnt_vnodecovered; 4241541Srgrimes vput(tdp); 4251541Srgrimes VREF(dp); 42622521Sdyson vn_lock(dp, LK_EXCLUSIVE | LK_RETRY, p); 4271541Srgrimes goto unionlookup; 4281541Srgrimes } 4291541Srgrimes 4301541Srgrimes if (error != EJUSTRETURN) 4311541Srgrimes goto bad; 4321541Srgrimes /* 4331541Srgrimes * If creating and at end of pathname, then can consider 4341541Srgrimes * allowing file to be created. 4351541Srgrimes */ 43611644Sdg if (rdonly) { 4371541Srgrimes error = EROFS; 4381541Srgrimes goto bad; 4391541Srgrimes } 4409804Sbde if (*cp == '\0' && trailing_slash && 4419804Sbde !(cnp->cn_flags & WILLBEDIR)) { 4429804Sbde error = ENOENT; 4439804Sbde goto bad; 4449804Sbde } 4451541Srgrimes /* 4461541Srgrimes * We return with ni_vp NULL to indicate that the entry 4471541Srgrimes * doesn't currently exist, leaving a pointer to the 4481541Srgrimes * (possibly locked) directory inode in ndp->ni_dvp. 4491541Srgrimes */ 4501541Srgrimes if (cnp->cn_flags & SAVESTART) { 4511541Srgrimes ndp->ni_startdir = ndp->ni_dvp; 4521541Srgrimes VREF(ndp->ni_startdir); 4531541Srgrimes } 4541541Srgrimes return (0); 4551541Srgrimes } 4561541Srgrimes#ifdef NAMEI_DIAGNOSTIC 4571541Srgrimes printf("found\n"); 4581541Srgrimes#endif 4591541Srgrimes 46024624Sdfr ASSERT_VOP_LOCKED(ndp->ni_vp, "lookup"); 46124624Sdfr 4621541Srgrimes /* 4631541Srgrimes * Take into account any additional components consumed by 4641541Srgrimes * the underlying filesystem. 4651541Srgrimes */ 4661541Srgrimes if (cnp->cn_consume > 0) { 4671541Srgrimes cnp->cn_nameptr += cnp->cn_consume; 4681541Srgrimes ndp->ni_next += cnp->cn_consume; 4691541Srgrimes ndp->ni_pathlen -= cnp->cn_consume; 4701541Srgrimes cnp->cn_consume = 0; 4711541Srgrimes } 4721541Srgrimes 4731541Srgrimes dp = ndp->ni_vp; 4741541Srgrimes 4751541Srgrimes /* 4761541Srgrimes * Check to see if the vnode has been mounted on; 4771541Srgrimes * if so find the root of the mounted file system. 4781541Srgrimes */ 4791541Srgrimes while (dp->v_type == VDIR && (mp = dp->v_mountedhere) && 4801541Srgrimes (cnp->cn_flags & NOCROSSMOUNT) == 0) { 48122521Sdyson if (vfs_busy(mp, 0, 0, p)) 4821541Srgrimes continue; 48322521Sdyson error = VFS_ROOT(mp, &tdp); 48422521Sdyson vfs_unbusy(mp, p); 4853148Sphk if (error) 4861541Srgrimes goto bad2; 4871541Srgrimes vput(dp); 4881541Srgrimes ndp->ni_vp = dp = tdp; 4891541Srgrimes } 4901541Srgrimes 49110219Sdfr /* 49210219Sdfr * Check for symbolic link 49310219Sdfr */ 49410219Sdfr if ((dp->v_type == VLNK) && 49510219Sdfr ((cnp->cn_flags & FOLLOW) || trailing_slash || 49610219Sdfr *ndp->ni_next == '/')) { 49710219Sdfr cnp->cn_flags |= ISSYMLINK; 49810219Sdfr return (0); 49910219Sdfr } 50010219Sdfr 50110219Sdfr /* 50210219Sdfr * Check for bogus trailing slashes. 50310219Sdfr */ 50410219Sdfr if (trailing_slash && dp->v_type != VDIR) { 50510219Sdfr error = ENOTDIR; 50610219Sdfr goto bad2; 50710219Sdfr } 50810219Sdfr 5091541Srgrimesnextname: 5101541Srgrimes /* 5111541Srgrimes * Not a symbolic link. If more pathname, 5121541Srgrimes * continue at next component, else return. 5131541Srgrimes */ 5141541Srgrimes if (*ndp->ni_next == '/') { 5151541Srgrimes cnp->cn_nameptr = ndp->ni_next; 5161541Srgrimes while (*cnp->cn_nameptr == '/') { 5171541Srgrimes cnp->cn_nameptr++; 5181541Srgrimes ndp->ni_pathlen--; 5191541Srgrimes } 52024624Sdfr if (ndp->ni_dvp != ndp->ni_vp) { 52124624Sdfr ASSERT_VOP_UNLOCKED(ndp->ni_dvp, "lookup"); 52224624Sdfr } 5231541Srgrimes vrele(ndp->ni_dvp); 5241541Srgrimes goto dirloop; 5251541Srgrimes } 5261541Srgrimes /* 52711644Sdg * Disallow directory write attempts on read-only file systems. 5281541Srgrimes */ 52911644Sdg if (rdonly && 53011644Sdg (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME)) { 53111644Sdg error = EROFS; 53211644Sdg goto bad2; 5331541Srgrimes } 5341541Srgrimes if (cnp->cn_flags & SAVESTART) { 5351541Srgrimes ndp->ni_startdir = ndp->ni_dvp; 5361541Srgrimes VREF(ndp->ni_startdir); 5371541Srgrimes } 5381541Srgrimes if (!wantparent) 5391541Srgrimes vrele(ndp->ni_dvp); 5401541Srgrimes if ((cnp->cn_flags & LOCKLEAF) == 0) 54122521Sdyson VOP_UNLOCK(dp, 0, p); 5421541Srgrimes return (0); 5431541Srgrimes 5441541Srgrimesbad2: 5451541Srgrimes if ((cnp->cn_flags & LOCKPARENT) && *ndp->ni_next == '\0') 54622521Sdyson VOP_UNLOCK(ndp->ni_dvp, 0, p); 5471541Srgrimes vrele(ndp->ni_dvp); 5481541Srgrimesbad: 5491541Srgrimes vput(dp); 5501541Srgrimes ndp->ni_vp = NULL; 5511541Srgrimes return (error); 5521541Srgrimes} 5531541Srgrimes 5543148Sphk/* 5553148Sphk * relookup - lookup a path name component 5563148Sphk * Used by lookup to re-aquire things. 5573148Sphk */ 5583148Sphkint 5593148Sphkrelookup(dvp, vpp, cnp) 5603148Sphk struct vnode *dvp, **vpp; 5613148Sphk struct componentname *cnp; 5623148Sphk{ 56322521Sdyson struct proc *p = cnp->cn_proc; 56422521Sdyson struct vnode *dp = 0; /* the directory we are searching */ 5653148Sphk int docache; /* == 0 do not cache last component */ 5663148Sphk int wantparent; /* 1 => wantparent or lockparent flag */ 5673148Sphk int rdonly; /* lookup read-only flag bit */ 5683148Sphk int error = 0; 5693148Sphk#ifdef NAMEI_DIAGNOSTIC 5703148Sphk int newhash; /* DEBUG: check name hash */ 5713148Sphk char *cp; /* DEBUG: check name ptr/len */ 5723148Sphk#endif 5731541Srgrimes 5743148Sphk /* 5753148Sphk * Setup: break out flag bits into variables. 5763148Sphk */ 5773148Sphk wantparent = cnp->cn_flags & (LOCKPARENT|WANTPARENT); 5783148Sphk docache = (cnp->cn_flags & NOCACHE) ^ NOCACHE; 5793148Sphk if (cnp->cn_nameiop == DELETE || 5803148Sphk (wantparent && cnp->cn_nameiop != CREATE)) 5813148Sphk docache = 0; 5823148Sphk rdonly = cnp->cn_flags & RDONLY; 5833148Sphk cnp->cn_flags &= ~ISSYMLINK; 5843148Sphk dp = dvp; 58522521Sdyson vn_lock(dp, LK_EXCLUSIVE | LK_RETRY, p); 5863148Sphk 5873148Sphk/* dirloop: */ 5883148Sphk /* 5893148Sphk * Search a new directory. 5903148Sphk * 5913148Sphk * The cn_hash value is for use by vfs_cache. 5923148Sphk * The last component of the filename is left accessible via 5933148Sphk * cnp->cn_nameptr for callers that need the name. Callers needing 5943148Sphk * the name set the SAVENAME flag. When done, they assume 5953148Sphk * responsibility for freeing the pathname buffer. 5963148Sphk */ 5973148Sphk#ifdef NAMEI_DIAGNOSTIC 5983148Sphk for (newhash = 0, cp = cnp->cn_nameptr; *cp != 0 && *cp != '/'; cp++) 5993148Sphk newhash += (unsigned char)*cp; 6003148Sphk if (newhash != cnp->cn_hash) 6013148Sphk panic("relookup: bad hash"); 6023148Sphk if (cnp->cn_namelen != cp - cnp->cn_nameptr) 6033148Sphk panic ("relookup: bad len"); 6043148Sphk if (*cp != 0) 6053148Sphk panic("relookup: not last component"); 6063148Sphk printf("{%s}: ", cnp->cn_nameptr); 6073148Sphk#endif 6083148Sphk 6093148Sphk /* 6103148Sphk * Check for degenerate name (e.g. / or "") 6113148Sphk * which is a way of talking about a directory, 6123148Sphk * e.g. like "/." or ".". 6133148Sphk */ 6143148Sphk if (cnp->cn_nameptr[0] == '\0') { 6153148Sphk if (cnp->cn_nameiop != LOOKUP || wantparent) { 6163148Sphk error = EISDIR; 6173148Sphk goto bad; 6183148Sphk } 6193148Sphk if (dp->v_type != VDIR) { 6203148Sphk error = ENOTDIR; 6213148Sphk goto bad; 6223148Sphk } 6233148Sphk if (!(cnp->cn_flags & LOCKLEAF)) 62422521Sdyson VOP_UNLOCK(dp, 0, p); 6253148Sphk *vpp = dp; 6263148Sphk if (cnp->cn_flags & SAVESTART) 6273148Sphk panic("lookup: SAVESTART"); 6283148Sphk return (0); 6293148Sphk } 6303148Sphk 6313148Sphk if (cnp->cn_flags & ISDOTDOT) 6323148Sphk panic ("relookup: lookup on dot-dot"); 6333148Sphk 6343148Sphk /* 6353148Sphk * We now have a segment name to search for, and a directory to search. 6363148Sphk */ 63722521Sdyson if (error = VOP_LOOKUP(dp, vpp, cnp)) { 6383148Sphk#ifdef DIAGNOSTIC 6393148Sphk if (*vpp != NULL) 6403148Sphk panic("leaf should be empty"); 6413148Sphk#endif 6423148Sphk if (error != EJUSTRETURN) 6433148Sphk goto bad; 6443148Sphk /* 6453148Sphk * If creating and at end of pathname, then can consider 6463148Sphk * allowing file to be created. 6473148Sphk */ 64811644Sdg if (rdonly) { 6493148Sphk error = EROFS; 6503148Sphk goto bad; 6513148Sphk } 6523148Sphk /* ASSERT(dvp == ndp->ni_startdir) */ 6533148Sphk if (cnp->cn_flags & SAVESTART) 6543148Sphk VREF(dvp); 6553148Sphk /* 6563148Sphk * We return with ni_vp NULL to indicate that the entry 6573148Sphk * doesn't currently exist, leaving a pointer to the 6583148Sphk * (possibly locked) directory inode in ndp->ni_dvp. 6593148Sphk */ 6603148Sphk return (0); 6613148Sphk } 6623148Sphk dp = *vpp; 6633148Sphk 6643148Sphk#ifdef DIAGNOSTIC 6653148Sphk /* 6663148Sphk * Check for symbolic link 6673148Sphk */ 6683148Sphk if (dp->v_type == VLNK && (cnp->cn_flags & FOLLOW)) 6693148Sphk panic ("relookup: symlink found.\n"); 6703148Sphk#endif 6713148Sphk 6723148Sphk /* 67311644Sdg * Disallow directory write attempts on read-only file systems. 6743148Sphk */ 67511644Sdg if (rdonly && 67611644Sdg (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME)) { 67711644Sdg error = EROFS; 67811644Sdg goto bad2; 6793148Sphk } 6803148Sphk /* ASSERT(dvp == ndp->ni_startdir) */ 6813148Sphk if (cnp->cn_flags & SAVESTART) 6823148Sphk VREF(dvp); 68322521Sdyson 6843148Sphk if (!wantparent) 6853148Sphk vrele(dvp); 6863148Sphk if ((cnp->cn_flags & LOCKLEAF) == 0) 68722521Sdyson VOP_UNLOCK(dp, 0, p); 6883148Sphk return (0); 6893148Sphk 6903148Sphkbad2: 6913148Sphk if ((cnp->cn_flags & LOCKPARENT) && (cnp->cn_flags & ISLASTCN)) 69222521Sdyson VOP_UNLOCK(dvp, 0, p); 6933148Sphk vrele(dvp); 6943148Sphkbad: 6953148Sphk vput(dp); 6963148Sphk *vpp = NULL; 6973148Sphk return (error); 6983148Sphk} 699