+#if CONFIG_TRIGGERS
+
+/*
+ * We've detected a file system boundary on the server and
+ * need to mount a new file system so that our file systems
+ * MIRROR the file systems on the server.
+ *
+ * Build the mount arguments for the new mount and call kernel_mount().
+ */
+int
+nfs_mirror_mount_domount(vnode_t dvp, vnode_t vp, vfs_context_t ctx)
+{
+ nfsnode_t np = VTONFS(vp);
+ nfsnode_t dnp = VTONFS(dvp);
+ struct nfsmount *nmp = NFSTONMP(np);
+ char fstype[MFSTYPENAMELEN], *mntfromname = NULL, *path = NULL, *relpath, *p, *cp;
+ int error = 0, pathbuflen = MAXPATHLEN, i, mntflags = 0, referral, skipcopy = 0;
+ size_t nlen;
+ struct xdrbuf xb, xbnew;
+ uint32_t mattrs[NFS_MATTR_BITMAP_LEN];
+ uint32_t newmattrs[NFS_MATTR_BITMAP_LEN];
+ uint32_t newmflags[NFS_MFLAG_BITMAP_LEN];
+ uint32_t newmflags_mask[NFS_MFLAG_BITMAP_LEN];
+ uint32_t argslength = 0, val, count, mlen, mlen2, rlen, relpathcomps;
+ uint32_t argslength_offset, attrslength_offset, end_offset;
+ uint32_t numlocs, loc, numserv, serv, numaddr, addr, numcomp, comp;
+ char buf[XDRWORD];
+ struct nfs_fs_locations nfsls;
+
+ referral = (np->n_vattr.nva_flags & NFS_FFLAG_TRIGGER_REFERRAL);
+ if (referral)
+ bzero(&nfsls, sizeof(nfsls));
+
+ xb_init(&xbnew, 0);
+
+ if (!nmp || (nmp->nm_state & NFSSTA_FORCE))
+ return (ENXIO);
+
+ /* allocate a couple path buffers we need */
+ MALLOC_ZONE(mntfromname, char *, pathbuflen, M_NAMEI, M_WAITOK);
+ if (!mntfromname) {
+ error = ENOMEM;
+ goto nfsmerr;
+ }
+ MALLOC_ZONE(path, char *, pathbuflen, M_NAMEI, M_WAITOK);
+ if (!path) {
+ error = ENOMEM;
+ goto nfsmerr;
+ }
+
+ /* get the path for the directory being mounted on */
+ error = vn_getpath(vp, path, &pathbuflen);
+ if (error) {
+ error = ENOMEM;
+ goto nfsmerr;
+ }
+
+ /*
+ * Set up the mntfromname for the new mount based on the
+ * current mount's mntfromname and the directory's path
+ * relative to the current mount's mntonname.
+ * Set up relpath to point at the relative path on the current mount.
+ * Also, count the number of components in relpath.
+ * We'll be adding those to each fs location path in the new args.
+ */
+ nlen = strlcpy(mntfromname, vfs_statfs(nmp->nm_mountp)->f_mntfromname, MAXPATHLEN);
+ if ((nlen > 0) && (mntfromname[nlen-1] == '/')) { /* avoid double '/' in new name */
+ mntfromname[nlen-1] = '\0';
+ nlen--;
+ }
+ relpath = mntfromname + nlen;
+ nlen = strlcat(mntfromname, path + strlen(vfs_statfs(nmp->nm_mountp)->f_mntonname), MAXPATHLEN);
+ if (nlen >= MAXPATHLEN) {
+ error = ENAMETOOLONG;
+ goto nfsmerr;
+ }
+ /* count the number of components in relpath */
+ p = relpath;
+ while (*p && (*p == '/'))
+ p++;
+ relpathcomps = 0;
+ while (*p) {
+ relpathcomps++;
+ while (*p && (*p != '/'))
+ p++;
+ while (*p && (*p == '/'))
+ p++;
+ }
+
+ /* grab a copy of the file system type */
+ vfs_name(vnode_mount(vp), fstype);
+
+ /* for referrals, fetch the fs locations */
+ if (referral) {
+ const char *vname = vnode_getname(NFSTOV(np));
+ if (!vname) {
+ error = ENOENT;
+ } else {
+ error = nfs4_get_fs_locations(nmp, dnp, NULL, 0, vname, ctx, &nfsls);
+ vnode_putname(vname);
+ if (!error && (nfsls.nl_numlocs < 1))
+ error = ENOENT;
+ }
+ nfsmerr_if(error);
+ }
+
+ /* set up NFS mount args based on current mount args */
+
+#define xb_copy_32(E, XBSRC, XBDST, V) \
+ do { \
+ if (E) break; \
+ xb_get_32((E), (XBSRC), (V)); \
+ if (skipcopy) break; \
+ xb_add_32((E), (XBDST), (V)); \
+ } while (0)
+#define xb_copy_opaque(E, XBSRC, XBDST) \
+ do { \
+ uint32_t __count, __val; \
+ xb_copy_32((E), (XBSRC), (XBDST), __count); \
+ if (E) break; \
+ __count = nfsm_rndup(__count); \
+ __count /= XDRWORD; \
+ while (__count-- > 0) \
+ xb_copy_32((E), (XBSRC), (XBDST), __val); \
+ } while (0)
+
+ xb_init_buffer(&xb, nmp->nm_args, 2*XDRWORD);
+ xb_get_32(error, &xb, val); /* version */
+ xb_get_32(error, &xb, argslength); /* args length */
+ xb_init_buffer(&xb, nmp->nm_args, argslength);
+
+ xb_init_buffer(&xbnew, NULL, 0);
+ xb_copy_32(error, &xb, &xbnew, val); /* version */
+ argslength_offset = xb_offset(&xbnew);
+ xb_copy_32(error, &xb, &xbnew, val); /* args length */
+ xb_copy_32(error, &xb, &xbnew, val); /* XDR args version */
+ count = NFS_MATTR_BITMAP_LEN;
+ xb_get_bitmap(error, &xb, mattrs, count); /* mount attribute bitmap */
+ nfsmerr_if(error);
+ for (i = 0; i < NFS_MATTR_BITMAP_LEN; i++)
+ newmattrs[i] = mattrs[i];
+ if (referral)
+ NFS_BITMAP_SET(newmattrs, NFS_MATTR_FS_LOCATIONS);
+ else
+ NFS_BITMAP_SET(newmattrs, NFS_MATTR_FH);
+ NFS_BITMAP_SET(newmattrs, NFS_MATTR_FLAGS);
+ NFS_BITMAP_SET(newmattrs, NFS_MATTR_MNTFLAGS);
+ NFS_BITMAP_CLR(newmattrs, NFS_MATTR_MNTFROM);
+ xb_add_bitmap(error, &xbnew, newmattrs, NFS_MATTR_BITMAP_LEN);
+ attrslength_offset = xb_offset(&xbnew);
+ xb_copy_32(error, &xb, &xbnew, val); /* attrs length */
+ NFS_BITMAP_ZERO(newmflags_mask, NFS_MFLAG_BITMAP_LEN);
+ NFS_BITMAP_ZERO(newmflags, NFS_MFLAG_BITMAP_LEN);
+ if (NFS_BITMAP_ISSET(mattrs, NFS_MATTR_FLAGS)) {
+ count = NFS_MFLAG_BITMAP_LEN;
+ xb_get_bitmap(error, &xb, newmflags_mask, count); /* mount flag mask bitmap */
+ count = NFS_MFLAG_BITMAP_LEN;
+ xb_get_bitmap(error, &xb, newmflags, count); /* mount flag bitmap */
+ }
+ NFS_BITMAP_SET(newmflags_mask, NFS_MFLAG_EPHEMERAL);
+ NFS_BITMAP_SET(newmflags, NFS_MFLAG_EPHEMERAL);
+ xb_add_bitmap(error, &xbnew, newmflags_mask, NFS_MFLAG_BITMAP_LEN);
+ xb_add_bitmap(error, &xbnew, newmflags, NFS_MFLAG_BITMAP_LEN);
+ if (NFS_BITMAP_ISSET(mattrs, NFS_MATTR_NFS_VERSION))
+ xb_copy_32(error, &xb, &xbnew, val);
+ if (NFS_BITMAP_ISSET(mattrs, NFS_MATTR_NFS_MINOR_VERSION))
+ xb_copy_32(error, &xb, &xbnew, val);
+ if (NFS_BITMAP_ISSET(mattrs, NFS_MATTR_READ_SIZE))
+ xb_copy_32(error, &xb, &xbnew, val);
+ if (NFS_BITMAP_ISSET(mattrs, NFS_MATTR_WRITE_SIZE))
+ xb_copy_32(error, &xb, &xbnew, val);
+ if (NFS_BITMAP_ISSET(mattrs, NFS_MATTR_READDIR_SIZE))
+ xb_copy_32(error, &xb, &xbnew, val);
+ if (NFS_BITMAP_ISSET(mattrs, NFS_MATTR_READAHEAD))
+ xb_copy_32(error, &xb, &xbnew, val);
+ if (NFS_BITMAP_ISSET(mattrs, NFS_MATTR_ATTRCACHE_REG_MIN)) {
+ xb_copy_32(error, &xb, &xbnew, val);
+ xb_copy_32(error, &xb, &xbnew, val);
+ }
+ if (NFS_BITMAP_ISSET(mattrs, NFS_MATTR_ATTRCACHE_REG_MAX)) {
+ xb_copy_32(error, &xb, &xbnew, val);
+ xb_copy_32(error, &xb, &xbnew, val);
+ }
+ if (NFS_BITMAP_ISSET(mattrs, NFS_MATTR_ATTRCACHE_DIR_MIN)) {
+ xb_copy_32(error, &xb, &xbnew, val);
+ xb_copy_32(error, &xb, &xbnew, val);
+ }
+ if (NFS_BITMAP_ISSET(mattrs, NFS_MATTR_ATTRCACHE_DIR_MAX)) {
+ xb_copy_32(error, &xb, &xbnew, val);
+ xb_copy_32(error, &xb, &xbnew, val);
+ }
+ if (NFS_BITMAP_ISSET(mattrs, NFS_MATTR_LOCK_MODE))
+ xb_copy_32(error, &xb, &xbnew, val);
+ if (NFS_BITMAP_ISSET(mattrs, NFS_MATTR_SECURITY)) {
+ xb_copy_32(error, &xb, &xbnew, count);
+ while (!error && (count-- > 0))
+ xb_copy_32(error, &xb, &xbnew, val);
+ }
+ if (NFS_BITMAP_ISSET(mattrs, NFS_MATTR_MAX_GROUP_LIST))
+ xb_copy_32(error, &xb, &xbnew, val);
+ if (NFS_BITMAP_ISSET(mattrs, NFS_MATTR_SOCKET_TYPE))
+ xb_copy_opaque(error, &xb, &xbnew);
+ if (NFS_BITMAP_ISSET(mattrs, NFS_MATTR_NFS_PORT))
+ xb_copy_32(error, &xb, &xbnew, val);
+ if (NFS_BITMAP_ISSET(mattrs, NFS_MATTR_MOUNT_PORT))
+ xb_copy_32(error, &xb, &xbnew, val);
+ if (NFS_BITMAP_ISSET(mattrs, NFS_MATTR_REQUEST_TIMEOUT)) {
+ xb_copy_32(error, &xb, &xbnew, val);
+ xb_copy_32(error, &xb, &xbnew, val);
+ }
+ if (NFS_BITMAP_ISSET(mattrs, NFS_MATTR_SOFT_RETRY_COUNT))
+ xb_copy_32(error, &xb, &xbnew, val);
+ if (NFS_BITMAP_ISSET(mattrs, NFS_MATTR_DEAD_TIMEOUT)) {
+ xb_copy_32(error, &xb, &xbnew, val);
+ xb_copy_32(error, &xb, &xbnew, val);
+ }
+ if (NFS_BITMAP_ISSET(mattrs, NFS_MATTR_FH)) {
+ xb_get_32(error, &xb, count);
+ xb_skip(error, &xb, count);
+ }
+ if (!referral) {
+ /* set the initial file handle to the directory's file handle */
+ xb_add_fh(error, &xbnew, np->n_fhp, np->n_fhsize);
+ }
+ /* copy/extend/skip fs locations */
+ if (NFS_BITMAP_ISSET(mattrs, NFS_MATTR_FS_LOCATIONS)) {
+ numlocs = numserv = numaddr = numcomp = 0;
+ if (referral) /* don't copy the fs locations for a referral */
+ skipcopy = 1;
+ xb_copy_32(error, &xb, &xbnew, numlocs); /* location count */
+ for (loc = 0; !error && (loc < numlocs); loc++) {
+ xb_copy_32(error, &xb, &xbnew, numserv); /* server count */
+ for (serv = 0; !error && (serv < numserv); serv++) {
+ xb_copy_opaque(error, &xb, &xbnew); /* server name */
+ xb_copy_32(error, &xb, &xbnew, numaddr); /* address count */
+ for (addr = 0; !error && (addr < numaddr); addr++)
+ xb_copy_opaque(error, &xb, &xbnew); /* address */
+ xb_copy_opaque(error, &xb, &xbnew); /* server info */
+ }
+ /* pathname */
+ xb_get_32(error, &xb, numcomp); /* component count */
+ if (!skipcopy)
+ xb_add_32(error, &xbnew, numcomp+relpathcomps); /* new component count */
+ for (comp = 0; !error && (comp < numcomp); comp++)
+ xb_copy_opaque(error, &xb, &xbnew); /* component */
+ /* add additional components */
+ for (comp = 0; !skipcopy && !error && (comp < relpathcomps); comp++) {
+ p = relpath;
+ while (*p && (*p == '/'))
+ p++;
+ while (*p && !error) {
+ cp = p;
+ while (*p && (*p != '/'))
+ p++;
+ xb_add_string(error, &xbnew, cp, (p - cp)); /* component */
+ while (*p && (*p == '/'))
+ p++;
+ }
+ }
+ xb_copy_opaque(error, &xb, &xbnew); /* fs location info */
+ }
+ if (referral)
+ skipcopy = 0;
+ }
+ if (referral) {
+ /* add referral's fs locations */
+ xb_add_32(error, &xbnew, nfsls.nl_numlocs); /* FS_LOCATIONS */
+ for (loc = 0; !error && (loc < nfsls.nl_numlocs); loc++) {
+ xb_add_32(error, &xbnew, nfsls.nl_locations[loc]->nl_servcount);
+ for (serv = 0; !error && (serv < nfsls.nl_locations[loc]->nl_servcount); serv++) {
+ xb_add_string(error, &xbnew, nfsls.nl_locations[loc]->nl_servers[serv]->ns_name,
+ strlen(nfsls.nl_locations[loc]->nl_servers[serv]->ns_name));
+ xb_add_32(error, &xbnew, nfsls.nl_locations[loc]->nl_servers[serv]->ns_addrcount);
+ for (addr = 0; !error && (addr < nfsls.nl_locations[loc]->nl_servers[serv]->ns_addrcount); addr++)
+ xb_add_string(error, &xbnew, nfsls.nl_locations[loc]->nl_servers[serv]->ns_addresses[addr],
+ strlen(nfsls.nl_locations[loc]->nl_servers[serv]->ns_addresses[addr]));
+ xb_add_32(error, &xbnew, 0); /* empty server info */
+ }
+ xb_add_32(error, &xbnew, nfsls.nl_locations[loc]->nl_path.np_compcount);
+ for (comp = 0; !error && (comp < nfsls.nl_locations[loc]->nl_path.np_compcount); comp++)
+ xb_add_string(error, &xbnew, nfsls.nl_locations[loc]->nl_path.np_components[comp],
+ strlen(nfsls.nl_locations[loc]->nl_path.np_components[comp]));
+ xb_add_32(error, &xbnew, 0); /* empty fs location info */
+ }
+ }
+ if (NFS_BITMAP_ISSET(mattrs, NFS_MATTR_MNTFLAGS))
+ xb_get_32(error, &xb, mntflags);
+ /*
+ * We add the following mount flags to the ones for the mounted-on mount:
+ * MNT_DONTBROWSE - to keep the mount from showing up as a separate volume
+ * MNT_AUTOMOUNTED - to keep DiskArb from retriggering the mount after
+ * an unmount (looking for /.autodiskmounted)
+ */
+ mntflags |= (MNT_AUTOMOUNTED | MNT_DONTBROWSE);
+ xb_add_32(error, &xbnew, mntflags);
+ if (!referral && NFS_BITMAP_ISSET(mattrs, NFS_MATTR_MNTFROM)) {
+ /* copy mntfrom string and add relpath */
+ rlen = strlen(relpath);
+ xb_get_32(error, &xb, mlen);
+ nfsmerr_if(error);
+ mlen2 = mlen + ((relpath[0] != '/') ? 1 : 0) + rlen;
+ xb_add_32(error, &xbnew, mlen2);
+ count = mlen/XDRWORD;
+ /* copy the original string */
+ while (count-- > 0)
+ xb_copy_32(error, &xb, &xbnew, val);
+ if (!error && (mlen % XDRWORD)) {
+ error = xb_get_bytes(&xb, buf, mlen%XDRWORD, 0);
+ if (!error)
+ error = xb_add_bytes(&xbnew, buf, mlen%XDRWORD, 1);
+ }
+ /* insert a '/' if the relative path doesn't start with one */
+ if (!error && (relpath[0] != '/')) {
+ buf[0] = '/';
+ error = xb_add_bytes(&xbnew, buf, 1, 1);
+ }
+ /* add the additional relative path */
+ if (!error)
+ error = xb_add_bytes(&xbnew, relpath, rlen, 1);
+ /* make sure the resulting string has the right number of pad bytes */
+ if (!error && (mlen2 != nfsm_rndup(mlen2))) {
+ bzero(buf, sizeof(buf));
+ count = nfsm_rndup(mlen2) - mlen2;
+ error = xb_add_bytes(&xbnew, buf, count, 1);
+ }
+ }
+ xb_build_done(error, &xbnew);
+
+ /* update opaque counts */
+ end_offset = xb_offset(&xbnew);
+ if (!error) {
+ error = xb_seek(&xbnew, argslength_offset);
+ argslength = end_offset - argslength_offset + XDRWORD/*version*/;
+ xb_add_32(error, &xbnew, argslength);
+ }
+ if (!error) {
+ error = xb_seek(&xbnew, attrslength_offset);
+ xb_add_32(error, &xbnew, end_offset - attrslength_offset - XDRWORD/*don't include length field*/);
+ }
+ nfsmerr_if(error);
+
+ /*
+ * For kernel_mount() call, use the existing mount flags (instead of the
+ * original flags) because flags like MNT_NOSUID and MNT_NODEV may have
+ * been silently enforced.
+ */
+ mntflags = vnode_vfsvisflags(vp);
+ mntflags |= (MNT_AUTOMOUNTED | MNT_DONTBROWSE);
+
+ /* do the mount */
+ error = kernel_mount(fstype, dvp, vp, path, xb_buffer_base(&xbnew), argslength,
+ mntflags, KERNEL_MOUNT_PERMIT_UNMOUNT | KERNEL_MOUNT_NOAUTH, ctx);
+
+nfsmerr:
+ if (error)
+ printf("nfs: mirror mount of %s on %s failed (%d)\n",
+ mntfromname, path, error);
+ /* clean up */
+ xb_cleanup(&xbnew);
+ if (referral)
+ nfs_fs_locations_cleanup(&nfsls);
+ if (path)
+ FREE_ZONE(path, MAXPATHLEN, M_NAMEI);
+ if (mntfromname)
+ FREE_ZONE(mntfromname, MAXPATHLEN, M_NAMEI);
+ if (!error)
+ nfs_ephemeral_mount_harvester_start();
+ return (error);
+}
+
+/*
+ * trigger vnode functions
+ */
+
+resolver_result_t
+nfs_mirror_mount_trigger_resolve(
+ vnode_t vp,
+ const struct componentname *cnp,
+ enum path_operation pop,
+ __unused int flags,
+ __unused void *data,
+ vfs_context_t ctx)
+{
+ nfsnode_t np = VTONFS(vp);
+ vnode_t pvp = NULLVP;
+ int error = 0;
+ resolver_result_t result;
+
+ /*
+ * We have a trigger node that doesn't have anything mounted on it yet.
+ * We'll do the mount if either:
+ * (a) this isn't the last component of the path OR
+ * (b) this is an op that looks like it should trigger the mount.
+ */
+ if (cnp->cn_flags & ISLASTCN) {
+ switch (pop) {
+ case OP_MOUNT:
+ case OP_UNMOUNT:
+ case OP_STATFS:
+ case OP_LINK:
+ case OP_UNLINK:
+ case OP_RENAME:
+ case OP_MKNOD:
+ case OP_MKFIFO:
+ case OP_SYMLINK:
+ case OP_ACCESS:
+ case OP_GETATTR:
+ case OP_MKDIR:
+ case OP_RMDIR:
+ case OP_REVOKE:
+ case OP_GETXATTR:
+ case OP_LISTXATTR:
+ /* don't perform the mount for these operations */
+ result = vfs_resolver_result(np->n_trigseq, RESOLVER_NOCHANGE, 0);
+#ifdef NFS_TRIGGER_DEBUG
+ NP(np, "nfs trigger RESOLVE: no change, last %d nameiop %d, seq %d",
+ (cnp->cn_flags & ISLASTCN) ? 1 : 0, cnp->cn_nameiop, np->n_trigseq);
+#endif
+ return (result);
+ case OP_OPEN:
+ case OP_CHDIR:
+ case OP_CHROOT:
+ case OP_TRUNCATE:
+ case OP_COPYFILE:
+ case OP_PATHCONF:
+ case OP_READLINK:
+ case OP_SETATTR:
+ case OP_EXCHANGEDATA:
+ case OP_SEARCHFS:
+ case OP_FSCTL:
+ case OP_SETXATTR:
+ case OP_REMOVEXATTR:
+ default:
+ /* go ahead and do the mount */
+ break;
+ }
+ }
+
+ if (vnode_mountedhere(vp) != NULL) {
+ /*
+ * Um... there's already something mounted.
+ * Been there. Done that. Let's just say it succeeded.
+ */
+ error = 0;
+ goto skipmount;
+ }
+
+ if ((error = nfs_node_set_busy(np, vfs_context_thread(ctx)))) {
+ result = vfs_resolver_result(np->n_trigseq, RESOLVER_ERROR, error);
+#ifdef NFS_TRIGGER_DEBUG
+ NP(np, "nfs trigger RESOLVE: busy error %d, last %d nameiop %d, seq %d",
+ error, (cnp->cn_flags & ISLASTCN) ? 1 : 0, cnp->cn_nameiop, np->n_trigseq);
+#endif
+ return (result);
+ }
+
+ pvp = vnode_getparent(vp);
+ if (pvp == NULLVP)
+ error = EINVAL;
+ if (!error)
+ error = nfs_mirror_mount_domount(pvp, vp, ctx);
+skipmount:
+ if (!error)
+ np->n_trigseq++;
+ result = vfs_resolver_result(np->n_trigseq, error ? RESOLVER_ERROR : RESOLVER_RESOLVED, error);
+#ifdef NFS_TRIGGER_DEBUG
+ NP(np, "nfs trigger RESOLVE: %s %d, last %d nameiop %d, seq %d",
+ error ? "error" : "resolved", error,
+ (cnp->cn_flags & ISLASTCN) ? 1 : 0, cnp->cn_nameiop, np->n_trigseq);
+#endif
+
+ if (pvp != NULLVP)
+ vnode_put(pvp);
+ nfs_node_clear_busy(np);
+ return (result);
+}
+
+resolver_result_t
+nfs_mirror_mount_trigger_unresolve(
+ vnode_t vp,
+ int flags,
+ __unused void *data,
+ vfs_context_t ctx)
+{
+ nfsnode_t np = VTONFS(vp);
+ mount_t mp;
+ int error;
+ resolver_result_t result;
+
+ if ((error = nfs_node_set_busy(np, vfs_context_thread(ctx)))) {
+ result = vfs_resolver_result(np->n_trigseq, RESOLVER_ERROR, error);
+#ifdef NFS_TRIGGER_DEBUG
+ NP(np, "nfs trigger UNRESOLVE: busy error %d, seq %d", error, np->n_trigseq);
+#endif
+ return (result);
+ }
+
+ mp = vnode_mountedhere(vp);
+ if (!mp)
+ error = EINVAL;
+ if (!error)
+ error = vfs_unmountbyfsid(&(vfs_statfs(mp)->f_fsid), flags, ctx);
+ if (!error)
+ np->n_trigseq++;
+ result = vfs_resolver_result(np->n_trigseq, error ? RESOLVER_ERROR : RESOLVER_UNRESOLVED, error);
+#ifdef NFS_TRIGGER_DEBUG
+ NP(np, "nfs trigger UNRESOLVE: %s %d, seq %d",
+ error ? "error" : "unresolved", error, np->n_trigseq);
+#endif
+ nfs_node_clear_busy(np);
+ return (result);
+}
+
+resolver_result_t
+nfs_mirror_mount_trigger_rearm(
+ vnode_t vp,
+ __unused int flags,
+ __unused void *data,
+ vfs_context_t ctx)
+{
+ nfsnode_t np = VTONFS(vp);
+ int error;
+ resolver_result_t result;
+
+ if ((error = nfs_node_set_busy(np, vfs_context_thread(ctx)))) {
+ result = vfs_resolver_result(np->n_trigseq, RESOLVER_ERROR, error);
+#ifdef NFS_TRIGGER_DEBUG
+ NP(np, "nfs trigger REARM: busy error %d, seq %d", error, np->n_trigseq);
+#endif
+ return (result);
+ }
+
+ np->n_trigseq++;
+ result = vfs_resolver_result(np->n_trigseq,
+ vnode_mountedhere(vp) ? RESOLVER_RESOLVED : RESOLVER_UNRESOLVED, 0);
+#ifdef NFS_TRIGGER_DEBUG
+ NP(np, "nfs trigger REARM: %s, seq %d",
+ vnode_mountedhere(vp) ? "resolved" : "unresolved", np->n_trigseq);
+#endif
+ nfs_node_clear_busy(np);
+ return (result);
+}
+
+/*
+ * Periodically attempt to unmount ephemeral (mirror) mounts in an attempt to limit
+ * the number of unused mounts.
+ */
+
+#define NFS_EPHEMERAL_MOUNT_HARVEST_INTERVAL 120 /* how often the harvester runs */
+struct nfs_ephemeral_mount_harvester_info {
+ fsid_t fsid; /* FSID that we need to try to unmount */
+ uint32_t mountcount; /* count of ephemeral mounts seen in scan */
+ };
+/* various globals for the harvester */
+static thread_call_t nfs_ephemeral_mount_harvester_timer = NULL;
+static int nfs_ephemeral_mount_harvester_on = 0;
+
+kern_return_t thread_terminate(thread_t);
+
+static int
+nfs_ephemeral_mount_harvester_callback(mount_t mp, void *arg)
+{
+ struct nfs_ephemeral_mount_harvester_info *hinfo = arg;
+ struct nfsmount *nmp;
+ struct timeval now;
+
+ if (strcmp(mp->mnt_vfsstat.f_fstypename, "nfs"))
+ return (VFS_RETURNED);
+ nmp = VFSTONFS(mp);
+ if (!nmp || !NMFLAG(nmp, EPHEMERAL))
+ return (VFS_RETURNED);
+ hinfo->mountcount++;
+
+ /* avoid unmounting mounts that have been triggered within the last harvest interval */
+ microtime(&now);
+ if ((nmp->nm_mounttime >> 32) > ((uint32_t)now.tv_sec - NFS_EPHEMERAL_MOUNT_HARVEST_INTERVAL))
+ return (VFS_RETURNED);
+
+ if (hinfo->fsid.val[0] || hinfo->fsid.val[1]) {
+ /* attempt to unmount previously-found ephemeral mount */
+ vfs_unmountbyfsid(&hinfo->fsid, 0, vfs_context_kernel());
+ hinfo->fsid.val[0] = hinfo->fsid.val[1] = 0;
+ }
+
+ /*
+ * We can't call unmount here since we hold a mount iter ref
+ * on mp so save its fsid for the next call iteration to unmount.
+ */
+ hinfo->fsid.val[0] = mp->mnt_vfsstat.f_fsid.val[0];
+ hinfo->fsid.val[1] = mp->mnt_vfsstat.f_fsid.val[1];
+
+ return (VFS_RETURNED);
+}
+
+/*
+ * Spawn a thread to do the ephemeral mount harvesting.
+ */
+static void
+nfs_ephemeral_mount_harvester_timer_func(void)
+{
+ thread_t thd;
+
+ if (kernel_thread_start(nfs_ephemeral_mount_harvester, NULL, &thd) == KERN_SUCCESS)
+ thread_deallocate(thd);
+}
+
+/*
+ * Iterate all mounts looking for NFS ephemeral mounts to try to unmount.
+ */
+void
+nfs_ephemeral_mount_harvester(__unused void *arg, __unused wait_result_t wr)
+{
+ struct nfs_ephemeral_mount_harvester_info hinfo;
+ uint64_t deadline;
+
+ hinfo.mountcount = 0;
+ hinfo.fsid.val[0] = hinfo.fsid.val[1] = 0;
+ vfs_iterate(VFS_ITERATE_TAIL_FIRST, nfs_ephemeral_mount_harvester_callback, &hinfo);
+ if (hinfo.fsid.val[0] || hinfo.fsid.val[1]) {
+ /* attempt to unmount last found ephemeral mount */
+ vfs_unmountbyfsid(&hinfo.fsid, 0, vfs_context_kernel());
+ }
+
+ lck_mtx_lock(nfs_global_mutex);
+ if (!hinfo.mountcount) {
+ /* no more ephemeral mounts - don't need timer */
+ nfs_ephemeral_mount_harvester_on = 0;
+ } else {
+ /* re-arm the timer */
+ clock_interval_to_deadline(NFS_EPHEMERAL_MOUNT_HARVEST_INTERVAL, NSEC_PER_SEC, &deadline);
+ thread_call_enter_delayed(nfs_ephemeral_mount_harvester_timer, deadline);
+ nfs_ephemeral_mount_harvester_on = 1;
+ }
+ lck_mtx_unlock(nfs_global_mutex);
+
+ /* thread done */
+ thread_terminate(current_thread());
+}
+
+/*
+ * Make sure the NFS ephemeral mount harvester timer is running.
+ */
+void
+nfs_ephemeral_mount_harvester_start(void)
+{
+ uint64_t deadline;
+
+ lck_mtx_lock(nfs_global_mutex);
+ if (nfs_ephemeral_mount_harvester_on) {
+ lck_mtx_unlock(nfs_global_mutex);
+ return;
+ }
+ if (nfs_ephemeral_mount_harvester_timer == NULL)
+ nfs_ephemeral_mount_harvester_timer = thread_call_allocate((thread_call_func_t)nfs_ephemeral_mount_harvester_timer_func, NULL);
+ clock_interval_to_deadline(NFS_EPHEMERAL_MOUNT_HARVEST_INTERVAL, NSEC_PER_SEC, &deadline);
+ thread_call_enter_delayed(nfs_ephemeral_mount_harvester_timer, deadline);
+ nfs_ephemeral_mount_harvester_on = 1;
+ lck_mtx_unlock(nfs_global_mutex);
+}
+
+#endif
+
+/*
+ * Send a MOUNT protocol MOUNT request to the server to get the initial file handle (and security).
+ */
+int
+nfs3_mount_rpc(struct nfsmount *nmp, struct sockaddr *sa, int sotype, int nfsvers, char *path, vfs_context_t ctx, int timeo, fhandle_t *fh, struct nfs_sec *sec)
+{
+ int error = 0, slen, mntproto;
+ thread_t thd = vfs_context_thread(ctx);
+ kauth_cred_t cred = vfs_context_ucred(ctx);
+ uint64_t xid = 0;
+ struct nfsm_chain nmreq, nmrep;
+ mbuf_t mreq;
+ uint32_t mntvers, mntport, val;
+ struct sockaddr_storage ss;
+ struct sockaddr *saddr = (struct sockaddr*)&ss;
+
+ nfsm_chain_null(&nmreq);
+ nfsm_chain_null(&nmrep);
+
+ mntvers = (nfsvers == NFS_VER2) ? RPCMNT_VER1 : RPCMNT_VER3;
+ mntproto = (NM_OMFLAG(nmp, MNTUDP) || (sotype == SOCK_DGRAM)) ? IPPROTO_UDP : IPPROTO_TCP;
+ sec->count = 0;
+
+ bcopy(sa, saddr, min(sizeof(ss), sa->sa_len));
+ if (saddr->sa_family == AF_INET) {
+ if (nmp->nm_mountport)
+ ((struct sockaddr_in*)saddr)->sin_port = htons(nmp->nm_mountport);
+ mntport = ntohs(((struct sockaddr_in*)saddr)->sin_port);
+ } else {
+ if (nmp->nm_mountport)
+ ((struct sockaddr_in6*)saddr)->sin6_port = htons(nmp->nm_mountport);
+ mntport = ntohs(((struct sockaddr_in6*)saddr)->sin6_port);
+ }
+
+ while (!mntport) {
+ error = nfs_portmap_lookup(nmp, ctx, saddr, NULL, RPCPROG_MNT, mntvers, mntproto, timeo);
+ nfsmout_if(error);
+ if (saddr->sa_family == AF_INET)
+ mntport = ntohs(((struct sockaddr_in*)saddr)->sin_port);
+ else
+ mntport = ntohs(((struct sockaddr_in6*)saddr)->sin6_port);
+ if (!mntport) {
+ /* if not found and TCP, then retry with UDP */
+ if (mntproto == IPPROTO_UDP) {
+ error = EPROGUNAVAIL;
+ break;
+ }
+ mntproto = IPPROTO_UDP;
+ bcopy(sa, saddr, min(sizeof(ss), sa->sa_len));
+ }
+ }
+ nfsmout_if(error || !mntport);
+
+ /* MOUNT protocol MOUNT request */
+ slen = strlen(path);
+ nfsm_chain_build_alloc_init(error, &nmreq, NFSX_UNSIGNED + nfsm_rndup(slen));
+ nfsm_chain_add_name(error, &nmreq, path, slen, nmp);
+ nfsm_chain_build_done(error, &nmreq);
+ nfsmout_if(error);
+ error = nfsm_rpchead2(nmp, (mntproto == IPPROTO_UDP) ? SOCK_DGRAM : SOCK_STREAM,
+ RPCPROG_MNT, mntvers, RPCMNT_MOUNT,
+ RPCAUTH_SYS, cred, NULL, nmreq.nmc_mhead, &xid, &mreq);
+ nfsmout_if(error);
+ nmreq.nmc_mhead = NULL;
+ error = nfs_aux_request(nmp, thd, saddr, NULL,
+ ((mntproto == IPPROTO_UDP) ? SOCK_DGRAM : SOCK_STREAM),
+ mreq, R_XID32(xid), 1, timeo, &nmrep);
+ nfsmout_if(error);
+ nfsm_chain_get_32(error, &nmrep, val);
+ if (!error && val)
+ error = val;
+ nfsm_chain_get_fh(error, &nmrep, nfsvers, fh);
+ if (!error && (nfsvers > NFS_VER2)) {
+ sec->count = NX_MAX_SEC_FLAVORS;
+ error = nfsm_chain_get_secinfo(&nmrep, &sec->flavors[0], &sec->count);
+ }
+nfsmout:
+ nfsm_chain_cleanup(&nmreq);
+ nfsm_chain_cleanup(&nmrep);
+ return (error);
+}
+
+
+/*
+ * Send a MOUNT protocol UNMOUNT request to tell the server we've unmounted it.
+ */
+void
+nfs3_umount_rpc(struct nfsmount *nmp, vfs_context_t ctx, int timeo)
+{
+ int error = 0, slen, mntproto;
+ thread_t thd = vfs_context_thread(ctx);
+ kauth_cred_t cred = vfs_context_ucred(ctx);
+ char *path;
+ uint64_t xid = 0;
+ struct nfsm_chain nmreq, nmrep;
+ mbuf_t mreq;
+ uint32_t mntvers, mntport;
+ struct sockaddr_storage ss;
+ struct sockaddr *saddr = (struct sockaddr*)&ss;
+
+ if (!nmp->nm_saddr)
+ return;
+
+ nfsm_chain_null(&nmreq);
+ nfsm_chain_null(&nmrep);
+
+ mntvers = (nmp->nm_vers == NFS_VER2) ? RPCMNT_VER1 : RPCMNT_VER3;
+ mntproto = (NM_OMFLAG(nmp, MNTUDP) || (nmp->nm_sotype == SOCK_DGRAM)) ? IPPROTO_UDP : IPPROTO_TCP;
+ mntport = nmp->nm_mountport;
+
+ bcopy(nmp->nm_saddr, saddr, min(sizeof(ss), nmp->nm_saddr->sa_len));
+ if (saddr->sa_family == AF_INET)
+ ((struct sockaddr_in*)saddr)->sin_port = htons(mntport);
+ else
+ ((struct sockaddr_in6*)saddr)->sin6_port = htons(mntport);
+
+ while (!mntport) {
+ error = nfs_portmap_lookup(nmp, ctx, saddr, NULL, RPCPROG_MNT, mntvers, mntproto, timeo);
+ nfsmout_if(error);
+ if (saddr->sa_family == AF_INET)
+ mntport = ntohs(((struct sockaddr_in*)saddr)->sin_port);
+ else
+ mntport = ntohs(((struct sockaddr_in6*)saddr)->sin6_port);
+ /* if not found and mntvers > VER1, then retry with VER1 */
+ if (!mntport) {
+ if (mntvers > RPCMNT_VER1) {
+ mntvers = RPCMNT_VER1;
+ } else if (mntproto == IPPROTO_TCP) {
+ mntproto = IPPROTO_UDP;
+ mntvers = (nmp->nm_vers == NFS_VER2) ? RPCMNT_VER1 : RPCMNT_VER3;
+ } else {
+ break;
+ }
+ bcopy(nmp->nm_saddr, saddr, min(sizeof(ss), nmp->nm_saddr->sa_len));
+ }
+ }
+ nfsmout_if(!mntport);
+
+ /* MOUNT protocol UNMOUNT request */
+ path = &vfs_statfs(nmp->nm_mountp)->f_mntfromname[0];
+ while (*path && (*path != '/'))
+ path++;
+ slen = strlen(path);
+ nfsm_chain_build_alloc_init(error, &nmreq, NFSX_UNSIGNED + nfsm_rndup(slen));
+ nfsm_chain_add_name(error, &nmreq, path, slen, nmp);
+ nfsm_chain_build_done(error, &nmreq);
+ nfsmout_if(error);
+ error = nfsm_rpchead2(nmp, (mntproto == IPPROTO_UDP) ? SOCK_DGRAM : SOCK_STREAM,
+ RPCPROG_MNT, RPCMNT_VER1, RPCMNT_UMOUNT,
+ RPCAUTH_SYS, cred, NULL, nmreq.nmc_mhead, &xid, &mreq);
+ nfsmout_if(error);
+ nmreq.nmc_mhead = NULL;
+ error = nfs_aux_request(nmp, thd, saddr, NULL,
+ ((mntproto == IPPROTO_UDP) ? SOCK_DGRAM : SOCK_STREAM),
+ mreq, R_XID32(xid), 1, timeo, &nmrep);
+nfsmout:
+ nfsm_chain_cleanup(&nmreq);
+ nfsm_chain_cleanup(&nmrep);
+}
+