2 * Copyright (c) 2008 Isilon Inc http://www.isilon.com/
3 * Authors: Doug Rabson <dfr@rabson.org>
4 * Developed with Red Inc: Alfred Perlstein <alfred@freebsd.org>
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 #include "opt_inet6.h"
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
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>
41 #if __FreeBSD_version >= 700000
45 #include <sys/socket.h>
46 #include <sys/socketvar.h>
47 #include <sys/syscall.h>
48 #include <sys/sysctl.h>
49 #include <sys/sysent.h>
50 #include <sys/syslog.h>
51 #include <sys/sysproto.h>
52 #include <sys/systm.h>
53 #include <sys/taskqueue.h>
54 #include <sys/unistd.h>
55 #include <sys/vnode.h>
57 #include <nfs/nfsproto.h>
58 #include <nfs/nfs_lock.h>
60 #include <nlm/nlm_prot.h>
61 #include <nlm/sm_inter.h>
63 #include <rpc/rpc_com.h>
64 #include <rpc/rpcb_prot.h>
66 MALLOC_DEFINE(M_NLM, "NLM", "Network Lock Manager");
69 * If a host is inactive (and holds no locks) for this amount of
70 * seconds, we consider it idle and stop tracking it.
72 #define NLM_IDLE_TIMEOUT 30
75 * We check the host list for idle every few seconds.
77 #define NLM_IDLE_PERIOD 5
80 * We only look for GRANTED_RES messages for a little while.
82 #define NLM_EXPIRE_TIMEOUT 10
85 * Support for sysctl vfs.nlm.sysid
87 static SYSCTL_NODE(_vfs, OID_AUTO, nlm, CTLFLAG_RW, NULL,
88 "Network Lock Manager");
89 static SYSCTL_NODE(_vfs_nlm, OID_AUTO, sysid, CTLFLAG_RW, NULL, "");
94 static int nlm_syscall_offset = SYS_nlm_syscall;
95 static struct sysent nlm_syscall_prev_sysent;
96 #if __FreeBSD_version < 700000
97 static struct sysent nlm_syscall_sysent = {
98 (sizeof(struct nlm_syscall_args) / sizeof(register_t)) | SYF_MPSAFE,
99 (sy_call_t *) nlm_syscall
102 MAKE_SYSENT(nlm_syscall);
104 static bool_t nlm_syscall_registered = FALSE;
107 * Debug level passed in from userland. We also support a sysctl hook
108 * so that it can be changed on a live system.
110 static int nlm_debug_level;
111 SYSCTL_INT(_debug, OID_AUTO, nlm_debug, CTLFLAG_RW, &nlm_debug_level, 0, "");
113 #define NLM_DEBUG(_level, args...) \
115 if (nlm_debug_level >= (_level)) \
116 log(LOG_DEBUG, args); \
118 #define NLM_ERR(args...) \
120 log(LOG_ERR, args); \
124 * Grace period handling. The value of nlm_grace_threshold is the
125 * value of time_uptime after which we are serving requests normally.
127 static time_t nlm_grace_threshold;
130 * We check for idle hosts if time_uptime is greater than
131 * nlm_next_idle_check,
133 static time_t nlm_next_idle_check;
136 * A socket to use for RPC - shared by all IPv4 RPC clients.
138 static struct socket *nlm_socket;
143 * A socket to use for RPC - shared by all IPv6 RPC clients.
145 static struct socket *nlm_socket6;
150 * An RPC client handle that can be used to communicate with the local
153 static CLIENT *nlm_nsm;
156 * An AUTH handle for the server's creds.
158 static AUTH *nlm_auth;
161 * A zero timeval for sending async RPC messages.
163 struct timeval nlm_zero_tv = { 0, 0 };
166 * The local NSM state number
172 * A lock to protect the host list and waiting lock list.
174 static struct mtx nlm_global_lock;
178 * (l) locked by nh_lock
179 * (s) only accessed via server RPC which is single threaded
180 * (g) locked by nlm_global_lock
181 * (c) const until freeing
182 * (a) modified using atomic ops
186 * A pending client-side lock request, stored on the nlm_waiting_locks
189 struct nlm_waiting_lock {
190 TAILQ_ENTRY(nlm_waiting_lock) nw_link; /* (g) */
191 bool_t nw_waiting; /* (g) */
192 nlm4_lock nw_lock; /* (c) */
193 union nfsfh nw_fh; /* (c) */
194 struct vnode *nw_vp; /* (c) */
196 TAILQ_HEAD(nlm_waiting_lock_list, nlm_waiting_lock);
198 struct nlm_waiting_lock_list nlm_waiting_locks; /* (g) */
201 * A pending server-side asynchronous lock request, stored on the
202 * nh_pending list of the NLM host.
204 struct nlm_async_lock {
205 TAILQ_ENTRY(nlm_async_lock) af_link; /* (l) host's list of locks */
206 struct task af_task; /* (c) async callback details */
207 void *af_cookie; /* (l) lock manager cancel token */
208 struct vnode *af_vp; /* (l) vnode to lock */
209 struct flock af_fl; /* (c) lock details */
210 struct nlm_host *af_host; /* (c) host which is locking */
211 CLIENT *af_rpc; /* (c) rpc client to send message */
212 nlm4_testargs af_granted; /* (c) notification details */
213 time_t af_expiretime; /* (c) notification time */
215 TAILQ_HEAD(nlm_async_lock_list, nlm_async_lock);
220 enum nlm_host_state {
228 CLIENT *nr_client; /* (l) RPC client handle */
229 time_t nr_create_time; /* (l) when client was created */
234 volatile u_int nh_refs; /* (a) reference count */
235 TAILQ_ENTRY(nlm_host) nh_link; /* (g) global list of hosts */
236 char nh_caller_name[MAXNAMELEN]; /* (c) printable name of host */
237 uint32_t nh_sysid; /* (c) our allocaed system ID */
238 char nh_sysid_string[10]; /* (c) string rep. of sysid */
239 struct sockaddr_storage nh_addr; /* (s) remote address of host */
240 struct nlm_rpc nh_srvrpc; /* (l) RPC for server replies */
241 struct nlm_rpc nh_clntrpc; /* (l) RPC for client requests */
242 rpcvers_t nh_vers; /* (s) NLM version of host */
243 int nh_state; /* (s) last seen NSM state of host */
244 enum nlm_host_state nh_monstate; /* (l) local NSM monitoring state */
245 time_t nh_idle_timeout; /* (s) Time at which host is idle */
246 struct sysctl_ctx_list nh_sysctl; /* (c) vfs.nlm.sysid nodes */
247 uint32_t nh_grantcookie; /* (l) grant cookie counter */
248 struct nlm_async_lock_list nh_pending; /* (l) pending async locks */
249 struct nlm_async_lock_list nh_granted; /* (l) granted locks */
250 struct nlm_async_lock_list nh_finished; /* (l) finished async locks */
252 TAILQ_HEAD(nlm_host_list, nlm_host);
254 static struct nlm_host_list nlm_hosts; /* (g) */
255 static uint32_t nlm_next_sysid = 1; /* (g) */
257 static void nlm_host_unmonitor(struct nlm_host *);
259 struct nlm_grantcookie {
264 static inline uint32_t
265 ng_sysid(struct netobj *src)
268 return ((struct nlm_grantcookie *)src->n_bytes)->ng_sysid;
271 static inline uint32_t
272 ng_cookie(struct netobj *src)
275 return ((struct nlm_grantcookie *)src->n_bytes)->ng_cookie;
278 /**********************************************************************/
281 * Initialise NLM globals.
284 nlm_init(void *dummy)
288 mtx_init(&nlm_global_lock, "nlm_global_lock", NULL, MTX_DEF);
289 TAILQ_INIT(&nlm_waiting_locks);
290 TAILQ_INIT(&nlm_hosts);
292 error = syscall_register(&nlm_syscall_offset, &nlm_syscall_sysent,
293 &nlm_syscall_prev_sysent);
295 NLM_ERR("Can't register NLM syscall\n");
297 nlm_syscall_registered = TRUE;
299 SYSINIT(nlm_init, SI_SUB_LOCK, SI_ORDER_FIRST, nlm_init, NULL);
302 nlm_uninit(void *dummy)
305 if (nlm_syscall_registered)
306 syscall_deregister(&nlm_syscall_offset,
307 &nlm_syscall_prev_sysent);
309 SYSUNINIT(nlm_uninit, SI_SUB_LOCK, SI_ORDER_FIRST, nlm_uninit, NULL);
312 * Create a netobj from an arbitrary source.
315 nlm_make_netobj(struct netobj *dst, caddr_t src, size_t srcsize,
316 struct malloc_type *type)
319 dst->n_len = srcsize;
320 dst->n_bytes = malloc(srcsize, type, M_WAITOK);
321 memcpy(dst->n_bytes, src, srcsize);
325 * Copy a struct netobj.
328 nlm_copy_netobj(struct netobj *dst, struct netobj *src,
329 struct malloc_type *type)
332 nlm_make_netobj(dst, src->n_bytes, src->n_len, type);
337 * Create an RPC client handle for the given (address,prog,vers)
341 nlm_get_rpc(struct sockaddr *sa, rpcprog_t prog, rpcvers_t vers)
343 char *wchan = "nlmrcv";
344 const char* protofmly;
345 struct sockaddr_storage ss;
351 enum clnt_stat stat = RPC_SUCCESS;
352 int rpcvers = RPCBVERS4;
353 bool_t do_tcp = FALSE;
354 bool_t tryagain = FALSE;
355 struct portmap mapping;
359 * First we need to contact the remote RPCBIND service to find
362 memcpy(&ss, sa, sa->sa_len);
363 switch (ss.ss_family) {
365 ((struct sockaddr_in *)&ss)->sin_port = htons(111);
372 ((struct sockaddr_in6 *)&ss)->sin6_port = htons(111);
380 * Unsupported address family - fail.
385 rpcb = clnt_dg_create(so, (struct sockaddr *)&ss,
386 RPCBPROG, rpcvers, 0, 0);
394 parms.r_netid = "tcp";
396 parms.r_netid = "udp";
401 * Use the default timeout.
410 * Try RPCBIND 4 then 3.
413 stat = CLNT_CALL(rpcb, (rpcprog_t) RPCBPROC_GETADDR,
414 (xdrproc_t) xdr_rpcb, &parms,
415 (xdrproc_t) xdr_wrapstring, &uaddr, timo);
416 if (stat == RPC_SUCCESS) {
418 * We have a reply from the remote RPCBIND - turn it
419 * into an appropriate address and make a new client
420 * that can talk to the remote NLM.
422 * XXX fixup IPv6 scope ID.
425 a = __rpc_uaddr2taddr_af(ss.ss_family, uaddr);
430 memcpy(&ss, a->buf, a->len);
433 xdr_free((xdrproc_t) xdr_wrapstring, &uaddr);
436 if (tryagain || stat == RPC_PROGVERSMISMATCH) {
437 if (rpcvers == RPCBVERS4)
439 else if (rpcvers == RPCBVERS)
441 CLNT_CONTROL(rpcb, CLSET_VERS, &rpcvers);
449 mapping.pm_prog = parms.r_prog;
450 mapping.pm_vers = parms.r_vers;
451 mapping.pm_prot = do_tcp ? IPPROTO_TCP : IPPROTO_UDP;
454 stat = CLNT_CALL(rpcb, (rpcprog_t) PMAPPROC_GETPORT,
455 (xdrproc_t) xdr_portmap, &mapping,
456 (xdrproc_t) xdr_u_short, &port, timo);
458 if (stat == RPC_SUCCESS) {
459 switch (ss.ss_family) {
461 ((struct sockaddr_in *)&ss)->sin_port =
467 ((struct sockaddr_in6 *)&ss)->sin6_port =
475 panic("invalid rpcvers %d", rpcvers);
478 * We may have a positive response from the portmapper, but the NLM
479 * service was not found. Make sure we received a valid port.
481 switch (ss.ss_family) {
483 port = ((struct sockaddr_in *)&ss)->sin_port;
487 port = ((struct sockaddr_in6 *)&ss)->sin6_port;
491 if (stat != RPC_SUCCESS || !port) {
493 * If we were able to talk to rpcbind or portmap, but the udp
494 * variant wasn't available, ask about tcp.
496 * XXX - We could also check for a TCP portmapper, but
497 * if the host is running a portmapper at all, we should be able
498 * to hail it over UDP.
500 if (stat == RPC_SUCCESS && !do_tcp) {
505 /* Otherwise, bad news. */
506 NLM_ERR("NLM: failed to contact remote rpcbind, "
507 "stat = %d, port = %d\n", (int) stat, port);
514 * Destroy the UDP client we used to speak to rpcbind and
515 * recreate as a TCP client.
517 struct netconfig *nconf = NULL;
521 switch (ss.ss_family) {
523 nconf = getnetconfigent("tcp");
527 nconf = getnetconfigent("tcp6");
532 rpcb = clnt_reconnect_create(nconf, (struct sockaddr *)&ss,
534 CLNT_CONTROL(rpcb, CLSET_WAITCHAN, wchan);
535 rpcb->cl_auth = nlm_auth;
539 * Re-use the client we used to speak to rpcbind.
541 CLNT_CONTROL(rpcb, CLSET_SVC_ADDR, &ss);
542 CLNT_CONTROL(rpcb, CLSET_PROG, &prog);
543 CLNT_CONTROL(rpcb, CLSET_VERS, &vers);
544 CLNT_CONTROL(rpcb, CLSET_WAITCHAN, wchan);
545 rpcb->cl_auth = nlm_auth;
552 * This async callback after when an async lock request has been
553 * granted. We notify the host which initiated the request.
556 nlm_lock_callback(void *arg, int pending)
558 struct nlm_async_lock *af = (struct nlm_async_lock *) arg;
559 struct rpc_callextra ext;
561 NLM_DEBUG(2, "NLM: async lock %p for %s (sysid %d) granted,"
562 " cookie %d:%d\n", af, af->af_host->nh_caller_name,
563 af->af_host->nh_sysid, ng_sysid(&af->af_granted.cookie),
564 ng_cookie(&af->af_granted.cookie));
567 * Send the results back to the host.
569 * Note: there is a possible race here with nlm_host_notify
570 * destroying the RPC client. To avoid problems, the first
571 * thing nlm_host_notify does is to cancel pending async lock
574 memset(&ext, 0, sizeof(ext));
575 ext.rc_auth = nlm_auth;
576 if (af->af_host->nh_vers == NLM_VERS4) {
577 nlm4_granted_msg_4(&af->af_granted,
578 NULL, af->af_rpc, &ext, nlm_zero_tv);
581 * Back-convert to legacy protocol
583 nlm_testargs granted;
584 granted.cookie = af->af_granted.cookie;
585 granted.exclusive = af->af_granted.exclusive;
586 granted.alock.caller_name =
587 af->af_granted.alock.caller_name;
588 granted.alock.fh = af->af_granted.alock.fh;
589 granted.alock.oh = af->af_granted.alock.oh;
590 granted.alock.svid = af->af_granted.alock.svid;
591 granted.alock.l_offset =
592 af->af_granted.alock.l_offset;
593 granted.alock.l_len =
594 af->af_granted.alock.l_len;
596 nlm_granted_msg_1(&granted,
597 NULL, af->af_rpc, &ext, nlm_zero_tv);
601 * Move this entry to the nh_granted list.
603 af->af_expiretime = time_uptime + NLM_EXPIRE_TIMEOUT;
604 mtx_lock(&af->af_host->nh_lock);
605 TAILQ_REMOVE(&af->af_host->nh_pending, af, af_link);
606 TAILQ_INSERT_TAIL(&af->af_host->nh_granted, af, af_link);
607 mtx_unlock(&af->af_host->nh_lock);
611 * Free an async lock request. The request must have been removed from
615 nlm_free_async_lock(struct nlm_async_lock *af)
618 * Free an async lock.
621 CLNT_RELEASE(af->af_rpc);
622 xdr_free((xdrproc_t) xdr_nlm4_testargs, &af->af_granted);
629 * Cancel our async request - this must be called with
630 * af->nh_host->nh_lock held. This is slightly complicated by a
631 * potential race with our own callback. If we fail to cancel the
632 * lock, it must already have been granted - we make sure our async
633 * task has completed by calling taskqueue_drain in this case.
636 nlm_cancel_async_lock(struct nlm_async_lock *af)
638 struct nlm_host *host = af->af_host;
641 mtx_assert(&host->nh_lock, MA_OWNED);
643 mtx_unlock(&host->nh_lock);
645 error = VOP_ADVLOCKASYNC(af->af_vp, NULL, F_CANCEL, &af->af_fl,
646 F_REMOTE, NULL, &af->af_cookie);
650 * We failed to cancel - make sure our callback has
651 * completed before we continue.
653 taskqueue_drain(taskqueue_thread, &af->af_task);
656 mtx_lock(&host->nh_lock);
659 NLM_DEBUG(2, "NLM: async lock %p for %s (sysid %d) "
660 "cancelled\n", af, host->nh_caller_name, host->nh_sysid);
663 * Remove from the nh_pending list and free now that
664 * we are safe from the callback.
666 TAILQ_REMOVE(&host->nh_pending, af, af_link);
667 mtx_unlock(&host->nh_lock);
668 nlm_free_async_lock(af);
669 mtx_lock(&host->nh_lock);
676 nlm_check_expired_locks(struct nlm_host *host)
678 struct nlm_async_lock *af;
679 time_t uptime = time_uptime;
681 mtx_lock(&host->nh_lock);
682 while ((af = TAILQ_FIRST(&host->nh_granted)) != NULL
683 && uptime >= af->af_expiretime) {
684 NLM_DEBUG(2, "NLM: async lock %p for %s (sysid %d) expired,"
685 " cookie %d:%d\n", af, af->af_host->nh_caller_name,
686 af->af_host->nh_sysid, ng_sysid(&af->af_granted.cookie),
687 ng_cookie(&af->af_granted.cookie));
688 TAILQ_REMOVE(&host->nh_granted, af, af_link);
689 mtx_unlock(&host->nh_lock);
690 nlm_free_async_lock(af);
691 mtx_lock(&host->nh_lock);
693 while ((af = TAILQ_FIRST(&host->nh_finished)) != NULL) {
694 TAILQ_REMOVE(&host->nh_finished, af, af_link);
695 mtx_unlock(&host->nh_lock);
696 nlm_free_async_lock(af);
697 mtx_lock(&host->nh_lock);
699 mtx_unlock(&host->nh_lock);
703 * Free resources used by a host. This is called after the reference
704 * count has reached zero so it doesn't need to worry about locks.
707 nlm_host_destroy(struct nlm_host *host)
710 mtx_lock(&nlm_global_lock);
711 TAILQ_REMOVE(&nlm_hosts, host, nh_link);
712 mtx_unlock(&nlm_global_lock);
714 if (host->nh_srvrpc.nr_client)
715 CLNT_RELEASE(host->nh_srvrpc.nr_client);
716 if (host->nh_clntrpc.nr_client)
717 CLNT_RELEASE(host->nh_clntrpc.nr_client);
718 mtx_destroy(&host->nh_lock);
719 sysctl_ctx_free(&host->nh_sysctl);
724 * Thread start callback for client lock recovery
727 nlm_client_recovery_start(void *arg)
729 struct nlm_host *host = (struct nlm_host *) arg;
731 NLM_DEBUG(1, "NLM: client lock recovery for %s started\n",
732 host->nh_caller_name);
734 nlm_client_recovery(host);
736 NLM_DEBUG(1, "NLM: client lock recovery for %s completed\n",
737 host->nh_caller_name);
739 host->nh_monstate = NLM_MONITORED;
740 nlm_host_release(host);
746 * This is called when we receive a host state change notification. We
747 * unlock any active locks owned by the host. When rpc.lockd is
748 * shutting down, this function is called with newstate set to zero
749 * which allows us to cancel any pending async locks and clear the
753 nlm_host_notify(struct nlm_host *host, int newstate)
755 struct nlm_async_lock *af;
758 NLM_DEBUG(1, "NLM: host %s (sysid %d) rebooted, new "
759 "state is %d\n", host->nh_caller_name,
760 host->nh_sysid, newstate);
764 * Cancel any pending async locks for this host.
766 mtx_lock(&host->nh_lock);
767 while ((af = TAILQ_FIRST(&host->nh_pending)) != NULL) {
769 * nlm_cancel_async_lock will remove the entry from
770 * nh_pending and free it.
772 nlm_cancel_async_lock(af);
774 mtx_unlock(&host->nh_lock);
775 nlm_check_expired_locks(host);
778 * The host just rebooted - trash its locks.
780 lf_clearremotesys(host->nh_sysid);
781 host->nh_state = newstate;
784 * If we have any remote locks for this host (i.e. it
785 * represents a remote NFS server that our local NFS client
786 * has locks for), start a recovery thread.
789 && host->nh_monstate != NLM_RECOVERING
790 && lf_countlocks(NLM_SYSID_CLIENT | host->nh_sysid) > 0) {
792 host->nh_monstate = NLM_RECOVERING;
793 refcount_acquire(&host->nh_refs);
794 kthread_add(nlm_client_recovery_start, host, curproc, &td, 0, 0,
795 "NFS lock recovery for %s", host->nh_caller_name);
800 * Sysctl handler to count the number of locks for a sysid.
803 nlm_host_lock_count_sysctl(SYSCTL_HANDLER_ARGS)
805 struct nlm_host *host;
808 host = oidp->oid_arg1;
809 count = lf_countlocks(host->nh_sysid);
810 return sysctl_handle_int(oidp, &count, 0, req);
814 * Sysctl handler to count the number of client locks for a sysid.
817 nlm_host_client_lock_count_sysctl(SYSCTL_HANDLER_ARGS)
819 struct nlm_host *host;
822 host = oidp->oid_arg1;
823 count = lf_countlocks(NLM_SYSID_CLIENT | host->nh_sysid);
824 return sysctl_handle_int(oidp, &count, 0, req);
828 * Create a new NLM host.
830 static struct nlm_host *
831 nlm_create_host(const char* caller_name)
833 struct nlm_host *host;
834 struct sysctl_oid *oid;
836 mtx_assert(&nlm_global_lock, MA_OWNED);
838 NLM_DEBUG(1, "NLM: new host %s (sysid %d)\n",
839 caller_name, nlm_next_sysid);
840 host = malloc(sizeof(struct nlm_host), M_NLM, M_NOWAIT|M_ZERO);
843 mtx_init(&host->nh_lock, "nh_lock", NULL, MTX_DEF);
845 strlcpy(host->nh_caller_name, caller_name, MAXNAMELEN);
846 host->nh_sysid = nlm_next_sysid++;
847 snprintf(host->nh_sysid_string, sizeof(host->nh_sysid_string),
848 "%d", host->nh_sysid);
851 host->nh_monstate = NLM_UNMONITORED;
852 host->nh_grantcookie = 1;
853 TAILQ_INIT(&host->nh_pending);
854 TAILQ_INIT(&host->nh_granted);
855 TAILQ_INIT(&host->nh_finished);
856 TAILQ_INSERT_TAIL(&nlm_hosts, host, nh_link);
858 mtx_unlock(&nlm_global_lock);
860 sysctl_ctx_init(&host->nh_sysctl);
861 oid = SYSCTL_ADD_NODE(&host->nh_sysctl,
862 SYSCTL_STATIC_CHILDREN(_vfs_nlm_sysid),
863 OID_AUTO, host->nh_sysid_string, CTLFLAG_RD, NULL, "");
864 SYSCTL_ADD_STRING(&host->nh_sysctl, SYSCTL_CHILDREN(oid), OID_AUTO,
865 "hostname", CTLFLAG_RD, host->nh_caller_name, 0, "");
866 SYSCTL_ADD_UINT(&host->nh_sysctl, SYSCTL_CHILDREN(oid), OID_AUTO,
867 "version", CTLFLAG_RD, &host->nh_vers, 0, "");
868 SYSCTL_ADD_UINT(&host->nh_sysctl, SYSCTL_CHILDREN(oid), OID_AUTO,
869 "monitored", CTLFLAG_RD, &host->nh_monstate, 0, "");
870 SYSCTL_ADD_PROC(&host->nh_sysctl, SYSCTL_CHILDREN(oid), OID_AUTO,
871 "lock_count", CTLTYPE_INT | CTLFLAG_RD, host, 0,
872 nlm_host_lock_count_sysctl, "I", "");
873 SYSCTL_ADD_PROC(&host->nh_sysctl, SYSCTL_CHILDREN(oid), OID_AUTO,
874 "client_lock_count", CTLTYPE_INT | CTLFLAG_RD, host, 0,
875 nlm_host_client_lock_count_sysctl, "I", "");
877 mtx_lock(&nlm_global_lock);
883 * Acquire the next sysid for remote locks not handled by the NLM.
886 nlm_acquire_next_sysid(void)
890 mtx_lock(&nlm_global_lock);
891 next_sysid = nlm_next_sysid++;
892 mtx_unlock(&nlm_global_lock);
897 * Return non-zero if the address parts of the two sockaddrs are the
901 nlm_compare_addr(const struct sockaddr *a, const struct sockaddr *b)
903 const struct sockaddr_in *a4, *b4;
905 const struct sockaddr_in6 *a6, *b6;
908 if (a->sa_family != b->sa_family)
911 switch (a->sa_family) {
913 a4 = (const struct sockaddr_in *) a;
914 b4 = (const struct sockaddr_in *) b;
915 return !memcmp(&a4->sin_addr, &b4->sin_addr,
916 sizeof(a4->sin_addr));
919 a6 = (const struct sockaddr_in6 *) a;
920 b6 = (const struct sockaddr_in6 *) b;
921 return !memcmp(&a6->sin6_addr, &b6->sin6_addr,
922 sizeof(a6->sin6_addr));
930 * Check for idle hosts and stop monitoring them. We could also free
931 * the host structure here, possibly after a larger timeout but that
932 * would require some care to avoid races with
933 * e.g. nlm_host_lock_count_sysctl.
938 struct nlm_host *host;
940 mtx_assert(&nlm_global_lock, MA_OWNED);
942 if (time_uptime <= nlm_next_idle_check)
945 nlm_next_idle_check = time_uptime + NLM_IDLE_PERIOD;
947 TAILQ_FOREACH(host, &nlm_hosts, nh_link) {
948 if (host->nh_monstate == NLM_MONITORED
949 && time_uptime > host->nh_idle_timeout) {
950 mtx_unlock(&nlm_global_lock);
951 if (lf_countlocks(host->nh_sysid) > 0
952 || lf_countlocks(NLM_SYSID_CLIENT
954 host->nh_idle_timeout =
955 time_uptime + NLM_IDLE_TIMEOUT;
956 mtx_lock(&nlm_global_lock);
959 nlm_host_unmonitor(host);
960 mtx_lock(&nlm_global_lock);
966 * Search for an existing NLM host that matches the given name
967 * (typically the caller_name element of an nlm4_lock). If none is
968 * found, create a new host. If 'addr' is non-NULL, record the remote
969 * address of the host so that we can call it back for async
970 * responses. If 'vers' is greater than zero then record the NLM
971 * program version to use to communicate with this client.
974 nlm_find_host_by_name(const char *name, const struct sockaddr *addr,
977 struct nlm_host *host;
979 mtx_lock(&nlm_global_lock);
982 * The remote host is determined by caller_name.
984 TAILQ_FOREACH(host, &nlm_hosts, nh_link) {
985 if (!strcmp(host->nh_caller_name, name))
990 host = nlm_create_host(name);
992 mtx_unlock(&nlm_global_lock);
996 refcount_acquire(&host->nh_refs);
998 host->nh_idle_timeout = time_uptime + NLM_IDLE_TIMEOUT;
1001 * If we have an address for the host, record it so that we
1002 * can send async replies etc.
1006 KASSERT(addr->sa_len < sizeof(struct sockaddr_storage),
1007 ("Strange remote transport address length"));
1010 * If we have seen an address before and we currently
1011 * have an RPC client handle, make sure the address is
1012 * the same, otherwise discard the client handle.
1014 if (host->nh_addr.ss_len && host->nh_srvrpc.nr_client) {
1015 if (!nlm_compare_addr(
1016 (struct sockaddr *) &host->nh_addr,
1018 || host->nh_vers != vers) {
1020 mtx_lock(&host->nh_lock);
1021 client = host->nh_srvrpc.nr_client;
1022 host->nh_srvrpc.nr_client = NULL;
1023 mtx_unlock(&host->nh_lock);
1025 CLNT_RELEASE(client);
1029 memcpy(&host->nh_addr, addr, addr->sa_len);
1030 host->nh_vers = vers;
1035 mtx_unlock(&nlm_global_lock);
1041 * Search for an existing NLM host that matches the given remote
1042 * address. If none is found, create a new host with the requested
1043 * address and remember 'vers' as the NLM protocol version to use for
1047 nlm_find_host_by_addr(const struct sockaddr *addr, int vers)
1050 * Fake up a name using inet_ntop. This buffer is
1051 * large enough for an IPv6 address.
1053 char tmp[sizeof "ffff:ffff:ffff:ffff:ffff:ffff:255.255.255.255"];
1054 struct nlm_host *host;
1056 switch (addr->sa_family) {
1059 &((const struct sockaddr_in *) addr)->sin_addr,
1065 &((const struct sockaddr_in6 *) addr)->sin6_addr,
1070 strcmp(tmp, "<unknown>");
1074 mtx_lock(&nlm_global_lock);
1077 * The remote host is determined by caller_name.
1079 TAILQ_FOREACH(host, &nlm_hosts, nh_link) {
1080 if (nlm_compare_addr(addr,
1081 (const struct sockaddr *) &host->nh_addr))
1086 host = nlm_create_host(tmp);
1088 mtx_unlock(&nlm_global_lock);
1091 memcpy(&host->nh_addr, addr, addr->sa_len);
1092 host->nh_vers = vers;
1094 refcount_acquire(&host->nh_refs);
1096 host->nh_idle_timeout = time_uptime + NLM_IDLE_TIMEOUT;
1100 mtx_unlock(&nlm_global_lock);
1106 * Find the NLM host that matches the value of 'sysid'. If none
1107 * exists, return NULL.
1109 static struct nlm_host *
1110 nlm_find_host_by_sysid(int sysid)
1112 struct nlm_host *host;
1114 TAILQ_FOREACH(host, &nlm_hosts, nh_link) {
1115 if (host->nh_sysid == sysid) {
1116 refcount_acquire(&host->nh_refs);
1124 void nlm_host_release(struct nlm_host *host)
1126 if (refcount_release(&host->nh_refs)) {
1130 nlm_host_destroy(host);
1135 * Unregister this NLM host with the local NSM due to idleness.
1138 nlm_host_unmonitor(struct nlm_host *host)
1142 struct timeval timo;
1143 enum clnt_stat stat;
1145 NLM_DEBUG(1, "NLM: unmonitoring %s (sysid %d)\n",
1146 host->nh_caller_name, host->nh_sysid);
1149 * We put our assigned system ID value in the priv field to
1150 * make it simpler to find the host if we are notified of a
1153 smmonid.mon_name = host->nh_caller_name;
1154 smmonid.my_id.my_name = "localhost";
1155 smmonid.my_id.my_prog = NLM_PROG;
1156 smmonid.my_id.my_vers = NLM_SM;
1157 smmonid.my_id.my_proc = NLM_SM_NOTIFY;
1161 stat = CLNT_CALL(nlm_nsm, SM_UNMON,
1162 (xdrproc_t) xdr_mon, &smmonid,
1163 (xdrproc_t) xdr_sm_stat, &smstat, timo);
1165 if (stat != RPC_SUCCESS) {
1166 NLM_ERR("Failed to contact local NSM - rpc error %d\n", stat);
1169 if (smstat.res_stat == stat_fail) {
1170 NLM_ERR("Local NSM refuses to unmonitor %s\n",
1171 host->nh_caller_name);
1175 host->nh_monstate = NLM_UNMONITORED;
1179 * Register this NLM host with the local NSM so that we can be
1180 * notified if it reboots.
1183 nlm_host_monitor(struct nlm_host *host, int state)
1187 struct timeval timo;
1188 enum clnt_stat stat;
1190 if (state && !host->nh_state) {
1192 * This is the first time we have seen an NSM state
1193 * value for this host. We record it here to help
1194 * detect host reboots.
1196 host->nh_state = state;
1197 NLM_DEBUG(1, "NLM: host %s (sysid %d) has NSM state %d\n",
1198 host->nh_caller_name, host->nh_sysid, state);
1201 mtx_lock(&host->nh_lock);
1202 if (host->nh_monstate != NLM_UNMONITORED) {
1203 mtx_unlock(&host->nh_lock);
1206 host->nh_monstate = NLM_MONITORED;
1207 mtx_unlock(&host->nh_lock);
1209 NLM_DEBUG(1, "NLM: monitoring %s (sysid %d)\n",
1210 host->nh_caller_name, host->nh_sysid);
1213 * We put our assigned system ID value in the priv field to
1214 * make it simpler to find the host if we are notified of a
1217 smmon.mon_id.mon_name = host->nh_caller_name;
1218 smmon.mon_id.my_id.my_name = "localhost";
1219 smmon.mon_id.my_id.my_prog = NLM_PROG;
1220 smmon.mon_id.my_id.my_vers = NLM_SM;
1221 smmon.mon_id.my_id.my_proc = NLM_SM_NOTIFY;
1222 memcpy(smmon.priv, &host->nh_sysid, sizeof(host->nh_sysid));
1226 stat = CLNT_CALL(nlm_nsm, SM_MON,
1227 (xdrproc_t) xdr_mon, &smmon,
1228 (xdrproc_t) xdr_sm_stat, &smstat, timo);
1230 if (stat != RPC_SUCCESS) {
1231 NLM_ERR("Failed to contact local NSM - rpc error %d\n", stat);
1234 if (smstat.res_stat == stat_fail) {
1235 NLM_ERR("Local NSM refuses to monitor %s\n",
1236 host->nh_caller_name);
1237 mtx_lock(&host->nh_lock);
1238 host->nh_monstate = NLM_MONITOR_FAILED;
1239 mtx_unlock(&host->nh_lock);
1243 host->nh_monstate = NLM_MONITORED;
1247 * Return an RPC client handle that can be used to talk to the NLM
1248 * running on the given host.
1251 nlm_host_get_rpc(struct nlm_host *host, bool_t isserver)
1253 struct nlm_rpc *rpc;
1256 mtx_lock(&host->nh_lock);
1259 rpc = &host->nh_srvrpc;
1261 rpc = &host->nh_clntrpc;
1264 * We can't hold onto RPC handles for too long - the async
1265 * call/reply protocol used by some NLM clients makes it hard
1266 * to tell when they change port numbers (e.g. after a
1267 * reboot). Note that if a client reboots while it isn't
1268 * holding any locks, it won't bother to notify us. We
1269 * expire the RPC handles after two minutes.
1271 if (rpc->nr_client && time_uptime > rpc->nr_create_time + 2*60) {
1272 client = rpc->nr_client;
1273 rpc->nr_client = NULL;
1274 mtx_unlock(&host->nh_lock);
1275 CLNT_RELEASE(client);
1276 mtx_lock(&host->nh_lock);
1279 if (!rpc->nr_client) {
1280 mtx_unlock(&host->nh_lock);
1281 client = nlm_get_rpc((struct sockaddr *)&host->nh_addr,
1282 NLM_PROG, host->nh_vers);
1283 mtx_lock(&host->nh_lock);
1286 if (rpc->nr_client) {
1287 mtx_unlock(&host->nh_lock);
1288 CLNT_DESTROY(client);
1289 mtx_lock(&host->nh_lock);
1291 rpc->nr_client = client;
1292 rpc->nr_create_time = time_uptime;
1297 client = rpc->nr_client;
1299 CLNT_ACQUIRE(client);
1300 mtx_unlock(&host->nh_lock);
1306 int nlm_host_get_sysid(struct nlm_host *host)
1309 return (host->nh_sysid);
1313 nlm_host_get_state(struct nlm_host *host)
1316 return (host->nh_state);
1320 nlm_register_wait_lock(struct nlm4_lock *lock, struct vnode *vp)
1322 struct nlm_waiting_lock *nw;
1324 nw = malloc(sizeof(struct nlm_waiting_lock), M_NLM, M_WAITOK);
1325 nw->nw_lock = *lock;
1326 memcpy(&nw->nw_fh.fh_bytes, nw->nw_lock.fh.n_bytes,
1327 nw->nw_lock.fh.n_len);
1328 nw->nw_lock.fh.n_bytes = nw->nw_fh.fh_bytes;
1329 nw->nw_waiting = TRUE;
1331 mtx_lock(&nlm_global_lock);
1332 TAILQ_INSERT_TAIL(&nlm_waiting_locks, nw, nw_link);
1333 mtx_unlock(&nlm_global_lock);
1339 nlm_deregister_wait_lock(void *handle)
1341 struct nlm_waiting_lock *nw = handle;
1343 mtx_lock(&nlm_global_lock);
1344 TAILQ_REMOVE(&nlm_waiting_locks, nw, nw_link);
1345 mtx_unlock(&nlm_global_lock);
1351 nlm_wait_lock(void *handle, int timo)
1353 struct nlm_waiting_lock *nw = handle;
1357 * If the granted message arrived before we got here,
1358 * nw->nw_waiting will be FALSE - in that case, don't sleep.
1360 mtx_lock(&nlm_global_lock);
1363 error = msleep(nw, &nlm_global_lock, PCATCH, "nlmlock", timo);
1364 TAILQ_REMOVE(&nlm_waiting_locks, nw, nw_link);
1367 * The granted message may arrive after the
1368 * interrupt/timeout but before we manage to lock the
1369 * mutex. Detect this by examining nw_lock.
1371 if (!nw->nw_waiting)
1375 * If nlm_cancel_wait is called, then error will be
1376 * zero but nw_waiting will still be TRUE. We
1377 * translate this into EINTR.
1382 mtx_unlock(&nlm_global_lock);
1390 nlm_cancel_wait(struct vnode *vp)
1392 struct nlm_waiting_lock *nw;
1394 mtx_lock(&nlm_global_lock);
1395 TAILQ_FOREACH(nw, &nlm_waiting_locks, nw_link) {
1396 if (nw->nw_vp == vp) {
1400 mtx_unlock(&nlm_global_lock);
1404 /**********************************************************************/
1407 * Syscall interface with userland.
1410 extern void nlm_prog_0(struct svc_req *rqstp, SVCXPRT *transp);
1411 extern void nlm_prog_1(struct svc_req *rqstp, SVCXPRT *transp);
1412 extern void nlm_prog_3(struct svc_req *rqstp, SVCXPRT *transp);
1413 extern void nlm_prog_4(struct svc_req *rqstp, SVCXPRT *transp);
1416 nlm_register_services(SVCPOOL *pool, int addr_count, char **addrs)
1418 static rpcvers_t versions[] = {
1419 NLM_SM, NLM_VERS, NLM_VERSX, NLM_VERS4
1421 static void (*dispatchers[])(struct svc_req *, SVCXPRT *) = {
1422 nlm_prog_0, nlm_prog_1, nlm_prog_3, nlm_prog_4
1424 static const int version_count = sizeof(versions) / sizeof(versions[0]);
1429 struct netconfig *nconf;
1433 NLM_ERR("NLM: no service addresses given - can't start server");
1437 xprts = malloc(addr_count * sizeof(SVCXPRT *), M_NLM, M_WAITOK|M_ZERO);
1438 for (i = 0; i < version_count; i++) {
1439 for (j = 0; j < addr_count; j++) {
1441 * Create transports for the first version and
1442 * then just register everything else to the
1448 error = copyin(&addrs[2*j], &up,
1452 error = copyinstr(up, netid, sizeof(netid),
1456 error = copyin(&addrs[2*j+1], &up,
1460 error = copyinstr(up, uaddr, sizeof(uaddr),
1464 nconf = getnetconfigent(netid);
1466 NLM_ERR("Can't lookup netid %s\n",
1471 xprts[j] = svc_tp_create(pool, dispatchers[i],
1472 NLM_PROG, versions[i], uaddr, nconf);
1474 NLM_ERR("NLM: unable to create "
1475 "(NLM_PROG, %d).\n", versions[i]);
1479 freenetconfigent(nconf);
1481 nconf = getnetconfigent(xprts[j]->xp_netid);
1482 rpcb_unset(NLM_PROG, versions[i], nconf);
1483 if (!svc_reg(xprts[j], NLM_PROG, versions[i],
1484 dispatchers[i], nconf)) {
1485 NLM_ERR("NLM: can't register "
1486 "(NLM_PROG, %d)\n", versions[i]);
1495 for (j = 0; j < addr_count; j++) {
1497 SVC_RELEASE(xprts[j]);
1504 * Main server entry point. Contacts the local NSM to get its current
1505 * state and send SM_UNMON_ALL. Registers the NLM services and then
1506 * services requests. Does not return until the server is interrupted
1510 nlm_server_main(int addr_count, char **addrs)
1512 struct thread *td = curthread;
1514 SVCPOOL *pool = NULL;
1518 struct sockaddr_in6 sin6;
1520 struct sockaddr_in sin;
1523 struct timeval timo;
1524 enum clnt_stat stat;
1525 struct nlm_host *host, *nhost;
1526 struct nlm_waiting_lock *nw;
1527 vop_advlock_t *old_nfs_advlock;
1528 vop_reclaim_t *old_nfs_reclaim;
1535 NLM_ERR("NLM: can't start server - "
1536 "it appears to be running already\n");
1540 memset(&opt, 0, sizeof(opt));
1543 error = socreate(AF_INET, &nlm_socket, SOCK_DGRAM, 0,
1546 NLM_ERR("NLM: can't create IPv4 socket - error %d\n", error);
1549 opt.sopt_dir = SOPT_SET;
1550 opt.sopt_level = IPPROTO_IP;
1551 opt.sopt_name = IP_PORTRANGE;
1552 portlow = IP_PORTRANGE_LOW;
1553 opt.sopt_val = &portlow;
1554 opt.sopt_valsize = sizeof(portlow);
1555 sosetopt(nlm_socket, &opt);
1559 error = socreate(AF_INET6, &nlm_socket6, SOCK_DGRAM, 0,
1562 NLM_ERR("NLM: can't create IPv6 socket - error %d\n", error);
1566 opt.sopt_dir = SOPT_SET;
1567 opt.sopt_level = IPPROTO_IPV6;
1568 opt.sopt_name = IPV6_PORTRANGE;
1569 portlow = IPV6_PORTRANGE_LOW;
1570 opt.sopt_val = &portlow;
1571 opt.sopt_valsize = sizeof(portlow);
1572 sosetopt(nlm_socket6, &opt);
1575 nlm_auth = authunix_create(curthread->td_ucred);
1578 memset(&sin6, 0, sizeof(sin6));
1579 sin6.sin6_len = sizeof(sin6);
1580 sin6.sin6_family = AF_INET6;
1581 sin6.sin6_addr = in6addr_loopback;
1582 nlm_nsm = nlm_get_rpc((struct sockaddr *) &sin6, SM_PROG, SM_VERS);
1585 memset(&sin, 0, sizeof(sin));
1586 sin.sin_len = sizeof(sin);
1587 sin.sin_family = AF_INET;
1588 sin.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
1589 nlm_nsm = nlm_get_rpc((struct sockaddr *) &sin, SM_PROG,
1596 NLM_ERR("Can't start NLM - unable to contact NSM\n");
1601 pool = svcpool_create("NLM", NULL);
1603 error = nlm_register_services(pool, addr_count, addrs);
1607 memset(&id, 0, sizeof(id));
1608 id.my_name = "NFS NLM";
1612 stat = CLNT_CALL(nlm_nsm, SM_UNMON_ALL,
1613 (xdrproc_t) xdr_my_id, &id,
1614 (xdrproc_t) xdr_sm_stat, &smstat, timo);
1616 if (stat != RPC_SUCCESS) {
1619 CLNT_GETERR(nlm_nsm, &err);
1620 NLM_ERR("NLM: unexpected error contacting NSM, "
1621 "stat=%d, errno=%d\n", stat, err.re_errno);
1626 NLM_DEBUG(1, "NLM: local NSM state is %d\n", smstat.state);
1627 nlm_nsm_state = smstat.state;
1629 old_nfs_advlock = nfs_advlock_p;
1630 nfs_advlock_p = nlm_advlock;
1631 old_nfs_reclaim = nfs_reclaim_p;
1632 nfs_reclaim_p = nlm_reclaim;
1637 nfs_advlock_p = old_nfs_advlock;
1638 nfs_reclaim_p = old_nfs_reclaim;
1642 svcpool_destroy(pool);
1645 * We are finished communicating with the NSM.
1648 CLNT_RELEASE(nlm_nsm);
1653 * Trash all the existing state so that if the server
1654 * restarts, it gets a clean slate. This is complicated by the
1655 * possibility that there may be other threads trying to make
1656 * client locking requests.
1658 * First we fake a client reboot notification which will
1659 * cancel any pending async locks and purge remote lock state
1660 * from the local lock manager. We release the reference from
1661 * nlm_hosts to the host (which may remove it from the list
1662 * and free it). After this phase, the only entries in the
1663 * nlm_host list should be from other threads performing
1664 * client lock requests. We arrange to defer closing the
1665 * sockets until the last RPC client handle is released.
1671 mtx_lock(&nlm_global_lock);
1672 TAILQ_FOREACH(nw, &nlm_waiting_locks, nw_link) {
1675 TAILQ_FOREACH_SAFE(host, &nlm_hosts, nh_link, nhost) {
1676 mtx_unlock(&nlm_global_lock);
1677 nlm_host_notify(host, 0);
1678 nlm_host_release(host);
1679 mtx_lock(&nlm_global_lock);
1681 TAILQ_FOREACH_SAFE(host, &nlm_hosts, nh_link, nhost) {
1682 mtx_lock(&host->nh_lock);
1683 if (host->nh_srvrpc.nr_client
1684 || host->nh_clntrpc.nr_client) {
1685 if (host->nh_addr.ss_family == AF_INET)
1688 if (host->nh_addr.ss_family == AF_INET6)
1692 * Note that the rpc over udp code copes
1693 * correctly with the fact that a socket may
1694 * be used by many rpc handles.
1696 if (host->nh_srvrpc.nr_client)
1697 CLNT_CONTROL(host->nh_srvrpc.nr_client,
1699 if (host->nh_clntrpc.nr_client)
1700 CLNT_CONTROL(host->nh_clntrpc.nr_client,
1703 mtx_unlock(&host->nh_lock);
1705 mtx_unlock(&nlm_global_lock);
1707 AUTH_DESTROY(nlm_auth);
1710 soclose(nlm_socket);
1714 soclose(nlm_socket6);
1722 sys_nlm_syscall(struct thread *td, struct nlm_syscall_args *uap)
1726 #if __FreeBSD_version >= 700000
1727 error = priv_check(td, PRIV_NFS_LOCKD);
1734 nlm_debug_level = uap->debug_level;
1735 nlm_grace_threshold = time_uptime + uap->grace_period;
1736 nlm_next_idle_check = time_uptime + NLM_IDLE_PERIOD;
1738 return nlm_server_main(uap->addr_count, uap->addrs);
1741 /**********************************************************************/
1744 * NLM implementation details, called from the RPC stubs.
1749 nlm_sm_notify(struct nlm_sm_status *argp)
1752 struct nlm_host *host;
1754 NLM_DEBUG(3, "nlm_sm_notify(): mon_name = %s\n", argp->mon_name);
1755 memcpy(&sysid, &argp->priv, sizeof(sysid));
1756 host = nlm_find_host_by_sysid(sysid);
1758 nlm_host_notify(host, argp->state);
1759 nlm_host_release(host);
1764 nlm_convert_to_fhandle_t(fhandle_t *fhp, struct netobj *p)
1766 memcpy(fhp, p->n_bytes, sizeof(fhandle_t));
1770 struct mount *vs_mp;
1771 struct vnode *vs_vp;
1777 nlm_get_vfs_state(struct nlm_host *host, struct svc_req *rqstp,
1778 fhandle_t *fhp, struct vfs_state *vs, accmode_t accmode)
1781 struct ucred *cred = NULL, *credanon = NULL;
1783 memset(vs, 0, sizeof(*vs));
1785 vs->vs_mp = vfs_getvfs(&fhp->fh_fsid);
1789 vs->vs_vfslocked = VFS_LOCK_GIANT(vs->vs_mp);
1791 /* accmode == 0 means don't check, since it is an unlock. */
1793 error = VFS_CHECKEXP(vs->vs_mp,
1794 (struct sockaddr *)&host->nh_addr, &exflags, &credanon,
1799 if (exflags & MNT_EXRDONLY ||
1800 (vs->vs_mp->mnt_flag & MNT_RDONLY)) {
1806 error = VFS_FHTOVP(vs->vs_mp, &fhp->fh_fid, LK_EXCLUSIVE, &vs->vs_vp);
1809 vs->vs_vnlocked = TRUE;
1812 if (!svc_getcred(rqstp, &cred, NULL)) {
1816 if (cred->cr_uid == 0 || (exflags & MNT_EXPORTANON)) {
1825 error = VOP_ACCESS(vs->vs_vp, accmode, cred, curthread);
1827 * If this failed and accmode != VWRITE, try again with
1828 * VWRITE to maintain backwards compatibility with the
1829 * old code that always used VWRITE.
1831 if (error != 0 && accmode != VWRITE)
1832 error = VOP_ACCESS(vs->vs_vp, VWRITE, cred, curthread);
1837 #if __FreeBSD_version < 800011
1838 VOP_UNLOCK(vs->vs_vp, 0, curthread);
1840 VOP_UNLOCK(vs->vs_vp, 0);
1842 vs->vs_vnlocked = FALSE;
1854 nlm_release_vfs_state(struct vfs_state *vs)
1858 if (vs->vs_vnlocked)
1865 VFS_UNLOCK_GIANT(vs->vs_vfslocked);
1869 nlm_convert_error(int error)
1872 if (error == ESTALE)
1873 return nlm4_stale_fh;
1874 else if (error == EROFS)
1881 nlm_do_test(nlm4_testargs *argp, nlm4_testres *result, struct svc_req *rqstp,
1885 struct vfs_state vs;
1886 struct nlm_host *host, *bhost;
1891 memset(result, 0, sizeof(*result));
1892 memset(&vs, 0, sizeof(vs));
1894 host = nlm_find_host_by_name(argp->alock.caller_name,
1895 svc_getrpccaller(rqstp), rqstp->rq_vers);
1897 result->stat.stat = nlm4_denied_nolocks;
1901 NLM_DEBUG(3, "nlm_do_test(): caller_name = %s (sysid = %d)\n",
1902 host->nh_caller_name, host->nh_sysid);
1904 nlm_check_expired_locks(host);
1905 sysid = host->nh_sysid;
1907 nlm_convert_to_fhandle_t(&fh, &argp->alock.fh);
1908 nlm_copy_netobj(&result->cookie, &argp->cookie, M_RPC);
1910 if (time_uptime < nlm_grace_threshold) {
1911 result->stat.stat = nlm4_denied_grace_period;
1915 accmode = argp->exclusive ? VWRITE : VREAD;
1916 error = nlm_get_vfs_state(host, rqstp, &fh, &vs, accmode);
1918 result->stat.stat = nlm_convert_error(error);
1922 fl.l_start = argp->alock.l_offset;
1923 fl.l_len = argp->alock.l_len;
1924 fl.l_pid = argp->alock.svid;
1926 fl.l_whence = SEEK_SET;
1927 if (argp->exclusive)
1928 fl.l_type = F_WRLCK;
1930 fl.l_type = F_RDLCK;
1931 error = VOP_ADVLOCK(vs.vs_vp, NULL, F_GETLK, &fl, F_REMOTE);
1933 result->stat.stat = nlm4_failed;
1937 if (fl.l_type == F_UNLCK) {
1938 result->stat.stat = nlm4_granted;
1940 result->stat.stat = nlm4_denied;
1941 result->stat.nlm4_testrply_u.holder.exclusive =
1942 (fl.l_type == F_WRLCK);
1943 result->stat.nlm4_testrply_u.holder.svid = fl.l_pid;
1944 bhost = nlm_find_host_by_sysid(fl.l_sysid);
1947 * We don't have any useful way of recording
1948 * the value of oh used in the original lock
1949 * request. Ideally, the test reply would have
1950 * a space for the owning host's name allowing
1951 * our caller's NLM to keep track.
1953 * As far as I can see, Solaris uses an eight
1954 * byte structure for oh which contains a four
1955 * byte pid encoded in local byte order and
1956 * the first four bytes of the host
1957 * name. Linux uses a variable length string
1958 * 'pid@hostname' in ascii but doesn't even
1959 * return that in test replies.
1961 * For the moment, return nothing in oh
1962 * (already zero'ed above).
1964 nlm_host_release(bhost);
1966 result->stat.nlm4_testrply_u.holder.l_offset = fl.l_start;
1967 result->stat.nlm4_testrply_u.holder.l_len = fl.l_len;
1971 nlm_release_vfs_state(&vs);
1973 *rpcp = nlm_host_get_rpc(host, TRUE);
1974 nlm_host_release(host);
1979 nlm_do_lock(nlm4_lockargs *argp, nlm4_res *result, struct svc_req *rqstp,
1980 bool_t monitor, CLIENT **rpcp)
1983 struct vfs_state vs;
1984 struct nlm_host *host;
1989 memset(result, 0, sizeof(*result));
1990 memset(&vs, 0, sizeof(vs));
1992 host = nlm_find_host_by_name(argp->alock.caller_name,
1993 svc_getrpccaller(rqstp), rqstp->rq_vers);
1995 result->stat.stat = nlm4_denied_nolocks;
1999 NLM_DEBUG(3, "nlm_do_lock(): caller_name = %s (sysid = %d)\n",
2000 host->nh_caller_name, host->nh_sysid);
2002 if (monitor && host->nh_state && argp->state
2003 && host->nh_state != argp->state) {
2005 * The host rebooted without telling us. Trash its
2008 nlm_host_notify(host, argp->state);
2011 nlm_check_expired_locks(host);
2012 sysid = host->nh_sysid;
2014 nlm_convert_to_fhandle_t(&fh, &argp->alock.fh);
2015 nlm_copy_netobj(&result->cookie, &argp->cookie, M_RPC);
2017 if (time_uptime < nlm_grace_threshold && !argp->reclaim) {
2018 result->stat.stat = nlm4_denied_grace_period;
2022 accmode = argp->exclusive ? VWRITE : VREAD;
2023 error = nlm_get_vfs_state(host, rqstp, &fh, &vs, accmode);
2025 result->stat.stat = nlm_convert_error(error);
2029 fl.l_start = argp->alock.l_offset;
2030 fl.l_len = argp->alock.l_len;
2031 fl.l_pid = argp->alock.svid;
2033 fl.l_whence = SEEK_SET;
2034 if (argp->exclusive)
2035 fl.l_type = F_WRLCK;
2037 fl.l_type = F_RDLCK;
2039 struct nlm_async_lock *af;
2041 struct nlm_grantcookie cookie;
2044 * First, make sure we can contact the host's NLM.
2046 client = nlm_host_get_rpc(host, TRUE);
2048 result->stat.stat = nlm4_failed;
2053 * First we need to check and see if there is an
2054 * existing blocked lock that matches. This could be a
2055 * badly behaved client or an RPC re-send. If we find
2056 * one, just return nlm4_blocked.
2058 mtx_lock(&host->nh_lock);
2059 TAILQ_FOREACH(af, &host->nh_pending, af_link) {
2060 if (af->af_fl.l_start == fl.l_start
2061 && af->af_fl.l_len == fl.l_len
2062 && af->af_fl.l_pid == fl.l_pid
2063 && af->af_fl.l_type == fl.l_type) {
2068 cookie.ng_sysid = host->nh_sysid;
2069 cookie.ng_cookie = host->nh_grantcookie++;
2071 mtx_unlock(&host->nh_lock);
2073 CLNT_RELEASE(client);
2074 result->stat.stat = nlm4_blocked;
2078 af = malloc(sizeof(struct nlm_async_lock), M_NLM,
2080 TASK_INIT(&af->af_task, 0, nlm_lock_callback, af);
2081 af->af_vp = vs.vs_vp;
2084 af->af_rpc = client;
2086 * We use M_RPC here so that we can xdr_free the thing
2089 nlm_make_netobj(&af->af_granted.cookie,
2090 (caddr_t)&cookie, sizeof(cookie), M_RPC);
2091 af->af_granted.exclusive = argp->exclusive;
2092 af->af_granted.alock.caller_name =
2093 strdup(argp->alock.caller_name, M_RPC);
2094 nlm_copy_netobj(&af->af_granted.alock.fh,
2095 &argp->alock.fh, M_RPC);
2096 nlm_copy_netobj(&af->af_granted.alock.oh,
2097 &argp->alock.oh, M_RPC);
2098 af->af_granted.alock.svid = argp->alock.svid;
2099 af->af_granted.alock.l_offset = argp->alock.l_offset;
2100 af->af_granted.alock.l_len = argp->alock.l_len;
2103 * Put the entry on the pending list before calling
2104 * VOP_ADVLOCKASYNC. We do this in case the lock
2105 * request was blocked (returning EINPROGRESS) but
2106 * then granted before we manage to run again. The
2107 * client may receive the granted message before we
2108 * send our blocked reply but thats their problem.
2110 mtx_lock(&host->nh_lock);
2111 TAILQ_INSERT_TAIL(&host->nh_pending, af, af_link);
2112 mtx_unlock(&host->nh_lock);
2114 error = VOP_ADVLOCKASYNC(vs.vs_vp, NULL, F_SETLK, &fl, F_REMOTE,
2115 &af->af_task, &af->af_cookie);
2118 * If the lock completed synchronously, just free the
2119 * tracking structure now.
2121 if (error != EINPROGRESS) {
2122 CLNT_RELEASE(af->af_rpc);
2123 mtx_lock(&host->nh_lock);
2124 TAILQ_REMOVE(&host->nh_pending, af, af_link);
2125 mtx_unlock(&host->nh_lock);
2126 xdr_free((xdrproc_t) xdr_nlm4_testargs,
2130 NLM_DEBUG(2, "NLM: pending async lock %p for %s "
2131 "(sysid %d)\n", af, host->nh_caller_name, sysid);
2133 * Don't vrele the vnode just yet - this must
2134 * wait until either the async callback
2135 * happens or the lock is cancelled.
2140 error = VOP_ADVLOCK(vs.vs_vp, NULL, F_SETLK, &fl, F_REMOTE);
2144 if (error == EINPROGRESS) {
2145 result->stat.stat = nlm4_blocked;
2146 } else if (error == EDEADLK) {
2147 result->stat.stat = nlm4_deadlck;
2148 } else if (error == EAGAIN) {
2149 result->stat.stat = nlm4_denied;
2151 result->stat.stat = nlm4_failed;
2155 nlm_host_monitor(host, argp->state);
2156 result->stat.stat = nlm4_granted;
2160 nlm_release_vfs_state(&vs);
2162 *rpcp = nlm_host_get_rpc(host, TRUE);
2163 nlm_host_release(host);
2168 nlm_do_cancel(nlm4_cancargs *argp, nlm4_res *result, struct svc_req *rqstp,
2172 struct vfs_state vs;
2173 struct nlm_host *host;
2176 struct nlm_async_lock *af;
2178 memset(result, 0, sizeof(*result));
2179 memset(&vs, 0, sizeof(vs));
2181 host = nlm_find_host_by_name(argp->alock.caller_name,
2182 svc_getrpccaller(rqstp), rqstp->rq_vers);
2184 result->stat.stat = nlm4_denied_nolocks;
2188 NLM_DEBUG(3, "nlm_do_cancel(): caller_name = %s (sysid = %d)\n",
2189 host->nh_caller_name, host->nh_sysid);
2191 nlm_check_expired_locks(host);
2192 sysid = host->nh_sysid;
2194 nlm_convert_to_fhandle_t(&fh, &argp->alock.fh);
2195 nlm_copy_netobj(&result->cookie, &argp->cookie, M_RPC);
2197 if (time_uptime < nlm_grace_threshold) {
2198 result->stat.stat = nlm4_denied_grace_period;
2202 error = nlm_get_vfs_state(host, rqstp, &fh, &vs, (accmode_t)0);
2204 result->stat.stat = nlm_convert_error(error);
2208 fl.l_start = argp->alock.l_offset;
2209 fl.l_len = argp->alock.l_len;
2210 fl.l_pid = argp->alock.svid;
2212 fl.l_whence = SEEK_SET;
2213 if (argp->exclusive)
2214 fl.l_type = F_WRLCK;
2216 fl.l_type = F_RDLCK;
2219 * First we need to try and find the async lock request - if
2220 * there isn't one, we give up and return nlm4_denied.
2222 mtx_lock(&host->nh_lock);
2224 TAILQ_FOREACH(af, &host->nh_pending, af_link) {
2225 if (af->af_fl.l_start == fl.l_start
2226 && af->af_fl.l_len == fl.l_len
2227 && af->af_fl.l_pid == fl.l_pid
2228 && af->af_fl.l_type == fl.l_type) {
2234 mtx_unlock(&host->nh_lock);
2235 result->stat.stat = nlm4_denied;
2239 error = nlm_cancel_async_lock(af);
2242 result->stat.stat = nlm4_denied;
2244 result->stat.stat = nlm4_granted;
2247 mtx_unlock(&host->nh_lock);
2250 nlm_release_vfs_state(&vs);
2252 *rpcp = nlm_host_get_rpc(host, TRUE);
2253 nlm_host_release(host);
2258 nlm_do_unlock(nlm4_unlockargs *argp, nlm4_res *result, struct svc_req *rqstp,
2262 struct vfs_state vs;
2263 struct nlm_host *host;
2267 memset(result, 0, sizeof(*result));
2268 memset(&vs, 0, sizeof(vs));
2270 host = nlm_find_host_by_name(argp->alock.caller_name,
2271 svc_getrpccaller(rqstp), rqstp->rq_vers);
2273 result->stat.stat = nlm4_denied_nolocks;
2277 NLM_DEBUG(3, "nlm_do_unlock(): caller_name = %s (sysid = %d)\n",
2278 host->nh_caller_name, host->nh_sysid);
2280 nlm_check_expired_locks(host);
2281 sysid = host->nh_sysid;
2283 nlm_convert_to_fhandle_t(&fh, &argp->alock.fh);
2284 nlm_copy_netobj(&result->cookie, &argp->cookie, M_RPC);
2286 if (time_uptime < nlm_grace_threshold) {
2287 result->stat.stat = nlm4_denied_grace_period;
2291 error = nlm_get_vfs_state(host, rqstp, &fh, &vs, (accmode_t)0);
2293 result->stat.stat = nlm_convert_error(error);
2297 fl.l_start = argp->alock.l_offset;
2298 fl.l_len = argp->alock.l_len;
2299 fl.l_pid = argp->alock.svid;
2301 fl.l_whence = SEEK_SET;
2302 fl.l_type = F_UNLCK;
2303 error = VOP_ADVLOCK(vs.vs_vp, NULL, F_UNLCK, &fl, F_REMOTE);
2306 * Ignore the error - there is no result code for failure,
2307 * only for grace period.
2309 result->stat.stat = nlm4_granted;
2312 nlm_release_vfs_state(&vs);
2314 *rpcp = nlm_host_get_rpc(host, TRUE);
2315 nlm_host_release(host);
2320 nlm_do_granted(nlm4_testargs *argp, nlm4_res *result, struct svc_req *rqstp,
2324 struct nlm_host *host;
2325 struct nlm_waiting_lock *nw;
2327 memset(result, 0, sizeof(*result));
2329 host = nlm_find_host_by_addr(svc_getrpccaller(rqstp), rqstp->rq_vers);
2331 result->stat.stat = nlm4_denied_nolocks;
2335 nlm_copy_netobj(&result->cookie, &argp->cookie, M_RPC);
2336 result->stat.stat = nlm4_denied;
2337 KFAIL_POINT_CODE(DEBUG_FP, nlm_deny_grant, goto out);
2339 mtx_lock(&nlm_global_lock);
2340 TAILQ_FOREACH(nw, &nlm_waiting_locks, nw_link) {
2341 if (!nw->nw_waiting)
2343 if (argp->alock.svid == nw->nw_lock.svid
2344 && argp->alock.l_offset == nw->nw_lock.l_offset
2345 && argp->alock.l_len == nw->nw_lock.l_len
2346 && argp->alock.fh.n_len == nw->nw_lock.fh.n_len
2347 && !memcmp(argp->alock.fh.n_bytes, nw->nw_lock.fh.n_bytes,
2348 nw->nw_lock.fh.n_len)) {
2349 nw->nw_waiting = FALSE;
2351 result->stat.stat = nlm4_granted;
2355 mtx_unlock(&nlm_global_lock);
2359 *rpcp = nlm_host_get_rpc(host, TRUE);
2360 nlm_host_release(host);
2365 nlm_do_granted_res(nlm4_res *argp, struct svc_req *rqstp)
2367 struct nlm_host *host = NULL;
2368 struct nlm_async_lock *af = NULL;
2371 if (argp->cookie.n_len != sizeof(struct nlm_grantcookie)) {
2372 NLM_DEBUG(1, "NLM: bogus grant cookie");
2376 host = nlm_find_host_by_sysid(ng_sysid(&argp->cookie));
2378 NLM_DEBUG(1, "NLM: Unknown host rejected our grant");
2382 mtx_lock(&host->nh_lock);
2383 TAILQ_FOREACH(af, &host->nh_granted, af_link)
2384 if (ng_cookie(&argp->cookie) ==
2385 ng_cookie(&af->af_granted.cookie))
2388 TAILQ_REMOVE(&host->nh_granted, af, af_link);
2389 mtx_unlock(&host->nh_lock);
2392 NLM_DEBUG(1, "NLM: host %s (sysid %d) replied to our grant "
2393 "with unrecognized cookie %d:%d", host->nh_caller_name,
2394 host->nh_sysid, ng_sysid(&argp->cookie),
2395 ng_cookie(&argp->cookie));
2399 if (argp->stat.stat != nlm4_granted) {
2400 af->af_fl.l_type = F_UNLCK;
2401 error = VOP_ADVLOCK(af->af_vp, NULL, F_UNLCK, &af->af_fl, F_REMOTE);
2403 NLM_DEBUG(1, "NLM: host %s (sysid %d) rejected our grant "
2404 "and we failed to unlock (%d)", host->nh_caller_name,
2405 host->nh_sysid, error);
2409 NLM_DEBUG(5, "NLM: async lock %p rejected by host %s (sysid %d)",
2410 af, host->nh_caller_name, host->nh_sysid);
2412 NLM_DEBUG(5, "NLM: async lock %p accepted by host %s (sysid %d)",
2413 af, host->nh_caller_name, host->nh_sysid);
2418 nlm_free_async_lock(af);
2420 nlm_host_release(host);
2424 nlm_do_free_all(nlm4_notify *argp)
2426 struct nlm_host *host, *thost;
2428 TAILQ_FOREACH_SAFE(host, &nlm_hosts, nh_link, thost) {
2429 if (!strcmp(host->nh_caller_name, argp->name))
2430 nlm_host_notify(host, argp->state);
2435 * Kernel module glue
2438 nfslockd_modevent(module_t mod, int type, void *data)
2443 static moduledata_t nfslockd_mod = {
2448 DECLARE_MODULE(nfslockd, nfslockd_mod, SI_SUB_VFS, SI_ORDER_ANY);
2450 /* So that loader and kldload(2) can find us, wherever we are.. */
2451 MODULE_DEPEND(nfslockd, krpc, 1, 1, 1);
2452 MODULE_DEPEND(nfslockd, nfslock, 1, 1, 1);
2453 MODULE_VERSION(nfslockd, 1);