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.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
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 * 4. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * @(#)vfs_lookup.c 8.4 (Berkeley) 2/16/94
37 #include <sys/cdefs.h>
38 __FBSDID("$FreeBSD$");
40 #include "opt_ktrace.h"
43 #include <sys/param.h>
44 #include <sys/systm.h>
45 #include <sys/kernel.h>
48 #include <sys/mutex.h>
49 #include <sys/namei.h>
50 #include <sys/vnode.h>
51 #include <sys/mount.h>
52 #include <sys/filedesc.h>
55 #include <sys/ktrace.h>
61 * Allocation zone for namei
63 uma_zone_t namei_zone;
66 nameiinit(void *dummy __unused)
68 namei_zone = uma_zcreate("NAMEI", MAXPATHLEN, NULL, NULL, NULL, NULL,
72 SYSINIT(vfs, SI_SUB_VFS, SI_ORDER_SECOND, nameiinit, NULL)
75 * Convert a pathname into a pointer to a locked inode.
77 * The FOLLOW flag is set when symbolic links are to be followed
78 * when they occur at the end of the name translation process.
79 * Symbolic links are always followed for all other pathname
80 * components other than the last.
82 * The segflg defines whether the name is to be copied from user
83 * space or kernel space.
85 * Overall outline of namei:
88 * get starting directory
89 * while (!done && !error) {
90 * call lookup to search path.
91 * if symbolic link, massage name in buffer and continue
96 register struct nameidata *ndp;
98 register struct filedesc *fdp; /* pointer to file descriptor state */
99 register char *cp; /* pointer into pathname argument */
100 register struct vnode *dp; /* the directory we are searching */
101 struct iovec aiov; /* uio for reading symbolic links */
104 struct componentname *cnp = &ndp->ni_cnd;
105 struct thread *td = cnp->cn_thread;
106 struct proc *p = td->td_proc;
110 ndp->ni_cnd.cn_cred = ndp->ni_cnd.cn_thread->td_ucred;
111 KASSERT(cnp->cn_cred && p, ("namei: bad cred/proc"));
112 KASSERT((cnp->cn_nameiop & (~OPMASK)) == 0,
113 ("namei: nameiop contaminated with flags"));
114 KASSERT((cnp->cn_flags & OPMASK) == 0,
115 ("namei: flags contaminated with nameiops"));
119 * Get a buffer for the name to be translated, and copy the
120 * name into the buffer.
122 if ((cnp->cn_flags & HASBUF) == 0)
123 cnp->cn_pnbuf = uma_zalloc(namei_zone, M_WAITOK);
124 if (ndp->ni_segflg == UIO_SYSSPACE)
125 error = copystr(ndp->ni_dirp, cnp->cn_pnbuf,
126 MAXPATHLEN, (size_t *)&ndp->ni_pathlen);
128 error = copyinstr(ndp->ni_dirp, cnp->cn_pnbuf,
129 MAXPATHLEN, (size_t *)&ndp->ni_pathlen);
132 * Don't allow empty pathnames.
134 if (!error && *cnp->cn_pnbuf == '\0')
138 uma_zfree(namei_zone, cnp->cn_pnbuf);
140 cnp->cn_pnbuf = NULL;
141 cnp->cn_nameptr = NULL;
148 if (KTRPOINT(td, KTR_NAMEI)) {
149 KASSERT(cnp->cn_thread == curthread,
150 ("namei not using curthread"));
151 ktrnamei(cnp->cn_pnbuf);
156 * Get starting point for the translation.
159 ndp->ni_rootdir = fdp->fd_rdir;
160 ndp->ni_topdir = fdp->fd_jdir;
164 FILEDESC_UNLOCK(fdp);
167 * Check if root directory should replace current directory.
168 * Done at start of translation and after symbolic link.
170 cnp->cn_nameptr = cnp->cn_pnbuf;
171 if (*(cnp->cn_nameptr) == '/') {
173 while (*(cnp->cn_nameptr) == '/') {
177 dp = ndp->ni_rootdir;
180 ndp->ni_startdir = dp;
183 uma_zfree(namei_zone, cnp->cn_pnbuf);
185 cnp->cn_pnbuf = NULL;
186 cnp->cn_nameptr = NULL;
191 * Check for symbolic link
193 if ((cnp->cn_flags & ISSYMLINK) == 0) {
194 if ((cnp->cn_flags & (SAVENAME | SAVESTART)) == 0) {
195 uma_zfree(namei_zone, cnp->cn_pnbuf);
197 cnp->cn_pnbuf = NULL;
198 cnp->cn_nameptr = NULL;
201 cnp->cn_flags |= HASBUF;
203 if (vn_canvmio(ndp->ni_vp) == TRUE &&
204 (cnp->cn_nameiop != DELETE) &&
205 ((cnp->cn_flags & (NOOBJ|LOCKLEAF)) ==
207 vfs_object_create(ndp->ni_vp, td,
208 ndp->ni_cnd.cn_cred);
212 if ((cnp->cn_flags & LOCKPARENT) && ndp->ni_pathlen == 1)
213 VOP_UNLOCK(ndp->ni_dvp, 0, td);
214 if (ndp->ni_loopcnt++ >= MAXSYMLINKS) {
219 if ((cnp->cn_flags & NOMACCHECK) == 0) {
220 error = mac_check_vnode_readlink(td->td_ucred,
226 if (ndp->ni_pathlen > 1)
227 cp = uma_zalloc(namei_zone, M_WAITOK);
231 aiov.iov_len = MAXPATHLEN;
232 auio.uio_iov = &aiov;
235 auio.uio_rw = UIO_READ;
236 auio.uio_segflg = UIO_SYSSPACE;
237 auio.uio_td = (struct thread *)0;
238 auio.uio_resid = MAXPATHLEN;
239 error = VOP_READLINK(ndp->ni_vp, &auio, cnp->cn_cred);
241 if (ndp->ni_pathlen > 1)
242 uma_zfree(namei_zone, cp);
245 linklen = MAXPATHLEN - auio.uio_resid;
247 if (ndp->ni_pathlen > 1)
248 uma_zfree(namei_zone, cp);
252 if (linklen + ndp->ni_pathlen >= MAXPATHLEN) {
253 if (ndp->ni_pathlen > 1)
254 uma_zfree(namei_zone, cp);
255 error = ENAMETOOLONG;
258 if (ndp->ni_pathlen > 1) {
259 bcopy(ndp->ni_next, cp + linklen, ndp->ni_pathlen);
260 uma_zfree(namei_zone, cnp->cn_pnbuf);
263 cnp->cn_pnbuf[linklen] = '\0';
264 ndp->ni_pathlen += linklen;
268 uma_zfree(namei_zone, cnp->cn_pnbuf);
270 cnp->cn_pnbuf = NULL;
271 cnp->cn_nameptr = NULL;
281 * This is a very central and rather complicated routine.
283 * The pathname is pointed to by ni_ptr and is of length ni_pathlen.
284 * The starting directory is taken from ni_startdir. The pathname is
285 * descended until done, or a symbolic link is encountered. The variable
286 * ni_more is clear if the path is completed; it is set to one if a
287 * symbolic link needing interpretation is encountered.
289 * The flag argument is LOOKUP, CREATE, RENAME, or DELETE depending on
290 * whether the name is to be looked up, created, renamed, or deleted.
291 * When CREATE, RENAME, or DELETE is specified, information usable in
292 * creating, renaming, or deleting a directory entry may be calculated.
293 * If flag has LOCKPARENT or'ed into it, the parent directory is returned
294 * locked. If flag has WANTPARENT or'ed into it, the parent directory is
295 * returned unlocked. Otherwise the parent directory is not returned. If
296 * the target of the pathname exists and LOCKLEAF is or'ed into the flag
297 * the target is returned locked, otherwise it is returned unlocked.
298 * When creating or renaming and LOCKPARENT is specified, the target may not
299 * be ".". When deleting and LOCKPARENT is specified, the target may be ".".
301 * Overall outline of lookup:
304 * identify next component of name at ndp->ni_ptr
305 * handle degenerate case where name is null string
306 * if .. and crossing mount points and on mounted filesys, find parent
307 * call VOP_LOOKUP routine for next component name
308 * directory vnode returned in ni_dvp, unlocked unless LOCKPARENT set
309 * component vnode returned in ni_vp (if it exists), locked.
310 * if result vnode is mounted on and crossing mount points,
311 * find mounted on vnode
312 * if more components of name, do next level at dirloop
313 * return the answer in ni_vp, locked if LOCKLEAF set
314 * if LOCKPARENT set, return locked parent in ni_dvp
315 * if WANTPARENT set, return unlocked parent in ni_dvp
319 register struct nameidata *ndp;
321 register char *cp; /* pointer into pathname argument */
322 register struct vnode *dp = 0; /* the directory we are searching */
323 struct vnode *tdp; /* saved dp */
324 struct mount *mp; /* mount table entry */
325 int docache; /* == 0 do not cache last component */
326 int wantparent; /* 1 => wantparent or lockparent flag */
327 int rdonly; /* lookup read-only flag bit */
330 int dpunlocked = 0; /* dp has already been unlocked */
331 struct componentname *cnp = &ndp->ni_cnd;
332 struct thread *td = cnp->cn_thread;
335 * Setup: break out flag bits into variables.
337 wantparent = cnp->cn_flags & (LOCKPARENT | WANTPARENT);
338 docache = (cnp->cn_flags & NOCACHE) ^ NOCACHE;
339 if (cnp->cn_nameiop == DELETE ||
340 (wantparent && cnp->cn_nameiop != CREATE &&
341 cnp->cn_nameiop != LOOKUP))
343 rdonly = cnp->cn_flags & RDONLY;
345 cnp->cn_flags &= ~ISSYMLINK;
346 dp = ndp->ni_startdir;
347 ndp->ni_startdir = NULLVP;
348 vn_lock(dp, LK_EXCLUSIVE | LK_RETRY, td);
352 * Search a new directory.
354 * The last component of the filename is left accessible via
355 * cnp->cn_nameptr for callers that need the name. Callers needing
356 * the name set the SAVENAME flag. When done, they assume
357 * responsibility for freeing the pathname buffer.
360 for (cp = cnp->cn_nameptr; *cp != 0 && *cp != '/'; cp++)
362 cnp->cn_namelen = cp - cnp->cn_nameptr;
363 if (cnp->cn_namelen > NAME_MAX) {
364 error = ENAMETOOLONG;
367 #ifdef NAMEI_DIAGNOSTIC
370 printf("{%s}: ", cnp->cn_nameptr);
373 ndp->ni_pathlen -= cnp->cn_namelen;
377 * Replace multiple slashes by a single slash and trailing slashes
378 * by a null. This must be done before VOP_LOOKUP() because some
379 * fs's don't know about trailing slashes. Remember if there were
380 * trailing slashes to handle symlinks, existing non-directories
381 * and non-existing files that won't be directories specially later.
384 while (*cp == '/' && (cp[1] == '/' || cp[1] == '\0')) {
389 *ndp->ni_next = '\0'; /* XXX for direnter() ... */
394 cnp->cn_flags |= MAKEENTRY;
395 if (*cp == '\0' && docache == 0)
396 cnp->cn_flags &= ~MAKEENTRY;
397 if (cnp->cn_namelen == 2 &&
398 cnp->cn_nameptr[1] == '.' && cnp->cn_nameptr[0] == '.')
399 cnp->cn_flags |= ISDOTDOT;
401 cnp->cn_flags &= ~ISDOTDOT;
402 if (*ndp->ni_next == 0)
403 cnp->cn_flags |= ISLASTCN;
405 cnp->cn_flags &= ~ISLASTCN;
409 * Check for degenerate name (e.g. / or "")
410 * which is a way of talking about a directory,
411 * e.g. like "/." or ".".
413 if (cnp->cn_nameptr[0] == '\0') {
414 if (dp->v_type != VDIR) {
418 if (cnp->cn_nameiop != LOOKUP) {
427 if (!(cnp->cn_flags & (LOCKPARENT | LOCKLEAF)))
428 VOP_UNLOCK(dp, 0, td);
429 /* XXX This should probably move to the top of function. */
430 if (cnp->cn_flags & SAVESTART)
431 panic("lookup: SAVESTART");
436 * Handle "..": two special cases.
437 * 1. If at root directory (e.g. after chroot)
438 * or at absolute root directory
439 * then ignore it so can't get out.
440 * 2. If this vnode is the root of a mounted
441 * filesystem, then replace it with the
442 * vnode which was mounted on so we take the
443 * .. in the other filesystem.
444 * 3. If the vnode is the top directory of
445 * the jail or chroot, don't let them out.
447 if (cnp->cn_flags & ISDOTDOT) {
449 if (dp == ndp->ni_rootdir ||
450 dp == ndp->ni_topdir ||
457 if ((dp->v_vflag & VV_ROOT) == 0 ||
458 (cnp->cn_flags & NOCROSSMOUNT))
460 if (dp->v_mount == NULL) { /* forced unmount */
465 dp = dp->v_mount->mnt_vnodecovered;
468 vn_lock(dp, LK_EXCLUSIVE | LK_RETRY, td);
473 * We now have a segment name to search for, and a directory to search.
477 if ((cnp->cn_flags & NOMACCHECK) == 0) {
478 error = mac_check_vnode_lookup(td->td_ucred, dp, cnp);
485 cnp->cn_flags &= ~PDIRUNLOCK;
486 ASSERT_VOP_LOCKED(dp, "lookup");
487 if ((error = VOP_LOOKUP(dp, &ndp->ni_vp, cnp)) != 0) {
488 KASSERT(ndp->ni_vp == NULL, ("leaf should be empty"));
489 #ifdef NAMEI_DIAGNOSTIC
490 printf("not found\n");
492 if ((error == ENOENT) &&
493 (dp->v_vflag & VV_ROOT) && (dp->v_mount != NULL) &&
494 (dp->v_mount->mnt_flag & MNT_UNION)) {
496 dp = dp->v_mount->mnt_vnodecovered;
497 if (cnp->cn_flags & PDIRUNLOCK)
502 vn_lock(dp, LK_EXCLUSIVE | LK_RETRY, td);
506 if (error != EJUSTRETURN)
509 * If creating and at end of pathname, then can consider
510 * allowing file to be created.
516 if (*cp == '\0' && trailing_slash &&
517 !(cnp->cn_flags & WILLBEDIR)) {
522 * We return with ni_vp NULL to indicate that the entry
523 * doesn't currently exist, leaving a pointer to the
524 * (possibly locked) directory inode in ndp->ni_dvp.
526 if (cnp->cn_flags & SAVESTART) {
527 ndp->ni_startdir = ndp->ni_dvp;
528 VREF(ndp->ni_startdir);
532 #ifdef NAMEI_DIAGNOSTIC
536 ASSERT_VOP_LOCKED(ndp->ni_vp, "lookup");
539 * Take into account any additional components consumed by
540 * the underlying filesystem.
542 if (cnp->cn_consume > 0) {
543 cnp->cn_nameptr += cnp->cn_consume;
544 ndp->ni_next += cnp->cn_consume;
545 ndp->ni_pathlen -= cnp->cn_consume;
552 * Check to see if the vnode has been mounted on;
553 * if so find the root of the mounted filesystem.
555 while (dp->v_type == VDIR && (mp = dp->v_mountedhere) &&
556 (cnp->cn_flags & NOCROSSMOUNT) == 0) {
557 if (vfs_busy(mp, 0, 0, td))
559 VOP_UNLOCK(dp, 0, td);
560 error = VFS_ROOT(mp, &tdp, td);
567 ndp->ni_vp = dp = tdp;
571 * Check for symbolic link
573 if ((dp->v_type == VLNK) &&
574 ((cnp->cn_flags & FOLLOW) || trailing_slash ||
575 *ndp->ni_next == '/')) {
576 cnp->cn_flags |= ISSYMLINK;
577 if (dp->v_mount == NULL) {
578 /* We can't know whether the directory was mounted with
579 * NOSYMFOLLOW, so we can't follow safely. */
583 if (dp->v_mount->mnt_flag & MNT_NOSYMFOLLOW) {
591 * Check for bogus trailing slashes.
593 if (trailing_slash && dp->v_type != VDIR) {
600 * Not a symbolic link. If more pathname,
601 * continue at next component, else return.
603 if (*ndp->ni_next == '/') {
604 cnp->cn_nameptr = ndp->ni_next;
605 while (*cnp->cn_nameptr == '/') {
609 if (ndp->ni_dvp != ndp->ni_vp)
610 ASSERT_VOP_UNLOCKED(ndp->ni_dvp, "lookup");
615 * Disallow directory write attempts on read-only filesystems.
618 (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME)) {
622 if (cnp->cn_flags & SAVESTART) {
623 ndp->ni_startdir = ndp->ni_dvp;
624 VREF(ndp->ni_startdir);
629 if ((cnp->cn_flags & LOCKLEAF) == 0)
630 VOP_UNLOCK(dp, 0, td);
634 if ((cnp->cn_flags & (LOCKPARENT | PDIRUNLOCK)) == LOCKPARENT &&
635 *ndp->ni_next == '\0')
636 VOP_UNLOCK(ndp->ni_dvp, 0, td);
648 * relookup - lookup a path name component
649 * Used by lookup to re-aquire things.
652 relookup(dvp, vpp, cnp)
653 struct vnode *dvp, **vpp;
654 struct componentname *cnp;
656 struct thread *td = cnp->cn_thread;
657 struct vnode *dp = 0; /* the directory we are searching */
658 int docache; /* == 0 do not cache last component */
659 int wantparent; /* 1 => wantparent or lockparent flag */
660 int rdonly; /* lookup read-only flag bit */
662 #ifdef NAMEI_DIAGNOSTIC
663 int newhash; /* DEBUG: check name hash */
664 char *cp; /* DEBUG: check name ptr/len */
668 * Setup: break out flag bits into variables.
670 wantparent = cnp->cn_flags & (LOCKPARENT|WANTPARENT);
671 docache = (cnp->cn_flags & NOCACHE) ^ NOCACHE;
672 if (cnp->cn_nameiop == DELETE ||
673 (wantparent && cnp->cn_nameiop != CREATE))
675 rdonly = cnp->cn_flags & RDONLY;
676 cnp->cn_flags &= ~ISSYMLINK;
678 vn_lock(dp, LK_EXCLUSIVE | LK_RETRY, td);
682 * Search a new directory.
684 * The last component of the filename is left accessible via
685 * cnp->cn_nameptr for callers that need the name. Callers needing
686 * the name set the SAVENAME flag. When done, they assume
687 * responsibility for freeing the pathname buffer.
689 #ifdef NAMEI_DIAGNOSTIC
690 if (cnp->cn_namelen != cp - cnp->cn_nameptr)
691 panic ("relookup: bad len");
693 panic("relookup: not last component");
694 printf("{%s}: ", cnp->cn_nameptr);
698 * Check for degenerate name (e.g. / or "")
699 * which is a way of talking about a directory,
700 * e.g. like "/." or ".".
702 if (cnp->cn_nameptr[0] == '\0') {
703 if (cnp->cn_nameiop != LOOKUP || wantparent) {
707 if (dp->v_type != VDIR) {
711 if (!(cnp->cn_flags & LOCKLEAF))
712 VOP_UNLOCK(dp, 0, td);
714 /* XXX This should probably move to the top of function. */
715 if (cnp->cn_flags & SAVESTART)
716 panic("lookup: SAVESTART");
720 if (cnp->cn_flags & ISDOTDOT)
721 panic ("relookup: lookup on dot-dot");
724 * We now have a segment name to search for, and a directory to search.
726 if ((error = VOP_LOOKUP(dp, vpp, cnp)) != 0) {
727 KASSERT(*vpp == NULL, ("leaf should be empty"));
728 if (error != EJUSTRETURN)
731 * If creating and at end of pathname, then can consider
732 * allowing file to be created.
738 /* ASSERT(dvp == ndp->ni_startdir) */
739 if (cnp->cn_flags & SAVESTART)
742 * We return with ni_vp NULL to indicate that the entry
743 * doesn't currently exist, leaving a pointer to the
744 * (possibly locked) directory inode in ndp->ni_dvp.
751 * Check for symbolic link
753 KASSERT(dp->v_type != VLNK || !(cnp->cn_flags & FOLLOW),
754 ("relookup: symlink found.\n"));
757 * Disallow directory write attempts on read-only filesystems.
760 (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME)) {
764 /* ASSERT(dvp == ndp->ni_startdir) */
765 if (cnp->cn_flags & SAVESTART)
771 if (vn_canvmio(dp) == TRUE &&
772 ((cnp->cn_flags & (NOOBJ|LOCKLEAF)) == LOCKLEAF))
773 vfs_object_create(dp, td, cnp->cn_cred);
775 if ((cnp->cn_flags & LOCKLEAF) == 0)
776 VOP_UNLOCK(dp, 0, td);
780 if ((cnp->cn_flags & LOCKPARENT) && (cnp->cn_flags & ISLASTCN))
781 VOP_UNLOCK(dvp, 0, td);