vfs_lookup.c revision 65805
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 3950477Speter * $FreeBSD: head/sys/kern/vfs_lookup.c 65805 2000-09-13 08:57:56Z bp $ 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; 13351649Sphk ndp->ni_topdir = fdp->fd_jdir; 13433360Sdyson 1351541Srgrimes dp = fdp->fd_cdir; 1361541Srgrimes VREF(dp); 1371541Srgrimes for (;;) { 1381541Srgrimes /* 1391541Srgrimes * Check if root directory should replace current directory. 1401541Srgrimes * Done at start of translation and after symbolic link. 1411541Srgrimes */ 1421541Srgrimes cnp->cn_nameptr = cnp->cn_pnbuf; 1431541Srgrimes if (*(cnp->cn_nameptr) == '/') { 1441541Srgrimes vrele(dp); 1451541Srgrimes while (*(cnp->cn_nameptr) == '/') { 1461541Srgrimes cnp->cn_nameptr++; 1471541Srgrimes ndp->ni_pathlen--; 1481541Srgrimes } 1491541Srgrimes dp = ndp->ni_rootdir; 1501541Srgrimes VREF(dp); 1511541Srgrimes } 1521541Srgrimes ndp->ni_startdir = dp; 1533148Sphk error = lookup(ndp); 1543148Sphk if (error) { 15529653Sdyson zfree(namei_zone, cnp->cn_pnbuf); 1561541Srgrimes return (error); 1571541Srgrimes } 1581541Srgrimes /* 1591541Srgrimes * Check for symbolic link 1601541Srgrimes */ 1611541Srgrimes if ((cnp->cn_flags & ISSYMLINK) == 0) { 1621541Srgrimes if ((cnp->cn_flags & (SAVENAME | SAVESTART)) == 0) 16329653Sdyson zfree(namei_zone, cnp->cn_pnbuf); 1641541Srgrimes else 1651541Srgrimes cnp->cn_flags |= HASBUF; 16632286Sdyson 16749101Salc if (vn_canvmio(ndp->ni_vp) == TRUE && 16832286Sdyson (cnp->cn_nameiop != DELETE) && 16942315Seivind ((cnp->cn_flags & (NOOBJ|LOCKLEAF)) == 17042315Seivind LOCKLEAF)) 17132286Sdyson vfs_object_create(ndp->ni_vp, 17242315Seivind ndp->ni_cnd.cn_proc, 17342315Seivind ndp->ni_cnd.cn_cred); 17432286Sdyson 1751541Srgrimes return (0); 1761541Srgrimes } 1771541Srgrimes if ((cnp->cn_flags & LOCKPARENT) && ndp->ni_pathlen == 1) 17822521Sdyson VOP_UNLOCK(ndp->ni_dvp, 0, p); 1791541Srgrimes if (ndp->ni_loopcnt++ >= MAXSYMLINKS) { 1801541Srgrimes error = ELOOP; 1811541Srgrimes break; 1821541Srgrimes } 1831541Srgrimes if (ndp->ni_pathlen > 1) 18429653Sdyson cp = zalloc(namei_zone); 1851541Srgrimes else 1861541Srgrimes cp = cnp->cn_pnbuf; 1871541Srgrimes aiov.iov_base = cp; 1881541Srgrimes aiov.iov_len = MAXPATHLEN; 1891541Srgrimes auio.uio_iov = &aiov; 1901541Srgrimes auio.uio_iovcnt = 1; 1911541Srgrimes auio.uio_offset = 0; 1921541Srgrimes auio.uio_rw = UIO_READ; 1931541Srgrimes auio.uio_segflg = UIO_SYSSPACE; 1941541Srgrimes auio.uio_procp = (struct proc *)0; 1951541Srgrimes auio.uio_resid = MAXPATHLEN; 1963148Sphk error = VOP_READLINK(ndp->ni_vp, &auio, cnp->cn_cred); 1973148Sphk if (error) { 1981541Srgrimes if (ndp->ni_pathlen > 1) 19929653Sdyson zfree(namei_zone, cp); 2001541Srgrimes break; 2011541Srgrimes } 2021541Srgrimes linklen = MAXPATHLEN - auio.uio_resid; 2031541Srgrimes if (linklen + ndp->ni_pathlen >= MAXPATHLEN) { 2041541Srgrimes if (ndp->ni_pathlen > 1) 20529653Sdyson zfree(namei_zone, cp); 2061541Srgrimes error = ENAMETOOLONG; 2071541Srgrimes break; 2081541Srgrimes } 2091541Srgrimes if (ndp->ni_pathlen > 1) { 2101541Srgrimes bcopy(ndp->ni_next, cp + linklen, ndp->ni_pathlen); 21129653Sdyson zfree(namei_zone, cnp->cn_pnbuf); 2121541Srgrimes cnp->cn_pnbuf = cp; 2131541Srgrimes } else 2141541Srgrimes cnp->cn_pnbuf[linklen] = '\0'; 2151541Srgrimes ndp->ni_pathlen += linklen; 2161541Srgrimes vput(ndp->ni_vp); 2171541Srgrimes dp = ndp->ni_dvp; 2181541Srgrimes } 21929653Sdyson zfree(namei_zone, cnp->cn_pnbuf); 2201541Srgrimes vrele(ndp->ni_dvp); 2211541Srgrimes vput(ndp->ni_vp); 2221541Srgrimes ndp->ni_vp = NULL; 2231541Srgrimes return (error); 2241541Srgrimes} 2251541Srgrimes 2261541Srgrimes/* 2271541Srgrimes * Search a pathname. 2281541Srgrimes * This is a very central and rather complicated routine. 2291541Srgrimes * 2301541Srgrimes * The pathname is pointed to by ni_ptr and is of length ni_pathlen. 2311541Srgrimes * The starting directory is taken from ni_startdir. The pathname is 2321541Srgrimes * descended until done, or a symbolic link is encountered. The variable 2331541Srgrimes * ni_more is clear if the path is completed; it is set to one if a 2341541Srgrimes * symbolic link needing interpretation is encountered. 2351541Srgrimes * 2361541Srgrimes * The flag argument is LOOKUP, CREATE, RENAME, or DELETE depending on 2371541Srgrimes * whether the name is to be looked up, created, renamed, or deleted. 2381541Srgrimes * When CREATE, RENAME, or DELETE is specified, information usable in 2391541Srgrimes * creating, renaming, or deleting a directory entry may be calculated. 2401541Srgrimes * If flag has LOCKPARENT or'ed into it, the parent directory is returned 2411541Srgrimes * locked. If flag has WANTPARENT or'ed into it, the parent directory is 2421541Srgrimes * returned unlocked. Otherwise the parent directory is not returned. If 2431541Srgrimes * the target of the pathname exists and LOCKLEAF is or'ed into the flag 2441541Srgrimes * the target is returned locked, otherwise it is returned unlocked. 2451541Srgrimes * When creating or renaming and LOCKPARENT is specified, the target may not 2461541Srgrimes * be ".". When deleting and LOCKPARENT is specified, the target may be ".". 2478876Srgrimes * 2481541Srgrimes * Overall outline of lookup: 2491541Srgrimes * 2501541Srgrimes * dirloop: 2511541Srgrimes * identify next component of name at ndp->ni_ptr 2521541Srgrimes * handle degenerate case where name is null string 2531541Srgrimes * if .. and crossing mount points and on mounted filesys, find parent 2541541Srgrimes * call VOP_LOOKUP routine for next component name 2551541Srgrimes * directory vnode returned in ni_dvp, unlocked unless LOCKPARENT set 2561541Srgrimes * component vnode returned in ni_vp (if it exists), locked. 2571541Srgrimes * if result vnode is mounted on and crossing mount points, 2581541Srgrimes * find mounted on vnode 2591541Srgrimes * if more components of name, do next level at dirloop 2601541Srgrimes * return the answer in ni_vp, locked if LOCKLEAF set 2611541Srgrimes * if LOCKPARENT set, return locked parent in ni_dvp 2621541Srgrimes * if WANTPARENT set, return unlocked parent in ni_dvp 2631541Srgrimes */ 2641541Srgrimesint 2651541Srgrimeslookup(ndp) 2661541Srgrimes register struct nameidata *ndp; 2671541Srgrimes{ 2681541Srgrimes register char *cp; /* pointer into pathname argument */ 2691541Srgrimes register struct vnode *dp = 0; /* the directory we are searching */ 2701541Srgrimes struct vnode *tdp; /* saved dp */ 2711541Srgrimes struct mount *mp; /* mount table entry */ 2721541Srgrimes int docache; /* == 0 do not cache last component */ 2731541Srgrimes int wantparent; /* 1 => wantparent or lockparent flag */ 2741541Srgrimes int rdonly; /* lookup read-only flag bit */ 2759804Sbde int trailing_slash; 2761541Srgrimes int error = 0; 27765805Sbp int dpunlocked = 0; /* dp has already been unlocked */ 2781541Srgrimes struct componentname *cnp = &ndp->ni_cnd; 27922521Sdyson struct proc *p = cnp->cn_proc; 2801541Srgrimes 2811541Srgrimes /* 2821541Srgrimes * Setup: break out flag bits into variables. 2831541Srgrimes */ 2841541Srgrimes wantparent = cnp->cn_flags & (LOCKPARENT | WANTPARENT); 2851541Srgrimes docache = (cnp->cn_flags & NOCACHE) ^ NOCACHE; 2861541Srgrimes if (cnp->cn_nameiop == DELETE || 28722874Sbde (wantparent && cnp->cn_nameiop != CREATE && 28822874Sbde cnp->cn_nameiop != LOOKUP)) 2891541Srgrimes docache = 0; 2901541Srgrimes rdonly = cnp->cn_flags & RDONLY; 2911541Srgrimes ndp->ni_dvp = NULL; 2921541Srgrimes cnp->cn_flags &= ~ISSYMLINK; 2931541Srgrimes dp = ndp->ni_startdir; 2941541Srgrimes ndp->ni_startdir = NULLVP; 29522521Sdyson vn_lock(dp, LK_EXCLUSIVE | LK_RETRY, p); 2961541Srgrimes 2971541Srgrimesdirloop: 2981541Srgrimes /* 2991541Srgrimes * Search a new directory. 3001541Srgrimes * 3011541Srgrimes * The last component of the filename is left accessible via 3021541Srgrimes * cnp->cn_nameptr for callers that need the name. Callers needing 3031541Srgrimes * the name set the SAVENAME flag. When done, they assume 3041541Srgrimes * responsibility for freeing the pathname buffer. 3051541Srgrimes */ 3061541Srgrimes cnp->cn_consume = 0; 3071541Srgrimes for (cp = cnp->cn_nameptr; *cp != 0 && *cp != '/'; cp++) 30851906Sphk continue; 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); 37654655Seivind /* XXX This should probably move to the top of function. */ 3771541Srgrimes if (cnp->cn_flags & SAVESTART) 3781541Srgrimes panic("lookup: SAVESTART"); 3791541Srgrimes return (0); 3801541Srgrimes } 3811541Srgrimes 3821541Srgrimes /* 3831541Srgrimes * Handle "..": two special cases. 3841541Srgrimes * 1. If at root directory (e.g. after chroot) 3851541Srgrimes * or at absolute root directory 3861541Srgrimes * then ignore it so can't get out. 3871541Srgrimes * 2. If this vnode is the root of a mounted 3881541Srgrimes * filesystem, then replace it with the 3891541Srgrimes * vnode which was mounted on so we take the 3901541Srgrimes * .. in the other file system. 39151649Sphk * 3. If the vnode is the top directory of 39251649Sphk * the jail or chroot, don't let them out. 3931541Srgrimes */ 3941541Srgrimes if (cnp->cn_flags & ISDOTDOT) { 3951541Srgrimes for (;;) { 39651649Sphk if (dp == ndp->ni_rootdir || 39751649Sphk dp == ndp->ni_topdir || 39851649Sphk dp == rootvnode) { 3991541Srgrimes ndp->ni_dvp = dp; 4001541Srgrimes ndp->ni_vp = dp; 4011541Srgrimes VREF(dp); 4021541Srgrimes goto nextname; 4031541Srgrimes } 4041541Srgrimes if ((dp->v_flag & VROOT) == 0 || 4051541Srgrimes (cnp->cn_flags & NOCROSSMOUNT)) 4061541Srgrimes break; 4071541Srgrimes tdp = dp; 4081541Srgrimes dp = dp->v_mount->mnt_vnodecovered; 4091541Srgrimes vput(tdp); 4101541Srgrimes VREF(dp); 41122521Sdyson vn_lock(dp, LK_EXCLUSIVE | LK_RETRY, p); 4121541Srgrimes } 4131541Srgrimes } 4141541Srgrimes 4151541Srgrimes /* 4161541Srgrimes * We now have a segment name to search for, and a directory to search. 4171541Srgrimes */ 4181541Srgrimesunionlookup: 4191541Srgrimes ndp->ni_dvp = dp; 42022521Sdyson ndp->ni_vp = NULL; 42124624Sdfr ASSERT_VOP_LOCKED(dp, "lookup"); 42243301Sdillon if ((error = VOP_LOOKUP(dp, &ndp->ni_vp, cnp)) != 0) { 42342408Seivind KASSERT(ndp->ni_vp == NULL, ("leaf should be empty")); 4241541Srgrimes#ifdef NAMEI_DIAGNOSTIC 4251541Srgrimes printf("not found\n"); 4261541Srgrimes#endif 4271541Srgrimes if ((error == ENOENT) && 4281541Srgrimes (dp->v_flag & VROOT) && 4291541Srgrimes (dp->v_mount->mnt_flag & MNT_UNION)) { 4301541Srgrimes tdp = dp; 4311541Srgrimes dp = dp->v_mount->mnt_vnodecovered; 4321541Srgrimes vput(tdp); 4331541Srgrimes VREF(dp); 43422521Sdyson vn_lock(dp, LK_EXCLUSIVE | LK_RETRY, p); 4351541Srgrimes goto unionlookup; 4361541Srgrimes } 4371541Srgrimes 4381541Srgrimes if (error != EJUSTRETURN) 4391541Srgrimes goto bad; 4401541Srgrimes /* 4411541Srgrimes * If creating and at end of pathname, then can consider 4421541Srgrimes * allowing file to be created. 4431541Srgrimes */ 44411644Sdg if (rdonly) { 4451541Srgrimes error = EROFS; 4461541Srgrimes goto bad; 4471541Srgrimes } 4489804Sbde if (*cp == '\0' && trailing_slash && 4499804Sbde !(cnp->cn_flags & WILLBEDIR)) { 4509804Sbde error = ENOENT; 4519804Sbde goto bad; 4529804Sbde } 4531541Srgrimes /* 4541541Srgrimes * We return with ni_vp NULL to indicate that the entry 4551541Srgrimes * doesn't currently exist, leaving a pointer to the 4561541Srgrimes * (possibly locked) directory inode in ndp->ni_dvp. 4571541Srgrimes */ 4581541Srgrimes if (cnp->cn_flags & SAVESTART) { 4591541Srgrimes ndp->ni_startdir = ndp->ni_dvp; 4601541Srgrimes VREF(ndp->ni_startdir); 4611541Srgrimes } 4621541Srgrimes return (0); 4631541Srgrimes } 4641541Srgrimes#ifdef NAMEI_DIAGNOSTIC 4651541Srgrimes printf("found\n"); 4661541Srgrimes#endif 4671541Srgrimes 46824624Sdfr ASSERT_VOP_LOCKED(ndp->ni_vp, "lookup"); 46924624Sdfr 4701541Srgrimes /* 4711541Srgrimes * Take into account any additional components consumed by 4721541Srgrimes * the underlying filesystem. 4731541Srgrimes */ 4741541Srgrimes if (cnp->cn_consume > 0) { 4751541Srgrimes cnp->cn_nameptr += cnp->cn_consume; 4761541Srgrimes ndp->ni_next += cnp->cn_consume; 4771541Srgrimes ndp->ni_pathlen -= cnp->cn_consume; 4781541Srgrimes cnp->cn_consume = 0; 4791541Srgrimes } 4801541Srgrimes 4811541Srgrimes dp = ndp->ni_vp; 4821541Srgrimes 4831541Srgrimes /* 4841541Srgrimes * Check to see if the vnode has been mounted on; 4851541Srgrimes * if so find the root of the mounted file system. 4861541Srgrimes */ 4871541Srgrimes while (dp->v_type == VDIR && (mp = dp->v_mountedhere) && 4881541Srgrimes (cnp->cn_flags & NOCROSSMOUNT) == 0) { 48922521Sdyson if (vfs_busy(mp, 0, 0, p)) 4901541Srgrimes continue; 49165805Sbp VOP_UNLOCK(dp, 0, p); 49222521Sdyson error = VFS_ROOT(mp, &tdp); 49322521Sdyson vfs_unbusy(mp, p); 49465805Sbp if (error) { 49565805Sbp dpunlocked = 1; 4961541Srgrimes goto bad2; 49765805Sbp } 49865805Sbp vrele(dp); 4991541Srgrimes ndp->ni_vp = dp = tdp; 5001541Srgrimes } 5011541Srgrimes 50210219Sdfr /* 50310219Sdfr * Check for symbolic link 50410219Sdfr */ 50510219Sdfr if ((dp->v_type == VLNK) && 50610219Sdfr ((cnp->cn_flags & FOLLOW) || trailing_slash || 50710219Sdfr *ndp->ni_next == '/')) { 50810219Sdfr cnp->cn_flags |= ISSYMLINK; 50935105Swosch if (dp->v_mount->mnt_flag & MNT_NOSYMFOLLOW) { 51035105Swosch error = EACCES; 51135105Swosch goto bad2; 51235105Swosch } 51310219Sdfr return (0); 51410219Sdfr } 51510219Sdfr 51610219Sdfr /* 51710219Sdfr * Check for bogus trailing slashes. 51810219Sdfr */ 51910219Sdfr if (trailing_slash && dp->v_type != VDIR) { 52010219Sdfr error = ENOTDIR; 52110219Sdfr goto bad2; 52210219Sdfr } 52310219Sdfr 5241541Srgrimesnextname: 5251541Srgrimes /* 5261541Srgrimes * Not a symbolic link. If more pathname, 5271541Srgrimes * continue at next component, else return. 5281541Srgrimes */ 5291541Srgrimes if (*ndp->ni_next == '/') { 5301541Srgrimes cnp->cn_nameptr = ndp->ni_next; 5311541Srgrimes while (*cnp->cn_nameptr == '/') { 5321541Srgrimes cnp->cn_nameptr++; 5331541Srgrimes ndp->ni_pathlen--; 5341541Srgrimes } 53554655Seivind if (ndp->ni_dvp != ndp->ni_vp) 53654655Seivind ASSERT_VOP_UNLOCKED(ndp->ni_dvp, "lookup"); 5371541Srgrimes vrele(ndp->ni_dvp); 5381541Srgrimes goto dirloop; 5391541Srgrimes } 5401541Srgrimes /* 54111644Sdg * Disallow directory write attempts on read-only file systems. 5421541Srgrimes */ 54311644Sdg if (rdonly && 54411644Sdg (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME)) { 54511644Sdg error = EROFS; 54611644Sdg goto bad2; 5471541Srgrimes } 5481541Srgrimes if (cnp->cn_flags & SAVESTART) { 5491541Srgrimes ndp->ni_startdir = ndp->ni_dvp; 5501541Srgrimes VREF(ndp->ni_startdir); 5511541Srgrimes } 5521541Srgrimes if (!wantparent) 5531541Srgrimes vrele(ndp->ni_dvp); 55432071Sdyson 5551541Srgrimes if ((cnp->cn_flags & LOCKLEAF) == 0) 55622521Sdyson VOP_UNLOCK(dp, 0, p); 5571541Srgrimes return (0); 5581541Srgrimes 5591541Srgrimesbad2: 5601541Srgrimes if ((cnp->cn_flags & LOCKPARENT) && *ndp->ni_next == '\0') 56122521Sdyson VOP_UNLOCK(ndp->ni_dvp, 0, p); 5621541Srgrimes vrele(ndp->ni_dvp); 5631541Srgrimesbad: 56465805Sbp if (dpunlocked) 56565805Sbp vrele(dp); 56665805Sbp else 56765805Sbp vput(dp); 5681541Srgrimes ndp->ni_vp = NULL; 5691541Srgrimes return (error); 5701541Srgrimes} 5711541Srgrimes 5723148Sphk/* 5733148Sphk * relookup - lookup a path name component 5743148Sphk * Used by lookup to re-aquire things. 5753148Sphk */ 5763148Sphkint 5773148Sphkrelookup(dvp, vpp, cnp) 5783148Sphk struct vnode *dvp, **vpp; 5793148Sphk struct componentname *cnp; 5803148Sphk{ 58122521Sdyson struct proc *p = cnp->cn_proc; 58222521Sdyson struct vnode *dp = 0; /* the directory we are searching */ 5833148Sphk int docache; /* == 0 do not cache last component */ 5843148Sphk int wantparent; /* 1 => wantparent or lockparent flag */ 5853148Sphk int rdonly; /* lookup read-only flag bit */ 5863148Sphk int error = 0; 5873148Sphk#ifdef NAMEI_DIAGNOSTIC 5883148Sphk int newhash; /* DEBUG: check name hash */ 5893148Sphk char *cp; /* DEBUG: check name ptr/len */ 5903148Sphk#endif 5911541Srgrimes 5923148Sphk /* 5933148Sphk * Setup: break out flag bits into variables. 5943148Sphk */ 5953148Sphk wantparent = cnp->cn_flags & (LOCKPARENT|WANTPARENT); 5963148Sphk docache = (cnp->cn_flags & NOCACHE) ^ NOCACHE; 5973148Sphk if (cnp->cn_nameiop == DELETE || 5983148Sphk (wantparent && cnp->cn_nameiop != CREATE)) 5993148Sphk docache = 0; 6003148Sphk rdonly = cnp->cn_flags & RDONLY; 6013148Sphk cnp->cn_flags &= ~ISSYMLINK; 6023148Sphk dp = dvp; 60322521Sdyson vn_lock(dp, LK_EXCLUSIVE | LK_RETRY, p); 6043148Sphk 6053148Sphk/* dirloop: */ 6063148Sphk /* 6073148Sphk * Search a new directory. 6083148Sphk * 6093148Sphk * The last component of the filename is left accessible via 6103148Sphk * cnp->cn_nameptr for callers that need the name. Callers needing 6113148Sphk * the name set the SAVENAME flag. When done, they assume 6123148Sphk * responsibility for freeing the pathname buffer. 6133148Sphk */ 6143148Sphk#ifdef NAMEI_DIAGNOSTIC 6153148Sphk if (cnp->cn_namelen != cp - cnp->cn_nameptr) 6163148Sphk panic ("relookup: bad len"); 6173148Sphk if (*cp != 0) 6183148Sphk panic("relookup: not last component"); 6193148Sphk printf("{%s}: ", cnp->cn_nameptr); 6203148Sphk#endif 6213148Sphk 6223148Sphk /* 6233148Sphk * Check for degenerate name (e.g. / or "") 6243148Sphk * which is a way of talking about a directory, 6253148Sphk * e.g. like "/." or ".". 6263148Sphk */ 6273148Sphk if (cnp->cn_nameptr[0] == '\0') { 6283148Sphk if (cnp->cn_nameiop != LOOKUP || wantparent) { 6293148Sphk error = EISDIR; 6303148Sphk goto bad; 6313148Sphk } 6323148Sphk if (dp->v_type != VDIR) { 6333148Sphk error = ENOTDIR; 6343148Sphk goto bad; 6353148Sphk } 6363148Sphk if (!(cnp->cn_flags & LOCKLEAF)) 63722521Sdyson VOP_UNLOCK(dp, 0, p); 6383148Sphk *vpp = dp; 63954655Seivind /* XXX This should probably move to the top of function. */ 6403148Sphk if (cnp->cn_flags & SAVESTART) 6413148Sphk panic("lookup: SAVESTART"); 6423148Sphk return (0); 6433148Sphk } 6443148Sphk 6453148Sphk if (cnp->cn_flags & ISDOTDOT) 6463148Sphk panic ("relookup: lookup on dot-dot"); 6473148Sphk 6483148Sphk /* 6493148Sphk * We now have a segment name to search for, and a directory to search. 6503148Sphk */ 65143311Sdillon if ((error = VOP_LOOKUP(dp, vpp, cnp)) != 0) { 65242408Seivind KASSERT(*vpp == NULL, ("leaf should be empty")); 6533148Sphk if (error != EJUSTRETURN) 6543148Sphk goto bad; 6553148Sphk /* 6563148Sphk * If creating and at end of pathname, then can consider 6573148Sphk * allowing file to be created. 6583148Sphk */ 65911644Sdg if (rdonly) { 6603148Sphk error = EROFS; 6613148Sphk goto bad; 6623148Sphk } 6633148Sphk /* ASSERT(dvp == ndp->ni_startdir) */ 6643148Sphk if (cnp->cn_flags & SAVESTART) 6653148Sphk VREF(dvp); 6663148Sphk /* 6673148Sphk * We return with ni_vp NULL to indicate that the entry 6683148Sphk * doesn't currently exist, leaving a pointer to the 6693148Sphk * (possibly locked) directory inode in ndp->ni_dvp. 6703148Sphk */ 6713148Sphk return (0); 6723148Sphk } 6733148Sphk dp = *vpp; 6743148Sphk 6753148Sphk /* 6763148Sphk * Check for symbolic link 6773148Sphk */ 67842408Seivind KASSERT(dp->v_type != VLNK || !(cnp->cn_flags & FOLLOW), 67942453Seivind ("relookup: symlink found.\n")); 6803148Sphk 6813148Sphk /* 68211644Sdg * Disallow directory write attempts on read-only file systems. 6833148Sphk */ 68411644Sdg if (rdonly && 68511644Sdg (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME)) { 68611644Sdg error = EROFS; 68711644Sdg goto bad2; 6883148Sphk } 6893148Sphk /* ASSERT(dvp == ndp->ni_startdir) */ 6903148Sphk if (cnp->cn_flags & SAVESTART) 6913148Sphk VREF(dvp); 69222521Sdyson 6933148Sphk if (!wantparent) 6943148Sphk vrele(dvp); 69532071Sdyson 69649101Salc if (vn_canvmio(dp) == TRUE && 69732286Sdyson ((cnp->cn_flags & (NOOBJ|LOCKLEAF)) == LOCKLEAF)) 69842315Seivind vfs_object_create(dp, cnp->cn_proc, cnp->cn_cred); 69932071Sdyson 7003148Sphk if ((cnp->cn_flags & LOCKLEAF) == 0) 70122521Sdyson VOP_UNLOCK(dp, 0, p); 7023148Sphk return (0); 7033148Sphk 7043148Sphkbad2: 7053148Sphk if ((cnp->cn_flags & LOCKPARENT) && (cnp->cn_flags & ISLASTCN)) 70622521Sdyson VOP_UNLOCK(dvp, 0, p); 7073148Sphk vrele(dvp); 7083148Sphkbad: 7093148Sphk vput(dp); 7103148Sphk *vpp = NULL; 7113148Sphk return (error); 7123148Sphk} 713