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