vfs_lookup.c revision 243726
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: head/sys/kern/vfs_lookup.c 243726 2012-11-30 23:18:49Z pjd $"); 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> 47224778Srwatson#include <sys/capability.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); 73211616SrpauloSDT_PROBE_DEFINE3(vfs, namei, lookup, entry, entry, "struct vnode *", "char *", 74190759Srwatson "unsigned long"); 75211616SrpauloSDT_PROBE_DEFINE2(vfs, namei, lookup, return, 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 */ 1241541Srgrimesint 125161011Srwatsonnamei(struct nameidata *ndp) 1261541Srgrimes{ 127161011Srwatson struct filedesc *fdp; /* pointer to file descriptor state */ 128161011Srwatson char *cp; /* pointer into pathname argument */ 129161011Srwatson struct vnode *dp; /* the directory we are searching */ 1301541Srgrimes struct iovec aiov; /* uio for reading symbolic links */ 1311541Srgrimes struct uio auio; 1321541Srgrimes int error, linklen; 1331541Srgrimes struct componentname *cnp = &ndp->ni_cnd; 13483366Sjulian struct thread *td = cnp->cn_thread; 13583366Sjulian struct proc *p = td->td_proc; 1361541Srgrimes 13791419Sjhb ndp->ni_cnd.cn_cred = ndp->ni_cnd.cn_thread->td_ucred; 13883366Sjulian KASSERT(cnp->cn_cred && p, ("namei: bad cred/proc")); 13942408Seivind KASSERT((cnp->cn_nameiop & (~OPMASK)) == 0, 14042453Seivind ("namei: nameiop contaminated with flags")); 14142408Seivind KASSERT((cnp->cn_flags & OPMASK) == 0, 14242453Seivind ("namei: flags contaminated with nameiops")); 143144613Sjeff if (!lookup_shared) 144144613Sjeff cnp->cn_flags &= ~LOCKSHARED; 14583366Sjulian fdp = p->p_fd; 1461541Srgrimes 147193028Sdes /* We will set this ourselves if we need it. */ 148193028Sdes cnp->cn_flags &= ~TRAILINGSLASH; 149193028Sdes 1501541Srgrimes /* 1511541Srgrimes * Get a buffer for the name to be translated, and copy the 1521541Srgrimes * name into the buffer. 1531541Srgrimes */ 1541541Srgrimes if ((cnp->cn_flags & HASBUF) == 0) 155111119Simp cnp->cn_pnbuf = uma_zalloc(namei_zone, M_WAITOK); 1561541Srgrimes if (ndp->ni_segflg == UIO_SYSSPACE) 1571541Srgrimes error = copystr(ndp->ni_dirp, cnp->cn_pnbuf, 15836735Sdfr MAXPATHLEN, (size_t *)&ndp->ni_pathlen); 1591541Srgrimes else 1601541Srgrimes error = copyinstr(ndp->ni_dirp, cnp->cn_pnbuf, 16136735Sdfr MAXPATHLEN, (size_t *)&ndp->ni_pathlen); 16220069Sbde 16320069Sbde /* 16420069Sbde * Don't allow empty pathnames. 16520069Sbde */ 16620069Sbde if (!error && *cnp->cn_pnbuf == '\0') 16720069Sbde error = ENOENT; 16820069Sbde 169224810Sjonathan#ifdef CAPABILITY_MODE 170224810Sjonathan /* 171224810Sjonathan * In capability mode, lookups must be "strictly relative" (i.e. 172224810Sjonathan * not an absolute path, and not containing '..' components) to 173224810Sjonathan * a real file descriptor, not the pseudo-descriptor AT_FDCWD. 174224810Sjonathan */ 175243612Spjd if (IN_CAPABILITY_MODE(td) && (cnp->cn_flags & NOCAPCHECK) == 0) { 176224810Sjonathan ndp->ni_strictrelative = 1; 177226495Sdes if (ndp->ni_dirfd == AT_FDCWD) { 178226495Sdes#ifdef KTRACE 179226495Sdes if (KTRPOINT(td, KTR_CAPFAIL)) 180226495Sdes ktrcapfail(CAPFAIL_LOOKUP, 0, 0); 181226495Sdes#endif 182224810Sjonathan error = ECAPMODE; 183226495Sdes } 184224810Sjonathan } 185224810Sjonathan#endif 1861541Srgrimes if (error) { 18792751Sjeff uma_zfree(namei_zone, cnp->cn_pnbuf); 188100613Srwatson#ifdef DIAGNOSTIC 189100613Srwatson cnp->cn_pnbuf = NULL; 190100613Srwatson cnp->cn_nameptr = NULL; 191100613Srwatson#endif 1921541Srgrimes ndp->ni_vp = NULL; 1931541Srgrimes return (error); 1941541Srgrimes } 1951541Srgrimes ndp->ni_loopcnt = 0; 1961541Srgrimes#ifdef KTRACE 19797994Sjhb if (KTRPOINT(td, KTR_NAMEI)) { 19897994Sjhb KASSERT(cnp->cn_thread == curthread, 19997994Sjhb ("namei not using curthread")); 20097994Sjhb ktrnamei(cnp->cn_pnbuf); 20197994Sjhb } 2021541Srgrimes#endif 2031541Srgrimes /* 2041541Srgrimes * Get starting point for the translation. 2051541Srgrimes */ 206168355Srwatson FILEDESC_SLOCK(fdp); 20733360Sdyson ndp->ni_rootdir = fdp->fd_rdir; 20851649Sphk ndp->ni_topdir = fdp->fd_jdir; 20933360Sdyson 210243726Spjd /* 211243726Spjd * If we are auditing the kernel pathname, save the user pathname. 212243726Spjd */ 213243726Spjd if (cnp->cn_flags & AUDITVNODE1) 214243726Spjd AUDIT_ARG_UPATH1(td, ndp->ni_dirfd, , cnp->cn_pnbuf); 215243726Spjd if (cnp->cn_flags & AUDITVNODE2) 216243726Spjd AUDIT_ARG_UPATH2(td, ndp->ni_dirfd, , cnp->cn_pnbuf); 217243726Spjd 218185029Spjd dp = NULL; 219185029Spjd if (cnp->cn_pnbuf[0] != '/') { 220185029Spjd if (ndp->ni_startdir != NULL) { 221185029Spjd dp = ndp->ni_startdir; 222185029Spjd error = 0; 223195925Srwatson } else if (ndp->ni_dirfd != AT_FDCWD) { 224195925Srwatson if (cnp->cn_flags & AUDITVNODE1) 225195925Srwatson AUDIT_ARG_ATFD1(ndp->ni_dirfd); 226195925Srwatson if (cnp->cn_flags & AUDITVNODE2) 227195925Srwatson AUDIT_ARG_ATFD2(ndp->ni_dirfd); 228224810Sjonathan error = fgetvp_rights(td, ndp->ni_dirfd, 229224810Sjonathan ndp->ni_rightsneeded | CAP_LOOKUP, 230224810Sjonathan &(ndp->ni_baserights), &dp); 231224810Sjonathan#ifdef CAPABILITIES 232224810Sjonathan /* 233224810Sjonathan * Lookups relative to a capability must also be 234224810Sjonathan * strictly relative. 235224810Sjonathan * 236224810Sjonathan * Note that a capability with rights CAP_MASK_VALID 237224810Sjonathan * is treated exactly like a regular file descriptor. 238224810Sjonathan */ 239224810Sjonathan if (ndp->ni_baserights != CAP_MASK_VALID) 240224810Sjonathan ndp->ni_strictrelative = 1; 241224778Srwatson#endif 242195925Srwatson } 243185029Spjd if (error != 0 || dp != NULL) { 244185029Spjd FILEDESC_SUNLOCK(fdp); 245185029Spjd if (error == 0 && dp->v_type != VDIR) { 246185029Spjd vrele(dp); 247185029Spjd error = ENOTDIR; 248185029Spjd } 249177785Skib } 250177785Skib if (error) { 251177785Skib uma_zfree(namei_zone, cnp->cn_pnbuf); 252177785Skib#ifdef DIAGNOSTIC 253177785Skib cnp->cn_pnbuf = NULL; 254177785Skib cnp->cn_nameptr = NULL; 255177785Skib#endif 256177785Skib return (error); 257177785Skib } 258185029Spjd } 259185029Spjd if (dp == NULL) { 260177785Skib dp = fdp->fd_cdir; 261177785Skib VREF(dp); 262177785Skib FILEDESC_SUNLOCK(fdp); 263241896Skib if (ndp->ni_startdir != NULL) 264185029Spjd vrele(ndp->ni_startdir); 265177785Skib } 266190759Srwatson SDT_PROBE(vfs, namei, lookup, entry, dp, cnp->cn_pnbuf, 267190759Srwatson cnp->cn_flags, 0, 0); 2681541Srgrimes for (;;) { 2691541Srgrimes /* 2701541Srgrimes * Check if root directory should replace current directory. 2711541Srgrimes * Done at start of translation and after symbolic link. 2721541Srgrimes */ 2731541Srgrimes cnp->cn_nameptr = cnp->cn_pnbuf; 2741541Srgrimes if (*(cnp->cn_nameptr) == '/') { 2751541Srgrimes vrele(dp); 276226495Sdes if (ndp->ni_strictrelative != 0) { 277226495Sdes#ifdef KTRACE 278226495Sdes if (KTRPOINT(curthread, KTR_CAPFAIL)) 279226495Sdes ktrcapfail(CAPFAIL_LOOKUP, 0, 0); 280226495Sdes#endif 281224810Sjonathan return (ENOTCAPABLE); 282226495Sdes } 2831541Srgrimes while (*(cnp->cn_nameptr) == '/') { 2841541Srgrimes cnp->cn_nameptr++; 2851541Srgrimes ndp->ni_pathlen--; 2861541Srgrimes } 2871541Srgrimes dp = ndp->ni_rootdir; 2881541Srgrimes VREF(dp); 2891541Srgrimes } 2901541Srgrimes ndp->ni_startdir = dp; 2913148Sphk error = lookup(ndp); 2923148Sphk if (error) { 29392751Sjeff uma_zfree(namei_zone, cnp->cn_pnbuf); 294100613Srwatson#ifdef DIAGNOSTIC 295100613Srwatson cnp->cn_pnbuf = NULL; 296100613Srwatson cnp->cn_nameptr = NULL; 297100613Srwatson#endif 298190759Srwatson SDT_PROBE(vfs, namei, lookup, return, error, NULL, 0, 299190759Srwatson 0, 0); 3001541Srgrimes return (error); 3011541Srgrimes } 3021541Srgrimes /* 303193027Sdes * If not a symbolic link, we're done. 3041541Srgrimes */ 3051541Srgrimes if ((cnp->cn_flags & ISSYMLINK) == 0) { 306100613Srwatson if ((cnp->cn_flags & (SAVENAME | SAVESTART)) == 0) { 30792751Sjeff uma_zfree(namei_zone, cnp->cn_pnbuf); 308100613Srwatson#ifdef DIAGNOSTIC 309100613Srwatson cnp->cn_pnbuf = NULL; 310100613Srwatson cnp->cn_nameptr = NULL; 311100613Srwatson#endif 312100613Srwatson } else 3131541Srgrimes cnp->cn_flags |= HASBUF; 31432286Sdyson 315190759Srwatson SDT_PROBE(vfs, namei, lookup, return, 0, ndp->ni_vp, 316190759Srwatson 0, 0, 0); 3171541Srgrimes return (0); 3181541Srgrimes } 3191541Srgrimes if (ndp->ni_loopcnt++ >= MAXSYMLINKS) { 3201541Srgrimes error = ELOOP; 3211541Srgrimes break; 3221541Srgrimes } 323101127Srwatson#ifdef MAC 324105479Srwatson if ((cnp->cn_flags & NOMACCHECK) == 0) { 325172930Srwatson error = mac_vnode_check_readlink(td->td_ucred, 326105479Srwatson ndp->ni_vp); 327105479Srwatson if (error) 328105479Srwatson break; 329105479Srwatson } 330101127Srwatson#endif 3311541Srgrimes if (ndp->ni_pathlen > 1) 332111119Simp cp = uma_zalloc(namei_zone, M_WAITOK); 3331541Srgrimes else 3341541Srgrimes cp = cnp->cn_pnbuf; 3351541Srgrimes aiov.iov_base = cp; 3361541Srgrimes aiov.iov_len = MAXPATHLEN; 3371541Srgrimes auio.uio_iov = &aiov; 3381541Srgrimes auio.uio_iovcnt = 1; 3391541Srgrimes auio.uio_offset = 0; 3401541Srgrimes auio.uio_rw = UIO_READ; 3411541Srgrimes auio.uio_segflg = UIO_SYSSPACE; 34283366Sjulian auio.uio_td = (struct thread *)0; 3431541Srgrimes auio.uio_resid = MAXPATHLEN; 3443148Sphk error = VOP_READLINK(ndp->ni_vp, &auio, cnp->cn_cred); 3453148Sphk if (error) { 3461541Srgrimes if (ndp->ni_pathlen > 1) 34792751Sjeff uma_zfree(namei_zone, cp); 3481541Srgrimes break; 3491541Srgrimes } 3501541Srgrimes linklen = MAXPATHLEN - auio.uio_resid; 35178692Sdillon if (linklen == 0) { 35278692Sdillon if (ndp->ni_pathlen > 1) 35392751Sjeff uma_zfree(namei_zone, cp); 35478692Sdillon error = ENOENT; 35578692Sdillon break; 35678692Sdillon } 3571541Srgrimes if (linklen + ndp->ni_pathlen >= MAXPATHLEN) { 3581541Srgrimes if (ndp->ni_pathlen > 1) 35992751Sjeff uma_zfree(namei_zone, cp); 3601541Srgrimes error = ENAMETOOLONG; 3611541Srgrimes break; 3621541Srgrimes } 3631541Srgrimes if (ndp->ni_pathlen > 1) { 3641541Srgrimes bcopy(ndp->ni_next, cp + linklen, ndp->ni_pathlen); 36592751Sjeff uma_zfree(namei_zone, cnp->cn_pnbuf); 3661541Srgrimes cnp->cn_pnbuf = cp; 3671541Srgrimes } else 3681541Srgrimes cnp->cn_pnbuf[linklen] = '\0'; 3691541Srgrimes ndp->ni_pathlen += linklen; 3701541Srgrimes vput(ndp->ni_vp); 3711541Srgrimes dp = ndp->ni_dvp; 3721541Srgrimes } 37392751Sjeff uma_zfree(namei_zone, cnp->cn_pnbuf); 374100613Srwatson#ifdef DIAGNOSTIC 375100613Srwatson cnp->cn_pnbuf = NULL; 376100613Srwatson cnp->cn_nameptr = NULL; 377100613Srwatson#endif 378144833Sjeff vput(ndp->ni_vp); 379144833Sjeff ndp->ni_vp = NULL; 3801541Srgrimes vrele(ndp->ni_dvp); 381190759Srwatson SDT_PROBE(vfs, namei, lookup, return, error, NULL, 0, 0, 0); 3821541Srgrimes return (error); 3831541Srgrimes} 3841541Srgrimes 385162288Smohansstatic int 386240283Skibcompute_cn_lkflags(struct mount *mp, int lkflags, int cnflags) 387162288Smohans{ 388184597Sjhb 389240283Skib if (mp == NULL || ((lkflags & LK_SHARED) && 390240283Skib (!(mp->mnt_kern_flag & MNTK_LOOKUP_SHARED) || 391240283Skib ((cnflags & ISDOTDOT) && 392240283Skib (mp->mnt_kern_flag & MNTK_LOOKUP_EXCL_DOTDOT))))) { 393162288Smohans lkflags &= ~LK_SHARED; 394162288Smohans lkflags |= LK_EXCLUSIVE; 395162288Smohans } 396184597Sjhb return (lkflags); 397162288Smohans} 398162288Smohans 399189696Sjhbstatic __inline int 400189696Sjhbneeds_exclusive_leaf(struct mount *mp, int flags) 401189696Sjhb{ 402189696Sjhb 403189696Sjhb /* 404189696Sjhb * Intermediate nodes can use shared locks, we only need to 405189696Sjhb * force an exclusive lock for leaf nodes. 406189696Sjhb */ 407189696Sjhb if ((flags & (ISLASTCN | LOCKLEAF)) != (ISLASTCN | LOCKLEAF)) 408189696Sjhb return (0); 409189696Sjhb 410189696Sjhb /* Always use exclusive locks if LOCKSHARED isn't set. */ 411189696Sjhb if (!(flags & LOCKSHARED)) 412189696Sjhb return (1); 413189696Sjhb 414189696Sjhb /* 415189696Sjhb * For lookups during open(), if the mount point supports 416189696Sjhb * extended shared operations, then use a shared lock for the 417189696Sjhb * leaf node, otherwise use an exclusive lock. 418189696Sjhb */ 419189696Sjhb if (flags & ISOPEN) { 420189696Sjhb if (mp != NULL && 421189696Sjhb (mp->mnt_kern_flag & MNTK_EXTENDED_SHARED)) 422189696Sjhb return (0); 423189696Sjhb else 424189696Sjhb return (1); 425189696Sjhb } 426189696Sjhb 427189696Sjhb /* 428189696Sjhb * Lookup requests outside of open() that specify LOCKSHARED 429189696Sjhb * only need a shared lock on the leaf vnode. 430189696Sjhb */ 431189697Sjhb return (0); 432189696Sjhb} 433189696Sjhb 4341541Srgrimes/* 4351541Srgrimes * Search a pathname. 4361541Srgrimes * This is a very central and rather complicated routine. 4371541Srgrimes * 4381541Srgrimes * The pathname is pointed to by ni_ptr and is of length ni_pathlen. 4391541Srgrimes * The starting directory is taken from ni_startdir. The pathname is 4401541Srgrimes * descended until done, or a symbolic link is encountered. The variable 4411541Srgrimes * ni_more is clear if the path is completed; it is set to one if a 4421541Srgrimes * symbolic link needing interpretation is encountered. 4431541Srgrimes * 4441541Srgrimes * The flag argument is LOOKUP, CREATE, RENAME, or DELETE depending on 4451541Srgrimes * whether the name is to be looked up, created, renamed, or deleted. 4461541Srgrimes * When CREATE, RENAME, or DELETE is specified, information usable in 4471541Srgrimes * creating, renaming, or deleting a directory entry may be calculated. 4481541Srgrimes * If flag has LOCKPARENT or'ed into it, the parent directory is returned 4491541Srgrimes * locked. If flag has WANTPARENT or'ed into it, the parent directory is 4501541Srgrimes * returned unlocked. Otherwise the parent directory is not returned. If 4511541Srgrimes * the target of the pathname exists and LOCKLEAF is or'ed into the flag 4521541Srgrimes * the target is returned locked, otherwise it is returned unlocked. 4531541Srgrimes * When creating or renaming and LOCKPARENT is specified, the target may not 4541541Srgrimes * be ".". When deleting and LOCKPARENT is specified, the target may be ".". 4558876Srgrimes * 4561541Srgrimes * Overall outline of lookup: 4571541Srgrimes * 4581541Srgrimes * dirloop: 4591541Srgrimes * identify next component of name at ndp->ni_ptr 4601541Srgrimes * handle degenerate case where name is null string 4611541Srgrimes * if .. and crossing mount points and on mounted filesys, find parent 4621541Srgrimes * call VOP_LOOKUP routine for next component name 4631541Srgrimes * directory vnode returned in ni_dvp, unlocked unless LOCKPARENT set 4641541Srgrimes * component vnode returned in ni_vp (if it exists), locked. 4651541Srgrimes * if result vnode is mounted on and crossing mount points, 4661541Srgrimes * find mounted on vnode 4671541Srgrimes * if more components of name, do next level at dirloop 4681541Srgrimes * return the answer in ni_vp, locked if LOCKLEAF set 4691541Srgrimes * if LOCKPARENT set, return locked parent in ni_dvp 4701541Srgrimes * if WANTPARENT set, return unlocked parent in ni_dvp 4711541Srgrimes */ 4721541Srgrimesint 473161011Srwatsonlookup(struct nameidata *ndp) 4741541Srgrimes{ 475161011Srwatson char *cp; /* pointer into pathname argument */ 476161011Srwatson struct vnode *dp = 0; /* the directory we are searching */ 4771541Srgrimes struct vnode *tdp; /* saved dp */ 4781541Srgrimes struct mount *mp; /* mount table entry */ 479192895Sjamie struct prison *pr; 4801541Srgrimes int docache; /* == 0 do not cache last component */ 4811541Srgrimes int wantparent; /* 1 => wantparent or lockparent flag */ 4821541Srgrimes int rdonly; /* lookup read-only flag bit */ 4831541Srgrimes int error = 0; 48465805Sbp int dpunlocked = 0; /* dp has already been unlocked */ 4851541Srgrimes struct componentname *cnp = &ndp->ni_cnd; 486162288Smohans int lkflags_save; 487229185Skib int ni_dvp_unlocked; 488162288Smohans 4891541Srgrimes /* 4901541Srgrimes * Setup: break out flag bits into variables. 4911541Srgrimes */ 492229185Skib ni_dvp_unlocked = 0; 4931541Srgrimes wantparent = cnp->cn_flags & (LOCKPARENT | WANTPARENT); 494144229Sjeff KASSERT(cnp->cn_nameiop == LOOKUP || wantparent, 495144229Sjeff ("CREATE, DELETE, RENAME require LOCKPARENT or WANTPARENT.")); 4961541Srgrimes docache = (cnp->cn_flags & NOCACHE) ^ NOCACHE; 4971541Srgrimes if (cnp->cn_nameiop == DELETE || 49822874Sbde (wantparent && cnp->cn_nameiop != CREATE && 49922874Sbde cnp->cn_nameiop != LOOKUP)) 5001541Srgrimes docache = 0; 5011541Srgrimes rdonly = cnp->cn_flags & RDONLY; 502144286Sjeff cnp->cn_flags &= ~ISSYMLINK; 5031541Srgrimes ndp->ni_dvp = NULL; 504144286Sjeff /* 505144286Sjeff * We use shared locks until we hit the parent of the last cn then 506144286Sjeff * we adjust based on the requesting flags. 507144286Sjeff */ 508144613Sjeff if (lookup_shared) 509144613Sjeff cnp->cn_lkflags = LK_SHARED; 510144613Sjeff else 511144613Sjeff cnp->cn_lkflags = LK_EXCLUSIVE; 5121541Srgrimes dp = ndp->ni_startdir; 5131541Srgrimes ndp->ni_startdir = NULLVP; 514175202Sattilio vn_lock(dp, 515240283Skib compute_cn_lkflags(dp->v_mount, cnp->cn_lkflags | LK_RETRY, 516240283Skib cnp->cn_flags)); 5171541Srgrimes 5181541Srgrimesdirloop: 5191541Srgrimes /* 5201541Srgrimes * Search a new directory. 5211541Srgrimes * 5221541Srgrimes * The last component of the filename is left accessible via 5231541Srgrimes * cnp->cn_nameptr for callers that need the name. Callers needing 5241541Srgrimes * the name set the SAVENAME flag. When done, they assume 5251541Srgrimes * responsibility for freeing the pathname buffer. 5261541Srgrimes */ 5271541Srgrimes cnp->cn_consume = 0; 5281541Srgrimes for (cp = cnp->cn_nameptr; *cp != 0 && *cp != '/'; cp++) 52951906Sphk continue; 5301541Srgrimes cnp->cn_namelen = cp - cnp->cn_nameptr; 5311541Srgrimes if (cnp->cn_namelen > NAME_MAX) { 5321541Srgrimes error = ENAMETOOLONG; 5331541Srgrimes goto bad; 5341541Srgrimes } 5351541Srgrimes#ifdef NAMEI_DIAGNOSTIC 5361541Srgrimes { char c = *cp; 5371541Srgrimes *cp = '\0'; 5381541Srgrimes printf("{%s}: ", cnp->cn_nameptr); 5391541Srgrimes *cp = c; } 5401541Srgrimes#endif 5411541Srgrimes ndp->ni_pathlen -= cnp->cn_namelen; 5421541Srgrimes ndp->ni_next = cp; 5439804Sbde 5449804Sbde /* 5459804Sbde * Replace multiple slashes by a single slash and trailing slashes 5469804Sbde * by a null. This must be done before VOP_LOOKUP() because some 5479804Sbde * fs's don't know about trailing slashes. Remember if there were 5489804Sbde * trailing slashes to handle symlinks, existing non-directories 5499804Sbde * and non-existing files that won't be directories specially later. 5509804Sbde */ 5519804Sbde while (*cp == '/' && (cp[1] == '/' || cp[1] == '\0')) { 5529804Sbde cp++; 5539804Sbde ndp->ni_pathlen--; 5549804Sbde if (*cp == '\0') { 555193557Sdes *ndp->ni_next = '\0'; 556193028Sdes cnp->cn_flags |= TRAILINGSLASH; 5579804Sbde } 5589804Sbde } 5599804Sbde ndp->ni_next = cp; 5609804Sbde 5611541Srgrimes cnp->cn_flags |= MAKEENTRY; 5621541Srgrimes if (*cp == '\0' && docache == 0) 5631541Srgrimes cnp->cn_flags &= ~MAKEENTRY; 5641541Srgrimes if (cnp->cn_namelen == 2 && 5651541Srgrimes cnp->cn_nameptr[1] == '.' && cnp->cn_nameptr[0] == '.') 5661541Srgrimes cnp->cn_flags |= ISDOTDOT; 5671541Srgrimes else 5681541Srgrimes cnp->cn_flags &= ~ISDOTDOT; 5691541Srgrimes if (*ndp->ni_next == 0) 5701541Srgrimes cnp->cn_flags |= ISLASTCN; 5711541Srgrimes else 5721541Srgrimes cnp->cn_flags &= ~ISLASTCN; 5731541Srgrimes 574199137Skib if ((cnp->cn_flags & ISLASTCN) != 0 && 575199137Skib cnp->cn_namelen == 1 && cnp->cn_nameptr[0] == '.' && 576199137Skib (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME)) { 577199137Skib error = EINVAL; 578199137Skib goto bad; 579199137Skib } 5801541Srgrimes 5811541Srgrimes /* 5821541Srgrimes * Check for degenerate name (e.g. / or "") 5831541Srgrimes * which is a way of talking about a directory, 5841541Srgrimes * e.g. like "/." or ".". 5851541Srgrimes */ 5861541Srgrimes if (cnp->cn_nameptr[0] == '\0') { 58722521Sdyson if (dp->v_type != VDIR) { 58822521Sdyson error = ENOTDIR; 58922521Sdyson goto bad; 59022521Sdyson } 5911541Srgrimes if (cnp->cn_nameiop != LOOKUP) { 5921541Srgrimes error = EISDIR; 5931541Srgrimes goto bad; 5941541Srgrimes } 5951541Srgrimes if (wantparent) { 5961541Srgrimes ndp->ni_dvp = dp; 5971541Srgrimes VREF(dp); 5981541Srgrimes } 5991541Srgrimes ndp->ni_vp = dp; 600155334Srwatson 601155334Srwatson if (cnp->cn_flags & AUDITVNODE1) 602195926Srwatson AUDIT_ARG_VNODE1(dp); 603155334Srwatson else if (cnp->cn_flags & AUDITVNODE2) 604195926Srwatson AUDIT_ARG_VNODE2(dp); 605155334Srwatson 6061541Srgrimes if (!(cnp->cn_flags & (LOCKPARENT | LOCKLEAF))) 607175294Sattilio VOP_UNLOCK(dp, 0); 60854655Seivind /* XXX This should probably move to the top of function. */ 6091541Srgrimes if (cnp->cn_flags & SAVESTART) 6101541Srgrimes panic("lookup: SAVESTART"); 611140714Sjeff goto success; 6121541Srgrimes } 6131541Srgrimes 6141541Srgrimes /* 615224810Sjonathan * Handle "..": five special cases. 616224810Sjonathan * 0. If doing a capability lookup, return ENOTCAPABLE (this is a 617224810Sjonathan * fairly conservative design choice, but it's the only one that we 618224810Sjonathan * are satisfied guarantees the property we're looking for). 619154649Struckman * 1. Return an error if this is the last component of 620154649Struckman * the name and the operation is DELETE or RENAME. 621154649Struckman * 2. If at root directory (e.g. after chroot) 6221541Srgrimes * or at absolute root directory 6231541Srgrimes * then ignore it so can't get out. 624154649Struckman * 3. If this vnode is the root of a mounted 6251541Srgrimes * filesystem, then replace it with the 6261541Srgrimes * vnode which was mounted on so we take the 62796755Strhodes * .. in the other filesystem. 628154649Struckman * 4. If the vnode is the top directory of 62951649Sphk * the jail or chroot, don't let them out. 6301541Srgrimes */ 6311541Srgrimes if (cnp->cn_flags & ISDOTDOT) { 632224810Sjonathan if (ndp->ni_strictrelative != 0) { 633226495Sdes#ifdef KTRACE 634226495Sdes if (KTRPOINT(curthread, KTR_CAPFAIL)) 635226495Sdes ktrcapfail(CAPFAIL_LOOKUP, 0, 0); 636226495Sdes#endif 637224810Sjonathan error = ENOTCAPABLE; 638224810Sjonathan goto bad; 639224810Sjonathan } 640154649Struckman if ((cnp->cn_flags & ISLASTCN) != 0 && 641154649Struckman (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME)) { 642154690Struckman error = EINVAL; 643154649Struckman goto bad; 644154649Struckman } 6451541Srgrimes for (;;) { 646192895Sjamie for (pr = cnp->cn_cred->cr_prison; pr != NULL; 647192895Sjamie pr = pr->pr_parent) 648192895Sjamie if (dp == pr->pr_root) 649192895Sjamie break; 65051649Sphk if (dp == ndp->ni_rootdir || 65151649Sphk dp == ndp->ni_topdir || 652166744Skib dp == rootvnode || 653192895Sjamie pr != NULL || 654166744Skib ((dp->v_vflag & VV_ROOT) != 0 && 655166744Skib (cnp->cn_flags & NOCROSSMOUNT) != 0)) { 6561541Srgrimes ndp->ni_dvp = dp; 6571541Srgrimes ndp->ni_vp = dp; 6581541Srgrimes VREF(dp); 6591541Srgrimes goto nextname; 6601541Srgrimes } 661166744Skib if ((dp->v_vflag & VV_ROOT) == 0) 6621541Srgrimes break; 663155385Sjeff if (dp->v_iflag & VI_DOOMED) { /* forced unmount */ 664190387Sjhb error = ENOENT; 66569405Salfred goto bad; 66669405Salfred } 6671541Srgrimes tdp = dp; 668144833Sjeff dp = dp->v_mount->mnt_vnodecovered; 669144833Sjeff VREF(dp); 6701541Srgrimes vput(tdp); 671175202Sattilio vn_lock(dp, 672175202Sattilio compute_cn_lkflags(dp->v_mount, cnp->cn_lkflags | 673240283Skib LK_RETRY, ISDOTDOT)); 6741541Srgrimes } 6751541Srgrimes } 6761541Srgrimes 6771541Srgrimes /* 6781541Srgrimes * We now have a segment name to search for, and a directory to search. 6791541Srgrimes */ 6801541Srgrimesunionlookup: 681101127Srwatson#ifdef MAC 682105479Srwatson if ((cnp->cn_flags & NOMACCHECK) == 0) { 683191990Sattilio error = mac_vnode_check_lookup(cnp->cn_thread->td_ucred, dp, 684191990Sattilio cnp); 685105479Srwatson if (error) 686105479Srwatson goto bad; 687105479Srwatson } 688101127Srwatson#endif 6891541Srgrimes ndp->ni_dvp = dp; 69022521Sdyson ndp->ni_vp = NULL; 69124624Sdfr ASSERT_VOP_LOCKED(dp, "lookup"); 692144286Sjeff /* 693144286Sjeff * If we have a shared lock we may need to upgrade the lock for the 694144286Sjeff * last operation. 695144286Sjeff */ 696166167Skib if (dp != vp_crossmp && 697176559Sattilio VOP_ISLOCKED(dp) == LK_SHARED && 698144286Sjeff (cnp->cn_flags & ISLASTCN) && (cnp->cn_flags & LOCKPARENT)) 699175202Sattilio vn_lock(dp, LK_UPGRADE|LK_RETRY); 700144286Sjeff /* 701144286Sjeff * If we're looking up the last component and we need an exclusive 702144286Sjeff * lock, adjust our lkflags. 703144286Sjeff */ 704189696Sjhb if (needs_exclusive_leaf(dp->v_mount, cnp->cn_flags)) 705144286Sjeff cnp->cn_lkflags = LK_EXCLUSIVE; 706138345Sphk#ifdef NAMEI_DIAGNOSTIC 707138345Sphk vprint("lookup in", dp); 708138345Sphk#endif 709162288Smohans lkflags_save = cnp->cn_lkflags; 710240283Skib cnp->cn_lkflags = compute_cn_lkflags(dp->v_mount, cnp->cn_lkflags, 711240283Skib cnp->cn_flags); 71243301Sdillon if ((error = VOP_LOOKUP(dp, &ndp->ni_vp, cnp)) != 0) { 713162288Smohans cnp->cn_lkflags = lkflags_save; 71442408Seivind KASSERT(ndp->ni_vp == NULL, ("leaf should be empty")); 7151541Srgrimes#ifdef NAMEI_DIAGNOSTIC 7161541Srgrimes printf("not found\n"); 7171541Srgrimes#endif 7181541Srgrimes if ((error == ENOENT) && 719101308Sjeff (dp->v_vflag & VV_ROOT) && (dp->v_mount != NULL) && 7201541Srgrimes (dp->v_mount->mnt_flag & MNT_UNION)) { 7211541Srgrimes tdp = dp; 722144833Sjeff dp = dp->v_mount->mnt_vnodecovered; 723144833Sjeff VREF(dp); 724144203Sjeff vput(tdp); 725175202Sattilio vn_lock(dp, 726175202Sattilio compute_cn_lkflags(dp->v_mount, cnp->cn_lkflags | 727240283Skib LK_RETRY, cnp->cn_flags)); 7281541Srgrimes goto unionlookup; 7291541Srgrimes } 7301541Srgrimes 7311541Srgrimes if (error != EJUSTRETURN) 7321541Srgrimes goto bad; 7331541Srgrimes /* 734193557Sdes * At this point, we know we're at the end of the 735193557Sdes * pathname. If creating / renaming, we can consider 736193557Sdes * allowing the file or directory to be created / renamed, 737193557Sdes * provided we're not on a read-only filesystem. 7381541Srgrimes */ 73911644Sdg if (rdonly) { 7401541Srgrimes error = EROFS; 7411541Srgrimes goto bad; 7421541Srgrimes } 743193557Sdes /* trailing slash only allowed for directories */ 744193557Sdes if ((cnp->cn_flags & TRAILINGSLASH) && 745193557Sdes !(cnp->cn_flags & WILLBEDIR)) { 7469804Sbde error = ENOENT; 7479804Sbde goto bad; 7489804Sbde } 749144203Sjeff if ((cnp->cn_flags & LOCKPARENT) == 0) 750175294Sattilio VOP_UNLOCK(dp, 0); 7511541Srgrimes /* 7521541Srgrimes * We return with ni_vp NULL to indicate that the entry 7531541Srgrimes * doesn't currently exist, leaving a pointer to the 754161010Srwatson * (possibly locked) directory vnode in ndp->ni_dvp. 7551541Srgrimes */ 7561541Srgrimes if (cnp->cn_flags & SAVESTART) { 7571541Srgrimes ndp->ni_startdir = ndp->ni_dvp; 7581541Srgrimes VREF(ndp->ni_startdir); 7591541Srgrimes } 760140714Sjeff goto success; 761162288Smohans } else 762162288Smohans cnp->cn_lkflags = lkflags_save; 7631541Srgrimes#ifdef NAMEI_DIAGNOSTIC 7641541Srgrimes printf("found\n"); 7651541Srgrimes#endif 766144203Sjeff /* 7671541Srgrimes * Take into account any additional components consumed by 7681541Srgrimes * the underlying filesystem. 7691541Srgrimes */ 7701541Srgrimes if (cnp->cn_consume > 0) { 7711541Srgrimes cnp->cn_nameptr += cnp->cn_consume; 7721541Srgrimes ndp->ni_next += cnp->cn_consume; 7731541Srgrimes ndp->ni_pathlen -= cnp->cn_consume; 7741541Srgrimes cnp->cn_consume = 0; 7751541Srgrimes } 7761541Srgrimes 7771541Srgrimes dp = ndp->ni_vp; 7781541Srgrimes 7791541Srgrimes /* 7801541Srgrimes * Check to see if the vnode has been mounted on; 78196755Strhodes * if so find the root of the mounted filesystem. 7821541Srgrimes */ 7831541Srgrimes while (dp->v_type == VDIR && (mp = dp->v_mountedhere) && 7841541Srgrimes (cnp->cn_flags & NOCROSSMOUNT) == 0) { 785184554Sattilio if (vfs_busy(mp, 0)) 7861541Srgrimes continue; 787144833Sjeff vput(dp); 788158094Sjeff if (dp != ndp->ni_dvp) 789166167Skib vput(ndp->ni_dvp); 790166167Skib else 791166167Skib vrele(ndp->ni_dvp); 792166167Skib vref(vp_crossmp); 793166167Skib ndp->ni_dvp = vp_crossmp; 794240283Skib error = VFS_ROOT(mp, compute_cn_lkflags(mp, cnp->cn_lkflags, 795240283Skib cnp->cn_flags), &tdp); 796182542Sattilio vfs_unbusy(mp); 797175202Sattilio if (vn_lock(vp_crossmp, LK_SHARED | LK_NOWAIT)) 798166167Skib panic("vp_crossmp exclusively locked or reclaimed"); 79965805Sbp if (error) { 80065805Sbp dpunlocked = 1; 8011541Srgrimes goto bad2; 80265805Sbp } 8031541Srgrimes ndp->ni_vp = dp = tdp; 8041541Srgrimes } 8051541Srgrimes 80610219Sdfr /* 80710219Sdfr * Check for symbolic link 80810219Sdfr */ 80910219Sdfr if ((dp->v_type == VLNK) && 810193557Sdes ((cnp->cn_flags & FOLLOW) || (cnp->cn_flags & TRAILINGSLASH) || 81110219Sdfr *ndp->ni_next == '/')) { 81210219Sdfr cnp->cn_flags |= ISSYMLINK; 813155385Sjeff if (dp->v_iflag & VI_DOOMED) { 814190387Sjhb /* 815190387Sjhb * We can't know whether the directory was mounted with 816190387Sjhb * NOSYMFOLLOW, so we can't follow safely. 817190387Sjhb */ 818190387Sjhb error = ENOENT; 81969405Salfred goto bad2; 82069405Salfred } 82135105Swosch if (dp->v_mount->mnt_flag & MNT_NOSYMFOLLOW) { 82235105Swosch error = EACCES; 82335105Swosch goto bad2; 82435105Swosch } 825144833Sjeff /* 826144833Sjeff * Symlink code always expects an unlocked dvp. 827144833Sjeff */ 828229185Skib if (ndp->ni_dvp != ndp->ni_vp) { 829175294Sattilio VOP_UNLOCK(ndp->ni_dvp, 0); 830229185Skib ni_dvp_unlocked = 1; 831229185Skib } 832140714Sjeff goto success; 83310219Sdfr } 83410219Sdfr 8351541Srgrimesnextname: 8361541Srgrimes /* 837193557Sdes * Not a symbolic link that we will follow. Continue with the 838193557Sdes * next component if there is any; otherwise, we're done. 8391541Srgrimes */ 840144203Sjeff KASSERT((cnp->cn_flags & ISLASTCN) || *ndp->ni_next == '/', 841144203Sjeff ("lookup: invalid path state.")); 8421541Srgrimes if (*ndp->ni_next == '/') { 8431541Srgrimes cnp->cn_nameptr = ndp->ni_next; 8441541Srgrimes while (*cnp->cn_nameptr == '/') { 8451541Srgrimes cnp->cn_nameptr++; 8461541Srgrimes ndp->ni_pathlen--; 8471541Srgrimes } 848144833Sjeff if (ndp->ni_dvp != dp) 849144833Sjeff vput(ndp->ni_dvp); 850144833Sjeff else 851144833Sjeff vrele(ndp->ni_dvp); 8521541Srgrimes goto dirloop; 8531541Srgrimes } 8541541Srgrimes /* 855193028Sdes * If we're processing a path with a trailing slash, 856193028Sdes * check that the end result is a directory. 857193028Sdes */ 858193028Sdes if ((cnp->cn_flags & TRAILINGSLASH) && dp->v_type != VDIR) { 859193028Sdes error = ENOTDIR; 860193028Sdes goto bad2; 861193028Sdes } 862193028Sdes /* 86396755Strhodes * Disallow directory write attempts on read-only filesystems. 8641541Srgrimes */ 86511644Sdg if (rdonly && 86611644Sdg (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME)) { 86711644Sdg error = EROFS; 86811644Sdg goto bad2; 8691541Srgrimes } 8701541Srgrimes if (cnp->cn_flags & SAVESTART) { 8711541Srgrimes ndp->ni_startdir = ndp->ni_dvp; 8721541Srgrimes VREF(ndp->ni_startdir); 8731541Srgrimes } 874144833Sjeff if (!wantparent) { 875229185Skib ni_dvp_unlocked = 2; 876144833Sjeff if (ndp->ni_dvp != dp) 877144833Sjeff vput(ndp->ni_dvp); 878144833Sjeff else 879144833Sjeff vrele(ndp->ni_dvp); 880229185Skib } else if ((cnp->cn_flags & LOCKPARENT) == 0 && ndp->ni_dvp != dp) { 881175294Sattilio VOP_UNLOCK(ndp->ni_dvp, 0); 882229185Skib ni_dvp_unlocked = 1; 883229185Skib } 88432071Sdyson 885155334Srwatson if (cnp->cn_flags & AUDITVNODE1) 886195926Srwatson AUDIT_ARG_VNODE1(dp); 887155334Srwatson else if (cnp->cn_flags & AUDITVNODE2) 888195926Srwatson AUDIT_ARG_VNODE2(dp); 889155334Srwatson 8901541Srgrimes if ((cnp->cn_flags & LOCKLEAF) == 0) 891175294Sattilio VOP_UNLOCK(dp, 0); 892140714Sjeffsuccess: 893172274Spjd /* 894172274Spjd * Because of lookup_shared we may have the vnode shared locked, but 895172274Spjd * the caller may want it to be exclusively locked. 896172274Spjd */ 897189696Sjhb if (needs_exclusive_leaf(dp->v_mount, cnp->cn_flags) && 898189696Sjhb VOP_ISLOCKED(dp) != LK_EXCLUSIVE) { 899175202Sattilio vn_lock(dp, LK_UPGRADE | LK_RETRY); 900186276Skib if (dp->v_iflag & VI_DOOMED) { 901186276Skib error = ENOENT; 902186276Skib goto bad2; 903186276Skib } 904172274Spjd } 9051541Srgrimes return (0); 9061541Srgrimes 9071541Srgrimesbad2: 908229185Skib if (ni_dvp_unlocked != 2) { 909229185Skib if (dp != ndp->ni_dvp && !ni_dvp_unlocked) 910229185Skib vput(ndp->ni_dvp); 911229185Skib else 912229185Skib vrele(ndp->ni_dvp); 913229185Skib } 9141541Srgrimesbad: 915144833Sjeff if (!dpunlocked) 91665805Sbp vput(dp); 9171541Srgrimes ndp->ni_vp = NULL; 9181541Srgrimes return (error); 9191541Srgrimes} 9201541Srgrimes 9213148Sphk/* 9223148Sphk * relookup - lookup a path name component 923170035Srwatson * Used by lookup to re-acquire things. 9243148Sphk */ 9253148Sphkint 926161011Srwatsonrelookup(struct vnode *dvp, struct vnode **vpp, struct componentname *cnp) 9273148Sphk{ 92822521Sdyson struct vnode *dp = 0; /* the directory we are searching */ 9293148Sphk int wantparent; /* 1 => wantparent or lockparent flag */ 9303148Sphk int rdonly; /* lookup read-only flag bit */ 9313148Sphk int error = 0; 9321541Srgrimes 933144203Sjeff KASSERT(cnp->cn_flags & ISLASTCN, 934144203Sjeff ("relookup: Not given last component.")); 9353148Sphk /* 9363148Sphk * Setup: break out flag bits into variables. 9373148Sphk */ 9383148Sphk wantparent = cnp->cn_flags & (LOCKPARENT|WANTPARENT); 939145004Sjeff KASSERT(wantparent, ("relookup: parent not wanted.")); 9403148Sphk rdonly = cnp->cn_flags & RDONLY; 9413148Sphk cnp->cn_flags &= ~ISSYMLINK; 9423148Sphk dp = dvp; 943144286Sjeff cnp->cn_lkflags = LK_EXCLUSIVE; 944175202Sattilio vn_lock(dp, LK_EXCLUSIVE | LK_RETRY); 9453148Sphk 9463148Sphk /* 9473148Sphk * Search a new directory. 9483148Sphk * 9493148Sphk * The last component of the filename is left accessible via 9503148Sphk * cnp->cn_nameptr for callers that need the name. Callers needing 9513148Sphk * the name set the SAVENAME flag. When done, they assume 9523148Sphk * responsibility for freeing the pathname buffer. 9533148Sphk */ 9543148Sphk#ifdef NAMEI_DIAGNOSTIC 9553148Sphk printf("{%s}: ", cnp->cn_nameptr); 9563148Sphk#endif 9573148Sphk 9583148Sphk /* 959205682Sjh * Check for "" which represents the root directory after slash 960205682Sjh * removal. 9613148Sphk */ 9623148Sphk if (cnp->cn_nameptr[0] == '\0') { 963205682Sjh /* 964205682Sjh * Support only LOOKUP for "/" because lookup() 965205682Sjh * can't succeed for CREATE, DELETE and RENAME. 966205682Sjh */ 967205682Sjh KASSERT(cnp->cn_nameiop == LOOKUP, ("nameiop must be LOOKUP")); 968205682Sjh KASSERT(dp->v_type == VDIR, ("dp is not a directory")); 969205682Sjh 9703148Sphk if (!(cnp->cn_flags & LOCKLEAF)) 971175294Sattilio VOP_UNLOCK(dp, 0); 9723148Sphk *vpp = dp; 97354655Seivind /* XXX This should probably move to the top of function. */ 9743148Sphk if (cnp->cn_flags & SAVESTART) 9753148Sphk panic("lookup: SAVESTART"); 9763148Sphk return (0); 9773148Sphk } 9783148Sphk 9793148Sphk if (cnp->cn_flags & ISDOTDOT) 9803148Sphk panic ("relookup: lookup on dot-dot"); 9813148Sphk 9823148Sphk /* 9833148Sphk * We now have a segment name to search for, and a directory to search. 9843148Sphk */ 985138345Sphk#ifdef NAMEI_DIAGNOSTIC 986138345Sphk vprint("search in:", dp); 987138345Sphk#endif 98843311Sdillon if ((error = VOP_LOOKUP(dp, vpp, cnp)) != 0) { 98942408Seivind KASSERT(*vpp == NULL, ("leaf should be empty")); 9903148Sphk if (error != EJUSTRETURN) 9913148Sphk goto bad; 9923148Sphk /* 9933148Sphk * If creating and at end of pathname, then can consider 9943148Sphk * allowing file to be created. 9953148Sphk */ 99611644Sdg if (rdonly) { 9973148Sphk error = EROFS; 9983148Sphk goto bad; 9993148Sphk } 10003148Sphk /* ASSERT(dvp == ndp->ni_startdir) */ 10013148Sphk if (cnp->cn_flags & SAVESTART) 10023148Sphk VREF(dvp); 1003144203Sjeff if ((cnp->cn_flags & LOCKPARENT) == 0) 1004175294Sattilio VOP_UNLOCK(dp, 0); 10053148Sphk /* 10063148Sphk * We return with ni_vp NULL to indicate that the entry 10073148Sphk * doesn't currently exist, leaving a pointer to the 1008161010Srwatson * (possibly locked) directory vnode in ndp->ni_dvp. 10093148Sphk */ 10103148Sphk return (0); 10113148Sphk } 1012162288Smohans 10133148Sphk dp = *vpp; 10143148Sphk 10153148Sphk /* 101696755Strhodes * Disallow directory write attempts on read-only filesystems. 10173148Sphk */ 101811644Sdg if (rdonly && 101911644Sdg (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME)) { 1020145004Sjeff if (dvp == dp) 1021145004Sjeff vrele(dvp); 1022145004Sjeff else 1023145004Sjeff vput(dvp); 102411644Sdg error = EROFS; 1025145004Sjeff goto bad; 10263148Sphk } 1027145004Sjeff /* 1028145004Sjeff * Set the parent lock/ref state to the requested state. 1029145004Sjeff */ 1030145004Sjeff if ((cnp->cn_flags & LOCKPARENT) == 0 && dvp != dp) { 1031145004Sjeff if (wantparent) 1032175294Sattilio VOP_UNLOCK(dvp, 0); 1033145004Sjeff else 1034145004Sjeff vput(dvp); 1035145004Sjeff } else if (!wantparent) 1036145004Sjeff vrele(dvp); 1037145004Sjeff /* 1038145004Sjeff * Check for symbolic link 1039145004Sjeff */ 1040145004Sjeff KASSERT(dp->v_type != VLNK || !(cnp->cn_flags & FOLLOW), 1041145004Sjeff ("relookup: symlink found.\n")); 1042145004Sjeff 10433148Sphk /* ASSERT(dvp == ndp->ni_startdir) */ 10443148Sphk if (cnp->cn_flags & SAVESTART) 10453148Sphk VREF(dvp); 104622521Sdyson 10473148Sphk if ((cnp->cn_flags & LOCKLEAF) == 0) 1048175294Sattilio VOP_UNLOCK(dp, 0); 10493148Sphk return (0); 10503148Sphkbad: 10513148Sphk vput(dp); 10523148Sphk *vpp = NULL; 10533148Sphk return (error); 10543148Sphk} 1055141471Sjhb 1056141471Sjhb/* 1057144661Sjeff * Free data allocated by namei(); see namei(9) for details. 1058144661Sjeff */ 1059144661Sjeffvoid 1060161011SrwatsonNDFREE(struct nameidata *ndp, const u_int flags) 1061144661Sjeff{ 1062144833Sjeff int unlock_dvp; 1063144833Sjeff int unlock_vp; 1064144661Sjeff 1065144833Sjeff unlock_dvp = 0; 1066144833Sjeff unlock_vp = 0; 1067144833Sjeff 1068144661Sjeff if (!(flags & NDF_NO_FREE_PNBUF) && 1069144661Sjeff (ndp->ni_cnd.cn_flags & HASBUF)) { 1070144661Sjeff uma_zfree(namei_zone, ndp->ni_cnd.cn_pnbuf); 1071144661Sjeff ndp->ni_cnd.cn_flags &= ~HASBUF; 1072144661Sjeff } 1073144833Sjeff if (!(flags & NDF_NO_VP_UNLOCK) && 1074144833Sjeff (ndp->ni_cnd.cn_flags & LOCKLEAF) && ndp->ni_vp) 1075144833Sjeff unlock_vp = 1; 1076144833Sjeff if (!(flags & NDF_NO_VP_RELE) && ndp->ni_vp) { 1077144833Sjeff if (unlock_vp) { 1078144833Sjeff vput(ndp->ni_vp); 1079144833Sjeff unlock_vp = 0; 1080144833Sjeff } else 1081144833Sjeff vrele(ndp->ni_vp); 1082144833Sjeff ndp->ni_vp = NULL; 1083144833Sjeff } 1084144833Sjeff if (unlock_vp) 1085175294Sattilio VOP_UNLOCK(ndp->ni_vp, 0); 1086144661Sjeff if (!(flags & NDF_NO_DVP_UNLOCK) && 1087144661Sjeff (ndp->ni_cnd.cn_flags & LOCKPARENT) && 1088144661Sjeff ndp->ni_dvp != ndp->ni_vp) 1089144833Sjeff unlock_dvp = 1; 1090144661Sjeff if (!(flags & NDF_NO_DVP_RELE) && 1091144661Sjeff (ndp->ni_cnd.cn_flags & (LOCKPARENT|WANTPARENT))) { 1092144833Sjeff if (unlock_dvp) { 1093144833Sjeff vput(ndp->ni_dvp); 1094144833Sjeff unlock_dvp = 0; 1095144833Sjeff } else 1096144833Sjeff vrele(ndp->ni_dvp); 1097144661Sjeff ndp->ni_dvp = NULL; 1098144661Sjeff } 1099144833Sjeff if (unlock_dvp) 1100175294Sattilio VOP_UNLOCK(ndp->ni_dvp, 0); 1101144661Sjeff if (!(flags & NDF_NO_STARTDIR_RELE) && 1102144661Sjeff (ndp->ni_cnd.cn_flags & SAVESTART)) { 1103144661Sjeff vrele(ndp->ni_startdir); 1104144661Sjeff ndp->ni_startdir = NULL; 1105144661Sjeff } 1106144661Sjeff} 1107144661Sjeff 1108144661Sjeff/* 1109141471Sjhb * Determine if there is a suitable alternate filename under the specified 1110141471Sjhb * prefix for the specified path. If the create flag is set, then the 1111141471Sjhb * alternate prefix will be used so long as the parent directory exists. 1112141471Sjhb * This is used by the various compatiblity ABIs so that Linux binaries prefer 1113141471Sjhb * files under /compat/linux for example. The chosen path (whether under 1114141471Sjhb * the prefix or under /) is returned in a kernel malloc'd buffer pointed 1115141471Sjhb * to by pathbuf. The caller is responsible for free'ing the buffer from 1116141471Sjhb * the M_TEMP bucket if one is returned. 1117141471Sjhb */ 1118141471Sjhbint 1119177997Skibkern_alternate_path(struct thread *td, const char *prefix, const char *path, 1120177997Skib enum uio_seg pathseg, char **pathbuf, int create, int dirfd) 1121141471Sjhb{ 1122141471Sjhb struct nameidata nd, ndroot; 1123141471Sjhb char *ptr, *buf, *cp; 1124141471Sjhb size_t len, sz; 1125141471Sjhb int error; 1126141471Sjhb 1127141471Sjhb buf = (char *) malloc(MAXPATHLEN, M_TEMP, M_WAITOK); 1128141471Sjhb *pathbuf = buf; 1129141471Sjhb 1130141471Sjhb /* Copy the prefix into the new pathname as a starting point. */ 1131141471Sjhb len = strlcpy(buf, prefix, MAXPATHLEN); 1132141471Sjhb if (len >= MAXPATHLEN) { 1133141471Sjhb *pathbuf = NULL; 1134141471Sjhb free(buf, M_TEMP); 1135141471Sjhb return (EINVAL); 1136141471Sjhb } 1137141471Sjhb sz = MAXPATHLEN - len; 1138141471Sjhb ptr = buf + len; 1139141471Sjhb 1140141471Sjhb /* Append the filename to the prefix. */ 1141141471Sjhb if (pathseg == UIO_SYSSPACE) 1142141471Sjhb error = copystr(path, ptr, sz, &len); 1143141471Sjhb else 1144141471Sjhb error = copyinstr(path, ptr, sz, &len); 1145141471Sjhb 1146141471Sjhb if (error) { 1147141471Sjhb *pathbuf = NULL; 1148141471Sjhb free(buf, M_TEMP); 1149141471Sjhb return (error); 1150141471Sjhb } 1151141471Sjhb 1152141471Sjhb /* Only use a prefix with absolute pathnames. */ 1153141471Sjhb if (*ptr != '/') { 1154141471Sjhb error = EINVAL; 1155141471Sjhb goto keeporig; 1156141471Sjhb } 1157141471Sjhb 1158177997Skib if (dirfd != AT_FDCWD) { 1159177997Skib /* 1160177997Skib * We want the original because the "prefix" is 1161177997Skib * included in the already opened dirfd. 1162177997Skib */ 1163177997Skib bcopy(ptr, buf, len); 1164177997Skib return (0); 1165177997Skib } 1166177997Skib 1167141471Sjhb /* 1168141471Sjhb * We know that there is a / somewhere in this pathname. 1169141471Sjhb * Search backwards for it, to find the file's parent dir 1170141471Sjhb * to see if it exists in the alternate tree. If it does, 1171141471Sjhb * and we want to create a file (cflag is set). We don't 1172141471Sjhb * need to worry about the root comparison in this case. 1173141471Sjhb */ 1174141471Sjhb 1175141471Sjhb if (create) { 1176141471Sjhb for (cp = &ptr[len] - 1; *cp != '/'; cp--); 1177141471Sjhb *cp = '\0'; 1178141471Sjhb 1179241896Skib NDINIT(&nd, LOOKUP, FOLLOW, UIO_SYSSPACE, buf, td); 1180141471Sjhb error = namei(&nd); 1181141471Sjhb *cp = '/'; 1182141471Sjhb if (error != 0) 1183150431Sjhb goto keeporig; 1184141471Sjhb } else { 1185241896Skib NDINIT(&nd, LOOKUP, FOLLOW, UIO_SYSSPACE, buf, td); 1186141471Sjhb 1187141471Sjhb error = namei(&nd); 1188141471Sjhb if (error != 0) 1189150431Sjhb goto keeporig; 1190141471Sjhb 1191141471Sjhb /* 1192141471Sjhb * We now compare the vnode of the prefix to the one 1193141471Sjhb * vnode asked. If they resolve to be the same, then we 1194141471Sjhb * ignore the match so that the real root gets used. 1195141471Sjhb * This avoids the problem of traversing "../.." to find the 1196141471Sjhb * root directory and never finding it, because "/" resolves 1197141471Sjhb * to the emulation root directory. This is expensive :-( 1198141471Sjhb */ 1199241896Skib NDINIT(&ndroot, LOOKUP, FOLLOW, UIO_SYSSPACE, prefix, 1200150431Sjhb td); 1201141471Sjhb 1202141471Sjhb /* We shouldn't ever get an error from this namei(). */ 1203141471Sjhb error = namei(&ndroot); 1204141471Sjhb if (error == 0) { 1205141471Sjhb if (nd.ni_vp == ndroot.ni_vp) 1206141471Sjhb error = ENOENT; 1207141471Sjhb 1208141471Sjhb NDFREE(&ndroot, NDF_ONLY_PNBUF); 1209141471Sjhb vrele(ndroot.ni_vp); 1210141471Sjhb } 1211141471Sjhb } 1212141471Sjhb 1213141471Sjhb NDFREE(&nd, NDF_ONLY_PNBUF); 1214141471Sjhb vrele(nd.ni_vp); 1215141471Sjhb 1216141471Sjhbkeeporig: 1217141471Sjhb /* If there was an error, use the original path name. */ 1218141471Sjhb if (error) 1219141471Sjhb bcopy(ptr, buf, len); 1220141471Sjhb return (error); 1221141471Sjhb} 1222