vfs_lookup.c revision 42453
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 3942453Seivind * $Id: vfs_lookup.c,v 1.30 1999/01/08 17:31:16 eivind 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; 9242408Seivind KASSERT(cnp->cn_cred && cnp->cn_proc, ("namei: bad cred/proc")); 9342408Seivind KASSERT((cnp->cn_nameiop & (~OPMASK)) == 0, 9442453Seivind ("namei: nameiop contaminated with flags")); 9542408Seivind KASSERT((cnp->cn_flags & OPMASK) == 0, 9642453Seivind ("namei: flags contaminated with nameiops")); 971541Srgrimes fdp = cnp->cn_proc->p_fd; 981541Srgrimes 991541Srgrimes /* 1001541Srgrimes * Get a buffer for the name to be translated, and copy the 1011541Srgrimes * name into the buffer. 1021541Srgrimes */ 1031541Srgrimes if ((cnp->cn_flags & HASBUF) == 0) 10429653Sdyson cnp->cn_pnbuf = zalloc(namei_zone); 1051541Srgrimes if (ndp->ni_segflg == UIO_SYSSPACE) 1061541Srgrimes error = copystr(ndp->ni_dirp, cnp->cn_pnbuf, 10736735Sdfr MAXPATHLEN, (size_t *)&ndp->ni_pathlen); 1081541Srgrimes else 1091541Srgrimes error = copyinstr(ndp->ni_dirp, cnp->cn_pnbuf, 11036735Sdfr MAXPATHLEN, (size_t *)&ndp->ni_pathlen); 11120069Sbde 11220069Sbde /* 11320069Sbde * Don't allow empty pathnames. 11420069Sbde */ 11520069Sbde if (!error && *cnp->cn_pnbuf == '\0') 11620069Sbde error = ENOENT; 11720069Sbde 1181541Srgrimes if (error) { 11929653Sdyson zfree(namei_zone, cnp->cn_pnbuf); 1201541Srgrimes ndp->ni_vp = NULL; 1211541Srgrimes return (error); 1221541Srgrimes } 1231541Srgrimes ndp->ni_loopcnt = 0; 1241541Srgrimes#ifdef KTRACE 1251541Srgrimes if (KTRPOINT(cnp->cn_proc, KTR_NAMEI)) 1261541Srgrimes ktrnamei(cnp->cn_proc->p_tracep, cnp->cn_pnbuf); 1271541Srgrimes#endif 1281541Srgrimes 1291541Srgrimes /* 1301541Srgrimes * Get starting point for the translation. 1311541Srgrimes */ 13233360Sdyson ndp->ni_rootdir = fdp->fd_rdir; 13333360Sdyson 1341541Srgrimes dp = fdp->fd_cdir; 1351541Srgrimes VREF(dp); 1361541Srgrimes for (;;) { 1371541Srgrimes /* 1381541Srgrimes * Check if root directory should replace current directory. 1391541Srgrimes * Done at start of translation and after symbolic link. 1401541Srgrimes */ 1411541Srgrimes cnp->cn_nameptr = cnp->cn_pnbuf; 1421541Srgrimes if (*(cnp->cn_nameptr) == '/') { 1431541Srgrimes vrele(dp); 1441541Srgrimes while (*(cnp->cn_nameptr) == '/') { 1451541Srgrimes cnp->cn_nameptr++; 1461541Srgrimes ndp->ni_pathlen--; 1471541Srgrimes } 1481541Srgrimes dp = ndp->ni_rootdir; 1491541Srgrimes VREF(dp); 1501541Srgrimes } 1511541Srgrimes ndp->ni_startdir = dp; 1523148Sphk error = lookup(ndp); 1533148Sphk if (error) { 15429653Sdyson zfree(namei_zone, cnp->cn_pnbuf); 1551541Srgrimes return (error); 1561541Srgrimes } 1571541Srgrimes /* 1581541Srgrimes * Check for symbolic link 1591541Srgrimes */ 1601541Srgrimes if ((cnp->cn_flags & ISSYMLINK) == 0) { 1611541Srgrimes if ((cnp->cn_flags & (SAVENAME | SAVESTART)) == 0) 16229653Sdyson zfree(namei_zone, cnp->cn_pnbuf); 1631541Srgrimes else 1641541Srgrimes cnp->cn_flags |= HASBUF; 16532286Sdyson 16632286Sdyson if (ndp->ni_vp && ndp->ni_vp->v_type == VREG && 16732286Sdyson (cnp->cn_nameiop != DELETE) && 16842315Seivind ((cnp->cn_flags & (NOOBJ|LOCKLEAF)) == 16942315Seivind LOCKLEAF)) 17032286Sdyson vfs_object_create(ndp->ni_vp, 17142315Seivind ndp->ni_cnd.cn_proc, 17242315Seivind ndp->ni_cnd.cn_cred); 17332286Sdyson 1741541Srgrimes return (0); 1751541Srgrimes } 1761541Srgrimes if ((cnp->cn_flags & LOCKPARENT) && ndp->ni_pathlen == 1) 17722521Sdyson VOP_UNLOCK(ndp->ni_dvp, 0, p); 1781541Srgrimes if (ndp->ni_loopcnt++ >= MAXSYMLINKS) { 1791541Srgrimes error = ELOOP; 1801541Srgrimes break; 1811541Srgrimes } 1821541Srgrimes if (ndp->ni_pathlen > 1) 18329653Sdyson cp = zalloc(namei_zone); 1841541Srgrimes else 1851541Srgrimes cp = cnp->cn_pnbuf; 1861541Srgrimes aiov.iov_base = cp; 1871541Srgrimes aiov.iov_len = MAXPATHLEN; 1881541Srgrimes auio.uio_iov = &aiov; 1891541Srgrimes auio.uio_iovcnt = 1; 1901541Srgrimes auio.uio_offset = 0; 1911541Srgrimes auio.uio_rw = UIO_READ; 1921541Srgrimes auio.uio_segflg = UIO_SYSSPACE; 1931541Srgrimes auio.uio_procp = (struct proc *)0; 1941541Srgrimes auio.uio_resid = MAXPATHLEN; 1953148Sphk error = VOP_READLINK(ndp->ni_vp, &auio, cnp->cn_cred); 1963148Sphk if (error) { 1971541Srgrimes if (ndp->ni_pathlen > 1) 19829653Sdyson zfree(namei_zone, cp); 1991541Srgrimes break; 2001541Srgrimes } 2011541Srgrimes linklen = MAXPATHLEN - auio.uio_resid; 2021541Srgrimes if (linklen + ndp->ni_pathlen >= MAXPATHLEN) { 2031541Srgrimes if (ndp->ni_pathlen > 1) 20429653Sdyson zfree(namei_zone, cp); 2051541Srgrimes error = ENAMETOOLONG; 2061541Srgrimes break; 2071541Srgrimes } 2081541Srgrimes if (ndp->ni_pathlen > 1) { 2091541Srgrimes bcopy(ndp->ni_next, cp + linklen, ndp->ni_pathlen); 21029653Sdyson zfree(namei_zone, cnp->cn_pnbuf); 2111541Srgrimes cnp->cn_pnbuf = cp; 2121541Srgrimes } else 2131541Srgrimes cnp->cn_pnbuf[linklen] = '\0'; 2141541Srgrimes ndp->ni_pathlen += linklen; 2151541Srgrimes vput(ndp->ni_vp); 2161541Srgrimes dp = ndp->ni_dvp; 2171541Srgrimes } 21829653Sdyson zfree(namei_zone, cnp->cn_pnbuf); 2191541Srgrimes vrele(ndp->ni_dvp); 2201541Srgrimes vput(ndp->ni_vp); 2211541Srgrimes ndp->ni_vp = NULL; 2221541Srgrimes return (error); 2231541Srgrimes} 2241541Srgrimes 2251541Srgrimes/* 2261541Srgrimes * Search a pathname. 2271541Srgrimes * This is a very central and rather complicated routine. 2281541Srgrimes * 2291541Srgrimes * The pathname is pointed to by ni_ptr and is of length ni_pathlen. 2301541Srgrimes * The starting directory is taken from ni_startdir. The pathname is 2311541Srgrimes * descended until done, or a symbolic link is encountered. The variable 2321541Srgrimes * ni_more is clear if the path is completed; it is set to one if a 2331541Srgrimes * symbolic link needing interpretation is encountered. 2341541Srgrimes * 2351541Srgrimes * The flag argument is LOOKUP, CREATE, RENAME, or DELETE depending on 2361541Srgrimes * whether the name is to be looked up, created, renamed, or deleted. 2371541Srgrimes * When CREATE, RENAME, or DELETE is specified, information usable in 2381541Srgrimes * creating, renaming, or deleting a directory entry may be calculated. 2391541Srgrimes * If flag has LOCKPARENT or'ed into it, the parent directory is returned 2401541Srgrimes * locked. If flag has WANTPARENT or'ed into it, the parent directory is 2411541Srgrimes * returned unlocked. Otherwise the parent directory is not returned. If 2421541Srgrimes * the target of the pathname exists and LOCKLEAF is or'ed into the flag 2431541Srgrimes * the target is returned locked, otherwise it is returned unlocked. 2441541Srgrimes * When creating or renaming and LOCKPARENT is specified, the target may not 2451541Srgrimes * be ".". When deleting and LOCKPARENT is specified, the target may be ".". 2468876Srgrimes * 2471541Srgrimes * Overall outline of lookup: 2481541Srgrimes * 2491541Srgrimes * dirloop: 2501541Srgrimes * identify next component of name at ndp->ni_ptr 2511541Srgrimes * handle degenerate case where name is null string 2521541Srgrimes * if .. and crossing mount points and on mounted filesys, find parent 2531541Srgrimes * call VOP_LOOKUP routine for next component name 2541541Srgrimes * directory vnode returned in ni_dvp, unlocked unless LOCKPARENT set 2551541Srgrimes * component vnode returned in ni_vp (if it exists), locked. 2561541Srgrimes * if result vnode is mounted on and crossing mount points, 2571541Srgrimes * find mounted on vnode 2581541Srgrimes * if more components of name, do next level at dirloop 2591541Srgrimes * return the answer in ni_vp, locked if LOCKLEAF set 2601541Srgrimes * if LOCKPARENT set, return locked parent in ni_dvp 2611541Srgrimes * if WANTPARENT set, return unlocked parent in ni_dvp 2621541Srgrimes */ 2631541Srgrimesint 2641541Srgrimeslookup(ndp) 2651541Srgrimes register struct nameidata *ndp; 2661541Srgrimes{ 2671541Srgrimes register char *cp; /* pointer into pathname argument */ 2681541Srgrimes register struct vnode *dp = 0; /* the directory we are searching */ 2691541Srgrimes struct vnode *tdp; /* saved dp */ 2701541Srgrimes struct mount *mp; /* mount table entry */ 2711541Srgrimes int docache; /* == 0 do not cache last component */ 2721541Srgrimes int wantparent; /* 1 => wantparent or lockparent flag */ 2731541Srgrimes int rdonly; /* lookup read-only flag bit */ 2749804Sbde int trailing_slash; 2751541Srgrimes int error = 0; 2761541Srgrimes struct componentname *cnp = &ndp->ni_cnd; 27722521Sdyson struct proc *p = cnp->cn_proc; 2781541Srgrimes 2791541Srgrimes /* 2801541Srgrimes * Setup: break out flag bits into variables. 2811541Srgrimes */ 2821541Srgrimes wantparent = cnp->cn_flags & (LOCKPARENT | WANTPARENT); 2831541Srgrimes docache = (cnp->cn_flags & NOCACHE) ^ NOCACHE; 2841541Srgrimes if (cnp->cn_nameiop == DELETE || 28522874Sbde (wantparent && cnp->cn_nameiop != CREATE && 28622874Sbde cnp->cn_nameiop != LOOKUP)) 2871541Srgrimes docache = 0; 2881541Srgrimes rdonly = cnp->cn_flags & RDONLY; 2891541Srgrimes ndp->ni_dvp = NULL; 2901541Srgrimes cnp->cn_flags &= ~ISSYMLINK; 2911541Srgrimes dp = ndp->ni_startdir; 2921541Srgrimes ndp->ni_startdir = NULLVP; 29322521Sdyson vn_lock(dp, LK_EXCLUSIVE | LK_RETRY, p); 2941541Srgrimes 2951541Srgrimesdirloop: 2961541Srgrimes /* 2971541Srgrimes * Search a new directory. 2981541Srgrimes * 2991541Srgrimes * The cn_hash value is for use by vfs_cache. 3001541Srgrimes * The last component of the filename is left accessible via 3011541Srgrimes * cnp->cn_nameptr for callers that need the name. Callers needing 3021541Srgrimes * the name set the SAVENAME flag. When done, they assume 3031541Srgrimes * responsibility for freeing the pathname buffer. 3041541Srgrimes */ 3051541Srgrimes cnp->cn_consume = 0; 3061541Srgrimes cnp->cn_hash = 0; 3071541Srgrimes for (cp = cnp->cn_nameptr; *cp != 0 && *cp != '/'; cp++) 3081541Srgrimes cnp->cn_hash += (unsigned char)*cp; 3091541Srgrimes cnp->cn_namelen = cp - cnp->cn_nameptr; 3101541Srgrimes if (cnp->cn_namelen > NAME_MAX) { 3111541Srgrimes error = ENAMETOOLONG; 3121541Srgrimes goto bad; 3131541Srgrimes } 3141541Srgrimes#ifdef NAMEI_DIAGNOSTIC 3151541Srgrimes { char c = *cp; 3161541Srgrimes *cp = '\0'; 3171541Srgrimes printf("{%s}: ", cnp->cn_nameptr); 3181541Srgrimes *cp = c; } 3191541Srgrimes#endif 3201541Srgrimes ndp->ni_pathlen -= cnp->cn_namelen; 3211541Srgrimes ndp->ni_next = cp; 3229804Sbde 3239804Sbde /* 3249804Sbde * Replace multiple slashes by a single slash and trailing slashes 3259804Sbde * by a null. This must be done before VOP_LOOKUP() because some 3269804Sbde * fs's don't know about trailing slashes. Remember if there were 3279804Sbde * trailing slashes to handle symlinks, existing non-directories 3289804Sbde * and non-existing files that won't be directories specially later. 3299804Sbde */ 3309804Sbde trailing_slash = 0; 3319804Sbde while (*cp == '/' && (cp[1] == '/' || cp[1] == '\0')) { 3329804Sbde cp++; 3339804Sbde ndp->ni_pathlen--; 3349804Sbde if (*cp == '\0') { 3359804Sbde trailing_slash = 1; 3369804Sbde *ndp->ni_next = '\0'; /* XXX for direnter() ... */ 3379804Sbde } 3389804Sbde } 3399804Sbde ndp->ni_next = cp; 3409804Sbde 3411541Srgrimes cnp->cn_flags |= MAKEENTRY; 3421541Srgrimes if (*cp == '\0' && docache == 0) 3431541Srgrimes cnp->cn_flags &= ~MAKEENTRY; 3441541Srgrimes if (cnp->cn_namelen == 2 && 3451541Srgrimes cnp->cn_nameptr[1] == '.' && cnp->cn_nameptr[0] == '.') 3461541Srgrimes cnp->cn_flags |= ISDOTDOT; 3471541Srgrimes else 3481541Srgrimes cnp->cn_flags &= ~ISDOTDOT; 3491541Srgrimes if (*ndp->ni_next == 0) 3501541Srgrimes cnp->cn_flags |= ISLASTCN; 3511541Srgrimes else 3521541Srgrimes cnp->cn_flags &= ~ISLASTCN; 3531541Srgrimes 3541541Srgrimes 3551541Srgrimes /* 3561541Srgrimes * Check for degenerate name (e.g. / or "") 3571541Srgrimes * which is a way of talking about a directory, 3581541Srgrimes * e.g. like "/." or ".". 3591541Srgrimes */ 3601541Srgrimes if (cnp->cn_nameptr[0] == '\0') { 36122521Sdyson if (dp->v_type != VDIR) { 36222521Sdyson error = ENOTDIR; 36322521Sdyson goto bad; 36422521Sdyson } 3651541Srgrimes if (cnp->cn_nameiop != LOOKUP) { 3661541Srgrimes error = EISDIR; 3671541Srgrimes goto bad; 3681541Srgrimes } 3691541Srgrimes if (wantparent) { 3701541Srgrimes ndp->ni_dvp = dp; 3711541Srgrimes VREF(dp); 3721541Srgrimes } 3731541Srgrimes ndp->ni_vp = dp; 3741541Srgrimes if (!(cnp->cn_flags & (LOCKPARENT | LOCKLEAF))) 37522521Sdyson VOP_UNLOCK(dp, 0, p); 3761541Srgrimes if (cnp->cn_flags & SAVESTART) 3771541Srgrimes panic("lookup: SAVESTART"); 3781541Srgrimes return (0); 3791541Srgrimes } 3801541Srgrimes 3811541Srgrimes /* 3821541Srgrimes * Handle "..": two special cases. 3831541Srgrimes * 1. If at root directory (e.g. after chroot) 3841541Srgrimes * or at absolute root directory 3851541Srgrimes * then ignore it so can't get out. 3861541Srgrimes * 2. If this vnode is the root of a mounted 3871541Srgrimes * filesystem, then replace it with the 3881541Srgrimes * vnode which was mounted on so we take the 3891541Srgrimes * .. in the other file system. 3901541Srgrimes */ 3911541Srgrimes if (cnp->cn_flags & ISDOTDOT) { 3921541Srgrimes for (;;) { 3931541Srgrimes if (dp == ndp->ni_rootdir || dp == rootvnode) { 3941541Srgrimes ndp->ni_dvp = dp; 3951541Srgrimes ndp->ni_vp = dp; 3961541Srgrimes VREF(dp); 3971541Srgrimes goto nextname; 3981541Srgrimes } 3991541Srgrimes if ((dp->v_flag & VROOT) == 0 || 4001541Srgrimes (cnp->cn_flags & NOCROSSMOUNT)) 4011541Srgrimes break; 4021541Srgrimes tdp = dp; 4031541Srgrimes dp = dp->v_mount->mnt_vnodecovered; 4041541Srgrimes vput(tdp); 4051541Srgrimes VREF(dp); 40622521Sdyson vn_lock(dp, LK_EXCLUSIVE | LK_RETRY, p); 4071541Srgrimes } 4081541Srgrimes } 4091541Srgrimes 4101541Srgrimes /* 4111541Srgrimes * We now have a segment name to search for, and a directory to search. 4121541Srgrimes */ 4131541Srgrimesunionlookup: 4141541Srgrimes ndp->ni_dvp = dp; 41522521Sdyson ndp->ni_vp = NULL; 41624624Sdfr ASSERT_VOP_LOCKED(dp, "lookup"); 41722521Sdyson if (error = VOP_LOOKUP(dp, &ndp->ni_vp, cnp)) { 41842408Seivind KASSERT(ndp->ni_vp == NULL, ("leaf should be empty")); 4191541Srgrimes#ifdef NAMEI_DIAGNOSTIC 4201541Srgrimes printf("not found\n"); 4211541Srgrimes#endif 4221541Srgrimes if ((error == ENOENT) && 4231541Srgrimes (dp->v_flag & VROOT) && 4241541Srgrimes (dp->v_mount->mnt_flag & MNT_UNION)) { 4251541Srgrimes tdp = dp; 4261541Srgrimes dp = dp->v_mount->mnt_vnodecovered; 4271541Srgrimes vput(tdp); 4281541Srgrimes VREF(dp); 42922521Sdyson vn_lock(dp, LK_EXCLUSIVE | LK_RETRY, p); 4301541Srgrimes goto unionlookup; 4311541Srgrimes } 4321541Srgrimes 4331541Srgrimes if (error != EJUSTRETURN) 4341541Srgrimes goto bad; 4351541Srgrimes /* 4361541Srgrimes * If creating and at end of pathname, then can consider 4371541Srgrimes * allowing file to be created. 4381541Srgrimes */ 43911644Sdg if (rdonly) { 4401541Srgrimes error = EROFS; 4411541Srgrimes goto bad; 4421541Srgrimes } 4439804Sbde if (*cp == '\0' && trailing_slash && 4449804Sbde !(cnp->cn_flags & WILLBEDIR)) { 4459804Sbde error = ENOENT; 4469804Sbde goto bad; 4479804Sbde } 4481541Srgrimes /* 4491541Srgrimes * We return with ni_vp NULL to indicate that the entry 4501541Srgrimes * doesn't currently exist, leaving a pointer to the 4511541Srgrimes * (possibly locked) directory inode in ndp->ni_dvp. 4521541Srgrimes */ 4531541Srgrimes if (cnp->cn_flags & SAVESTART) { 4541541Srgrimes ndp->ni_startdir = ndp->ni_dvp; 4551541Srgrimes VREF(ndp->ni_startdir); 4561541Srgrimes } 4571541Srgrimes return (0); 4581541Srgrimes } 4591541Srgrimes#ifdef NAMEI_DIAGNOSTIC 4601541Srgrimes printf("found\n"); 4611541Srgrimes#endif 4621541Srgrimes 46324624Sdfr ASSERT_VOP_LOCKED(ndp->ni_vp, "lookup"); 46424624Sdfr 4651541Srgrimes /* 4661541Srgrimes * Take into account any additional components consumed by 4671541Srgrimes * the underlying filesystem. 4681541Srgrimes */ 4691541Srgrimes if (cnp->cn_consume > 0) { 4701541Srgrimes cnp->cn_nameptr += cnp->cn_consume; 4711541Srgrimes ndp->ni_next += cnp->cn_consume; 4721541Srgrimes ndp->ni_pathlen -= cnp->cn_consume; 4731541Srgrimes cnp->cn_consume = 0; 4741541Srgrimes } 4751541Srgrimes 4761541Srgrimes dp = ndp->ni_vp; 4771541Srgrimes 4781541Srgrimes /* 4791541Srgrimes * Check to see if the vnode has been mounted on; 4801541Srgrimes * if so find the root of the mounted file system. 4811541Srgrimes */ 4821541Srgrimes while (dp->v_type == VDIR && (mp = dp->v_mountedhere) && 4831541Srgrimes (cnp->cn_flags & NOCROSSMOUNT) == 0) { 48422521Sdyson if (vfs_busy(mp, 0, 0, p)) 4851541Srgrimes continue; 48622521Sdyson error = VFS_ROOT(mp, &tdp); 48722521Sdyson vfs_unbusy(mp, p); 4883148Sphk if (error) 4891541Srgrimes goto bad2; 4901541Srgrimes vput(dp); 4911541Srgrimes ndp->ni_vp = dp = tdp; 4921541Srgrimes } 4931541Srgrimes 49410219Sdfr /* 49510219Sdfr * Check for symbolic link 49610219Sdfr */ 49710219Sdfr if ((dp->v_type == VLNK) && 49810219Sdfr ((cnp->cn_flags & FOLLOW) || trailing_slash || 49910219Sdfr *ndp->ni_next == '/')) { 50010219Sdfr cnp->cn_flags |= ISSYMLINK; 50135105Swosch if (dp->v_mount->mnt_flag & MNT_NOSYMFOLLOW) { 50235105Swosch error = EACCES; 50335105Swosch goto bad2; 50435105Swosch } 50510219Sdfr return (0); 50610219Sdfr } 50710219Sdfr 50810219Sdfr /* 50910219Sdfr * Check for bogus trailing slashes. 51010219Sdfr */ 51110219Sdfr if (trailing_slash && dp->v_type != VDIR) { 51210219Sdfr error = ENOTDIR; 51310219Sdfr goto bad2; 51410219Sdfr } 51510219Sdfr 5161541Srgrimesnextname: 5171541Srgrimes /* 5181541Srgrimes * Not a symbolic link. If more pathname, 5191541Srgrimes * continue at next component, else return. 5201541Srgrimes */ 5211541Srgrimes if (*ndp->ni_next == '/') { 5221541Srgrimes cnp->cn_nameptr = ndp->ni_next; 5231541Srgrimes while (*cnp->cn_nameptr == '/') { 5241541Srgrimes cnp->cn_nameptr++; 5251541Srgrimes ndp->ni_pathlen--; 5261541Srgrimes } 52724624Sdfr if (ndp->ni_dvp != ndp->ni_vp) { 52824624Sdfr ASSERT_VOP_UNLOCKED(ndp->ni_dvp, "lookup"); 52924624Sdfr } 5301541Srgrimes vrele(ndp->ni_dvp); 5311541Srgrimes goto dirloop; 5321541Srgrimes } 5331541Srgrimes /* 53411644Sdg * Disallow directory write attempts on read-only file systems. 5351541Srgrimes */ 53611644Sdg if (rdonly && 53711644Sdg (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME)) { 53811644Sdg error = EROFS; 53911644Sdg goto bad2; 5401541Srgrimes } 5411541Srgrimes if (cnp->cn_flags & SAVESTART) { 5421541Srgrimes ndp->ni_startdir = ndp->ni_dvp; 5431541Srgrimes VREF(ndp->ni_startdir); 5441541Srgrimes } 5451541Srgrimes if (!wantparent) 5461541Srgrimes vrele(ndp->ni_dvp); 54732071Sdyson 5481541Srgrimes if ((cnp->cn_flags & LOCKLEAF) == 0) 54922521Sdyson VOP_UNLOCK(dp, 0, p); 5501541Srgrimes return (0); 5511541Srgrimes 5521541Srgrimesbad2: 5531541Srgrimes if ((cnp->cn_flags & LOCKPARENT) && *ndp->ni_next == '\0') 55422521Sdyson VOP_UNLOCK(ndp->ni_dvp, 0, p); 5551541Srgrimes vrele(ndp->ni_dvp); 5561541Srgrimesbad: 5571541Srgrimes vput(dp); 5581541Srgrimes ndp->ni_vp = NULL; 5591541Srgrimes return (error); 5601541Srgrimes} 5611541Srgrimes 5623148Sphk/* 5633148Sphk * relookup - lookup a path name component 5643148Sphk * Used by lookup to re-aquire things. 5653148Sphk */ 5663148Sphkint 5673148Sphkrelookup(dvp, vpp, cnp) 5683148Sphk struct vnode *dvp, **vpp; 5693148Sphk struct componentname *cnp; 5703148Sphk{ 57122521Sdyson struct proc *p = cnp->cn_proc; 57222521Sdyson struct vnode *dp = 0; /* the directory we are searching */ 5733148Sphk int docache; /* == 0 do not cache last component */ 5743148Sphk int wantparent; /* 1 => wantparent or lockparent flag */ 5753148Sphk int rdonly; /* lookup read-only flag bit */ 5763148Sphk int error = 0; 5773148Sphk#ifdef NAMEI_DIAGNOSTIC 5783148Sphk int newhash; /* DEBUG: check name hash */ 5793148Sphk char *cp; /* DEBUG: check name ptr/len */ 5803148Sphk#endif 5811541Srgrimes 5823148Sphk /* 5833148Sphk * Setup: break out flag bits into variables. 5843148Sphk */ 5853148Sphk wantparent = cnp->cn_flags & (LOCKPARENT|WANTPARENT); 5863148Sphk docache = (cnp->cn_flags & NOCACHE) ^ NOCACHE; 5873148Sphk if (cnp->cn_nameiop == DELETE || 5883148Sphk (wantparent && cnp->cn_nameiop != CREATE)) 5893148Sphk docache = 0; 5903148Sphk rdonly = cnp->cn_flags & RDONLY; 5913148Sphk cnp->cn_flags &= ~ISSYMLINK; 5923148Sphk dp = dvp; 59322521Sdyson vn_lock(dp, LK_EXCLUSIVE | LK_RETRY, p); 5943148Sphk 5953148Sphk/* dirloop: */ 5963148Sphk /* 5973148Sphk * Search a new directory. 5983148Sphk * 5993148Sphk * The cn_hash value is for use by vfs_cache. 6003148Sphk * The last component of the filename is left accessible via 6013148Sphk * cnp->cn_nameptr for callers that need the name. Callers needing 6023148Sphk * the name set the SAVENAME flag. When done, they assume 6033148Sphk * responsibility for freeing the pathname buffer. 6043148Sphk */ 6053148Sphk#ifdef NAMEI_DIAGNOSTIC 6063148Sphk for (newhash = 0, cp = cnp->cn_nameptr; *cp != 0 && *cp != '/'; cp++) 6073148Sphk newhash += (unsigned char)*cp; 6083148Sphk if (newhash != cnp->cn_hash) 6093148Sphk panic("relookup: bad hash"); 6103148Sphk if (cnp->cn_namelen != cp - cnp->cn_nameptr) 6113148Sphk panic ("relookup: bad len"); 6123148Sphk if (*cp != 0) 6133148Sphk panic("relookup: not last component"); 6143148Sphk printf("{%s}: ", cnp->cn_nameptr); 6153148Sphk#endif 6163148Sphk 6173148Sphk /* 6183148Sphk * Check for degenerate name (e.g. / or "") 6193148Sphk * which is a way of talking about a directory, 6203148Sphk * e.g. like "/." or ".". 6213148Sphk */ 6223148Sphk if (cnp->cn_nameptr[0] == '\0') { 6233148Sphk if (cnp->cn_nameiop != LOOKUP || wantparent) { 6243148Sphk error = EISDIR; 6253148Sphk goto bad; 6263148Sphk } 6273148Sphk if (dp->v_type != VDIR) { 6283148Sphk error = ENOTDIR; 6293148Sphk goto bad; 6303148Sphk } 6313148Sphk if (!(cnp->cn_flags & LOCKLEAF)) 63222521Sdyson VOP_UNLOCK(dp, 0, p); 6333148Sphk *vpp = dp; 6343148Sphk if (cnp->cn_flags & SAVESTART) 6353148Sphk panic("lookup: SAVESTART"); 6363148Sphk return (0); 6373148Sphk } 6383148Sphk 6393148Sphk if (cnp->cn_flags & ISDOTDOT) 6403148Sphk panic ("relookup: lookup on dot-dot"); 6413148Sphk 6423148Sphk /* 6433148Sphk * We now have a segment name to search for, and a directory to search. 6443148Sphk */ 64522521Sdyson if (error = VOP_LOOKUP(dp, vpp, cnp)) { 64642408Seivind KASSERT(*vpp == NULL, ("leaf should be empty")); 6473148Sphk if (error != EJUSTRETURN) 6483148Sphk goto bad; 6493148Sphk /* 6503148Sphk * If creating and at end of pathname, then can consider 6513148Sphk * allowing file to be created. 6523148Sphk */ 65311644Sdg if (rdonly) { 6543148Sphk error = EROFS; 6553148Sphk goto bad; 6563148Sphk } 6573148Sphk /* ASSERT(dvp == ndp->ni_startdir) */ 6583148Sphk if (cnp->cn_flags & SAVESTART) 6593148Sphk VREF(dvp); 6603148Sphk /* 6613148Sphk * We return with ni_vp NULL to indicate that the entry 6623148Sphk * doesn't currently exist, leaving a pointer to the 6633148Sphk * (possibly locked) directory inode in ndp->ni_dvp. 6643148Sphk */ 6653148Sphk return (0); 6663148Sphk } 6673148Sphk dp = *vpp; 6683148Sphk 6693148Sphk /* 6703148Sphk * Check for symbolic link 6713148Sphk */ 67242408Seivind KASSERT(dp->v_type != VLNK || !(cnp->cn_flags & FOLLOW), 67342453Seivind ("relookup: symlink found.\n")); 6743148Sphk 6753148Sphk /* 67611644Sdg * Disallow directory write attempts on read-only file systems. 6773148Sphk */ 67811644Sdg if (rdonly && 67911644Sdg (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME)) { 68011644Sdg error = EROFS; 68111644Sdg goto bad2; 6823148Sphk } 6833148Sphk /* ASSERT(dvp == ndp->ni_startdir) */ 6843148Sphk if (cnp->cn_flags & SAVESTART) 6853148Sphk VREF(dvp); 68622521Sdyson 6873148Sphk if (!wantparent) 6883148Sphk vrele(dvp); 68932071Sdyson 69032286Sdyson if (dp->v_type == VREG && 69132286Sdyson ((cnp->cn_flags & (NOOBJ|LOCKLEAF)) == LOCKLEAF)) 69242315Seivind vfs_object_create(dp, cnp->cn_proc, cnp->cn_cred); 69332071Sdyson 6943148Sphk if ((cnp->cn_flags & LOCKLEAF) == 0) 69522521Sdyson VOP_UNLOCK(dp, 0, p); 6963148Sphk return (0); 6973148Sphk 6983148Sphkbad2: 6993148Sphk if ((cnp->cn_flags & LOCKPARENT) && (cnp->cn_flags & ISLASTCN)) 70022521Sdyson VOP_UNLOCK(dvp, 0, p); 7013148Sphk vrele(dvp); 7023148Sphkbad: 7033148Sphk vput(dp); 7043148Sphk *vpp = NULL; 7053148Sphk return (error); 7063148Sphk} 707