1139804Simp/*- 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 * 4. Neither the name of the University nor the names of its contributors 191541Srgrimes * may be used to endorse or promote products derived from this software 201541Srgrimes * without specific prior written permission. 211541Srgrimes * 221541Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 231541Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 241541Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 251541Srgrimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 261541Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 271541Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 281541Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 291541Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 301541Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 311541Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 321541Srgrimes * SUCH DAMAGE. 331541Srgrimes * 341541Srgrimes * @(#)vfs_lookup.c 8.4 (Berkeley) 2/16/94 351541Srgrimes */ 361541Srgrimes 37116182Sobrien#include <sys/cdefs.h> 38116182Sobrien__FBSDID("$FreeBSD: stable/10/sys/kern/vfs_lookup.c 308469 2016-11-09 17:07:45Z kib $"); 39116182Sobrien 40224778Srwatson#include "opt_capsicum.h" 41190759Srwatson#include "opt_kdtrace.h" 4213203Swollman#include "opt_ktrace.h" 4313203Swollman 441541Srgrimes#include <sys/param.h> 452112Swollman#include <sys/systm.h> 4669664Speter#include <sys/kernel.h> 47280258Srwatson#include <sys/capsicum.h> 48177785Skib#include <sys/fcntl.h> 49192895Sjamie#include <sys/jail.h> 5076166Smarkm#include <sys/lock.h> 5189316Salfred#include <sys/mutex.h> 521541Srgrimes#include <sys/namei.h> 531541Srgrimes#include <sys/vnode.h> 541541Srgrimes#include <sys/mount.h> 551541Srgrimes#include <sys/filedesc.h> 561541Srgrimes#include <sys/proc.h> 57190759Srwatson#include <sys/sdt.h> 58141471Sjhb#include <sys/syscallsubr.h> 59144613Sjeff#include <sys/sysctl.h> 601541Srgrimes#ifdef KTRACE 611541Srgrimes#include <sys/ktrace.h> 621541Srgrimes#endif 631541Srgrimes 64155334Srwatson#include <security/audit/audit.h> 65163606Srwatson#include <security/mac/mac_framework.h> 66155334Srwatson 6792751Sjeff#include <vm/uma.h> 6832011Sbde 69155168Sjeff#define NAMEI_DIAGNOSTIC 1 70138345Sphk#undef NAMEI_DIAGNOSTIC 71138345Sphk 72190759SrwatsonSDT_PROVIDER_DECLARE(vfs); 73260817SavgSDT_PROBE_DEFINE3(vfs, namei, lookup, entry, "struct vnode *", "char *", 74190759Srwatson "unsigned long"); 75260817SavgSDT_PROBE_DEFINE2(vfs, namei, lookup, return, "int", "struct vnode *"); 76190759Srwatson 771541Srgrimes/* 7869664Speter * Allocation zone for namei 7969664Speter */ 8092751Sjeffuma_zone_t namei_zone; 81166167Skib/* 82166167Skib * Placeholder vnode for mp traversal 83166167Skib */ 84166167Skibstatic struct vnode *vp_crossmp; 8569664Speter 8669664Speterstatic void 8769664Speternameiinit(void *dummy __unused) 8869664Speter{ 89168138Srwatson 9092654Sjeff namei_zone = uma_zcreate("NAMEI", MAXPATHLEN, NULL, NULL, NULL, NULL, 9192654Sjeff UMA_ALIGN_PTR, 0); 92211531Sjhb getnewvnode("crossmp", NULL, &dead_vnodeops, &vp_crossmp); 93211531Sjhb vn_lock(vp_crossmp, LK_EXCLUSIVE); 94176519Sattilio VN_LOCK_ASHARE(vp_crossmp); 95211531Sjhb VOP_UNLOCK(vp_crossmp, 0); 9669664Speter} 97177253SrwatsonSYSINIT(vfs, SI_SUB_VFS, SI_ORDER_SECOND, nameiinit, NULL); 9869664Speter 99183520Sjhbstatic int lookup_shared = 1; 100144613SjeffSYSCTL_INT(_vfs, OID_AUTO, lookup_shared, CTLFLAG_RW, &lookup_shared, 0, 101144613Sjeff "Enables/Disables shared locks for path name translation"); 102183519SjhbTUNABLE_INT("vfs.lookup_shared", &lookup_shared); 103144613Sjeff 10469664Speter/* 105161010Srwatson * Convert a pathname into a pointer to a locked vnode. 1061541Srgrimes * 1071541Srgrimes * The FOLLOW flag is set when symbolic links are to be followed 1081541Srgrimes * when they occur at the end of the name translation process. 1091541Srgrimes * Symbolic links are always followed for all other pathname 1101541Srgrimes * components other than the last. 1111541Srgrimes * 1121541Srgrimes * The segflg defines whether the name is to be copied from user 1131541Srgrimes * space or kernel space. 1141541Srgrimes * 1151541Srgrimes * Overall outline of namei: 1161541Srgrimes * 1171541Srgrimes * copy in name 1181541Srgrimes * get starting directory 1191541Srgrimes * while (!done && !error) { 1201541Srgrimes * call lookup to search path. 1211541Srgrimes * if symbolic link, massage name in buffer and continue 1221541Srgrimes * } 1231541Srgrimes */ 124273411Sdelphijstatic void 125273411Sdelphijnamei_cleanup_cnp(struct componentname *cnp) 126273411Sdelphij{ 127273411Sdelphij uma_zfree(namei_zone, cnp->cn_pnbuf); 128273411Sdelphij#ifdef DIAGNOSTIC 129273411Sdelphij cnp->cn_pnbuf = NULL; 130273411Sdelphij cnp->cn_nameptr = NULL; 131273411Sdelphij#endif 132273411Sdelphij} 133273411Sdelphij 1341541Srgrimesint 135161011Srwatsonnamei(struct nameidata *ndp) 1361541Srgrimes{ 137161011Srwatson struct filedesc *fdp; /* pointer to file descriptor state */ 138161011Srwatson char *cp; /* pointer into pathname argument */ 139161011Srwatson struct vnode *dp; /* the directory we are searching */ 1401541Srgrimes struct iovec aiov; /* uio for reading symbolic links */ 1411541Srgrimes struct uio auio; 1421541Srgrimes int error, linklen; 1431541Srgrimes struct componentname *cnp = &ndp->ni_cnd; 14483366Sjulian struct thread *td = cnp->cn_thread; 14583366Sjulian struct proc *p = td->td_proc; 1461541Srgrimes 14791419Sjhb ndp->ni_cnd.cn_cred = ndp->ni_cnd.cn_thread->td_ucred; 14883366Sjulian KASSERT(cnp->cn_cred && p, ("namei: bad cred/proc")); 14942408Seivind KASSERT((cnp->cn_nameiop & (~OPMASK)) == 0, 15042453Seivind ("namei: nameiop contaminated with flags")); 15142408Seivind KASSERT((cnp->cn_flags & OPMASK) == 0, 15242453Seivind ("namei: flags contaminated with nameiops")); 153144613Sjeff if (!lookup_shared) 154144613Sjeff cnp->cn_flags &= ~LOCKSHARED; 15583366Sjulian fdp = p->p_fd; 1561541Srgrimes 157193028Sdes /* We will set this ourselves if we need it. */ 158193028Sdes cnp->cn_flags &= ~TRAILINGSLASH; 159193028Sdes 1601541Srgrimes /* 1611541Srgrimes * Get a buffer for the name to be translated, and copy the 1621541Srgrimes * name into the buffer. 1631541Srgrimes */ 1641541Srgrimes if ((cnp->cn_flags & HASBUF) == 0) 165111119Simp cnp->cn_pnbuf = uma_zalloc(namei_zone, M_WAITOK); 1661541Srgrimes if (ndp->ni_segflg == UIO_SYSSPACE) 167308469Skib error = copystr(ndp->ni_dirp, cnp->cn_pnbuf, MAXPATHLEN, 168308469Skib &ndp->ni_pathlen); 1691541Srgrimes else 170308469Skib error = copyinstr(ndp->ni_dirp, cnp->cn_pnbuf, MAXPATHLEN, 171308469Skib &ndp->ni_pathlen); 17220069Sbde 17320069Sbde /* 17420069Sbde * Don't allow empty pathnames. 17520069Sbde */ 17620069Sbde if (!error && *cnp->cn_pnbuf == '\0') 17720069Sbde error = ENOENT; 17820069Sbde 179224810Sjonathan#ifdef CAPABILITY_MODE 180224810Sjonathan /* 181224810Sjonathan * In capability mode, lookups must be "strictly relative" (i.e. 182224810Sjonathan * not an absolute path, and not containing '..' components) to 183224810Sjonathan * a real file descriptor, not the pseudo-descriptor AT_FDCWD. 184224810Sjonathan */ 185253969Skib if (error == 0 && IN_CAPABILITY_MODE(td) && 186253969Skib (cnp->cn_flags & NOCAPCHECK) == 0) { 187224810Sjonathan ndp->ni_strictrelative = 1; 188226495Sdes if (ndp->ni_dirfd == AT_FDCWD) { 189226495Sdes#ifdef KTRACE 190226495Sdes if (KTRPOINT(td, KTR_CAPFAIL)) 191255677Spjd ktrcapfail(CAPFAIL_LOOKUP, NULL, NULL); 192226495Sdes#endif 193224810Sjonathan error = ECAPMODE; 194226495Sdes } 195224810Sjonathan } 196224810Sjonathan#endif 1971541Srgrimes if (error) { 198273411Sdelphij namei_cleanup_cnp(cnp); 1991541Srgrimes ndp->ni_vp = NULL; 2001541Srgrimes return (error); 2011541Srgrimes } 2021541Srgrimes ndp->ni_loopcnt = 0; 2031541Srgrimes#ifdef KTRACE 20497994Sjhb if (KTRPOINT(td, KTR_NAMEI)) { 20597994Sjhb KASSERT(cnp->cn_thread == curthread, 20697994Sjhb ("namei not using curthread")); 20797994Sjhb ktrnamei(cnp->cn_pnbuf); 20897994Sjhb } 2091541Srgrimes#endif 2101541Srgrimes /* 2111541Srgrimes * Get starting point for the translation. 2121541Srgrimes */ 213168355Srwatson FILEDESC_SLOCK(fdp); 21433360Sdyson ndp->ni_rootdir = fdp->fd_rdir; 21551649Sphk ndp->ni_topdir = fdp->fd_jdir; 21633360Sdyson 217243726Spjd /* 218243726Spjd * If we are auditing the kernel pathname, save the user pathname. 219243726Spjd */ 220243726Spjd if (cnp->cn_flags & AUDITVNODE1) 221243746Spjd AUDIT_ARG_UPATH1(td, ndp->ni_dirfd, cnp->cn_pnbuf); 222243726Spjd if (cnp->cn_flags & AUDITVNODE2) 223243746Spjd AUDIT_ARG_UPATH2(td, ndp->ni_dirfd, cnp->cn_pnbuf); 224243726Spjd 225185029Spjd dp = NULL; 226185029Spjd if (cnp->cn_pnbuf[0] != '/') { 227185029Spjd if (ndp->ni_startdir != NULL) { 228185029Spjd dp = ndp->ni_startdir; 229185029Spjd error = 0; 230195925Srwatson } else if (ndp->ni_dirfd != AT_FDCWD) { 231255219Spjd cap_rights_t rights; 232255219Spjd 233255219Spjd rights = ndp->ni_rightsneeded; 234255219Spjd cap_rights_set(&rights, CAP_LOOKUP); 235255219Spjd 236195925Srwatson if (cnp->cn_flags & AUDITVNODE1) 237195925Srwatson AUDIT_ARG_ATFD1(ndp->ni_dirfd); 238195925Srwatson if (cnp->cn_flags & AUDITVNODE2) 239195925Srwatson AUDIT_ARG_ATFD2(ndp->ni_dirfd); 240224810Sjonathan error = fgetvp_rights(td, ndp->ni_dirfd, 241255219Spjd &rights, &ndp->ni_filecaps, &dp); 242224810Sjonathan#ifdef CAPABILITIES 243224810Sjonathan /* 244247602Spjd * If file descriptor doesn't have all rights, 245247602Spjd * all lookups relative to it must also be 246224810Sjonathan * strictly relative. 247224810Sjonathan */ 248255219Spjd CAP_ALL(&rights); 249255219Spjd if (!cap_rights_contains(&ndp->ni_filecaps.fc_rights, 250255219Spjd &rights) || 251247602Spjd ndp->ni_filecaps.fc_fcntls != CAP_FCNTL_ALL || 252247602Spjd ndp->ni_filecaps.fc_nioctls != -1) { 253224810Sjonathan ndp->ni_strictrelative = 1; 254247602Spjd } 255224778Srwatson#endif 256195925Srwatson } 257185029Spjd if (error != 0 || dp != NULL) { 258185029Spjd FILEDESC_SUNLOCK(fdp); 259185029Spjd if (error == 0 && dp->v_type != VDIR) { 260185029Spjd vrele(dp); 261185029Spjd error = ENOTDIR; 262185029Spjd } 263177785Skib } 264177785Skib if (error) { 265273411Sdelphij namei_cleanup_cnp(cnp); 266177785Skib return (error); 267177785Skib } 268185029Spjd } 269185029Spjd if (dp == NULL) { 270177785Skib dp = fdp->fd_cdir; 271177785Skib VREF(dp); 272177785Skib FILEDESC_SUNLOCK(fdp); 273241896Skib if (ndp->ni_startdir != NULL) 274185029Spjd vrele(ndp->ni_startdir); 275177785Skib } 276289798Savg SDT_PROBE3(vfs, namei, lookup, entry, dp, cnp->cn_pnbuf, 277289798Savg cnp->cn_flags); 2781541Srgrimes for (;;) { 2791541Srgrimes /* 2801541Srgrimes * Check if root directory should replace current directory. 2811541Srgrimes * Done at start of translation and after symbolic link. 2821541Srgrimes */ 2831541Srgrimes cnp->cn_nameptr = cnp->cn_pnbuf; 2841541Srgrimes if (*(cnp->cn_nameptr) == '/') { 2851541Srgrimes vrele(dp); 286226495Sdes if (ndp->ni_strictrelative != 0) { 287226495Sdes#ifdef KTRACE 288226495Sdes if (KTRPOINT(curthread, KTR_CAPFAIL)) 289255677Spjd ktrcapfail(CAPFAIL_LOOKUP, NULL, NULL); 290226495Sdes#endif 291273411Sdelphij namei_cleanup_cnp(cnp); 292224810Sjonathan return (ENOTCAPABLE); 293226495Sdes } 2941541Srgrimes while (*(cnp->cn_nameptr) == '/') { 2951541Srgrimes cnp->cn_nameptr++; 2961541Srgrimes ndp->ni_pathlen--; 2971541Srgrimes } 2981541Srgrimes dp = ndp->ni_rootdir; 2991541Srgrimes VREF(dp); 3001541Srgrimes } 3011541Srgrimes ndp->ni_startdir = dp; 3023148Sphk error = lookup(ndp); 3033148Sphk if (error) { 304273411Sdelphij namei_cleanup_cnp(cnp); 305289798Savg SDT_PROBE2(vfs, namei, lookup, return, error, NULL); 3061541Srgrimes return (error); 3071541Srgrimes } 3081541Srgrimes /* 309193027Sdes * If not a symbolic link, we're done. 3101541Srgrimes */ 3111541Srgrimes if ((cnp->cn_flags & ISSYMLINK) == 0) { 312100613Srwatson if ((cnp->cn_flags & (SAVENAME | SAVESTART)) == 0) { 313273411Sdelphij namei_cleanup_cnp(cnp); 314100613Srwatson } else 3151541Srgrimes cnp->cn_flags |= HASBUF; 31632286Sdyson 317289798Savg SDT_PROBE2(vfs, namei, lookup, return, 0, ndp->ni_vp); 3181541Srgrimes return (0); 3191541Srgrimes } 3201541Srgrimes if (ndp->ni_loopcnt++ >= MAXSYMLINKS) { 3211541Srgrimes error = ELOOP; 3221541Srgrimes break; 3231541Srgrimes } 324101127Srwatson#ifdef MAC 325105479Srwatson if ((cnp->cn_flags & NOMACCHECK) == 0) { 326172930Srwatson error = mac_vnode_check_readlink(td->td_ucred, 327105479Srwatson ndp->ni_vp); 328105479Srwatson if (error) 329105479Srwatson break; 330105479Srwatson } 331101127Srwatson#endif 3321541Srgrimes if (ndp->ni_pathlen > 1) 333111119Simp cp = uma_zalloc(namei_zone, M_WAITOK); 3341541Srgrimes else 3351541Srgrimes cp = cnp->cn_pnbuf; 3361541Srgrimes aiov.iov_base = cp; 3371541Srgrimes aiov.iov_len = MAXPATHLEN; 3381541Srgrimes auio.uio_iov = &aiov; 3391541Srgrimes auio.uio_iovcnt = 1; 3401541Srgrimes auio.uio_offset = 0; 3411541Srgrimes auio.uio_rw = UIO_READ; 3421541Srgrimes auio.uio_segflg = UIO_SYSSPACE; 343247116Sjhb auio.uio_td = td; 3441541Srgrimes auio.uio_resid = MAXPATHLEN; 3453148Sphk error = VOP_READLINK(ndp->ni_vp, &auio, cnp->cn_cred); 3463148Sphk if (error) { 3471541Srgrimes if (ndp->ni_pathlen > 1) 34892751Sjeff uma_zfree(namei_zone, cp); 3491541Srgrimes break; 3501541Srgrimes } 3511541Srgrimes linklen = MAXPATHLEN - auio.uio_resid; 35278692Sdillon if (linklen == 0) { 35378692Sdillon if (ndp->ni_pathlen > 1) 35492751Sjeff uma_zfree(namei_zone, cp); 35578692Sdillon error = ENOENT; 35678692Sdillon break; 35778692Sdillon } 3581541Srgrimes if (linklen + ndp->ni_pathlen >= MAXPATHLEN) { 3591541Srgrimes if (ndp->ni_pathlen > 1) 36092751Sjeff uma_zfree(namei_zone, cp); 3611541Srgrimes error = ENAMETOOLONG; 3621541Srgrimes break; 3631541Srgrimes } 3641541Srgrimes if (ndp->ni_pathlen > 1) { 3651541Srgrimes bcopy(ndp->ni_next, cp + linklen, ndp->ni_pathlen); 36692751Sjeff uma_zfree(namei_zone, cnp->cn_pnbuf); 3671541Srgrimes cnp->cn_pnbuf = cp; 3681541Srgrimes } else 3691541Srgrimes cnp->cn_pnbuf[linklen] = '\0'; 3701541Srgrimes ndp->ni_pathlen += linklen; 3711541Srgrimes vput(ndp->ni_vp); 3721541Srgrimes dp = ndp->ni_dvp; 3731541Srgrimes } 374273411Sdelphij namei_cleanup_cnp(cnp); 375144833Sjeff vput(ndp->ni_vp); 376144833Sjeff ndp->ni_vp = NULL; 3771541Srgrimes vrele(ndp->ni_dvp); 378289798Savg SDT_PROBE2(vfs, namei, lookup, return, error, NULL); 3791541Srgrimes return (error); 3801541Srgrimes} 3811541Srgrimes 382162288Smohansstatic int 383240283Skibcompute_cn_lkflags(struct mount *mp, int lkflags, int cnflags) 384162288Smohans{ 385184597Sjhb 386240283Skib if (mp == NULL || ((lkflags & LK_SHARED) && 387240283Skib (!(mp->mnt_kern_flag & MNTK_LOOKUP_SHARED) || 388240283Skib ((cnflags & ISDOTDOT) && 389240283Skib (mp->mnt_kern_flag & MNTK_LOOKUP_EXCL_DOTDOT))))) { 390162288Smohans lkflags &= ~LK_SHARED; 391162288Smohans lkflags |= LK_EXCLUSIVE; 392162288Smohans } 393274606Skib lkflags |= LK_NODDLKTREAT; 394184597Sjhb return (lkflags); 395162288Smohans} 396162288Smohans 397189696Sjhbstatic __inline int 398189696Sjhbneeds_exclusive_leaf(struct mount *mp, int flags) 399189696Sjhb{ 400189696Sjhb 401189696Sjhb /* 402189696Sjhb * Intermediate nodes can use shared locks, we only need to 403189696Sjhb * force an exclusive lock for leaf nodes. 404189696Sjhb */ 405189696Sjhb if ((flags & (ISLASTCN | LOCKLEAF)) != (ISLASTCN | LOCKLEAF)) 406189696Sjhb return (0); 407189696Sjhb 408189696Sjhb /* Always use exclusive locks if LOCKSHARED isn't set. */ 409189696Sjhb if (!(flags & LOCKSHARED)) 410189696Sjhb return (1); 411189696Sjhb 412189696Sjhb /* 413189696Sjhb * For lookups during open(), if the mount point supports 414189696Sjhb * extended shared operations, then use a shared lock for the 415189696Sjhb * leaf node, otherwise use an exclusive lock. 416189696Sjhb */ 417259294Skib if ((flags & ISOPEN) != 0) 418259294Skib return (!MNT_EXTENDED_SHARED(mp)); 419189696Sjhb 420189696Sjhb /* 421189696Sjhb * Lookup requests outside of open() that specify LOCKSHARED 422189696Sjhb * only need a shared lock on the leaf vnode. 423189696Sjhb */ 424189697Sjhb return (0); 425189696Sjhb} 426189696Sjhb 4271541Srgrimes/* 4281541Srgrimes * Search a pathname. 4291541Srgrimes * This is a very central and rather complicated routine. 4301541Srgrimes * 4311541Srgrimes * The pathname is pointed to by ni_ptr and is of length ni_pathlen. 4321541Srgrimes * The starting directory is taken from ni_startdir. The pathname is 4331541Srgrimes * descended until done, or a symbolic link is encountered. The variable 4341541Srgrimes * ni_more is clear if the path is completed; it is set to one if a 4351541Srgrimes * symbolic link needing interpretation is encountered. 4361541Srgrimes * 4371541Srgrimes * The flag argument is LOOKUP, CREATE, RENAME, or DELETE depending on 4381541Srgrimes * whether the name is to be looked up, created, renamed, or deleted. 4391541Srgrimes * When CREATE, RENAME, or DELETE is specified, information usable in 4401541Srgrimes * creating, renaming, or deleting a directory entry may be calculated. 4411541Srgrimes * If flag has LOCKPARENT or'ed into it, the parent directory is returned 4421541Srgrimes * locked. If flag has WANTPARENT or'ed into it, the parent directory is 4431541Srgrimes * returned unlocked. Otherwise the parent directory is not returned. If 4441541Srgrimes * the target of the pathname exists and LOCKLEAF is or'ed into the flag 4451541Srgrimes * the target is returned locked, otherwise it is returned unlocked. 4461541Srgrimes * When creating or renaming and LOCKPARENT is specified, the target may not 4471541Srgrimes * be ".". When deleting and LOCKPARENT is specified, the target may be ".". 4488876Srgrimes * 4491541Srgrimes * Overall outline of lookup: 4501541Srgrimes * 4511541Srgrimes * dirloop: 4521541Srgrimes * identify next component of name at ndp->ni_ptr 4531541Srgrimes * handle degenerate case where name is null string 4541541Srgrimes * if .. and crossing mount points and on mounted filesys, find parent 4551541Srgrimes * call VOP_LOOKUP routine for next component name 4561541Srgrimes * directory vnode returned in ni_dvp, unlocked unless LOCKPARENT set 4571541Srgrimes * component vnode returned in ni_vp (if it exists), locked. 4581541Srgrimes * if result vnode is mounted on and crossing mount points, 4591541Srgrimes * find mounted on vnode 4601541Srgrimes * if more components of name, do next level at dirloop 4611541Srgrimes * return the answer in ni_vp, locked if LOCKLEAF set 4621541Srgrimes * if LOCKPARENT set, return locked parent in ni_dvp 4631541Srgrimes * if WANTPARENT set, return unlocked parent in ni_dvp 4641541Srgrimes */ 4651541Srgrimesint 466161011Srwatsonlookup(struct nameidata *ndp) 4671541Srgrimes{ 468161011Srwatson char *cp; /* pointer into pathname argument */ 469161011Srwatson struct vnode *dp = 0; /* the directory we are searching */ 4701541Srgrimes struct vnode *tdp; /* saved dp */ 4711541Srgrimes struct mount *mp; /* mount table entry */ 472192895Sjamie struct prison *pr; 4731541Srgrimes int docache; /* == 0 do not cache last component */ 4741541Srgrimes int wantparent; /* 1 => wantparent or lockparent flag */ 4751541Srgrimes int rdonly; /* lookup read-only flag bit */ 4761541Srgrimes int error = 0; 47765805Sbp int dpunlocked = 0; /* dp has already been unlocked */ 4781541Srgrimes struct componentname *cnp = &ndp->ni_cnd; 479162288Smohans int lkflags_save; 480229185Skib int ni_dvp_unlocked; 481162288Smohans 4821541Srgrimes /* 4831541Srgrimes * Setup: break out flag bits into variables. 4841541Srgrimes */ 485229185Skib ni_dvp_unlocked = 0; 4861541Srgrimes wantparent = cnp->cn_flags & (LOCKPARENT | WANTPARENT); 487144229Sjeff KASSERT(cnp->cn_nameiop == LOOKUP || wantparent, 488144229Sjeff ("CREATE, DELETE, RENAME require LOCKPARENT or WANTPARENT.")); 4891541Srgrimes docache = (cnp->cn_flags & NOCACHE) ^ NOCACHE; 4901541Srgrimes if (cnp->cn_nameiop == DELETE || 49122874Sbde (wantparent && cnp->cn_nameiop != CREATE && 49222874Sbde cnp->cn_nameiop != LOOKUP)) 4931541Srgrimes docache = 0; 4941541Srgrimes rdonly = cnp->cn_flags & RDONLY; 495144286Sjeff cnp->cn_flags &= ~ISSYMLINK; 4961541Srgrimes ndp->ni_dvp = NULL; 497144286Sjeff /* 498144286Sjeff * We use shared locks until we hit the parent of the last cn then 499144286Sjeff * we adjust based on the requesting flags. 500144286Sjeff */ 501144613Sjeff if (lookup_shared) 502144613Sjeff cnp->cn_lkflags = LK_SHARED; 503144613Sjeff else 504144613Sjeff cnp->cn_lkflags = LK_EXCLUSIVE; 5051541Srgrimes dp = ndp->ni_startdir; 5061541Srgrimes ndp->ni_startdir = NULLVP; 507175202Sattilio vn_lock(dp, 508240283Skib compute_cn_lkflags(dp->v_mount, cnp->cn_lkflags | LK_RETRY, 509240283Skib cnp->cn_flags)); 5101541Srgrimes 5111541Srgrimesdirloop: 5121541Srgrimes /* 5131541Srgrimes * Search a new directory. 5141541Srgrimes * 5151541Srgrimes * The last component of the filename is left accessible via 5161541Srgrimes * cnp->cn_nameptr for callers that need the name. Callers needing 5171541Srgrimes * the name set the SAVENAME flag. When done, they assume 5181541Srgrimes * responsibility for freeing the pathname buffer. 5191541Srgrimes */ 5201541Srgrimes cnp->cn_consume = 0; 5211541Srgrimes for (cp = cnp->cn_nameptr; *cp != 0 && *cp != '/'; cp++) 52251906Sphk continue; 5231541Srgrimes cnp->cn_namelen = cp - cnp->cn_nameptr; 5241541Srgrimes if (cnp->cn_namelen > NAME_MAX) { 5251541Srgrimes error = ENAMETOOLONG; 5261541Srgrimes goto bad; 5271541Srgrimes } 5281541Srgrimes#ifdef NAMEI_DIAGNOSTIC 5291541Srgrimes { char c = *cp; 5301541Srgrimes *cp = '\0'; 5311541Srgrimes printf("{%s}: ", cnp->cn_nameptr); 5321541Srgrimes *cp = c; } 5331541Srgrimes#endif 5341541Srgrimes ndp->ni_pathlen -= cnp->cn_namelen; 5351541Srgrimes ndp->ni_next = cp; 5369804Sbde 5379804Sbde /* 5389804Sbde * Replace multiple slashes by a single slash and trailing slashes 5399804Sbde * by a null. This must be done before VOP_LOOKUP() because some 5409804Sbde * fs's don't know about trailing slashes. Remember if there were 5419804Sbde * trailing slashes to handle symlinks, existing non-directories 5429804Sbde * and non-existing files that won't be directories specially later. 5439804Sbde */ 5449804Sbde while (*cp == '/' && (cp[1] == '/' || cp[1] == '\0')) { 5459804Sbde cp++; 5469804Sbde ndp->ni_pathlen--; 5479804Sbde if (*cp == '\0') { 548193557Sdes *ndp->ni_next = '\0'; 549193028Sdes cnp->cn_flags |= TRAILINGSLASH; 5509804Sbde } 5519804Sbde } 5529804Sbde ndp->ni_next = cp; 5539804Sbde 5541541Srgrimes cnp->cn_flags |= MAKEENTRY; 5551541Srgrimes if (*cp == '\0' && docache == 0) 5561541Srgrimes cnp->cn_flags &= ~MAKEENTRY; 5571541Srgrimes if (cnp->cn_namelen == 2 && 5581541Srgrimes cnp->cn_nameptr[1] == '.' && cnp->cn_nameptr[0] == '.') 5591541Srgrimes cnp->cn_flags |= ISDOTDOT; 5601541Srgrimes else 5611541Srgrimes cnp->cn_flags &= ~ISDOTDOT; 5621541Srgrimes if (*ndp->ni_next == 0) 5631541Srgrimes cnp->cn_flags |= ISLASTCN; 5641541Srgrimes else 5651541Srgrimes cnp->cn_flags &= ~ISLASTCN; 5661541Srgrimes 567199137Skib if ((cnp->cn_flags & ISLASTCN) != 0 && 568199137Skib cnp->cn_namelen == 1 && cnp->cn_nameptr[0] == '.' && 569199137Skib (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME)) { 570199137Skib error = EINVAL; 571199137Skib goto bad; 572199137Skib } 5731541Srgrimes 5741541Srgrimes /* 5751541Srgrimes * Check for degenerate name (e.g. / or "") 5761541Srgrimes * which is a way of talking about a directory, 5771541Srgrimes * e.g. like "/." or ".". 5781541Srgrimes */ 5791541Srgrimes if (cnp->cn_nameptr[0] == '\0') { 58022521Sdyson if (dp->v_type != VDIR) { 58122521Sdyson error = ENOTDIR; 58222521Sdyson goto bad; 58322521Sdyson } 5841541Srgrimes if (cnp->cn_nameiop != LOOKUP) { 5851541Srgrimes error = EISDIR; 5861541Srgrimes goto bad; 5871541Srgrimes } 5881541Srgrimes if (wantparent) { 5891541Srgrimes ndp->ni_dvp = dp; 5901541Srgrimes VREF(dp); 5911541Srgrimes } 5921541Srgrimes ndp->ni_vp = dp; 593155334Srwatson 594155334Srwatson if (cnp->cn_flags & AUDITVNODE1) 595195926Srwatson AUDIT_ARG_VNODE1(dp); 596155334Srwatson else if (cnp->cn_flags & AUDITVNODE2) 597195926Srwatson AUDIT_ARG_VNODE2(dp); 598155334Srwatson 5991541Srgrimes if (!(cnp->cn_flags & (LOCKPARENT | LOCKLEAF))) 600175294Sattilio VOP_UNLOCK(dp, 0); 60154655Seivind /* XXX This should probably move to the top of function. */ 6021541Srgrimes if (cnp->cn_flags & SAVESTART) 6031541Srgrimes panic("lookup: SAVESTART"); 604140714Sjeff goto success; 6051541Srgrimes } 6061541Srgrimes 6071541Srgrimes /* 608224810Sjonathan * Handle "..": five special cases. 609224810Sjonathan * 0. If doing a capability lookup, return ENOTCAPABLE (this is a 610224810Sjonathan * fairly conservative design choice, but it's the only one that we 611224810Sjonathan * are satisfied guarantees the property we're looking for). 612154649Struckman * 1. Return an error if this is the last component of 613154649Struckman * the name and the operation is DELETE or RENAME. 614154649Struckman * 2. If at root directory (e.g. after chroot) 6151541Srgrimes * or at absolute root directory 6161541Srgrimes * then ignore it so can't get out. 617154649Struckman * 3. If this vnode is the root of a mounted 6181541Srgrimes * filesystem, then replace it with the 6191541Srgrimes * vnode which was mounted on so we take the 62096755Strhodes * .. in the other filesystem. 621154649Struckman * 4. If the vnode is the top directory of 62251649Sphk * the jail or chroot, don't let them out. 6231541Srgrimes */ 6241541Srgrimes if (cnp->cn_flags & ISDOTDOT) { 625224810Sjonathan if (ndp->ni_strictrelative != 0) { 626226495Sdes#ifdef KTRACE 627226495Sdes if (KTRPOINT(curthread, KTR_CAPFAIL)) 628255677Spjd ktrcapfail(CAPFAIL_LOOKUP, NULL, NULL); 629226495Sdes#endif 630224810Sjonathan error = ENOTCAPABLE; 631224810Sjonathan goto bad; 632224810Sjonathan } 633154649Struckman if ((cnp->cn_flags & ISLASTCN) != 0 && 634154649Struckman (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME)) { 635154690Struckman error = EINVAL; 636154649Struckman goto bad; 637154649Struckman } 6381541Srgrimes for (;;) { 639192895Sjamie for (pr = cnp->cn_cred->cr_prison; pr != NULL; 640192895Sjamie pr = pr->pr_parent) 641192895Sjamie if (dp == pr->pr_root) 642192895Sjamie break; 64351649Sphk if (dp == ndp->ni_rootdir || 64451649Sphk dp == ndp->ni_topdir || 645166744Skib dp == rootvnode || 646192895Sjamie pr != NULL || 647166744Skib ((dp->v_vflag & VV_ROOT) != 0 && 648166744Skib (cnp->cn_flags & NOCROSSMOUNT) != 0)) { 6491541Srgrimes ndp->ni_dvp = dp; 6501541Srgrimes ndp->ni_vp = dp; 6511541Srgrimes VREF(dp); 6521541Srgrimes goto nextname; 6531541Srgrimes } 654166744Skib if ((dp->v_vflag & VV_ROOT) == 0) 6551541Srgrimes break; 656155385Sjeff if (dp->v_iflag & VI_DOOMED) { /* forced unmount */ 657190387Sjhb error = ENOENT; 65869405Salfred goto bad; 65969405Salfred } 6601541Srgrimes tdp = dp; 661144833Sjeff dp = dp->v_mount->mnt_vnodecovered; 662144833Sjeff VREF(dp); 6631541Srgrimes vput(tdp); 664175202Sattilio vn_lock(dp, 665175202Sattilio compute_cn_lkflags(dp->v_mount, cnp->cn_lkflags | 666240283Skib LK_RETRY, ISDOTDOT)); 6671541Srgrimes } 6681541Srgrimes } 6691541Srgrimes 6701541Srgrimes /* 6711541Srgrimes * We now have a segment name to search for, and a directory to search. 6721541Srgrimes */ 6731541Srgrimesunionlookup: 674101127Srwatson#ifdef MAC 675105479Srwatson if ((cnp->cn_flags & NOMACCHECK) == 0) { 676191990Sattilio error = mac_vnode_check_lookup(cnp->cn_thread->td_ucred, dp, 677191990Sattilio cnp); 678105479Srwatson if (error) 679105479Srwatson goto bad; 680105479Srwatson } 681101127Srwatson#endif 6821541Srgrimes ndp->ni_dvp = dp; 68322521Sdyson ndp->ni_vp = NULL; 68424624Sdfr ASSERT_VOP_LOCKED(dp, "lookup"); 685144286Sjeff /* 686144286Sjeff * If we have a shared lock we may need to upgrade the lock for the 687144286Sjeff * last operation. 688144286Sjeff */ 689166167Skib if (dp != vp_crossmp && 690176559Sattilio VOP_ISLOCKED(dp) == LK_SHARED && 691144286Sjeff (cnp->cn_flags & ISLASTCN) && (cnp->cn_flags & LOCKPARENT)) 692175202Sattilio vn_lock(dp, LK_UPGRADE|LK_RETRY); 693248969Skib if ((dp->v_iflag & VI_DOOMED) != 0) { 694248969Skib error = ENOENT; 695248969Skib goto bad; 696248969Skib } 697144286Sjeff /* 698144286Sjeff * If we're looking up the last component and we need an exclusive 699144286Sjeff * lock, adjust our lkflags. 700144286Sjeff */ 701189696Sjhb if (needs_exclusive_leaf(dp->v_mount, cnp->cn_flags)) 702144286Sjeff cnp->cn_lkflags = LK_EXCLUSIVE; 703138345Sphk#ifdef NAMEI_DIAGNOSTIC 704138345Sphk vprint("lookup in", dp); 705138345Sphk#endif 706162288Smohans lkflags_save = cnp->cn_lkflags; 707240283Skib cnp->cn_lkflags = compute_cn_lkflags(dp->v_mount, cnp->cn_lkflags, 708240283Skib cnp->cn_flags); 70943301Sdillon if ((error = VOP_LOOKUP(dp, &ndp->ni_vp, cnp)) != 0) { 710162288Smohans cnp->cn_lkflags = lkflags_save; 71142408Seivind KASSERT(ndp->ni_vp == NULL, ("leaf should be empty")); 7121541Srgrimes#ifdef NAMEI_DIAGNOSTIC 7131541Srgrimes printf("not found\n"); 7141541Srgrimes#endif 7151541Srgrimes if ((error == ENOENT) && 716101308Sjeff (dp->v_vflag & VV_ROOT) && (dp->v_mount != NULL) && 7171541Srgrimes (dp->v_mount->mnt_flag & MNT_UNION)) { 7181541Srgrimes tdp = dp; 719144833Sjeff dp = dp->v_mount->mnt_vnodecovered; 720144833Sjeff VREF(dp); 721144203Sjeff vput(tdp); 722175202Sattilio vn_lock(dp, 723175202Sattilio compute_cn_lkflags(dp->v_mount, cnp->cn_lkflags | 724240283Skib LK_RETRY, cnp->cn_flags)); 7251541Srgrimes goto unionlookup; 7261541Srgrimes } 7271541Srgrimes 7281541Srgrimes if (error != EJUSTRETURN) 7291541Srgrimes goto bad; 7301541Srgrimes /* 731193557Sdes * At this point, we know we're at the end of the 732193557Sdes * pathname. If creating / renaming, we can consider 733193557Sdes * allowing the file or directory to be created / renamed, 734193557Sdes * provided we're not on a read-only filesystem. 7351541Srgrimes */ 73611644Sdg if (rdonly) { 7371541Srgrimes error = EROFS; 7381541Srgrimes goto bad; 7391541Srgrimes } 740193557Sdes /* trailing slash only allowed for directories */ 741193557Sdes if ((cnp->cn_flags & TRAILINGSLASH) && 742193557Sdes !(cnp->cn_flags & WILLBEDIR)) { 7439804Sbde error = ENOENT; 7449804Sbde goto bad; 7459804Sbde } 746144203Sjeff if ((cnp->cn_flags & LOCKPARENT) == 0) 747175294Sattilio VOP_UNLOCK(dp, 0); 7481541Srgrimes /* 7491541Srgrimes * We return with ni_vp NULL to indicate that the entry 7501541Srgrimes * doesn't currently exist, leaving a pointer to the 751161010Srwatson * (possibly locked) directory vnode in ndp->ni_dvp. 7521541Srgrimes */ 7531541Srgrimes if (cnp->cn_flags & SAVESTART) { 7541541Srgrimes ndp->ni_startdir = ndp->ni_dvp; 7551541Srgrimes VREF(ndp->ni_startdir); 7561541Srgrimes } 757140714Sjeff goto success; 758162288Smohans } else 759162288Smohans cnp->cn_lkflags = lkflags_save; 7601541Srgrimes#ifdef NAMEI_DIAGNOSTIC 7611541Srgrimes printf("found\n"); 7621541Srgrimes#endif 763144203Sjeff /* 7641541Srgrimes * Take into account any additional components consumed by 7651541Srgrimes * the underlying filesystem. 7661541Srgrimes */ 7671541Srgrimes if (cnp->cn_consume > 0) { 7681541Srgrimes cnp->cn_nameptr += cnp->cn_consume; 7691541Srgrimes ndp->ni_next += cnp->cn_consume; 7701541Srgrimes ndp->ni_pathlen -= cnp->cn_consume; 7711541Srgrimes cnp->cn_consume = 0; 7721541Srgrimes } 7731541Srgrimes 7741541Srgrimes dp = ndp->ni_vp; 7751541Srgrimes 7761541Srgrimes /* 7771541Srgrimes * Check to see if the vnode has been mounted on; 77896755Strhodes * if so find the root of the mounted filesystem. 7791541Srgrimes */ 7801541Srgrimes while (dp->v_type == VDIR && (mp = dp->v_mountedhere) && 7811541Srgrimes (cnp->cn_flags & NOCROSSMOUNT) == 0) { 782184554Sattilio if (vfs_busy(mp, 0)) 7831541Srgrimes continue; 784144833Sjeff vput(dp); 785158094Sjeff if (dp != ndp->ni_dvp) 786166167Skib vput(ndp->ni_dvp); 787166167Skib else 788166167Skib vrele(ndp->ni_dvp); 789166167Skib vref(vp_crossmp); 790166167Skib ndp->ni_dvp = vp_crossmp; 791240283Skib error = VFS_ROOT(mp, compute_cn_lkflags(mp, cnp->cn_lkflags, 792240283Skib cnp->cn_flags), &tdp); 793182542Sattilio vfs_unbusy(mp); 794175202Sattilio if (vn_lock(vp_crossmp, LK_SHARED | LK_NOWAIT)) 795166167Skib panic("vp_crossmp exclusively locked or reclaimed"); 79665805Sbp if (error) { 79765805Sbp dpunlocked = 1; 7981541Srgrimes goto bad2; 79965805Sbp } 8001541Srgrimes ndp->ni_vp = dp = tdp; 8011541Srgrimes } 8021541Srgrimes 80310219Sdfr /* 80410219Sdfr * Check for symbolic link 80510219Sdfr */ 80610219Sdfr if ((dp->v_type == VLNK) && 807193557Sdes ((cnp->cn_flags & FOLLOW) || (cnp->cn_flags & TRAILINGSLASH) || 80810219Sdfr *ndp->ni_next == '/')) { 80910219Sdfr cnp->cn_flags |= ISSYMLINK; 810155385Sjeff if (dp->v_iflag & VI_DOOMED) { 811190387Sjhb /* 812190387Sjhb * We can't know whether the directory was mounted with 813190387Sjhb * NOSYMFOLLOW, so we can't follow safely. 814190387Sjhb */ 815190387Sjhb error = ENOENT; 81669405Salfred goto bad2; 81769405Salfred } 81835105Swosch if (dp->v_mount->mnt_flag & MNT_NOSYMFOLLOW) { 81935105Swosch error = EACCES; 82035105Swosch goto bad2; 82135105Swosch } 822144833Sjeff /* 823144833Sjeff * Symlink code always expects an unlocked dvp. 824144833Sjeff */ 825229185Skib if (ndp->ni_dvp != ndp->ni_vp) { 826175294Sattilio VOP_UNLOCK(ndp->ni_dvp, 0); 827229185Skib ni_dvp_unlocked = 1; 828229185Skib } 829140714Sjeff goto success; 83010219Sdfr } 83110219Sdfr 8321541Srgrimesnextname: 8331541Srgrimes /* 834193557Sdes * Not a symbolic link that we will follow. Continue with the 835193557Sdes * next component if there is any; otherwise, we're done. 8361541Srgrimes */ 837144203Sjeff KASSERT((cnp->cn_flags & ISLASTCN) || *ndp->ni_next == '/', 838144203Sjeff ("lookup: invalid path state.")); 8391541Srgrimes if (*ndp->ni_next == '/') { 8401541Srgrimes cnp->cn_nameptr = ndp->ni_next; 8411541Srgrimes while (*cnp->cn_nameptr == '/') { 8421541Srgrimes cnp->cn_nameptr++; 8431541Srgrimes ndp->ni_pathlen--; 8441541Srgrimes } 845144833Sjeff if (ndp->ni_dvp != dp) 846144833Sjeff vput(ndp->ni_dvp); 847144833Sjeff else 848144833Sjeff vrele(ndp->ni_dvp); 8491541Srgrimes goto dirloop; 8501541Srgrimes } 8511541Srgrimes /* 852193028Sdes * If we're processing a path with a trailing slash, 853193028Sdes * check that the end result is a directory. 854193028Sdes */ 855193028Sdes if ((cnp->cn_flags & TRAILINGSLASH) && dp->v_type != VDIR) { 856193028Sdes error = ENOTDIR; 857193028Sdes goto bad2; 858193028Sdes } 859193028Sdes /* 86096755Strhodes * Disallow directory write attempts on read-only filesystems. 8611541Srgrimes */ 86211644Sdg if (rdonly && 86311644Sdg (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME)) { 86411644Sdg error = EROFS; 86511644Sdg goto bad2; 8661541Srgrimes } 8671541Srgrimes if (cnp->cn_flags & SAVESTART) { 8681541Srgrimes ndp->ni_startdir = ndp->ni_dvp; 8691541Srgrimes VREF(ndp->ni_startdir); 8701541Srgrimes } 871144833Sjeff if (!wantparent) { 872229185Skib ni_dvp_unlocked = 2; 873144833Sjeff if (ndp->ni_dvp != dp) 874144833Sjeff vput(ndp->ni_dvp); 875144833Sjeff else 876144833Sjeff vrele(ndp->ni_dvp); 877229185Skib } else if ((cnp->cn_flags & LOCKPARENT) == 0 && ndp->ni_dvp != dp) { 878175294Sattilio VOP_UNLOCK(ndp->ni_dvp, 0); 879229185Skib ni_dvp_unlocked = 1; 880229185Skib } 88132071Sdyson 882155334Srwatson if (cnp->cn_flags & AUDITVNODE1) 883195926Srwatson AUDIT_ARG_VNODE1(dp); 884155334Srwatson else if (cnp->cn_flags & AUDITVNODE2) 885195926Srwatson AUDIT_ARG_VNODE2(dp); 886155334Srwatson 8871541Srgrimes if ((cnp->cn_flags & LOCKLEAF) == 0) 888175294Sattilio VOP_UNLOCK(dp, 0); 889140714Sjeffsuccess: 890172274Spjd /* 891172274Spjd * Because of lookup_shared we may have the vnode shared locked, but 892172274Spjd * the caller may want it to be exclusively locked. 893172274Spjd */ 894189696Sjhb if (needs_exclusive_leaf(dp->v_mount, cnp->cn_flags) && 895189696Sjhb VOP_ISLOCKED(dp) != LK_EXCLUSIVE) { 896175202Sattilio vn_lock(dp, LK_UPGRADE | LK_RETRY); 897186276Skib if (dp->v_iflag & VI_DOOMED) { 898186276Skib error = ENOENT; 899186276Skib goto bad2; 900186276Skib } 901172274Spjd } 9021541Srgrimes return (0); 9031541Srgrimes 9041541Srgrimesbad2: 905229185Skib if (ni_dvp_unlocked != 2) { 906229185Skib if (dp != ndp->ni_dvp && !ni_dvp_unlocked) 907229185Skib vput(ndp->ni_dvp); 908229185Skib else 909229185Skib vrele(ndp->ni_dvp); 910229185Skib } 9111541Srgrimesbad: 912144833Sjeff if (!dpunlocked) 91365805Sbp vput(dp); 9141541Srgrimes ndp->ni_vp = NULL; 9151541Srgrimes return (error); 9161541Srgrimes} 9171541Srgrimes 9183148Sphk/* 9193148Sphk * relookup - lookup a path name component 920170035Srwatson * Used by lookup to re-acquire things. 9213148Sphk */ 9223148Sphkint 923161011Srwatsonrelookup(struct vnode *dvp, struct vnode **vpp, struct componentname *cnp) 9243148Sphk{ 92522521Sdyson struct vnode *dp = 0; /* the directory we are searching */ 9263148Sphk int wantparent; /* 1 => wantparent or lockparent flag */ 9273148Sphk int rdonly; /* lookup read-only flag bit */ 9283148Sphk int error = 0; 9291541Srgrimes 930144203Sjeff KASSERT(cnp->cn_flags & ISLASTCN, 931144203Sjeff ("relookup: Not given last component.")); 9323148Sphk /* 9333148Sphk * Setup: break out flag bits into variables. 9343148Sphk */ 9353148Sphk wantparent = cnp->cn_flags & (LOCKPARENT|WANTPARENT); 936145004Sjeff KASSERT(wantparent, ("relookup: parent not wanted.")); 9373148Sphk rdonly = cnp->cn_flags & RDONLY; 9383148Sphk cnp->cn_flags &= ~ISSYMLINK; 9393148Sphk dp = dvp; 940144286Sjeff cnp->cn_lkflags = LK_EXCLUSIVE; 941175202Sattilio vn_lock(dp, LK_EXCLUSIVE | LK_RETRY); 9423148Sphk 9433148Sphk /* 9443148Sphk * Search a new directory. 9453148Sphk * 9463148Sphk * The last component of the filename is left accessible via 9473148Sphk * cnp->cn_nameptr for callers that need the name. Callers needing 9483148Sphk * the name set the SAVENAME flag. When done, they assume 9493148Sphk * responsibility for freeing the pathname buffer. 9503148Sphk */ 9513148Sphk#ifdef NAMEI_DIAGNOSTIC 9523148Sphk printf("{%s}: ", cnp->cn_nameptr); 9533148Sphk#endif 9543148Sphk 9553148Sphk /* 956205682Sjh * Check for "" which represents the root directory after slash 957205682Sjh * removal. 9583148Sphk */ 9593148Sphk if (cnp->cn_nameptr[0] == '\0') { 960205682Sjh /* 961205682Sjh * Support only LOOKUP for "/" because lookup() 962205682Sjh * can't succeed for CREATE, DELETE and RENAME. 963205682Sjh */ 964205682Sjh KASSERT(cnp->cn_nameiop == LOOKUP, ("nameiop must be LOOKUP")); 965205682Sjh KASSERT(dp->v_type == VDIR, ("dp is not a directory")); 966205682Sjh 9673148Sphk if (!(cnp->cn_flags & LOCKLEAF)) 968175294Sattilio VOP_UNLOCK(dp, 0); 9693148Sphk *vpp = dp; 97054655Seivind /* XXX This should probably move to the top of function. */ 9713148Sphk if (cnp->cn_flags & SAVESTART) 9723148Sphk panic("lookup: SAVESTART"); 9733148Sphk return (0); 9743148Sphk } 9753148Sphk 9763148Sphk if (cnp->cn_flags & ISDOTDOT) 9773148Sphk panic ("relookup: lookup on dot-dot"); 9783148Sphk 9793148Sphk /* 9803148Sphk * We now have a segment name to search for, and a directory to search. 9813148Sphk */ 982138345Sphk#ifdef NAMEI_DIAGNOSTIC 983138345Sphk vprint("search in:", dp); 984138345Sphk#endif 98543311Sdillon if ((error = VOP_LOOKUP(dp, vpp, cnp)) != 0) { 98642408Seivind KASSERT(*vpp == NULL, ("leaf should be empty")); 9873148Sphk if (error != EJUSTRETURN) 9883148Sphk goto bad; 9893148Sphk /* 9903148Sphk * If creating and at end of pathname, then can consider 9913148Sphk * allowing file to be created. 9923148Sphk */ 99311644Sdg if (rdonly) { 9943148Sphk error = EROFS; 9953148Sphk goto bad; 9963148Sphk } 9973148Sphk /* ASSERT(dvp == ndp->ni_startdir) */ 9983148Sphk if (cnp->cn_flags & SAVESTART) 9993148Sphk VREF(dvp); 1000144203Sjeff if ((cnp->cn_flags & LOCKPARENT) == 0) 1001175294Sattilio VOP_UNLOCK(dp, 0); 10023148Sphk /* 10033148Sphk * We return with ni_vp NULL to indicate that the entry 10043148Sphk * doesn't currently exist, leaving a pointer to the 1005161010Srwatson * (possibly locked) directory vnode in ndp->ni_dvp. 10063148Sphk */ 10073148Sphk return (0); 10083148Sphk } 1009162288Smohans 10103148Sphk dp = *vpp; 10113148Sphk 10123148Sphk /* 101396755Strhodes * Disallow directory write attempts on read-only filesystems. 10143148Sphk */ 101511644Sdg if (rdonly && 101611644Sdg (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME)) { 1017145004Sjeff if (dvp == dp) 1018145004Sjeff vrele(dvp); 1019145004Sjeff else 1020145004Sjeff vput(dvp); 102111644Sdg error = EROFS; 1022145004Sjeff goto bad; 10233148Sphk } 1024145004Sjeff /* 1025145004Sjeff * Set the parent lock/ref state to the requested state. 1026145004Sjeff */ 1027145004Sjeff if ((cnp->cn_flags & LOCKPARENT) == 0 && dvp != dp) { 1028145004Sjeff if (wantparent) 1029175294Sattilio VOP_UNLOCK(dvp, 0); 1030145004Sjeff else 1031145004Sjeff vput(dvp); 1032145004Sjeff } else if (!wantparent) 1033145004Sjeff vrele(dvp); 1034145004Sjeff /* 1035145004Sjeff * Check for symbolic link 1036145004Sjeff */ 1037145004Sjeff KASSERT(dp->v_type != VLNK || !(cnp->cn_flags & FOLLOW), 1038145004Sjeff ("relookup: symlink found.\n")); 1039145004Sjeff 10403148Sphk /* ASSERT(dvp == ndp->ni_startdir) */ 10413148Sphk if (cnp->cn_flags & SAVESTART) 10423148Sphk VREF(dvp); 104322521Sdyson 10443148Sphk if ((cnp->cn_flags & LOCKLEAF) == 0) 1045175294Sattilio VOP_UNLOCK(dp, 0); 10463148Sphk return (0); 10473148Sphkbad: 10483148Sphk vput(dp); 10493148Sphk *vpp = NULL; 10503148Sphk return (error); 10513148Sphk} 1052141471Sjhb 1053255219Spjdvoid 1054255219SpjdNDINIT_ALL(struct nameidata *ndp, u_long op, u_long flags, enum uio_seg segflg, 1055255219Spjd const char *namep, int dirfd, struct vnode *startdir, cap_rights_t *rightsp, 1056255219Spjd struct thread *td) 1057255219Spjd{ 1058255219Spjd 1059255219Spjd ndp->ni_cnd.cn_nameiop = op; 1060255219Spjd ndp->ni_cnd.cn_flags = flags; 1061255219Spjd ndp->ni_segflg = segflg; 1062255219Spjd ndp->ni_dirp = namep; 1063255219Spjd ndp->ni_dirfd = dirfd; 1064255219Spjd ndp->ni_startdir = startdir; 1065255219Spjd ndp->ni_strictrelative = 0; 1066255219Spjd if (rightsp != NULL) 1067255219Spjd ndp->ni_rightsneeded = *rightsp; 1068255219Spjd else 1069255219Spjd cap_rights_init(&ndp->ni_rightsneeded); 1070255219Spjd filecaps_init(&ndp->ni_filecaps); 1071255219Spjd ndp->ni_cnd.cn_thread = td; 1072255219Spjd} 1073255219Spjd 1074141471Sjhb/* 1075144661Sjeff * Free data allocated by namei(); see namei(9) for details. 1076144661Sjeff */ 1077144661Sjeffvoid 1078161011SrwatsonNDFREE(struct nameidata *ndp, const u_int flags) 1079144661Sjeff{ 1080144833Sjeff int unlock_dvp; 1081144833Sjeff int unlock_vp; 1082144661Sjeff 1083144833Sjeff unlock_dvp = 0; 1084144833Sjeff unlock_vp = 0; 1085144833Sjeff 1086144661Sjeff if (!(flags & NDF_NO_FREE_PNBUF) && 1087144661Sjeff (ndp->ni_cnd.cn_flags & HASBUF)) { 1088144661Sjeff uma_zfree(namei_zone, ndp->ni_cnd.cn_pnbuf); 1089144661Sjeff ndp->ni_cnd.cn_flags &= ~HASBUF; 1090144661Sjeff } 1091144833Sjeff if (!(flags & NDF_NO_VP_UNLOCK) && 1092144833Sjeff (ndp->ni_cnd.cn_flags & LOCKLEAF) && ndp->ni_vp) 1093144833Sjeff unlock_vp = 1; 1094144833Sjeff if (!(flags & NDF_NO_VP_RELE) && ndp->ni_vp) { 1095144833Sjeff if (unlock_vp) { 1096144833Sjeff vput(ndp->ni_vp); 1097144833Sjeff unlock_vp = 0; 1098144833Sjeff } else 1099144833Sjeff vrele(ndp->ni_vp); 1100144833Sjeff ndp->ni_vp = NULL; 1101144833Sjeff } 1102144833Sjeff if (unlock_vp) 1103175294Sattilio VOP_UNLOCK(ndp->ni_vp, 0); 1104144661Sjeff if (!(flags & NDF_NO_DVP_UNLOCK) && 1105144661Sjeff (ndp->ni_cnd.cn_flags & LOCKPARENT) && 1106144661Sjeff ndp->ni_dvp != ndp->ni_vp) 1107144833Sjeff unlock_dvp = 1; 1108144661Sjeff if (!(flags & NDF_NO_DVP_RELE) && 1109144661Sjeff (ndp->ni_cnd.cn_flags & (LOCKPARENT|WANTPARENT))) { 1110144833Sjeff if (unlock_dvp) { 1111144833Sjeff vput(ndp->ni_dvp); 1112144833Sjeff unlock_dvp = 0; 1113144833Sjeff } else 1114144833Sjeff vrele(ndp->ni_dvp); 1115144661Sjeff ndp->ni_dvp = NULL; 1116144661Sjeff } 1117144833Sjeff if (unlock_dvp) 1118175294Sattilio VOP_UNLOCK(ndp->ni_dvp, 0); 1119144661Sjeff if (!(flags & NDF_NO_STARTDIR_RELE) && 1120144661Sjeff (ndp->ni_cnd.cn_flags & SAVESTART)) { 1121144661Sjeff vrele(ndp->ni_startdir); 1122144661Sjeff ndp->ni_startdir = NULL; 1123144661Sjeff } 1124144661Sjeff} 1125144661Sjeff 1126144661Sjeff/* 1127141471Sjhb * Determine if there is a suitable alternate filename under the specified 1128141471Sjhb * prefix for the specified path. If the create flag is set, then the 1129141471Sjhb * alternate prefix will be used so long as the parent directory exists. 1130302234Sbdrewery * This is used by the various compatibility ABIs so that Linux binaries prefer 1131141471Sjhb * files under /compat/linux for example. The chosen path (whether under 1132141471Sjhb * the prefix or under /) is returned in a kernel malloc'd buffer pointed 1133141471Sjhb * to by pathbuf. The caller is responsible for free'ing the buffer from 1134141471Sjhb * the M_TEMP bucket if one is returned. 1135141471Sjhb */ 1136141471Sjhbint 1137177997Skibkern_alternate_path(struct thread *td, const char *prefix, const char *path, 1138177997Skib enum uio_seg pathseg, char **pathbuf, int create, int dirfd) 1139141471Sjhb{ 1140141471Sjhb struct nameidata nd, ndroot; 1141141471Sjhb char *ptr, *buf, *cp; 1142141471Sjhb size_t len, sz; 1143141471Sjhb int error; 1144141471Sjhb 1145141471Sjhb buf = (char *) malloc(MAXPATHLEN, M_TEMP, M_WAITOK); 1146141471Sjhb *pathbuf = buf; 1147141471Sjhb 1148141471Sjhb /* Copy the prefix into the new pathname as a starting point. */ 1149141471Sjhb len = strlcpy(buf, prefix, MAXPATHLEN); 1150141471Sjhb if (len >= MAXPATHLEN) { 1151141471Sjhb *pathbuf = NULL; 1152141471Sjhb free(buf, M_TEMP); 1153141471Sjhb return (EINVAL); 1154141471Sjhb } 1155141471Sjhb sz = MAXPATHLEN - len; 1156141471Sjhb ptr = buf + len; 1157141471Sjhb 1158141471Sjhb /* Append the filename to the prefix. */ 1159141471Sjhb if (pathseg == UIO_SYSSPACE) 1160141471Sjhb error = copystr(path, ptr, sz, &len); 1161141471Sjhb else 1162141471Sjhb error = copyinstr(path, ptr, sz, &len); 1163141471Sjhb 1164141471Sjhb if (error) { 1165141471Sjhb *pathbuf = NULL; 1166141471Sjhb free(buf, M_TEMP); 1167141471Sjhb return (error); 1168141471Sjhb } 1169141471Sjhb 1170141471Sjhb /* Only use a prefix with absolute pathnames. */ 1171141471Sjhb if (*ptr != '/') { 1172141471Sjhb error = EINVAL; 1173141471Sjhb goto keeporig; 1174141471Sjhb } 1175141471Sjhb 1176177997Skib if (dirfd != AT_FDCWD) { 1177177997Skib /* 1178177997Skib * We want the original because the "prefix" is 1179177997Skib * included in the already opened dirfd. 1180177997Skib */ 1181177997Skib bcopy(ptr, buf, len); 1182177997Skib return (0); 1183177997Skib } 1184177997Skib 1185141471Sjhb /* 1186141471Sjhb * We know that there is a / somewhere in this pathname. 1187141471Sjhb * Search backwards for it, to find the file's parent dir 1188141471Sjhb * to see if it exists in the alternate tree. If it does, 1189141471Sjhb * and we want to create a file (cflag is set). We don't 1190141471Sjhb * need to worry about the root comparison in this case. 1191141471Sjhb */ 1192141471Sjhb 1193141471Sjhb if (create) { 1194141471Sjhb for (cp = &ptr[len] - 1; *cp != '/'; cp--); 1195141471Sjhb *cp = '\0'; 1196141471Sjhb 1197241896Skib NDINIT(&nd, LOOKUP, FOLLOW, UIO_SYSSPACE, buf, td); 1198141471Sjhb error = namei(&nd); 1199141471Sjhb *cp = '/'; 1200141471Sjhb if (error != 0) 1201150431Sjhb goto keeporig; 1202141471Sjhb } else { 1203241896Skib NDINIT(&nd, LOOKUP, FOLLOW, UIO_SYSSPACE, buf, td); 1204141471Sjhb 1205141471Sjhb error = namei(&nd); 1206141471Sjhb if (error != 0) 1207150431Sjhb goto keeporig; 1208141471Sjhb 1209141471Sjhb /* 1210141471Sjhb * We now compare the vnode of the prefix to the one 1211141471Sjhb * vnode asked. If they resolve to be the same, then we 1212141471Sjhb * ignore the match so that the real root gets used. 1213141471Sjhb * This avoids the problem of traversing "../.." to find the 1214141471Sjhb * root directory and never finding it, because "/" resolves 1215141471Sjhb * to the emulation root directory. This is expensive :-( 1216141471Sjhb */ 1217241896Skib NDINIT(&ndroot, LOOKUP, FOLLOW, UIO_SYSSPACE, prefix, 1218150431Sjhb td); 1219141471Sjhb 1220141471Sjhb /* We shouldn't ever get an error from this namei(). */ 1221141471Sjhb error = namei(&ndroot); 1222141471Sjhb if (error == 0) { 1223141471Sjhb if (nd.ni_vp == ndroot.ni_vp) 1224141471Sjhb error = ENOENT; 1225141471Sjhb 1226141471Sjhb NDFREE(&ndroot, NDF_ONLY_PNBUF); 1227141471Sjhb vrele(ndroot.ni_vp); 1228141471Sjhb } 1229141471Sjhb } 1230141471Sjhb 1231141471Sjhb NDFREE(&nd, NDF_ONLY_PNBUF); 1232141471Sjhb vrele(nd.ni_vp); 1233141471Sjhb 1234141471Sjhbkeeporig: 1235141471Sjhb /* If there was an error, use the original path name. */ 1236141471Sjhb if (error) 1237141471Sjhb bcopy(ptr, buf, len); 1238141471Sjhb return (error); 1239141471Sjhb} 1240