vfs_lookup.c revision 13203
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 3913203Swollman * $Id: vfs_lookup.c,v 1.10 1995/10/22 09:32:25 davidg Exp $ 401541Srgrimes */ 411541Srgrimes 4213203Swollman#include "opt_ktrace.h" 4313203Swollman 441541Srgrimes#include <sys/param.h> 452112Swollman#include <sys/systm.h> 461541Srgrimes#include <sys/syslimits.h> 471541Srgrimes#include <sys/time.h> 481541Srgrimes#include <sys/namei.h> 491541Srgrimes#include <sys/vnode.h> 501541Srgrimes#include <sys/mount.h> 511541Srgrimes#include <sys/errno.h> 521541Srgrimes#include <sys/malloc.h> 531541Srgrimes#include <sys/filedesc.h> 541541Srgrimes#include <sys/proc.h> 551541Srgrimes 561541Srgrimes#ifdef KTRACE 571541Srgrimes#include <sys/ktrace.h> 581541Srgrimes#endif 591541Srgrimes 601541Srgrimes/* 611541Srgrimes * Convert a pathname into a pointer to a locked inode. 621541Srgrimes * 631541Srgrimes * The FOLLOW flag is set when symbolic links are to be followed 641541Srgrimes * when they occur at the end of the name translation process. 651541Srgrimes * Symbolic links are always followed for all other pathname 661541Srgrimes * components other than the last. 671541Srgrimes * 681541Srgrimes * The segflg defines whether the name is to be copied from user 691541Srgrimes * space or kernel space. 701541Srgrimes * 711541Srgrimes * Overall outline of namei: 721541Srgrimes * 731541Srgrimes * copy in name 741541Srgrimes * get starting directory 751541Srgrimes * while (!done && !error) { 761541Srgrimes * call lookup to search path. 771541Srgrimes * if symbolic link, massage name in buffer and continue 781541Srgrimes * } 791541Srgrimes */ 801541Srgrimesint 811541Srgrimesnamei(ndp) 821541Srgrimes register struct nameidata *ndp; 831541Srgrimes{ 841541Srgrimes register struct filedesc *fdp; /* pointer to file descriptor state */ 851541Srgrimes register char *cp; /* pointer into pathname argument */ 861541Srgrimes register struct vnode *dp; /* the directory we are searching */ 871541Srgrimes struct iovec aiov; /* uio for reading symbolic links */ 881541Srgrimes struct uio auio; 891541Srgrimes int error, linklen; 901541Srgrimes struct componentname *cnp = &ndp->ni_cnd; 911541Srgrimes 921541Srgrimes ndp->ni_cnd.cn_cred = ndp->ni_cnd.cn_proc->p_ucred; 931541Srgrimes#ifdef DIAGNOSTIC 941541Srgrimes if (!cnp->cn_cred || !cnp->cn_proc) 951541Srgrimes panic ("namei: bad cred/proc"); 961541Srgrimes if (cnp->cn_nameiop & (~OPMASK)) 971541Srgrimes panic ("namei: nameiop contaminated with flags"); 981541Srgrimes if (cnp->cn_flags & OPMASK) 991541Srgrimes panic ("namei: flags contaminated with nameiops"); 1001541Srgrimes#endif 1011541Srgrimes fdp = cnp->cn_proc->p_fd; 1021541Srgrimes 1031541Srgrimes /* 1041541Srgrimes * Get a buffer for the name to be translated, and copy the 1051541Srgrimes * name into the buffer. 1061541Srgrimes */ 1071541Srgrimes if ((cnp->cn_flags & HASBUF) == 0) 1081541Srgrimes MALLOC(cnp->cn_pnbuf, caddr_t, MAXPATHLEN, M_NAMEI, M_WAITOK); 1091541Srgrimes if (ndp->ni_segflg == UIO_SYSSPACE) 1101541Srgrimes error = copystr(ndp->ni_dirp, cnp->cn_pnbuf, 1112142Sdg MAXPATHLEN, (u_int *)&ndp->ni_pathlen); 1121541Srgrimes else 1131541Srgrimes error = copyinstr(ndp->ni_dirp, cnp->cn_pnbuf, 1142142Sdg MAXPATHLEN, (u_int *)&ndp->ni_pathlen); 1151541Srgrimes if (error) { 1161541Srgrimes free(cnp->cn_pnbuf, M_NAMEI); 1171541Srgrimes ndp->ni_vp = NULL; 1181541Srgrimes return (error); 1191541Srgrimes } 1201541Srgrimes ndp->ni_loopcnt = 0; 1211541Srgrimes#ifdef KTRACE 1221541Srgrimes if (KTRPOINT(cnp->cn_proc, KTR_NAMEI)) 1231541Srgrimes ktrnamei(cnp->cn_proc->p_tracep, cnp->cn_pnbuf); 1241541Srgrimes#endif 1251541Srgrimes 1261541Srgrimes /* 1271541Srgrimes * Get starting point for the translation. 1281541Srgrimes */ 1291541Srgrimes if ((ndp->ni_rootdir = fdp->fd_rdir) == NULL) 1301541Srgrimes ndp->ni_rootdir = rootvnode; 1311541Srgrimes dp = fdp->fd_cdir; 1321541Srgrimes VREF(dp); 1331541Srgrimes for (;;) { 1341541Srgrimes /* 1351541Srgrimes * Check if root directory should replace current directory. 1361541Srgrimes * Done at start of translation and after symbolic link. 1371541Srgrimes */ 1381541Srgrimes cnp->cn_nameptr = cnp->cn_pnbuf; 1391541Srgrimes if (*(cnp->cn_nameptr) == '/') { 1401541Srgrimes vrele(dp); 1411541Srgrimes while (*(cnp->cn_nameptr) == '/') { 1421541Srgrimes cnp->cn_nameptr++; 1431541Srgrimes ndp->ni_pathlen--; 1441541Srgrimes } 1451541Srgrimes dp = ndp->ni_rootdir; 1461541Srgrimes VREF(dp); 1471541Srgrimes } 1481541Srgrimes ndp->ni_startdir = dp; 1493148Sphk error = lookup(ndp); 1503148Sphk if (error) { 1511541Srgrimes FREE(cnp->cn_pnbuf, M_NAMEI); 1521541Srgrimes return (error); 1531541Srgrimes } 1541541Srgrimes /* 1551541Srgrimes * Check for symbolic link 1561541Srgrimes */ 1571541Srgrimes if ((cnp->cn_flags & ISSYMLINK) == 0) { 1581541Srgrimes if ((cnp->cn_flags & (SAVENAME | SAVESTART)) == 0) 1591541Srgrimes FREE(cnp->cn_pnbuf, M_NAMEI); 1601541Srgrimes else 1611541Srgrimes cnp->cn_flags |= HASBUF; 1621541Srgrimes return (0); 1631541Srgrimes } 1641541Srgrimes if ((cnp->cn_flags & LOCKPARENT) && ndp->ni_pathlen == 1) 1651541Srgrimes VOP_UNLOCK(ndp->ni_dvp); 1661541Srgrimes if (ndp->ni_loopcnt++ >= MAXSYMLINKS) { 1671541Srgrimes error = ELOOP; 1681541Srgrimes break; 1691541Srgrimes } 1701541Srgrimes if (ndp->ni_pathlen > 1) 1711541Srgrimes MALLOC(cp, char *, MAXPATHLEN, M_NAMEI, M_WAITOK); 1721541Srgrimes else 1731541Srgrimes cp = cnp->cn_pnbuf; 1741541Srgrimes aiov.iov_base = cp; 1751541Srgrimes aiov.iov_len = MAXPATHLEN; 1761541Srgrimes auio.uio_iov = &aiov; 1771541Srgrimes auio.uio_iovcnt = 1; 1781541Srgrimes auio.uio_offset = 0; 1791541Srgrimes auio.uio_rw = UIO_READ; 1801541Srgrimes auio.uio_segflg = UIO_SYSSPACE; 1811541Srgrimes auio.uio_procp = (struct proc *)0; 1821541Srgrimes auio.uio_resid = MAXPATHLEN; 1833148Sphk error = VOP_READLINK(ndp->ni_vp, &auio, cnp->cn_cred); 1843148Sphk if (error) { 1851541Srgrimes if (ndp->ni_pathlen > 1) 1861541Srgrimes free(cp, M_NAMEI); 1871541Srgrimes break; 1881541Srgrimes } 1891541Srgrimes linklen = MAXPATHLEN - auio.uio_resid; 1901541Srgrimes if (linklen + ndp->ni_pathlen >= MAXPATHLEN) { 1911541Srgrimes if (ndp->ni_pathlen > 1) 1921541Srgrimes free(cp, M_NAMEI); 1931541Srgrimes error = ENAMETOOLONG; 1941541Srgrimes break; 1951541Srgrimes } 1961541Srgrimes if (ndp->ni_pathlen > 1) { 1971541Srgrimes bcopy(ndp->ni_next, cp + linklen, ndp->ni_pathlen); 1981541Srgrimes FREE(cnp->cn_pnbuf, M_NAMEI); 1991541Srgrimes cnp->cn_pnbuf = cp; 2001541Srgrimes } else 2011541Srgrimes cnp->cn_pnbuf[linklen] = '\0'; 2021541Srgrimes ndp->ni_pathlen += linklen; 2031541Srgrimes vput(ndp->ni_vp); 2041541Srgrimes dp = ndp->ni_dvp; 2051541Srgrimes } 2061541Srgrimes FREE(cnp->cn_pnbuf, M_NAMEI); 2071541Srgrimes vrele(ndp->ni_dvp); 2081541Srgrimes vput(ndp->ni_vp); 2091541Srgrimes ndp->ni_vp = NULL; 2101541Srgrimes return (error); 2111541Srgrimes} 2121541Srgrimes 2131541Srgrimes/* 2141541Srgrimes * Search a pathname. 2151541Srgrimes * This is a very central and rather complicated routine. 2161541Srgrimes * 2171541Srgrimes * The pathname is pointed to by ni_ptr and is of length ni_pathlen. 2181541Srgrimes * The starting directory is taken from ni_startdir. The pathname is 2191541Srgrimes * descended until done, or a symbolic link is encountered. The variable 2201541Srgrimes * ni_more is clear if the path is completed; it is set to one if a 2211541Srgrimes * symbolic link needing interpretation is encountered. 2221541Srgrimes * 2231541Srgrimes * The flag argument is LOOKUP, CREATE, RENAME, or DELETE depending on 2241541Srgrimes * whether the name is to be looked up, created, renamed, or deleted. 2251541Srgrimes * When CREATE, RENAME, or DELETE is specified, information usable in 2261541Srgrimes * creating, renaming, or deleting a directory entry may be calculated. 2271541Srgrimes * If flag has LOCKPARENT or'ed into it, the parent directory is returned 2281541Srgrimes * locked. If flag has WANTPARENT or'ed into it, the parent directory is 2291541Srgrimes * returned unlocked. Otherwise the parent directory is not returned. If 2301541Srgrimes * the target of the pathname exists and LOCKLEAF is or'ed into the flag 2311541Srgrimes * the target is returned locked, otherwise it is returned unlocked. 2321541Srgrimes * When creating or renaming and LOCKPARENT is specified, the target may not 2331541Srgrimes * be ".". When deleting and LOCKPARENT is specified, the target may be ".". 2348876Srgrimes * 2351541Srgrimes * Overall outline of lookup: 2361541Srgrimes * 2371541Srgrimes * dirloop: 2381541Srgrimes * identify next component of name at ndp->ni_ptr 2391541Srgrimes * handle degenerate case where name is null string 2401541Srgrimes * if .. and crossing mount points and on mounted filesys, find parent 2411541Srgrimes * call VOP_LOOKUP routine for next component name 2421541Srgrimes * directory vnode returned in ni_dvp, unlocked unless LOCKPARENT set 2431541Srgrimes * component vnode returned in ni_vp (if it exists), locked. 2441541Srgrimes * if result vnode is mounted on and crossing mount points, 2451541Srgrimes * find mounted on vnode 2461541Srgrimes * if more components of name, do next level at dirloop 2471541Srgrimes * return the answer in ni_vp, locked if LOCKLEAF set 2481541Srgrimes * if LOCKPARENT set, return locked parent in ni_dvp 2491541Srgrimes * if WANTPARENT set, return unlocked parent in ni_dvp 2501541Srgrimes */ 2511541Srgrimesint 2521541Srgrimeslookup(ndp) 2531541Srgrimes register struct nameidata *ndp; 2541541Srgrimes{ 2551541Srgrimes register char *cp; /* pointer into pathname argument */ 2561541Srgrimes register struct vnode *dp = 0; /* the directory we are searching */ 2571541Srgrimes struct vnode *tdp; /* saved dp */ 2581541Srgrimes struct mount *mp; /* mount table entry */ 2591541Srgrimes int docache; /* == 0 do not cache last component */ 2601541Srgrimes int wantparent; /* 1 => wantparent or lockparent flag */ 2611541Srgrimes int rdonly; /* lookup read-only flag bit */ 2629804Sbde int trailing_slash; 2631541Srgrimes int error = 0; 2641541Srgrimes struct componentname *cnp = &ndp->ni_cnd; 2651541Srgrimes 2661541Srgrimes /* 2671541Srgrimes * Setup: break out flag bits into variables. 2681541Srgrimes */ 2691541Srgrimes wantparent = cnp->cn_flags & (LOCKPARENT | WANTPARENT); 2701541Srgrimes docache = (cnp->cn_flags & NOCACHE) ^ NOCACHE; 2711541Srgrimes if (cnp->cn_nameiop == DELETE || 2721541Srgrimes (wantparent && cnp->cn_nameiop != CREATE)) 2731541Srgrimes docache = 0; 2741541Srgrimes rdonly = cnp->cn_flags & RDONLY; 2751541Srgrimes ndp->ni_dvp = NULL; 2761541Srgrimes cnp->cn_flags &= ~ISSYMLINK; 2771541Srgrimes dp = ndp->ni_startdir; 2781541Srgrimes ndp->ni_startdir = NULLVP; 2791541Srgrimes VOP_LOCK(dp); 2801541Srgrimes 2811541Srgrimesdirloop: 2821541Srgrimes /* 2831541Srgrimes * Search a new directory. 2841541Srgrimes * 2851541Srgrimes * The cn_hash value is for use by vfs_cache. 2861541Srgrimes * The last component of the filename is left accessible via 2871541Srgrimes * cnp->cn_nameptr for callers that need the name. Callers needing 2881541Srgrimes * the name set the SAVENAME flag. When done, they assume 2891541Srgrimes * responsibility for freeing the pathname buffer. 2901541Srgrimes */ 2911541Srgrimes cnp->cn_consume = 0; 2921541Srgrimes cnp->cn_hash = 0; 2931541Srgrimes for (cp = cnp->cn_nameptr; *cp != 0 && *cp != '/'; cp++) 2941541Srgrimes cnp->cn_hash += (unsigned char)*cp; 2951541Srgrimes cnp->cn_namelen = cp - cnp->cn_nameptr; 2961541Srgrimes if (cnp->cn_namelen > NAME_MAX) { 2971541Srgrimes error = ENAMETOOLONG; 2981541Srgrimes goto bad; 2991541Srgrimes } 3001541Srgrimes#ifdef NAMEI_DIAGNOSTIC 3011541Srgrimes { char c = *cp; 3021541Srgrimes *cp = '\0'; 3031541Srgrimes printf("{%s}: ", cnp->cn_nameptr); 3041541Srgrimes *cp = c; } 3051541Srgrimes#endif 3061541Srgrimes ndp->ni_pathlen -= cnp->cn_namelen; 3071541Srgrimes ndp->ni_next = cp; 3089804Sbde 3099804Sbde /* 3109804Sbde * Replace multiple slashes by a single slash and trailing slashes 3119804Sbde * by a null. This must be done before VOP_LOOKUP() because some 3129804Sbde * fs's don't know about trailing slashes. Remember if there were 3139804Sbde * trailing slashes to handle symlinks, existing non-directories 3149804Sbde * and non-existing files that won't be directories specially later. 3159804Sbde */ 3169804Sbde trailing_slash = 0; 3179804Sbde while (*cp == '/' && (cp[1] == '/' || cp[1] == '\0')) { 3189804Sbde cp++; 3199804Sbde ndp->ni_pathlen--; 3209804Sbde if (*cp == '\0') { 3219804Sbde trailing_slash = 1; 3229804Sbde *ndp->ni_next = '\0'; /* XXX for direnter() ... */ 3239804Sbde } 3249804Sbde } 3259804Sbde ndp->ni_next = cp; 3269804Sbde 3271541Srgrimes cnp->cn_flags |= MAKEENTRY; 3281541Srgrimes if (*cp == '\0' && docache == 0) 3291541Srgrimes cnp->cn_flags &= ~MAKEENTRY; 3301541Srgrimes if (cnp->cn_namelen == 2 && 3311541Srgrimes cnp->cn_nameptr[1] == '.' && cnp->cn_nameptr[0] == '.') 3321541Srgrimes cnp->cn_flags |= ISDOTDOT; 3331541Srgrimes else 3341541Srgrimes cnp->cn_flags &= ~ISDOTDOT; 3351541Srgrimes if (*ndp->ni_next == 0) 3361541Srgrimes cnp->cn_flags |= ISLASTCN; 3371541Srgrimes else 3381541Srgrimes cnp->cn_flags &= ~ISLASTCN; 3391541Srgrimes 3401541Srgrimes 3411541Srgrimes /* 3421541Srgrimes * Check for degenerate name (e.g. / or "") 3431541Srgrimes * which is a way of talking about a directory, 3441541Srgrimes * e.g. like "/." or ".". 3451541Srgrimes */ 3461541Srgrimes if (cnp->cn_nameptr[0] == '\0') { 3471541Srgrimes if (cnp->cn_nameiop != LOOKUP) { 3481541Srgrimes error = EISDIR; 3491541Srgrimes goto bad; 3501541Srgrimes } 3511541Srgrimes if (dp->v_type != VDIR) { 3521541Srgrimes error = ENOTDIR; 3531541Srgrimes goto bad; 3541541Srgrimes } 3551541Srgrimes if (wantparent) { 3561541Srgrimes ndp->ni_dvp = dp; 3571541Srgrimes VREF(dp); 3581541Srgrimes } 3591541Srgrimes ndp->ni_vp = dp; 3601541Srgrimes if (!(cnp->cn_flags & (LOCKPARENT | LOCKLEAF))) 3611541Srgrimes VOP_UNLOCK(dp); 3621541Srgrimes if (cnp->cn_flags & SAVESTART) 3631541Srgrimes panic("lookup: SAVESTART"); 3641541Srgrimes return (0); 3651541Srgrimes } 3661541Srgrimes 3671541Srgrimes /* 3681541Srgrimes * Handle "..": two special cases. 3691541Srgrimes * 1. If at root directory (e.g. after chroot) 3701541Srgrimes * or at absolute root directory 3711541Srgrimes * then ignore it so can't get out. 3721541Srgrimes * 2. If this vnode is the root of a mounted 3731541Srgrimes * filesystem, then replace it with the 3741541Srgrimes * vnode which was mounted on so we take the 3751541Srgrimes * .. in the other file system. 3761541Srgrimes */ 3771541Srgrimes if (cnp->cn_flags & ISDOTDOT) { 3781541Srgrimes for (;;) { 3791541Srgrimes if (dp == ndp->ni_rootdir || dp == rootvnode) { 3801541Srgrimes ndp->ni_dvp = dp; 3811541Srgrimes ndp->ni_vp = dp; 3821541Srgrimes VREF(dp); 3831541Srgrimes goto nextname; 3841541Srgrimes } 3851541Srgrimes if ((dp->v_flag & VROOT) == 0 || 3861541Srgrimes (cnp->cn_flags & NOCROSSMOUNT)) 3871541Srgrimes break; 3881541Srgrimes tdp = dp; 3891541Srgrimes dp = dp->v_mount->mnt_vnodecovered; 3901541Srgrimes vput(tdp); 3911541Srgrimes VREF(dp); 3921541Srgrimes VOP_LOCK(dp); 3931541Srgrimes } 3941541Srgrimes } 3951541Srgrimes 3961541Srgrimes /* 3971541Srgrimes * We now have a segment name to search for, and a directory to search. 3981541Srgrimes */ 3991541Srgrimesunionlookup: 4001541Srgrimes ndp->ni_dvp = dp; 4013148Sphk error = VOP_LOOKUP(dp, &ndp->ni_vp, cnp); 4023148Sphk if (error) { 4031541Srgrimes#ifdef DIAGNOSTIC 4041541Srgrimes if (ndp->ni_vp != NULL) 4051541Srgrimes panic("leaf should be empty"); 4061541Srgrimes#endif 4071541Srgrimes#ifdef NAMEI_DIAGNOSTIC 4081541Srgrimes printf("not found\n"); 4091541Srgrimes#endif 4101541Srgrimes if ((error == ENOENT) && 4111541Srgrimes (dp->v_flag & VROOT) && 4121541Srgrimes (dp->v_mount->mnt_flag & MNT_UNION)) { 4131541Srgrimes tdp = dp; 4141541Srgrimes dp = dp->v_mount->mnt_vnodecovered; 4151541Srgrimes vput(tdp); 4161541Srgrimes VREF(dp); 4171541Srgrimes VOP_LOCK(dp); 4181541Srgrimes goto unionlookup; 4191541Srgrimes } 4201541Srgrimes 4211541Srgrimes if (error != EJUSTRETURN) 4221541Srgrimes goto bad; 4231541Srgrimes /* 4241541Srgrimes * If creating and at end of pathname, then can consider 4251541Srgrimes * allowing file to be created. 4261541Srgrimes */ 42711644Sdg if (rdonly) { 4281541Srgrimes error = EROFS; 4291541Srgrimes goto bad; 4301541Srgrimes } 4319804Sbde if (*cp == '\0' && trailing_slash && 4329804Sbde !(cnp->cn_flags & WILLBEDIR)) { 4339804Sbde error = ENOENT; 4349804Sbde goto bad; 4359804Sbde } 4361541Srgrimes /* 4371541Srgrimes * We return with ni_vp NULL to indicate that the entry 4381541Srgrimes * doesn't currently exist, leaving a pointer to the 4391541Srgrimes * (possibly locked) directory inode in ndp->ni_dvp. 4401541Srgrimes */ 4411541Srgrimes if (cnp->cn_flags & SAVESTART) { 4421541Srgrimes ndp->ni_startdir = ndp->ni_dvp; 4431541Srgrimes VREF(ndp->ni_startdir); 4441541Srgrimes } 4451541Srgrimes return (0); 4461541Srgrimes } 4471541Srgrimes#ifdef NAMEI_DIAGNOSTIC 4481541Srgrimes printf("found\n"); 4491541Srgrimes#endif 4501541Srgrimes 4511541Srgrimes /* 4521541Srgrimes * Take into account any additional components consumed by 4531541Srgrimes * the underlying filesystem. 4541541Srgrimes */ 4551541Srgrimes if (cnp->cn_consume > 0) { 4561541Srgrimes cnp->cn_nameptr += cnp->cn_consume; 4571541Srgrimes ndp->ni_next += cnp->cn_consume; 4581541Srgrimes ndp->ni_pathlen -= cnp->cn_consume; 4591541Srgrimes cnp->cn_consume = 0; 4601541Srgrimes } 4611541Srgrimes 4621541Srgrimes dp = ndp->ni_vp; 4631541Srgrimes 4641541Srgrimes /* 4651541Srgrimes * Check to see if the vnode has been mounted on; 4661541Srgrimes * if so find the root of the mounted file system. 4671541Srgrimes */ 4681541Srgrimes while (dp->v_type == VDIR && (mp = dp->v_mountedhere) && 4691541Srgrimes (cnp->cn_flags & NOCROSSMOUNT) == 0) { 4701541Srgrimes if (mp->mnt_flag & MNT_MLOCK) { 4711541Srgrimes mp->mnt_flag |= MNT_MWAIT; 4723396Sdg (void) tsleep((caddr_t)mp, PVFS, "lookup", 0); 4731541Srgrimes continue; 4741541Srgrimes } 4753148Sphk error = VFS_ROOT(dp->v_mountedhere, &tdp); 4763148Sphk if (error) 4771541Srgrimes goto bad2; 4781541Srgrimes vput(dp); 4791541Srgrimes ndp->ni_vp = dp = tdp; 4801541Srgrimes } 4811541Srgrimes 48210219Sdfr /* 48310219Sdfr * Check for symbolic link 48410219Sdfr */ 48510219Sdfr if ((dp->v_type == VLNK) && 48610219Sdfr ((cnp->cn_flags & FOLLOW) || trailing_slash || 48710219Sdfr *ndp->ni_next == '/')) { 48810219Sdfr cnp->cn_flags |= ISSYMLINK; 48910219Sdfr return (0); 49010219Sdfr } 49110219Sdfr 49210219Sdfr /* 49310219Sdfr * Check for bogus trailing slashes. 49410219Sdfr */ 49510219Sdfr if (trailing_slash && dp->v_type != VDIR) { 49610219Sdfr error = ENOTDIR; 49710219Sdfr goto bad2; 49810219Sdfr } 49910219Sdfr 5001541Srgrimesnextname: 5011541Srgrimes /* 5021541Srgrimes * Not a symbolic link. If more pathname, 5031541Srgrimes * continue at next component, else return. 5041541Srgrimes */ 5051541Srgrimes if (*ndp->ni_next == '/') { 5061541Srgrimes cnp->cn_nameptr = ndp->ni_next; 5071541Srgrimes while (*cnp->cn_nameptr == '/') { 5081541Srgrimes cnp->cn_nameptr++; 5091541Srgrimes ndp->ni_pathlen--; 5101541Srgrimes } 5111541Srgrimes vrele(ndp->ni_dvp); 5121541Srgrimes goto dirloop; 5131541Srgrimes } 5141541Srgrimes /* 51511644Sdg * Disallow directory write attempts on read-only file systems. 5161541Srgrimes */ 51711644Sdg if (rdonly && 51811644Sdg (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME)) { 51911644Sdg error = EROFS; 52011644Sdg goto bad2; 5211541Srgrimes } 5221541Srgrimes if (cnp->cn_flags & SAVESTART) { 5231541Srgrimes ndp->ni_startdir = ndp->ni_dvp; 5241541Srgrimes VREF(ndp->ni_startdir); 5251541Srgrimes } 5261541Srgrimes if (!wantparent) 5271541Srgrimes vrele(ndp->ni_dvp); 5281541Srgrimes if ((cnp->cn_flags & LOCKLEAF) == 0) 5291541Srgrimes VOP_UNLOCK(dp); 5301541Srgrimes return (0); 5311541Srgrimes 5321541Srgrimesbad2: 5331541Srgrimes if ((cnp->cn_flags & LOCKPARENT) && *ndp->ni_next == '\0') 5341541Srgrimes VOP_UNLOCK(ndp->ni_dvp); 5351541Srgrimes vrele(ndp->ni_dvp); 5361541Srgrimesbad: 5371541Srgrimes vput(dp); 5381541Srgrimes ndp->ni_vp = NULL; 5391541Srgrimes return (error); 5401541Srgrimes} 5411541Srgrimes 5423148Sphk/* 5433148Sphk * relookup - lookup a path name component 5443148Sphk * Used by lookup to re-aquire things. 5453148Sphk */ 5463148Sphkint 5473148Sphkrelookup(dvp, vpp, cnp) 5483148Sphk struct vnode *dvp, **vpp; 5493148Sphk struct componentname *cnp; 5503148Sphk{ 5513148Sphk register struct vnode *dp = 0; /* the directory we are searching */ 5523148Sphk int docache; /* == 0 do not cache last component */ 5533148Sphk int wantparent; /* 1 => wantparent or lockparent flag */ 5543148Sphk int rdonly; /* lookup read-only flag bit */ 5553148Sphk int error = 0; 5563148Sphk#ifdef NAMEI_DIAGNOSTIC 5573148Sphk int newhash; /* DEBUG: check name hash */ 5583148Sphk char *cp; /* DEBUG: check name ptr/len */ 5593148Sphk#endif 5601541Srgrimes 5613148Sphk /* 5623148Sphk * Setup: break out flag bits into variables. 5633148Sphk */ 5643148Sphk wantparent = cnp->cn_flags & (LOCKPARENT|WANTPARENT); 5653148Sphk docache = (cnp->cn_flags & NOCACHE) ^ NOCACHE; 5663148Sphk if (cnp->cn_nameiop == DELETE || 5673148Sphk (wantparent && cnp->cn_nameiop != CREATE)) 5683148Sphk docache = 0; 5693148Sphk rdonly = cnp->cn_flags & RDONLY; 5703148Sphk cnp->cn_flags &= ~ISSYMLINK; 5713148Sphk dp = dvp; 5723148Sphk VOP_LOCK(dp); 5733148Sphk 5743148Sphk/* dirloop: */ 5753148Sphk /* 5763148Sphk * Search a new directory. 5773148Sphk * 5783148Sphk * The cn_hash value is for use by vfs_cache. 5793148Sphk * The last component of the filename is left accessible via 5803148Sphk * cnp->cn_nameptr for callers that need the name. Callers needing 5813148Sphk * the name set the SAVENAME flag. When done, they assume 5823148Sphk * responsibility for freeing the pathname buffer. 5833148Sphk */ 5843148Sphk#ifdef NAMEI_DIAGNOSTIC 5853148Sphk for (newhash = 0, cp = cnp->cn_nameptr; *cp != 0 && *cp != '/'; cp++) 5863148Sphk newhash += (unsigned char)*cp; 5873148Sphk if (newhash != cnp->cn_hash) 5883148Sphk panic("relookup: bad hash"); 5893148Sphk if (cnp->cn_namelen != cp - cnp->cn_nameptr) 5903148Sphk panic ("relookup: bad len"); 5913148Sphk if (*cp != 0) 5923148Sphk panic("relookup: not last component"); 5933148Sphk printf("{%s}: ", cnp->cn_nameptr); 5943148Sphk#endif 5953148Sphk 5963148Sphk /* 5973148Sphk * Check for degenerate name (e.g. / or "") 5983148Sphk * which is a way of talking about a directory, 5993148Sphk * e.g. like "/." or ".". 6003148Sphk */ 6013148Sphk if (cnp->cn_nameptr[0] == '\0') { 6023148Sphk if (cnp->cn_nameiop != LOOKUP || wantparent) { 6033148Sphk error = EISDIR; 6043148Sphk goto bad; 6053148Sphk } 6063148Sphk if (dp->v_type != VDIR) { 6073148Sphk error = ENOTDIR; 6083148Sphk goto bad; 6093148Sphk } 6103148Sphk if (!(cnp->cn_flags & LOCKLEAF)) 6113148Sphk VOP_UNLOCK(dp); 6123148Sphk *vpp = dp; 6133148Sphk if (cnp->cn_flags & SAVESTART) 6143148Sphk panic("lookup: SAVESTART"); 6153148Sphk return (0); 6163148Sphk } 6173148Sphk 6183148Sphk if (cnp->cn_flags & ISDOTDOT) 6193148Sphk panic ("relookup: lookup on dot-dot"); 6203148Sphk 6213148Sphk /* 6223148Sphk * We now have a segment name to search for, and a directory to search. 6233148Sphk */ 6243148Sphk error = VOP_LOOKUP(dp, vpp, cnp); 6253148Sphk if (error) { 6263148Sphk#ifdef DIAGNOSTIC 6273148Sphk if (*vpp != NULL) 6283148Sphk panic("leaf should be empty"); 6293148Sphk#endif 6303148Sphk if (error != EJUSTRETURN) 6313148Sphk goto bad; 6323148Sphk /* 6333148Sphk * If creating and at end of pathname, then can consider 6343148Sphk * allowing file to be created. 6353148Sphk */ 63611644Sdg if (rdonly) { 6373148Sphk error = EROFS; 6383148Sphk goto bad; 6393148Sphk } 6403148Sphk /* ASSERT(dvp == ndp->ni_startdir) */ 6413148Sphk if (cnp->cn_flags & SAVESTART) 6423148Sphk VREF(dvp); 6433148Sphk /* 6443148Sphk * We return with ni_vp NULL to indicate that the entry 6453148Sphk * doesn't currently exist, leaving a pointer to the 6463148Sphk * (possibly locked) directory inode in ndp->ni_dvp. 6473148Sphk */ 6483148Sphk return (0); 6493148Sphk } 6503148Sphk dp = *vpp; 6513148Sphk 6523148Sphk#ifdef DIAGNOSTIC 6533148Sphk /* 6543148Sphk * Check for symbolic link 6553148Sphk */ 6563148Sphk if (dp->v_type == VLNK && (cnp->cn_flags & FOLLOW)) 6573148Sphk panic ("relookup: symlink found.\n"); 6583148Sphk#endif 6593148Sphk 6603148Sphk /* 66111644Sdg * Disallow directory write attempts on read-only file systems. 6623148Sphk */ 66311644Sdg if (rdonly && 66411644Sdg (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME)) { 66511644Sdg error = EROFS; 66611644Sdg goto bad2; 6673148Sphk } 6683148Sphk /* ASSERT(dvp == ndp->ni_startdir) */ 6693148Sphk if (cnp->cn_flags & SAVESTART) 6703148Sphk VREF(dvp); 6718876Srgrimes 6723148Sphk if (!wantparent) 6733148Sphk vrele(dvp); 6743148Sphk if ((cnp->cn_flags & LOCKLEAF) == 0) 6753148Sphk VOP_UNLOCK(dp); 6763148Sphk return (0); 6773148Sphk 6783148Sphkbad2: 6793148Sphk if ((cnp->cn_flags & LOCKPARENT) && (cnp->cn_flags & ISLASTCN)) 6803148Sphk VOP_UNLOCK(dvp); 6813148Sphk vrele(dvp); 6823148Sphkbad: 6833148Sphk vput(dp); 6843148Sphk *vpp = NULL; 6853148Sphk return (error); 6863148Sphk} 687