/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (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 2013 Nexenta Systems, Inc. All rights reserved.
*/
/* 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.
*/
#include <sys/pathname.h>
#include <sys/sysmacros.h>
/*
* 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;
if (flags & FIGNORECASE) {
}
/*
* 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.
*/
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.
*/
if (pp)
return (EIO);
}
/*
* Crossing mount points. For eg: We are doing
* a lookup of ".." for file systems root vnode
* mounted here, and VOP_LOOKUP() (with covered vnode)
* will be on underlying file systems mount point
* vnode. Set retry_with_kcred flag as we might end
* up doing VOP_LOOKUP() with kcred if required.
*/
goto checkforroot;
}
}
/*
* Perform a lookup in the current directory.
*/
/*
* Retry with kcred - If crossing mount points & error is EACCES.
*
* If we are crossing mount points here and doing ".." lookup,
* VOP_LOOKUP() might fail if the underlying file systems
* mount point has no execute permission. In cases like these,
* we retry VOP_LOOKUP() by giving as much privilage as possible
* by passing kcred credentials.
*
* In case of hierarchical file systems, passing kcred still may
* or may not work.
* For eg: UFS FS --> Mount NFS FS --> Again mount UFS on some
* directory inside NFS FS.
*/
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;
/*
* We inform the caller that the desired entry must be
* a directory by adding a '/' to the component name.
*/
goto bad;
if (pp)
return (0);
}
/*
* Traverse mount points.
*/
/*
* 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.
*/
if (++nlink > MAXSYMLINKS) {
goto bad;
}
goto bad;
}
if (pn_pathleft(&linkpath) == 0)
if (error)
goto bad;
if (pnp->pn_pathlen == 0) {
goto bad;
}
do {
pnp->pn_pathlen--;
}
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 (flags & FIGNORECASE) {
/*
* Return the case-preserved name
* within the resolved path.
*/
} else {
}
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.
*/
if (pp)
return (EINVAL);
}
} else
else
if (rpnp) {
else if (rpnp->pn_pathlen == 0)
}
else
if (pp)
return (0);
}
/*
* 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:
/*
* 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.
*/
if (pp)
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 (;;) {
/*
* Used to try to read lock the vnode here.
*/
/*
* 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);
}
/*
* Get the vnode path, relative to the passed rootvp.
* Our vncache always fills in v_path, so this is easy.
*/
/* ARGSUSED */
int
{
if (vrootp)
p += rvp_len;
else
p = "/";
return (0);
}