+ 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);
+}
+
+/*
+ * unmount system call
+ */
+int
+nfs_vfs_unmount(
+ mount_t mp,
+ int mntflags,
+ __unused vfs_context_t ctx)
+{
+ struct nfsmount *nmp;
+ vnode_t vp;
+ int error, flags = 0;
+ struct timespec ts = { 1, 0 };
+
+ nmp = VFSTONFS(mp);
+ lck_mtx_lock(&nmp->nm_lock);
+ /*
+ * Set the flag indicating that an unmount attempt is in progress.
+ */
+ nmp->nm_state |= NFSSTA_UNMOUNTING;
+ /*
+ * During a force unmount we want to...
+ * Mark that we are doing a force unmount.
+ * Make the mountpoint soft.
+ */
+ if (mntflags & MNT_FORCE) {
+ flags |= FORCECLOSE;
+ nmp->nm_state |= NFSSTA_FORCE;
+ NFS_BITMAP_SET(nmp->nm_flags, NFS_MFLAG_SOFT);
+ }
+ /*
+ * Wait for any in-progress monitored node scan to complete.
+ */
+ while (nmp->nm_state & NFSSTA_MONITOR_SCAN)
+ msleep(&nmp->nm_state, &nmp->nm_lock, PZERO-1, "nfswaitmonscan", &ts);
+ /*
+ * Goes something like this..
+ * - Call vflush() to clear out vnodes for this file system,
+ * except for the swap files. Deal with them in 2nd pass.
+ * - Decrement reference on the vnode representing remote root.
+ * - Clean up the NFS mount structure.
+ */
+ vp = NFSTOV(nmp->nm_dnp);
+ lck_mtx_unlock(&nmp->nm_lock);
+
+ /*
+ * vflush will check for busy vnodes on mountpoint.
+ * Will do the right thing for MNT_FORCE. That is, we should
+ * not get EBUSY back.
+ */
+ error = vflush(mp, vp, SKIPSWAP | flags);
+ if (mntflags & MNT_FORCE) {
+ error = vflush(mp, NULLVP, flags); /* locks vp in the process */
+ } else {
+ if (vnode_isinuse(vp, 1))
+ error = EBUSY;
+ else
+ error = vflush(mp, vp, flags);
+ }
+ if (error) {
+ lck_mtx_lock(&nmp->nm_lock);
+ nmp->nm_state &= ~NFSSTA_UNMOUNTING;
+ lck_mtx_unlock(&nmp->nm_lock);
+ return (error);
+ }
+
+ lck_mtx_lock(&nmp->nm_lock);
+ nmp->nm_dnp = NULL;
+ lck_mtx_unlock(&nmp->nm_lock);
+
+ /*
+ * Release the root vnode reference held by mountnfs()
+ */
+ error = vnode_get(vp);
+ vnode_rele(vp);
+ if (!error)
+ vnode_put(vp);
+
+ vflush(mp, NULLVP, FORCECLOSE);
+
+ /* Wait for all other references to be released and free the mount */
+ nfs_mount_drain_and_cleanup(nmp);
+
+ return (0);
+}
+
+/*
+ * cleanup/destroy NFS fs locations structure
+ */
+void
+nfs_fs_locations_cleanup(struct nfs_fs_locations *nfslsp)
+{
+ struct nfs_fs_location *fsl;
+ struct nfs_fs_server *fss;
+ struct nfs_fs_path *fsp;
+ uint32_t loc, serv, addr, comp;
+
+ /* free up fs locations */
+ if (!nfslsp->nl_numlocs || !nfslsp->nl_locations)
+ return;
+
+ for (loc = 0; loc < nfslsp->nl_numlocs; loc++) {
+ fsl = nfslsp->nl_locations[loc];
+ if (!fsl)
+ continue;
+ if ((fsl->nl_servcount > 0) && fsl->nl_servers) {
+ for (serv = 0; serv < fsl->nl_servcount; serv++) {
+ fss = fsl->nl_servers[serv];
+ if (!fss)
+ continue;
+ if ((fss->ns_addrcount > 0) && fss->ns_addresses) {
+ for (addr = 0; addr < fss->ns_addrcount; addr++)
+ FREE(fss->ns_addresses[addr], M_TEMP);
+ FREE(fss->ns_addresses, M_TEMP);
+ }
+ FREE(fss->ns_name, M_TEMP);
+ FREE(fss, M_TEMP);
+ }
+ FREE(fsl->nl_servers, M_TEMP);
+ }
+ fsp = &fsl->nl_path;
+ if (fsp->np_compcount && fsp->np_components) {
+ for (comp = 0; comp < fsp->np_compcount; comp++)
+ if (fsp->np_components[comp])
+ FREE(fsp->np_components[comp], M_TEMP);
+ FREE(fsp->np_components, M_TEMP);
+ }
+ FREE(fsl, M_TEMP);
+ }
+ FREE(nfslsp->nl_locations, M_TEMP);
+ nfslsp->nl_numlocs = 0;
+ nfslsp->nl_locations = NULL;
+}
+
+void
+nfs_mount_rele(struct nfsmount *nmp)
+{
+ int wup = 0;
+
+ lck_mtx_lock(&nmp->nm_lock);
+ if (nmp->nm_ref < 1)
+ panic("nfs zombie mount underflow\n");
+ nmp->nm_ref--;
+ if (nmp->nm_ref == 0)
+ wup = nmp->nm_state & NFSSTA_MOUNT_DRAIN;
+ lck_mtx_unlock(&nmp->nm_lock);
+ if (wup)
+ wakeup(&nmp->nm_ref);
+}
+
+void
+nfs_mount_drain_and_cleanup(struct nfsmount *nmp)
+{
+ lck_mtx_lock(&nmp->nm_lock);
+ nmp->nm_state |= NFSSTA_MOUNT_DRAIN;
+ while (nmp->nm_ref > 0) {
+ msleep(&nmp->nm_ref, &nmp->nm_lock, PZERO-1, "nfs_mount_drain", NULL);
+ }
+ assert(nmp->nm_ref == 0);
+ lck_mtx_unlock(&nmp->nm_lock);
+ nfs_mount_cleanup(nmp);
+}
+
+/*
+ * nfs_mount_zombie
+ */
+void
+nfs_mount_zombie(struct nfsmount *nmp, int nm_state_flags)
+{
+ struct nfsreq *req, *treq;
+ struct nfs_reqqhead iodq, resendq;
+ struct timespec ts = { 1, 0 };
+ struct nfs_open_owner *noop, *nextnoop;
+ nfsnode_t np;
+ int docallback;
+
+ lck_mtx_lock(&nmp->nm_lock);
+ nmp->nm_state |= nm_state_flags;
+ nmp->nm_ref++;
+ lck_mtx_unlock(&nmp->nm_lock);
+
+ /* stop callbacks */
+ if ((nmp->nm_vers >= NFS_VER4) && !NMFLAG(nmp, NOCALLBACK) && nmp->nm_cbid)
+ nfs4_mount_callback_shutdown(nmp);
+
+ /* Destroy any RPCSEC_GSS contexts */
+ nfs_gss_clnt_ctx_unmount(nmp);
+
+ /* mark the socket for termination */
+ lck_mtx_lock(&nmp->nm_lock);
+ nmp->nm_sockflags |= NMSOCK_UNMOUNT;
+
+ /* Have the socket thread send the unmount RPC, if requested/appropriate. */
+ if ((nmp->nm_vers < NFS_VER4) && (nmp->nm_state & NFSSTA_MOUNTED) &&
+ !(nmp->nm_state & (NFSSTA_FORCE|NFSSTA_DEAD)) && NMFLAG(nmp, CALLUMNT))
+ nfs_mount_sock_thread_wake(nmp);
+
+ /* wait for the socket thread to terminate */
+ while (nmp->nm_sockthd && current_thread() != nmp->nm_sockthd) {
+ wakeup(&nmp->nm_sockthd);
+ msleep(&nmp->nm_sockthd, &nmp->nm_lock, PZERO-1, "nfswaitsockthd", &ts);
+ }
+ lck_mtx_unlock(&nmp->nm_lock);
+
+ /* tear down the socket */
+ nfs_disconnect(nmp);
+
+ lck_mtx_lock(&nmp->nm_lock);
+
+ if ((nmp->nm_vers >= NFS_VER4) && !NMFLAG(nmp, NOCALLBACK) && nmp->nm_cbid) {
+ /* clear out any pending delegation return requests */
+ while ((np = TAILQ_FIRST(&nmp->nm_dreturnq))) {
+ TAILQ_REMOVE(&nmp->nm_dreturnq, np, n_dreturn);
+ np->n_dreturn.tqe_next = NFSNOLIST;
+ }
+ }
+
+ /* cancel any renew timer */
+ if ((nmp->nm_vers >= NFS_VER4) && nmp->nm_renew_timer) {
+ thread_call_cancel(nmp->nm_renew_timer);
+ thread_call_free(nmp->nm_renew_timer);
+ }
+
+ lck_mtx_unlock(&nmp->nm_lock);
+
+ if (nmp->nm_state & NFSSTA_MOUNTED)
+ switch (nmp->nm_lockmode) {
+ case NFS_LOCK_MODE_DISABLED:
+ case NFS_LOCK_MODE_LOCAL:
+ break;
+ case NFS_LOCK_MODE_ENABLED:
+ default:
+ if (nmp->nm_vers <= NFS_VER3) {
+ nfs_lockd_mount_unregister(nmp);
+ nmp->nm_lockmode = NFS_LOCK_MODE_DISABLED;
+ }
+ break;
+ }
+
+ if ((nmp->nm_vers >= NFS_VER4) && nmp->nm_longid) {
+ /* remove/deallocate the client ID data */
+ lck_mtx_lock(nfs_global_mutex);
+ TAILQ_REMOVE(&nfsclientids, nmp->nm_longid, nci_link);
+ if (nmp->nm_longid->nci_id)
+ FREE(nmp->nm_longid->nci_id, M_TEMP);
+ FREE(nmp->nm_longid, M_TEMP);
+ lck_mtx_unlock(nfs_global_mutex);
+ }
+
+ /*
+ * Be sure all requests for this mount are completed
+ * and removed from the resend queue.
+ */
+ TAILQ_INIT(&resendq);
+ lck_mtx_lock(nfs_request_mutex);
+ TAILQ_FOREACH(req, &nfs_reqq, r_chain) {
+ if (req->r_nmp == nmp) {
+ lck_mtx_lock(&req->r_mtx);
+ if (!req->r_error && req->r_nmrep.nmc_mhead == NULL)
+ req->r_error = EIO;
+ if (req->r_flags & R_RESENDQ) {
+ lck_mtx_lock(&nmp->nm_lock);
+ req->r_flags &= ~R_RESENDQ;
+ if (req->r_rchain.tqe_next != NFSREQNOLIST) {
+ TAILQ_REMOVE(&nmp->nm_resendq, req, r_rchain);
+ /*
+ * Queue up the request so that we can unreference them
+ * with out holding nfs_request_mutex
+ */
+ TAILQ_INSERT_TAIL(&resendq, req, r_rchain);
+ }
+ lck_mtx_unlock(&nmp->nm_lock);
+ }
+ wakeup(req);
+ lck_mtx_unlock(&req->r_mtx);
+ }
+ }
+ lck_mtx_unlock(nfs_request_mutex);
+
+ /* Since we've drop the request mutex we can now safely unreference the request */
+ TAILQ_FOREACH_SAFE(req, &resendq, r_rchain, treq) {
+ TAILQ_REMOVE(&resendq, req, r_rchain);
+ nfs_request_rele(req);
+ }
+
+ /*
+ * Now handle and outstanding async requests. We need to walk the
+ * request queue again this time with the nfsiod_mutex held. No
+ * other iods can grab our requests until we've put them on our own
+ * local iod queue for processing.
+ */
+ TAILQ_INIT(&iodq);
+ lck_mtx_lock(nfs_request_mutex);
+ lck_mtx_lock(nfsiod_mutex);
+ TAILQ_FOREACH(req, &nfs_reqq, r_chain) {
+ if (req->r_nmp == nmp) {
+ lck_mtx_lock(&req->r_mtx);
+ if (req->r_callback.rcb_func
+ && !(req->r_flags & R_WAITSENT) && !(req->r_flags & R_IOD)) {
+ /*
+ * Since R_IOD is not set then we need to handle it. If
+ * we're not on a list add it to our iod queue. Otherwise
+ * we must already be on nm_iodq which is added to our
+ * local queue below.
+ * %%% We should really keep a back pointer to our iod queue
+ * that we're on.
+ */
+ req->r_flags |= R_IOD;
+ if (req->r_achain.tqe_next == NFSREQNOLIST) {
+ TAILQ_INSERT_TAIL(&iodq, req, r_achain);
+ }
+ }
+ lck_mtx_unlock(&req->r_mtx);
+ }
+ }
+
+ /* finish any async I/O RPCs queued up */
+ if (nmp->nm_iodlink.tqe_next != NFSNOLIST)
+ TAILQ_REMOVE(&nfsiodmounts, nmp, nm_iodlink);
+ TAILQ_CONCAT(&iodq, &nmp->nm_iodq, r_achain);
+ lck_mtx_unlock(nfsiod_mutex);
+ lck_mtx_unlock(nfs_request_mutex);
+
+ TAILQ_FOREACH_SAFE(req, &iodq, r_achain, treq) {
+ TAILQ_REMOVE(&iodq, req, r_achain);
+ req->r_achain.tqe_next = NFSREQNOLIST;
+ lck_mtx_lock(&req->r_mtx);
+ docallback = !(req->r_flags & R_WAITSENT);
+ lck_mtx_unlock(&req->r_mtx);
+ if (docallback)
+ req->r_callback.rcb_func(req);
+ }
+
+ /* clean up common state */
+ lck_mtx_lock(&nmp->nm_lock);
+ while ((np = LIST_FIRST(&nmp->nm_monlist))) {
+ LIST_REMOVE(np, n_monlink);
+ np->n_monlink.le_next = NFSNOLIST;
+ }
+ TAILQ_FOREACH_SAFE(noop, &nmp->nm_open_owners, noo_link, nextnoop) {
+ TAILQ_REMOVE(&nmp->nm_open_owners, noop, noo_link);
+ noop->noo_flags &= ~NFS_OPEN_OWNER_LINK;
+ if (noop->noo_refcnt)
+ continue;
+ nfs_open_owner_destroy(noop);
+ }
+ lck_mtx_unlock(&nmp->nm_lock);
+
+ /* clean up NFSv4 state */
+ if (nmp->nm_vers >= NFS_VER4) {
+ lck_mtx_lock(&nmp->nm_lock);
+ while ((np = TAILQ_FIRST(&nmp->nm_delegations))) {
+ TAILQ_REMOVE(&nmp->nm_delegations, np, n_dlink);
+ np->n_dlink.tqe_next = NFSNOLIST;
+ }
+ lck_mtx_unlock(&nmp->nm_lock);
+ }
+
+ nfs_mount_rele(nmp);
+}
+
+/*
+ * cleanup/destroy an nfsmount
+ */
+void
+nfs_mount_cleanup(struct nfsmount *nmp)
+{
+ if (!nmp)
+ return;
+
+ nfs_mount_zombie(nmp, 0);
+
+ NFS_VFS_DBG("Unmounting %s from %s\n",
+ vfs_statfs(nmp->nm_mountp)->f_mntfromname,
+ vfs_statfs(nmp->nm_mountp)->f_mntonname);
+ NFS_VFS_DBG("nfs state = 0x%8.8x\n", nmp->nm_state);
+ NFS_VFS_DBG("nfs socket flags = 0x%8.8x\n", nmp->nm_sockflags);
+ NFS_VFS_DBG("nfs mount ref count is %d\n", nmp->nm_ref);
+ NFS_VFS_DBG("mount ref count is %d\n", nmp->nm_mountp->mnt_count);
+
+ if (nmp->nm_mountp)
+ vfs_setfsprivate(nmp->nm_mountp, NULL);
+
+ lck_mtx_lock(&nmp->nm_lock);
+ if (nmp->nm_ref)
+ panic("Some one has grabbed a ref %d state flags = 0x%8.8x\n", nmp->nm_ref, nmp->nm_state);
+
+ if (nmp->nm_saddr)
+ FREE(nmp->nm_saddr, M_SONAME);
+ if ((nmp->nm_vers < NFS_VER4) && nmp->nm_rqsaddr)
+ FREE(nmp->nm_rqsaddr, M_SONAME);
+
+ if (IS_VALID_CRED(nmp->nm_mcred))
+ kauth_cred_unref(&nmp->nm_mcred);
+
+ nfs_fs_locations_cleanup(&nmp->nm_locations);
+
+ if (nmp->nm_realm)
+ FREE(nmp->nm_realm, M_TEMP);
+ if (nmp->nm_principal)
+ FREE(nmp->nm_principal, M_TEMP);
+ if (nmp->nm_sprinc)
+ FREE(nmp->nm_sprinc, M_TEMP);
+
+ if (nmp->nm_args)
+ xb_free(nmp->nm_args);
+
+ lck_mtx_unlock(&nmp->nm_lock);
+
+ lck_mtx_destroy(&nmp->nm_lock, nfs_mount_grp);
+ if (nmp->nm_fh)
+ FREE(nmp->nm_fh, M_TEMP);
+ FREE_ZONE((caddr_t)nmp, sizeof (struct nfsmount), M_NFSMNT);
+}
+
+/*
+ * Return root of a filesystem
+ */
+int
+nfs_vfs_root(mount_t mp, vnode_t *vpp, __unused vfs_context_t ctx)
+{
+ vnode_t vp;
+ struct nfsmount *nmp;
+ int error;
+ u_int32_t vpid;
+
+ nmp = VFSTONFS(mp);
+ if (!nmp || !nmp->nm_dnp)
+ return (ENXIO);
+ vp = NFSTOV(nmp->nm_dnp);
+ vpid = vnode_vid(vp);
+ while ((error = vnode_getwithvid(vp, vpid))) {
+ /* vnode_get() may return ENOENT if the dir changes. */
+ /* If that happens, just try it again, else return the error. */
+ if ((error != ENOENT) || (vnode_vid(vp) == vpid))
+ return (error);
+ vpid = vnode_vid(vp);
+ }
+ *vpp = vp;
+ return (0);
+}
+
+/*
+ * Do operations associated with quotas
+ */
+#if !QUOTA
+int
+nfs_vfs_quotactl(
+ __unused mount_t mp,
+ __unused int cmds,
+ __unused uid_t uid,
+ __unused caddr_t datap,
+ __unused vfs_context_t context)
+{
+ return (ENOTSUP);
+}
+#else
+
+static int
+nfs_sa_getport(struct sockaddr *sa, int *error)
+{
+ int port = 0;
+
+ if (sa->sa_family == AF_INET6)
+ port = ntohs(((struct sockaddr_in6*)sa)->sin6_port);
+ else if (sa->sa_family == AF_INET)
+ port = ntohs(((struct sockaddr_in*)sa)->sin_port);
+ else if (error)
+ *error = EIO;
+
+ return port;
+}
+
+static void
+nfs_sa_setport(struct sockaddr *sa, int port)
+{
+ if (sa->sa_family == AF_INET6)
+ ((struct sockaddr_in6*)sa)->sin6_port = htons(port);
+ else if (sa->sa_family == AF_INET)
+ ((struct sockaddr_in*)sa)->sin_port = htons(port);
+}
+
+int
+nfs3_getquota(struct nfsmount *nmp, vfs_context_t ctx, uid_t id, int type, struct dqblk *dqb)
+{
+ int error = 0, slen, timeo;
+ int rqport = 0, rqproto, rqvers = (type == GRPQUOTA) ? RPCRQUOTA_EXT_VER : RPCRQUOTA_VER;
+ 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 val = 0, bsize = 0;
+ struct sockaddr *rqsaddr;
+ struct timeval now;
+ struct timespec ts = { 1, 0 };
+
+ if (!nmp->nm_saddr)
+ return (ENXIO);
+
+ if (NMFLAG(nmp, NOQUOTA))
+ return (ENOTSUP);
+
+ /*
+ * Allocate an address for rquotad if needed
+ */
+ if (!nmp->nm_rqsaddr) {
+ int need_free = 0;
+
+ MALLOC(rqsaddr, struct sockaddr *, sizeof(struct sockaddr_storage), M_SONAME, M_WAITOK|M_ZERO);
+ bcopy(nmp->nm_saddr, rqsaddr, min(sizeof(struct sockaddr_storage), nmp->nm_saddr->sa_len));
+ /* Set the port to zero, will call rpcbind to get the port below */
+ nfs_sa_setport(rqsaddr, 0);
+ microuptime(&now);
+
+ lck_mtx_lock(&nmp->nm_lock);
+ if (!nmp->nm_rqsaddr) {
+ nmp->nm_rqsaddr = rqsaddr;
+ nmp->nm_rqsaddrstamp = now.tv_sec;
+ } else {
+ need_free = 1;
+ }
+ lck_mtx_unlock(&nmp->nm_lock);
+ if (need_free)
+ FREE(rqsaddr, M_SONAME);
+ }
+
+ timeo = NMFLAG(nmp, SOFT) ? 10 : 60;
+ rqproto = IPPROTO_UDP; /* XXX should prefer TCP if mount is TCP */
+
+ /* check if we have a recently cached rquota port */
+ microuptime(&now);
+ lck_mtx_lock(&nmp->nm_lock);
+ rqsaddr = nmp->nm_rqsaddr;
+ rqport = nfs_sa_getport(rqsaddr, &error);
+ while (!error && (!rqport || ((nmp->nm_rqsaddrstamp + 60) <= (uint32_t)now.tv_sec))) {
+ error = nfs_sigintr(nmp, NULL, thd, 1);
+ if (error) {
+ lck_mtx_unlock(&nmp->nm_lock);
+ return (error);
+ }
+ if (nmp->nm_state & NFSSTA_RQUOTAINPROG) {
+ nmp->nm_state |= NFSSTA_WANTRQUOTA;
+ msleep(&nmp->nm_rqsaddr, &nmp->nm_lock, PZERO-1, "nfswaitrquotaaddr", &ts);
+ rqport = nfs_sa_getport(rqsaddr, &error);
+ continue;
+ }
+ nmp->nm_state |= NFSSTA_RQUOTAINPROG;
+ lck_mtx_unlock(&nmp->nm_lock);
+
+ /* send portmap request to get rquota port */
+ error = nfs_portmap_lookup(nmp, ctx, rqsaddr, NULL, RPCPROG_RQUOTA, rqvers, rqproto, timeo);
+ if (error)
+ goto out;
+ rqport = nfs_sa_getport(rqsaddr, &error);
+ if (error)
+ goto out;
+
+ if (!rqport) {
+ /*
+ * We overload PMAPPORT for the port if rquotad is not
+ * currently registered or up at the server. In the
+ * while loop above, port will be set and we will defer
+ * for a bit. Perhaps the service isn't online yet.
+ *
+ * Note that precludes using indirect, but we're not doing
+ * that here.
+ */
+ rqport = PMAPPORT;
+ nfs_sa_setport(rqsaddr, rqport);
+ }
+ microuptime(&now);
+ nmp->nm_rqsaddrstamp = now.tv_sec;
+ out:
+ lck_mtx_lock(&nmp->nm_lock);
+ nmp->nm_state &= ~NFSSTA_RQUOTAINPROG;
+ if (nmp->nm_state & NFSSTA_WANTRQUOTA) {
+ nmp->nm_state &= ~NFSSTA_WANTRQUOTA;
+ wakeup(&nmp->nm_rqsaddr);
+ }
+ }
+ lck_mtx_unlock(&nmp->nm_lock);
+ if (error)
+ return (error);