+
+/* maximum times retry from potentially transient errors in vnode_issubdir */
+#define MAX_ERROR_RETRY 3
+
+/*
+ * This function checks if a given directory (vp) is a subdirectory of dvp.
+ * It walks backwards from vp and if it hits dvp in its parent chain,
+ * it is a subdirectory. If it encounters the root directory, it is not
+ * a subdirectory.
+ *
+ * This function returns an error if it is unsuccessful and 0 on success.
+ *
+ * On entry (and exit) vp has an iocount and if this function has to take
+ * any iocounts on other vnodes in the parent chain traversal, it releases them.
+ */
+int
+vnode_issubdir(vnode_t vp, vnode_t dvp, int *is_subdir, vfs_context_t ctx)
+{
+ vnode_t start_vp, tvp;
+ vnode_t vp_with_iocount;
+ int error = 0;
+ char dotdotbuf[] = "..";
+ int error_retry_count = 0; /* retry count for potentially transient
+ errors */
+
+ *is_subdir = FALSE;
+ tvp = start_vp = vp;
+ /*
+ * Anytime we acquire an iocount in this function, we save the vnode
+ * in this variable and release it before exiting.
+ */
+ vp_with_iocount = NULLVP;
+
+ while (1) {
+ boolean_t defer;
+ vnode_t pvp;
+ uint32_t vid;
+ struct componentname cn;
+ boolean_t is_subdir_locked = FALSE;
+
+ if (tvp == dvp) {
+ *is_subdir = TRUE;
+ break;
+ } else if (tvp == rootvnode) {
+ /* *is_subdir = FALSE */
+ break;
+ }
+
+ NAME_CACHE_LOCK_SHARED();
+
+ defer = cache_check_vnode_issubdir(tvp, dvp, &is_subdir_locked,
+ &tvp);
+
+ if (defer && tvp)
+ vid = vnode_vid(tvp);
+
+ NAME_CACHE_UNLOCK();
+
+ if (!defer) {
+ *is_subdir = is_subdir_locked;
+ break;
+ }
+
+ if (!tvp) {
+ if (error_retry_count++ < MAX_ERROR_RETRY) {
+ tvp = vp;
+ continue;
+ }
+ error = ENOENT;
+ break;
+ }
+
+ if (tvp != start_vp) {
+ if (vp_with_iocount) {
+ vnode_put(vp_with_iocount);
+ vp_with_iocount = NULLVP;
+ }
+
+ error = vnode_getwithvid(tvp, vid);
+ if (error) {
+ if (error_retry_count++ < MAX_ERROR_RETRY) {
+ tvp = vp;
+ error = 0;
+ continue;
+ }
+ break;
+ }
+
+ vp_with_iocount = tvp;
+ }
+
+ bzero(&cn, sizeof(cn));
+ cn.cn_nameiop = LOOKUP;
+ cn.cn_flags = ISLASTCN | ISDOTDOT;
+ cn.cn_context = ctx;
+ cn.cn_pnbuf = &dotdotbuf[0];
+ cn.cn_pnlen = sizeof(dotdotbuf);
+ cn.cn_nameptr = cn.cn_pnbuf;
+ cn.cn_namelen = 2;
+
+ pvp = NULLVP;
+ if ((error = VNOP_LOOKUP(tvp, &pvp, &cn, ctx)))
+ break;
+
+ if (!(tvp->v_flag & VISHARDLINK) && tvp->v_parent != pvp) {
+ (void)vnode_update_identity(tvp, pvp, NULL, 0, 0,
+ VNODE_UPDATE_PARENT);
+ }
+
+ if (vp_with_iocount)
+ vnode_put(vp_with_iocount);
+
+ vp_with_iocount = tvp = pvp;
+ }
+
+ if (vp_with_iocount)
+ vnode_put(vp_with_iocount);
+
+ return (error);
+}
+
+/*
+ * This function builds the path in "buff" from the supplied vnode.
+ * The length of the buffer *INCLUDING* the trailing zero byte is
+ * returned in outlen. NOTE: the length includes the trailing zero
+ * byte and thus the length is one greater than what strlen would
+ * return. This is important and lots of code elsewhere in the kernel
+ * assumes this behavior.
+ *
+ * This function can call vnop in file system if the parent vnode
+ * does not exist or when called for hardlinks via volfs path.
+ * If BUILDPATH_NO_FS_ENTER is set in flags, it only uses values present
+ * in the name cache and does not enter the file system.
+ *
+ * If BUILDPATH_CHECK_MOVED is set in flags, we return EAGAIN when
+ * we encounter ENOENT during path reconstruction. ENOENT means that
+ * one of the parents moved while we were building the path. The
+ * caller can special handle this case by calling build_path again.
+ *
+ * If BUILDPATH_VOLUME_RELATIVE is set in flags, we return path
+ * that is relative to the nearest mount point, i.e. do not
+ * cross over mount points during building the path.
+ *
+ * passed in vp must have a valid io_count reference
+ *
+ * If parent vnode is non-NULL it also must have an io count. This
+ * allows build_path_with_parent to be safely called for operations
+ * unlink, rmdir and rename that already have io counts on the target
+ * and the directory. In this way build_path_with_parent does not have
+ * to try and obtain an additional io count on the parent. Taking an
+ * io count ont the parent can lead to dead lock if a forced unmount
+ * occures at the right moment. For a fuller explaination on how this
+ * can occur see the comment for vn_getpath_with_parent.
+ *
+ */
+int
+build_path_with_parent(vnode_t first_vp, vnode_t parent_vp, char *buff, int buflen, int *outlen, int flags, vfs_context_t ctx)
+{
+ vnode_t vp, tvp;
+ vnode_t vp_with_iocount;
+ vnode_t proc_root_dir_vp;
+ char *end;
+ const char *str;
+ int len;
+ int ret = 0;
+ int fixhardlink;
+
+ if (first_vp == NULLVP)
+ return (EINVAL);
+
+ if (buflen <= 1)
+ return (ENOSPC);
+
+ /*
+ * Grab the process fd so we can evaluate fd_rdir.
+ */
+ if (vfs_context_proc(ctx)->p_fd)
+ proc_root_dir_vp = vfs_context_proc(ctx)->p_fd->fd_rdir;
+ else
+ proc_root_dir_vp = NULL;
+
+ vp_with_iocount = NULLVP;
+again:
+ vp = first_vp;
+
+ end = &buff[buflen-1];
+ *end = '\0';
+
+ /*
+ * holding the NAME_CACHE_LOCK in shared mode is
+ * sufficient to stabilize both the vp->v_parent chain
+ * and the 'vp->v_mount->mnt_vnodecovered' chain
+ *
+ * if we need to drop this lock, we must first grab the v_id
+ * from the vnode we're currently working with... if that
+ * vnode doesn't already have an io_count reference (the vp
+ * passed in comes with one), we must grab a reference
+ * after we drop the NAME_CACHE_LOCK via vnode_getwithvid...
+ * deadlocks may result if you call vnode_get while holding
+ * the NAME_CACHE_LOCK... we lazily release the reference
+ * we pick up the next time we encounter a need to drop
+ * the NAME_CACHE_LOCK or before we return from this routine
+ */
+ NAME_CACHE_LOCK_SHARED();
+
+ /*
+ * Check if this is the root of a file system.
+ */
+ while (vp && vp->v_flag & VROOT) {
+ if (vp->v_mount == NULL) {
+ ret = EINVAL;
+ goto out_unlock;
+ }
+ if ((vp->v_mount->mnt_flag & MNT_ROOTFS) || (vp == proc_root_dir_vp)) {
+ /*
+ * It's the root of the root file system, so it's
+ * just "/".
+ */
+ *--end = '/';
+
+ goto out_unlock;
+ } else {
+ /*
+ * This the root of the volume and the caller does not
+ * want to cross mount points. Therefore just return
+ * '/' as the relative path.
+ */
+ if (flags & BUILDPATH_VOLUME_RELATIVE) {
+ *--end = '/';
+ goto out_unlock;
+ } else {
+ vp = vp->v_mount->mnt_vnodecovered;
+ }
+ }
+ }
+
+ while ((vp != NULLVP) && (vp->v_parent != vp)) {
+ int vid;
+
+ /*
+ * For hardlinks the v_name may be stale, so if its OK
+ * to enter a file system, ask the file system for the
+ * name and parent (below).
+ */
+ fixhardlink = (vp->v_flag & VISHARDLINK) &&
+ (vp->v_mount->mnt_kern_flag & MNTK_PATH_FROM_ID) &&
+ !(flags & BUILDPATH_NO_FS_ENTER);
+
+ if (!fixhardlink) {
+ str = vp->v_name;
+
+ if (str == NULL || *str == '\0') {
+ if (vp->v_parent != NULL)
+ ret = EINVAL;
+ else
+ ret = ENOENT;
+ goto out_unlock;
+ }
+ len = strlen(str);
+ /*
+ * Check that there's enough space (including space for the '/')
+ */
+ if ((end - buff) < (len + 1)) {
+ ret = ENOSPC;
+ goto out_unlock;
+ }
+ /*
+ * Copy the name backwards.
+ */
+ str += len;
+
+ for (; len > 0; len--)
+ *--end = *--str;
+ /*
+ * Add a path separator.
+ */
+ *--end = '/';
+ }
+
+ /*
+ * Walk up the parent chain.
+ */
+ if (((vp->v_parent != NULLVP) && !fixhardlink) ||
+ (flags & BUILDPATH_NO_FS_ENTER)) {
+
+ /*
+ * In this if () block we are not allowed to enter the filesystem
+ * to conclusively get the most accurate parent identifier.
+ * As a result, if 'vp' does not identify '/' and it
+ * does not have a valid v_parent, then error out
+ * and disallow further path construction
+ */
+ if ((vp->v_parent == NULLVP) && (rootvnode != vp)) {
+ /*
+ * Only '/' is allowed to have a NULL parent
+ * pointer. Upper level callers should ideally
+ * re-drive name lookup on receiving a ENOENT.
+ */
+ ret = ENOENT;
+
+ /* The code below will exit early if 'tvp = vp' == NULL */
+ }
+ vp = vp->v_parent;
+
+ /*
+ * if the vnode we have in hand isn't a directory and it
+ * has a v_parent, then we started with the resource fork
+ * so skip up to avoid getting a duplicate copy of the
+ * file name in the path.
+ */
+ if (vp && !vnode_isdir(vp) && vp->v_parent) {
+ vp = vp->v_parent;
+ }
+ } else {
+ /*
+ * No parent, go get it if supported.
+ */
+ struct vnode_attr va;
+ vnode_t dvp;
+
+ /*
+ * Make sure file system supports obtaining a path from id.
+ */
+ if (!(vp->v_mount->mnt_kern_flag & MNTK_PATH_FROM_ID)) {
+ ret = ENOENT;
+ goto out_unlock;
+ }
+ vid = vp->v_id;
+
+ NAME_CACHE_UNLOCK();
+
+ if (vp != first_vp && vp != parent_vp && vp != vp_with_iocount) {
+ if (vp_with_iocount) {
+ vnode_put(vp_with_iocount);
+ vp_with_iocount = NULLVP;
+ }
+ if (vnode_getwithvid(vp, vid))
+ goto again;
+ vp_with_iocount = vp;
+ }
+ VATTR_INIT(&va);
+ VATTR_WANTED(&va, va_parentid);
+
+ if (fixhardlink) {
+ VATTR_WANTED(&va, va_name);
+ MALLOC_ZONE(va.va_name, caddr_t, MAXPATHLEN, M_NAMEI, M_WAITOK);
+ } else {
+ va.va_name = NULL;
+ }
+ /*
+ * Ask the file system for its parent id and for its name (optional).
+ */
+ ret = vnode_getattr(vp, &va, ctx);
+
+ if (fixhardlink) {
+ if ((ret == 0) && (VATTR_IS_SUPPORTED(&va, va_name))) {
+ str = va.va_name;
+ vnode_update_identity(vp, NULL, str, strlen(str), 0, VNODE_UPDATE_NAME);
+ } else if (vp->v_name) {
+ str = vp->v_name;
+ ret = 0;
+ } else {
+ ret = ENOENT;
+ goto bad_news;
+ }
+ len = strlen(str);
+
+ /*
+ * Check that there's enough space.
+ */
+ if ((end - buff) < (len + 1)) {
+ ret = ENOSPC;
+ } else {
+ /* Copy the name backwards. */
+ str += len;
+
+ for (; len > 0; len--) {
+ *--end = *--str;
+ }
+ /*
+ * Add a path separator.
+ */
+ *--end = '/';
+ }
+bad_news:
+ FREE_ZONE(va.va_name, MAXPATHLEN, M_NAMEI);
+ }
+ if (ret || !VATTR_IS_SUPPORTED(&va, va_parentid)) {
+ ret = ENOENT;
+ goto out;
+ }
+ /*
+ * Ask the file system for the parent vnode.
+ */
+ if ((ret = VFS_VGET(vp->v_mount, (ino64_t)va.va_parentid, &dvp, ctx)))
+ goto out;
+
+ if (!fixhardlink && (vp->v_parent != dvp))
+ vnode_update_identity(vp, dvp, NULL, 0, 0, VNODE_UPDATE_PARENT);
+
+ if (vp_with_iocount)
+ vnode_put(vp_with_iocount);
+ vp = dvp;
+ vp_with_iocount = vp;
+
+ NAME_CACHE_LOCK_SHARED();
+
+ /*
+ * if the vnode we have in hand isn't a directory and it
+ * has a v_parent, then we started with the resource fork
+ * so skip up to avoid getting a duplicate copy of the
+ * file name in the path.
+ */
+ if (vp && !vnode_isdir(vp) && vp->v_parent)
+ vp = vp->v_parent;
+ }
+
+ if (vp && (flags & BUILDPATH_CHECKACCESS)) {
+ vid = vp->v_id;
+
+ NAME_CACHE_UNLOCK();
+
+ if (vp != first_vp && vp != parent_vp && vp != vp_with_iocount) {
+ if (vp_with_iocount) {
+ vnode_put(vp_with_iocount);
+ vp_with_iocount = NULLVP;
+ }
+ if (vnode_getwithvid(vp, vid))
+ goto again;
+ vp_with_iocount = vp;
+ }
+ if ((ret = vnode_authorize(vp, NULL, KAUTH_VNODE_SEARCH, ctx)))
+ goto out; /* no peeking */
+
+ NAME_CACHE_LOCK_SHARED();
+ }
+
+ /*
+ * When a mount point is crossed switch the vp.
+ * Continue until we find the root or we find
+ * a vnode that's not the root of a mounted
+ * file system.
+ */
+ tvp = vp;
+
+ while (tvp) {
+ if (tvp == proc_root_dir_vp)
+ goto out_unlock; /* encountered the root */
+
+ if (!(tvp->v_flag & VROOT) || !tvp->v_mount)
+ break; /* not the root of a mounted FS */
+
+ if (flags & BUILDPATH_VOLUME_RELATIVE) {
+ /* Do not cross over mount points */
+ tvp = NULL;
+ } else {
+ tvp = tvp->v_mount->mnt_vnodecovered;
+ }
+ }
+ if (tvp == NULLVP)
+ goto out_unlock;
+ vp = tvp;
+ }
+out_unlock:
+ NAME_CACHE_UNLOCK();
+out:
+ if (vp_with_iocount)
+ vnode_put(vp_with_iocount);
+ /*
+ * Slide the name down to the beginning of the buffer.
+ */
+ memmove(buff, end, &buff[buflen] - end);
+
+ /*
+ * length includes the trailing zero byte
+ */
+ *outlen = &buff[buflen] - end;
+
+ /* One of the parents was moved during path reconstruction.
+ * The caller is interested in knowing whether any of the
+ * parents moved via BUILDPATH_CHECK_MOVED, so return EAGAIN.
+ */
+ if ((ret == ENOENT) && (flags & BUILDPATH_CHECK_MOVED)) {
+ ret = EAGAIN;
+ }
+
+ return (ret);
+}
+
+int
+build_path(vnode_t first_vp, char *buff, int buflen, int *outlen, int flags, vfs_context_t ctx)
+{
+ return (build_path_with_parent(first_vp, NULL, buff, buflen, outlen, flags, ctx));
+}
+
+/*
+ * return NULLVP if vp's parent doesn't
+ * exist, or we can't get a valid iocount
+ * else return the parent of vp
+ */
+vnode_t
+vnode_getparent(vnode_t vp)
+{
+ vnode_t pvp = NULLVP;
+ int pvid;
+
+ NAME_CACHE_LOCK_SHARED();
+ /*
+ * v_parent is stable behind the name_cache lock
+ * however, the only thing we can really guarantee
+ * is that we've grabbed a valid iocount on the
+ * parent of 'vp' at the time we took the name_cache lock...
+ * once we drop the lock, vp could get re-parented
+ */
+ if ( (pvp = vp->v_parent) != NULLVP ) {
+ pvid = pvp->v_id;
+
+ NAME_CACHE_UNLOCK();
+
+ if (vnode_getwithvid(pvp, pvid) != 0)
+ pvp = NULL;
+ } else
+ NAME_CACHE_UNLOCK();
+ return (pvp);
+}
+
+const char *
+vnode_getname(vnode_t vp)
+{
+ const char *name = NULL;
+
+ NAME_CACHE_LOCK_SHARED();
+
+ if (vp->v_name)
+ name = vfs_addname(vp->v_name, strlen(vp->v_name), 0, 0);
+ NAME_CACHE_UNLOCK();
+
+ return (name);
+}
+
+void
+vnode_putname(const char *name)
+{
+ vfs_removename(name);
+}
+
+static const char unknown_vnodename[] = "(unknown vnode name)";
+
+const char *
+vnode_getname_printable(vnode_t vp)
+{
+ const char *name = vnode_getname(vp);
+ if (name != NULL)
+ return name;
+
+ switch (vp->v_type) {
+ case VCHR:
+ case VBLK:
+ {
+ /*
+ * Create an artificial dev name from
+ * major and minor device number
+ */
+ char dev_name[64];
+ (void) snprintf(dev_name, sizeof(dev_name),
+ "%c(%u, %u)", VCHR == vp->v_type ? 'c':'b',
+ major(vp->v_rdev), minor(vp->v_rdev));
+ /*
+ * Add the newly created dev name to the name
+ * cache to allow easier cleanup. Also,
+ * vfs_addname allocates memory for the new name
+ * and returns it.
+ */
+ NAME_CACHE_LOCK_SHARED();
+ name = vfs_addname(dev_name, strlen(dev_name), 0, 0);
+ NAME_CACHE_UNLOCK();
+ return name;
+ }
+ default:
+ return unknown_vnodename;
+ }
+}
+
+void
+vnode_putname_printable(const char *name)
+{
+ if (name == unknown_vnodename)
+ return;
+ vnode_putname(name);
+}
+
+
+/*
+ * if VNODE_UPDATE_PARENT, and we can take
+ * a reference on dvp, then update vp with
+ * it's new parent... if vp already has a parent,
+ * then drop the reference vp held on it
+ *
+ * if VNODE_UPDATE_NAME,
+ * then drop string ref on v_name if it exists, and if name is non-NULL
+ * then pick up a string reference on name and record it in v_name...
+ * optionally pass in the length and hashval of name if known
+ *
+ * if VNODE_UPDATE_CACHE, flush the name cache entries associated with vp
+ */
+void
+vnode_update_identity(vnode_t vp, vnode_t dvp, const char *name, int name_len, uint32_t name_hashval, int flags)
+{
+ struct namecache *ncp;
+ vnode_t old_parentvp = NULLVP;
+#if NAMEDSTREAMS
+ int isstream = (vp->v_flag & VISNAMEDSTREAM);
+ int kusecountbumped = 0;
+#endif
+ kauth_cred_t tcred = NULL;
+ const char *vname = NULL;
+ const char *tname = NULL;
+
+ if (flags & VNODE_UPDATE_PARENT) {
+ if (dvp && vnode_ref(dvp) != 0) {
+ dvp = NULLVP;
+ }
+#if NAMEDSTREAMS
+ /* Don't count a stream's parent ref during unmounts */
+ if (isstream && dvp && (dvp != vp) && (dvp != vp->v_parent) && (dvp->v_type == VREG)) {
+ vnode_lock_spin(dvp);
+ ++dvp->v_kusecount;
+ kusecountbumped = 1;
+ vnode_unlock(dvp);
+ }
+#endif
+ } else {
+ dvp = NULLVP;
+ }
+ if ( (flags & VNODE_UPDATE_NAME) ) {
+ if (name != vp->v_name) {
+ if (name && *name) {
+ if (name_len == 0)
+ name_len = strlen(name);
+ tname = vfs_addname(name, name_len, name_hashval, 0);
+ }
+ } else
+ flags &= ~VNODE_UPDATE_NAME;
+ }
+ if ( (flags & (VNODE_UPDATE_PURGE | VNODE_UPDATE_PARENT | VNODE_UPDATE_CACHE | VNODE_UPDATE_NAME)) ) {
+
+ NAME_CACHE_LOCK();
+
+ if ( (flags & VNODE_UPDATE_PURGE) ) {
+
+ if (vp->v_parent)
+ vp->v_parent->v_nc_generation++;
+
+ while ( (ncp = LIST_FIRST(&vp->v_nclinks)) )
+ cache_delete(ncp, 1);
+
+ while ( (ncp = TAILQ_FIRST(&vp->v_ncchildren)) )
+ cache_delete(ncp, 1);
+
+ /*
+ * Use a temp variable to avoid kauth_cred_unref() while NAME_CACHE_LOCK is held
+ */
+ tcred = vp->v_cred;
+ vp->v_cred = NOCRED;
+ vp->v_authorized_actions = 0;
+ vp->v_cred_timestamp = 0;
+ }
+ if ( (flags & VNODE_UPDATE_NAME) ) {
+ vname = vp->v_name;
+ vp->v_name = tname;
+ }
+ if (flags & VNODE_UPDATE_PARENT) {
+ if (dvp != vp && dvp != vp->v_parent) {
+ old_parentvp = vp->v_parent;
+ vp->v_parent = dvp;
+ dvp = NULLVP;
+
+ if (old_parentvp)
+ flags |= VNODE_UPDATE_CACHE;
+ }
+ }
+ if (flags & VNODE_UPDATE_CACHE) {
+ while ( (ncp = LIST_FIRST(&vp->v_nclinks)) )
+ cache_delete(ncp, 1);
+ }
+ NAME_CACHE_UNLOCK();
+
+ if (vname != NULL)
+ vfs_removename(vname);
+
+ if (IS_VALID_CRED(tcred))
+ kauth_cred_unref(&tcred);
+ }
+ if (dvp != NULLVP) {
+#if NAMEDSTREAMS
+ /* Back-out the ref we took if we lost a race for vp->v_parent. */
+ if (kusecountbumped) {
+ vnode_lock_spin(dvp);
+ if (dvp->v_kusecount > 0)
+ --dvp->v_kusecount;
+ vnode_unlock(dvp);
+ }
+#endif
+ vnode_rele(dvp);
+ }
+ if (old_parentvp) {
+ struct uthread *ut;
+
+#if NAMEDSTREAMS
+ if (isstream) {
+ vnode_lock_spin(old_parentvp);
+ if ((old_parentvp->v_type != VDIR) && (old_parentvp->v_kusecount > 0))
+ --old_parentvp->v_kusecount;
+ vnode_unlock(old_parentvp);
+ }
+#endif
+ ut = get_bsdthread_info(current_thread());
+
+ /*
+ * indicated to vnode_rele that it shouldn't do a
+ * vnode_reclaim at this time... instead it will
+ * chain the vnode to the uu_vreclaims list...
+ * we'll be responsible for calling vnode_reclaim
+ * on each of the vnodes in this list...
+ */
+ ut->uu_defer_reclaims = 1;
+ ut->uu_vreclaims = NULLVP;
+
+ while ( (vp = old_parentvp) != NULLVP ) {
+
+ vnode_lock_spin(vp);
+ vnode_rele_internal(vp, 0, 0, 1);
+
+ /*
+ * check to see if the vnode is now in the state
+ * that would have triggered a vnode_reclaim in vnode_rele
+ * if it is, we save it's parent pointer and then NULL
+ * out the v_parent field... we'll drop the reference
+ * that was held on the next iteration of this loop...
+ * this short circuits a potential deep recursion if we
+ * have a long chain of parents in this state...
+ * we'll sit in this loop until we run into
+ * a parent in this chain that is not in this state
+ *
+ * make our check and the vnode_rele atomic
+ * with respect to the current vnode we're working on
+ * by holding the vnode lock
+ * if vnode_rele deferred the vnode_reclaim and has put
+ * this vnode on the list to be reaped by us, than
+ * it has left this vnode with an iocount == 1
+ */
+ if ( (vp->v_iocount == 1) && (vp->v_usecount == 0) &&
+ ((vp->v_lflag & (VL_MARKTERM | VL_TERMINATE | VL_DEAD)) == VL_MARKTERM)) {
+ /*
+ * vnode_rele wanted to do a vnode_reclaim on this vnode
+ * it should be sitting on the head of the uu_vreclaims chain
+ * pull the parent pointer now so that when we do the
+ * vnode_reclaim for each of the vnodes in the uu_vreclaims
+ * list, we won't recurse back through here
+ *
+ * need to do a convert here in case vnode_rele_internal
+ * returns with the lock held in the spin mode... it
+ * can drop and retake the lock under certain circumstances
+ */
+ vnode_lock_convert(vp);
+
+ NAME_CACHE_LOCK();
+ old_parentvp = vp->v_parent;
+ vp->v_parent = NULLVP;
+ NAME_CACHE_UNLOCK();
+ } else {
+ /*
+ * we're done... we ran into a vnode that isn't
+ * being terminated
+ */
+ old_parentvp = NULLVP;
+ }
+ vnode_unlock(vp);
+ }
+ ut->uu_defer_reclaims = 0;
+
+ while ( (vp = ut->uu_vreclaims) != NULLVP) {
+ ut->uu_vreclaims = vp->v_defer_reclaimlist;
+
+ /*
+ * vnode_put will drive the vnode_reclaim if
+ * we are still the only reference on this vnode
+ */
+ vnode_put(vp);
+ }
+ }
+}
+
+
+/*
+ * Mark a vnode as having multiple hard links. HFS makes use of this
+ * because it keeps track of each link separately, and wants to know
+ * which link was actually used.
+ *
+ * This will cause the name cache to force a VNOP_LOOKUP on the vnode
+ * so that HFS can post-process the lookup. Also, volfs will call
+ * VNOP_GETATTR2 to determine the parent, instead of using v_parent.
+ */
+void vnode_setmultipath(vnode_t vp)
+{
+ vnode_lock_spin(vp);
+
+ /*
+ * In theory, we're changing the vnode's identity as far as the
+ * name cache is concerned, so we ought to grab the name cache lock
+ * here. However, there is already a race, and grabbing the name
+ * cache lock only makes the race window slightly smaller.
+ *
+ * The race happens because the vnode already exists in the name
+ * cache, and could be found by one thread before another thread
+ * can set the hard link flag.
+ */
+
+ vp->v_flag |= VISHARDLINK;
+
+ vnode_unlock(vp);
+}
+
+
+
+/*
+ * backwards compatibility
+ */
+void vnode_uncache_credentials(vnode_t vp)
+{
+ vnode_uncache_authorized_action(vp, KAUTH_INVALIDATE_CACHED_RIGHTS);
+}
+
+
+/*
+ * use the exclusive form of NAME_CACHE_LOCK to protect the update of the
+ * following fields in the vnode: v_cred_timestamp, v_cred, v_authorized_actions
+ * we use this lock so that we can look at the v_cred and v_authorized_actions
+ * atomically while behind the NAME_CACHE_LOCK in shared mode in 'cache_lookup_path',
+ * which is the super-hot path... if we are updating the authorized actions for this
+ * vnode, we are already in the super-slow and far less frequented path so its not
+ * that bad that we take the lock exclusive for this case... of course we strive
+ * to hold it for the minimum amount of time possible
+ */
+
+void vnode_uncache_authorized_action(vnode_t vp, kauth_action_t action)
+{
+ kauth_cred_t tcred = NOCRED;
+
+ NAME_CACHE_LOCK();
+
+ vp->v_authorized_actions &= ~action;
+
+ if (action == KAUTH_INVALIDATE_CACHED_RIGHTS &&
+ IS_VALID_CRED(vp->v_cred)) {
+ /*
+ * Use a temp variable to avoid kauth_cred_unref() while NAME_CACHE_LOCK is held
+ */
+ tcred = vp->v_cred;
+ vp->v_cred = NOCRED;
+ }
+ NAME_CACHE_UNLOCK();
+
+ if (tcred != NOCRED)
+ kauth_cred_unref(&tcred);
+}
+
+
+extern int bootarg_vnode_cache_defeat; /* default = 0, from bsd_init.c */
+
+boolean_t
+vnode_cache_is_authorized(vnode_t vp, vfs_context_t ctx, kauth_action_t action)
+{
+ kauth_cred_t ucred;
+ boolean_t retval = FALSE;
+
+ /* Boot argument to defeat rights caching */
+ if (bootarg_vnode_cache_defeat)
+ return FALSE;
+
+ if ( (vp->v_mount->mnt_kern_flag & (MNTK_AUTH_OPAQUE | MNTK_AUTH_CACHE_TTL)) ) {
+ /*
+ * a TTL is enabled on the rights cache... handle it here
+ * a TTL of 0 indicates that no rights should be cached
+ */
+ if (vp->v_mount->mnt_authcache_ttl) {
+ if ( !(vp->v_mount->mnt_kern_flag & MNTK_AUTH_CACHE_TTL) ) {
+ /*
+ * For filesystems marked only MNTK_AUTH_OPAQUE (generally network ones),
+ * we will only allow a SEARCH right on a directory to be cached...
+ * that cached right always has a default TTL associated with it
+ */
+ if (action != KAUTH_VNODE_SEARCH || vp->v_type != VDIR)
+ vp = NULLVP;
+ }
+ if (vp != NULLVP && vnode_cache_is_stale(vp) == TRUE) {
+ vnode_uncache_authorized_action(vp, vp->v_authorized_actions);
+ vp = NULLVP;
+ }
+ } else
+ vp = NULLVP;
+ }
+ if (vp != NULLVP) {
+ ucred = vfs_context_ucred(ctx);
+
+ NAME_CACHE_LOCK_SHARED();
+
+ if (vp->v_cred == ucred && (vp->v_authorized_actions & action) == action)
+ retval = TRUE;
+
+ NAME_CACHE_UNLOCK();
+ }
+ return retval;
+}
+
+
+void vnode_cache_authorized_action(vnode_t vp, vfs_context_t ctx, kauth_action_t action)
+{
+ kauth_cred_t tcred = NOCRED;
+ kauth_cred_t ucred;
+ struct timeval tv;
+ boolean_t ttl_active = FALSE;
+
+ ucred = vfs_context_ucred(ctx);
+
+ if (!IS_VALID_CRED(ucred) || action == 0)
+ return;
+
+ if ( (vp->v_mount->mnt_kern_flag & (MNTK_AUTH_OPAQUE | MNTK_AUTH_CACHE_TTL)) ) {
+ /*
+ * a TTL is enabled on the rights cache... handle it here
+ * a TTL of 0 indicates that no rights should be cached
+ */
+ if (vp->v_mount->mnt_authcache_ttl == 0)
+ return;
+
+ if ( !(vp->v_mount->mnt_kern_flag & MNTK_AUTH_CACHE_TTL) ) {
+ /*
+ * only cache SEARCH action for filesystems marked
+ * MNTK_AUTH_OPAQUE on VDIRs...
+ * the lookup_path code will time these out
+ */
+ if ( (action & ~KAUTH_VNODE_SEARCH) || vp->v_type != VDIR )
+ return;
+ }
+ ttl_active = TRUE;
+
+ microuptime(&tv);
+ }
+ NAME_CACHE_LOCK();
+
+ if (vp->v_cred != ucred) {
+ kauth_cred_ref(ucred);
+ /*
+ * Use a temp variable to avoid kauth_cred_unref() while NAME_CACHE_LOCK is held
+ */
+ tcred = vp->v_cred;
+ vp->v_cred = ucred;
+ vp->v_authorized_actions = 0;
+ }
+ if (ttl_active == TRUE && vp->v_authorized_actions == 0) {
+ /*
+ * only reset the timestamnp on the
+ * first authorization cached after the previous
+ * timer has expired or we're switching creds...
+ * 'vnode_cache_is_authorized' will clear the
+ * authorized actions if the TTL is active and
+ * it has expired
+ */
+ vp->v_cred_timestamp = tv.tv_sec;
+ }
+ vp->v_authorized_actions |= action;
+
+ NAME_CACHE_UNLOCK();
+
+ if (IS_VALID_CRED(tcred))
+ kauth_cred_unref(&tcred);