xref: /csrg-svn/sys/kern/vfs_lookup.c (revision 38400)
123401Smckusick /*
237715Smckusick  * Copyright (c) 1982, 1986, 1989 Regents of the University of California.
337715Smckusick  * All rights reserved.
423401Smckusick  *
537715Smckusick  * Redistribution and use in source and binary forms are permitted
637715Smckusick  * provided that the above copyright notice and this paragraph are
737715Smckusick  * duplicated in all such forms and that any documentation,
837715Smckusick  * advertising materials, and other materials related to such
937715Smckusick  * distribution and use acknowledge that the software was developed
1037715Smckusick  * by the University of California, Berkeley.  The name of the
1137715Smckusick  * University may not be used to endorse or promote products derived
1237715Smckusick  * from this software without specific prior written permission.
1337715Smckusick  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
1437715Smckusick  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
1537715Smckusick  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
1637715Smckusick  *
17*38400Smckusick  *	@(#)vfs_lookup.c	7.17 (Berkeley) 07/03/89
1823401Smckusick  */
1930Sbill 
2017100Sbloom #include "param.h"
2137715Smckusick #include "time.h"
2237715Smckusick #include "namei.h"
2337715Smckusick #include "vnode.h"
2417100Sbloom #include "mount.h"
2537715Smckusick #include "errno.h"
2631650Smckusick #include "malloc.h"
2737715Smckusick 
2837582Smarc #ifdef KTRACE
2937715Smckusick #include "user.h"
3037582Smarc #include "proc.h"
3137582Smarc #include "ktrace.h"
3237582Smarc #endif
3330Sbill 
3430Sbill /*
3527268Smckusick  * Convert a pathname into a pointer to a locked inode.
367534Sroot  * This is a very central and rather complicated routine.
3730Sbill  *
3837715Smckusick  * The flag argument is LOOKUP, CREATE, RENAME, or DELETE depending on
3937715Smckusick  * whether the name is to be looked up, created, renamed, or deleted.
4037715Smckusick  * When CREATE, RENAME, or DELETE is specified, information usable in
4137715Smckusick  * creating, renaming, or deleting a directory entry may be calculated.
4237715Smckusick  * If flag has LOCKPARENT or'ed into it and the target of the pathname
4337715Smckusick  * exists, namei returns both the target and its parent directory locked.
4437715Smckusick  * When creating or renaming and LOCKPARENT is specified, the target may not
4537715Smckusick  * be ".".  When deleting and LOCKPARENT is specified, the target may be ".".
469166Ssam  *
4716688Smckusick  * The FOLLOW flag is set when symbolic links are to be followed
489166Ssam  * when they occur at the end of the name translation process.
4927268Smckusick  * Symbolic links are always followed for all other pathname
5027268Smckusick  * components other than the last.
519166Ssam  *
5227268Smckusick  * The segflg defines whether the name is to be copied from user
5327268Smckusick  * space or kernel space.
5427268Smckusick  *
5515798Smckusick  * Overall outline of namei:
5615798Smckusick  *
577534Sroot  *	copy in name
587534Sroot  *	get starting directory
597534Sroot  * dirloop:
6016688Smckusick  *	copy next component of name to ndp->ni_dent
617534Sroot  *	handle degenerate case where name is null string
6237715Smckusick  *	if .. and on mounted filesys, find parent
6337715Smckusick  *	call lookup routine for next component name
6437715Smckusick  *	  directory vnode returned in ni_dvp, unlocked unless LOCKPARENT set
6537715Smckusick  *	  component vnode returned in ni_vp (if it exists), locked.
667534Sroot  *	if symbolic link, massage name in buffer and continue at dirloop
6737715Smckusick  *	if result inode is mounted on, find mounted on vnode
687534Sroot  *	if more components of name, do next level at dirloop
6937715Smckusick  *	return the answer in ni_vp as locked vnode;
7037715Smckusick  *	  if LOCKPARENT set, return locked parent in ni_dvp
719166Ssam  *
7237715Smckusick  * NOTE: (LOOKUP | LOCKPARENT) currently returns the parent vnode unlocked.
7330Sbill  */
7416688Smckusick namei(ndp)
7516688Smckusick 	register struct nameidata *ndp;
7630Sbill {
777534Sroot 	register char *cp;		/* pointer into pathname argument */
7837715Smckusick 	register struct vnode *dp = 0;	/* the directory we are searching */
7937715Smckusick 	register int i;		   	/* Temp counter */
8037715Smckusick 	struct vnode *tdp;		/* saved dp */
8137715Smckusick 	struct mount *mp;		/* mount table entry */
8218109Smckusick 	int docache;			/* == 0 do not cache last component */
8337715Smckusick 	int flag;			/* LOOKUP, CREATE, RENAME or DELETE */
8437715Smckusick 	int wantparent;			/* 1 => wantparent or lockparent flag */
8538001Smckusick 	int lockparent;			/* 1 => lockparent flag */
8637715Smckusick 	int error = 0;
8730Sbill 
8837715Smckusick 	flag = ndp->ni_nameiop & OPFLAG;
8937715Smckusick 	wantparent = ndp->ni_nameiop & (LOCKPARENT|WANTPARENT);
9038001Smckusick 	lockparent = ndp->ni_nameiop & LOCKPARENT;
9116688Smckusick 	docache = (ndp->ni_nameiop & NOCACHE) ^ NOCACHE;
9237715Smckusick 	if (flag == DELETE || wantparent)
9315798Smckusick 		docache = 0;
9430Sbill 	/*
957534Sroot 	 * Get a buffer for the name to be translated, and copy the
967534Sroot 	 * name into the buffer.
975972Swnj 	 */
9837715Smckusick 	MALLOC(ndp->ni_pnbuf, caddr_t, MAXPATHLEN, M_NAMEI, M_WAITOK);
9916688Smckusick 	if (ndp->ni_segflg == UIO_SYSSPACE)
10037715Smckusick 		error = copystr(ndp->ni_dirp, ndp->ni_pnbuf, MAXPATHLEN,
10137715Smckusick 		    &ndp->ni_pathlen);
10216688Smckusick 	else
10337715Smckusick 		error = copyinstr(ndp->ni_dirp, ndp->ni_pnbuf, MAXPATHLEN,
10437715Smckusick 		    &ndp->ni_pathlen);
10516688Smckusick 	if (error) {
10637715Smckusick 		free(ndp->ni_pnbuf, M_NAMEI);
10738003Smckusick 		ndp->ni_vp = NULL;
10837715Smckusick 		return (error);
1095972Swnj 	}
11037715Smckusick 	ndp->ni_ptr = ndp->ni_pnbuf;
11137715Smckusick 	ndp->ni_loopcnt = 0;
11237715Smckusick 	dp = ndp->ni_cdir;
11338347Smckusick 	VREF(dp);
11437582Smarc #ifdef KTRACE
11537582Smarc 	if (KTRPOINT(u.u_procp, KTR_NAMEI))
11637715Smckusick 		ktrnamei(u.u_procp->p_tracep, ndp->ni_pnbuf);
11737582Smarc #endif
1187534Sroot 
11937715Smckusick start:
1205972Swnj 	/*
1217534Sroot 	 * Get starting directory.
12237715Smckusick 	 * Done at start of translation and after symbolic link.
12330Sbill 	 */
12437715Smckusick 	if (*ndp->ni_ptr == '/') {
12537715Smckusick 		vrele(dp);
12637715Smckusick 		while (*ndp->ni_ptr == '/') {
12737715Smckusick 			ndp->ni_ptr++;
12837715Smckusick 			ndp->ni_pathlen--;
12937715Smckusick 		}
13037715Smckusick 		if ((dp = ndp->ni_rdir) == NULL)
13130Sbill 			dp = rootdir;
13238347Smckusick 		VREF(dp);
13337715Smckusick 	}
13437715Smckusick 	VOP_LOCK(dp);
13518027Smckusick 	ndp->ni_endoff = 0;
1367534Sroot 
1377534Sroot 	/*
1387534Sroot 	 * We come to dirloop to search a new directory.
1397534Sroot 	 */
1406571Smckusic dirloop:
14130Sbill 	/*
14216688Smckusick 	 * Copy next component of name to ndp->ni_dent.
14337715Smckusick 	 * XXX kern_exec looks at d_name
14437715Smckusick 	 * ??? The ni_hash value may be useful for vfs_cache
14537715Smckusick 	 * XXX There must be the last component of the filename left
14637715Smckusick 	 * somewhere accessible via. ndp for NFS (and any other stateless file
14737715Smckusick 	 * systems) in case they are doing a CREATE. The "Towards a..." noted
14837715Smckusick 	 * that ni_ptr would be left pointing to the last component, but since
14937715Smckusick 	 * the ni_pnbuf gets free'd, that is not a good idea.
1507534Sroot 	 */
15137743Smckusick #ifdef notdef
15237743Smckusick 	for (cp = ndp->ni_ptr; *cp != 0 && *cp != '/'; cp++) {
15337743Smckusick 		if ((*cp & 0200) == 0)
15437743Smckusick 			continue;
15537743Smckusick 		if ((*cp&0377) == ('/'|0200) || flag != DELETE) {
15637743Smckusick 			error = EINVAL;
15737743Smckusick 			goto bad;
15837743Smckusick 		}
15937743Smckusick 	}
16037743Smckusick 	ndp->ni_namelen = cp - ndp->ni_ptr;
16137743Smckusick 	if (ndp->ni_namelen >= MAXNAMLEN) {
16237743Smckusick 		error = ENAMETOOLONG;
16337743Smckusick 		goto bad;
16437743Smckusick 	}
16537743Smckusick 	ndp->ni_pathlen -= ndp->ni_namelen;
16637743Smckusick #ifdef NAMEI_DIAGNOSTIC
16737743Smckusick 	{ char c = *cp;
16837743Smckusick 	*cp = '\0';
16937743Smckusick 	printf("{%s}: ", ndp->ni_ptr);
17037743Smckusick 	*cp = c; }
17137743Smckusick #endif
17237743Smckusick #else fornow
17337715Smckusick 	ndp->ni_hash = 0;
17437715Smckusick 	for (cp = ndp->ni_ptr, i = 0; *cp != 0 && *cp != '/'; cp++) {
1756571Smckusic 		if (i >= MAXNAMLEN) {
17637715Smckusick 			error = ENAMETOOLONG;
1777534Sroot 			goto bad;
1785972Swnj 		}
17921014Smckusick 		if (*cp & 0200)
18021014Smckusick 			if ((*cp&0377) == ('/'|0200) || flag != DELETE) {
18137715Smckusick 				error = EINVAL;
18221014Smckusick 				goto bad;
18321014Smckusick 			}
18416688Smckusick 		ndp->ni_dent.d_name[i++] = *cp;
18537715Smckusick 		ndp->ni_hash += (unsigned char)*cp * i;
1865972Swnj 	}
18737715Smckusick 	ndp->ni_namelen = i;
18816688Smckusick 	ndp->ni_dent.d_namlen = i;
18916688Smckusick 	ndp->ni_dent.d_name[i] = '\0';
19037715Smckusick 	ndp->ni_pathlen -= i;
19137715Smckusick #ifdef NAMEI_DIAGNOSTIC
19237715Smckusick 	printf("{%s}: ", ndp->ni_dent.d_name);
19337715Smckusick #endif
19437743Smckusick #endif fornow
19537715Smckusick 	ndp->ni_next = cp;
19637715Smckusick 	ndp->ni_makeentry = 1;
19718109Smckusick 	if (*cp == '\0' && docache == 0)
19837715Smckusick 		ndp->ni_makeentry = 0;
19937715Smckusick 	ndp->ni_isdotdot = (ndp->ni_namelen == 2 &&
20037715Smckusick 		ndp->ni_dent.d_name[1] == '.' && ndp->ni_dent.d_name[0] == '.');
2017534Sroot 
2027534Sroot 	/*
2037534Sroot 	 * Check for degenerate name (e.g. / or "")
2047534Sroot 	 * which is a way of talking about a directory,
2057534Sroot 	 * e.g. like "/." or ".".
2067534Sroot 	 */
20737715Smckusick 	if (ndp->ni_ptr[0] == '\0') {
20837715Smckusick 		if (flag != LOOKUP || wantparent) {
20937715Smckusick 			error = EISDIR;
2107534Sroot 			goto bad;
2115972Swnj 		}
21237715Smckusick 		free(ndp->ni_pnbuf, M_NAMEI);
21337715Smckusick 		if (!(ndp->ni_nameiop & LOCKLEAF))
21437715Smckusick 			VOP_UNLOCK(dp);
21537715Smckusick 		ndp->ni_vp = dp;
21637715Smckusick 		return (0);
2175972Swnj 	}
2187534Sroot 
2196571Smckusic 	/*
22037715Smckusick 	 * Handle "..": two special cases.
22137715Smckusick 	 * 1. If at root directory (e.g. after chroot)
22237715Smckusick 	 *    then ignore it so can't get out.
22337715Smckusick 	 * 2. If this vnode is the root of a mounted
22437715Smckusick 	 *    file system, then replace it with the
22537715Smckusick 	 *    vnode which was mounted on so we take the
22637715Smckusick 	 *    .. in the other file system.
22736547Smckusick 	 */
22837715Smckusick 	if (ndp->ni_isdotdot) {
22936547Smckusick 		for (;;) {
23037715Smckusick 			if (dp == ndp->ni_rdir || dp == rootdir) {
23137715Smckusick 				ndp->ni_dvp = dp;
23238390Smckusick 				ndp->ni_vp = dp;
23338347Smckusick 				VREF(dp);
23437715Smckusick 				goto nextname;
23536547Smckusick 			}
23637715Smckusick 			if ((dp->v_flag & VROOT) == 0)
23736547Smckusick 				break;
23837715Smckusick 			tdp = dp;
23937715Smckusick 			dp = dp->v_mount->m_vnodecovered;
24037715Smckusick 			vput(tdp);
24138390Smckusick 			VREF(dp);
24237715Smckusick 			VOP_LOCK(dp);
24336547Smckusick 		}
24436547Smckusick 	}
24536547Smckusick 
24636547Smckusick 	/*
24715798Smckusick 	 * We now have a segment name to search for, and a directory to search.
24815798Smckusick 	 */
24937715Smckusick 	if (error = VOP_LOOKUP(dp, ndp)) {
25037715Smckusick 		if (ndp->ni_vp != NULL)
25137715Smckusick 			panic("leaf should be empty");
25237582Smarc #ifdef NAMEI_DIAGNOSTIC
25337715Smckusick 		printf("not found\n");
25437582Smarc #endif
2555972Swnj 		/*
25637715Smckusick 		 * If creating and at end of pathname, then can consider
25737715Smckusick 		 * allowing file to be created.
2585972Swnj 		 */
25937715Smckusick 		if (ndp->ni_dvp->v_mount->m_flag & M_RDONLY)
26037715Smckusick 			error = EROFS;
26137742Smckusick 		if (flag == LOOKUP || flag == DELETE ||
26237742Smckusick 		    error != ENOENT || *cp != 0)
2637534Sroot 			goto bad;
2645972Swnj 		/*
26537715Smckusick 		 * We return with ni_vp NULL to indicate that the entry
26637715Smckusick 		 * doesn't currently exist, leaving a pointer to the
26737715Smckusick 		 * (possibly locked) directory inode in ndp->ni_dvp.
2685972Swnj 		 */
26937715Smckusick 		FREE(ndp->ni_pnbuf, M_NAMEI);
27037715Smckusick 		return (0);	/* should this be ENOENT? */
2717534Sroot 	}
27237582Smarc #ifdef NAMEI_DIAGNOSTIC
27337715Smckusick 	printf("found\n");
27437582Smarc #endif
2757534Sroot 
2767534Sroot 	/*
27737715Smckusick 	 * Check for symbolic link
2787534Sroot 	 */
27937715Smckusick 	dp = ndp->ni_vp;
28037715Smckusick 	if ((dp->v_type == VLNK) &&
28137715Smckusick 	    ((ndp->ni_nameiop & FOLLOW) || *ndp->ni_next == '/')) {
28237715Smckusick 		struct iovec aiov;
28337715Smckusick 		struct uio auio;
28437715Smckusick 		int linklen;
2857534Sroot 
28637715Smckusick 		if (++ndp->ni_loopcnt > MAXSYMLINKS) {
28737715Smckusick 			error = ELOOP;
28837715Smckusick 			goto bad2;
28937715Smckusick 		}
29038215Smckusick 		if (ndp->ni_pathlen > 1)
29137715Smckusick 			MALLOC(cp, char *, MAXPATHLEN, M_NAMEI, M_WAITOK);
2927534Sroot 		else
29337715Smckusick 			cp = ndp->ni_pnbuf;
29437715Smckusick 		aiov.iov_base = cp;
29537715Smckusick 		aiov.iov_len = MAXPATHLEN;
29637715Smckusick 		auio.uio_iov = &aiov;
29737715Smckusick 		auio.uio_iovcnt = 1;
29837715Smckusick 		auio.uio_offset = 0;
29937715Smckusick 		auio.uio_rw = UIO_READ;
30037715Smckusick 		auio.uio_segflg = UIO_SYSSPACE;
30137715Smckusick 		auio.uio_resid = MAXPATHLEN;
30237715Smckusick 		if (error = VOP_READLINK(dp, &auio, ndp->ni_cred)) {
30338215Smckusick 			if (ndp->ni_pathlen > 1)
30437715Smckusick 				free(cp, M_NAMEI);
30537715Smckusick 			goto bad2;
3069166Ssam 		}
30737715Smckusick 		linklen = MAXPATHLEN - auio.uio_resid;
30837715Smckusick 		if (linklen + ndp->ni_pathlen >= MAXPATHLEN) {
30938215Smckusick 			if (ndp->ni_pathlen > 1)
31037715Smckusick 				free(cp, M_NAMEI);
31137715Smckusick 			error = ENAMETOOLONG;
31212011Smckusick 			goto bad2;
31315798Smckusick 		}
31438215Smckusick 		if (ndp->ni_pathlen > 1) {
31538215Smckusick 			bcopy(ndp->ni_next, cp + linklen, ndp->ni_pathlen);
31637715Smckusick 			FREE(ndp->ni_pnbuf, M_NAMEI);
31737715Smckusick 			ndp->ni_pnbuf = cp;
31838215Smckusick 		} else
31938001Smckusick 			ndp->ni_pnbuf[linklen] = '\0';
32037715Smckusick 		ndp->ni_ptr = cp;
32137715Smckusick 		vput(dp);
32237715Smckusick 		dp = ndp->ni_dvp;
32338393Smckusick 		if (lockparent && ndp->ni_pathlen == 1)
32438001Smckusick 			VOP_UNLOCK(dp);
32538393Smckusick 		ndp->ni_pathlen += linklen;
32637715Smckusick 		goto start;
32715798Smckusick 	}
32815798Smckusick 
3297534Sroot 	/*
33037715Smckusick 	 * Check to see if the vnode has been mounted on;
33137715Smckusick 	 * if so find the root of the mounted file system.
3327534Sroot 	 */
33337715Smckusick mntloop:
33437715Smckusick 	while (dp->v_type == VDIR && (mp = dp->v_mountedhere)) {
33537715Smckusick 		while(mp->m_flag & M_MLOCK) {
33637715Smckusick 			mp->m_flag |= M_MWAIT;
33737715Smckusick 			sleep((caddr_t)mp, PVFS);
33837715Smckusick 			goto mntloop;
33921014Smckusick 		}
34037715Smckusick 		error = VFS_ROOT(dp->v_mountedhere, &tdp);
34137715Smckusick 		if (error)
3427534Sroot 			goto bad2;
34337715Smckusick 		vput(dp);
34437715Smckusick 		ndp->ni_vp = dp = tdp;
34530Sbill 	}
3467534Sroot 
34737715Smckusick nextname:
34830Sbill 	/*
3497534Sroot 	 * Not a symbolic link.  If more pathname,
3507534Sroot 	 * continue at next component, else return.
35130Sbill 	 */
35237715Smckusick 	ndp->ni_ptr = ndp->ni_next;
35337715Smckusick 	if (*ndp->ni_ptr == '/') {
35437715Smckusick 		while (*ndp->ni_ptr == '/') {
35537715Smckusick 			ndp->ni_ptr++;
35637715Smckusick 			ndp->ni_pathlen--;
35737715Smckusick 		}
35837715Smckusick 		vrele(ndp->ni_dvp);
3597534Sroot 		goto dirloop;
36030Sbill 	}
3617534Sroot 	/*
362*38400Smckusick 	 * Check for read-only file systems.
3637534Sroot 	 */
364*38400Smckusick 	if (flag == DELETE || flag == RENAME) {
36538267Smckusick 		/*
366*38400Smckusick 		 * Disallow directory write attempts on read-only
367*38400Smckusick 		 * file systems.
36838267Smckusick 		 */
369*38400Smckusick 		if ((dp->v_mount->m_flag & M_RDONLY) ||
370*38400Smckusick 		    (wantparent && (ndp->ni_dvp->v_mount->m_flag & M_RDONLY))) {
37138267Smckusick 			error = EROFS;
37238267Smckusick 			goto bad2;
37338267Smckusick 		}
37438267Smckusick 	}
37537715Smckusick 	if (!wantparent)
37637715Smckusick 		vrele(ndp->ni_dvp);
37737715Smckusick 	if ((ndp->ni_nameiop & LOCKLEAF) == 0)
37837715Smckusick 		VOP_UNLOCK(dp);
37937715Smckusick 	FREE(ndp->ni_pnbuf, M_NAMEI);
38037715Smckusick 	return (0);
3817534Sroot 
38237715Smckusick bad2:
38338001Smckusick 	if (lockparent && *ndp->ni_next == '\0')
38438001Smckusick 		VOP_UNLOCK(ndp->ni_dvp);
38537715Smckusick 	vrele(ndp->ni_dvp);
38637715Smckusick bad:
38737715Smckusick 	vput(dp);
38837715Smckusick 	ndp->ni_vp = NULL;
38937715Smckusick 	FREE(ndp->ni_pnbuf, M_NAMEI);
39010849Ssam 	return (error);
3915972Swnj }
392