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