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