+/*
+ * This function tries to check if a directory vp is a subdirectory of dvp
+ * only from valid v_parent pointers. It is called with the name cache lock
+ * held and does not drop the lock anytime inside the function.
+ *
+ * It returns a boolean that indicates whether or not it was able to
+ * successfully infer the parent/descendent relationship via the v_parent
+ * pointers, or if it could not infer such relationship and that the decision
+ * must be delegated to the owning filesystem.
+ *
+ * If it does not defer the decision, i.e. it was successfuly able to determine
+ * the parent/descendent relationship, *is_subdir tells the caller if vp is a
+ * subdirectory of dvp.
+ *
+ * If the decision is deferred, *next_vp is where it stopped i.e. *next_vp
+ * is the vnode whose parent is to be determined from the filesystem.
+ * *is_subdir, in this case, is not indicative of anything and should be
+ * ignored.
+ *
+ * The return value and output args should be used as follows :
+ *
+ * defer = cache_check_vnode_issubdir(vp, dvp, is_subdir, next_vp);
+ * if (!defer) {
+ * if (*is_subdir)
+ * vp is subdirectory;
+ * else
+ * vp is not a subdirectory;
+ * } else {
+ * if (*next_vp)
+ * check this vnode's parent from the filesystem
+ * else
+ * error (likely because of forced unmount).
+ * }
+ *
+ */
+static boolean_t
+cache_check_vnode_issubdir(vnode_t vp, vnode_t dvp, boolean_t *is_subdir,
+ vnode_t *next_vp)
+{
+ vnode_t tvp = vp;
+ int defer = FALSE;
+
+ *is_subdir = FALSE;
+ *next_vp = NULLVP;
+ while (1) {
+ mount_t tmp;
+
+ if (tvp == dvp) {
+ *is_subdir = TRUE;
+ break;
+ } else if (tvp == rootvnode) {
+ /* *is_subdir = FALSE */
+ break;
+ }
+
+ tmp = tvp->v_mount;
+ while ((tvp->v_flag & VROOT) && tmp && tmp->mnt_vnodecovered &&
+ tvp != dvp && tvp != rootvnode) {
+ tvp = tmp->mnt_vnodecovered;
+ tmp = tvp->v_mount;
+ }
+
+ /*
+ * If dvp is not at the top of a mount "stack" then
+ * vp is not a subdirectory of dvp either.
+ */
+ if (tvp == dvp || tvp == rootvnode) {
+ /* *is_subdir = FALSE */
+ break;
+ }
+
+ if (!tmp) {
+ defer = TRUE;
+ *next_vp = NULLVP;
+ break;
+ }
+
+ if ((tvp->v_flag & VISHARDLINK) || !(tvp->v_parent)) {
+ defer = TRUE;
+ *next_vp = tvp;
+ break;
+ }
+
+ tvp = tvp->v_parent;
+ }
+
+ return defer;
+}
+
+/* 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);
+ }