Home | History | Annotate | Line # | Download | only in kern
vfs_lookup.c revision 1.11
      1  1.10  cgd /*
      2  1.10  cgd  * Copyright (c) 1982, 1986, 1989 Regents of the University of California.
      3  1.10  cgd  * All rights reserved.
      4  1.10  cgd  * (c) UNIX System Laboratories, Inc.
      5  1.10  cgd  * All or some portions of this file are derived from material licensed
      6  1.10  cgd  * to the University of California by American Telephone and Telegraph
      7  1.10  cgd  * Co. or Unix System Laboratories, Inc. and are reproduced herein with
      8  1.10  cgd  * the permission of UNIX System Laboratories, Inc.
      9  1.10  cgd  *
     10  1.10  cgd  * Redistribution and use in source and binary forms, with or without
     11  1.10  cgd  * modification, are permitted provided that the following conditions
     12  1.10  cgd  * are met:
     13  1.10  cgd  * 1. Redistributions of source code must retain the above copyright
     14  1.10  cgd  *    notice, this list of conditions and the following disclaimer.
     15  1.10  cgd  * 2. Redistributions in binary form must reproduce the above copyright
     16  1.10  cgd  *    notice, this list of conditions and the following disclaimer in the
     17  1.10  cgd  *    documentation and/or other materials provided with the distribution.
     18  1.10  cgd  * 3. All advertising materials mentioning features or use of this software
     19  1.10  cgd  *    must display the following acknowledgement:
     20  1.10  cgd  *	This product includes software developed by the University of
     21  1.10  cgd  *	California, Berkeley and its contributors.
     22  1.10  cgd  * 4. Neither the name of the University nor the names of its contributors
     23  1.10  cgd  *    may be used to endorse or promote products derived from this software
     24  1.10  cgd  *    without specific prior written permission.
     25  1.10  cgd  *
     26  1.10  cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     27  1.10  cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     28  1.10  cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     29  1.10  cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     30  1.10  cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     31  1.10  cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     32  1.10  cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     33  1.10  cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     34  1.10  cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     35  1.10  cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     36  1.10  cgd  * SUCH DAMAGE.
     37  1.10  cgd  *
     38  1.10  cgd  *	from: @(#)vfs_lookup.c	7.32 (Berkeley) 5/21/91
     39  1.11  cgd  *	$Id: vfs_lookup.c,v 1.11 1994/05/18 05:12:44 cgd Exp $
     40  1.10  cgd  */
     41  1.10  cgd 
     42  1.10  cgd #include <sys/param.h>
     43  1.10  cgd #include <sys/syslimits.h>
     44  1.10  cgd #include <sys/time.h>
     45  1.10  cgd #include <sys/namei.h>
     46  1.10  cgd #include <sys/vnode.h>
     47  1.10  cgd #include <sys/mount.h>
     48  1.10  cgd #include <sys/errno.h>
     49  1.10  cgd #include <sys/malloc.h>
     50  1.10  cgd #include <sys/filedesc.h>
     51  1.10  cgd #include <sys/proc.h>
     52  1.10  cgd #ifdef KTRACE
     53  1.10  cgd #include <sys/ktrace.h>
     54  1.10  cgd #endif
     55  1.10  cgd 
     56  1.10  cgd /*
     57  1.10  cgd  * Convert a pathname into a pointer to a locked inode.
     58  1.10  cgd  *
     59  1.10  cgd  * The FOLLOW flag is set when symbolic links are to be followed
     60  1.10  cgd  * when they occur at the end of the name translation process.
     61  1.10  cgd  * Symbolic links are always followed for all other pathname
     62  1.10  cgd  * components other than the last.
     63  1.10  cgd  *
     64  1.10  cgd  * The segflg defines whether the name is to be copied from user
     65  1.10  cgd  * space or kernel space.
     66  1.10  cgd  *
     67  1.10  cgd  * Overall outline of namei:
     68  1.10  cgd  *
     69  1.10  cgd  *	copy in name
     70  1.10  cgd  *	get starting directory
     71  1.10  cgd  *	while (!done && !error) {
     72  1.10  cgd  *		call lookup to search path.
     73  1.10  cgd  *		if symbolic link, massage name in buffer and continue
     74  1.10  cgd  *	}
     75  1.10  cgd  */
     76  1.10  cgd namei(ndp, p)
     77  1.10  cgd 	register struct nameidata *ndp;
     78  1.10  cgd 	struct proc *p;
     79  1.10  cgd {
     80  1.10  cgd 	register struct filedesc *fdp;	/* pointer to file descriptor state */
     81  1.10  cgd 	register char *cp;		/* pointer into pathname argument */
     82  1.10  cgd 	register struct vnode *dp;	/* the directory we are searching */
     83  1.10  cgd 	struct iovec aiov;		/* uio for reading symbolic links */
     84  1.10  cgd 	struct uio auio;
     85  1.10  cgd 	int error, linklen;
     86  1.10  cgd 
     87  1.10  cgd 	ndp->ni_cred = p->p_ucred;
     88  1.10  cgd 	fdp = p->p_fd;
     89  1.10  cgd 
     90  1.10  cgd 	/*
     91  1.10  cgd 	 * Get a buffer for the name to be translated, and copy the
     92  1.10  cgd 	 * name into the buffer.
     93  1.10  cgd 	 */
     94  1.10  cgd 	if ((ndp->ni_nameiop & HASBUF) == 0)
     95  1.10  cgd 		MALLOC(ndp->ni_pnbuf, caddr_t, MAXPATHLEN, M_NAMEI, M_WAITOK);
     96  1.10  cgd 	if (ndp->ni_segflg == UIO_SYSSPACE)
     97  1.10  cgd 		error = copystr(ndp->ni_dirp, ndp->ni_pnbuf,
     98  1.10  cgd 			    MAXPATHLEN, &ndp->ni_pathlen);
     99  1.10  cgd 	else
    100  1.10  cgd 		error = copyinstr(ndp->ni_dirp, ndp->ni_pnbuf,
    101  1.10  cgd 			    MAXPATHLEN, &ndp->ni_pathlen);
    102  1.10  cgd 	if (error) {
    103  1.10  cgd 		free(ndp->ni_pnbuf, M_NAMEI);
    104  1.10  cgd 		ndp->ni_vp = NULL;
    105  1.10  cgd 		return (error);
    106  1.10  cgd 	}
    107  1.10  cgd 	ndp->ni_loopcnt = 0;
    108  1.10  cgd #ifdef KTRACE
    109  1.10  cgd 	if (KTRPOINT(p, KTR_NAMEI))
    110  1.10  cgd 		ktrnamei(p->p_tracep, ndp->ni_pnbuf);
    111  1.10  cgd #endif
    112  1.10  cgd 
    113  1.10  cgd 	/*
    114  1.10  cgd 	 * Get starting point for the translation.
    115  1.10  cgd 	 */
    116  1.10  cgd 	if ((ndp->ni_rootdir = fdp->fd_rdir) == NULL)
    117  1.11  cgd 		ndp->ni_rootdir = rootvnode;
    118  1.10  cgd 	dp = fdp->fd_cdir;
    119  1.10  cgd 	VREF(dp);
    120  1.10  cgd 	for (;;) {
    121  1.10  cgd 		/*
    122  1.10  cgd 		 * Check if root directory should replace current directory.
    123  1.10  cgd 		 * Done at start of translation and after symbolic link.
    124  1.10  cgd 		 */
    125  1.10  cgd 		ndp->ni_ptr = ndp->ni_pnbuf;
    126  1.10  cgd 		if (*ndp->ni_ptr == '/') {
    127  1.10  cgd 			vrele(dp);
    128  1.10  cgd 			while (*ndp->ni_ptr == '/') {
    129  1.10  cgd 				ndp->ni_ptr++;
    130  1.10  cgd 				ndp->ni_pathlen--;
    131  1.10  cgd 			}
    132  1.10  cgd 			dp = ndp->ni_rootdir;
    133  1.10  cgd 			VREF(dp);
    134  1.10  cgd 		}
    135  1.10  cgd 		ndp->ni_startdir = dp;
    136  1.10  cgd 		if (error = lookup(ndp, p)) {
    137  1.10  cgd 			FREE(ndp->ni_pnbuf, M_NAMEI);
    138  1.10  cgd 			return (error);
    139  1.10  cgd 		}
    140  1.10  cgd 		/*
    141  1.10  cgd 		 * Check for symbolic link
    142  1.10  cgd 		 */
    143  1.10  cgd 		if (ndp->ni_more == 0) {
    144  1.10  cgd 			if ((ndp->ni_nameiop & (SAVENAME | SAVESTART)) == 0)
    145  1.10  cgd 				FREE(ndp->ni_pnbuf, M_NAMEI);
    146  1.10  cgd 			else
    147  1.10  cgd 				ndp->ni_nameiop |= HASBUF;
    148  1.10  cgd 			return (0);
    149  1.10  cgd 		}
    150  1.10  cgd 		if ((ndp->ni_nameiop & LOCKPARENT) && ndp->ni_pathlen == 1)
    151  1.10  cgd 			VOP_UNLOCK(ndp->ni_dvp);
    152  1.10  cgd 		if (ndp->ni_loopcnt++ >= MAXSYMLINKS) {
    153  1.10  cgd 			error = ELOOP;
    154  1.10  cgd 			break;
    155  1.10  cgd 		}
    156  1.10  cgd 		if (ndp->ni_pathlen > 1)
    157  1.10  cgd 			MALLOC(cp, char *, MAXPATHLEN, M_NAMEI, M_WAITOK);
    158  1.10  cgd 		else
    159  1.10  cgd 			cp = ndp->ni_pnbuf;
    160  1.10  cgd 		aiov.iov_base = cp;
    161  1.10  cgd 		aiov.iov_len = MAXPATHLEN;
    162  1.10  cgd 		auio.uio_iov = &aiov;
    163  1.10  cgd 		auio.uio_iovcnt = 1;
    164  1.10  cgd 		auio.uio_offset = 0;
    165  1.10  cgd 		auio.uio_rw = UIO_READ;
    166  1.10  cgd 		auio.uio_segflg = UIO_SYSSPACE;
    167  1.10  cgd 		auio.uio_procp = (struct proc *)0;
    168  1.10  cgd 		auio.uio_resid = MAXPATHLEN;
    169  1.10  cgd 		if (error = VOP_READLINK(ndp->ni_vp, &auio, p->p_ucred)) {
    170  1.10  cgd 			if (ndp->ni_pathlen > 1)
    171  1.10  cgd 				free(cp, M_NAMEI);
    172  1.10  cgd 			break;
    173  1.10  cgd 		}
    174  1.10  cgd 		linklen = MAXPATHLEN - auio.uio_resid;
    175  1.10  cgd 		if (linklen + ndp->ni_pathlen >= MAXPATHLEN) {
    176  1.10  cgd 			if (ndp->ni_pathlen > 1)
    177  1.10  cgd 				free(cp, M_NAMEI);
    178  1.10  cgd 			error = ENAMETOOLONG;
    179  1.10  cgd 			break;
    180  1.10  cgd 		}
    181  1.10  cgd 		if (ndp->ni_pathlen > 1) {
    182  1.10  cgd 			bcopy(ndp->ni_next, cp + linklen, ndp->ni_pathlen);
    183  1.10  cgd 			FREE(ndp->ni_pnbuf, M_NAMEI);
    184  1.10  cgd 			ndp->ni_pnbuf = cp;
    185  1.10  cgd 		} else
    186  1.10  cgd 			ndp->ni_pnbuf[linklen] = '\0';
    187  1.10  cgd 		ndp->ni_pathlen += linklen;
    188  1.10  cgd 		vput(ndp->ni_vp);
    189  1.10  cgd 		dp = ndp->ni_dvp;
    190  1.10  cgd 	}
    191  1.10  cgd 	FREE(ndp->ni_pnbuf, M_NAMEI);
    192  1.10  cgd 	vrele(ndp->ni_dvp);
    193  1.10  cgd 	vput(ndp->ni_vp);
    194  1.10  cgd 	ndp->ni_vp = NULL;
    195  1.10  cgd 	return (error);
    196  1.10  cgd }
    197  1.10  cgd 
    198  1.10  cgd /*
    199  1.10  cgd  * Search a pathname.
    200  1.10  cgd  * This is a very central and rather complicated routine.
    201  1.10  cgd  *
    202  1.10  cgd  * The pathname is pointed to by ni_ptr and is of length ni_pathlen.
    203  1.10  cgd  * The starting directory is taken from ni_startdir. The pathname is
    204  1.10  cgd  * descended until done, or a symbolic link is encountered. The variable
    205  1.10  cgd  * ni_more is clear if the path is completed; it is set to one if a
    206  1.10  cgd  * symbolic link needing interpretation is encountered.
    207  1.10  cgd  *
    208  1.10  cgd  * The flag argument is LOOKUP, CREATE, RENAME, or DELETE depending on
    209  1.10  cgd  * whether the name is to be looked up, created, renamed, or deleted.
    210  1.10  cgd  * When CREATE, RENAME, or DELETE is specified, information usable in
    211  1.10  cgd  * creating, renaming, or deleting a directory entry may be calculated.
    212  1.10  cgd  * If flag has LOCKPARENT or'ed into it, the parent directory is returned
    213  1.10  cgd  * locked. If flag has WANTPARENT or'ed into it, the parent directory is
    214  1.10  cgd  * returned unlocked. Otherwise the parent directory is not returned. If
    215  1.10  cgd  * the target of the pathname exists and LOCKLEAF is or'ed into the flag
    216  1.10  cgd  * the target is returned locked, otherwise it is returned unlocked.
    217  1.10  cgd  * When creating or renaming and LOCKPARENT is specified, the target may not
    218  1.10  cgd  * be ".".  When deleting and LOCKPARENT is specified, the target may be ".".
    219  1.10  cgd  * NOTE: (LOOKUP | LOCKPARENT) currently returns the parent vnode unlocked.
    220  1.10  cgd  *
    221  1.10  cgd  * Overall outline of lookup:
    222  1.10  cgd  *
    223  1.10  cgd  * dirloop:
    224  1.10  cgd  *	identify next component of name at ndp->ni_ptr
    225  1.10  cgd  *	handle degenerate case where name is null string
    226  1.10  cgd  *	if .. and crossing mount points and on mounted filesys, find parent
    227  1.10  cgd  *	call VOP_LOOKUP routine for next component name
    228  1.10  cgd  *	    directory vnode returned in ni_dvp, unlocked unless LOCKPARENT set
    229  1.10  cgd  *	    component vnode returned in ni_vp (if it exists), locked.
    230  1.10  cgd  *	if result vnode is mounted on and crossing mount points,
    231  1.10  cgd  *	    find mounted on vnode
    232  1.10  cgd  *	if more components of name, do next level at dirloop
    233  1.10  cgd  *	return the answer in ni_vp, locked if LOCKLEAF set
    234  1.10  cgd  *	    if LOCKPARENT set, return locked parent in ni_dvp
    235  1.10  cgd  *	    if WANTPARENT set, return unlocked parent in ni_dvp
    236  1.10  cgd  */
    237  1.10  cgd lookup(ndp, p)
    238  1.10  cgd 	register struct nameidata *ndp;
    239  1.10  cgd 	struct proc *p;
    240  1.10  cgd {
    241  1.10  cgd 	register char *cp;		/* pointer into pathname argument */
    242  1.10  cgd 	register struct vnode *dp = 0;	/* the directory we are searching */
    243  1.10  cgd 	struct vnode *tdp;		/* saved dp */
    244  1.10  cgd 	struct mount *mp;		/* mount table entry */
    245  1.10  cgd 	int docache;			/* == 0 do not cache last component */
    246  1.10  cgd 	int flag;			/* LOOKUP, CREATE, RENAME or DELETE */
    247  1.10  cgd 	int wantparent;			/* 1 => wantparent or lockparent flag */
    248  1.10  cgd 	int rdonly;			/* mounted read-only flag bit(s) */
    249  1.10  cgd 	int error = 0;
    250  1.10  cgd 
    251  1.10  cgd 	/*
    252  1.10  cgd 	 * Setup: break out flag bits into variables.
    253  1.10  cgd 	 */
    254  1.10  cgd 	flag = ndp->ni_nameiop & OPMASK;
    255  1.10  cgd 	wantparent = ndp->ni_nameiop & (LOCKPARENT|WANTPARENT);
    256  1.10  cgd 	docache = (ndp->ni_nameiop & NOCACHE) ^ NOCACHE;
    257  1.10  cgd 	if (flag == DELETE || (wantparent && flag != CREATE))
    258  1.10  cgd 		docache = 0;
    259  1.10  cgd 	rdonly = MNT_RDONLY;
    260  1.10  cgd 	if (ndp->ni_nameiop & REMOTE)
    261  1.10  cgd 		rdonly |= MNT_EXRDONLY;
    262  1.10  cgd 	ndp->ni_dvp = NULL;
    263  1.10  cgd 	ndp->ni_more = 0;
    264  1.10  cgd 	dp = ndp->ni_startdir;
    265  1.10  cgd 	ndp->ni_startdir = NULLVP;
    266  1.10  cgd 	VOP_LOCK(dp);
    267  1.10  cgd 
    268  1.10  cgd dirloop:
    269  1.10  cgd 	/*
    270  1.10  cgd 	 * Search a new directory.
    271  1.10  cgd 	 *
    272  1.10  cgd 	 * The ni_hash value is for use by vfs_cache.
    273  1.10  cgd 	 * The last component of the filename is left accessible via
    274  1.10  cgd 	 * ndp->ptr for callers that need the name. Callers needing
    275  1.10  cgd 	 * the name set the SAVENAME flag. When done, they assume
    276  1.10  cgd 	 * responsibility for freeing the pathname buffer.
    277  1.10  cgd 	 */
    278  1.10  cgd 	ndp->ni_hash = 0;
    279  1.10  cgd 	for (cp = ndp->ni_ptr; *cp != 0 && *cp != '/'; cp++)
    280  1.10  cgd 		ndp->ni_hash += (unsigned char)*cp;
    281  1.10  cgd 	ndp->ni_hash ^= dp->v_id;
    282  1.10  cgd 	ndp->ni_namelen = cp - ndp->ni_ptr;
    283  1.10  cgd 	if (ndp->ni_namelen >= NAME_MAX) {
    284  1.10  cgd 		error = ENAMETOOLONG;
    285  1.10  cgd 		goto bad;
    286  1.10  cgd 	}
    287  1.10  cgd #ifdef NAMEI_DIAGNOSTIC
    288  1.10  cgd 	{ char c = *cp;
    289  1.10  cgd 	*cp = '\0';
    290  1.10  cgd 	printf("{%s}: ", ndp->ni_ptr);
    291  1.10  cgd 	*cp = c; }
    292  1.10  cgd #endif
    293  1.10  cgd 	ndp->ni_pathlen -= ndp->ni_namelen;
    294  1.10  cgd 	ndp->ni_next = cp;
    295  1.10  cgd 	ndp->ni_makeentry = 1;
    296  1.10  cgd 	if (*cp == '\0' && docache == 0)
    297  1.10  cgd 		ndp->ni_makeentry = 0;
    298  1.10  cgd 	ndp->ni_isdotdot = (ndp->ni_namelen == 2 &&
    299  1.10  cgd 		ndp->ni_ptr[1] == '.' && ndp->ni_ptr[0] == '.');
    300  1.10  cgd 
    301  1.10  cgd 	/*
    302  1.10  cgd 	 * Check for degenerate name (e.g. / or "")
    303  1.10  cgd 	 * which is a way of talking about a directory,
    304  1.10  cgd 	 * e.g. like "/." or ".".
    305  1.10  cgd 	 */
    306  1.10  cgd 	if (ndp->ni_ptr[0] == '\0') {
    307  1.10  cgd 		if (flag != LOOKUP || wantparent) {
    308  1.10  cgd 			error = EISDIR;
    309  1.10  cgd 			goto bad;
    310  1.10  cgd 		}
    311  1.10  cgd 		if (dp->v_type != VDIR) {
    312  1.10  cgd 			error = ENOTDIR;
    313  1.10  cgd 			goto bad;
    314  1.10  cgd 		}
    315  1.10  cgd 		if (!(ndp->ni_nameiop & LOCKLEAF))
    316  1.10  cgd 			VOP_UNLOCK(dp);
    317  1.10  cgd 		ndp->ni_vp = dp;
    318  1.10  cgd 		if (ndp->ni_nameiop & SAVESTART)
    319  1.10  cgd 			panic("lookup: SAVESTART");
    320  1.10  cgd 		return (0);
    321  1.10  cgd 	}
    322  1.10  cgd 
    323  1.10  cgd 	/*
    324  1.10  cgd 	 * Handle "..": two special cases.
    325  1.10  cgd 	 * 1. If at root directory (e.g. after chroot)
    326  1.10  cgd 	 *    then ignore it so can't get out.
    327  1.10  cgd 	 * 2. If this vnode is the root of a mounted
    328  1.10  cgd 	 *    filesystem, then replace it with the
    329  1.10  cgd 	 *    vnode which was mounted on so we take the
    330  1.10  cgd 	 *    .. in the other file system.
    331  1.10  cgd 	 */
    332  1.10  cgd 	if (ndp->ni_isdotdot) {
    333  1.10  cgd 		for (;;) {
    334  1.11  cgd 			if ((dp == ndp->ni_rootdir) || (dp == rootvnode)) {
    335  1.10  cgd 				ndp->ni_dvp = dp;
    336  1.10  cgd 				ndp->ni_vp = dp;
    337  1.10  cgd 				VREF(dp);
    338  1.10  cgd 				goto nextname;
    339  1.10  cgd 			}
    340  1.10  cgd 			if ((dp->v_flag & VROOT) == 0 ||
    341  1.10  cgd 			    (ndp->ni_nameiop & NOCROSSMOUNT))
    342  1.10  cgd 				break;
    343  1.10  cgd 			tdp = dp;
    344  1.10  cgd 			dp = dp->v_mount->mnt_vnodecovered;
    345  1.10  cgd 			vput(tdp);
    346  1.10  cgd 			VREF(dp);
    347  1.10  cgd 			VOP_LOCK(dp);
    348  1.10  cgd 		}
    349  1.10  cgd 	}
    350  1.10  cgd 
    351  1.10  cgd 	/*
    352  1.10  cgd 	 * We now have a segment name to search for, and a directory to search.
    353  1.10  cgd 	 * The filesystem will return EJUSTRETURN on the lookup if the file
    354  1.10  cgd 	 * doesn't exist, but can be created.
    355  1.10  cgd 	 */
    356  1.10  cgd relookup:
    357  1.10  cgd 	if (error = VOP_LOOKUP(dp, ndp, p)) {
    358  1.10  cgd #ifdef DIAGNOSTIC
    359  1.10  cgd 		if (ndp->ni_vp != NULL)
    360  1.10  cgd 			panic("leaf should be empty");
    361  1.10  cgd #endif
    362  1.10  cgd #ifdef NAMEI_DIAGNOSTIC
    363  1.10  cgd 		printf("not found\n");
    364  1.10  cgd #endif
    365  1.10  cgd 		if (error == ENOENT &&
    366  1.10  cgd 		    (dp->v_flag & VROOT) &&
    367  1.10  cgd 		    (dp->v_mount->mnt_flag & MNT_UNION)) {
    368  1.10  cgd 			tdp = dp;
    369  1.10  cgd 			dp = dp->v_mount->mnt_vnodecovered;
    370  1.10  cgd 			vput(tdp);
    371  1.10  cgd 			VREF(dp);
    372  1.10  cgd 			VOP_LOCK(dp);
    373  1.10  cgd 			goto relookup;
    374  1.10  cgd 		}
    375  1.10  cgd 		if (error != EJUSTRETURN)
    376  1.10  cgd 			goto bad;
    377  1.10  cgd 
    378  1.10  cgd 		/*
    379  1.10  cgd 		 * If creating and at end of pathname, then can consider
    380  1.10  cgd 		 * allowing file to be created.
    381  1.10  cgd 		 */
    382  1.10  cgd 		if (ndp->ni_dvp->v_mount->mnt_flag & rdonly) {
    383  1.10  cgd 			error = EROFS;
    384  1.10  cgd 			goto bad;
    385  1.10  cgd 		}
    386  1.10  cgd 		/*
    387  1.10  cgd 		 * We return with ni_vp NULL to indicate that the entry
    388  1.10  cgd 		 * doesn't currently exist, leaving a pointer to the
    389  1.10  cgd 		 * (possibly locked) directory inode in ndp->ni_dvp.
    390  1.10  cgd 		 */
    391  1.10  cgd 		if (ndp->ni_nameiop & SAVESTART) {
    392  1.10  cgd 			ndp->ni_startdir = ndp->ni_dvp;
    393  1.10  cgd 			VREF(ndp->ni_startdir);
    394  1.10  cgd 		}
    395  1.10  cgd 		return (0);
    396  1.10  cgd 	}
    397  1.10  cgd #ifdef NAMEI_DIAGNOSTIC
    398  1.10  cgd 	printf("found\n");
    399  1.10  cgd #endif
    400  1.10  cgd 
    401  1.10  cgd 	dp = ndp->ni_vp;
    402  1.10  cgd 	/*
    403  1.10  cgd 	 * Check for symbolic link
    404  1.10  cgd 	 */
    405  1.10  cgd 	if ((dp->v_type == VLNK) &&
    406  1.10  cgd 	    ((ndp->ni_nameiop & FOLLOW) || *ndp->ni_next == '/')) {
    407  1.10  cgd 		ndp->ni_more = 1;
    408  1.10  cgd 		return (0);
    409  1.10  cgd 	}
    410  1.10  cgd 
    411  1.10  cgd 	/*
    412  1.10  cgd 	 * Check to see if the vnode has been mounted on;
    413  1.10  cgd 	 * if so find the root of the mounted file system.
    414  1.10  cgd 	 */
    415  1.10  cgd mntloop:
    416  1.10  cgd 	while (dp->v_type == VDIR && (mp = dp->v_mountedhere) &&
    417  1.10  cgd 	       (ndp->ni_nameiop & NOCROSSMOUNT) == 0) {
    418  1.10  cgd 		while(mp->mnt_flag & MNT_MLOCK) {
    419  1.10  cgd 			mp->mnt_flag |= MNT_MWAIT;
    420  1.10  cgd 			tsleep((caddr_t)mp, PVFS, "lookup", 0);
    421  1.10  cgd 			goto mntloop;
    422  1.10  cgd 		}
    423  1.10  cgd 		if (error = VFS_ROOT(dp->v_mountedhere, &tdp))
    424  1.10  cgd 			goto bad2;
    425  1.10  cgd 		vput(dp);
    426  1.10  cgd 		ndp->ni_vp = dp = tdp;
    427  1.10  cgd 	}
    428  1.10  cgd 
    429  1.10  cgd nextname:
    430  1.10  cgd 	/*
    431  1.10  cgd 	 * Not a symbolic link.  If more pathname,
    432  1.10  cgd 	 * continue at next component, else return.
    433  1.10  cgd 	 */
    434  1.10  cgd 	if (*ndp->ni_next == '/') {
    435  1.10  cgd 		ndp->ni_ptr = ndp->ni_next;
    436  1.10  cgd 		while (*ndp->ni_ptr == '/') {
    437  1.10  cgd 			ndp->ni_ptr++;
    438  1.10  cgd 			ndp->ni_pathlen--;
    439  1.10  cgd 		}
    440  1.10  cgd 		vrele(ndp->ni_dvp);
    441  1.10  cgd 		goto dirloop;
    442  1.10  cgd 	}
    443  1.10  cgd 	/*
    444  1.10  cgd 	 * Check for read-only file systems.
    445  1.10  cgd 	 */
    446  1.10  cgd 	if (flag == DELETE || flag == RENAME) {
    447  1.10  cgd 		/*
    448  1.10  cgd 		 * Disallow directory write attempts on read-only
    449  1.10  cgd 		 * file systems.
    450  1.10  cgd 		 */
    451  1.10  cgd 		if ((dp->v_mount->mnt_flag & rdonly) ||
    452  1.10  cgd 		    (wantparent && (ndp->ni_dvp->v_mount->mnt_flag & rdonly))) {
    453  1.10  cgd 			error = EROFS;
    454  1.10  cgd 			goto bad2;
    455  1.10  cgd 		}
    456  1.10  cgd 	}
    457  1.10  cgd 	if (ndp->ni_nameiop & SAVESTART) {
    458  1.10  cgd 		ndp->ni_startdir = ndp->ni_dvp;
    459  1.10  cgd 		VREF(ndp->ni_startdir);
    460  1.10  cgd 	}
    461  1.10  cgd 	if (!wantparent)
    462  1.10  cgd 		vrele(ndp->ni_dvp);
    463  1.10  cgd 	if ((ndp->ni_nameiop & LOCKLEAF) == 0)
    464  1.10  cgd 		VOP_UNLOCK(dp);
    465  1.10  cgd 	return (0);
    466  1.10  cgd 
    467  1.10  cgd bad2:
    468  1.10  cgd 	if ((ndp->ni_nameiop & LOCKPARENT) && *ndp->ni_next == '\0')
    469  1.10  cgd 		VOP_UNLOCK(ndp->ni_dvp);
    470  1.10  cgd 	vrele(ndp->ni_dvp);
    471  1.10  cgd bad:
    472  1.10  cgd 	vput(dp);
    473  1.10  cgd 	ndp->ni_vp = NULL;
    474  1.10  cgd 	return (error);
    475  1.10  cgd }
    476