lookup.c revision ca2c313831d5ab50b351578f2405a2c129436534
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License, Version 1.0 only
* (the "License"). You may not use this file except in compliance
* with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright 2005 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
/* Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T */
/* All Rights Reserved */
/*
* University Copyright- Copyright (c) 1982, 1986, 1988
* The Regents of the University of California
* All Rights Reserved
*
* University Acknowledgment- Portions of this document are derived from
* software developed by the University of California, Berkeley, and its
* contributors.
*/
#pragma ident "%Z%%M% %I% %E% SMI"
#include <sys/pathname.h>
#include <sys/sysmacros.h>
/* Controls whether paths are stored with vnodes. */
int vfs_vnode_path = 1;
int
char *fnamep,
enum symfollow followlink,
{
}
/*
* Lookup the user file name,
* Handle allocation and freeing of pathname buffer, return error.
*/
int
char *fnamep, /* user pathname */
{
char namebuf[TYPICALMAXPATHLEN];
int error;
if (error == 0) {
#ifdef C2_AUDIT
if (audit_active)
#endif
}
if (error == ENAMETOOLONG) {
/*
* This thread used a pathname > TYPICALMAXPATHLEN bytes long.
*/
return (error);
}
return (error);
}
/*
* Lookup the user file name from a given vp,
*/
int
enum symfollow followlink,
{
}
int
{
if (pnp->pn_pathlen == 0)
return (ENOENT);
} else {
}
mutex_exit(&p->p_lock);
/*
* Skip over leading slashes
*/
do {
pnp->pn_pathlen--;
}
}
/* Private flag to do our getcwd() dirty work */
#define LOOKUP_CHECKREAD 0x10
#define LOOKUP_MASK (~LOOKUP_CHECKREAD)
/*
* Starting at current directory, translate pathname pnp to end.
* Leave pathname of final component in pnp, return the vnode
* for the final component in *compvpp, and return the vnode
* for the parent of the final component in dirvpp.
*
* This is the central routine in pathname translation and handles
* multiple components in pathnames, separating them at /'s. It also
* implements mounted file systems and processes symbolic links.
*
* vp is the vnode where the directory search should start.
*
* Reference counts: vp must be held prior to calling this function. rootvp
* should only be held if rootvp != rootdir.
*/
int
int flags, /* follow symlinks */
{
int error;
int nlink;
int lookup_flags;
int must_be_directory = 0;
nlink = 0;
if (rpnp)
rpnp->pn_pathlen = 0;
#ifdef C2_AUDIT
if (audit_active)
#endif
/*
* Eliminate any trailing slashes in the pathname.
* If there are any, we must follow all symlinks.
* Also, we must guarantee that the last component is a directory.
*/
if (pn_fixslash(pnp)) {
must_be_directory = 1;
}
next:
/*
* Make sure we have a directory.
*/
goto bad;
}
/*
* Process the next component of the pathname.
*/
#ifdef C2_AUDIT
if (audit_active)
#endif
goto bad;
}
/*
* Handle "..": two special cases.
* 1. If we're at the root directory (e.g. after chroot or
* zone_enter) then change ".." to "." so we can't get
* out of this subtree.
* 2. If this vnode is the root of a mounted file system,
* then replace it with the vnode that was mounted on
* so that we take the ".." in the other file system.
*/
/*
* While we deal with the vfs pointer from the vnode
* the filesystem could have been forcefully unmounted
* and the vnode's v_vfsp could have been invalidated
* by VFS_UNMOUNT. Hence, we cache v_vfsp and use it
* with vfs_rlock_wait/vfs_unlock.
* It is safe to use the v_vfsp even it is freed by
* VFS_UNMOUNT because vfs_rlock_wait/vfs_unlock
* do not dereference v_vfsp. It is just used as a
* magic cookie.
* One more corner case here is the memory getting
* reused for another vfs structure. In this case
* lookuppnvp's vfs_rlock_wait will succeed, domount's
* vfs_lock will fail and domount will bail out with an
* error (EBUSY).
*/
/*
* This lock is used to synchronize
* writers version vfs_lock_wait().
*/
/*
* If this vnode is on a file system that
* has been forcibly unmounted,
* we can't proceed. Cancel this operation
* and return EIO.
*
* vfs_vnodecovered is NULL if unmounted.
* Currently, nfs uses VFS_UNMOUNTED to
* check if it's a forced-umount. Keep the
* same checking here as well even though it
* may not be needed.
*/
return (EIO);
}
goto checkforroot;
}
}
/*
* LOOKUP_CHECKREAD is a private flag used by vnodetopath() to indicate
* that we need to have read permission on every directory in the entire
* path. This is used to ensure that a forward-lookup of a cached value
* has the same effect as a reverse-lookup when the cached value cannot
* be found.
*/
if ((flags & LOOKUP_CHECKREAD) &&
goto bad;
/*
* Perform a lookup in the current directory.
*/
if (error) {
/*
* On error, return hard error if
* (a) we're not at the end of the pathname yet, or
* (b) the caller didn't want the parent directory, or
* (c) we failed for some reason other than a missing entry.
*/
goto bad;
#ifdef C2_AUDIT
if (audit_active) { /* directory access */
goto bad_noaudit;
}
#endif
/*
* We inform the caller that the desired entry must be
* a directory by adding a '/' to the component name.
*/
goto bad;
return (0);
}
/*
* Traverse mount points.
* XXX why don't we need to hold a read lock here (call vn_vfsrlock)?
* What prevents a concurrent update to v_vfsmountedhere?
* Possible answer: if mounting, we might not see the mount
* if it is concurrently coming into existence, but that's
* really not much different from the thread running a bit slower.
* If unmounting, we may get into traverse() when we shouldn't,
* but traverse() will catch this case for us.
* (For this to work, fetching v_vfsmountedhere had better
* be atomic!)
*/
/*
* It is required to assign cvp here, because
* traverse() will return a held vnode which
* may different than the vnode that was passed
* in (even in the error case). If traverse()
* changes the vnode it releases the original,
* and holds the new one.
*/
goto bad;
}
}
/*
* If we hit a symbolic link and there is more path to be
* translated or this operation does not wish to apply
* to a link, then place the contents of the link at the
* front of the remaining pathname.
*/
#ifdef C2_AUDIT
if (audit_active) {
goto bad;
}
#endif
if (++nlink > MAXSYMLINKS) {
goto bad;
}
goto bad;
}
#ifdef C2_AUDIT
if (audit_active)
#endif /* C2_AUDIT */
if (pn_pathleft(&linkpath) == 0)
if (error)
goto bad;
if (pnp->pn_pathlen == 0) {
goto bad;
}
do {
pnp->pn_pathlen--;
}
#ifdef C2_AUDIT
if (audit_active)
#endif
if (pn_fixslash(pnp)) {
must_be_directory = 1;
}
goto next;
}
/*
* If rpnp is non-NULL, remember the resolved path name therein.
* Do not include "." components. Collapse occurrences of
* "previous/..", so long as "previous" is not itself "..".
* Exhausting rpnp results in error ENAMETOOLONG.
*/
rpnp->pn_pathlen != 0 &&
while (rpnp->pn_pathlen &&
rpnp->pn_pathlen--;
rpnp->pn_pathlen--;
} else {
if (rpnp->pn_pathlen != 0 &&
if (error) /* copystr() returns ENAMETOOLONG */
goto bad;
}
}
/*
* If no more components, return last directory (if wanted) and
* last component (if wanted).
*/
if (pn_pathleft(pnp) == 0) {
/*
* If there was a trailing slash in the pathname,
* make sure the last component is a directory.
*/
goto bad;
}
/*
* Check that we have the real parent and not
* an alias of the last component.
*/
#ifdef C2_AUDIT
if (audit_active)
#endif
return (EINVAL);
}
#ifdef C2_AUDIT
if (audit_active) {
goto bad;
}
#endif
} else
#ifdef C2_AUDIT
if (audit_active)
#endif
else
if (rpnp) {
else if (rpnp->pn_pathlen == 0)
}
else
return (0);
}
#ifdef C2_AUDIT
if (audit_active) {
goto bad;
}
#endif
/*
* Skip over slashes from end of last component.
*/
pnp->pn_pathlen--;
}
/*
* Searched through another level of directory:
* release previous directory handle and save new (result
* of lookup) as current directory.
*/
goto next;
bad:
#ifdef C2_AUDIT
if (audit_active) /* reached end of path */
#endif
/*
* Error. Release vnodes and return.
*/
if (cvp)
/*
* If the error was ESTALE and the current directory to look in
* was the root for this lookup, the root for a mounted file
* system, or the starting directory for lookups, then
* return ENOENT instead of ESTALE. In this case, no recovery
* is possible by the higher level. If ESTALE was returned for
* some intermediate directory along the path, then recovery
* is potentially possible and retrying from the higher level
* will either correct the situation by purging stale cache
* entries or eventually get back to the point where no recovery
* is possible.
*/
return (error);
}
/*
* Traverse a mount point. Routine accepts a vnode pointer as a reference
* parameter and performs the indirection, releasing the original vnode.
*/
int
{
int error = 0;
/*
* If this vnode is mounted on, then we transparently indirect
* to the vnode which is the root of the mounted file system.
* Before we do this we must check that an unmount is not in
* progress on this vnode.
*/
for (;;) {
/*
* Try to read lock the vnode. If this fails because
* the vnode is already write locked, then check to
* see whether it is the current thread which locked
* the vnode. If it is not, then read lock the vnode
* by waiting to acquire the lock.
*
* The code path in domount() is an example of support
* which needs to look up two pathnames and locks one
* of them in between the two lookups.
*/
if (error) {
if (!vn_vfswlock_held(cvp))
if (error != 0) {
/*
* lookuppn() expects a held vnode to be
* returned because it promptly calls
* VN_RELE after the error return
*/
return (error);
}
}
/*
* Reached the end of the mount chain?
*/
break;
}
/*
* The read lock must be held across the call to VFS_ROOT() to
* prevent a concurrent unmount from destroying the vfs.
*/
if (error)
break;
}
return (error);
}
/*
* Return the lowermost vnode if this is a mountpoint.
*/
static vnode_t *
{
break;
}
}
return (vp);
}
static int
{
/*
* If we have a device file, check to see if is a cloned open of the
* same device. For self-cloning devices, the major numbers will match.
* For devices cloned through the 'clone' driver, the minor number of
* the source device will be the same as the major number of the cloned
* device.
*/
return (1);
if (spec_is_clone(v1) &&
return (1);
if (spec_is_clone(v2) &&
return (1);
}
/*
* This check for symbolic links handles the pseudo-symlinks in procfs.
* These particular links have v_type of VDIR, but the attributes have a
* type of VLNK. We need to avoid these links because otherwise if we
* as the same vnode.
*/
return (0);
return (0);
}
/*
* Find the entry in the directory corresponding to the target vnode.
*/
int
{
int err;
int eof;
/*
* This is necessary because of the strange semantics of VOP_LOOKUP().
*/
eof = 0;
uio.uio_loffset = 0;
return (err);
while (!eof) {
break;
/*
* Ignore '.' and '..' entries
*/
continue;
}
/*
* We only want to bail out if there was an error other
* than ENOENT. Otherwise, it could be that someone
* just removed an entry since the readdir() call, and
* the entry we want is further on in the directory.
*/
if (err == 0) {
return (0);
}
return (err);
}
}
}
/*
* Something strange has happened, this directory does not contain the
* specified vnode. This should never happen in the normal case, since
* we ensured that dvp is the parent of vp. This may be possible in
* some race conditions, so fail gracefully.
*/
if (err == 0)
return (err);
}
/*
* Given a global path (from rootdir), and a vnode that is the current root,
* return the portion of the path that is beneath the current root or NULL on
* failure. The path MUST be a resolved path (no '..' entries or symlinks),
* otherwise this function will fail.
*/
static char *
{
char component[MAXNAMELEN];
/*
* We use vn_compare() instead of VN_CMP() in order to detect lofs
* mounts and stacked vnodes.
*/
return (path);
return (NULL);
while (pn_pathleft(&pn)) {
pn_skipslash(&pn);
break;
break;
break;
break;
}
}
return (ret);
}
/*
* Given a directory, return the full, resolved path. This looks up "..",
* searches for the given vnode in the parent, appends the component, etc. It
* is used to implement vnodetopath() and getcwd() when the cached path fails
* (or vfs_vnode_path is not set).
*/
static int
{
int err = 0;
char *dbuf;
dirent64_t *dp;
char *bufloc;
/* Operation only allowed on directories */
/* We must have at least enough space for "/" */
if (buflen < 2)
return (ENAMETOOLONG);
/* Start at end of string with terminating null */
*bufloc = '\0';
/*
* Begin with an additional reference on vp. This will be decremented
* during the loop.
*/
for (;;) {
/*
* Return if we've reached the root. If the buffer is empty,
* return '/'. We explicitly don't use vn_compare(), since it
* compares the real vnodes. A lofs mount of '/' would produce
* incorrect results otherwise.
*/
if (*bufloc == '\0')
*--bufloc = '/';
break;
}
/*
* If we've reached the VFS root, something has gone wrong. We
* should have reached the root in the above check. The only
* explantation is that 'vp' is not contained withing the given
* root, in which case we return EPERM.
*/
goto out;
}
/*
* Shortcut: see if this vnode is a mountpoint. If so,
* grab the path information from the vfs_t.
*/
== 0) {
/*
* Ensure the mointpoint still exists.
*/
goto out;
}
complen);
break;
} else {
}
}
} else {
}
}
/*
* Shortcuts failed, search for this vnode in its parent. If
* this is a mountpoint, then get the vnode underneath.
*/
!= 0)
goto out;
/*
* With extended attributes, it's possible for a directory to
* have a parent that is a regular file. Check for that here.
*/
goto out;
}
/*
* If this is true, something strange has happened. This is
* only true if we are the root of a filesystem, which should
* have been caught by the check above.
*/
goto out;
}
/*
* Search the parent directory for the entry corresponding to
* this vnode.
*/
!= 0)
goto out;
err = ENAMETOOLONG;
goto out;
}
/* Prepend a slash to the current path. */
*--bufloc = '/';
/* And continue with the next component */
}
/*
* Place the path at the beginning of the buffer.
*/
out:
/*
* If the error was ESTALE and the current directory to look in
* was the root for this lookup, the root for a mounted file
* system, or the starting directory for lookups, then
* return ENOENT instead of ESTALE. In this case, no recovery
* is possible by the higher level. If ESTALE was returned for
* some intermediate directory along the path, then recovery
* is potentially possible and retrying from the higher level
* will either correct the situation by purging stale cache
* entries or eventually get back to the point where no recovery
* is possible.
*/
if (pvp)
return (err);
}
/*
* The additional flag, LOOKUP_CHECKREAD, is ued to enforce artificial
* constraints in order to be standards compliant. For example, if we have
* only), then we can legitimately look up the path to the current working
* directory without needing read permission. Existing standards tests,
* however, assume that we are determining the path by repeatedly looking up
* "..". We need to keep this behavior in order to maintain backwards
* compatibility.
*/
static int
{
char path[MAXNAMELEN];
int doclose = 0;
/*
* If vrootp is NULL, get the root for curproc. Callers with any other
* requirements should pass in a different vrootp.
*/
mutex_enter(&p->p_lock);
mutex_exit(&p->p_lock);
} else {
}
/*
* This is to get around an annoying artifact of the /proc filesystem,
* directory is. We can't rely on VOP_REALVP(), since that will break
* lofs. The only difference between procfs and lofs is that opening
* the file will return the underling vnode in the case of procfs.
*/
doclose = 1;
else
}
/*
* Check to see if we have a cached path in the vnode.
*/
/* We should only cache absolute paths */
/*
* If we are in a zone or a chroot environment, then we have to
* take additional steps, since the path to the root might not
* be readable with the current credentials, even though the
* process can legitmately access the file. In this case, we
* do the following:
*
* lookuppnvp() with all privileges to get the resolved path.
* call localpath() to get the local portion of the path, and
* continue as normal.
*
* If the the conversion to a local path fails, then we continue
* as normal. This is a heuristic to make process object file
* paths available from within a zone. Because lofs doesn't
* support page operations, the vnode stored in the seg_t is
* actually the underlying real vnode, not the lofs node itself.
* Most of the time, the lofs path is the same as the underlying
*/
kcred);
}
/*
* The original pn was changed through lookuppnvp(), so
* reset it.
*/
if (local) {
} else {
} else {
goto notcached;
}
}
}
/*
* We should have a local path at this point, so start the
* search from the root of the current process.
*/
if (ret == 0) {
/*
* Check to see if the returned vnode is the same as
* the one we expect. If not, give up.
*/
goto notcached;
}
/*
* Return the result.
*/
goto notcached;
if (doclose) {
}
return (0);
}
} else {
}
/*
* If we don't have a directory, try to find it in the dnlc via
* reverse lookup. Once this is found, we can use the regular
* directory search to find the full path.
*/
if (ret == 0) {
ret = ENAMETOOLONG;
} else {
}
}
} else
} else
if (doclose) {
}
return (ret);
}
int
{
}
int
{
int ret;
const char *value;
/*
* Check to see if there is a cached version of the cwd. If so, lookup
* the cached value and make sure it is the same vnode.
*/
mutex_enter(&p->p_lock);
mutex_exit(&p->p_lock);
/*
* Make sure we have permission to access the current directory.
*/
return (ret);
}
if (cwd) {
return (ret);
}
return (ENAMETOOLONG);
}
return (0);
}
}
}
/*
* Store the new cwd and replace the existing cached copy.
*/
if (ret == 0)
else
mutex_enter(&p->p_lock);
mutex_exit(&p->p_lock);
if (oldcwd)
return (ret);
}