vfs_lookup.c revision 96755
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 96755 2002-05-16 21:28:32Z trhodes $ 401541Srgrimes */ 411541Srgrimes 4213203Swollman#include "opt_ktrace.h" 4313203Swollman 441541Srgrimes#include <sys/param.h> 452112Swollman#include <sys/systm.h> 4669664Speter#include <sys/kernel.h> 4776166Smarkm#include <sys/lock.h> 4889316Salfred#include <sys/mutex.h> 491541Srgrimes#include <sys/namei.h> 501541Srgrimes#include <sys/vnode.h> 511541Srgrimes#include <sys/mount.h> 521541Srgrimes#include <sys/filedesc.h> 531541Srgrimes#include <sys/proc.h> 541541Srgrimes#ifdef KTRACE 551541Srgrimes#include <sys/ktrace.h> 561541Srgrimes#endif 571541Srgrimes 5892751Sjeff#include <vm/uma.h> 5932011Sbde 601541Srgrimes/* 6169664Speter * Allocation zone for namei 6269664Speter */ 6392751Sjeffuma_zone_t namei_zone; 6469664Speter 6569664Speterstatic void 6669664Speternameiinit(void *dummy __unused) 6769664Speter{ 6892654Sjeff namei_zone = uma_zcreate("NAMEI", MAXPATHLEN, NULL, NULL, NULL, NULL, 6992654Sjeff UMA_ALIGN_PTR, 0); 7069664Speter 7169664Speter} 7269664SpeterSYSINIT(vfs, SI_SUB_VFS, SI_ORDER_SECOND, nameiinit, NULL) 7369664Speter 7469664Speter/* 751541Srgrimes * Convert a pathname into a pointer to a locked inode. 761541Srgrimes * 771541Srgrimes * The FOLLOW flag is set when symbolic links are to be followed 781541Srgrimes * when they occur at the end of the name translation process. 791541Srgrimes * Symbolic links are always followed for all other pathname 801541Srgrimes * components other than the last. 811541Srgrimes * 821541Srgrimes * The segflg defines whether the name is to be copied from user 831541Srgrimes * space or kernel space. 841541Srgrimes * 851541Srgrimes * Overall outline of namei: 861541Srgrimes * 871541Srgrimes * copy in name 881541Srgrimes * get starting directory 891541Srgrimes * while (!done && !error) { 901541Srgrimes * call lookup to search path. 911541Srgrimes * if symbolic link, massage name in buffer and continue 921541Srgrimes * } 931541Srgrimes */ 941541Srgrimesint 951541Srgrimesnamei(ndp) 961541Srgrimes register struct nameidata *ndp; 971541Srgrimes{ 981541Srgrimes register struct filedesc *fdp; /* pointer to file descriptor state */ 991541Srgrimes register char *cp; /* pointer into pathname argument */ 1001541Srgrimes register struct vnode *dp; /* the directory we are searching */ 1011541Srgrimes struct iovec aiov; /* uio for reading symbolic links */ 1021541Srgrimes struct uio auio; 1031541Srgrimes int error, linklen; 1041541Srgrimes struct componentname *cnp = &ndp->ni_cnd; 10583366Sjulian struct thread *td = cnp->cn_thread; 10683366Sjulian struct proc *p = td->td_proc; 1071541Srgrimes 10891419Sjhb ndp->ni_cnd.cn_cred = ndp->ni_cnd.cn_thread->td_ucred; 10983366Sjulian KASSERT(cnp->cn_cred && p, ("namei: bad cred/proc")); 11042408Seivind KASSERT((cnp->cn_nameiop & (~OPMASK)) == 0, 11142453Seivind ("namei: nameiop contaminated with flags")); 11242408Seivind KASSERT((cnp->cn_flags & OPMASK) == 0, 11342453Seivind ("namei: flags contaminated with nameiops")); 11483366Sjulian fdp = p->p_fd; 1151541Srgrimes 1161541Srgrimes /* 1171541Srgrimes * Get a buffer for the name to be translated, and copy the 1181541Srgrimes * name into the buffer. 1191541Srgrimes */ 1201541Srgrimes if ((cnp->cn_flags & HASBUF) == 0) 12192751Sjeff cnp->cn_pnbuf = uma_zalloc(namei_zone, M_WAITOK); 1221541Srgrimes if (ndp->ni_segflg == UIO_SYSSPACE) 1231541Srgrimes error = copystr(ndp->ni_dirp, cnp->cn_pnbuf, 12436735Sdfr MAXPATHLEN, (size_t *)&ndp->ni_pathlen); 1251541Srgrimes else 1261541Srgrimes error = copyinstr(ndp->ni_dirp, cnp->cn_pnbuf, 12736735Sdfr MAXPATHLEN, (size_t *)&ndp->ni_pathlen); 12820069Sbde 12920069Sbde /* 13020069Sbde * Don't allow empty pathnames. 13120069Sbde */ 13220069Sbde if (!error && *cnp->cn_pnbuf == '\0') 13320069Sbde error = ENOENT; 13420069Sbde 1351541Srgrimes if (error) { 13692751Sjeff uma_zfree(namei_zone, cnp->cn_pnbuf); 1371541Srgrimes ndp->ni_vp = NULL; 1381541Srgrimes return (error); 1391541Srgrimes } 1401541Srgrimes ndp->ni_loopcnt = 0; 1411541Srgrimes#ifdef KTRACE 14283366Sjulian if (KTRPOINT(p, KTR_NAMEI)) 14383366Sjulian ktrnamei(p->p_tracep, cnp->cn_pnbuf); 1441541Srgrimes#endif 1451541Srgrimes 1461541Srgrimes /* 1471541Srgrimes * Get starting point for the translation. 1481541Srgrimes */ 14989306Salfred FILEDESC_LOCK(fdp); 15033360Sdyson ndp->ni_rootdir = fdp->fd_rdir; 15151649Sphk ndp->ni_topdir = fdp->fd_jdir; 15233360Sdyson 1531541Srgrimes dp = fdp->fd_cdir; 1541541Srgrimes VREF(dp); 15589306Salfred FILEDESC_UNLOCK(fdp); 1561541Srgrimes for (;;) { 1571541Srgrimes /* 1581541Srgrimes * Check if root directory should replace current directory. 1591541Srgrimes * Done at start of translation and after symbolic link. 1601541Srgrimes */ 1611541Srgrimes cnp->cn_nameptr = cnp->cn_pnbuf; 1621541Srgrimes if (*(cnp->cn_nameptr) == '/') { 1631541Srgrimes vrele(dp); 1641541Srgrimes while (*(cnp->cn_nameptr) == '/') { 1651541Srgrimes cnp->cn_nameptr++; 1661541Srgrimes ndp->ni_pathlen--; 1671541Srgrimes } 1681541Srgrimes dp = ndp->ni_rootdir; 1691541Srgrimes VREF(dp); 1701541Srgrimes } 1711541Srgrimes ndp->ni_startdir = dp; 1723148Sphk error = lookup(ndp); 1733148Sphk if (error) { 17492751Sjeff uma_zfree(namei_zone, cnp->cn_pnbuf); 1751541Srgrimes return (error); 1761541Srgrimes } 1771541Srgrimes /* 1781541Srgrimes * Check for symbolic link 1791541Srgrimes */ 1801541Srgrimes if ((cnp->cn_flags & ISSYMLINK) == 0) { 1811541Srgrimes if ((cnp->cn_flags & (SAVENAME | SAVESTART)) == 0) 18292751Sjeff uma_zfree(namei_zone, cnp->cn_pnbuf); 1831541Srgrimes else 1841541Srgrimes cnp->cn_flags |= HASBUF; 18532286Sdyson 18649101Salc if (vn_canvmio(ndp->ni_vp) == TRUE && 18732286Sdyson (cnp->cn_nameiop != DELETE) && 18842315Seivind ((cnp->cn_flags & (NOOBJ|LOCKLEAF)) == 18942315Seivind LOCKLEAF)) 19083366Sjulian vfs_object_create(ndp->ni_vp, td, 19142315Seivind ndp->ni_cnd.cn_cred); 19232286Sdyson 1931541Srgrimes return (0); 1941541Srgrimes } 1951541Srgrimes if ((cnp->cn_flags & LOCKPARENT) && ndp->ni_pathlen == 1) 19683366Sjulian VOP_UNLOCK(ndp->ni_dvp, 0, td); 1971541Srgrimes if (ndp->ni_loopcnt++ >= MAXSYMLINKS) { 1981541Srgrimes error = ELOOP; 1991541Srgrimes break; 2001541Srgrimes } 2011541Srgrimes if (ndp->ni_pathlen > 1) 20292751Sjeff cp = uma_zalloc(namei_zone, M_WAITOK); 2031541Srgrimes else 2041541Srgrimes cp = cnp->cn_pnbuf; 2051541Srgrimes aiov.iov_base = cp; 2061541Srgrimes aiov.iov_len = MAXPATHLEN; 2071541Srgrimes auio.uio_iov = &aiov; 2081541Srgrimes auio.uio_iovcnt = 1; 2091541Srgrimes auio.uio_offset = 0; 2101541Srgrimes auio.uio_rw = UIO_READ; 2111541Srgrimes auio.uio_segflg = UIO_SYSSPACE; 21283366Sjulian auio.uio_td = (struct thread *)0; 2131541Srgrimes auio.uio_resid = MAXPATHLEN; 2143148Sphk error = VOP_READLINK(ndp->ni_vp, &auio, cnp->cn_cred); 2153148Sphk if (error) { 2161541Srgrimes if (ndp->ni_pathlen > 1) 21792751Sjeff uma_zfree(namei_zone, cp); 2181541Srgrimes break; 2191541Srgrimes } 2201541Srgrimes linklen = MAXPATHLEN - auio.uio_resid; 22178692Sdillon if (linklen == 0) { 22278692Sdillon if (ndp->ni_pathlen > 1) 22392751Sjeff uma_zfree(namei_zone, cp); 22478692Sdillon error = ENOENT; 22578692Sdillon break; 22678692Sdillon } 2271541Srgrimes if (linklen + ndp->ni_pathlen >= MAXPATHLEN) { 2281541Srgrimes if (ndp->ni_pathlen > 1) 22992751Sjeff uma_zfree(namei_zone, cp); 2301541Srgrimes error = ENAMETOOLONG; 2311541Srgrimes break; 2321541Srgrimes } 2331541Srgrimes if (ndp->ni_pathlen > 1) { 2341541Srgrimes bcopy(ndp->ni_next, cp + linklen, ndp->ni_pathlen); 23592751Sjeff uma_zfree(namei_zone, cnp->cn_pnbuf); 2361541Srgrimes cnp->cn_pnbuf = cp; 2371541Srgrimes } else 2381541Srgrimes cnp->cn_pnbuf[linklen] = '\0'; 2391541Srgrimes ndp->ni_pathlen += linklen; 2401541Srgrimes vput(ndp->ni_vp); 2411541Srgrimes dp = ndp->ni_dvp; 2421541Srgrimes } 24392751Sjeff uma_zfree(namei_zone, cnp->cn_pnbuf); 2441541Srgrimes vrele(ndp->ni_dvp); 2451541Srgrimes vput(ndp->ni_vp); 2461541Srgrimes ndp->ni_vp = NULL; 2471541Srgrimes return (error); 2481541Srgrimes} 2491541Srgrimes 2501541Srgrimes/* 2511541Srgrimes * Search a pathname. 2521541Srgrimes * This is a very central and rather complicated routine. 2531541Srgrimes * 2541541Srgrimes * The pathname is pointed to by ni_ptr and is of length ni_pathlen. 2551541Srgrimes * The starting directory is taken from ni_startdir. The pathname is 2561541Srgrimes * descended until done, or a symbolic link is encountered. The variable 2571541Srgrimes * ni_more is clear if the path is completed; it is set to one if a 2581541Srgrimes * symbolic link needing interpretation is encountered. 2591541Srgrimes * 2601541Srgrimes * The flag argument is LOOKUP, CREATE, RENAME, or DELETE depending on 2611541Srgrimes * whether the name is to be looked up, created, renamed, or deleted. 2621541Srgrimes * When CREATE, RENAME, or DELETE is specified, information usable in 2631541Srgrimes * creating, renaming, or deleting a directory entry may be calculated. 2641541Srgrimes * If flag has LOCKPARENT or'ed into it, the parent directory is returned 2651541Srgrimes * locked. If flag has WANTPARENT or'ed into it, the parent directory is 2661541Srgrimes * returned unlocked. Otherwise the parent directory is not returned. If 2671541Srgrimes * the target of the pathname exists and LOCKLEAF is or'ed into the flag 2681541Srgrimes * the target is returned locked, otherwise it is returned unlocked. 2691541Srgrimes * When creating or renaming and LOCKPARENT is specified, the target may not 2701541Srgrimes * be ".". When deleting and LOCKPARENT is specified, the target may be ".". 2718876Srgrimes * 2721541Srgrimes * Overall outline of lookup: 2731541Srgrimes * 2741541Srgrimes * dirloop: 2751541Srgrimes * identify next component of name at ndp->ni_ptr 2761541Srgrimes * handle degenerate case where name is null string 2771541Srgrimes * if .. and crossing mount points and on mounted filesys, find parent 2781541Srgrimes * call VOP_LOOKUP routine for next component name 2791541Srgrimes * directory vnode returned in ni_dvp, unlocked unless LOCKPARENT set 2801541Srgrimes * component vnode returned in ni_vp (if it exists), locked. 2811541Srgrimes * if result vnode is mounted on and crossing mount points, 2821541Srgrimes * find mounted on vnode 2831541Srgrimes * if more components of name, do next level at dirloop 2841541Srgrimes * return the answer in ni_vp, locked if LOCKLEAF set 2851541Srgrimes * if LOCKPARENT set, return locked parent in ni_dvp 2861541Srgrimes * if WANTPARENT set, return unlocked parent in ni_dvp 2871541Srgrimes */ 2881541Srgrimesint 2891541Srgrimeslookup(ndp) 2901541Srgrimes register struct nameidata *ndp; 2911541Srgrimes{ 2921541Srgrimes register char *cp; /* pointer into pathname argument */ 2931541Srgrimes register struct vnode *dp = 0; /* the directory we are searching */ 2941541Srgrimes struct vnode *tdp; /* saved dp */ 2951541Srgrimes struct mount *mp; /* mount table entry */ 2961541Srgrimes int docache; /* == 0 do not cache last component */ 2971541Srgrimes int wantparent; /* 1 => wantparent or lockparent flag */ 2981541Srgrimes int rdonly; /* lookup read-only flag bit */ 2999804Sbde int trailing_slash; 3001541Srgrimes int error = 0; 30165805Sbp int dpunlocked = 0; /* dp has already been unlocked */ 3021541Srgrimes struct componentname *cnp = &ndp->ni_cnd; 30383366Sjulian struct thread *td = cnp->cn_thread; 3041541Srgrimes 3051541Srgrimes /* 3061541Srgrimes * Setup: break out flag bits into variables. 3071541Srgrimes */ 3081541Srgrimes wantparent = cnp->cn_flags & (LOCKPARENT | WANTPARENT); 3091541Srgrimes docache = (cnp->cn_flags & NOCACHE) ^ NOCACHE; 3101541Srgrimes if (cnp->cn_nameiop == DELETE || 31122874Sbde (wantparent && cnp->cn_nameiop != CREATE && 31222874Sbde cnp->cn_nameiop != LOOKUP)) 3131541Srgrimes docache = 0; 3141541Srgrimes rdonly = cnp->cn_flags & RDONLY; 3151541Srgrimes ndp->ni_dvp = NULL; 3161541Srgrimes cnp->cn_flags &= ~ISSYMLINK; 3171541Srgrimes dp = ndp->ni_startdir; 3181541Srgrimes ndp->ni_startdir = NULLVP; 31983366Sjulian vn_lock(dp, LK_EXCLUSIVE | LK_RETRY, td); 3201541Srgrimes 3211541Srgrimesdirloop: 3221541Srgrimes /* 3231541Srgrimes * Search a new directory. 3241541Srgrimes * 3251541Srgrimes * The last component of the filename is left accessible via 3261541Srgrimes * cnp->cn_nameptr for callers that need the name. Callers needing 3271541Srgrimes * the name set the SAVENAME flag. When done, they assume 3281541Srgrimes * responsibility for freeing the pathname buffer. 3291541Srgrimes */ 3301541Srgrimes cnp->cn_consume = 0; 3311541Srgrimes for (cp = cnp->cn_nameptr; *cp != 0 && *cp != '/'; cp++) 33251906Sphk continue; 3331541Srgrimes cnp->cn_namelen = cp - cnp->cn_nameptr; 3341541Srgrimes if (cnp->cn_namelen > NAME_MAX) { 3351541Srgrimes error = ENAMETOOLONG; 3361541Srgrimes goto bad; 3371541Srgrimes } 3381541Srgrimes#ifdef NAMEI_DIAGNOSTIC 3391541Srgrimes { char c = *cp; 3401541Srgrimes *cp = '\0'; 3411541Srgrimes printf("{%s}: ", cnp->cn_nameptr); 3421541Srgrimes *cp = c; } 3431541Srgrimes#endif 3441541Srgrimes ndp->ni_pathlen -= cnp->cn_namelen; 3451541Srgrimes ndp->ni_next = cp; 3469804Sbde 3479804Sbde /* 3489804Sbde * Replace multiple slashes by a single slash and trailing slashes 3499804Sbde * by a null. This must be done before VOP_LOOKUP() because some 3509804Sbde * fs's don't know about trailing slashes. Remember if there were 3519804Sbde * trailing slashes to handle symlinks, existing non-directories 3529804Sbde * and non-existing files that won't be directories specially later. 3539804Sbde */ 3549804Sbde trailing_slash = 0; 3559804Sbde while (*cp == '/' && (cp[1] == '/' || cp[1] == '\0')) { 3569804Sbde cp++; 3579804Sbde ndp->ni_pathlen--; 3589804Sbde if (*cp == '\0') { 3599804Sbde trailing_slash = 1; 3609804Sbde *ndp->ni_next = '\0'; /* XXX for direnter() ... */ 3619804Sbde } 3629804Sbde } 3639804Sbde ndp->ni_next = cp; 3649804Sbde 3651541Srgrimes cnp->cn_flags |= MAKEENTRY; 3661541Srgrimes if (*cp == '\0' && docache == 0) 3671541Srgrimes cnp->cn_flags &= ~MAKEENTRY; 3681541Srgrimes if (cnp->cn_namelen == 2 && 3691541Srgrimes cnp->cn_nameptr[1] == '.' && cnp->cn_nameptr[0] == '.') 3701541Srgrimes cnp->cn_flags |= ISDOTDOT; 3711541Srgrimes else 3721541Srgrimes cnp->cn_flags &= ~ISDOTDOT; 3731541Srgrimes if (*ndp->ni_next == 0) 3741541Srgrimes cnp->cn_flags |= ISLASTCN; 3751541Srgrimes else 3761541Srgrimes cnp->cn_flags &= ~ISLASTCN; 3771541Srgrimes 3781541Srgrimes 3791541Srgrimes /* 3801541Srgrimes * Check for degenerate name (e.g. / or "") 3811541Srgrimes * which is a way of talking about a directory, 3821541Srgrimes * e.g. like "/." or ".". 3831541Srgrimes */ 3841541Srgrimes if (cnp->cn_nameptr[0] == '\0') { 38522521Sdyson if (dp->v_type != VDIR) { 38622521Sdyson error = ENOTDIR; 38722521Sdyson goto bad; 38822521Sdyson } 3891541Srgrimes if (cnp->cn_nameiop != LOOKUP) { 3901541Srgrimes error = EISDIR; 3911541Srgrimes goto bad; 3921541Srgrimes } 3931541Srgrimes if (wantparent) { 3941541Srgrimes ndp->ni_dvp = dp; 3951541Srgrimes VREF(dp); 3961541Srgrimes } 3971541Srgrimes ndp->ni_vp = dp; 3981541Srgrimes if (!(cnp->cn_flags & (LOCKPARENT | LOCKLEAF))) 39983366Sjulian VOP_UNLOCK(dp, 0, td); 40054655Seivind /* XXX This should probably move to the top of function. */ 4011541Srgrimes if (cnp->cn_flags & SAVESTART) 4021541Srgrimes panic("lookup: SAVESTART"); 4031541Srgrimes return (0); 4041541Srgrimes } 4051541Srgrimes 4061541Srgrimes /* 4071541Srgrimes * Handle "..": two special cases. 4081541Srgrimes * 1. If at root directory (e.g. after chroot) 4091541Srgrimes * or at absolute root directory 4101541Srgrimes * then ignore it so can't get out. 4111541Srgrimes * 2. If this vnode is the root of a mounted 4121541Srgrimes * filesystem, then replace it with the 4131541Srgrimes * vnode which was mounted on so we take the 41496755Strhodes * .. in the other filesystem. 41551649Sphk * 3. If the vnode is the top directory of 41651649Sphk * the jail or chroot, don't let them out. 4171541Srgrimes */ 4181541Srgrimes if (cnp->cn_flags & ISDOTDOT) { 4191541Srgrimes for (;;) { 42051649Sphk if (dp == ndp->ni_rootdir || 42151649Sphk dp == ndp->ni_topdir || 42251649Sphk dp == rootvnode) { 4231541Srgrimes ndp->ni_dvp = dp; 4241541Srgrimes ndp->ni_vp = dp; 4251541Srgrimes VREF(dp); 4261541Srgrimes goto nextname; 4271541Srgrimes } 4281541Srgrimes if ((dp->v_flag & VROOT) == 0 || 4291541Srgrimes (cnp->cn_flags & NOCROSSMOUNT)) 4301541Srgrimes break; 43169405Salfred if (dp->v_mount == NULL) { /* forced unmount */ 43269405Salfred error = EBADF; 43369405Salfred goto bad; 43469405Salfred } 4351541Srgrimes tdp = dp; 4361541Srgrimes dp = dp->v_mount->mnt_vnodecovered; 4371541Srgrimes vput(tdp); 4381541Srgrimes VREF(dp); 43983366Sjulian vn_lock(dp, LK_EXCLUSIVE | LK_RETRY, td); 4401541Srgrimes } 4411541Srgrimes } 4421541Srgrimes 4431541Srgrimes /* 4441541Srgrimes * We now have a segment name to search for, and a directory to search. 4451541Srgrimes */ 4461541Srgrimesunionlookup: 4471541Srgrimes ndp->ni_dvp = dp; 44822521Sdyson ndp->ni_vp = NULL; 44965973Sbp cnp->cn_flags &= ~PDIRUNLOCK; 45024624Sdfr ASSERT_VOP_LOCKED(dp, "lookup"); 45143301Sdillon if ((error = VOP_LOOKUP(dp, &ndp->ni_vp, cnp)) != 0) { 45242408Seivind KASSERT(ndp->ni_vp == NULL, ("leaf should be empty")); 4531541Srgrimes#ifdef NAMEI_DIAGNOSTIC 4541541Srgrimes printf("not found\n"); 4551541Srgrimes#endif 4561541Srgrimes if ((error == ENOENT) && 45769405Salfred (dp->v_flag & VROOT) && (dp->v_mount != NULL) && 4581541Srgrimes (dp->v_mount->mnt_flag & MNT_UNION)) { 4591541Srgrimes tdp = dp; 4601541Srgrimes dp = dp->v_mount->mnt_vnodecovered; 46165973Sbp if (cnp->cn_flags & PDIRUNLOCK) 46265973Sbp vrele(tdp); 46365973Sbp else 46465973Sbp vput(tdp); 4651541Srgrimes VREF(dp); 46683366Sjulian vn_lock(dp, LK_EXCLUSIVE | LK_RETRY, td); 4671541Srgrimes goto unionlookup; 4681541Srgrimes } 4691541Srgrimes 4701541Srgrimes if (error != EJUSTRETURN) 4711541Srgrimes goto bad; 4721541Srgrimes /* 4731541Srgrimes * If creating and at end of pathname, then can consider 4741541Srgrimes * allowing file to be created. 4751541Srgrimes */ 47611644Sdg if (rdonly) { 4771541Srgrimes error = EROFS; 4781541Srgrimes goto bad; 4791541Srgrimes } 4809804Sbde if (*cp == '\0' && trailing_slash && 4819804Sbde !(cnp->cn_flags & WILLBEDIR)) { 4829804Sbde error = ENOENT; 4839804Sbde goto bad; 4849804Sbde } 4851541Srgrimes /* 4861541Srgrimes * We return with ni_vp NULL to indicate that the entry 4871541Srgrimes * doesn't currently exist, leaving a pointer to the 4881541Srgrimes * (possibly locked) directory inode in ndp->ni_dvp. 4891541Srgrimes */ 4901541Srgrimes if (cnp->cn_flags & SAVESTART) { 4911541Srgrimes ndp->ni_startdir = ndp->ni_dvp; 4921541Srgrimes VREF(ndp->ni_startdir); 4931541Srgrimes } 4941541Srgrimes return (0); 4951541Srgrimes } 4961541Srgrimes#ifdef NAMEI_DIAGNOSTIC 4971541Srgrimes printf("found\n"); 4981541Srgrimes#endif 4991541Srgrimes 50024624Sdfr ASSERT_VOP_LOCKED(ndp->ni_vp, "lookup"); 50124624Sdfr 5021541Srgrimes /* 5031541Srgrimes * Take into account any additional components consumed by 5041541Srgrimes * the underlying filesystem. 5051541Srgrimes */ 5061541Srgrimes if (cnp->cn_consume > 0) { 5071541Srgrimes cnp->cn_nameptr += cnp->cn_consume; 5081541Srgrimes ndp->ni_next += cnp->cn_consume; 5091541Srgrimes ndp->ni_pathlen -= cnp->cn_consume; 5101541Srgrimes cnp->cn_consume = 0; 5111541Srgrimes } 5121541Srgrimes 5131541Srgrimes dp = ndp->ni_vp; 5141541Srgrimes 5151541Srgrimes /* 5161541Srgrimes * Check to see if the vnode has been mounted on; 51796755Strhodes * if so find the root of the mounted filesystem. 5181541Srgrimes */ 5191541Srgrimes while (dp->v_type == VDIR && (mp = dp->v_mountedhere) && 5201541Srgrimes (cnp->cn_flags & NOCROSSMOUNT) == 0) { 52183366Sjulian if (vfs_busy(mp, 0, 0, td)) 5221541Srgrimes continue; 52383366Sjulian VOP_UNLOCK(dp, 0, td); 52422521Sdyson error = VFS_ROOT(mp, &tdp); 52583366Sjulian vfs_unbusy(mp, td); 52665805Sbp if (error) { 52765805Sbp dpunlocked = 1; 5281541Srgrimes goto bad2; 52965805Sbp } 53065805Sbp vrele(dp); 5311541Srgrimes ndp->ni_vp = dp = tdp; 5321541Srgrimes } 5331541Srgrimes 53410219Sdfr /* 53510219Sdfr * Check for symbolic link 53610219Sdfr */ 53710219Sdfr if ((dp->v_type == VLNK) && 53810219Sdfr ((cnp->cn_flags & FOLLOW) || trailing_slash || 53910219Sdfr *ndp->ni_next == '/')) { 54010219Sdfr cnp->cn_flags |= ISSYMLINK; 54169405Salfred if (dp->v_mount == NULL) { 54269405Salfred /* We can't know whether the directory was mounted with 54369405Salfred * NOSYMFOLLOW, so we can't follow safely. */ 54469405Salfred error = EBADF; 54569405Salfred goto bad2; 54669405Salfred } 54735105Swosch if (dp->v_mount->mnt_flag & MNT_NOSYMFOLLOW) { 54835105Swosch error = EACCES; 54935105Swosch goto bad2; 55035105Swosch } 55110219Sdfr return (0); 55210219Sdfr } 55310219Sdfr 55410219Sdfr /* 55510219Sdfr * Check for bogus trailing slashes. 55610219Sdfr */ 55710219Sdfr if (trailing_slash && dp->v_type != VDIR) { 55810219Sdfr error = ENOTDIR; 55910219Sdfr goto bad2; 56010219Sdfr } 56110219Sdfr 5621541Srgrimesnextname: 5631541Srgrimes /* 5641541Srgrimes * Not a symbolic link. If more pathname, 5651541Srgrimes * continue at next component, else return. 5661541Srgrimes */ 5671541Srgrimes if (*ndp->ni_next == '/') { 5681541Srgrimes cnp->cn_nameptr = ndp->ni_next; 5691541Srgrimes while (*cnp->cn_nameptr == '/') { 5701541Srgrimes cnp->cn_nameptr++; 5711541Srgrimes ndp->ni_pathlen--; 5721541Srgrimes } 57354655Seivind if (ndp->ni_dvp != ndp->ni_vp) 57454655Seivind ASSERT_VOP_UNLOCKED(ndp->ni_dvp, "lookup"); 5751541Srgrimes vrele(ndp->ni_dvp); 5761541Srgrimes goto dirloop; 5771541Srgrimes } 5781541Srgrimes /* 57996755Strhodes * Disallow directory write attempts on read-only filesystems. 5801541Srgrimes */ 58111644Sdg if (rdonly && 58211644Sdg (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME)) { 58311644Sdg error = EROFS; 58411644Sdg goto bad2; 5851541Srgrimes } 5861541Srgrimes if (cnp->cn_flags & SAVESTART) { 5871541Srgrimes ndp->ni_startdir = ndp->ni_dvp; 5881541Srgrimes VREF(ndp->ni_startdir); 5891541Srgrimes } 5901541Srgrimes if (!wantparent) 5911541Srgrimes vrele(ndp->ni_dvp); 59232071Sdyson 5931541Srgrimes if ((cnp->cn_flags & LOCKLEAF) == 0) 59483366Sjulian VOP_UNLOCK(dp, 0, td); 5951541Srgrimes return (0); 5961541Srgrimes 5971541Srgrimesbad2: 59865973Sbp if ((cnp->cn_flags & (LOCKPARENT | PDIRUNLOCK)) == LOCKPARENT && 59965973Sbp *ndp->ni_next == '\0') 60083366Sjulian VOP_UNLOCK(ndp->ni_dvp, 0, td); 6011541Srgrimes vrele(ndp->ni_dvp); 6021541Srgrimesbad: 60365805Sbp if (dpunlocked) 60465805Sbp vrele(dp); 60565805Sbp else 60665805Sbp vput(dp); 6071541Srgrimes ndp->ni_vp = NULL; 6081541Srgrimes return (error); 6091541Srgrimes} 6101541Srgrimes 6113148Sphk/* 6123148Sphk * relookup - lookup a path name component 6133148Sphk * Used by lookup to re-aquire things. 6143148Sphk */ 6153148Sphkint 6163148Sphkrelookup(dvp, vpp, cnp) 6173148Sphk struct vnode *dvp, **vpp; 6183148Sphk struct componentname *cnp; 6193148Sphk{ 62083366Sjulian struct thread *td = cnp->cn_thread; 62122521Sdyson struct vnode *dp = 0; /* the directory we are searching */ 6223148Sphk int docache; /* == 0 do not cache last component */ 6233148Sphk int wantparent; /* 1 => wantparent or lockparent flag */ 6243148Sphk int rdonly; /* lookup read-only flag bit */ 6253148Sphk int error = 0; 6263148Sphk#ifdef NAMEI_DIAGNOSTIC 6273148Sphk int newhash; /* DEBUG: check name hash */ 6283148Sphk char *cp; /* DEBUG: check name ptr/len */ 6293148Sphk#endif 6301541Srgrimes 6313148Sphk /* 6323148Sphk * Setup: break out flag bits into variables. 6333148Sphk */ 6343148Sphk wantparent = cnp->cn_flags & (LOCKPARENT|WANTPARENT); 6353148Sphk docache = (cnp->cn_flags & NOCACHE) ^ NOCACHE; 6363148Sphk if (cnp->cn_nameiop == DELETE || 6373148Sphk (wantparent && cnp->cn_nameiop != CREATE)) 6383148Sphk docache = 0; 6393148Sphk rdonly = cnp->cn_flags & RDONLY; 6403148Sphk cnp->cn_flags &= ~ISSYMLINK; 6413148Sphk dp = dvp; 64283366Sjulian vn_lock(dp, LK_EXCLUSIVE | LK_RETRY, td); 6433148Sphk 6443148Sphk/* dirloop: */ 6453148Sphk /* 6463148Sphk * Search a new directory. 6473148Sphk * 6483148Sphk * The last component of the filename is left accessible via 6493148Sphk * cnp->cn_nameptr for callers that need the name. Callers needing 6503148Sphk * the name set the SAVENAME flag. When done, they assume 6513148Sphk * responsibility for freeing the pathname buffer. 6523148Sphk */ 6533148Sphk#ifdef NAMEI_DIAGNOSTIC 6543148Sphk if (cnp->cn_namelen != cp - cnp->cn_nameptr) 6553148Sphk panic ("relookup: bad len"); 6563148Sphk if (*cp != 0) 6573148Sphk panic("relookup: not last component"); 6583148Sphk printf("{%s}: ", cnp->cn_nameptr); 6593148Sphk#endif 6603148Sphk 6613148Sphk /* 6623148Sphk * Check for degenerate name (e.g. / or "") 6633148Sphk * which is a way of talking about a directory, 6643148Sphk * e.g. like "/." or ".". 6653148Sphk */ 6663148Sphk if (cnp->cn_nameptr[0] == '\0') { 6673148Sphk if (cnp->cn_nameiop != LOOKUP || wantparent) { 6683148Sphk error = EISDIR; 6693148Sphk goto bad; 6703148Sphk } 6713148Sphk if (dp->v_type != VDIR) { 6723148Sphk error = ENOTDIR; 6733148Sphk goto bad; 6743148Sphk } 6753148Sphk if (!(cnp->cn_flags & LOCKLEAF)) 67683366Sjulian VOP_UNLOCK(dp, 0, td); 6773148Sphk *vpp = dp; 67854655Seivind /* XXX This should probably move to the top of function. */ 6793148Sphk if (cnp->cn_flags & SAVESTART) 6803148Sphk panic("lookup: SAVESTART"); 6813148Sphk return (0); 6823148Sphk } 6833148Sphk 6843148Sphk if (cnp->cn_flags & ISDOTDOT) 6853148Sphk panic ("relookup: lookup on dot-dot"); 6863148Sphk 6873148Sphk /* 6883148Sphk * We now have a segment name to search for, and a directory to search. 6893148Sphk */ 69043311Sdillon if ((error = VOP_LOOKUP(dp, vpp, cnp)) != 0) { 69142408Seivind KASSERT(*vpp == NULL, ("leaf should be empty")); 6923148Sphk if (error != EJUSTRETURN) 6933148Sphk goto bad; 6943148Sphk /* 6953148Sphk * If creating and at end of pathname, then can consider 6963148Sphk * allowing file to be created. 6973148Sphk */ 69811644Sdg if (rdonly) { 6993148Sphk error = EROFS; 7003148Sphk goto bad; 7013148Sphk } 7023148Sphk /* ASSERT(dvp == ndp->ni_startdir) */ 7033148Sphk if (cnp->cn_flags & SAVESTART) 7043148Sphk VREF(dvp); 7053148Sphk /* 7063148Sphk * We return with ni_vp NULL to indicate that the entry 7073148Sphk * doesn't currently exist, leaving a pointer to the 7083148Sphk * (possibly locked) directory inode in ndp->ni_dvp. 7093148Sphk */ 7103148Sphk return (0); 7113148Sphk } 7123148Sphk dp = *vpp; 7133148Sphk 7143148Sphk /* 7153148Sphk * Check for symbolic link 7163148Sphk */ 71742408Seivind KASSERT(dp->v_type != VLNK || !(cnp->cn_flags & FOLLOW), 71842453Seivind ("relookup: symlink found.\n")); 7193148Sphk 7203148Sphk /* 72196755Strhodes * Disallow directory write attempts on read-only filesystems. 7223148Sphk */ 72311644Sdg if (rdonly && 72411644Sdg (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME)) { 72511644Sdg error = EROFS; 72611644Sdg goto bad2; 7273148Sphk } 7283148Sphk /* ASSERT(dvp == ndp->ni_startdir) */ 7293148Sphk if (cnp->cn_flags & SAVESTART) 7303148Sphk VREF(dvp); 73122521Sdyson 7323148Sphk if (!wantparent) 7333148Sphk vrele(dvp); 73432071Sdyson 73549101Salc if (vn_canvmio(dp) == TRUE && 73632286Sdyson ((cnp->cn_flags & (NOOBJ|LOCKLEAF)) == LOCKLEAF)) 73783366Sjulian vfs_object_create(dp, td, cnp->cn_cred); 73832071Sdyson 7393148Sphk if ((cnp->cn_flags & LOCKLEAF) == 0) 74083366Sjulian VOP_UNLOCK(dp, 0, td); 7413148Sphk return (0); 7423148Sphk 7433148Sphkbad2: 7443148Sphk if ((cnp->cn_flags & LOCKPARENT) && (cnp->cn_flags & ISLASTCN)) 74583366Sjulian VOP_UNLOCK(dvp, 0, td); 7463148Sphk vrele(dvp); 7473148Sphkbad: 7483148Sphk vput(dp); 7493148Sphk *vpp = NULL; 7503148Sphk return (error); 7513148Sphk} 752