2 * SPDX-License-Identifier: BSD-2-Clause
4 * Copyright (c) 2008 Isilon Inc http://www.isilon.com/
5 * Authors: Doug Rabson <dfr@rabson.org>
6 * Developed with Red Inc: Alfred Perlstein <alfred@freebsd.org>
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 #include "opt_inet6.h"
32 #include <sys/cdefs.h>
33 #include <sys/param.h>
35 #include <sys/fcntl.h>
36 #include <sys/kernel.h>
37 #include <sys/kthread.h>
38 #include <sys/lockf.h>
39 #include <sys/malloc.h>
40 #include <sys/mount.h>
43 #include <sys/socket.h>
44 #include <sys/socketvar.h>
45 #include <sys/syscall.h>
46 #include <sys/sysctl.h>
47 #include <sys/sysent.h>
48 #include <sys/syslog.h>
49 #include <sys/sysproto.h>
50 #include <sys/systm.h>
51 #include <sys/taskqueue.h>
52 #include <sys/unistd.h>
53 #include <sys/vnode.h>
55 #include <nfs/nfsproto.h>
56 #include <nfs/nfs_lock.h>
58 #include <nlm/nlm_prot.h>
59 #include <nlm/sm_inter.h>
61 #include <rpc/rpc_com.h>
62 #include <rpc/rpcb_prot.h>
64 MALLOC_DEFINE(M_NLM, "NLM", "Network Lock Manager");
67 * If a host is inactive (and holds no locks) for this amount of
68 * seconds, we consider it idle and stop tracking it.
70 #define NLM_IDLE_TIMEOUT 30
73 * We check the host list for idle every few seconds.
75 #define NLM_IDLE_PERIOD 5
78 * We only look for GRANTED_RES messages for a little while.
80 #define NLM_EXPIRE_TIMEOUT 10
83 * Support for sysctl vfs.nlm.sysid
85 static SYSCTL_NODE(_vfs, OID_AUTO, nlm, CTLFLAG_RW | CTLFLAG_MPSAFE, NULL,
86 "Network Lock Manager");
87 static SYSCTL_NODE(_vfs_nlm, OID_AUTO, sysid,
88 CTLFLAG_RW | CTLFLAG_MPSAFE, NULL,
94 static struct syscall_helper_data nlm_syscalls[] = {
95 SYSCALL_INIT_HELPER(nlm_syscall),
100 * Debug level passed in from userland. We also support a sysctl hook
101 * so that it can be changed on a live system.
103 static int nlm_debug_level;
104 SYSCTL_INT(_debug, OID_AUTO, nlm_debug, CTLFLAG_RW, &nlm_debug_level, 0, "");
106 #define NLM_DEBUG(_level, args...) \
108 if (nlm_debug_level >= (_level)) \
109 log(LOG_DEBUG, args); \
111 #define NLM_ERR(args...) \
113 log(LOG_ERR, args); \
117 * Grace period handling. The value of nlm_grace_threshold is the
118 * value of time_uptime after which we are serving requests normally.
120 static time_t nlm_grace_threshold;
123 * We check for idle hosts if time_uptime is greater than
124 * nlm_next_idle_check,
126 static time_t nlm_next_idle_check;
129 * A flag to indicate the server is already running.
131 static int nlm_is_running;
134 * A socket to use for RPC - shared by all IPv4 RPC clients.
136 static struct socket *nlm_socket;
141 * A socket to use for RPC - shared by all IPv6 RPC clients.
143 static struct socket *nlm_socket6;
148 * An RPC client handle that can be used to communicate with the local
151 static CLIENT *nlm_nsm;
154 * An AUTH handle for the server's creds.
156 static AUTH *nlm_auth;
159 * A zero timeval for sending async RPC messages.
161 struct timeval nlm_zero_tv = { 0, 0 };
164 * The local NSM state number
169 * A lock to protect the host list and waiting lock list.
171 static struct mtx nlm_global_lock;
175 * (l) locked by nh_lock
176 * (s) only accessed via server RPC which is single threaded
177 * (g) locked by nlm_global_lock
178 * (c) const until freeing
179 * (a) modified using atomic ops
183 * A pending client-side lock request, stored on the nlm_waiting_locks
186 struct nlm_waiting_lock {
187 TAILQ_ENTRY(nlm_waiting_lock) nw_link; /* (g) */
188 bool_t nw_waiting; /* (g) */
189 nlm4_lock nw_lock; /* (c) */
190 union nfsfh nw_fh; /* (c) */
191 struct vnode *nw_vp; /* (c) */
193 TAILQ_HEAD(nlm_waiting_lock_list, nlm_waiting_lock);
195 struct nlm_waiting_lock_list nlm_waiting_locks; /* (g) */
198 * A pending server-side asynchronous lock request, stored on the
199 * nh_pending list of the NLM host.
201 struct nlm_async_lock {
202 TAILQ_ENTRY(nlm_async_lock) af_link; /* (l) host's list of locks */
203 struct task af_task; /* (c) async callback details */
204 void *af_cookie; /* (l) lock manager cancel token */
205 struct vnode *af_vp; /* (l) vnode to lock */
206 struct flock af_fl; /* (c) lock details */
207 struct nlm_host *af_host; /* (c) host which is locking */
208 CLIENT *af_rpc; /* (c) rpc client to send message */
209 nlm4_testargs af_granted; /* (c) notification details */
210 time_t af_expiretime; /* (c) notification time */
212 TAILQ_HEAD(nlm_async_lock_list, nlm_async_lock);
217 enum nlm_host_state {
225 CLIENT *nr_client; /* (l) RPC client handle */
226 time_t nr_create_time; /* (l) when client was created */
231 volatile u_int nh_refs; /* (a) reference count */
232 TAILQ_ENTRY(nlm_host) nh_link; /* (g) global list of hosts */
233 char nh_caller_name[MAXNAMELEN]; /* (c) printable name of host */
234 uint32_t nh_sysid; /* (c) our allocaed system ID */
235 char nh_sysid_string[10]; /* (c) string rep. of sysid */
236 struct sockaddr_storage nh_addr; /* (s) remote address of host */
237 struct nlm_rpc nh_srvrpc; /* (l) RPC for server replies */
238 struct nlm_rpc nh_clntrpc; /* (l) RPC for client requests */
239 rpcvers_t nh_vers; /* (s) NLM version of host */
240 int nh_state; /* (s) last seen NSM state of host */
241 enum nlm_host_state nh_monstate; /* (l) local NSM monitoring state */
242 time_t nh_idle_timeout; /* (s) Time at which host is idle */
243 struct sysctl_ctx_list nh_sysctl; /* (c) vfs.nlm.sysid nodes */
244 uint32_t nh_grantcookie; /* (l) grant cookie counter */
245 struct nlm_async_lock_list nh_pending; /* (l) pending async locks */
246 struct nlm_async_lock_list nh_granted; /* (l) granted locks */
247 struct nlm_async_lock_list nh_finished; /* (l) finished async locks */
249 TAILQ_HEAD(nlm_host_list, nlm_host);
251 static struct nlm_host_list nlm_hosts; /* (g) */
252 static uint32_t nlm_next_sysid = 1; /* (g) */
254 static void nlm_host_unmonitor(struct nlm_host *);
256 struct nlm_grantcookie {
261 static inline uint32_t
262 ng_sysid(struct netobj *src)
265 return ((struct nlm_grantcookie *)src->n_bytes)->ng_sysid;
268 static inline uint32_t
269 ng_cookie(struct netobj *src)
272 return ((struct nlm_grantcookie *)src->n_bytes)->ng_cookie;
275 /**********************************************************************/
278 * Initialise NLM globals.
285 mtx_init(&nlm_global_lock, "nlm_global_lock", NULL, MTX_DEF);
286 TAILQ_INIT(&nlm_waiting_locks);
287 TAILQ_INIT(&nlm_hosts);
289 error = syscall_helper_register(nlm_syscalls, SY_THR_STATIC_KLD);
291 NLM_ERR("Can't register NLM syscall\n");
299 syscall_helper_unregister(nlm_syscalls);
303 * Create a netobj from an arbitrary source.
306 nlm_make_netobj(struct netobj *dst, caddr_t src, size_t srcsize,
307 struct malloc_type *type)
310 dst->n_len = srcsize;
311 dst->n_bytes = malloc(srcsize, type, M_WAITOK);
312 memcpy(dst->n_bytes, src, srcsize);
316 * Copy a struct netobj.
319 nlm_copy_netobj(struct netobj *dst, struct netobj *src,
320 struct malloc_type *type)
323 nlm_make_netobj(dst, src->n_bytes, src->n_len, type);
327 * Create an RPC client handle for the given (address,prog,vers)
331 nlm_get_rpc(struct sockaddr *sa, rpcprog_t prog, rpcvers_t vers)
333 char *wchan = "nlmrcv";
334 struct sockaddr_storage ss;
340 enum clnt_stat stat = RPC_SUCCESS;
341 int rpcvers = RPCBVERS4;
342 bool_t do_tcp = FALSE;
343 bool_t tryagain = FALSE;
344 struct portmap mapping;
348 * First we need to contact the remote RPCBIND service to find
351 memcpy(&ss, sa, sa->sa_len);
352 switch (ss.ss_family) {
354 ((struct sockaddr_in *)&ss)->sin_port = htons(111);
359 ((struct sockaddr_in6 *)&ss)->sin6_port = htons(111);
366 * Unsupported address family - fail.
371 rpcb = clnt_dg_create(so, (struct sockaddr *)&ss,
372 RPCBPROG, rpcvers, 0, 0);
380 parms.r_netid = "tcp";
382 parms.r_netid = "udp";
387 * Use the default timeout.
396 * Try RPCBIND 4 then 3.
399 stat = CLNT_CALL(rpcb, (rpcprog_t) RPCBPROC_GETADDR,
400 (xdrproc_t) xdr_rpcb, &parms,
401 (xdrproc_t) xdr_wrapstring, &uaddr, timo);
402 if (stat == RPC_SUCCESS) {
404 * We have a reply from the remote RPCBIND - turn it
405 * into an appropriate address and make a new client
406 * that can talk to the remote NLM.
408 * XXX fixup IPv6 scope ID.
411 a = __rpc_uaddr2taddr_af(ss.ss_family, uaddr);
416 memcpy(&ss, a->buf, a->len);
419 xdr_free((xdrproc_t) xdr_wrapstring, &uaddr);
422 if (tryagain || stat == RPC_PROGVERSMISMATCH) {
423 if (rpcvers == RPCBVERS4)
425 else if (rpcvers == RPCBVERS)
427 CLNT_CONTROL(rpcb, CLSET_VERS, &rpcvers);
435 mapping.pm_prog = parms.r_prog;
436 mapping.pm_vers = parms.r_vers;
437 mapping.pm_prot = do_tcp ? IPPROTO_TCP : IPPROTO_UDP;
440 stat = CLNT_CALL(rpcb, (rpcprog_t) PMAPPROC_GETPORT,
441 (xdrproc_t) xdr_portmap, &mapping,
442 (xdrproc_t) xdr_u_short, &port, timo);
444 if (stat == RPC_SUCCESS) {
445 switch (ss.ss_family) {
447 ((struct sockaddr_in *)&ss)->sin_port =
453 ((struct sockaddr_in6 *)&ss)->sin6_port =
461 panic("invalid rpcvers %d", rpcvers);
464 * We may have a positive response from the portmapper, but the NLM
465 * service was not found. Make sure we received a valid port.
467 switch (ss.ss_family) {
469 port = ((struct sockaddr_in *)&ss)->sin_port;
473 port = ((struct sockaddr_in6 *)&ss)->sin6_port;
477 if (stat != RPC_SUCCESS || !port) {
479 * If we were able to talk to rpcbind or portmap, but the udp
480 * variant wasn't available, ask about tcp.
482 * XXX - We could also check for a TCP portmapper, but
483 * if the host is running a portmapper at all, we should be able
484 * to hail it over UDP.
486 if (stat == RPC_SUCCESS && !do_tcp) {
491 /* Otherwise, bad news. */
492 NLM_ERR("NLM: failed to contact remote rpcbind, "
493 "stat = %d, port = %d\n", (int) stat, port);
500 * Destroy the UDP client we used to speak to rpcbind and
501 * recreate as a TCP client.
503 struct netconfig *nconf = NULL;
507 switch (ss.ss_family) {
509 nconf = getnetconfigent("tcp");
513 nconf = getnetconfigent("tcp6");
518 rpcb = clnt_reconnect_create(nconf, (struct sockaddr *)&ss,
520 CLNT_CONTROL(rpcb, CLSET_WAITCHAN, wchan);
521 rpcb->cl_auth = nlm_auth;
525 * Re-use the client we used to speak to rpcbind.
527 CLNT_CONTROL(rpcb, CLSET_SVC_ADDR, &ss);
528 CLNT_CONTROL(rpcb, CLSET_PROG, &prog);
529 CLNT_CONTROL(rpcb, CLSET_VERS, &vers);
530 CLNT_CONTROL(rpcb, CLSET_WAITCHAN, wchan);
531 rpcb->cl_auth = nlm_auth;
538 * This async callback after when an async lock request has been
539 * granted. We notify the host which initiated the request.
542 nlm_lock_callback(void *arg, int pending)
544 struct nlm_async_lock *af = (struct nlm_async_lock *) arg;
545 struct rpc_callextra ext;
547 NLM_DEBUG(2, "NLM: async lock %p for %s (sysid %d) granted,"
548 " cookie %d:%d\n", af, af->af_host->nh_caller_name,
549 af->af_host->nh_sysid, ng_sysid(&af->af_granted.cookie),
550 ng_cookie(&af->af_granted.cookie));
553 * Send the results back to the host.
555 * Note: there is a possible race here with nlm_host_notify
556 * destroying the RPC client. To avoid problems, the first
557 * thing nlm_host_notify does is to cancel pending async lock
560 memset(&ext, 0, sizeof(ext));
561 ext.rc_auth = nlm_auth;
562 if (af->af_host->nh_vers == NLM_VERS4) {
563 nlm4_granted_msg_4(&af->af_granted,
564 NULL, af->af_rpc, &ext, nlm_zero_tv);
567 * Back-convert to legacy protocol
569 nlm_testargs granted;
570 granted.cookie = af->af_granted.cookie;
571 granted.exclusive = af->af_granted.exclusive;
572 granted.alock.caller_name =
573 af->af_granted.alock.caller_name;
574 granted.alock.fh = af->af_granted.alock.fh;
575 granted.alock.oh = af->af_granted.alock.oh;
576 granted.alock.svid = af->af_granted.alock.svid;
577 granted.alock.l_offset =
578 af->af_granted.alock.l_offset;
579 granted.alock.l_len =
580 af->af_granted.alock.l_len;
582 nlm_granted_msg_1(&granted,
583 NULL, af->af_rpc, &ext, nlm_zero_tv);
587 * Move this entry to the nh_granted list.
589 af->af_expiretime = time_uptime + NLM_EXPIRE_TIMEOUT;
590 mtx_lock(&af->af_host->nh_lock);
591 TAILQ_REMOVE(&af->af_host->nh_pending, af, af_link);
592 TAILQ_INSERT_TAIL(&af->af_host->nh_granted, af, af_link);
593 mtx_unlock(&af->af_host->nh_lock);
597 * Free an async lock request. The request must have been removed from
601 nlm_free_async_lock(struct nlm_async_lock *af)
604 * Free an async lock.
607 CLNT_RELEASE(af->af_rpc);
608 xdr_free((xdrproc_t) xdr_nlm4_testargs, &af->af_granted);
615 * Cancel our async request - this must be called with
616 * af->nh_host->nh_lock held. This is slightly complicated by a
617 * potential race with our own callback. If we fail to cancel the
618 * lock, it must already have been granted - we make sure our async
619 * task has completed by calling taskqueue_drain in this case.
622 nlm_cancel_async_lock(struct nlm_async_lock *af)
624 struct nlm_host *host = af->af_host;
627 mtx_assert(&host->nh_lock, MA_OWNED);
629 mtx_unlock(&host->nh_lock);
631 error = VOP_ADVLOCKASYNC(af->af_vp, NULL, F_CANCEL, &af->af_fl,
632 F_REMOTE, NULL, &af->af_cookie);
636 * We failed to cancel - make sure our callback has
637 * completed before we continue.
639 taskqueue_drain(taskqueue_thread, &af->af_task);
642 mtx_lock(&host->nh_lock);
645 NLM_DEBUG(2, "NLM: async lock %p for %s (sysid %d) "
646 "cancelled\n", af, host->nh_caller_name, host->nh_sysid);
649 * Remove from the nh_pending list and free now that
650 * we are safe from the callback.
652 TAILQ_REMOVE(&host->nh_pending, af, af_link);
653 mtx_unlock(&host->nh_lock);
654 nlm_free_async_lock(af);
655 mtx_lock(&host->nh_lock);
662 nlm_check_expired_locks(struct nlm_host *host)
664 struct nlm_async_lock *af;
665 time_t uptime = time_uptime;
667 mtx_lock(&host->nh_lock);
668 while ((af = TAILQ_FIRST(&host->nh_granted)) != NULL
669 && uptime >= af->af_expiretime) {
670 NLM_DEBUG(2, "NLM: async lock %p for %s (sysid %d) expired,"
671 " cookie %d:%d\n", af, af->af_host->nh_caller_name,
672 af->af_host->nh_sysid, ng_sysid(&af->af_granted.cookie),
673 ng_cookie(&af->af_granted.cookie));
674 TAILQ_REMOVE(&host->nh_granted, af, af_link);
675 mtx_unlock(&host->nh_lock);
676 nlm_free_async_lock(af);
677 mtx_lock(&host->nh_lock);
679 while ((af = TAILQ_FIRST(&host->nh_finished)) != NULL) {
680 TAILQ_REMOVE(&host->nh_finished, af, af_link);
681 mtx_unlock(&host->nh_lock);
682 nlm_free_async_lock(af);
683 mtx_lock(&host->nh_lock);
685 mtx_unlock(&host->nh_lock);
689 * Free resources used by a host. This is called after the reference
690 * count has reached zero so it doesn't need to worry about locks.
693 nlm_host_destroy(struct nlm_host *host)
696 mtx_lock(&nlm_global_lock);
697 TAILQ_REMOVE(&nlm_hosts, host, nh_link);
698 mtx_unlock(&nlm_global_lock);
700 if (host->nh_srvrpc.nr_client)
701 CLNT_RELEASE(host->nh_srvrpc.nr_client);
702 if (host->nh_clntrpc.nr_client)
703 CLNT_RELEASE(host->nh_clntrpc.nr_client);
704 mtx_destroy(&host->nh_lock);
705 sysctl_ctx_free(&host->nh_sysctl);
710 * Thread start callback for client lock recovery
713 nlm_client_recovery_start(void *arg)
715 struct nlm_host *host = (struct nlm_host *) arg;
717 NLM_DEBUG(1, "NLM: client lock recovery for %s started\n",
718 host->nh_caller_name);
720 nlm_client_recovery(host);
722 NLM_DEBUG(1, "NLM: client lock recovery for %s completed\n",
723 host->nh_caller_name);
725 host->nh_monstate = NLM_MONITORED;
726 nlm_host_release(host);
732 * This is called when we receive a host state change notification. We
733 * unlock any active locks owned by the host. When rpc.lockd is
734 * shutting down, this function is called with newstate set to zero
735 * which allows us to cancel any pending async locks and clear the
739 nlm_host_notify(struct nlm_host *host, int newstate)
741 struct nlm_async_lock *af;
744 NLM_DEBUG(1, "NLM: host %s (sysid %d) rebooted, new "
745 "state is %d\n", host->nh_caller_name,
746 host->nh_sysid, newstate);
750 * Cancel any pending async locks for this host.
752 mtx_lock(&host->nh_lock);
753 while ((af = TAILQ_FIRST(&host->nh_pending)) != NULL) {
755 * nlm_cancel_async_lock will remove the entry from
756 * nh_pending and free it.
758 nlm_cancel_async_lock(af);
760 mtx_unlock(&host->nh_lock);
761 nlm_check_expired_locks(host);
764 * The host just rebooted - trash its locks.
766 lf_clearremotesys(host->nh_sysid);
767 host->nh_state = newstate;
770 * If we have any remote locks for this host (i.e. it
771 * represents a remote NFS server that our local NFS client
772 * has locks for), start a recovery thread.
775 && host->nh_monstate != NLM_RECOVERING
776 && lf_countlocks(NLM_SYSID_CLIENT | host->nh_sysid) > 0) {
778 host->nh_monstate = NLM_RECOVERING;
779 refcount_acquire(&host->nh_refs);
780 kthread_add(nlm_client_recovery_start, host, curproc, &td, 0, 0,
781 "NFS lock recovery for %s", host->nh_caller_name);
786 * Sysctl handler to count the number of locks for a sysid.
789 nlm_host_lock_count_sysctl(SYSCTL_HANDLER_ARGS)
791 struct nlm_host *host;
794 host = oidp->oid_arg1;
795 count = lf_countlocks(host->nh_sysid);
796 return sysctl_handle_int(oidp, &count, 0, req);
800 * Sysctl handler to count the number of client locks for a sysid.
803 nlm_host_client_lock_count_sysctl(SYSCTL_HANDLER_ARGS)
805 struct nlm_host *host;
808 host = oidp->oid_arg1;
809 count = lf_countlocks(NLM_SYSID_CLIENT | host->nh_sysid);
810 return sysctl_handle_int(oidp, &count, 0, req);
814 * Create a new NLM host.
816 static struct nlm_host *
817 nlm_create_host(const char* caller_name)
819 struct nlm_host *host;
820 struct sysctl_oid *oid;
822 mtx_assert(&nlm_global_lock, MA_OWNED);
824 NLM_DEBUG(1, "NLM: new host %s (sysid %d)\n",
825 caller_name, nlm_next_sysid);
826 host = malloc(sizeof(struct nlm_host), M_NLM, M_NOWAIT|M_ZERO);
829 mtx_init(&host->nh_lock, "nh_lock", NULL, MTX_DEF);
830 refcount_init(&host->nh_refs, 1);
831 strlcpy(host->nh_caller_name, caller_name, MAXNAMELEN);
832 host->nh_sysid = nlm_next_sysid++;
833 snprintf(host->nh_sysid_string, sizeof(host->nh_sysid_string),
834 "%d", host->nh_sysid);
837 host->nh_monstate = NLM_UNMONITORED;
838 host->nh_grantcookie = 1;
839 TAILQ_INIT(&host->nh_pending);
840 TAILQ_INIT(&host->nh_granted);
841 TAILQ_INIT(&host->nh_finished);
842 TAILQ_INSERT_TAIL(&nlm_hosts, host, nh_link);
844 mtx_unlock(&nlm_global_lock);
846 sysctl_ctx_init(&host->nh_sysctl);
847 oid = SYSCTL_ADD_NODE(&host->nh_sysctl,
848 SYSCTL_STATIC_CHILDREN(_vfs_nlm_sysid),
849 OID_AUTO, host->nh_sysid_string, CTLFLAG_RD | CTLFLAG_MPSAFE,
851 SYSCTL_ADD_STRING(&host->nh_sysctl, SYSCTL_CHILDREN(oid), OID_AUTO,
852 "hostname", CTLFLAG_RD, host->nh_caller_name, 0, "");
853 SYSCTL_ADD_UINT(&host->nh_sysctl, SYSCTL_CHILDREN(oid), OID_AUTO,
854 "version", CTLFLAG_RD, &host->nh_vers, 0, "");
855 SYSCTL_ADD_UINT(&host->nh_sysctl, SYSCTL_CHILDREN(oid), OID_AUTO,
856 "monitored", CTLFLAG_RD, &host->nh_monstate, 0, "");
857 SYSCTL_ADD_PROC(&host->nh_sysctl, SYSCTL_CHILDREN(oid), OID_AUTO,
858 "lock_count", CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_MPSAFE, host,
859 0, nlm_host_lock_count_sysctl, "I", "");
860 SYSCTL_ADD_PROC(&host->nh_sysctl, SYSCTL_CHILDREN(oid), OID_AUTO,
861 "client_lock_count", CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_MPSAFE,
862 host, 0, nlm_host_client_lock_count_sysctl, "I", "");
864 mtx_lock(&nlm_global_lock);
870 * Acquire the next sysid for remote locks not handled by the NLM.
873 nlm_acquire_next_sysid(void)
877 mtx_lock(&nlm_global_lock);
878 next_sysid = nlm_next_sysid++;
879 mtx_unlock(&nlm_global_lock);
884 * Return non-zero if the address parts of the two sockaddrs are the
888 nlm_compare_addr(const struct sockaddr *a, const struct sockaddr *b)
890 const struct sockaddr_in *a4, *b4;
892 const struct sockaddr_in6 *a6, *b6;
895 if (a->sa_family != b->sa_family)
898 switch (a->sa_family) {
900 a4 = (const struct sockaddr_in *) a;
901 b4 = (const struct sockaddr_in *) b;
902 return !memcmp(&a4->sin_addr, &b4->sin_addr,
903 sizeof(a4->sin_addr));
906 a6 = (const struct sockaddr_in6 *) a;
907 b6 = (const struct sockaddr_in6 *) b;
908 return !memcmp(&a6->sin6_addr, &b6->sin6_addr,
909 sizeof(a6->sin6_addr));
917 * Check for idle hosts and stop monitoring them. We could also free
918 * the host structure here, possibly after a larger timeout but that
919 * would require some care to avoid races with
920 * e.g. nlm_host_lock_count_sysctl.
925 struct nlm_host *host;
927 mtx_assert(&nlm_global_lock, MA_OWNED);
929 if (time_uptime <= nlm_next_idle_check)
932 nlm_next_idle_check = time_uptime + NLM_IDLE_PERIOD;
934 TAILQ_FOREACH(host, &nlm_hosts, nh_link) {
935 if (host->nh_monstate == NLM_MONITORED
936 && time_uptime > host->nh_idle_timeout) {
937 mtx_unlock(&nlm_global_lock);
938 if (lf_countlocks(host->nh_sysid) > 0
939 || lf_countlocks(NLM_SYSID_CLIENT
941 host->nh_idle_timeout =
942 time_uptime + NLM_IDLE_TIMEOUT;
943 mtx_lock(&nlm_global_lock);
946 nlm_host_unmonitor(host);
947 mtx_lock(&nlm_global_lock);
953 * Search for an existing NLM host that matches the given name
954 * (typically the caller_name element of an nlm4_lock). If none is
955 * found, create a new host. If 'addr' is non-NULL, record the remote
956 * address of the host so that we can call it back for async
957 * responses. If 'vers' is greater than zero then record the NLM
958 * program version to use to communicate with this client.
961 nlm_find_host_by_name(const char *name, const struct sockaddr *addr,
964 struct nlm_host *host;
966 mtx_lock(&nlm_global_lock);
969 * The remote host is determined by caller_name.
971 TAILQ_FOREACH(host, &nlm_hosts, nh_link) {
972 if (!strcmp(host->nh_caller_name, name))
977 host = nlm_create_host(name);
979 mtx_unlock(&nlm_global_lock);
983 refcount_acquire(&host->nh_refs);
985 host->nh_idle_timeout = time_uptime + NLM_IDLE_TIMEOUT;
988 * If we have an address for the host, record it so that we
989 * can send async replies etc.
993 KASSERT(addr->sa_len < sizeof(struct sockaddr_storage),
994 ("Strange remote transport address length"));
997 * If we have seen an address before and we currently
998 * have an RPC client handle, make sure the address is
999 * the same, otherwise discard the client handle.
1001 if (host->nh_addr.ss_len && host->nh_srvrpc.nr_client) {
1002 if (!nlm_compare_addr(
1003 (struct sockaddr *) &host->nh_addr,
1005 || host->nh_vers != vers) {
1007 mtx_lock(&host->nh_lock);
1008 client = host->nh_srvrpc.nr_client;
1009 host->nh_srvrpc.nr_client = NULL;
1010 mtx_unlock(&host->nh_lock);
1012 CLNT_RELEASE(client);
1016 memcpy(&host->nh_addr, addr, addr->sa_len);
1017 host->nh_vers = vers;
1022 mtx_unlock(&nlm_global_lock);
1028 * Search for an existing NLM host that matches the given remote
1029 * address. If none is found, create a new host with the requested
1030 * address and remember 'vers' as the NLM protocol version to use for
1034 nlm_find_host_by_addr(const struct sockaddr *addr, int vers)
1037 * Fake up a name using inet_ntop. This buffer is
1038 * large enough for an IPv6 address.
1040 char tmp[sizeof "ffff:ffff:ffff:ffff:ffff:ffff:255.255.255.255"];
1041 struct nlm_host *host;
1043 switch (addr->sa_family) {
1046 &((const struct sockaddr_in *) addr)->sin_addr,
1052 &((const struct sockaddr_in6 *) addr)->sin6_addr,
1057 strlcpy(tmp, "<unknown>", sizeof(tmp));
1060 mtx_lock(&nlm_global_lock);
1063 * The remote host is determined by caller_name.
1065 TAILQ_FOREACH(host, &nlm_hosts, nh_link) {
1066 if (nlm_compare_addr(addr,
1067 (const struct sockaddr *) &host->nh_addr))
1072 host = nlm_create_host(tmp);
1074 mtx_unlock(&nlm_global_lock);
1077 memcpy(&host->nh_addr, addr, addr->sa_len);
1078 host->nh_vers = vers;
1080 refcount_acquire(&host->nh_refs);
1082 host->nh_idle_timeout = time_uptime + NLM_IDLE_TIMEOUT;
1086 mtx_unlock(&nlm_global_lock);
1092 * Find the NLM host that matches the value of 'sysid'. If none
1093 * exists, return NULL.
1095 static struct nlm_host *
1096 nlm_find_host_by_sysid(int sysid)
1098 struct nlm_host *host;
1100 TAILQ_FOREACH(host, &nlm_hosts, nh_link) {
1101 if (host->nh_sysid == sysid) {
1102 refcount_acquire(&host->nh_refs);
1110 void nlm_host_release(struct nlm_host *host)
1112 if (refcount_release(&host->nh_refs)) {
1116 nlm_host_destroy(host);
1121 * Unregister this NLM host with the local NSM due to idleness.
1124 nlm_host_unmonitor(struct nlm_host *host)
1128 struct timeval timo;
1129 enum clnt_stat stat;
1131 NLM_DEBUG(1, "NLM: unmonitoring %s (sysid %d)\n",
1132 host->nh_caller_name, host->nh_sysid);
1135 * We put our assigned system ID value in the priv field to
1136 * make it simpler to find the host if we are notified of a
1139 smmonid.mon_name = host->nh_caller_name;
1140 smmonid.my_id.my_name = "localhost";
1141 smmonid.my_id.my_prog = NLM_PROG;
1142 smmonid.my_id.my_vers = NLM_SM;
1143 smmonid.my_id.my_proc = NLM_SM_NOTIFY;
1147 stat = CLNT_CALL(nlm_nsm, SM_UNMON,
1148 (xdrproc_t) xdr_mon, &smmonid,
1149 (xdrproc_t) xdr_sm_stat, &smstat, timo);
1151 if (stat != RPC_SUCCESS) {
1152 NLM_ERR("Failed to contact local NSM - rpc error %d\n", stat);
1155 if (smstat.res_stat == stat_fail) {
1156 NLM_ERR("Local NSM refuses to unmonitor %s\n",
1157 host->nh_caller_name);
1161 host->nh_monstate = NLM_UNMONITORED;
1165 * Register this NLM host with the local NSM so that we can be
1166 * notified if it reboots.
1169 nlm_host_monitor(struct nlm_host *host, int state)
1173 struct timeval timo;
1174 enum clnt_stat stat;
1176 if (state && !host->nh_state) {
1178 * This is the first time we have seen an NSM state
1179 * value for this host. We record it here to help
1180 * detect host reboots.
1182 host->nh_state = state;
1183 NLM_DEBUG(1, "NLM: host %s (sysid %d) has NSM state %d\n",
1184 host->nh_caller_name, host->nh_sysid, state);
1187 mtx_lock(&host->nh_lock);
1188 if (host->nh_monstate != NLM_UNMONITORED) {
1189 mtx_unlock(&host->nh_lock);
1192 host->nh_monstate = NLM_MONITORED;
1193 mtx_unlock(&host->nh_lock);
1195 NLM_DEBUG(1, "NLM: monitoring %s (sysid %d)\n",
1196 host->nh_caller_name, host->nh_sysid);
1199 * We put our assigned system ID value in the priv field to
1200 * make it simpler to find the host if we are notified of a
1203 smmon.mon_id.mon_name = host->nh_caller_name;
1204 smmon.mon_id.my_id.my_name = "localhost";
1205 smmon.mon_id.my_id.my_prog = NLM_PROG;
1206 smmon.mon_id.my_id.my_vers = NLM_SM;
1207 smmon.mon_id.my_id.my_proc = NLM_SM_NOTIFY;
1208 memcpy(smmon.priv, &host->nh_sysid, sizeof(host->nh_sysid));
1212 stat = CLNT_CALL(nlm_nsm, SM_MON,
1213 (xdrproc_t) xdr_mon, &smmon,
1214 (xdrproc_t) xdr_sm_stat, &smstat, timo);
1216 if (stat != RPC_SUCCESS) {
1217 NLM_ERR("Failed to contact local NSM - rpc error %d\n", stat);
1220 if (smstat.res_stat == stat_fail) {
1221 NLM_ERR("Local NSM refuses to monitor %s\n",
1222 host->nh_caller_name);
1223 mtx_lock(&host->nh_lock);
1224 host->nh_monstate = NLM_MONITOR_FAILED;
1225 mtx_unlock(&host->nh_lock);
1229 host->nh_monstate = NLM_MONITORED;
1233 * Return an RPC client handle that can be used to talk to the NLM
1234 * running on the given host.
1237 nlm_host_get_rpc(struct nlm_host *host, bool_t isserver)
1239 struct nlm_rpc *rpc;
1242 mtx_lock(&host->nh_lock);
1245 rpc = &host->nh_srvrpc;
1247 rpc = &host->nh_clntrpc;
1250 * We can't hold onto RPC handles for too long - the async
1251 * call/reply protocol used by some NLM clients makes it hard
1252 * to tell when they change port numbers (e.g. after a
1253 * reboot). Note that if a client reboots while it isn't
1254 * holding any locks, it won't bother to notify us. We
1255 * expire the RPC handles after two minutes.
1257 if (rpc->nr_client && time_uptime > rpc->nr_create_time + 2*60) {
1258 client = rpc->nr_client;
1259 rpc->nr_client = NULL;
1260 mtx_unlock(&host->nh_lock);
1261 CLNT_RELEASE(client);
1262 mtx_lock(&host->nh_lock);
1265 if (!rpc->nr_client) {
1266 mtx_unlock(&host->nh_lock);
1267 client = nlm_get_rpc((struct sockaddr *)&host->nh_addr,
1268 NLM_PROG, host->nh_vers);
1269 mtx_lock(&host->nh_lock);
1272 if (rpc->nr_client) {
1273 mtx_unlock(&host->nh_lock);
1274 CLNT_DESTROY(client);
1275 mtx_lock(&host->nh_lock);
1277 rpc->nr_client = client;
1278 rpc->nr_create_time = time_uptime;
1283 client = rpc->nr_client;
1285 CLNT_ACQUIRE(client);
1286 mtx_unlock(&host->nh_lock);
1292 int nlm_host_get_sysid(struct nlm_host *host)
1295 return (host->nh_sysid);
1299 nlm_host_get_state(struct nlm_host *host)
1302 return (host->nh_state);
1306 nlm_register_wait_lock(struct nlm4_lock *lock, struct vnode *vp)
1308 struct nlm_waiting_lock *nw;
1310 nw = malloc(sizeof(struct nlm_waiting_lock), M_NLM, M_WAITOK);
1311 nw->nw_lock = *lock;
1312 memcpy(&nw->nw_fh.fh_bytes, nw->nw_lock.fh.n_bytes,
1313 nw->nw_lock.fh.n_len);
1314 nw->nw_lock.fh.n_bytes = nw->nw_fh.fh_bytes;
1315 nw->nw_waiting = TRUE;
1317 mtx_lock(&nlm_global_lock);
1318 TAILQ_INSERT_TAIL(&nlm_waiting_locks, nw, nw_link);
1319 mtx_unlock(&nlm_global_lock);
1325 nlm_deregister_wait_lock(void *handle)
1327 struct nlm_waiting_lock *nw = handle;
1329 mtx_lock(&nlm_global_lock);
1330 TAILQ_REMOVE(&nlm_waiting_locks, nw, nw_link);
1331 mtx_unlock(&nlm_global_lock);
1337 nlm_wait_lock(void *handle, int timo)
1339 struct nlm_waiting_lock *nw = handle;
1340 int error, stops_deferred;
1343 * If the granted message arrived before we got here,
1344 * nw->nw_waiting will be FALSE - in that case, don't sleep.
1346 mtx_lock(&nlm_global_lock);
1348 if (nw->nw_waiting) {
1349 stops_deferred = sigdeferstop(SIGDEFERSTOP_ERESTART);
1350 error = msleep(nw, &nlm_global_lock, PCATCH, "nlmlock", timo);
1351 sigallowstop(stops_deferred);
1353 TAILQ_REMOVE(&nlm_waiting_locks, nw, nw_link);
1356 * The granted message may arrive after the
1357 * interrupt/timeout but before we manage to lock the
1358 * mutex. Detect this by examining nw_lock.
1360 if (!nw->nw_waiting)
1364 * If nlm_cancel_wait is called, then error will be
1365 * zero but nw_waiting will still be TRUE. We
1366 * translate this into EINTR.
1371 mtx_unlock(&nlm_global_lock);
1379 nlm_cancel_wait(struct vnode *vp)
1381 struct nlm_waiting_lock *nw;
1383 mtx_lock(&nlm_global_lock);
1384 TAILQ_FOREACH(nw, &nlm_waiting_locks, nw_link) {
1385 if (nw->nw_vp == vp) {
1389 mtx_unlock(&nlm_global_lock);
1392 /**********************************************************************/
1395 * Syscall interface with userland.
1398 extern void nlm_prog_0(struct svc_req *rqstp, SVCXPRT *transp);
1399 extern void nlm_prog_1(struct svc_req *rqstp, SVCXPRT *transp);
1400 extern void nlm_prog_3(struct svc_req *rqstp, SVCXPRT *transp);
1401 extern void nlm_prog_4(struct svc_req *rqstp, SVCXPRT *transp);
1404 nlm_register_services(SVCPOOL *pool, int addr_count, char **addrs)
1406 static rpcvers_t versions[] = {
1407 NLM_SM, NLM_VERS, NLM_VERSX, NLM_VERS4
1409 static void (*dispatchers[])(struct svc_req *, SVCXPRT *) = {
1410 nlm_prog_0, nlm_prog_1, nlm_prog_3, nlm_prog_4
1416 struct netconfig *nconf;
1420 NLM_ERR("NLM: no service addresses given - can't start server");
1424 if (addr_count < 0 || addr_count > 256 ) {
1425 NLM_ERR("NLM: too many service addresses (%d) given, "
1426 "max 256 - can't start server\n", addr_count);
1430 xprts = malloc(addr_count * sizeof(SVCXPRT *), M_NLM, M_WAITOK|M_ZERO);
1431 for (i = 0; i < nitems(versions); i++) {
1432 for (j = 0; j < addr_count; j++) {
1434 * Create transports for the first version and
1435 * then just register everything else to the
1441 error = copyin(&addrs[2*j], &up,
1445 error = copyinstr(up, netid, sizeof(netid),
1449 error = copyin(&addrs[2*j+1], &up,
1453 error = copyinstr(up, uaddr, sizeof(uaddr),
1457 nconf = getnetconfigent(netid);
1459 NLM_ERR("Can't lookup netid %s\n",
1464 xprts[j] = svc_tp_create(pool, dispatchers[i],
1465 NLM_PROG, versions[i], uaddr, nconf);
1467 NLM_ERR("NLM: unable to create "
1468 "(NLM_PROG, %d).\n", versions[i]);
1472 freenetconfigent(nconf);
1474 nconf = getnetconfigent(xprts[j]->xp_netid);
1475 rpcb_unset(NLM_PROG, versions[i], nconf);
1476 if (!svc_reg(xprts[j], NLM_PROG, versions[i],
1477 dispatchers[i], nconf)) {
1478 NLM_ERR("NLM: can't register "
1479 "(NLM_PROG, %d)\n", versions[i]);
1488 for (j = 0; j < addr_count; j++) {
1490 SVC_RELEASE(xprts[j]);
1497 * Main server entry point. Contacts the local NSM to get its current
1498 * state and send SM_UNMON_ALL. Registers the NLM services and then
1499 * services requests. Does not return until the server is interrupted
1503 nlm_server_main(int addr_count, char **addrs)
1505 struct thread *td = curthread;
1507 SVCPOOL *pool = NULL;
1511 struct sockaddr_in6 sin6;
1513 struct sockaddr_in sin;
1516 struct timeval timo;
1517 enum clnt_stat stat;
1518 struct nlm_host *host, *nhost;
1519 struct nlm_waiting_lock *nw;
1520 vop_advlock_t *old_nfs_advlock;
1521 vop_reclaim_t *old_nfs_reclaim;
1523 if (nlm_is_running != 0) {
1524 NLM_ERR("NLM: can't start server - "
1525 "it appears to be running already\n");
1529 if (nlm_socket == NULL) {
1530 memset(&opt, 0, sizeof(opt));
1532 error = socreate(AF_INET, &nlm_socket, SOCK_DGRAM, 0,
1535 NLM_ERR("NLM: can't create IPv4 socket - error %d\n",
1539 opt.sopt_dir = SOPT_SET;
1540 opt.sopt_level = IPPROTO_IP;
1541 opt.sopt_name = IP_PORTRANGE;
1542 portlow = IP_PORTRANGE_LOW;
1543 opt.sopt_val = &portlow;
1544 opt.sopt_valsize = sizeof(portlow);
1545 sosetopt(nlm_socket, &opt);
1549 error = socreate(AF_INET6, &nlm_socket6, SOCK_DGRAM, 0,
1552 NLM_ERR("NLM: can't create IPv6 socket - error %d\n",
1554 soclose(nlm_socket);
1558 opt.sopt_dir = SOPT_SET;
1559 opt.sopt_level = IPPROTO_IPV6;
1560 opt.sopt_name = IPV6_PORTRANGE;
1561 portlow = IPV6_PORTRANGE_LOW;
1562 opt.sopt_val = &portlow;
1563 opt.sopt_valsize = sizeof(portlow);
1564 sosetopt(nlm_socket6, &opt);
1568 nlm_auth = authunix_create(curthread->td_ucred);
1571 memset(&sin6, 0, sizeof(sin6));
1572 sin6.sin6_len = sizeof(sin6);
1573 sin6.sin6_family = AF_INET6;
1574 sin6.sin6_addr = in6addr_loopback;
1575 nlm_nsm = nlm_get_rpc((struct sockaddr *) &sin6, SM_PROG, SM_VERS);
1578 memset(&sin, 0, sizeof(sin));
1579 sin.sin_len = sizeof(sin);
1580 sin.sin_family = AF_INET;
1581 sin.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
1582 nlm_nsm = nlm_get_rpc((struct sockaddr *) &sin, SM_PROG,
1589 NLM_ERR("Can't start NLM - unable to contact NSM\n");
1594 pool = svcpool_create("NLM", NULL);
1596 error = nlm_register_services(pool, addr_count, addrs);
1600 memset(&id, 0, sizeof(id));
1601 id.my_name = "NFS NLM";
1605 stat = CLNT_CALL(nlm_nsm, SM_UNMON_ALL,
1606 (xdrproc_t) xdr_my_id, &id,
1607 (xdrproc_t) xdr_sm_stat, &smstat, timo);
1609 if (stat != RPC_SUCCESS) {
1612 CLNT_GETERR(nlm_nsm, &err);
1613 NLM_ERR("NLM: unexpected error contacting NSM, "
1614 "stat=%d, errno=%d\n", stat, err.re_errno);
1620 NLM_DEBUG(1, "NLM: local NSM state is %d\n", smstat.state);
1621 nlm_nsm_state = smstat.state;
1623 old_nfs_advlock = nfs_advlock_p;
1624 nfs_advlock_p = nlm_advlock;
1625 old_nfs_reclaim = nfs_reclaim_p;
1626 nfs_reclaim_p = nlm_reclaim;
1631 nfs_advlock_p = old_nfs_advlock;
1632 nfs_reclaim_p = old_nfs_reclaim;
1637 svcpool_destroy(pool);
1640 * We are finished communicating with the NSM.
1643 CLNT_RELEASE(nlm_nsm);
1648 * Trash all the existing state so that if the server
1649 * restarts, it gets a clean slate. This is complicated by the
1650 * possibility that there may be other threads trying to make
1651 * client locking requests.
1653 * First we fake a client reboot notification which will
1654 * cancel any pending async locks and purge remote lock state
1655 * from the local lock manager. We release the reference from
1656 * nlm_hosts to the host (which may remove it from the list
1657 * and free it). After this phase, the only entries in the
1658 * nlm_host list should be from other threads performing
1659 * client lock requests.
1661 mtx_lock(&nlm_global_lock);
1662 TAILQ_FOREACH(nw, &nlm_waiting_locks, nw_link) {
1665 TAILQ_FOREACH_SAFE(host, &nlm_hosts, nh_link, nhost) {
1666 mtx_unlock(&nlm_global_lock);
1667 nlm_host_notify(host, 0);
1668 nlm_host_release(host);
1669 mtx_lock(&nlm_global_lock);
1671 mtx_unlock(&nlm_global_lock);
1673 AUTH_DESTROY(nlm_auth);
1679 sys_nlm_syscall(struct thread *td, struct nlm_syscall_args *uap)
1683 error = priv_check(td, PRIV_NFS_LOCKD);
1687 nlm_debug_level = uap->debug_level;
1688 nlm_grace_threshold = time_uptime + uap->grace_period;
1689 nlm_next_idle_check = time_uptime + NLM_IDLE_PERIOD;
1691 return nlm_server_main(uap->addr_count, uap->addrs);
1694 /**********************************************************************/
1697 * NLM implementation details, called from the RPC stubs.
1701 nlm_sm_notify(struct nlm_sm_status *argp)
1704 struct nlm_host *host;
1706 NLM_DEBUG(3, "nlm_sm_notify(): mon_name = %s\n", argp->mon_name);
1707 memcpy(&sysid, &argp->priv, sizeof(sysid));
1708 host = nlm_find_host_by_sysid(sysid);
1710 nlm_host_notify(host, argp->state);
1711 nlm_host_release(host);
1716 nlm_convert_to_fhandle_t(fhandle_t *fhp, struct netobj *p)
1718 memcpy(fhp, p->n_bytes, sizeof(fhandle_t));
1722 struct mount *vs_mp;
1723 struct vnode *vs_vp;
1728 nlm_get_vfs_state(struct nlm_host *host, struct svc_req *rqstp,
1729 fhandle_t *fhp, struct vfs_state *vs, accmode_t accmode)
1733 struct ucred *cred = NULL, *credanon = NULL;
1735 memset(vs, 0, sizeof(*vs));
1737 vs->vs_mp = vfs_getvfs(&fhp->fh_fsid);
1742 /* accmode == 0 means don't check, since it is an unlock. */
1744 error = VFS_CHECKEXP(vs->vs_mp,
1745 (struct sockaddr *)&host->nh_addr, &exflags, &credanon,
1750 if (exflags & MNT_EXRDONLY ||
1751 (vs->vs_mp->mnt_flag & MNT_RDONLY)) {
1757 error = VFS_FHTOVP(vs->vs_mp, &fhp->fh_fid, LK_EXCLUSIVE, &vs->vs_vp);
1760 vs->vs_vnlocked = TRUE;
1763 if (!svc_getcred(rqstp, &cred, NULL)) {
1767 if (cred->cr_uid == 0 || (exflags & MNT_EXPORTANON)) {
1776 error = VOP_ACCESS(vs->vs_vp, accmode, cred, curthread);
1778 * If this failed and accmode != VWRITE, try again with
1779 * VWRITE to maintain backwards compatibility with the
1780 * old code that always used VWRITE.
1782 if (error != 0 && accmode != VWRITE)
1783 error = VOP_ACCESS(vs->vs_vp, VWRITE, cred, curthread);
1788 VOP_UNLOCK(vs->vs_vp);
1789 vs->vs_vnlocked = FALSE;
1801 nlm_release_vfs_state(struct vfs_state *vs)
1805 if (vs->vs_vnlocked)
1815 nlm_convert_error(int error)
1818 if (error == ESTALE)
1819 return nlm4_stale_fh;
1820 else if (error == EROFS)
1827 nlm_do_test(nlm4_testargs *argp, nlm4_testres *result, struct svc_req *rqstp,
1831 struct vfs_state vs;
1832 struct nlm_host *host, *bhost;
1837 memset(result, 0, sizeof(*result));
1838 memset(&vs, 0, sizeof(vs));
1840 host = nlm_find_host_by_name(argp->alock.caller_name,
1841 svc_getrpccaller(rqstp), rqstp->rq_vers);
1843 result->stat.stat = nlm4_denied_nolocks;
1847 NLM_DEBUG(3, "nlm_do_test(): caller_name = %s (sysid = %d)\n",
1848 host->nh_caller_name, host->nh_sysid);
1850 nlm_check_expired_locks(host);
1851 sysid = host->nh_sysid;
1853 nlm_convert_to_fhandle_t(&fh, &argp->alock.fh);
1854 nlm_copy_netobj(&result->cookie, &argp->cookie, M_RPC);
1856 if (time_uptime < nlm_grace_threshold) {
1857 result->stat.stat = nlm4_denied_grace_period;
1861 accmode = argp->exclusive ? VWRITE : VREAD;
1862 error = nlm_get_vfs_state(host, rqstp, &fh, &vs, accmode);
1864 result->stat.stat = nlm_convert_error(error);
1868 fl.l_start = argp->alock.l_offset;
1869 fl.l_len = argp->alock.l_len;
1870 fl.l_pid = argp->alock.svid;
1872 fl.l_whence = SEEK_SET;
1873 if (argp->exclusive)
1874 fl.l_type = F_WRLCK;
1876 fl.l_type = F_RDLCK;
1877 error = VOP_ADVLOCK(vs.vs_vp, NULL, F_GETLK, &fl, F_REMOTE);
1879 result->stat.stat = nlm4_failed;
1883 if (fl.l_type == F_UNLCK) {
1884 result->stat.stat = nlm4_granted;
1886 result->stat.stat = nlm4_denied;
1887 result->stat.nlm4_testrply_u.holder.exclusive =
1888 (fl.l_type == F_WRLCK);
1889 result->stat.nlm4_testrply_u.holder.svid = fl.l_pid;
1890 bhost = nlm_find_host_by_sysid(fl.l_sysid);
1893 * We don't have any useful way of recording
1894 * the value of oh used in the original lock
1895 * request. Ideally, the test reply would have
1896 * a space for the owning host's name allowing
1897 * our caller's NLM to keep track.
1899 * As far as I can see, Solaris uses an eight
1900 * byte structure for oh which contains a four
1901 * byte pid encoded in local byte order and
1902 * the first four bytes of the host
1903 * name. Linux uses a variable length string
1904 * 'pid@hostname' in ascii but doesn't even
1905 * return that in test replies.
1907 * For the moment, return nothing in oh
1908 * (already zero'ed above).
1910 nlm_host_release(bhost);
1912 result->stat.nlm4_testrply_u.holder.l_offset = fl.l_start;
1913 result->stat.nlm4_testrply_u.holder.l_len = fl.l_len;
1917 nlm_release_vfs_state(&vs);
1919 *rpcp = nlm_host_get_rpc(host, TRUE);
1920 nlm_host_release(host);
1925 nlm_do_lock(nlm4_lockargs *argp, nlm4_res *result, struct svc_req *rqstp,
1926 bool_t monitor, CLIENT **rpcp)
1929 struct vfs_state vs;
1930 struct nlm_host *host;
1935 memset(result, 0, sizeof(*result));
1936 memset(&vs, 0, sizeof(vs));
1938 host = nlm_find_host_by_name(argp->alock.caller_name,
1939 svc_getrpccaller(rqstp), rqstp->rq_vers);
1941 result->stat.stat = nlm4_denied_nolocks;
1945 NLM_DEBUG(3, "nlm_do_lock(): caller_name = %s (sysid = %d)\n",
1946 host->nh_caller_name, host->nh_sysid);
1948 if (monitor && host->nh_state && argp->state
1949 && host->nh_state != argp->state) {
1951 * The host rebooted without telling us. Trash its
1954 nlm_host_notify(host, argp->state);
1957 nlm_check_expired_locks(host);
1958 sysid = host->nh_sysid;
1960 nlm_convert_to_fhandle_t(&fh, &argp->alock.fh);
1961 nlm_copy_netobj(&result->cookie, &argp->cookie, M_RPC);
1963 if (time_uptime < nlm_grace_threshold && !argp->reclaim) {
1964 result->stat.stat = nlm4_denied_grace_period;
1968 accmode = argp->exclusive ? VWRITE : VREAD;
1969 error = nlm_get_vfs_state(host, rqstp, &fh, &vs, accmode);
1971 result->stat.stat = nlm_convert_error(error);
1975 fl.l_start = argp->alock.l_offset;
1976 fl.l_len = argp->alock.l_len;
1977 fl.l_pid = argp->alock.svid;
1979 fl.l_whence = SEEK_SET;
1980 if (argp->exclusive)
1981 fl.l_type = F_WRLCK;
1983 fl.l_type = F_RDLCK;
1985 struct nlm_async_lock *af;
1987 struct nlm_grantcookie cookie;
1990 * First, make sure we can contact the host's NLM.
1992 client = nlm_host_get_rpc(host, TRUE);
1994 result->stat.stat = nlm4_failed;
1999 * First we need to check and see if there is an
2000 * existing blocked lock that matches. This could be a
2001 * badly behaved client or an RPC re-send. If we find
2002 * one, just return nlm4_blocked.
2004 mtx_lock(&host->nh_lock);
2005 TAILQ_FOREACH(af, &host->nh_pending, af_link) {
2006 if (af->af_fl.l_start == fl.l_start
2007 && af->af_fl.l_len == fl.l_len
2008 && af->af_fl.l_pid == fl.l_pid
2009 && af->af_fl.l_type == fl.l_type) {
2014 cookie.ng_sysid = host->nh_sysid;
2015 cookie.ng_cookie = host->nh_grantcookie++;
2017 mtx_unlock(&host->nh_lock);
2019 CLNT_RELEASE(client);
2020 result->stat.stat = nlm4_blocked;
2024 af = malloc(sizeof(struct nlm_async_lock), M_NLM,
2026 TASK_INIT(&af->af_task, 0, nlm_lock_callback, af);
2027 af->af_vp = vs.vs_vp;
2030 af->af_rpc = client;
2032 * We use M_RPC here so that we can xdr_free the thing
2035 nlm_make_netobj(&af->af_granted.cookie,
2036 (caddr_t)&cookie, sizeof(cookie), M_RPC);
2037 af->af_granted.exclusive = argp->exclusive;
2038 af->af_granted.alock.caller_name =
2039 strdup(argp->alock.caller_name, M_RPC);
2040 nlm_copy_netobj(&af->af_granted.alock.fh,
2041 &argp->alock.fh, M_RPC);
2042 nlm_copy_netobj(&af->af_granted.alock.oh,
2043 &argp->alock.oh, M_RPC);
2044 af->af_granted.alock.svid = argp->alock.svid;
2045 af->af_granted.alock.l_offset = argp->alock.l_offset;
2046 af->af_granted.alock.l_len = argp->alock.l_len;
2049 * Put the entry on the pending list before calling
2050 * VOP_ADVLOCKASYNC. We do this in case the lock
2051 * request was blocked (returning EINPROGRESS) but
2052 * then granted before we manage to run again. The
2053 * client may receive the granted message before we
2054 * send our blocked reply but thats their problem.
2056 mtx_lock(&host->nh_lock);
2057 TAILQ_INSERT_TAIL(&host->nh_pending, af, af_link);
2058 mtx_unlock(&host->nh_lock);
2060 error = VOP_ADVLOCKASYNC(vs.vs_vp, NULL, F_SETLK, &fl, F_REMOTE,
2061 &af->af_task, &af->af_cookie);
2064 * If the lock completed synchronously, just free the
2065 * tracking structure now.
2067 if (error != EINPROGRESS) {
2068 CLNT_RELEASE(af->af_rpc);
2069 mtx_lock(&host->nh_lock);
2070 TAILQ_REMOVE(&host->nh_pending, af, af_link);
2071 mtx_unlock(&host->nh_lock);
2072 xdr_free((xdrproc_t) xdr_nlm4_testargs,
2076 NLM_DEBUG(2, "NLM: pending async lock %p for %s "
2077 "(sysid %d)\n", af, host->nh_caller_name, sysid);
2079 * Don't vrele the vnode just yet - this must
2080 * wait until either the async callback
2081 * happens or the lock is cancelled.
2086 error = VOP_ADVLOCK(vs.vs_vp, NULL, F_SETLK, &fl, F_REMOTE);
2090 if (error == EINPROGRESS) {
2091 result->stat.stat = nlm4_blocked;
2092 } else if (error == EDEADLK) {
2093 result->stat.stat = nlm4_deadlck;
2094 } else if (error == EAGAIN) {
2095 result->stat.stat = nlm4_denied;
2097 result->stat.stat = nlm4_failed;
2101 nlm_host_monitor(host, argp->state);
2102 result->stat.stat = nlm4_granted;
2106 nlm_release_vfs_state(&vs);
2108 *rpcp = nlm_host_get_rpc(host, TRUE);
2109 nlm_host_release(host);
2114 nlm_do_cancel(nlm4_cancargs *argp, nlm4_res *result, struct svc_req *rqstp,
2118 struct vfs_state vs;
2119 struct nlm_host *host;
2122 struct nlm_async_lock *af;
2124 memset(result, 0, sizeof(*result));
2125 memset(&vs, 0, sizeof(vs));
2127 host = nlm_find_host_by_name(argp->alock.caller_name,
2128 svc_getrpccaller(rqstp), rqstp->rq_vers);
2130 result->stat.stat = nlm4_denied_nolocks;
2134 NLM_DEBUG(3, "nlm_do_cancel(): caller_name = %s (sysid = %d)\n",
2135 host->nh_caller_name, host->nh_sysid);
2137 nlm_check_expired_locks(host);
2138 sysid = host->nh_sysid;
2140 nlm_convert_to_fhandle_t(&fh, &argp->alock.fh);
2141 nlm_copy_netobj(&result->cookie, &argp->cookie, M_RPC);
2143 if (time_uptime < nlm_grace_threshold) {
2144 result->stat.stat = nlm4_denied_grace_period;
2148 error = nlm_get_vfs_state(host, rqstp, &fh, &vs, (accmode_t)0);
2150 result->stat.stat = nlm_convert_error(error);
2154 fl.l_start = argp->alock.l_offset;
2155 fl.l_len = argp->alock.l_len;
2156 fl.l_pid = argp->alock.svid;
2158 fl.l_whence = SEEK_SET;
2159 if (argp->exclusive)
2160 fl.l_type = F_WRLCK;
2162 fl.l_type = F_RDLCK;
2165 * First we need to try and find the async lock request - if
2166 * there isn't one, we give up and return nlm4_denied.
2168 mtx_lock(&host->nh_lock);
2170 TAILQ_FOREACH(af, &host->nh_pending, af_link) {
2171 if (af->af_fl.l_start == fl.l_start
2172 && af->af_fl.l_len == fl.l_len
2173 && af->af_fl.l_pid == fl.l_pid
2174 && af->af_fl.l_type == fl.l_type) {
2180 mtx_unlock(&host->nh_lock);
2181 result->stat.stat = nlm4_denied;
2185 error = nlm_cancel_async_lock(af);
2188 result->stat.stat = nlm4_denied;
2190 result->stat.stat = nlm4_granted;
2193 mtx_unlock(&host->nh_lock);
2196 nlm_release_vfs_state(&vs);
2198 *rpcp = nlm_host_get_rpc(host, TRUE);
2199 nlm_host_release(host);
2204 nlm_do_unlock(nlm4_unlockargs *argp, nlm4_res *result, struct svc_req *rqstp,
2208 struct vfs_state vs;
2209 struct nlm_host *host;
2213 memset(result, 0, sizeof(*result));
2214 memset(&vs, 0, sizeof(vs));
2216 host = nlm_find_host_by_name(argp->alock.caller_name,
2217 svc_getrpccaller(rqstp), rqstp->rq_vers);
2219 result->stat.stat = nlm4_denied_nolocks;
2223 NLM_DEBUG(3, "nlm_do_unlock(): caller_name = %s (sysid = %d)\n",
2224 host->nh_caller_name, host->nh_sysid);
2226 nlm_check_expired_locks(host);
2227 sysid = host->nh_sysid;
2229 nlm_convert_to_fhandle_t(&fh, &argp->alock.fh);
2230 nlm_copy_netobj(&result->cookie, &argp->cookie, M_RPC);
2232 if (time_uptime < nlm_grace_threshold) {
2233 result->stat.stat = nlm4_denied_grace_period;
2237 error = nlm_get_vfs_state(host, rqstp, &fh, &vs, (accmode_t)0);
2239 result->stat.stat = nlm_convert_error(error);
2243 fl.l_start = argp->alock.l_offset;
2244 fl.l_len = argp->alock.l_len;
2245 fl.l_pid = argp->alock.svid;
2247 fl.l_whence = SEEK_SET;
2248 fl.l_type = F_UNLCK;
2249 error = VOP_ADVLOCK(vs.vs_vp, NULL, F_UNLCK, &fl, F_REMOTE);
2252 * Ignore the error - there is no result code for failure,
2253 * only for grace period.
2255 result->stat.stat = nlm4_granted;
2258 nlm_release_vfs_state(&vs);
2260 *rpcp = nlm_host_get_rpc(host, TRUE);
2261 nlm_host_release(host);
2266 nlm_do_granted(nlm4_testargs *argp, nlm4_res *result, struct svc_req *rqstp,
2270 struct nlm_host *host;
2271 struct nlm_waiting_lock *nw;
2273 memset(result, 0, sizeof(*result));
2275 host = nlm_find_host_by_addr(svc_getrpccaller(rqstp), rqstp->rq_vers);
2277 result->stat.stat = nlm4_denied_nolocks;
2281 nlm_copy_netobj(&result->cookie, &argp->cookie, M_RPC);
2282 result->stat.stat = nlm4_denied;
2283 KFAIL_POINT_CODE(DEBUG_FP, nlm_deny_grant, goto out);
2285 mtx_lock(&nlm_global_lock);
2286 TAILQ_FOREACH(nw, &nlm_waiting_locks, nw_link) {
2287 if (!nw->nw_waiting)
2289 if (argp->alock.svid == nw->nw_lock.svid
2290 && argp->alock.l_offset == nw->nw_lock.l_offset
2291 && argp->alock.l_len == nw->nw_lock.l_len
2292 && argp->alock.fh.n_len == nw->nw_lock.fh.n_len
2293 && !memcmp(argp->alock.fh.n_bytes, nw->nw_lock.fh.n_bytes,
2294 nw->nw_lock.fh.n_len)) {
2295 nw->nw_waiting = FALSE;
2297 result->stat.stat = nlm4_granted;
2301 mtx_unlock(&nlm_global_lock);
2305 *rpcp = nlm_host_get_rpc(host, TRUE);
2306 nlm_host_release(host);
2311 nlm_do_granted_res(nlm4_res *argp, struct svc_req *rqstp)
2313 struct nlm_host *host = NULL;
2314 struct nlm_async_lock *af = NULL;
2317 if (argp->cookie.n_len != sizeof(struct nlm_grantcookie)) {
2318 NLM_DEBUG(1, "NLM: bogus grant cookie");
2322 host = nlm_find_host_by_sysid(ng_sysid(&argp->cookie));
2324 NLM_DEBUG(1, "NLM: Unknown host rejected our grant");
2328 mtx_lock(&host->nh_lock);
2329 TAILQ_FOREACH(af, &host->nh_granted, af_link)
2330 if (ng_cookie(&argp->cookie) ==
2331 ng_cookie(&af->af_granted.cookie))
2334 TAILQ_REMOVE(&host->nh_granted, af, af_link);
2335 mtx_unlock(&host->nh_lock);
2338 NLM_DEBUG(1, "NLM: host %s (sysid %d) replied to our grant "
2339 "with unrecognized cookie %d:%d", host->nh_caller_name,
2340 host->nh_sysid, ng_sysid(&argp->cookie),
2341 ng_cookie(&argp->cookie));
2345 if (argp->stat.stat != nlm4_granted) {
2346 af->af_fl.l_type = F_UNLCK;
2347 error = VOP_ADVLOCK(af->af_vp, NULL, F_UNLCK, &af->af_fl, F_REMOTE);
2349 NLM_DEBUG(1, "NLM: host %s (sysid %d) rejected our grant "
2350 "and we failed to unlock (%d)", host->nh_caller_name,
2351 host->nh_sysid, error);
2355 NLM_DEBUG(5, "NLM: async lock %p rejected by host %s (sysid %d)",
2356 af, host->nh_caller_name, host->nh_sysid);
2358 NLM_DEBUG(5, "NLM: async lock %p accepted by host %s (sysid %d)",
2359 af, host->nh_caller_name, host->nh_sysid);
2364 nlm_free_async_lock(af);
2366 nlm_host_release(host);
2370 nlm_do_free_all(nlm4_notify *argp)
2372 struct nlm_host *host, *thost;
2374 TAILQ_FOREACH_SAFE(host, &nlm_hosts, nh_link, thost) {
2375 if (!strcmp(host->nh_caller_name, argp->name))
2376 nlm_host_notify(host, argp->state);
2381 * Kernel module glue
2384 nfslockd_modevent(module_t mod, int type, void *data)
2389 return (nlm_init());
2393 /* The NLM module cannot be safely unloaded. */
2396 return (EOPNOTSUPP);
2399 static moduledata_t nfslockd_mod = {
2404 DECLARE_MODULE(nfslockd, nfslockd_mod, SI_SUB_VFS, SI_ORDER_ANY);
2406 /* So that loader and kldload(2) can find us, wherever we are.. */
2407 MODULE_DEPEND(nfslockd, xdr, 1, 1, 1);
2408 MODULE_DEPEND(nfslockd, krpc, 1, 1, 1);
2409 MODULE_DEPEND(nfslockd, nfscommon, 1, 1, 1);
2410 MODULE_VERSION(nfslockd, 1);