+
+
+#if CONFIG_TRIGGERS
+
+#define TRIG_DEBUG 0
+
+#if TRIG_DEBUG
+#define TRIG_LOG(...) do { printf("%s: ", __FUNCTION__); printf(__VA_ARGS__); } while (0)
+#else
+#define TRIG_LOG(...)
+#endif
+
+/*
+ * Resolver result functions
+ */
+
+resolver_result_t
+vfs_resolver_result(uint32_t seq, enum resolver_status stat, int aux)
+{
+ /*
+ * |<--- 32 --->|<--- 28 --->|<- 4 ->|
+ * sequence auxiliary status
+ */
+ return (((uint64_t)seq) << 32) |
+ (((uint64_t)(aux & 0x0fffffff)) << 4) |
+ (uint64_t)(stat & 0x0000000F);
+}
+
+enum resolver_status
+vfs_resolver_status(resolver_result_t result)
+{
+ /* lower 4 bits is status */
+ return (result & 0x0000000F);
+}
+
+uint32_t
+vfs_resolver_sequence(resolver_result_t result)
+{
+ /* upper 32 bits is sequence */
+ return (uint32_t)(result >> 32);
+}
+
+int
+vfs_resolver_auxiliary(resolver_result_t result)
+{
+ /* 28 bits of auxiliary */
+ return (int)(((uint32_t)(result & 0xFFFFFFF0)) >> 4);
+}
+
+/*
+ * SPI
+ * Call in for resolvers to update vnode trigger state
+ */
+int
+vnode_trigger_update(vnode_t vp, resolver_result_t result)
+{
+ vnode_resolve_t rp;
+ uint32_t seq;
+ enum resolver_status stat;
+
+ if (vp->v_resolve == NULL) {
+ return (EINVAL);
+ }
+
+ stat = vfs_resolver_status(result);
+ seq = vfs_resolver_sequence(result);
+
+ if ((stat != RESOLVER_RESOLVED) && (stat != RESOLVER_UNRESOLVED)) {
+ return (EINVAL);
+ }
+
+ rp = vp->v_resolve;
+ lck_mtx_lock(&rp->vr_lock);
+
+ if (seq > rp->vr_lastseq) {
+ if (stat == RESOLVER_RESOLVED)
+ rp->vr_flags |= VNT_RESOLVED;
+ else
+ rp->vr_flags &= ~VNT_RESOLVED;
+
+ rp->vr_lastseq = seq;
+ }
+
+ lck_mtx_unlock(&rp->vr_lock);
+
+ return (0);
+}
+
+static int
+vnode_resolver_attach(vnode_t vp, vnode_resolve_t rp, boolean_t ref)
+{
+ int error;
+
+ vnode_lock_spin(vp);
+ if (vp->v_resolve != NULL) {
+ vnode_unlock(vp);
+ return EINVAL;
+ } else {
+ vp->v_resolve = rp;
+ }
+ vnode_unlock(vp);
+
+ if (ref) {
+ error = vnode_ref_ext(vp, O_EVTONLY, VNODE_REF_FORCE);
+ if (error != 0) {
+ panic("VNODE_REF_FORCE didn't help...");
+ }
+ }
+
+ return 0;
+}
+
+/*
+ * VFS internal interfaces for vnode triggers
+ *
+ * vnode must already have an io count on entry
+ * v_resolve is stable when io count is non-zero
+ */
+static int
+vnode_resolver_create(mount_t mp, vnode_t vp, struct vnode_trigger_param *tinfo, boolean_t external)
+{
+ vnode_resolve_t rp;
+ int result;
+ char byte;
+
+#if 1
+ /* minimum pointer test (debugging) */
+ if (tinfo->vnt_data)
+ byte = *((char *)tinfo->vnt_data);
+#endif
+ MALLOC(rp, vnode_resolve_t, sizeof(*rp), M_TEMP, M_WAITOK);
+ if (rp == NULL)
+ return (ENOMEM);
+
+ lck_mtx_init(&rp->vr_lock, trigger_vnode_lck_grp, trigger_vnode_lck_attr);
+
+ rp->vr_resolve_func = tinfo->vnt_resolve_func;
+ rp->vr_unresolve_func = tinfo->vnt_unresolve_func;
+ rp->vr_rearm_func = tinfo->vnt_rearm_func;
+ rp->vr_reclaim_func = tinfo->vnt_reclaim_func;
+ rp->vr_data = tinfo->vnt_data;
+ rp->vr_lastseq = 0;
+ rp->vr_flags = tinfo->vnt_flags & VNT_VALID_MASK;
+ if (external) {
+ rp->vr_flags |= VNT_EXTERNAL;
+ }
+
+ result = vnode_resolver_attach(vp, rp, external);
+ if (result != 0) {
+ goto out;
+ }
+
+ if (mp) {
+ OSAddAtomic(1, &mp->mnt_numtriggers);
+ }
+
+ return (result);
+
+out:
+ FREE(rp, M_TEMP);
+ return result;
+}
+
+static void
+vnode_resolver_release(vnode_resolve_t rp)
+{
+ /*
+ * Give them a chance to free any private data
+ */
+ if (rp->vr_data && rp->vr_reclaim_func) {
+ rp->vr_reclaim_func(NULLVP, rp->vr_data);
+ }
+
+ lck_mtx_destroy(&rp->vr_lock, trigger_vnode_lck_grp);
+ FREE(rp, M_TEMP);
+
+}
+
+/* Called after the vnode has been drained */
+static void
+vnode_resolver_detach(vnode_t vp)
+{
+ vnode_resolve_t rp;
+ mount_t mp;
+
+ mp = vnode_mount(vp);
+
+ vnode_lock(vp);
+ rp = vp->v_resolve;
+ vp->v_resolve = NULL;
+ vnode_unlock(vp);
+
+ if ((rp->vr_flags & VNT_EXTERNAL) != 0) {
+ vnode_rele_ext(vp, O_EVTONLY, 1);
+ }
+
+ vnode_resolver_release(rp);
+
+ /* Keep count of active trigger vnodes per mount */
+ OSAddAtomic(-1, &mp->mnt_numtriggers);
+}
+
+/*
+ * Pathname operations that don't trigger a mount for trigger vnodes
+ */
+static const u_int64_t ignorable_pathops_mask =
+ 1LL << OP_MOUNT |
+ 1LL << OP_UNMOUNT |
+ 1LL << OP_STATFS |
+ 1LL << OP_ACCESS |
+ 1LL << OP_GETATTR |
+ 1LL << OP_LISTXATTR;
+
+int
+vfs_istraditionaltrigger(enum path_operation op, const struct componentname *cnp)
+{
+ if (cnp->cn_flags & ISLASTCN)
+ return ((1LL << op) & ignorable_pathops_mask) == 0;
+ else
+ return (1);
+}
+
+__private_extern__
+void
+vnode_trigger_rearm(vnode_t vp, vfs_context_t ctx)
+{
+ vnode_resolve_t rp;
+ resolver_result_t result;
+ enum resolver_status status;
+ uint32_t seq;
+
+ if ((vp->v_resolve == NULL) ||
+ (vp->v_resolve->vr_rearm_func == NULL) ||
+ (vp->v_resolve->vr_flags & VNT_AUTO_REARM) == 0) {
+ return;
+ }
+
+ rp = vp->v_resolve;
+ lck_mtx_lock(&rp->vr_lock);
+
+ /*
+ * Check if VFS initiated this unmount. If so, we'll catch it after the unresolve completes.
+ */
+ if (rp->vr_flags & VNT_VFS_UNMOUNTED) {
+ lck_mtx_unlock(&rp->vr_lock);
+ return;
+ }
+
+ /* Check if this vnode is already armed */
+ if ((rp->vr_flags & VNT_RESOLVED) == 0) {
+ lck_mtx_unlock(&rp->vr_lock);
+ return;
+ }
+
+ lck_mtx_unlock(&rp->vr_lock);
+
+ result = rp->vr_rearm_func(vp, 0, rp->vr_data, ctx);
+ status = vfs_resolver_status(result);
+ seq = vfs_resolver_sequence(result);
+
+ lck_mtx_lock(&rp->vr_lock);
+ if (seq > rp->vr_lastseq) {
+ if (status == RESOLVER_UNRESOLVED)
+ rp->vr_flags &= ~VNT_RESOLVED;
+ rp->vr_lastseq = seq;
+ }
+ lck_mtx_unlock(&rp->vr_lock);
+}
+
+__private_extern__
+int
+vnode_trigger_resolve(vnode_t vp, struct nameidata *ndp, vfs_context_t ctx)
+{
+ vnode_resolve_t rp;
+ enum path_operation op;
+ resolver_result_t result;
+ enum resolver_status status;
+ uint32_t seq;
+
+ /* Only trigger on topmost vnodes */
+ if ((vp->v_resolve == NULL) ||
+ (vp->v_resolve->vr_resolve_func == NULL) ||
+ (vp->v_mountedhere != NULL)) {
+ return (0);
+ }
+
+ rp = vp->v_resolve;
+ lck_mtx_lock(&rp->vr_lock);
+
+ /* Check if this vnode is already resolved */
+ if (rp->vr_flags & VNT_RESOLVED) {
+ lck_mtx_unlock(&rp->vr_lock);
+ return (0);
+ }
+
+ lck_mtx_unlock(&rp->vr_lock);
+
+ /*
+ * XXX
+ * assumes that resolver will not access this trigger vnode (otherwise the kernel will deadlock)
+ * is there anyway to know this???
+ * there can also be other legitimate lookups in parallel
+ *
+ * XXX - should we call this on a separate thread with a timeout?
+ *
+ * XXX - should we use ISLASTCN to pick the op value??? Perhaps only leafs should
+ * get the richer set and non-leafs should get generic OP_LOOKUP? TBD
+ */
+ op = (ndp->ni_op < OP_MAXOP) ? ndp->ni_op: OP_LOOKUP;
+
+ result = rp->vr_resolve_func(vp, &ndp->ni_cnd, op, 0, rp->vr_data, ctx);
+ status = vfs_resolver_status(result);
+ seq = vfs_resolver_sequence(result);
+
+ lck_mtx_lock(&rp->vr_lock);
+ if (seq > rp->vr_lastseq) {
+ if (status == RESOLVER_RESOLVED)
+ rp->vr_flags |= VNT_RESOLVED;
+ rp->vr_lastseq = seq;
+ }
+ lck_mtx_unlock(&rp->vr_lock);
+
+ /* On resolver errors, propagate the error back up */
+ return (status == RESOLVER_ERROR ? vfs_resolver_auxiliary(result) : 0);
+}
+
+static int
+vnode_trigger_unresolve(vnode_t vp, int flags, vfs_context_t ctx)
+{
+ vnode_resolve_t rp;
+ resolver_result_t result;
+ enum resolver_status status;
+ uint32_t seq;
+
+ if ((vp->v_resolve == NULL) || (vp->v_resolve->vr_unresolve_func == NULL)) {
+ return (0);
+ }
+
+ rp = vp->v_resolve;
+ lck_mtx_lock(&rp->vr_lock);
+
+ /* Check if this vnode is already resolved */
+ if ((rp->vr_flags & VNT_RESOLVED) == 0) {
+ printf("vnode_trigger_unresolve: not currently resolved\n");
+ lck_mtx_unlock(&rp->vr_lock);
+ return (0);
+ }
+
+ rp->vr_flags |= VNT_VFS_UNMOUNTED;
+
+ lck_mtx_unlock(&rp->vr_lock);
+
+ /*
+ * XXX
+ * assumes that resolver will not access this trigger vnode (otherwise the kernel will deadlock)
+ * there can also be other legitimate lookups in parallel
+ *
+ * XXX - should we call this on a separate thread with a timeout?
+ */
+
+ result = rp->vr_unresolve_func(vp, flags, rp->vr_data, ctx);
+ status = vfs_resolver_status(result);
+ seq = vfs_resolver_sequence(result);
+
+ lck_mtx_lock(&rp->vr_lock);
+ if (seq > rp->vr_lastseq) {
+ if (status == RESOLVER_UNRESOLVED)
+ rp->vr_flags &= ~VNT_RESOLVED;
+ rp->vr_lastseq = seq;
+ }
+ rp->vr_flags &= ~VNT_VFS_UNMOUNTED;
+ lck_mtx_unlock(&rp->vr_lock);
+
+ /* On resolver errors, propagate the error back up */
+ return (status == RESOLVER_ERROR ? vfs_resolver_auxiliary(result) : 0);
+}
+
+static int
+triggerisdescendant(mount_t mp, mount_t rmp)
+{
+ int match = FALSE;
+
+ /*
+ * walk up vnode covered chain looking for a match
+ */
+ name_cache_lock_shared();
+
+ while (1) {
+ vnode_t vp;
+
+ /* did we encounter "/" ? */
+ if (mp->mnt_flag & MNT_ROOTFS)
+ break;
+
+ vp = mp->mnt_vnodecovered;
+ if (vp == NULLVP)
+ break;
+
+ mp = vp->v_mount;
+ if (mp == rmp) {
+ match = TRUE;
+ break;
+ }
+ }
+
+ name_cache_unlock();
+
+ return (match);
+}
+
+struct trigger_unmount_info {
+ vfs_context_t ctx;
+ mount_t top_mp;
+ vnode_t trigger_vp;
+ mount_t trigger_mp;
+ uint32_t trigger_vid;
+ int flags;
+};
+
+static int
+trigger_unmount_callback(mount_t mp, void * arg)
+{
+ struct trigger_unmount_info * infop = (struct trigger_unmount_info *)arg;
+ boolean_t mountedtrigger = FALSE;
+
+ /*
+ * When we encounter the top level mount we're done
+ */
+ if (mp == infop->top_mp)
+ return (VFS_RETURNED_DONE);
+
+ if ((mp->mnt_vnodecovered == NULL) ||
+ (vnode_getwithref(mp->mnt_vnodecovered) != 0)) {
+ return (VFS_RETURNED);
+ }
+
+ if ((mp->mnt_vnodecovered->v_mountedhere == mp) &&
+ (mp->mnt_vnodecovered->v_resolve != NULL) &&
+ (mp->mnt_vnodecovered->v_resolve->vr_flags & VNT_RESOLVED)) {
+ mountedtrigger = TRUE;
+ }
+ vnode_put(mp->mnt_vnodecovered);
+
+ /*
+ * When we encounter a mounted trigger, check if its under the top level mount
+ */
+ if ( !mountedtrigger || !triggerisdescendant(mp, infop->top_mp) )
+ return (VFS_RETURNED);
+
+ /*
+ * Process any pending nested mount (now that its not referenced)
+ */
+ if ((infop->trigger_vp != NULLVP) &&
+ (vnode_getwithvid(infop->trigger_vp, infop->trigger_vid) == 0)) {
+ vnode_t vp = infop->trigger_vp;
+ int error;
+
+ infop->trigger_vp = NULLVP;
+
+ if (mp == vp->v_mountedhere) {
+ vnode_put(vp);
+ printf("trigger_unmount_callback: unexpected match '%s'\n",
+ mp->mnt_vfsstat.f_mntonname);
+ return (VFS_RETURNED);
+ }
+ if (infop->trigger_mp != vp->v_mountedhere) {
+ vnode_put(vp);
+ printf("trigger_unmount_callback: trigger mnt changed! (%p != %p)\n",
+ infop->trigger_mp, vp->v_mountedhere);
+ goto savenext;
+ }
+
+ error = vnode_trigger_unresolve(vp, infop->flags, infop->ctx);
+ vnode_put(vp);
+ if (error) {
+ printf("unresolving: '%s', err %d\n",
+ vp->v_mountedhere ? vp->v_mountedhere->mnt_vfsstat.f_mntonname :
+ "???", error);
+ return (VFS_RETURNED_DONE); /* stop iteration on errors */
+ }
+ }
+savenext:
+ /*
+ * We can't call resolver here since we hold a mount iter
+ * ref on mp so save its covered vp for later processing
+ */
+ infop->trigger_vp = mp->mnt_vnodecovered;
+ if ((infop->trigger_vp != NULLVP) &&
+ (vnode_getwithref(infop->trigger_vp) == 0)) {
+ if (infop->trigger_vp->v_mountedhere == mp) {
+ infop->trigger_vid = infop->trigger_vp->v_id;
+ infop->trigger_mp = mp;
+ }
+ vnode_put(infop->trigger_vp);
+ }
+
+ return (VFS_RETURNED);
+}
+
+/*
+ * Attempt to unmount any trigger mounts nested underneath a mount.
+ * This is a best effort attempt and no retries are performed here.
+ *
+ * Note: mp->mnt_rwlock is held exclusively on entry (so be carefull)
+ */
+__private_extern__
+void
+vfs_nested_trigger_unmounts(mount_t mp, int flags, vfs_context_t ctx)
+{
+ struct trigger_unmount_info info;
+
+ /* Must have trigger vnodes */
+ if (mp->mnt_numtriggers == 0) {
+ return;
+ }
+ /* Avoid recursive requests (by checking covered vnode) */
+ if ((mp->mnt_vnodecovered != NULL) &&
+ (vnode_getwithref(mp->mnt_vnodecovered) == 0)) {
+ boolean_t recursive = FALSE;
+
+ if ((mp->mnt_vnodecovered->v_mountedhere == mp) &&
+ (mp->mnt_vnodecovered->v_resolve != NULL) &&
+ (mp->mnt_vnodecovered->v_resolve->vr_flags & VNT_VFS_UNMOUNTED)) {
+ recursive = TRUE;
+ }
+ vnode_put(mp->mnt_vnodecovered);
+ if (recursive)
+ return;
+ }
+
+ /*
+ * Attempt to unmount any nested trigger mounts (best effort)
+ */
+ info.ctx = ctx;
+ info.top_mp = mp;
+ info.trigger_vp = NULLVP;
+ info.trigger_vid = 0;
+ info.trigger_mp = NULL;
+ info.flags = flags;
+
+ (void) vfs_iterate(VFS_ITERATE_TAIL_FIRST, trigger_unmount_callback, &info);
+
+ /*
+ * Process remaining nested mount (now that its not referenced)
+ */
+ if ((info.trigger_vp != NULLVP) &&
+ (vnode_getwithvid(info.trigger_vp, info.trigger_vid) == 0)) {
+ vnode_t vp = info.trigger_vp;
+
+ if (info.trigger_mp == vp->v_mountedhere) {
+ (void) vnode_trigger_unresolve(vp, flags, ctx);
+ }
+ vnode_put(vp);
+ }
+}
+
+int
+vfs_addtrigger(mount_t mp, const char *relpath, struct vnode_trigger_info *vtip, vfs_context_t ctx)
+{
+ struct nameidata nd;
+ int res;
+ vnode_t rvp, vp;
+ struct vnode_trigger_param vtp;
+
+ /*
+ * Must be called for trigger callback, wherein rwlock is held
+ */
+ lck_rw_assert(&mp->mnt_rwlock, LCK_RW_ASSERT_HELD);
+
+ TRIG_LOG("Adding trigger at %s\n", relpath);
+ TRIG_LOG("Trying VFS_ROOT\n");
+
+ /*
+ * We do a lookup starting at the root of the mountpoint, unwilling
+ * to cross into other mountpoints.
+ */
+ res = VFS_ROOT(mp, &rvp, ctx);
+ if (res != 0) {
+ goto out;
+ }
+
+ TRIG_LOG("Trying namei\n");
+
+ NDINIT(&nd, LOOKUP, OP_LOOKUP, USEDVP | NOCROSSMOUNT | FOLLOW, UIO_SYSSPACE,
+ CAST_USER_ADDR_T(relpath), ctx);
+ nd.ni_dvp = rvp;
+ res = namei(&nd);
+ if (res != 0) {
+ vnode_put(rvp);
+ goto out;
+ }
+
+ vp = nd.ni_vp;
+ nameidone(&nd);
+ vnode_put(rvp);
+
+ TRIG_LOG("Trying vnode_resolver_create()\n");
+
+ /*
+ * Set up blob. vnode_create() takes a larger structure
+ * with creation info, and we needed something different
+ * for this case. One needs to win, or we need to munge both;
+ * vnode_create() wins.
+ */
+ bzero(&vtp, sizeof(vtp));
+ vtp.vnt_resolve_func = vtip->vti_resolve_func;
+ vtp.vnt_unresolve_func = vtip->vti_unresolve_func;
+ vtp.vnt_rearm_func = vtip->vti_rearm_func;
+ vtp.vnt_reclaim_func = vtip->vti_reclaim_func;
+ vtp.vnt_reclaim_func = vtip->vti_reclaim_func;
+ vtp.vnt_data = vtip->vti_data;
+ vtp.vnt_flags = vtip->vti_flags;
+
+ res = vnode_resolver_create(mp, vp, &vtp, TRUE);
+ vnode_put(vp);
+out:
+ TRIG_LOG("Returning %d\n", res);
+ return res;
+}
+
+#endif /* CONFIG_TRIGGERS */