#include <sys/param.h>
#include <sys/systm.h>
#include <sys/syslimits.h>
#include <sys/time.h>
#include <sys/namei.h>
#include <sys/vm.h>
#include <sys/vnode_internal.h>
#include <sys/mount_internal.h>
#include <sys/errno.h>
#include <sys/malloc.h>
#include <sys/filedesc.h>
#include <sys/proc_internal.h>
#include <sys/kdebug.h>
#include <sys/unistd.h>
#include <sys/uio_internal.h>
#include <sys/kauth.h>
#include <kern/kalloc.h>
#include <security/audit/audit.h>
#if CONFIG_MACF
#include <security/mac_framework.h>
#endif
#if NAMEDRSRCFORK
#include <sys/xattr.h>
#endif
#define VOLFS_MIN_PATH_LEN 9
static void kdebug_lookup(struct vnode *dp, struct componentname *cnp);
#if CONFIG_VOLFS
static int vfs_getrealpath(const char * path, char * realpath, size_t bufsize, vfs_context_t ctx);
#define MAX_VOLFS_RESTARTS 5
#endif
static int lookup_traverse_mountpoints(struct nameidata *ndp, struct componentname *cnp, vnode_t dp, int vbusyflags, vfs_context_t ctx);
static int lookup_handle_symlink(struct nameidata *ndp, vnode_t *new_dp, vfs_context_t ctx);
static int lookup_authorize_search(vnode_t dp, struct componentname *cnp, int dp_authorized_in_cache, vfs_context_t ctx);
static void lookup_consider_update_cache(vnode_t dvp, vnode_t vp, struct componentname *cnp, int nc_generation);
static int lookup_handle_found_vnode(struct nameidata *ndp, struct componentname *cnp, int rdonly,
int vbusyflags, int *keep_going, int nc_generation,
int wantparent, int atroot, vfs_context_t ctx);
static int lookup_handle_emptyname(struct nameidata *ndp, struct componentname *cnp, int wantparent);
#if NAMEDRSRCFORK
static int lookup_handle_rsrc_fork(vnode_t dp, struct nameidata *ndp, struct componentname *cnp, int wantparent, vfs_context_t ctx);
#endif
int
namei(struct nameidata *ndp)
{
struct filedesc *fdp;
struct vnode *dp;
struct vnode *usedvp = ndp->ni_dvp;
u_long cnpflags = ndp->ni_cnd.cn_flags;
int error;
struct componentname *cnp = &ndp->ni_cnd;
vfs_context_t ctx = cnp->cn_context;
proc_t p = vfs_context_proc(ctx);
#if CONFIG_AUDIT
uthread_t ut = (struct uthread *)get_bsdthread_info(current_thread());
#endif
#if CONFIG_VOLFS
int volfs_restarts = 0;
#endif
fdp = p->p_fd;
#if DIAGNOSTIC
if (!vfs_context_ucred(ctx) || !p)
panic ("namei: bad cred/proc");
if (cnp->cn_nameiop & (~OPMASK))
panic ("namei: nameiop contaminated with flags");
if (cnp->cn_flags & OPMASK)
panic ("namei: flags contaminated with nameiops");
#endif
if (ndp->ni_flag & NAMEI_CONTLOOKUP) {
int rdonly, vbusyflags, keep_going, wantparent;
rdonly = cnp->cn_flags & RDONLY;
vbusyflags = ((cnp->cn_flags & CN_NBMOUNTLOOK) != 0) ? LK_NOWAIT : 0;
keep_going = 0;
wantparent = cnp->cn_flags & (LOCKPARENT | WANTPARENT);
ndp->ni_flag &= ~(NAMEI_CONTLOOKUP);
error = lookup_handle_found_vnode(ndp, &ndp->ni_cnd, rdonly, vbusyflags,
&keep_going, ndp->ni_ncgeneration, wantparent, 0, ctx);
if (error)
goto out_drop;
if (keep_going) {
if ((cnp->cn_flags & ISSYMLINK) == 0) {
panic("We need to keep going on a continued lookup, but for vp type %d (tag %d)\n", ndp->ni_vp->v_type, ndp->ni_vp->v_tag);
}
goto continue_symlink;
}
return 0;
}
vnode_recycled:
if ((cnp->cn_flags & HASBUF) == 0) {
cnp->cn_pnbuf = ndp->ni_pathbuf;
cnp->cn_pnlen = PATHBUFLEN;
}
#if LP64_DEBUG
if ((UIO_SEG_IS_USER_SPACE(ndp->ni_segflg) == 0)
&& (ndp->ni_segflg != UIO_SYSSPACE)
&& (ndp->ni_segflg != UIO_SYSSPACE32)) {
panic("%s :%d - invalid ni_segflg\n", __FILE__, __LINE__);
}
#endif
retry_copy:
if (UIO_SEG_IS_USER_SPACE(ndp->ni_segflg)) {
error = copyinstr(ndp->ni_dirp, cnp->cn_pnbuf,
cnp->cn_pnlen, (size_t *)&ndp->ni_pathlen);
} else {
error = copystr(CAST_DOWN(void *, ndp->ni_dirp), cnp->cn_pnbuf,
cnp->cn_pnlen, (size_t *)&ndp->ni_pathlen);
}
if (error == ENAMETOOLONG && !(cnp->cn_flags & HASBUF)) {
MALLOC_ZONE(cnp->cn_pnbuf, caddr_t, MAXPATHLEN, M_NAMEI, M_WAITOK);
if (cnp->cn_pnbuf == NULL) {
error = ENOMEM;
goto error_out;
}
cnp->cn_flags |= HASBUF;
cnp->cn_pnlen = MAXPATHLEN;
goto retry_copy;
}
if (error)
goto error_out;
if (proc_is_forcing_hfs_case_sensitivity(p))
cnp->cn_flags |= CN_SKIPNAMECACHE;
#if CONFIG_VOLFS
if (ndp->ni_pathlen >= VOLFS_MIN_PATH_LEN &&
cnp->cn_pnbuf[0] == '/' &&
cnp->cn_pnbuf[1] == '.' &&
cnp->cn_pnbuf[2] == 'v' &&
cnp->cn_pnbuf[3] == 'o' &&
cnp->cn_pnbuf[4] == 'l' &&
cnp->cn_pnbuf[5] == '/' ) {
char * realpath;
int realpath_err;
MALLOC_ZONE(realpath, caddr_t, MAXPATHLEN, M_NAMEI, M_WAITOK);
if (realpath) {
if ((realpath_err= vfs_getrealpath(&cnp->cn_pnbuf[6], realpath, MAXPATHLEN, ctx))) {
FREE_ZONE(realpath, MAXPATHLEN, M_NAMEI);
if (realpath_err == ENOSPC || realpath_err == ENAMETOOLONG){
error = ENAMETOOLONG;
goto error_out;
}
} else {
if (cnp->cn_flags & HASBUF) {
FREE_ZONE(cnp->cn_pnbuf, cnp->cn_pnlen, M_NAMEI);
}
cnp->cn_pnbuf = realpath;
cnp->cn_pnlen = MAXPATHLEN;
ndp->ni_pathlen = strlen(realpath) + 1;
cnp->cn_flags |= HASBUF | CN_VOLFSPATH;
}
}
}
#endif
#if CONFIG_AUDIT
if (cnp->cn_flags & AUDITVNPATH1)
AUDIT_ARG(upath, ut->uu_cdir, cnp->cn_pnbuf, ARG_UPATH1);
if (cnp->cn_flags & AUDITVNPATH2)
AUDIT_ARG(upath, ut->uu_cdir, cnp->cn_pnbuf, ARG_UPATH2);
#endif
if (*cnp->cn_pnbuf == '\0') {
error = ENOENT;
goto error_out;
}
ndp->ni_loopcnt = 0;
if ((ndp->ni_rootdir = fdp->fd_rdir) == NULLVP) {
if ( !(fdp->fd_flags & FD_CHROOT))
ndp->ni_rootdir = rootvnode;
}
cnp->cn_nameptr = cnp->cn_pnbuf;
ndp->ni_usedvp = NULLVP;
if (*(cnp->cn_nameptr) == '/') {
while (*(cnp->cn_nameptr) == '/') {
cnp->cn_nameptr++;
ndp->ni_pathlen--;
}
dp = ndp->ni_rootdir;
} else if (cnp->cn_flags & USEDVP) {
dp = ndp->ni_dvp;
ndp->ni_usedvp = dp;
} else
dp = vfs_context_cwd(ctx);
if (dp == NULLVP || (dp->v_lflag & VL_DEAD)) {
error = ENOENT;
goto error_out;
}
ndp->ni_dvp = NULLVP;
ndp->ni_vp = NULLVP;
for (;;) {
ndp->ni_startdir = dp;
if ( (error = lookup(ndp)) ) {
goto error_out;
}
if ((cnp->cn_flags & ISSYMLINK) == 0) {
return (0);
}
continue_symlink:
error = lookup_handle_symlink(ndp, &dp, ctx);
if (error != 0) {
break;
}
}
out_drop:
if (ndp->ni_dvp)
vnode_put(ndp->ni_dvp);
if (ndp->ni_vp)
vnode_put(ndp->ni_vp);
error_out:
if ( (cnp->cn_flags & HASBUF) ) {
cnp->cn_flags &= ~HASBUF;
FREE_ZONE(cnp->cn_pnbuf, cnp->cn_pnlen, M_NAMEI);
}
cnp->cn_pnbuf = NULL;
ndp->ni_vp = NULLVP;
ndp->ni_dvp = NULLVP;
#if CONFIG_VOLFS
if ((error == ENOENT) && (ndp->ni_cnd.cn_flags & CN_VOLFSPATH)) {
if (volfs_restarts < MAX_VOLFS_RESTARTS) {
volfs_restarts++;
goto vnode_recycled;
}
}
#endif
if (error == ERECYCLE){
ndp->ni_cnd.cn_flags = cnpflags;
ndp->ni_dvp = usedvp;
goto vnode_recycled;
}
return (error);
}
int
namei_compound_available(vnode_t dp, struct nameidata *ndp)
{
if ((ndp->ni_flag & NAMEI_COMPOUNDOPEN) != 0) {
return vnode_compound_open_available(dp);
}
return 0;
}
static int
lookup_authorize_search(vnode_t dp, struct componentname *cnp, int dp_authorized_in_cache, vfs_context_t ctx)
{
#if !CONFIG_MACF
#pragma unused(cnp)
#endif
int error;
if (!dp_authorized_in_cache) {
error = vnode_authorize(dp, NULL, KAUTH_VNODE_SEARCH, ctx);
if (error)
return error;
}
#if CONFIG_MACF
error = mac_vnode_check_lookup(ctx, dp, cnp);
if (error)
return error;
#endif
return 0;
}
static void
lookup_consider_update_cache(vnode_t dvp, vnode_t vp, struct componentname *cnp, int nc_generation)
{
int isdot_or_dotdot;
isdot_or_dotdot = (cnp->cn_namelen == 1 && cnp->cn_nameptr[0] == '.') || (cnp->cn_flags & ISDOTDOT);
if (vp->v_name == NULL || vp->v_parent == NULLVP) {
int update_flags = 0;
if (isdot_or_dotdot == 0) {
if (vp->v_name == NULL)
update_flags |= VNODE_UPDATE_NAME;
if (dvp != NULLVP && vp->v_parent == NULLVP)
update_flags |= VNODE_UPDATE_PARENT;
if (update_flags)
vnode_update_identity(vp, dvp, cnp->cn_nameptr, cnp->cn_namelen, cnp->cn_hash, update_flags);
}
}
if ( (cnp->cn_flags & MAKEENTRY) && (vp->v_flag & VNCACHEABLE) && LIST_FIRST(&vp->v_nclinks) == NULL) {
if (dvp != NULLVP && (nc_generation == dvp->v_nc_generation) && (!isdot_or_dotdot))
cache_enter_with_gen(dvp, vp, cnp, nc_generation);
}
}
#if NAMEDRSRCFORK
static int
lookup_handle_rsrc_fork(vnode_t dp, struct nameidata *ndp, struct componentname *cnp, int wantparent, vfs_context_t ctx)
{
vnode_t svp = NULLVP;
enum nsoperation nsop;
int error;
if (dp->v_type != VREG) {
error = ENOENT;
goto out;
}
switch (cnp->cn_nameiop) {
case DELETE:
if (cnp->cn_flags & CN_ALLOWRSRCFORK) {
nsop = NS_DELETE;
} else {
error = EPERM;
goto out;
}
break;
case CREATE:
if (cnp->cn_flags & CN_ALLOWRSRCFORK) {
nsop = NS_CREATE;
} else {
error = EPERM;
goto out;
}
break;
case LOOKUP:
if (cnp->cn_flags & CN_ALLOWRSRCFORK) {
nsop = NS_OPEN;
} else {
error = EPERM;
goto out;
}
break;
default:
error = EPERM;
goto out;
}
error = vnode_getnamedstream(dp, &svp, XATTR_RESOURCEFORK_NAME, nsop, 0, ctx);
if (error == ENOATTR || error == ENOENT) {
error = (nsop == NS_CREATE) ? 0 : ENOENT;
}
if (error) {
goto out;
}
if (wantparent) {
if (ndp->ni_dvp) {
vnode_put(ndp->ni_dvp);
}
ndp->ni_dvp = dp;
} else {
vnode_put(dp);
}
ndp->ni_vp = svp;
if (ndp->ni_pathlen == 1 && ndp->ni_next[0] == '\0') {
ndp->ni_next[0] = '/';
}
cnp->cn_flags &= ~MAKEENTRY;
return 0;
out:
return error;
}
#endif
static int
lookup_handle_found_vnode(struct nameidata *ndp, struct componentname *cnp, int rdonly,
int vbusyflags, int *keep_going, int nc_generation,
int wantparent, int atroot, vfs_context_t ctx)
{
vnode_t dp;
int error;
char *cp;
dp = ndp->ni_vp;
*keep_going = 0;
if (ndp->ni_vp == NULLVP) {
panic("NULL ni_vp in %s\n", __FUNCTION__);
}
if (atroot) {
goto nextname;
}
#if CONFIG_TRIGGERS
if (dp->v_resolve) {
error = vnode_trigger_resolve(dp, ndp, ctx);
if (error) {
goto out;
}
}
#endif
if (cnp->cn_consume > 0) {
cnp->cn_nameptr += cnp->cn_consume;
ndp->ni_next += cnp->cn_consume;
ndp->ni_pathlen -= cnp->cn_consume;
cnp->cn_consume = 0;
} else {
lookup_consider_update_cache(ndp->ni_dvp, dp, cnp, nc_generation);
}
error = lookup_traverse_mountpoints(ndp, cnp, dp, vbusyflags, ctx);
dp = ndp->ni_vp;
if (error) {
goto out;
}
#if CONFIG_MACF
if (vfs_flags(vnode_mount(dp)) & MNT_MULTILABEL) {
error = vnode_label(vnode_mount(dp), NULL, dp, NULL, 0, ctx);
if (error)
goto out;
}
#endif
if ((dp->v_type == VLNK) &&
((cnp->cn_flags & FOLLOW) || (ndp->ni_flag & NAMEI_TRAILINGSLASH) || *ndp->ni_next == '/')) {
cnp->cn_flags |= ISSYMLINK;
*keep_going = 1;
return (0);
}
if ((ndp->ni_flag & NAMEI_TRAILINGSLASH)) {
if (dp->v_type != VDIR) {
error = ENOTDIR;
goto out;
}
ndp->ni_flag &= ~(NAMEI_TRAILINGSLASH);
}
#if NAMEDSTREAMS
if (vnode_isshadow (dp)) {
error = ENOENT;
goto out;
}
#endif
nextname:
if (*ndp->ni_next == '/') {
cnp->cn_nameptr = ndp->ni_next + 1;
ndp->ni_pathlen--;
while (*cnp->cn_nameptr == '/') {
cnp->cn_nameptr++;
ndp->ni_pathlen--;
}
cp = cnp->cn_nameptr;
vnode_put(ndp->ni_dvp);
ndp->ni_dvp = NULLVP;
if (*cp == '\0') {
goto emptyname;
}
*keep_going = 1;
return 0;
}
if (rdonly &&
(cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME)) {
error = EROFS;
goto out;
}
if (cnp->cn_flags & SAVESTART) {
if ( (vnode_get(ndp->ni_dvp)) ) {
error = ENOENT;
goto out;
}
ndp->ni_startdir = ndp->ni_dvp;
}
if (!wantparent && ndp->ni_dvp) {
vnode_put(ndp->ni_dvp);
ndp->ni_dvp = NULLVP;
}
if (cnp->cn_flags & AUDITVNPATH1)
AUDIT_ARG(vnpath, dp, ARG_VNODE1);
else if (cnp->cn_flags & AUDITVNPATH2)
AUDIT_ARG(vnpath, dp, ARG_VNODE2);
#if NAMEDRSRCFORK
if ((cnp->cn_flags & CN_WANTSRSRCFORK) && (dp != NULLVP)) {
error = lookup_handle_rsrc_fork(dp, ndp, cnp, wantparent, ctx);
if (error != 0)
goto out;
dp = ndp->ni_vp;
}
#endif
if (kdebug_enable)
kdebug_lookup(ndp->ni_vp, cnp);
return 0;
emptyname:
error = lookup_handle_emptyname(ndp, cnp, wantparent);
if (error != 0)
goto out;
return 0;
out:
return error;
}
static int
lookup_handle_emptyname(struct nameidata *ndp, struct componentname *cnp, int wantparent)
{
vnode_t dp;
int error = 0;
dp = ndp->ni_vp;
cnp->cn_namelen = 0;
if (dp->v_type != VDIR) {
error = ENOTDIR;
goto out;
}
if (cnp->cn_nameiop != LOOKUP) {
error = EISDIR;
goto out;
}
if (wantparent) {
if ( (vnode_get(dp)) ) {
error = ENOENT;
goto out;
}
ndp->ni_dvp = dp;
}
cnp->cn_flags &= ~ISDOTDOT;
cnp->cn_flags |= ISLASTCN;
ndp->ni_next = cnp->cn_nameptr;
ndp->ni_vp = dp;
if (cnp->cn_flags & AUDITVNPATH1)
AUDIT_ARG(vnpath, dp, ARG_VNODE1);
else if (cnp->cn_flags & AUDITVNPATH2)
AUDIT_ARG(vnpath, dp, ARG_VNODE2);
if (cnp->cn_flags & SAVESTART)
panic("lookup: SAVESTART");
return 0;
out:
return error;
}
int
lookup(struct nameidata *ndp)
{
char *cp;
vnode_t tdp;
vnode_t dp;
int docache = 1;
int wantparent;
int rdonly;
int dp_authorized = 0;
int error = 0;
struct componentname *cnp = &ndp->ni_cnd;
vfs_context_t ctx = cnp->cn_context;
int vbusyflags = 0;
int nc_generation = 0;
vnode_t last_dp = NULLVP;
int keep_going;
int atroot;
if (cnp->cn_flags & NOCACHE) {
docache = 0;
}
wantparent = cnp->cn_flags & (LOCKPARENT | WANTPARENT);
rdonly = cnp->cn_flags & RDONLY;
cnp->cn_flags &= ~ISSYMLINK;
cnp->cn_consume = 0;
dp = ndp->ni_startdir;
ndp->ni_startdir = NULLVP;
if ((cnp->cn_flags & CN_NBMOUNTLOOK) != 0)
vbusyflags = LK_NOWAIT;
cp = cnp->cn_nameptr;
if (*cp == '\0') {
if ( (vnode_getwithref(dp)) ) {
dp = NULLVP;
error = ENOENT;
goto bad;
}
ndp->ni_vp = dp;
error = lookup_handle_emptyname(ndp, cnp, wantparent);
if (error) {
goto bad;
}
return 0;
}
dirloop:
atroot = 0;
ndp->ni_vp = NULLVP;
if ( (error = cache_lookup_path(ndp, cnp, dp, ctx, &dp_authorized, last_dp)) ) {
dp = NULLVP;
goto bad;
}
if ((cnp->cn_flags & ISLASTCN)) {
if (docache)
cnp->cn_flags |= MAKEENTRY;
} else
cnp->cn_flags |= MAKEENTRY;
dp = ndp->ni_dvp;
if (ndp->ni_vp != NULLVP) {
if (dp != NULLVP)
nc_generation = dp->v_nc_generation - 1;
goto returned_from_lookup_path;
}
if ( (cnp->cn_flags & ISDOTDOT) ) {
for (;;) {
if (dp == ndp->ni_rootdir || dp == rootvnode) {
ndp->ni_dvp = dp;
ndp->ni_vp = dp;
if ( (vnode_get(dp)) ) {
error = ENOENT;
goto bad;
}
atroot = 1;
goto returned_from_lookup_path;
}
if ((dp->v_flag & VROOT) == 0 ||
(cnp->cn_flags & NOCROSSMOUNT))
break;
if (dp->v_mount == NULL) {
error = EBADF;
goto bad;
}
tdp = dp;
dp = tdp->v_mount->mnt_vnodecovered;
vnode_put(tdp);
if ( (vnode_getwithref(dp)) ) {
dp = NULLVP;
error = ENOENT;
goto bad;
}
ndp->ni_dvp = dp;
dp_authorized = 0;
}
}
unionlookup:
ndp->ni_vp = NULLVP;
if (dp->v_type != VDIR) {
error = ENOTDIR;
goto lookup_error;
}
if ( (cnp->cn_flags & DONOTAUTH) != DONOTAUTH ) {
error = lookup_authorize_search(dp, cnp, dp_authorized, ctx);
if (error) {
goto lookup_error;
}
}
#if NAMEDRSRCFORK
if ((cnp->cn_flags & ISLASTCN) && namei_compound_available(dp, ndp) && !(cnp->cn_flags & CN_WANTSRSRCFORK)) {
#else
if ((cnp->cn_flags & ISLASTCN) && namei_compound_available(dp, ndp)) {
#endif
ndp->ni_flag |= NAMEI_UNFINISHED;
ndp->ni_ncgeneration = dp->v_nc_generation;
return 0;
}
nc_generation = dp->v_nc_generation;
error = VNOP_LOOKUP(dp, &ndp->ni_vp, cnp, ctx);
if ( error ) {
lookup_error:
if ((error == ENOENT) &&
(dp->v_mount != NULL) &&
(dp->v_mount->mnt_flag & MNT_UNION)) {
tdp = dp;
error = lookup_traverse_union(tdp, &dp, ctx);
vnode_put(tdp);
if (error) {
dp = NULLVP;
goto bad;
}
ndp->ni_dvp = dp;
dp_authorized = 0;
goto unionlookup;
}
if (error != EJUSTRETURN)
goto bad;
if (ndp->ni_vp != NULLVP)
panic("leaf should be empty");
#if NAMEDRSRCFORK
if (cnp->cn_flags & CN_WANTSRSRCFORK) {
error = ENOENT;
goto bad;
}
#endif
error = lookup_validate_creation_path(ndp);
if (error)
goto bad;
if (cnp->cn_flags & SAVESTART) {
if ( (vnode_get(ndp->ni_dvp)) ) {
error = ENOENT;
goto bad;
}
ndp->ni_startdir = ndp->ni_dvp;
}
if (!wantparent)
vnode_put(ndp->ni_dvp);
if (kdebug_enable)
kdebug_lookup(ndp->ni_dvp, cnp);
return (0);
}
returned_from_lookup_path:
error = lookup_handle_found_vnode(ndp, cnp, rdonly, vbusyflags, &keep_going, nc_generation, wantparent, atroot, ctx);
if (error != 0) {
goto bad2;
}
if (keep_going) {
dp = ndp->ni_vp;
if ((dp->v_type == VLNK) &&
((cnp->cn_flags & FOLLOW) || (ndp->ni_flag & NAMEI_TRAILINGSLASH) || *ndp->ni_next == '/')) {
return 0;
}
last_dp = dp;
goto dirloop;
}
return (0);
bad2:
if (ndp->ni_dvp)
vnode_put(ndp->ni_dvp);
vnode_put(ndp->ni_vp);
ndp->ni_vp = NULLVP;
if (kdebug_enable)
kdebug_lookup(dp, cnp);
return (error);
bad:
if (dp)
vnode_put(dp);
ndp->ni_vp = NULLVP;
if (kdebug_enable)
kdebug_lookup(dp, cnp);
return (error);
}
int
lookup_traverse_union(vnode_t dvp, vnode_t *new_dvp, vfs_context_t ctx)
{
char *path = NULL, *pp;
const char *name, *np;
int len;
int error = 0;
struct nameidata nd;
vnode_t vp = dvp;
*new_dvp = NULL;
if (vp && vp->v_flag & VROOT) {
*new_dvp = vp->v_mount->mnt_vnodecovered;
if (vnode_getwithref(*new_dvp))
return ENOENT;
return 0;
}
path = (char *) kalloc(MAXPATHLEN);
if (path == NULL) {
error = ENOMEM;
goto done;
}
pp = path + (MAXPATHLEN - 1);
*pp = '\0';
while (1) {
name = vnode_getname(vp);
if (name == NULL) {
printf("lookup_traverse_union: null parent name: .%s\n", pp);
error = ENOENT;
goto done;
}
len = strlen(name);
if ((len + 1) > (pp - path)) { error = ENAMETOOLONG;
vnode_putname(name);
goto done;
}
for (np = name + len; len > 0; len--) *--pp = *--np;
vnode_putname(name);
vp = vp->v_parent;
if (vp == NULLVP || vp->v_flag & VROOT)
break;
*--pp = '/';
}
NDINIT(&nd, LOOKUP, OP_LOOKUP, USEDVP, UIO_SYSSPACE, CAST_USER_ADDR_T(pp), ctx);
nd.ni_dvp = dvp->v_mount->mnt_vnodecovered;
error = namei(&nd);
if (error == 0)
*new_dvp = nd.ni_vp;
nameidone(&nd);
done:
if (path)
kfree(path, MAXPATHLEN);
return error;
}
int
lookup_validate_creation_path(struct nameidata *ndp)
{
struct componentname *cnp = &ndp->ni_cnd;
if (cnp->cn_flags & RDONLY) {
return EROFS;
}
if ((cnp->cn_flags & ISLASTCN) && (ndp->ni_flag & NAMEI_TRAILINGSLASH) && !(cnp->cn_flags & WILLBEDIR)) {
return ENOENT;
}
return 0;
}
static int
lookup_traverse_mountpoints(struct nameidata *ndp, struct componentname *cnp, vnode_t dp,
int vbusyflags, vfs_context_t ctx)
{
mount_t mp;
vnode_t tdp;
int error = 0;
uint32_t depth = 0;
vnode_t mounted_on_dp;
int current_mount_generation = 0;
mounted_on_dp = dp;
current_mount_generation = mount_generation;
while ((dp->v_type == VDIR) && dp->v_mountedhere &&
((cnp->cn_flags & NOCROSSMOUNT) == 0)) {
if (dp->v_mountedhere->mnt_lflag & MNT_LFORCE) {
break; }
#if CONFIG_TRIGGERS
if (dp->v_resolve) {
(void) vnode_trigger_resolve(dp, ndp, ctx);
}
#endif
vnode_lock(dp);
if ((dp->v_type == VDIR) && (mp = dp->v_mountedhere)) {
mp->mnt_crossref++;
vnode_unlock(dp);
if (vfs_busy(mp, vbusyflags)) {
mount_dropcrossref(mp, dp, 0);
if (vbusyflags == LK_NOWAIT) {
error = ENOENT;
goto out;
}
continue;
}
error = VFS_ROOT(mp, &tdp, ctx);
mount_dropcrossref(mp, dp, 0);
vfs_unbusy(mp);
if (error) {
goto out;
}
vnode_put(dp);
ndp->ni_vp = dp = tdp;
depth++;
#if CONFIG_TRIGGERS
if (dp->v_resolve) {
error = vnode_trigger_resolve(dp, ndp, ctx);
if (error) {
goto out;
}
}
#endif
} else {
vnode_unlock(dp);
break;
}
}
if (depth) {
mp = mounted_on_dp->v_mountedhere;
if (mp) {
mount_lock_spin(mp);
mp->mnt_realrootvp_vid = dp->v_id;
mp->mnt_realrootvp = dp;
mp->mnt_generation = current_mount_generation;
mount_unlock(mp);
}
}
return 0;
out:
return error;
}
static int
lookup_handle_symlink(struct nameidata *ndp, vnode_t *new_dp, vfs_context_t ctx)
{
int error;
char *cp;
uio_t auio;
char uio_buf[ UIO_SIZEOF(1) ];
int need_newpathbuf;
u_int linklen;
struct componentname *cnp = &ndp->ni_cnd;
vnode_t dp;
char *tmppn;
if (ndp->ni_loopcnt++ >= MAXSYMLINKS) {
return ELOOP;
}
#if CONFIG_MACF
if ((error = mac_vnode_check_readlink(ctx, ndp->ni_vp)) != 0)
return error;
#endif
if (ndp->ni_pathlen > 1 || !(cnp->cn_flags & HASBUF))
need_newpathbuf = 1;
else
need_newpathbuf = 0;
if (need_newpathbuf) {
MALLOC_ZONE(cp, char *, MAXPATHLEN, M_NAMEI, M_WAITOK);
if (cp == NULL) {
return ENOMEM;
}
} else {
cp = cnp->cn_pnbuf;
}
auio = uio_createwithbuffer(1, 0, UIO_SYSSPACE, UIO_READ, &uio_buf[0], sizeof(uio_buf));
uio_addiov(auio, CAST_USER_ADDR_T(cp), MAXPATHLEN);
error = VNOP_READLINK(ndp->ni_vp, auio, ctx);
if (error) {
if (need_newpathbuf)
FREE_ZONE(cp, MAXPATHLEN, M_NAMEI);
return error;
}
linklen = MAXPATHLEN - (u_int)uio_resid(auio);
if (linklen + ndp->ni_pathlen > MAXPATHLEN) {
if (need_newpathbuf)
FREE_ZONE(cp, MAXPATHLEN, M_NAMEI);
return ENAMETOOLONG;
}
if (need_newpathbuf) {
long len = cnp->cn_pnlen;
tmppn = cnp->cn_pnbuf;
bcopy(ndp->ni_next, cp + linklen, ndp->ni_pathlen);
cnp->cn_pnbuf = cp;
cnp->cn_pnlen = MAXPATHLEN;
if ( (cnp->cn_flags & HASBUF) )
FREE_ZONE(tmppn, len, M_NAMEI);
else
cnp->cn_flags |= HASBUF;
} else
cnp->cn_pnbuf[linklen] = '\0';
ndp->ni_pathlen += linklen;
cnp->cn_nameptr = cnp->cn_pnbuf;
dp = ndp->ni_dvp;
vnode_put(ndp->ni_vp);
vnode_put(ndp->ni_dvp);
ndp->ni_vp = NULLVP;
ndp->ni_dvp = NULLVP;
if (*(cnp->cn_nameptr) == '/') {
while (*(cnp->cn_nameptr) == '/') {
cnp->cn_nameptr++;
ndp->ni_pathlen--;
}
if ((dp = ndp->ni_rootdir) == NULLVP) {
return ENOENT;
}
}
*new_dp = dp;
return 0;
}
int
relookup(struct vnode *dvp, struct vnode **vpp, struct componentname *cnp)
{
struct vnode *dp = NULL;
int wantparent;
int rdonly;
int error = 0;
#ifdef NAMEI_DIAGNOSTIC
int i, newhash;
char *cp;
#endif
vfs_context_t ctx = cnp->cn_context;;
wantparent = cnp->cn_flags & (LOCKPARENT|WANTPARENT);
rdonly = cnp->cn_flags & RDONLY;
cnp->cn_flags &= ~ISSYMLINK;
if (cnp->cn_flags & NOCACHE)
cnp->cn_flags &= ~MAKEENTRY;
else
cnp->cn_flags |= MAKEENTRY;
dp = dvp;
if (cnp->cn_nameptr[0] == '\0') {
if (cnp->cn_nameiop != LOOKUP || wantparent) {
error = EISDIR;
goto bad;
}
if (dp->v_type != VDIR) {
error = ENOTDIR;
goto bad;
}
if ( (vnode_get(dp)) ) {
error = ENOENT;
goto bad;
}
*vpp = dp;
if (cnp->cn_flags & SAVESTART)
panic("lookup: SAVESTART");
return (0);
}
if ( (error = VNOP_LOOKUP(dp, vpp, cnp, ctx)) ) {
if (error != EJUSTRETURN)
goto bad;
#if DIAGNOSTIC
if (*vpp != NULL)
panic("leaf should be empty");
#endif
if (rdonly) {
error = EROFS;
goto bad;
}
return (0);
}
dp = *vpp;
#if DIAGNOSTIC
if (dp->v_type == VLNK && (cnp->cn_flags & FOLLOW))
panic ("relookup: symlink found.\n");
#endif
if (rdonly &&
(cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME)) {
error = EROFS;
goto bad2;
}
return (0);
bad2:
vnode_put(dp);
bad:
*vpp = NULL;
return (error);
}
void
nameidone(struct nameidata *ndp)
{
if (ndp->ni_cnd.cn_flags & HASBUF) {
char *tmp = ndp->ni_cnd.cn_pnbuf;
ndp->ni_cnd.cn_pnbuf = NULL;
ndp->ni_cnd.cn_flags &= ~HASBUF;
FREE_ZONE(tmp, ndp->ni_cnd.cn_pnlen, M_NAMEI);
}
}
#if (KDEBUG_LEVEL >= KDEBUG_LEVEL_IST)
void
kdebug_lookup_gen_events(long *dbg_parms, int dbg_namelen, void *dp, boolean_t lookup)
{
int code;
unsigned int i;
if (lookup == TRUE)
code = (FSDBG_CODE(DBG_FSRW,36)) | DBG_FUNC_START;
else
code = (FSDBG_CODE(DBG_FSRW,39)) | DBG_FUNC_START;
if (dbg_namelen <= (int)(3 * sizeof(long)))
code |= DBG_FUNC_END;
KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE, code, VM_KERNEL_ADDRPERM(dp), dbg_parms[0], dbg_parms[1], dbg_parms[2], 0);
code &= ~DBG_FUNC_START;
for (i=3, dbg_namelen -= (3 * sizeof(long)); dbg_namelen > 0; i+=4, dbg_namelen -= (4 * sizeof(long))) {
if (dbg_namelen <= (int)(4 * sizeof(long)))
code |= DBG_FUNC_END;
KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE, code, dbg_parms[i], dbg_parms[i+1], dbg_parms[i+2], dbg_parms[i+3], 0);
}
}
static void
kdebug_lookup(vnode_t dp, struct componentname *cnp)
{
int dbg_namelen;
char *dbg_nameptr;
long dbg_parms[NUMPARMS];
dbg_namelen = (cnp->cn_nameptr - cnp->cn_pnbuf) + cnp->cn_namelen;
dbg_nameptr = cnp->cn_nameptr + cnp->cn_namelen;
if (dbg_namelen > (int)sizeof(dbg_parms))
dbg_namelen = sizeof(dbg_parms);
dbg_nameptr -= dbg_namelen;
memcpy(dbg_parms, dbg_nameptr, dbg_namelen);
if (dbg_namelen < (int)sizeof(dbg_parms)) {
memset((char *)dbg_parms + dbg_namelen,
*(cnp->cn_nameptr + cnp->cn_namelen) ? '>' : 0,
sizeof(dbg_parms) - dbg_namelen);
}
kdebug_lookup_gen_events(dbg_parms, dbg_namelen, (void *)dp, TRUE);
}
#else
void
kdebug_lookup_gen_events(long *dbg_parms __unused, int dbg_namelen __unused, void *dp __unused)
{
}
static void
kdebug_lookup(struct vnode *dp __unused, struct componentname *cnp __unused)
{
}
#endif
int
vfs_getbyid(fsid_t *fsid, ino64_t ino, vnode_t *vpp, vfs_context_t ctx)
{
mount_t mp;
int error;
mp = mount_lookupby_volfsid(fsid->val[0], 1);
if (mp == NULL) {
return EINVAL;
}
if (ino == 2) {
error = VFS_ROOT(mp, vpp, ctx);
} else {
error = VFS_VGET(mp, ino, vpp, ctx);
}
vfs_unbusy(mp);
return error;
}
#if CONFIG_VOLFS
static int
vfs_getrealpath(const char * path, char * realpath, size_t bufsize, vfs_context_t ctx)
{
vnode_t vp;
struct mount *mp = NULL;
char *str;
char ch;
uint32_t id;
ino64_t ino;
int error;
int length;
id = strtoul(path, &str, 10);
if (id == 0 || str[0] != '/') {
return (EINVAL);
}
while (*str == '/') {
str++;
}
ch = *str;
mp = mount_lookupby_volfsid(id, 1);
if (mp == NULL) {
return (EINVAL);
}
if (ch == '@' && str[1] == '\0') {
ino = 2;
str++;
} else {
ino = strtouq(str, &str, 10);
}
if (ino == 2) {
error = VFS_ROOT(mp, &vp, ctx);
} else {
error = VFS_VGET(mp, ino, &vp, ctx);
}
vfs_unbusy(mp);
if (error) {
goto out;
}
realpath[0] = '\0';
error = build_path(vp, realpath, bufsize, &length, 0, ctx);
vnode_put(vp);
if (error == 0 && *str != '\0') {
int attempt = strlcat(realpath, str, MAXPATHLEN);
if (attempt > MAXPATHLEN){
error = ENAMETOOLONG;
}
}
out:
return (error);
}
#endif
void
lookup_compound_vnop_post_hook(int error, vnode_t dvp, vnode_t vp, struct nameidata *ndp, int did_create)
{
if (error == 0 && vp == NULLVP) {
panic("NULL vp with error == 0.\n");
}
if ((ndp->ni_flag & NAMEI_COMPOUND_OP_MASK) == 0) {
return;
}
if (error == EKEEPLOOKING) {
return;
}
if ((vp != NULLVP) && !did_create) {
if ((error != 0) || (ndp->ni_cnd.cn_flags & MAKEENTRY)) {
lookup_consider_update_cache(dvp, vp, &ndp->ni_cnd, ndp->ni_ncgeneration);
}
if (ndp->ni_cnd.cn_flags & AUDITVNPATH1)
AUDIT_ARG(vnpath, vp, ARG_VNODE1);
else if (ndp->ni_cnd.cn_flags & AUDITVNPATH2)
AUDIT_ARG(vnpath, vp, ARG_VNODE2);
}
if (kdebug_enable) {
kdebug_lookup(vp ? vp : dvp, &ndp->ni_cnd);
}
}