nlm_prot_impl.c revision 214048
162587Sitojun/*-
295023Ssuz * Copyright (c) 2008 Isilon Inc http://www.isilon.com/
362587Sitojun * Authors: Doug Rabson <dfr@rabson.org>
4139826Simp * Developed with Red Inc: Alfred Perlstein <alfred@freebsd.org>
553541Sshin *
653541Sshin * Redistribution and use in source and binary forms, with or without
753541Sshin * modification, are permitted provided that the following conditions
853541Sshin * are met:
953541Sshin * 1. Redistributions of source code must retain the above copyright
1053541Sshin *    notice, this list of conditions and the following disclaimer.
1153541Sshin * 2. Redistributions in binary form must reproduce the above copyright
1253541Sshin *    notice, this list of conditions and the following disclaimer in the
1353541Sshin *    documentation and/or other materials provided with the distribution.
1453541Sshin *
1553541Sshin * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1653541Sshin * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1753541Sshin * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1853541Sshin * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
1953541Sshin * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2053541Sshin * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2153541Sshin * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2253541Sshin * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2353541Sshin * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2453541Sshin * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2553541Sshin * SUCH DAMAGE.
2653541Sshin */
2753541Sshin
2853541Sshin#include "opt_inet6.h"
2953541Sshin
3053541Sshin#include <sys/cdefs.h>
3153541Sshin__FBSDID("$FreeBSD: head/sys/nlm/nlm_prot_impl.c 214048 2010-10-19 00:20:00Z rmacklem $");
3253541Sshin
3353541Sshin#include <sys/param.h>
3453541Sshin#include <sys/fail.h>
3553541Sshin#include <sys/fcntl.h>
3653541Sshin#include <sys/kernel.h>
3753541Sshin#include <sys/kthread.h>
3853541Sshin#include <sys/lockf.h>
3953541Sshin#include <sys/malloc.h>
4053541Sshin#include <sys/mount.h>
4153541Sshin#if __FreeBSD_version >= 700000
4253541Sshin#include <sys/priv.h>
4353541Sshin#endif
4453541Sshin#include <sys/proc.h>
4553541Sshin#include <sys/socket.h>
4653541Sshin#include <sys/socketvar.h>
4753541Sshin#include <sys/syscall.h>
4853541Sshin#include <sys/sysctl.h>
4953541Sshin#include <sys/sysent.h>
5062587Sitojun#include <sys/syslog.h>
5153541Sshin#include <sys/sysproto.h>
5262587Sitojun#include <sys/systm.h>
53121684Sume#include <sys/taskqueue.h>
54121684Sume#include <sys/unistd.h>
5553541Sshin#include <sys/vnode.h>
5653541Sshin
5753541Sshin#include <nfs/nfsproto.h>
5853541Sshin#include <nfs/nfs_lock.h>
5953541Sshin
6053541Sshin#include <nlm/nlm_prot.h>
6153541Sshin#include <nlm/sm_inter.h>
6253541Sshin#include <nlm/nlm.h>
6362587Sitojun#include <rpc/rpc_com.h>
6453541Sshin#include <rpc/rpcb_prot.h>
6562587Sitojun
6662587SitojunMALLOC_DEFINE(M_NLM, "NLM", "Network Lock Manager");
6762587Sitojun
6862587Sitojun/*
6962587Sitojun * If a host is inactive (and holds no locks) for this amount of
7053541Sshin * seconds, we consider it idle and stop tracking it.
71121346Sume */
72121346Sume#define NLM_IDLE_TIMEOUT	30
73121346Sume
74121346Sume/*
75121346Sume * We check the host list for idle every few seconds.
76121346Sume */
77121346Sume#define NLM_IDLE_PERIOD		5
7853541Sshin
79121346Sume/*
80121346Sume * We only look for GRANTED_RES messages for a little while.
81121346Sume */
82121355Sume#define NLM_EXPIRE_TIMEOUT	10
83121346Sume
84121345Sume/*
8569774Sphk * Support for sysctl vfs.nlm.sysid
8662587Sitojun */
8753541SshinSYSCTL_NODE(_vfs, OID_AUTO, nlm, CTLFLAG_RW, NULL, "Network Lock Manager");
8853541SshinSYSCTL_NODE(_vfs_nlm, OID_AUTO, sysid, CTLFLAG_RW, NULL, "");
8953541Sshin
90157927Sps/*
91157927Sps * Syscall hooks
92157927Sps */
93157927Spsstatic int nlm_syscall_offset = SYS_nlm_syscall;
94157927Spsstatic struct sysent nlm_syscall_prev_sysent;
95157927Sps#if __FreeBSD_version < 700000
96157927Spsstatic struct sysent nlm_syscall_sysent = {
97157927Sps	(sizeof(struct nlm_syscall_args) / sizeof(register_t)) | SYF_MPSAFE,
9853541Sshin	(sy_call_t *) nlm_syscall
9953541Sshin};
10053541Sshin#else
10153541SshinMAKE_SYSENT(nlm_syscall);
10277969Sjesper#endif
103121345Sumestatic bool_t nlm_syscall_registered = FALSE;
104157927Sps
105157927Sps/*
10677969Sjesper * Debug level passed in from userland. We also support a sysctl hook
107121346Sume * so that it can be changed on a live system.
108121346Sume */
10953541Sshinstatic int nlm_debug_level;
11053541SshinSYSCTL_INT(_debug, OID_AUTO, nlm_debug, CTLFLAG_RW, &nlm_debug_level, 0, "");
11153541Sshin
11253541Sshin#define NLM_DEBUG(_level, args...)			\
11362587Sitojun	do {						\
11462587Sitojun		if (nlm_debug_level >= (_level))	\
11562587Sitojun			log(LOG_DEBUG, args);		\
11662587Sitojun	} while(0)
11762587Sitojun#define NLM_ERR(args...)			\
11862587Sitojun	do {					\
11962587Sitojun		log(LOG_ERR, args);		\
12062587Sitojun	} while(0)
12162587Sitojun
12262587Sitojun/*
12362587Sitojun * Grace period handling. The value of nlm_grace_threshold is the
12462587Sitojun * value of time_uptime after which we are serving requests normally.
12562587Sitojun */
12662587Sitojunstatic time_t nlm_grace_threshold;
12762587Sitojun
12862587Sitojun/*
12962587Sitojun * We check for idle hosts if time_uptime is greater than
13062587Sitojun * nlm_next_idle_check,
13162587Sitojun */
13262587Sitojunstatic time_t nlm_next_idle_check;
13362587Sitojun
13462587Sitojun/*
13562587Sitojun * A socket to use for RPC - shared by all IPv4 RPC clients.
13662587Sitojun */
13762587Sitojunstatic struct socket *nlm_socket;
13862587Sitojun
13962587Sitojun#ifdef INET6
14062587Sitojun
14162587Sitojun/*
14253541Sshin * A socket to use for RPC - shared by all IPv6 RPC clients.
14353541Sshin */
14453541Sshinstatic struct socket *nlm_socket6;
14553541Sshin
14653541Sshin#endif
14753541Sshin
14853541Sshin/*
14953541Sshin * An RPC client handle that can be used to communicate with the local
15053541Sshin * NSM.
15153541Sshin */
15253541Sshinstatic CLIENT *nlm_nsm;
15362587Sitojun
154121630Sume/*
155121630Sume * An AUTH handle for the server's creds.
156121630Sume */
15753541Sshinstatic AUTH *nlm_auth;
15853541Sshin
15962587Sitojun/*
16053541Sshin * A zero timeval for sending async RPC messages.
161121684Sume */
16253541Sshinstruct timeval nlm_zero_tv = { 0, 0 };
16362587Sitojun
16462587Sitojun/*
16553541Sshin * The local NSM state number
16653541Sshin */
16762587Sitojunint nlm_nsm_state;
16862587Sitojun
16962587Sitojun
170120856Sume/*
17162587Sitojun * A lock to protect the host list and waiting lock list.
17253541Sshin */
17353541Sshinstatic struct mtx nlm_global_lock;
17453541Sshin
17553541Sshin/*
176121630Sume * Locks:
177121630Sume * (l)		locked by nh_lock
17853541Sshin * (s)		only accessed via server RPC which is single threaded
17953541Sshin * (g)		locked by nlm_global_lock
18053541Sshin * (c)		const until freeing
18153541Sshin * (a)		modified using atomic ops
18253541Sshin */
18353541Sshin
18453541Sshin/*
18553541Sshin * A pending client-side lock request, stored on the nlm_waiting_locks
18653541Sshin * list.
18753541Sshin */
18853541Sshinstruct nlm_waiting_lock {
18953541Sshin	TAILQ_ENTRY(nlm_waiting_lock) nw_link; /* (g) */
19053541Sshin	bool_t		nw_waiting;	       /* (g) */
19153541Sshin	nlm4_lock	nw_lock;	       /* (c) */
19253541Sshin	union nfsfh	nw_fh;		       /* (c) */
19353541Sshin	struct vnode	*nw_vp;		       /* (c) */
19453541Sshin};
19553541SshinTAILQ_HEAD(nlm_waiting_lock_list, nlm_waiting_lock);
19653541Sshin
19753541Sshinstruct nlm_waiting_lock_list nlm_waiting_locks; /* (g) */
19853541Sshin
199120891Sume/*
200120891Sume * A pending server-side asynchronous lock request, stored on the
20153541Sshin * nh_pending list of the NLM host.
20253541Sshin */
20353541Sshinstruct nlm_async_lock {
20453541Sshin	TAILQ_ENTRY(nlm_async_lock) af_link; /* (l) host's list of locks */
20553541Sshin	struct task	af_task;	/* (c) async callback details */
20653541Sshin	void		*af_cookie;	/* (l) lock manager cancel token */
207120891Sume	struct vnode	*af_vp;		/* (l) vnode to lock */
20862587Sitojun	struct flock	af_fl;		/* (c) lock details */
20953541Sshin	struct nlm_host *af_host;	/* (c) host which is locking */
21053541Sshin	CLIENT		*af_rpc;	/* (c) rpc client to send message */
211121345Sume	nlm4_testargs	af_granted;	/* (c) notification details */
21278064Sume	time_t		af_expiretime;	/* (c) notification time */
213121345Sume};
214121345SumeTAILQ_HEAD(nlm_async_lock_list, nlm_async_lock);
215121345Sume
216121345Sume/*
217121345Sume * NLM host.
218121345Sume */
219121345Sumeenum nlm_host_state {
220121345Sume	NLM_UNMONITORED,
221121345Sume	NLM_MONITORED,
222121345Sume	NLM_MONITOR_FAILED,
22353541Sshin	NLM_RECOVERING
22453541Sshin};
22553541Sshin
22653541Sshinstruct nlm_rpc {
22753541Sshin	CLIENT		*nr_client;    /* (l) RPC client handle */
22853541Sshin	time_t		nr_create_time; /* (l) when client was created */
22953541Sshin};
23053541Sshin
23153541Sshinstruct nlm_host {
23253541Sshin	struct mtx	nh_lock;
23353541Sshin	volatile u_int	nh_refs;       /* (a) reference count */
23453541Sshin	TAILQ_ENTRY(nlm_host) nh_link; /* (g) global list of hosts */
23553541Sshin	char		nh_caller_name[MAXNAMELEN]; /* (c) printable name of host */
23653541Sshin	uint32_t	nh_sysid;	 /* (c) our allocaed system ID */
23753541Sshin	char		nh_sysid_string[10]; /* (c) string rep. of sysid */
238121345Sume	struct sockaddr_storage	nh_addr; /* (s) remote address of host */
239121345Sume	struct nlm_rpc	nh_srvrpc;	 /* (l) RPC for server replies */
240121345Sume	struct nlm_rpc	nh_clntrpc;	 /* (l) RPC for client requests */
24153541Sshin	rpcvers_t	nh_vers;	 /* (s) NLM version of host */
24278064Sume	int		nh_state;	 /* (s) last seen NSM state of host */
24378064Sume	enum nlm_host_state nh_monstate; /* (l) local NSM monitoring state */
24478064Sume	time_t		nh_idle_timeout; /* (s) Time at which host is idle */
24578064Sume	struct sysctl_ctx_list nh_sysctl; /* (c) vfs.nlm.sysid nodes */
24678064Sume	uint32_t	nh_grantcookie;  /* (l) grant cookie counter */
24753541Sshin	struct nlm_async_lock_list nh_pending; /* (l) pending async locks */
248121607Sume	struct nlm_async_lock_list nh_granted; /* (l) granted locks */
24953541Sshin	struct nlm_async_lock_list nh_finished; /* (l) finished async locks */
25053541Sshin};
25162587SitojunTAILQ_HEAD(nlm_host_list, nlm_host);
25253541Sshin
25353541Sshinstatic struct nlm_host_list nlm_hosts; /* (g) */
25453541Sshinstatic uint32_t nlm_next_sysid = 1;    /* (g) */
25562587Sitojun
25653541Sshinstatic void	nlm_host_unmonitor(struct nlm_host *);
25762587Sitojun
25862587Sitojunstruct nlm_grantcookie {
25962587Sitojun	uint32_t	ng_sysid;
26053541Sshin	uint32_t	ng_cookie;
26153541Sshin};
26253541Sshin
26353541Sshinstatic inline uint32_t
26453541Sshinng_sysid(struct netobj *src)
26553541Sshin{
266121345Sume
267121345Sume	return ((struct nlm_grantcookie *)src->n_bytes)->ng_sysid;
26853541Sshin}
26953541Sshin
27053541Sshinstatic inline uint32_t
27153541Sshinng_cookie(struct netobj *src)
27253541Sshin{
27353541Sshin
27453541Sshin	return ((struct nlm_grantcookie *)src->n_bytes)->ng_cookie;
27553541Sshin}
276120891Sume
277120891Sume/**********************************************************************/
27853541Sshin
27953541Sshin/*
28053541Sshin * Initialise NLM globals.
28153541Sshin */
28253541Sshinstatic void
28353541Sshinnlm_init(void *dummy)
28453541Sshin{
28553541Sshin	int error;
28662587Sitojun
28753541Sshin	mtx_init(&nlm_global_lock, "nlm_global_lock", NULL, MTX_DEF);
28853541Sshin	TAILQ_INIT(&nlm_waiting_locks);
28953541Sshin	TAILQ_INIT(&nlm_hosts);
29053541Sshin
291120891Sume	error = syscall_register(&nlm_syscall_offset, &nlm_syscall_sysent,
292120891Sume	    &nlm_syscall_prev_sysent);
293121345Sume	if (error)
294120856Sume		NLM_ERR("Can't register NLM syscall\n");
29553541Sshin	else
296120891Sume		nlm_syscall_registered = TRUE;
29753541Sshin}
298120891SumeSYSINIT(nlm_init, SI_SUB_LOCK, SI_ORDER_FIRST, nlm_init, NULL);
299120891Sume
300121345Sumestatic void
301120856Sumenlm_uninit(void *dummy)
30253541Sshin{
30353541Sshin
30453541Sshin	if (nlm_syscall_registered)
30553541Sshin		syscall_deregister(&nlm_syscall_offset,
30653541Sshin		    &nlm_syscall_prev_sysent);
30753541Sshin}
30853541SshinSYSUNINIT(nlm_uninit, SI_SUB_LOCK, SI_ORDER_FIRST, nlm_uninit, NULL);
30953541Sshin
31053541Sshin/*
31153541Sshin * Create a netobj from an arbitrary source.
31253541Sshin */
31353541Sshinvoid
31453541Sshinnlm_make_netobj(struct netobj *dst, caddr_t src, size_t srcsize,
31553541Sshin    struct malloc_type *type)
31653541Sshin{
31753541Sshin
31853541Sshin	dst->n_len = srcsize;
31953541Sshin	dst->n_bytes = malloc(srcsize, type, M_WAITOK);
32062587Sitojun	memcpy(dst->n_bytes, src, srcsize);
32153541Sshin}
32253541Sshin
32353541Sshin/*
32453541Sshin * Copy a struct netobj.
32553541Sshin */
32653541Sshinvoid
32753541Sshinnlm_copy_netobj(struct netobj *dst, struct netobj *src,
32853541Sshin    struct malloc_type *type)
32953541Sshin{
33053541Sshin
33153541Sshin	nlm_make_netobj(dst, src->n_bytes, src->n_len, type);
33253541Sshin}
333120891Sume
334120891Sume
335120891Sume/*
33653541Sshin * Create an RPC client handle for the given (address,prog,vers)
33753541Sshin * triple using UDP.
33853541Sshin */
33953541Sshinstatic CLIENT *
34062587Sitojunnlm_get_rpc(struct sockaddr *sa, rpcprog_t prog, rpcvers_t vers)
341121607Sume{
34262587Sitojun	char *wchan = "nlmrcv";
34362587Sitojun	const char* protofmly;
34462587Sitojun	struct sockaddr_storage ss;
34562587Sitojun	struct socket *so;
34662587Sitojun	CLIENT *rpcb;
34762587Sitojun	struct timeval timo;
34862587Sitojun	RPCB parms;
34953541Sshin	char *uaddr;
35053541Sshin	enum clnt_stat stat = RPC_SUCCESS;
35153541Sshin	int rpcvers = RPCBVERS4;
35253541Sshin	bool_t do_tcp = FALSE;
35353541Sshin	bool_t tryagain = FALSE;
35453541Sshin	struct portmap mapping;
35553541Sshin	u_short port = 0;
35653541Sshin
35753541Sshin	/*
35853541Sshin	 * First we need to contact the remote RPCBIND service to find
35953541Sshin	 * the right port.
36053541Sshin	 */
361121684Sume	memcpy(&ss, sa, sa->sa_len);
362121684Sume	switch (ss.ss_family) {
363121684Sume	case AF_INET:
364121684Sume		((struct sockaddr_in *)&ss)->sin_port = htons(111);
365121684Sume		protofmly = "inet";
366121684Sume		so = nlm_socket;
367121684Sume		break;
368121684Sume
369121684Sume#ifdef INET6
370121684Sume	case AF_INET6:
371121684Sume		((struct sockaddr_in6 *)&ss)->sin6_port = htons(111);
372121684Sume		protofmly = "inet6";
373121684Sume		so = nlm_socket6;
374121684Sume		break;
375121684Sume#endif
376121684Sume
377121684Sume	default:
378121684Sume		/*
379121684Sume		 * Unsupported address family - fail.
380121684Sume		 */
38153541Sshin		return (NULL);
38253541Sshin	}
38353541Sshin
38453541Sshin	rpcb = clnt_dg_create(so, (struct sockaddr *)&ss,
38553541Sshin	    RPCBPROG, rpcvers, 0, 0);
38653541Sshin	if (!rpcb)
38753541Sshin		return (NULL);
38862587Sitojun
38953541Sshintry_tcp:
39062587Sitojun	parms.r_prog = prog;
39162587Sitojun	parms.r_vers = vers;
39262587Sitojun	if (do_tcp)
39362587Sitojun		parms.r_netid = "tcp";
39462587Sitojun	else
39562587Sitojun		parms.r_netid = "udp";
39662587Sitojun	parms.r_addr = "";
39762587Sitojun	parms.r_owner = "";
39862587Sitojun
39962587Sitojun	/*
40062587Sitojun	 * Use the default timeout.
40162587Sitojun	 */
40262587Sitojun	timo.tv_sec = 25;
40362587Sitojun	timo.tv_usec = 0;
40462587Sitojunagain:
40562587Sitojun	switch (rpcvers) {
40662587Sitojun	case RPCBVERS4:
40762587Sitojun	case RPCBVERS:
40862587Sitojun		/*
40962587Sitojun		 * Try RPCBIND 4 then 3.
41062587Sitojun		 */
41162587Sitojun		uaddr = NULL;
41262587Sitojun		stat = CLNT_CALL(rpcb, (rpcprog_t) RPCBPROC_GETADDR,
41362587Sitojun		    (xdrproc_t) xdr_rpcb, &parms,
41462587Sitojun		    (xdrproc_t) xdr_wrapstring, &uaddr, timo);
41562587Sitojun		if (stat == RPC_SUCCESS) {
41662587Sitojun			/*
41762587Sitojun			 * We have a reply from the remote RPCBIND - turn it
41862587Sitojun			 * into an appropriate address and make a new client
41962587Sitojun			 * that can talk to the remote NLM.
42062587Sitojun			 *
42162587Sitojun			 * XXX fixup IPv6 scope ID.
42262587Sitojun			 */
42362587Sitojun			struct netbuf *a;
42462587Sitojun			a = __rpc_uaddr2taddr_af(ss.ss_family, uaddr);
42553541Sshin			if (!a) {
42653541Sshin				tryagain = TRUE;
42753541Sshin			} else {
428121345Sume				tryagain = FALSE;
429121345Sume				memcpy(&ss, a->buf, a->len);
43053541Sshin				free(a->buf, M_RPC);
43153541Sshin				free(a, M_RPC);
43253541Sshin				xdr_free((xdrproc_t) xdr_wrapstring, &uaddr);
43353541Sshin			}
43453541Sshin		}
43576899Ssumikawa		if (tryagain || stat == RPC_PROGVERSMISMATCH) {
43653541Sshin			if (rpcvers == RPCBVERS4)
43753541Sshin				rpcvers = RPCBVERS;
43853541Sshin			else if (rpcvers == RPCBVERS)
43976899Ssumikawa				rpcvers = PMAPVERS;
44076899Ssumikawa			CLNT_CONTROL(rpcb, CLSET_VERS, &rpcvers);
44153541Sshin			goto again;
44253541Sshin		}
44353541Sshin		break;
44453541Sshin	case PMAPVERS:
44553541Sshin		/*
44653541Sshin		 * Try portmap.
44776899Ssumikawa		 */
44853541Sshin		mapping.pm_prog = parms.r_prog;
44953541Sshin		mapping.pm_vers = parms.r_vers;
45053541Sshin		mapping.pm_prot = do_tcp ? IPPROTO_TCP : IPPROTO_UDP;
45176899Ssumikawa		mapping.pm_port = 0;
45276899Ssumikawa
45353541Sshin		stat = CLNT_CALL(rpcb, (rpcprog_t) PMAPPROC_GETPORT,
45453541Sshin		    (xdrproc_t) xdr_portmap, &mapping,
45553541Sshin		    (xdrproc_t) xdr_u_short, &port, timo);
45662587Sitojun
45753541Sshin		if (stat == RPC_SUCCESS) {
45853541Sshin			switch (ss.ss_family) {
45953541Sshin			case AF_INET:
46053541Sshin				((struct sockaddr_in *)&ss)->sin_port =
46153541Sshin					htons(port);
46253541Sshin				break;
46353541Sshin
46453541Sshin#ifdef INET6
46553541Sshin			case AF_INET6:
46653541Sshin				((struct sockaddr_in6 *)&ss)->sin6_port =
467121345Sume					htons(port);
468121345Sume				break;
46962587Sitojun#endif
47062587Sitojun			}
47162587Sitojun		}
47262587Sitojun		break;
47362587Sitojun	default:
47462587Sitojun		panic("invalid rpcvers %d", rpcvers);
47553541Sshin	}
47653541Sshin	/*
47753541Sshin	 * We may have a positive response from the portmapper, but the NLM
47853541Sshin	 * service was not found. Make sure we received a valid port.
479121345Sume	 */
48053541Sshin	switch (ss.ss_family) {
48153541Sshin	case AF_INET:
48253541Sshin		port = ((struct sockaddr_in *)&ss)->sin_port;
48353541Sshin		break;
48453541Sshin#ifdef INET6
485121345Sume	case AF_INET6:
48653541Sshin		port = ((struct sockaddr_in6 *)&ss)->sin6_port;
48753541Sshin		break;
48853541Sshin#endif
48953541Sshin	}
49053541Sshin	if (stat != RPC_SUCCESS || !port) {
49153541Sshin		/*
49253541Sshin		 * If we were able to talk to rpcbind or portmap, but the udp
49353541Sshin		 * variant wasn't available, ask about tcp.
49453541Sshin		 *
49562587Sitojun		 * XXX - We could also check for a TCP portmapper, but
49653541Sshin		 * if the host is running a portmapper at all, we should be able
49762587Sitojun		 * to hail it over UDP.
49862587Sitojun		 */
49953541Sshin		if (stat == RPC_SUCCESS && !do_tcp) {
50053541Sshin			do_tcp = TRUE;
50153541Sshin			goto try_tcp;
50262587Sitojun		}
50362587Sitojun
50462587Sitojun		/* Otherwise, bad news. */
50553541Sshin		NLM_ERR("NLM: failed to contact remote rpcbind, "
50653541Sshin		    "stat = %d, port = %d\n", (int) stat, port);
50753541Sshin		CLNT_DESTROY(rpcb);
50853541Sshin		return (NULL);
50962587Sitojun	}
51062587Sitojun
51153541Sshin	if (do_tcp) {
51253541Sshin		/*
51353541Sshin		 * Destroy the UDP client we used to speak to rpcbind and
51453541Sshin		 * recreate as a TCP client.
51562587Sitojun		 */
51662587Sitojun		struct netconfig *nconf = NULL;
51762587Sitojun
51853541Sshin		CLNT_DESTROY(rpcb);
51953541Sshin
52053541Sshin		switch (ss.ss_family) {
52153541Sshin		case AF_INET:
52262587Sitojun			nconf = getnetconfigent("tcp");
52353541Sshin			break;
52453541Sshin#ifdef INET6
52562587Sitojun		case AF_INET6:
52662587Sitojun			nconf = getnetconfigent("tcp6");
52762587Sitojun			break;
52862587Sitojun#endif
529111119Simp		}
53062587Sitojun
531121345Sume		rpcb = clnt_reconnect_create(nconf, (struct sockaddr *)&ss,
53262587Sitojun		    prog, vers, 0, 0);
53362587Sitojun		CLNT_CONTROL(rpcb, CLSET_WAITCHAN, wchan);
53462587Sitojun		rpcb->cl_auth = nlm_auth;
53562587Sitojun
53662587Sitojun	} else {
53762587Sitojun		/*
53853541Sshin		 * Re-use the client we used to speak to rpcbind.
53953541Sshin		 */
54053541Sshin		CLNT_CONTROL(rpcb, CLSET_SVC_ADDR, &ss);
54153541Sshin		CLNT_CONTROL(rpcb, CLSET_PROG, &prog);
54253541Sshin		CLNT_CONTROL(rpcb, CLSET_VERS, &vers);
54353541Sshin		CLNT_CONTROL(rpcb, CLSET_WAITCHAN, wchan);
54453541Sshin		rpcb->cl_auth = nlm_auth;
54553541Sshin	}
54653541Sshin
54753541Sshin	return (rpcb);
54853541Sshin}
549121345Sume
55053541Sshin/*
55153541Sshin * This async callback after when an async lock request has been
55253541Sshin * granted. We notify the host which initiated the request.
55353541Sshin */
55453541Sshinstatic void
55553541Sshinnlm_lock_callback(void *arg, int pending)
55653541Sshin{
55753541Sshin	struct nlm_async_lock *af = (struct nlm_async_lock *) arg;
55853541Sshin	struct rpc_callextra ext;
559120891Sume
56053541Sshin	NLM_DEBUG(2, "NLM: async lock %p for %s (sysid %d) granted,"
56153541Sshin	    " cookie %d:%d\n", af, af->af_host->nh_caller_name,
56253541Sshin	    af->af_host->nh_sysid, ng_sysid(&af->af_granted.cookie),
56353541Sshin	    ng_cookie(&af->af_granted.cookie));
56453541Sshin
56553541Sshin	/*
56653541Sshin	 * Send the results back to the host.
56753541Sshin	 *
56853541Sshin	 * Note: there is a possible race here with nlm_host_notify
56953541Sshin	 * destroying the RPC client. To avoid problems, the first
570121345Sume	 * thing nlm_host_notify does is to cancel pending async lock
57153541Sshin	 * requests.
57253541Sshin	 */
57353541Sshin	memset(&ext, 0, sizeof(ext));
574121346Sume	ext.rc_auth = nlm_auth;
57553541Sshin	if (af->af_host->nh_vers == NLM_VERS4) {
57653541Sshin		nlm4_granted_msg_4(&af->af_granted,
57753541Sshin		    NULL, af->af_rpc, &ext, nlm_zero_tv);
57853541Sshin	} else {
57953541Sshin		/*
58053541Sshin		 * Back-convert to legacy protocol
58153541Sshin		 */
58253541Sshin		nlm_testargs granted;
58353541Sshin		granted.cookie = af->af_granted.cookie;
58453541Sshin		granted.exclusive = af->af_granted.exclusive;
58553541Sshin		granted.alock.caller_name =
58653541Sshin			af->af_granted.alock.caller_name;
58753541Sshin		granted.alock.fh = af->af_granted.alock.fh;
58853541Sshin		granted.alock.oh = af->af_granted.alock.oh;
58953541Sshin		granted.alock.svid = af->af_granted.alock.svid;
59053541Sshin		granted.alock.l_offset =
591121355Sume			af->af_granted.alock.l_offset;
592121345Sume		granted.alock.l_len =
59353541Sshin			af->af_granted.alock.l_len;
59453541Sshin
59553541Sshin		nlm_granted_msg_1(&granted,
59653541Sshin		    NULL, af->af_rpc, &ext, nlm_zero_tv);
59753541Sshin	}
59853541Sshin
59953541Sshin	/*
60053541Sshin	 * Move this entry to the nh_granted list.
60153541Sshin	 */
60253541Sshin	af->af_expiretime = time_uptime + NLM_EXPIRE_TIMEOUT;
60353541Sshin	mtx_lock(&af->af_host->nh_lock);
60453541Sshin	TAILQ_REMOVE(&af->af_host->nh_pending, af, af_link);
60553541Sshin	TAILQ_INSERT_TAIL(&af->af_host->nh_granted, af, af_link);
60653541Sshin	mtx_unlock(&af->af_host->nh_lock);
60753541Sshin}
60853541Sshin
60953541Sshin/*
610120891Sume * Free an async lock request. The request must have been removed from
61153541Sshin * any list.
61253541Sshin */
61353541Sshinstatic void
61453541Sshinnlm_free_async_lock(struct nlm_async_lock *af)
61553541Sshin{
61662587Sitojun	/*
61753541Sshin	 * Free an async lock.
61862587Sitojun	 */
61953541Sshin	if (af->af_rpc)
62053541Sshin		CLNT_RELEASE(af->af_rpc);
621121345Sume	xdr_free((xdrproc_t) xdr_nlm4_testargs, &af->af_granted);
62253541Sshin	if (af->af_vp)
62353541Sshin		vrele(af->af_vp);
62453541Sshin	free(af, M_NLM);
62553541Sshin}
62653541Sshin
62753541Sshin/*
62853541Sshin * Cancel our async request - this must be called with
62953541Sshin * af->nh_host->nh_lock held. This is slightly complicated by a
63053541Sshin * potential race with our own callback. If we fail to cancel the
63153541Sshin * lock, it must already have been granted - we make sure our async
63253541Sshin * task has completed by calling taskqueue_drain in this case.
63353541Sshin */
634121345Sumestatic int
635121355Sumenlm_cancel_async_lock(struct nlm_async_lock *af)
636121345Sume{
63753541Sshin	struct nlm_host *host = af->af_host;
63853541Sshin	int error;
63953541Sshin
64053541Sshin	mtx_assert(&host->nh_lock, MA_OWNED);
64153541Sshin
64253541Sshin	mtx_unlock(&host->nh_lock);
64353541Sshin
64453541Sshin	error = VOP_ADVLOCKASYNC(af->af_vp, NULL, F_CANCEL, &af->af_fl,
64553541Sshin	    F_REMOTE, NULL, &af->af_cookie);
64653541Sshin
64753541Sshin	if (error) {
64853541Sshin		/*
64953541Sshin		 * We failed to cancel - make sure our callback has
650121345Sume		 * completed before we continue.
651121355Sume		 */
652121345Sume		taskqueue_drain(taskqueue_thread, &af->af_task);
65353541Sshin	}
65453541Sshin
65553541Sshin	mtx_lock(&host->nh_lock);
65653541Sshin
65753541Sshin	if (!error) {
65853541Sshin		NLM_DEBUG(2, "NLM: async lock %p for %s (sysid %d) "
65953541Sshin		    "cancelled\n", af, host->nh_caller_name, host->nh_sysid);
66053541Sshin
661121345Sume		/*
662121355Sume		 * Remove from the nh_pending list and free now that
663121345Sume		 * we are safe from the callback.
66453541Sshin		 */
66553541Sshin		TAILQ_REMOVE(&host->nh_pending, af, af_link);
66653541Sshin		mtx_unlock(&host->nh_lock);
66753541Sshin		nlm_free_async_lock(af);
66853541Sshin		mtx_lock(&host->nh_lock);
66953541Sshin	}
67053541Sshin
67153541Sshin	return (error);
67253541Sshin}
67353541Sshin
674121345Sumestatic void
675121355Sumenlm_check_expired_locks(struct nlm_host *host)
676121345Sume{
67753541Sshin	struct nlm_async_lock *af;
67853541Sshin	time_t uptime = time_uptime;
67953541Sshin
68053541Sshin	mtx_lock(&host->nh_lock);
68153541Sshin	while ((af = TAILQ_FIRST(&host->nh_granted)) != NULL
68278064Sume	    && uptime >= af->af_expiretime) {
68353541Sshin		NLM_DEBUG(2, "NLM: async lock %p for %s (sysid %d) expired,"
68453541Sshin		    " cookie %d:%d\n", af, af->af_host->nh_caller_name,
68553541Sshin		    af->af_host->nh_sysid, ng_sysid(&af->af_granted.cookie),
68653541Sshin		    ng_cookie(&af->af_granted.cookie));
68753541Sshin		TAILQ_REMOVE(&host->nh_granted, af, af_link);
68853541Sshin		mtx_unlock(&host->nh_lock);
68953541Sshin		nlm_free_async_lock(af);
69053541Sshin		mtx_lock(&host->nh_lock);
691121345Sume	}
69253541Sshin	while ((af = TAILQ_FIRST(&host->nh_finished)) != NULL) {
69353541Sshin		TAILQ_REMOVE(&host->nh_finished, af, af_link);
69453541Sshin		mtx_unlock(&host->nh_lock);
69553541Sshin		nlm_free_async_lock(af);
69653541Sshin		mtx_lock(&host->nh_lock);
69753541Sshin	}
69853541Sshin	mtx_unlock(&host->nh_lock);
69953541Sshin}
70053541Sshin
70153541Sshin/*
70253541Sshin * Free resources used by a host. This is called after the reference
70353541Sshin * count has reached zero so it doesn't need to worry about locks.
70453541Sshin */
70553541Sshinstatic void
70653541Sshinnlm_host_destroy(struct nlm_host *host)
70753541Sshin{
70878064Sume
70978064Sume	mtx_lock(&nlm_global_lock);
71053541Sshin	TAILQ_REMOVE(&nlm_hosts, host, nh_link);
71153541Sshin	mtx_unlock(&nlm_global_lock);
71253541Sshin
71353541Sshin	if (host->nh_srvrpc.nr_client)
714121345Sume		CLNT_RELEASE(host->nh_srvrpc.nr_client);
71562587Sitojun	if (host->nh_clntrpc.nr_client)
71662587Sitojun		CLNT_RELEASE(host->nh_clntrpc.nr_client);
71762587Sitojun	mtx_destroy(&host->nh_lock);
71862587Sitojun	sysctl_ctx_free(&host->nh_sysctl);
71962587Sitojun	free(host, M_NLM);
72062587Sitojun}
72162587Sitojun
72262587Sitojun/*
72362587Sitojun * Thread start callback for client lock recovery
72462587Sitojun */
72562587Sitojunstatic void
72662587Sitojunnlm_client_recovery_start(void *arg)
72762587Sitojun{
72862587Sitojun	struct nlm_host *host = (struct nlm_host *) arg;
72962587Sitojun
73062587Sitojun	NLM_DEBUG(1, "NLM: client lock recovery for %s started\n",
73153541Sshin	    host->nh_caller_name);
73253541Sshin
73353541Sshin	nlm_client_recovery(host);
73453541Sshin
73553541Sshin	NLM_DEBUG(1, "NLM: client lock recovery for %s completed\n",
73653541Sshin	    host->nh_caller_name);
73753541Sshin
73853541Sshin	host->nh_monstate = NLM_MONITORED;
739121345Sume	nlm_host_release(host);
740121346Sume
74153541Sshin	kthread_exit();
74253541Sshin}
74353541Sshin
74453541Sshin/*
74553541Sshin * This is called when we receive a host state change notification. We
74653541Sshin * unlock any active locks owned by the host. When rpc.lockd is
747121345Sume * shutting down, this function is called with newstate set to zero
74853541Sshin * which allows us to cancel any pending async locks and clear the
749 * locking state.
750 */
751static void
752nlm_host_notify(struct nlm_host *host, int newstate)
753{
754	struct nlm_async_lock *af;
755
756	if (newstate) {
757		NLM_DEBUG(1, "NLM: host %s (sysid %d) rebooted, new "
758		    "state is %d\n", host->nh_caller_name,
759		    host->nh_sysid, newstate);
760	}
761
762	/*
763	 * Cancel any pending async locks for this host.
764	 */
765	mtx_lock(&host->nh_lock);
766	while ((af = TAILQ_FIRST(&host->nh_pending)) != NULL) {
767		/*
768		 * nlm_cancel_async_lock will remove the entry from
769		 * nh_pending and free it.
770		 */
771		nlm_cancel_async_lock(af);
772	}
773	mtx_unlock(&host->nh_lock);
774	nlm_check_expired_locks(host);
775
776	/*
777	 * The host just rebooted - trash its locks.
778	 */
779	lf_clearremotesys(host->nh_sysid);
780	host->nh_state = newstate;
781
782	/*
783	 * If we have any remote locks for this host (i.e. it
784	 * represents a remote NFS server that our local NFS client
785	 * has locks for), start a recovery thread.
786	 */
787	if (newstate != 0
788	    && host->nh_monstate != NLM_RECOVERING
789	    && lf_countlocks(NLM_SYSID_CLIENT | host->nh_sysid) > 0) {
790		struct thread *td;
791		host->nh_monstate = NLM_RECOVERING;
792		refcount_acquire(&host->nh_refs);
793		kthread_add(nlm_client_recovery_start, host, curproc, &td, 0, 0,
794		    "NFS lock recovery for %s", host->nh_caller_name);
795	}
796}
797
798/*
799 * Sysctl handler to count the number of locks for a sysid.
800 */
801static int
802nlm_host_lock_count_sysctl(SYSCTL_HANDLER_ARGS)
803{
804	struct nlm_host *host;
805	int count;
806
807	host = oidp->oid_arg1;
808	count = lf_countlocks(host->nh_sysid);
809	return sysctl_handle_int(oidp, &count, 0, req);
810}
811
812/*
813 * Sysctl handler to count the number of client locks for a sysid.
814 */
815static int
816nlm_host_client_lock_count_sysctl(SYSCTL_HANDLER_ARGS)
817{
818	struct nlm_host *host;
819	int count;
820
821	host = oidp->oid_arg1;
822	count = lf_countlocks(NLM_SYSID_CLIENT | host->nh_sysid);
823	return sysctl_handle_int(oidp, &count, 0, req);
824}
825
826/*
827 * Create a new NLM host.
828 */
829static struct nlm_host *
830nlm_create_host(const char* caller_name)
831{
832	struct nlm_host *host;
833	struct sysctl_oid *oid;
834
835	mtx_assert(&nlm_global_lock, MA_OWNED);
836
837	NLM_DEBUG(1, "NLM: new host %s (sysid %d)\n",
838	    caller_name, nlm_next_sysid);
839	host = malloc(sizeof(struct nlm_host), M_NLM, M_NOWAIT|M_ZERO);
840	if (!host)
841		return (NULL);
842	mtx_init(&host->nh_lock, "nh_lock", NULL, MTX_DEF);
843	host->nh_refs = 1;
844	strlcpy(host->nh_caller_name, caller_name, MAXNAMELEN);
845	host->nh_sysid = nlm_next_sysid++;
846	snprintf(host->nh_sysid_string, sizeof(host->nh_sysid_string),
847		"%d", host->nh_sysid);
848	host->nh_vers = 0;
849	host->nh_state = 0;
850	host->nh_monstate = NLM_UNMONITORED;
851	host->nh_grantcookie = 1;
852	TAILQ_INIT(&host->nh_pending);
853	TAILQ_INIT(&host->nh_granted);
854	TAILQ_INIT(&host->nh_finished);
855	TAILQ_INSERT_TAIL(&nlm_hosts, host, nh_link);
856
857	mtx_unlock(&nlm_global_lock);
858
859	sysctl_ctx_init(&host->nh_sysctl);
860	oid = SYSCTL_ADD_NODE(&host->nh_sysctl,
861	    SYSCTL_STATIC_CHILDREN(_vfs_nlm_sysid),
862	    OID_AUTO, host->nh_sysid_string, CTLFLAG_RD, NULL, "");
863	SYSCTL_ADD_STRING(&host->nh_sysctl, SYSCTL_CHILDREN(oid), OID_AUTO,
864	    "hostname", CTLFLAG_RD, host->nh_caller_name, 0, "");
865	SYSCTL_ADD_INT(&host->nh_sysctl, SYSCTL_CHILDREN(oid), OID_AUTO,
866	    "version", CTLFLAG_RD, &host->nh_vers, 0, "");
867	SYSCTL_ADD_INT(&host->nh_sysctl, SYSCTL_CHILDREN(oid), OID_AUTO,
868	    "monitored", CTLFLAG_RD, &host->nh_monstate, 0, "");
869	SYSCTL_ADD_PROC(&host->nh_sysctl, SYSCTL_CHILDREN(oid), OID_AUTO,
870	    "lock_count", CTLTYPE_INT | CTLFLAG_RD, host, 0,
871	    nlm_host_lock_count_sysctl, "I", "");
872	SYSCTL_ADD_PROC(&host->nh_sysctl, SYSCTL_CHILDREN(oid), OID_AUTO,
873	    "client_lock_count", CTLTYPE_INT | CTLFLAG_RD, host, 0,
874	    nlm_host_client_lock_count_sysctl, "I", "");
875
876	mtx_lock(&nlm_global_lock);
877
878	return (host);
879}
880
881/*
882 * Acquire the next sysid for remote locks not handled by the NLM.
883 */
884uint32_t
885nlm_acquire_next_sysid(void)
886{
887	uint32_t next_sysid;
888
889	mtx_lock(&nlm_global_lock);
890	next_sysid = nlm_next_sysid++;
891	mtx_unlock(&nlm_global_lock);
892	return (next_sysid);
893}
894
895/*
896 * Return non-zero if the address parts of the two sockaddrs are the
897 * same.
898 */
899static int
900nlm_compare_addr(const struct sockaddr *a, const struct sockaddr *b)
901{
902	const struct sockaddr_in *a4, *b4;
903#ifdef INET6
904	const struct sockaddr_in6 *a6, *b6;
905#endif
906
907	if (a->sa_family != b->sa_family)
908		return (FALSE);
909
910	switch (a->sa_family) {
911	case AF_INET:
912		a4 = (const struct sockaddr_in *) a;
913		b4 = (const struct sockaddr_in *) b;
914		return !memcmp(&a4->sin_addr, &b4->sin_addr,
915		    sizeof(a4->sin_addr));
916#ifdef INET6
917	case AF_INET6:
918		a6 = (const struct sockaddr_in6 *) a;
919		b6 = (const struct sockaddr_in6 *) b;
920		return !memcmp(&a6->sin6_addr, &b6->sin6_addr,
921		    sizeof(a6->sin6_addr));
922#endif
923	}
924
925	return (0);
926}
927
928/*
929 * Check for idle hosts and stop monitoring them. We could also free
930 * the host structure here, possibly after a larger timeout but that
931 * would require some care to avoid races with
932 * e.g. nlm_host_lock_count_sysctl.
933 */
934static void
935nlm_check_idle(void)
936{
937	struct nlm_host *host;
938
939	mtx_assert(&nlm_global_lock, MA_OWNED);
940
941	if (time_uptime <= nlm_next_idle_check)
942		return;
943
944	nlm_next_idle_check = time_uptime + NLM_IDLE_PERIOD;
945
946	TAILQ_FOREACH(host, &nlm_hosts, nh_link) {
947		if (host->nh_monstate == NLM_MONITORED
948		    && time_uptime > host->nh_idle_timeout) {
949			mtx_unlock(&nlm_global_lock);
950			if (lf_countlocks(host->nh_sysid) > 0
951			    || lf_countlocks(NLM_SYSID_CLIENT
952				+ host->nh_sysid)) {
953				host->nh_idle_timeout =
954					time_uptime + NLM_IDLE_TIMEOUT;
955				mtx_lock(&nlm_global_lock);
956				continue;
957			}
958			nlm_host_unmonitor(host);
959			mtx_lock(&nlm_global_lock);
960		}
961	}
962}
963
964/*
965 * Search for an existing NLM host that matches the given name
966 * (typically the caller_name element of an nlm4_lock).  If none is
967 * found, create a new host. If 'addr' is non-NULL, record the remote
968 * address of the host so that we can call it back for async
969 * responses. If 'vers' is greater than zero then record the NLM
970 * program version to use to communicate with this client.
971 */
972struct nlm_host *
973nlm_find_host_by_name(const char *name, const struct sockaddr *addr,
974    rpcvers_t vers)
975{
976	struct nlm_host *host;
977
978	mtx_lock(&nlm_global_lock);
979
980	/*
981	 * The remote host is determined by caller_name.
982	 */
983	TAILQ_FOREACH(host, &nlm_hosts, nh_link) {
984		if (!strcmp(host->nh_caller_name, name))
985			break;
986	}
987
988	if (!host) {
989		host = nlm_create_host(name);
990		if (!host) {
991			mtx_unlock(&nlm_global_lock);
992			return (NULL);
993		}
994	}
995	refcount_acquire(&host->nh_refs);
996
997	host->nh_idle_timeout = time_uptime + NLM_IDLE_TIMEOUT;
998
999	/*
1000	 * If we have an address for the host, record it so that we
1001	 * can send async replies etc.
1002	 */
1003	if (addr) {
1004
1005		KASSERT(addr->sa_len < sizeof(struct sockaddr_storage),
1006		    ("Strange remote transport address length"));
1007
1008		/*
1009		 * If we have seen an address before and we currently
1010		 * have an RPC client handle, make sure the address is
1011		 * the same, otherwise discard the client handle.
1012		 */
1013		if (host->nh_addr.ss_len && host->nh_srvrpc.nr_client) {
1014			if (!nlm_compare_addr(
1015				    (struct sockaddr *) &host->nh_addr,
1016				    addr)
1017			    || host->nh_vers != vers) {
1018				CLIENT *client;
1019				mtx_lock(&host->nh_lock);
1020				client = host->nh_srvrpc.nr_client;
1021				host->nh_srvrpc.nr_client = NULL;
1022				mtx_unlock(&host->nh_lock);
1023				if (client) {
1024					CLNT_RELEASE(client);
1025				}
1026			}
1027		}
1028		memcpy(&host->nh_addr, addr, addr->sa_len);
1029		host->nh_vers = vers;
1030	}
1031
1032	nlm_check_idle();
1033
1034	mtx_unlock(&nlm_global_lock);
1035
1036	return (host);
1037}
1038
1039/*
1040 * Search for an existing NLM host that matches the given remote
1041 * address. If none is found, create a new host with the requested
1042 * address and remember 'vers' as the NLM protocol version to use for
1043 * that host.
1044 */
1045struct nlm_host *
1046nlm_find_host_by_addr(const struct sockaddr *addr, int vers)
1047{
1048	/*
1049	 * Fake up a name using inet_ntop. This buffer is
1050	 * large enough for an IPv6 address.
1051	 */
1052	char tmp[sizeof "ffff:ffff:ffff:ffff:ffff:ffff:255.255.255.255"];
1053	struct nlm_host *host;
1054
1055	switch (addr->sa_family) {
1056	case AF_INET:
1057		inet_ntop(AF_INET,
1058		    &((const struct sockaddr_in *) addr)->sin_addr,
1059		    tmp, sizeof tmp);
1060		break;
1061#ifdef INET6
1062	case AF_INET6:
1063		inet_ntop(AF_INET6,
1064		    &((const struct sockaddr_in6 *) addr)->sin6_addr,
1065		    tmp, sizeof tmp);
1066		break;
1067#endif
1068	default:
1069		strcmp(tmp, "<unknown>");
1070	}
1071
1072
1073	mtx_lock(&nlm_global_lock);
1074
1075	/*
1076	 * The remote host is determined by caller_name.
1077	 */
1078	TAILQ_FOREACH(host, &nlm_hosts, nh_link) {
1079		if (nlm_compare_addr(addr,
1080			(const struct sockaddr *) &host->nh_addr))
1081			break;
1082	}
1083
1084	if (!host) {
1085		host = nlm_create_host(tmp);
1086		if (!host) {
1087			mtx_unlock(&nlm_global_lock);
1088			return (NULL);
1089		}
1090		memcpy(&host->nh_addr, addr, addr->sa_len);
1091		host->nh_vers = vers;
1092	}
1093	refcount_acquire(&host->nh_refs);
1094
1095	host->nh_idle_timeout = time_uptime + NLM_IDLE_TIMEOUT;
1096
1097	nlm_check_idle();
1098
1099	mtx_unlock(&nlm_global_lock);
1100
1101	return (host);
1102}
1103
1104/*
1105 * Find the NLM host that matches the value of 'sysid'. If none
1106 * exists, return NULL.
1107 */
1108static struct nlm_host *
1109nlm_find_host_by_sysid(int sysid)
1110{
1111	struct nlm_host *host;
1112
1113	TAILQ_FOREACH(host, &nlm_hosts, nh_link) {
1114		if (host->nh_sysid == sysid) {
1115			refcount_acquire(&host->nh_refs);
1116			return (host);
1117		}
1118	}
1119
1120	return (NULL);
1121}
1122
1123void nlm_host_release(struct nlm_host *host)
1124{
1125	if (refcount_release(&host->nh_refs)) {
1126		/*
1127		 * Free the host
1128		 */
1129		nlm_host_destroy(host);
1130	}
1131}
1132
1133/*
1134 * Unregister this NLM host with the local NSM due to idleness.
1135 */
1136static void
1137nlm_host_unmonitor(struct nlm_host *host)
1138{
1139	mon_id smmonid;
1140	sm_stat_res smstat;
1141	struct timeval timo;
1142	enum clnt_stat stat;
1143
1144	NLM_DEBUG(1, "NLM: unmonitoring %s (sysid %d)\n",
1145	    host->nh_caller_name, host->nh_sysid);
1146
1147	/*
1148	 * We put our assigned system ID value in the priv field to
1149	 * make it simpler to find the host if we are notified of a
1150	 * host restart.
1151	 */
1152	smmonid.mon_name = host->nh_caller_name;
1153	smmonid.my_id.my_name = "localhost";
1154	smmonid.my_id.my_prog = NLM_PROG;
1155	smmonid.my_id.my_vers = NLM_SM;
1156	smmonid.my_id.my_proc = NLM_SM_NOTIFY;
1157
1158	timo.tv_sec = 25;
1159	timo.tv_usec = 0;
1160	stat = CLNT_CALL(nlm_nsm, SM_UNMON,
1161	    (xdrproc_t) xdr_mon, &smmonid,
1162	    (xdrproc_t) xdr_sm_stat, &smstat, timo);
1163
1164	if (stat != RPC_SUCCESS) {
1165		NLM_ERR("Failed to contact local NSM - rpc error %d\n", stat);
1166		return;
1167	}
1168	if (smstat.res_stat == stat_fail) {
1169		NLM_ERR("Local NSM refuses to unmonitor %s\n",
1170		    host->nh_caller_name);
1171		return;
1172	}
1173
1174	host->nh_monstate = NLM_UNMONITORED;
1175}
1176
1177/*
1178 * Register this NLM host with the local NSM so that we can be
1179 * notified if it reboots.
1180 */
1181void
1182nlm_host_monitor(struct nlm_host *host, int state)
1183{
1184	mon smmon;
1185	sm_stat_res smstat;
1186	struct timeval timo;
1187	enum clnt_stat stat;
1188
1189	if (state && !host->nh_state) {
1190		/*
1191		 * This is the first time we have seen an NSM state
1192		 * value for this host. We record it here to help
1193		 * detect host reboots.
1194		 */
1195		host->nh_state = state;
1196		NLM_DEBUG(1, "NLM: host %s (sysid %d) has NSM state %d\n",
1197		    host->nh_caller_name, host->nh_sysid, state);
1198	}
1199
1200	mtx_lock(&host->nh_lock);
1201	if (host->nh_monstate != NLM_UNMONITORED) {
1202		mtx_unlock(&host->nh_lock);
1203		return;
1204	}
1205	host->nh_monstate = NLM_MONITORED;
1206	mtx_unlock(&host->nh_lock);
1207
1208	NLM_DEBUG(1, "NLM: monitoring %s (sysid %d)\n",
1209	    host->nh_caller_name, host->nh_sysid);
1210
1211	/*
1212	 * We put our assigned system ID value in the priv field to
1213	 * make it simpler to find the host if we are notified of a
1214	 * host restart.
1215	 */
1216	smmon.mon_id.mon_name = host->nh_caller_name;
1217	smmon.mon_id.my_id.my_name = "localhost";
1218	smmon.mon_id.my_id.my_prog = NLM_PROG;
1219	smmon.mon_id.my_id.my_vers = NLM_SM;
1220	smmon.mon_id.my_id.my_proc = NLM_SM_NOTIFY;
1221	memcpy(smmon.priv, &host->nh_sysid, sizeof(host->nh_sysid));
1222
1223	timo.tv_sec = 25;
1224	timo.tv_usec = 0;
1225	stat = CLNT_CALL(nlm_nsm, SM_MON,
1226	    (xdrproc_t) xdr_mon, &smmon,
1227	    (xdrproc_t) xdr_sm_stat, &smstat, timo);
1228
1229	if (stat != RPC_SUCCESS) {
1230		NLM_ERR("Failed to contact local NSM - rpc error %d\n", stat);
1231		return;
1232	}
1233	if (smstat.res_stat == stat_fail) {
1234		NLM_ERR("Local NSM refuses to monitor %s\n",
1235		    host->nh_caller_name);
1236		mtx_lock(&host->nh_lock);
1237		host->nh_monstate = NLM_MONITOR_FAILED;
1238		mtx_unlock(&host->nh_lock);
1239		return;
1240	}
1241
1242	host->nh_monstate = NLM_MONITORED;
1243}
1244
1245/*
1246 * Return an RPC client handle that can be used to talk to the NLM
1247 * running on the given host.
1248 */
1249CLIENT *
1250nlm_host_get_rpc(struct nlm_host *host, bool_t isserver)
1251{
1252	struct nlm_rpc *rpc;
1253	CLIENT *client;
1254
1255	mtx_lock(&host->nh_lock);
1256
1257	if (isserver)
1258		rpc = &host->nh_srvrpc;
1259	else
1260		rpc = &host->nh_clntrpc;
1261
1262	/*
1263	 * We can't hold onto RPC handles for too long - the async
1264	 * call/reply protocol used by some NLM clients makes it hard
1265	 * to tell when they change port numbers (e.g. after a
1266	 * reboot). Note that if a client reboots while it isn't
1267	 * holding any locks, it won't bother to notify us. We
1268	 * expire the RPC handles after two minutes.
1269	 */
1270	if (rpc->nr_client && time_uptime > rpc->nr_create_time + 2*60) {
1271		client = rpc->nr_client;
1272		rpc->nr_client = NULL;
1273		mtx_unlock(&host->nh_lock);
1274		CLNT_RELEASE(client);
1275		mtx_lock(&host->nh_lock);
1276	}
1277
1278	if (!rpc->nr_client) {
1279		mtx_unlock(&host->nh_lock);
1280		client = nlm_get_rpc((struct sockaddr *)&host->nh_addr,
1281		    NLM_PROG, host->nh_vers);
1282		mtx_lock(&host->nh_lock);
1283
1284		if (client) {
1285			if (rpc->nr_client) {
1286				mtx_unlock(&host->nh_lock);
1287				CLNT_DESTROY(client);
1288				mtx_lock(&host->nh_lock);
1289			} else {
1290				rpc->nr_client = client;
1291				rpc->nr_create_time = time_uptime;
1292			}
1293		}
1294	}
1295
1296	client = rpc->nr_client;
1297	if (client)
1298		CLNT_ACQUIRE(client);
1299	mtx_unlock(&host->nh_lock);
1300
1301	return (client);
1302
1303}
1304
1305int nlm_host_get_sysid(struct nlm_host *host)
1306{
1307
1308	return (host->nh_sysid);
1309}
1310
1311int
1312nlm_host_get_state(struct nlm_host *host)
1313{
1314
1315	return (host->nh_state);
1316}
1317
1318void *
1319nlm_register_wait_lock(struct nlm4_lock *lock, struct vnode *vp)
1320{
1321	struct nlm_waiting_lock *nw;
1322
1323	nw = malloc(sizeof(struct nlm_waiting_lock), M_NLM, M_WAITOK);
1324	nw->nw_lock = *lock;
1325	memcpy(&nw->nw_fh.fh_bytes, nw->nw_lock.fh.n_bytes,
1326	    nw->nw_lock.fh.n_len);
1327	nw->nw_lock.fh.n_bytes = nw->nw_fh.fh_bytes;
1328	nw->nw_waiting = TRUE;
1329	nw->nw_vp = vp;
1330	mtx_lock(&nlm_global_lock);
1331	TAILQ_INSERT_TAIL(&nlm_waiting_locks, nw, nw_link);
1332	mtx_unlock(&nlm_global_lock);
1333
1334	return nw;
1335}
1336
1337void
1338nlm_deregister_wait_lock(void *handle)
1339{
1340	struct nlm_waiting_lock *nw = handle;
1341
1342	mtx_lock(&nlm_global_lock);
1343	TAILQ_REMOVE(&nlm_waiting_locks, nw, nw_link);
1344	mtx_unlock(&nlm_global_lock);
1345
1346	free(nw, M_NLM);
1347}
1348
1349int
1350nlm_wait_lock(void *handle, int timo)
1351{
1352	struct nlm_waiting_lock *nw = handle;
1353	int error;
1354
1355	/*
1356	 * If the granted message arrived before we got here,
1357	 * nw->nw_waiting will be FALSE - in that case, don't sleep.
1358	 */
1359	mtx_lock(&nlm_global_lock);
1360	error = 0;
1361	if (nw->nw_waiting)
1362		error = msleep(nw, &nlm_global_lock, PCATCH, "nlmlock", timo);
1363	TAILQ_REMOVE(&nlm_waiting_locks, nw, nw_link);
1364	if (error) {
1365		/*
1366		 * The granted message may arrive after the
1367		 * interrupt/timeout but before we manage to lock the
1368		 * mutex. Detect this by examining nw_lock.
1369		 */
1370		if (!nw->nw_waiting)
1371			error = 0;
1372	} else {
1373		/*
1374		 * If nlm_cancel_wait is called, then error will be
1375		 * zero but nw_waiting will still be TRUE. We
1376		 * translate this into EINTR.
1377		 */
1378		if (nw->nw_waiting)
1379			error = EINTR;
1380	}
1381	mtx_unlock(&nlm_global_lock);
1382
1383	free(nw, M_NLM);
1384
1385	return (error);
1386}
1387
1388void
1389nlm_cancel_wait(struct vnode *vp)
1390{
1391	struct nlm_waiting_lock *nw;
1392
1393	mtx_lock(&nlm_global_lock);
1394	TAILQ_FOREACH(nw, &nlm_waiting_locks, nw_link) {
1395		if (nw->nw_vp == vp) {
1396			wakeup(nw);
1397		}
1398	}
1399	mtx_unlock(&nlm_global_lock);
1400}
1401
1402
1403/**********************************************************************/
1404
1405/*
1406 * Syscall interface with userland.
1407 */
1408
1409extern void nlm_prog_0(struct svc_req *rqstp, SVCXPRT *transp);
1410extern void nlm_prog_1(struct svc_req *rqstp, SVCXPRT *transp);
1411extern void nlm_prog_3(struct svc_req *rqstp, SVCXPRT *transp);
1412extern void nlm_prog_4(struct svc_req *rqstp, SVCXPRT *transp);
1413
1414static int
1415nlm_register_services(SVCPOOL *pool, int addr_count, char **addrs)
1416{
1417	static rpcvers_t versions[] = {
1418		NLM_SM, NLM_VERS, NLM_VERSX, NLM_VERS4
1419	};
1420	static void (*dispatchers[])(struct svc_req *, SVCXPRT *) = {
1421		nlm_prog_0, nlm_prog_1, nlm_prog_3, nlm_prog_4
1422	};
1423	static const int version_count = sizeof(versions) / sizeof(versions[0]);
1424
1425	SVCXPRT **xprts;
1426	char netid[16];
1427	char uaddr[128];
1428	struct netconfig *nconf;
1429	int i, j, error;
1430
1431	if (!addr_count) {
1432		NLM_ERR("NLM: no service addresses given - can't start server");
1433		return (EINVAL);
1434	}
1435
1436	xprts = malloc(addr_count * sizeof(SVCXPRT *), M_NLM, M_WAITOK|M_ZERO);
1437	for (i = 0; i < version_count; i++) {
1438		for (j = 0; j < addr_count; j++) {
1439			/*
1440			 * Create transports for the first version and
1441			 * then just register everything else to the
1442			 * same transports.
1443			 */
1444			if (i == 0) {
1445				char *up;
1446
1447				error = copyin(&addrs[2*j], &up,
1448				    sizeof(char*));
1449				if (error)
1450					goto out;
1451				error = copyinstr(up, netid, sizeof(netid),
1452				    NULL);
1453				if (error)
1454					goto out;
1455				error = copyin(&addrs[2*j+1], &up,
1456				    sizeof(char*));
1457				if (error)
1458					goto out;
1459				error = copyinstr(up, uaddr, sizeof(uaddr),
1460				    NULL);
1461				if (error)
1462					goto out;
1463				nconf = getnetconfigent(netid);
1464				if (!nconf) {
1465					NLM_ERR("Can't lookup netid %s\n",
1466					    netid);
1467					error = EINVAL;
1468					goto out;
1469				}
1470				xprts[j] = svc_tp_create(pool, dispatchers[i],
1471				    NLM_PROG, versions[i], uaddr, nconf);
1472				if (!xprts[j]) {
1473					NLM_ERR("NLM: unable to create "
1474					    "(NLM_PROG, %d).\n", versions[i]);
1475					error = EINVAL;
1476					goto out;
1477				}
1478				freenetconfigent(nconf);
1479			} else {
1480				nconf = getnetconfigent(xprts[j]->xp_netid);
1481				rpcb_unset(NLM_PROG, versions[i], nconf);
1482				if (!svc_reg(xprts[j], NLM_PROG, versions[i],
1483					dispatchers[i], nconf)) {
1484					NLM_ERR("NLM: can't register "
1485					    "(NLM_PROG, %d)\n", versions[i]);
1486					error = EINVAL;
1487					goto out;
1488				}
1489			}
1490		}
1491	}
1492	error = 0;
1493out:
1494	for (j = 0; j < addr_count; j++) {
1495		if (xprts[j])
1496			SVC_RELEASE(xprts[j]);
1497	}
1498	free(xprts, M_NLM);
1499	return (error);
1500}
1501
1502/*
1503 * Main server entry point. Contacts the local NSM to get its current
1504 * state and send SM_UNMON_ALL. Registers the NLM services and then
1505 * services requests. Does not return until the server is interrupted
1506 * by a signal.
1507 */
1508static int
1509nlm_server_main(int addr_count, char **addrs)
1510{
1511	struct thread *td = curthread;
1512	int error;
1513	SVCPOOL *pool = NULL;
1514	struct sockopt opt;
1515	int portlow;
1516#ifdef INET6
1517	struct sockaddr_in6 sin6;
1518#endif
1519	struct sockaddr_in sin;
1520	my_id id;
1521	sm_stat smstat;
1522	struct timeval timo;
1523	enum clnt_stat stat;
1524	struct nlm_host *host, *nhost;
1525	struct nlm_waiting_lock *nw;
1526	vop_advlock_t *old_nfs_advlock;
1527	vop_reclaim_t *old_nfs_reclaim;
1528	int v4_used;
1529#ifdef INET6
1530	int v6_used;
1531#endif
1532
1533	if (nlm_socket) {
1534		NLM_ERR("NLM: can't start server - "
1535		    "it appears to be running already\n");
1536		return (EPERM);
1537	}
1538
1539	memset(&opt, 0, sizeof(opt));
1540
1541	nlm_socket = NULL;
1542	error = socreate(AF_INET, &nlm_socket, SOCK_DGRAM, 0,
1543	    td->td_ucred, td);
1544	if (error) {
1545		NLM_ERR("NLM: can't create IPv4 socket - error %d\n", error);
1546		return (error);
1547	}
1548	opt.sopt_dir = SOPT_SET;
1549	opt.sopt_level = IPPROTO_IP;
1550	opt.sopt_name = IP_PORTRANGE;
1551	portlow = IP_PORTRANGE_LOW;
1552	opt.sopt_val = &portlow;
1553	opt.sopt_valsize = sizeof(portlow);
1554	sosetopt(nlm_socket, &opt);
1555
1556#ifdef INET6
1557	nlm_socket6 = NULL;
1558	error = socreate(AF_INET6, &nlm_socket6, SOCK_DGRAM, 0,
1559	    td->td_ucred, td);
1560	if (error) {
1561		NLM_ERR("NLM: can't create IPv6 socket - error %d\n", error);
1562		goto out;
1563		return (error);
1564	}
1565	opt.sopt_dir = SOPT_SET;
1566	opt.sopt_level = IPPROTO_IPV6;
1567	opt.sopt_name = IPV6_PORTRANGE;
1568	portlow = IPV6_PORTRANGE_LOW;
1569	opt.sopt_val = &portlow;
1570	opt.sopt_valsize = sizeof(portlow);
1571	sosetopt(nlm_socket6, &opt);
1572#endif
1573
1574	nlm_auth = authunix_create(curthread->td_ucred);
1575
1576#ifdef INET6
1577	memset(&sin6, 0, sizeof(sin6));
1578	sin6.sin6_len = sizeof(sin6);
1579	sin6.sin6_family = AF_INET6;
1580	sin6.sin6_addr = in6addr_loopback;
1581	nlm_nsm = nlm_get_rpc((struct sockaddr *) &sin6, SM_PROG, SM_VERS);
1582	if (!nlm_nsm) {
1583#endif
1584		memset(&sin, 0, sizeof(sin));
1585		sin.sin_len = sizeof(sin);
1586		sin.sin_family = AF_INET;
1587		sin.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
1588		nlm_nsm = nlm_get_rpc((struct sockaddr *) &sin, SM_PROG,
1589		    SM_VERS);
1590#ifdef INET6
1591	}
1592#endif
1593
1594	if (!nlm_nsm) {
1595		NLM_ERR("Can't start NLM - unable to contact NSM\n");
1596		error = EINVAL;
1597		goto out;
1598	}
1599
1600	pool = svcpool_create("NLM", NULL);
1601
1602	error = nlm_register_services(pool, addr_count, addrs);
1603	if (error)
1604		goto out;
1605
1606	memset(&id, 0, sizeof(id));
1607	id.my_name = "NFS NLM";
1608
1609	timo.tv_sec = 25;
1610	timo.tv_usec = 0;
1611	stat = CLNT_CALL(nlm_nsm, SM_UNMON_ALL,
1612	    (xdrproc_t) xdr_my_id, &id,
1613	    (xdrproc_t) xdr_sm_stat, &smstat, timo);
1614
1615	if (stat != RPC_SUCCESS) {
1616		struct rpc_err err;
1617
1618		CLNT_GETERR(nlm_nsm, &err);
1619		NLM_ERR("NLM: unexpected error contacting NSM, "
1620		    "stat=%d, errno=%d\n", stat, err.re_errno);
1621		error = EINVAL;
1622		goto out;
1623	}
1624
1625	NLM_DEBUG(1, "NLM: local NSM state is %d\n", smstat.state);
1626	nlm_nsm_state = smstat.state;
1627
1628	old_nfs_advlock = nfs_advlock_p;
1629	nfs_advlock_p = nlm_advlock;
1630	old_nfs_reclaim = nfs_reclaim_p;
1631	nfs_reclaim_p = nlm_reclaim;
1632
1633	svc_run(pool);
1634	error = 0;
1635
1636	nfs_advlock_p = old_nfs_advlock;
1637	nfs_reclaim_p = old_nfs_reclaim;
1638
1639out:
1640	if (pool)
1641		svcpool_destroy(pool);
1642
1643	/*
1644	 * We are finished communicating with the NSM.
1645	 */
1646	if (nlm_nsm) {
1647		CLNT_RELEASE(nlm_nsm);
1648		nlm_nsm = NULL;
1649	}
1650
1651	/*
1652	 * Trash all the existing state so that if the server
1653	 * restarts, it gets a clean slate. This is complicated by the
1654	 * possibility that there may be other threads trying to make
1655	 * client locking requests.
1656	 *
1657	 * First we fake a client reboot notification which will
1658	 * cancel any pending async locks and purge remote lock state
1659	 * from the local lock manager. We release the reference from
1660	 * nlm_hosts to the host (which may remove it from the list
1661	 * and free it). After this phase, the only entries in the
1662	 * nlm_host list should be from other threads performing
1663	 * client lock requests. We arrange to defer closing the
1664	 * sockets until the last RPC client handle is released.
1665	 */
1666	v4_used = 0;
1667#ifdef INET6
1668	v6_used = 0;
1669#endif
1670	mtx_lock(&nlm_global_lock);
1671	TAILQ_FOREACH(nw, &nlm_waiting_locks, nw_link) {
1672		wakeup(nw);
1673	}
1674	TAILQ_FOREACH_SAFE(host, &nlm_hosts, nh_link, nhost) {
1675		mtx_unlock(&nlm_global_lock);
1676		nlm_host_notify(host, 0);
1677		nlm_host_release(host);
1678		mtx_lock(&nlm_global_lock);
1679	}
1680	TAILQ_FOREACH_SAFE(host, &nlm_hosts, nh_link, nhost) {
1681		mtx_lock(&host->nh_lock);
1682		if (host->nh_srvrpc.nr_client
1683		    || host->nh_clntrpc.nr_client) {
1684			if (host->nh_addr.ss_family == AF_INET)
1685				v4_used++;
1686#ifdef INET6
1687			if (host->nh_addr.ss_family == AF_INET6)
1688				v6_used++;
1689#endif
1690			/*
1691			 * Note that the rpc over udp code copes
1692			 * correctly with the fact that a socket may
1693			 * be used by many rpc handles.
1694			 */
1695			if (host->nh_srvrpc.nr_client)
1696				CLNT_CONTROL(host->nh_srvrpc.nr_client,
1697				    CLSET_FD_CLOSE, 0);
1698			if (host->nh_clntrpc.nr_client)
1699				CLNT_CONTROL(host->nh_clntrpc.nr_client,
1700				    CLSET_FD_CLOSE, 0);
1701		}
1702		mtx_unlock(&host->nh_lock);
1703	}
1704	mtx_unlock(&nlm_global_lock);
1705
1706	AUTH_DESTROY(nlm_auth);
1707
1708	if (!v4_used)
1709		soclose(nlm_socket);
1710	nlm_socket = NULL;
1711#ifdef INET6
1712	if (!v6_used)
1713		soclose(nlm_socket6);
1714	nlm_socket6 = NULL;
1715#endif
1716
1717	return (error);
1718}
1719
1720int
1721nlm_syscall(struct thread *td, struct nlm_syscall_args *uap)
1722{
1723	int error;
1724
1725#if __FreeBSD_version >= 700000
1726	error = priv_check(td, PRIV_NFS_LOCKD);
1727#else
1728	error = suser(td);
1729#endif
1730	if (error)
1731		return (error);
1732
1733	nlm_debug_level = uap->debug_level;
1734	nlm_grace_threshold = time_uptime + uap->grace_period;
1735	nlm_next_idle_check = time_uptime + NLM_IDLE_PERIOD;
1736
1737	return nlm_server_main(uap->addr_count, uap->addrs);
1738}
1739
1740/**********************************************************************/
1741
1742/*
1743 * NLM implementation details, called from the RPC stubs.
1744 */
1745
1746
1747void
1748nlm_sm_notify(struct nlm_sm_status *argp)
1749{
1750	uint32_t sysid;
1751	struct nlm_host *host;
1752
1753	NLM_DEBUG(3, "nlm_sm_notify(): mon_name = %s\n", argp->mon_name);
1754	memcpy(&sysid, &argp->priv, sizeof(sysid));
1755	host = nlm_find_host_by_sysid(sysid);
1756	if (host) {
1757		nlm_host_notify(host, argp->state);
1758		nlm_host_release(host);
1759	}
1760}
1761
1762static void
1763nlm_convert_to_fhandle_t(fhandle_t *fhp, struct netobj *p)
1764{
1765	memcpy(fhp, p->n_bytes, sizeof(fhandle_t));
1766}
1767
1768struct vfs_state {
1769	struct mount	*vs_mp;
1770	struct vnode	*vs_vp;
1771	int		vs_vfslocked;
1772	int		vs_vnlocked;
1773};
1774
1775static int
1776nlm_get_vfs_state(struct nlm_host *host, struct svc_req *rqstp,
1777    fhandle_t *fhp, struct vfs_state *vs)
1778{
1779	int error, exflags;
1780	struct ucred *cred = NULL, *credanon;
1781
1782	memset(vs, 0, sizeof(*vs));
1783
1784	vs->vs_mp = vfs_getvfs(&fhp->fh_fsid);
1785	if (!vs->vs_mp) {
1786		return (ESTALE);
1787	}
1788	vs->vs_vfslocked = VFS_LOCK_GIANT(vs->vs_mp);
1789
1790	error = VFS_CHECKEXP(vs->vs_mp, (struct sockaddr *)&host->nh_addr,
1791	    &exflags, &credanon, NULL, NULL);
1792	if (error)
1793		goto out;
1794
1795	if (exflags & MNT_EXRDONLY || (vs->vs_mp->mnt_flag & MNT_RDONLY)) {
1796		error = EROFS;
1797		goto out;
1798	}
1799
1800	error = VFS_FHTOVP(vs->vs_mp, &fhp->fh_fid, &vs->vs_vp);
1801	if (error)
1802		goto out;
1803	vs->vs_vnlocked = TRUE;
1804
1805	if (!svc_getcred(rqstp, &cred, NULL)) {
1806		error = EINVAL;
1807		goto out;
1808	}
1809	if (cred->cr_uid == 0 || (exflags & MNT_EXPORTANON)) {
1810		crfree(cred);
1811		cred = credanon;
1812		credanon = NULL;
1813	}
1814
1815	/*
1816	 * Check cred.
1817	 */
1818	error = VOP_ACCESS(vs->vs_vp, VWRITE, cred, curthread);
1819	if (error)
1820		goto out;
1821
1822#if __FreeBSD_version < 800011
1823	VOP_UNLOCK(vs->vs_vp, 0, curthread);
1824#else
1825	VOP_UNLOCK(vs->vs_vp, 0);
1826#endif
1827	vs->vs_vnlocked = FALSE;
1828
1829out:
1830	if (cred)
1831		crfree(cred);
1832	if (credanon)
1833		crfree(credanon);
1834
1835	return (error);
1836}
1837
1838static void
1839nlm_release_vfs_state(struct vfs_state *vs)
1840{
1841
1842	if (vs->vs_vp) {
1843		if (vs->vs_vnlocked)
1844			vput(vs->vs_vp);
1845		else
1846			vrele(vs->vs_vp);
1847	}
1848	if (vs->vs_mp)
1849		vfs_rel(vs->vs_mp);
1850	VFS_UNLOCK_GIANT(vs->vs_vfslocked);
1851}
1852
1853static nlm4_stats
1854nlm_convert_error(int error)
1855{
1856
1857	if (error == ESTALE)
1858		return nlm4_stale_fh;
1859	else if (error == EROFS)
1860		return nlm4_rofs;
1861	else
1862		return nlm4_failed;
1863}
1864
1865int
1866nlm_do_test(nlm4_testargs *argp, nlm4_testres *result, struct svc_req *rqstp,
1867	CLIENT **rpcp)
1868{
1869	fhandle_t fh;
1870	struct vfs_state vs;
1871	struct nlm_host *host, *bhost;
1872	int error, sysid;
1873	struct flock fl;
1874
1875	memset(result, 0, sizeof(*result));
1876	memset(&vs, 0, sizeof(vs));
1877
1878	host = nlm_find_host_by_name(argp->alock.caller_name,
1879	    svc_getrpccaller(rqstp), rqstp->rq_vers);
1880	if (!host) {
1881		result->stat.stat = nlm4_denied_nolocks;
1882		return (ENOMEM);
1883	}
1884
1885	NLM_DEBUG(3, "nlm_do_test(): caller_name = %s (sysid = %d)\n",
1886	    host->nh_caller_name, host->nh_sysid);
1887
1888	nlm_check_expired_locks(host);
1889	sysid = host->nh_sysid;
1890
1891	nlm_convert_to_fhandle_t(&fh, &argp->alock.fh);
1892	nlm_copy_netobj(&result->cookie, &argp->cookie, M_RPC);
1893
1894	if (time_uptime < nlm_grace_threshold) {
1895		result->stat.stat = nlm4_denied_grace_period;
1896		goto out;
1897	}
1898
1899	error = nlm_get_vfs_state(host, rqstp, &fh, &vs);
1900	if (error) {
1901		result->stat.stat = nlm_convert_error(error);
1902		goto out;
1903	}
1904
1905	fl.l_start = argp->alock.l_offset;
1906	fl.l_len = argp->alock.l_len;
1907	fl.l_pid = argp->alock.svid;
1908	fl.l_sysid = sysid;
1909	fl.l_whence = SEEK_SET;
1910	if (argp->exclusive)
1911		fl.l_type = F_WRLCK;
1912	else
1913		fl.l_type = F_RDLCK;
1914	error = VOP_ADVLOCK(vs.vs_vp, NULL, F_GETLK, &fl, F_REMOTE);
1915	if (error) {
1916		result->stat.stat = nlm4_failed;
1917		goto out;
1918	}
1919
1920	if (fl.l_type == F_UNLCK) {
1921		result->stat.stat = nlm4_granted;
1922	} else {
1923		result->stat.stat = nlm4_denied;
1924		result->stat.nlm4_testrply_u.holder.exclusive =
1925			(fl.l_type == F_WRLCK);
1926		result->stat.nlm4_testrply_u.holder.svid = fl.l_pid;
1927		bhost = nlm_find_host_by_sysid(fl.l_sysid);
1928		if (bhost) {
1929			/*
1930			 * We don't have any useful way of recording
1931			 * the value of oh used in the original lock
1932			 * request. Ideally, the test reply would have
1933			 * a space for the owning host's name allowing
1934			 * our caller's NLM to keep track.
1935			 *
1936			 * As far as I can see, Solaris uses an eight
1937			 * byte structure for oh which contains a four
1938			 * byte pid encoded in local byte order and
1939			 * the first four bytes of the host
1940			 * name. Linux uses a variable length string
1941			 * 'pid@hostname' in ascii but doesn't even
1942			 * return that in test replies.
1943			 *
1944			 * For the moment, return nothing in oh
1945			 * (already zero'ed above).
1946			 */
1947			nlm_host_release(bhost);
1948		}
1949		result->stat.nlm4_testrply_u.holder.l_offset = fl.l_start;
1950		result->stat.nlm4_testrply_u.holder.l_len = fl.l_len;
1951	}
1952
1953out:
1954	nlm_release_vfs_state(&vs);
1955	if (rpcp)
1956		*rpcp = nlm_host_get_rpc(host, TRUE);
1957	nlm_host_release(host);
1958	return (0);
1959}
1960
1961int
1962nlm_do_lock(nlm4_lockargs *argp, nlm4_res *result, struct svc_req *rqstp,
1963    bool_t monitor, CLIENT **rpcp)
1964{
1965	fhandle_t fh;
1966	struct vfs_state vs;
1967	struct nlm_host *host;
1968	int error, sysid;
1969	struct flock fl;
1970
1971	memset(result, 0, sizeof(*result));
1972	memset(&vs, 0, sizeof(vs));
1973
1974	host = nlm_find_host_by_name(argp->alock.caller_name,
1975	    svc_getrpccaller(rqstp), rqstp->rq_vers);
1976	if (!host) {
1977		result->stat.stat = nlm4_denied_nolocks;
1978		return (ENOMEM);
1979	}
1980
1981	NLM_DEBUG(3, "nlm_do_lock(): caller_name = %s (sysid = %d)\n",
1982	    host->nh_caller_name, host->nh_sysid);
1983
1984	if (monitor && host->nh_state && argp->state
1985	    && host->nh_state != argp->state) {
1986		/*
1987		 * The host rebooted without telling us. Trash its
1988		 * locks.
1989		 */
1990		nlm_host_notify(host, argp->state);
1991	}
1992
1993	nlm_check_expired_locks(host);
1994	sysid = host->nh_sysid;
1995
1996	nlm_convert_to_fhandle_t(&fh, &argp->alock.fh);
1997	nlm_copy_netobj(&result->cookie, &argp->cookie, M_RPC);
1998
1999	if (time_uptime < nlm_grace_threshold && !argp->reclaim) {
2000		result->stat.stat = nlm4_denied_grace_period;
2001		goto out;
2002	}
2003
2004	error = nlm_get_vfs_state(host, rqstp, &fh, &vs);
2005	if (error) {
2006		result->stat.stat = nlm_convert_error(error);
2007		goto out;
2008	}
2009
2010	fl.l_start = argp->alock.l_offset;
2011	fl.l_len = argp->alock.l_len;
2012	fl.l_pid = argp->alock.svid;
2013	fl.l_sysid = sysid;
2014	fl.l_whence = SEEK_SET;
2015	if (argp->exclusive)
2016		fl.l_type = F_WRLCK;
2017	else
2018		fl.l_type = F_RDLCK;
2019	if (argp->block) {
2020		struct nlm_async_lock *af;
2021		CLIENT *client;
2022		struct nlm_grantcookie cookie;
2023
2024		/*
2025		 * First, make sure we can contact the host's NLM.
2026		 */
2027		client = nlm_host_get_rpc(host, TRUE);
2028		if (!client) {
2029			result->stat.stat = nlm4_failed;
2030			goto out;
2031		}
2032
2033		/*
2034		 * First we need to check and see if there is an
2035		 * existing blocked lock that matches. This could be a
2036		 * badly behaved client or an RPC re-send. If we find
2037		 * one, just return nlm4_blocked.
2038		 */
2039		mtx_lock(&host->nh_lock);
2040		TAILQ_FOREACH(af, &host->nh_pending, af_link) {
2041			if (af->af_fl.l_start == fl.l_start
2042			    && af->af_fl.l_len == fl.l_len
2043			    && af->af_fl.l_pid == fl.l_pid
2044			    && af->af_fl.l_type == fl.l_type) {
2045				break;
2046			}
2047		}
2048		if (!af) {
2049			cookie.ng_sysid = host->nh_sysid;
2050			cookie.ng_cookie = host->nh_grantcookie++;
2051		}
2052		mtx_unlock(&host->nh_lock);
2053		if (af) {
2054			CLNT_RELEASE(client);
2055			result->stat.stat = nlm4_blocked;
2056			goto out;
2057		}
2058
2059		af = malloc(sizeof(struct nlm_async_lock), M_NLM,
2060		    M_WAITOK|M_ZERO);
2061		TASK_INIT(&af->af_task, 0, nlm_lock_callback, af);
2062		af->af_vp = vs.vs_vp;
2063		af->af_fl = fl;
2064		af->af_host = host;
2065		af->af_rpc = client;
2066		/*
2067		 * We use M_RPC here so that we can xdr_free the thing
2068		 * later.
2069		 */
2070		nlm_make_netobj(&af->af_granted.cookie,
2071		    (caddr_t)&cookie, sizeof(cookie), M_RPC);
2072		af->af_granted.exclusive = argp->exclusive;
2073		af->af_granted.alock.caller_name =
2074			strdup(argp->alock.caller_name, M_RPC);
2075		nlm_copy_netobj(&af->af_granted.alock.fh,
2076		    &argp->alock.fh, M_RPC);
2077		nlm_copy_netobj(&af->af_granted.alock.oh,
2078		    &argp->alock.oh, M_RPC);
2079		af->af_granted.alock.svid = argp->alock.svid;
2080		af->af_granted.alock.l_offset = argp->alock.l_offset;
2081		af->af_granted.alock.l_len = argp->alock.l_len;
2082
2083		/*
2084		 * Put the entry on the pending list before calling
2085		 * VOP_ADVLOCKASYNC. We do this in case the lock
2086		 * request was blocked (returning EINPROGRESS) but
2087		 * then granted before we manage to run again. The
2088		 * client may receive the granted message before we
2089		 * send our blocked reply but thats their problem.
2090		 */
2091		mtx_lock(&host->nh_lock);
2092		TAILQ_INSERT_TAIL(&host->nh_pending, af, af_link);
2093		mtx_unlock(&host->nh_lock);
2094
2095		error = VOP_ADVLOCKASYNC(vs.vs_vp, NULL, F_SETLK, &fl, F_REMOTE,
2096		    &af->af_task, &af->af_cookie);
2097
2098		/*
2099		 * If the lock completed synchronously, just free the
2100		 * tracking structure now.
2101		 */
2102		if (error != EINPROGRESS) {
2103			CLNT_RELEASE(af->af_rpc);
2104			mtx_lock(&host->nh_lock);
2105			TAILQ_REMOVE(&host->nh_pending, af, af_link);
2106			mtx_unlock(&host->nh_lock);
2107			xdr_free((xdrproc_t) xdr_nlm4_testargs,
2108			    &af->af_granted);
2109			free(af, M_NLM);
2110		} else {
2111			NLM_DEBUG(2, "NLM: pending async lock %p for %s "
2112			    "(sysid %d)\n", af, host->nh_caller_name, sysid);
2113			/*
2114			 * Don't vrele the vnode just yet - this must
2115			 * wait until either the async callback
2116			 * happens or the lock is cancelled.
2117			 */
2118			vs.vs_vp = NULL;
2119		}
2120	} else {
2121		error = VOP_ADVLOCK(vs.vs_vp, NULL, F_SETLK, &fl, F_REMOTE);
2122	}
2123
2124	if (error) {
2125		if (error == EINPROGRESS) {
2126			result->stat.stat = nlm4_blocked;
2127		} else if (error == EDEADLK) {
2128			result->stat.stat = nlm4_deadlck;
2129		} else if (error == EAGAIN) {
2130			result->stat.stat = nlm4_denied;
2131		} else {
2132			result->stat.stat = nlm4_failed;
2133		}
2134	} else {
2135		if (monitor)
2136			nlm_host_monitor(host, argp->state);
2137		result->stat.stat = nlm4_granted;
2138	}
2139
2140out:
2141	nlm_release_vfs_state(&vs);
2142	if (rpcp)
2143		*rpcp = nlm_host_get_rpc(host, TRUE);
2144	nlm_host_release(host);
2145	return (0);
2146}
2147
2148int
2149nlm_do_cancel(nlm4_cancargs *argp, nlm4_res *result, struct svc_req *rqstp,
2150    CLIENT **rpcp)
2151{
2152	fhandle_t fh;
2153	struct vfs_state vs;
2154	struct nlm_host *host;
2155	int error, sysid;
2156	struct flock fl;
2157	struct nlm_async_lock *af;
2158
2159	memset(result, 0, sizeof(*result));
2160	memset(&vs, 0, sizeof(vs));
2161
2162	host = nlm_find_host_by_name(argp->alock.caller_name,
2163	    svc_getrpccaller(rqstp), rqstp->rq_vers);
2164	if (!host) {
2165		result->stat.stat = nlm4_denied_nolocks;
2166		return (ENOMEM);
2167	}
2168
2169	NLM_DEBUG(3, "nlm_do_cancel(): caller_name = %s (sysid = %d)\n",
2170	    host->nh_caller_name, host->nh_sysid);
2171
2172	nlm_check_expired_locks(host);
2173	sysid = host->nh_sysid;
2174
2175	nlm_convert_to_fhandle_t(&fh, &argp->alock.fh);
2176	nlm_copy_netobj(&result->cookie, &argp->cookie, M_RPC);
2177
2178	if (time_uptime < nlm_grace_threshold) {
2179		result->stat.stat = nlm4_denied_grace_period;
2180		goto out;
2181	}
2182
2183	error = nlm_get_vfs_state(host, rqstp, &fh, &vs);
2184	if (error) {
2185		result->stat.stat = nlm_convert_error(error);
2186		goto out;
2187	}
2188
2189	fl.l_start = argp->alock.l_offset;
2190	fl.l_len = argp->alock.l_len;
2191	fl.l_pid = argp->alock.svid;
2192	fl.l_sysid = sysid;
2193	fl.l_whence = SEEK_SET;
2194	if (argp->exclusive)
2195		fl.l_type = F_WRLCK;
2196	else
2197		fl.l_type = F_RDLCK;
2198
2199	/*
2200	 * First we need to try and find the async lock request - if
2201	 * there isn't one, we give up and return nlm4_denied.
2202	 */
2203	mtx_lock(&host->nh_lock);
2204
2205	TAILQ_FOREACH(af, &host->nh_pending, af_link) {
2206		if (af->af_fl.l_start == fl.l_start
2207		    && af->af_fl.l_len == fl.l_len
2208		    && af->af_fl.l_pid == fl.l_pid
2209		    && af->af_fl.l_type == fl.l_type) {
2210			break;
2211		}
2212	}
2213
2214	if (!af) {
2215		mtx_unlock(&host->nh_lock);
2216		result->stat.stat = nlm4_denied;
2217		goto out;
2218	}
2219
2220	error = nlm_cancel_async_lock(af);
2221
2222	if (error) {
2223		result->stat.stat = nlm4_denied;
2224	} else {
2225		result->stat.stat = nlm4_granted;
2226	}
2227
2228	mtx_unlock(&host->nh_lock);
2229
2230out:
2231	nlm_release_vfs_state(&vs);
2232	if (rpcp)
2233		*rpcp = nlm_host_get_rpc(host, TRUE);
2234	nlm_host_release(host);
2235	return (0);
2236}
2237
2238int
2239nlm_do_unlock(nlm4_unlockargs *argp, nlm4_res *result, struct svc_req *rqstp,
2240    CLIENT **rpcp)
2241{
2242	fhandle_t fh;
2243	struct vfs_state vs;
2244	struct nlm_host *host;
2245	int error, sysid;
2246	struct flock fl;
2247
2248	memset(result, 0, sizeof(*result));
2249	memset(&vs, 0, sizeof(vs));
2250
2251	host = nlm_find_host_by_name(argp->alock.caller_name,
2252	    svc_getrpccaller(rqstp), rqstp->rq_vers);
2253	if (!host) {
2254		result->stat.stat = nlm4_denied_nolocks;
2255		return (ENOMEM);
2256	}
2257
2258	NLM_DEBUG(3, "nlm_do_unlock(): caller_name = %s (sysid = %d)\n",
2259	    host->nh_caller_name, host->nh_sysid);
2260
2261	nlm_check_expired_locks(host);
2262	sysid = host->nh_sysid;
2263
2264	nlm_convert_to_fhandle_t(&fh, &argp->alock.fh);
2265	nlm_copy_netobj(&result->cookie, &argp->cookie, M_RPC);
2266
2267	if (time_uptime < nlm_grace_threshold) {
2268		result->stat.stat = nlm4_denied_grace_period;
2269		goto out;
2270	}
2271
2272	error = nlm_get_vfs_state(host, rqstp, &fh, &vs);
2273	if (error) {
2274		result->stat.stat = nlm_convert_error(error);
2275		goto out;
2276	}
2277
2278	fl.l_start = argp->alock.l_offset;
2279	fl.l_len = argp->alock.l_len;
2280	fl.l_pid = argp->alock.svid;
2281	fl.l_sysid = sysid;
2282	fl.l_whence = SEEK_SET;
2283	fl.l_type = F_UNLCK;
2284	error = VOP_ADVLOCK(vs.vs_vp, NULL, F_UNLCK, &fl, F_REMOTE);
2285
2286	/*
2287	 * Ignore the error - there is no result code for failure,
2288	 * only for grace period.
2289	 */
2290	result->stat.stat = nlm4_granted;
2291
2292out:
2293	nlm_release_vfs_state(&vs);
2294	if (rpcp)
2295		*rpcp = nlm_host_get_rpc(host, TRUE);
2296	nlm_host_release(host);
2297	return (0);
2298}
2299
2300int
2301nlm_do_granted(nlm4_testargs *argp, nlm4_res *result, struct svc_req *rqstp,
2302
2303    CLIENT **rpcp)
2304{
2305	struct nlm_host *host;
2306	struct nlm_waiting_lock *nw;
2307
2308	memset(result, 0, sizeof(*result));
2309
2310	host = nlm_find_host_by_addr(svc_getrpccaller(rqstp), rqstp->rq_vers);
2311	if (!host) {
2312		result->stat.stat = nlm4_denied_nolocks;
2313		return (ENOMEM);
2314	}
2315
2316	nlm_copy_netobj(&result->cookie, &argp->cookie, M_RPC);
2317	result->stat.stat = nlm4_denied;
2318	KFAIL_POINT_CODE(DEBUG_FP, nlm_deny_grant, goto out);
2319
2320	mtx_lock(&nlm_global_lock);
2321	TAILQ_FOREACH(nw, &nlm_waiting_locks, nw_link) {
2322		if (!nw->nw_waiting)
2323			continue;
2324		if (argp->alock.svid == nw->nw_lock.svid
2325		    && argp->alock.l_offset == nw->nw_lock.l_offset
2326		    && argp->alock.l_len == nw->nw_lock.l_len
2327		    && argp->alock.fh.n_len == nw->nw_lock.fh.n_len
2328		    && !memcmp(argp->alock.fh.n_bytes, nw->nw_lock.fh.n_bytes,
2329			nw->nw_lock.fh.n_len)) {
2330			nw->nw_waiting = FALSE;
2331			wakeup(nw);
2332			result->stat.stat = nlm4_granted;
2333			break;
2334		}
2335	}
2336	mtx_unlock(&nlm_global_lock);
2337
2338out:
2339	if (rpcp)
2340		*rpcp = nlm_host_get_rpc(host, TRUE);
2341	nlm_host_release(host);
2342	return (0);
2343}
2344
2345void
2346nlm_do_granted_res(nlm4_res *argp, struct svc_req *rqstp)
2347{
2348	struct nlm_host *host = NULL;
2349	struct nlm_async_lock *af = NULL;
2350	int error;
2351
2352	if (argp->cookie.n_len != sizeof(struct nlm_grantcookie)) {
2353		NLM_DEBUG(1, "NLM: bogus grant cookie");
2354		goto out;
2355	}
2356
2357	host = nlm_find_host_by_sysid(ng_sysid(&argp->cookie));
2358	if (!host) {
2359		NLM_DEBUG(1, "NLM: Unknown host rejected our grant");
2360		goto out;
2361	}
2362
2363	mtx_lock(&host->nh_lock);
2364	TAILQ_FOREACH(af, &host->nh_granted, af_link)
2365	    if (ng_cookie(&argp->cookie) ==
2366		ng_cookie(&af->af_granted.cookie))
2367		    break;
2368	if (af)
2369		TAILQ_REMOVE(&host->nh_granted, af, af_link);
2370	mtx_unlock(&host->nh_lock);
2371
2372	if (!af) {
2373		NLM_DEBUG(1, "NLM: host %s (sysid %d) replied to our grant "
2374		    "with unrecognized cookie %d:%d", host->nh_caller_name,
2375		    host->nh_sysid, ng_sysid(&argp->cookie),
2376		    ng_cookie(&argp->cookie));
2377		goto out;
2378	}
2379
2380	if (argp->stat.stat != nlm4_granted) {
2381		af->af_fl.l_type = F_UNLCK;
2382		error = VOP_ADVLOCK(af->af_vp, NULL, F_UNLCK, &af->af_fl, F_REMOTE);
2383		if (error) {
2384			NLM_DEBUG(1, "NLM: host %s (sysid %d) rejected our grant "
2385			    "and we failed to unlock (%d)", host->nh_caller_name,
2386			    host->nh_sysid, error);
2387			goto out;
2388		}
2389
2390		NLM_DEBUG(5, "NLM: async lock %p rejected by host %s (sysid %d)",
2391		    af, host->nh_caller_name, host->nh_sysid);
2392	} else {
2393		NLM_DEBUG(5, "NLM: async lock %p accepted by host %s (sysid %d)",
2394		    af, host->nh_caller_name, host->nh_sysid);
2395	}
2396
2397 out:
2398	if (af)
2399		nlm_free_async_lock(af);
2400	if (host)
2401		nlm_host_release(host);
2402}
2403
2404void
2405nlm_do_free_all(nlm4_notify *argp)
2406{
2407	struct nlm_host *host, *thost;
2408
2409	TAILQ_FOREACH_SAFE(host, &nlm_hosts, nh_link, thost) {
2410		if (!strcmp(host->nh_caller_name, argp->name))
2411			nlm_host_notify(host, argp->state);
2412	}
2413}
2414
2415/*
2416 * Kernel module glue
2417 */
2418static int
2419nfslockd_modevent(module_t mod, int type, void *data)
2420{
2421
2422	return (0);
2423}
2424static moduledata_t nfslockd_mod = {
2425	"nfslockd",
2426	nfslockd_modevent,
2427	NULL,
2428};
2429DECLARE_MODULE(nfslockd, nfslockd_mod, SI_SUB_VFS, SI_ORDER_ANY);
2430
2431/* So that loader and kldload(2) can find us, wherever we are.. */
2432MODULE_DEPEND(nfslockd, krpc, 1, 1, 1);
2433MODULE_DEPEND(nfslockd, nfs, 1, 1, 1);
2434MODULE_DEPEND(nfslockd, nfslock, 1, 1, 1);
2435MODULE_VERSION(nfslockd, 1);
2436