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 SYSCTL_NODE(_vfs, OID_AUTO, nlm, CTLFLAG_RW, NULL, "Network Lock Manager");
88 SYSCTL_NODE(_vfs_nlm, OID_AUTO, sysid, CTLFLAG_RW, NULL, "");
93 static int nlm_syscall_offset = SYS_nlm_syscall;
94 static struct sysent nlm_syscall_prev_sysent;
95 #if __FreeBSD_version < 700000
96 static struct sysent nlm_syscall_sysent = {
97 (sizeof(struct nlm_syscall_args) / sizeof(register_t)) | SYF_MPSAFE,
98 (sy_call_t *) nlm_syscall
101 MAKE_SYSENT(nlm_syscall);
103 static bool_t nlm_syscall_registered = FALSE;
106 * Debug level passed in from userland. We also support a sysctl hook
107 * so that it can be changed on a live system.
109 static int nlm_debug_level;
110 SYSCTL_INT(_debug, OID_AUTO, nlm_debug, CTLFLAG_RW, &nlm_debug_level, 0, "");
112 #define NLM_DEBUG(_level, args...) \
114 if (nlm_debug_level >= (_level)) \
115 log(LOG_DEBUG, args); \
117 #define NLM_ERR(args...) \
119 log(LOG_ERR, args); \
123 * Grace period handling. The value of nlm_grace_threshold is the
124 * value of time_uptime after which we are serving requests normally.
126 static time_t nlm_grace_threshold;
129 * We check for idle hosts if time_uptime is greater than
130 * nlm_next_idle_check,
132 static time_t nlm_next_idle_check;
135 * A socket to use for RPC - shared by all IPv4 RPC clients.
137 static struct socket *nlm_socket;
142 * A socket to use for RPC - shared by all IPv6 RPC clients.
144 static struct socket *nlm_socket6;
149 * An RPC client handle that can be used to communicate with the local
152 static CLIENT *nlm_nsm;
155 * An AUTH handle for the server's creds.
157 static AUTH *nlm_auth;
160 * A zero timeval for sending async RPC messages.
162 struct timeval nlm_zero_tv = { 0, 0 };
165 * The local NSM state number
171 * A lock to protect the host list and waiting lock list.
173 static struct mtx nlm_global_lock;
177 * (l) locked by nh_lock
178 * (s) only accessed via server RPC which is single threaded
179 * (g) locked by nlm_global_lock
180 * (c) const until freeing
181 * (a) modified using atomic ops
185 * A pending client-side lock request, stored on the nlm_waiting_locks
188 struct nlm_waiting_lock {
189 TAILQ_ENTRY(nlm_waiting_lock) nw_link; /* (g) */
190 bool_t nw_waiting; /* (g) */
191 nlm4_lock nw_lock; /* (c) */
192 union nfsfh nw_fh; /* (c) */
193 struct vnode *nw_vp; /* (c) */
195 TAILQ_HEAD(nlm_waiting_lock_list, nlm_waiting_lock);
197 struct nlm_waiting_lock_list nlm_waiting_locks; /* (g) */
200 * A pending server-side asynchronous lock request, stored on the
201 * nh_pending list of the NLM host.
203 struct nlm_async_lock {
204 TAILQ_ENTRY(nlm_async_lock) af_link; /* (l) host's list of locks */
205 struct task af_task; /* (c) async callback details */
206 void *af_cookie; /* (l) lock manager cancel token */
207 struct vnode *af_vp; /* (l) vnode to lock */
208 struct flock af_fl; /* (c) lock details */
209 struct nlm_host *af_host; /* (c) host which is locking */
210 CLIENT *af_rpc; /* (c) rpc client to send message */
211 nlm4_testargs af_granted; /* (c) notification details */
212 time_t af_expiretime; /* (c) notification time */
214 TAILQ_HEAD(nlm_async_lock_list, nlm_async_lock);
219 enum nlm_host_state {
227 CLIENT *nr_client; /* (l) RPC client handle */
228 time_t nr_create_time; /* (l) when client was created */
233 volatile u_int nh_refs; /* (a) reference count */
234 TAILQ_ENTRY(nlm_host) nh_link; /* (g) global list of hosts */
235 char nh_caller_name[MAXNAMELEN]; /* (c) printable name of host */
236 uint32_t nh_sysid; /* (c) our allocaed system ID */
237 char nh_sysid_string[10]; /* (c) string rep. of sysid */
238 struct sockaddr_storage nh_addr; /* (s) remote address of host */
239 struct nlm_rpc nh_srvrpc; /* (l) RPC for server replies */
240 struct nlm_rpc nh_clntrpc; /* (l) RPC for client requests */
241 rpcvers_t nh_vers; /* (s) NLM version of host */
242 int nh_state; /* (s) last seen NSM state of host */
243 enum nlm_host_state nh_monstate; /* (l) local NSM monitoring state */
244 time_t nh_idle_timeout; /* (s) Time at which host is idle */
245 struct sysctl_ctx_list nh_sysctl; /* (c) vfs.nlm.sysid nodes */
246 uint32_t nh_grantcookie; /* (l) grant cookie counter */
247 struct nlm_async_lock_list nh_pending; /* (l) pending async locks */
248 struct nlm_async_lock_list nh_granted; /* (l) granted locks */
249 struct nlm_async_lock_list nh_finished; /* (l) finished async locks */
251 TAILQ_HEAD(nlm_host_list, nlm_host);
253 static struct nlm_host_list nlm_hosts; /* (g) */
254 static uint32_t nlm_next_sysid = 1; /* (g) */
256 static void nlm_host_unmonitor(struct nlm_host *);
258 struct nlm_grantcookie {
263 static inline uint32_t
264 ng_sysid(struct netobj *src)
267 return ((struct nlm_grantcookie *)src->n_bytes)->ng_sysid;
270 static inline uint32_t
271 ng_cookie(struct netobj *src)
274 return ((struct nlm_grantcookie *)src->n_bytes)->ng_cookie;
277 /**********************************************************************/
280 * Initialise NLM globals.
283 nlm_init(void *dummy)
287 mtx_init(&nlm_global_lock, "nlm_global_lock", NULL, MTX_DEF);
288 TAILQ_INIT(&nlm_waiting_locks);
289 TAILQ_INIT(&nlm_hosts);
291 error = syscall_register(&nlm_syscall_offset, &nlm_syscall_sysent,
292 &nlm_syscall_prev_sysent);
294 NLM_ERR("Can't register NLM syscall\n");
296 nlm_syscall_registered = TRUE;
298 SYSINIT(nlm_init, SI_SUB_LOCK, SI_ORDER_FIRST, nlm_init, NULL);
301 nlm_uninit(void *dummy)
304 if (nlm_syscall_registered)
305 syscall_deregister(&nlm_syscall_offset,
306 &nlm_syscall_prev_sysent);
308 SYSUNINIT(nlm_uninit, SI_SUB_LOCK, SI_ORDER_FIRST, nlm_uninit, NULL);
311 * Create a netobj from an arbitrary source.
314 nlm_make_netobj(struct netobj *dst, caddr_t src, size_t srcsize,
315 struct malloc_type *type)
318 dst->n_len = srcsize;
319 dst->n_bytes = malloc(srcsize, type, M_WAITOK);
320 memcpy(dst->n_bytes, src, srcsize);
324 * Copy a struct netobj.
327 nlm_copy_netobj(struct netobj *dst, struct netobj *src,
328 struct malloc_type *type)
331 nlm_make_netobj(dst, src->n_bytes, src->n_len, type);
336 * Create an RPC client handle for the given (address,prog,vers)
340 nlm_get_rpc(struct sockaddr *sa, rpcprog_t prog, rpcvers_t vers)
342 char *wchan = "nlmrcv";
343 const char* protofmly;
344 struct sockaddr_storage ss;
350 enum clnt_stat stat = RPC_SUCCESS;
351 int rpcvers = RPCBVERS4;
352 bool_t do_tcp = FALSE;
353 bool_t tryagain = FALSE;
354 struct portmap mapping;
358 * First we need to contact the remote RPCBIND service to find
361 memcpy(&ss, sa, sa->sa_len);
362 switch (ss.ss_family) {
364 ((struct sockaddr_in *)&ss)->sin_port = htons(111);
371 ((struct sockaddr_in6 *)&ss)->sin6_port = htons(111);
379 * Unsupported address family - fail.
384 rpcb = clnt_dg_create(so, (struct sockaddr *)&ss,
385 RPCBPROG, rpcvers, 0, 0);
393 parms.r_netid = "tcp";
395 parms.r_netid = "udp";
400 * Use the default timeout.
409 * Try RPCBIND 4 then 3.
412 stat = CLNT_CALL(rpcb, (rpcprog_t) RPCBPROC_GETADDR,
413 (xdrproc_t) xdr_rpcb, &parms,
414 (xdrproc_t) xdr_wrapstring, &uaddr, timo);
415 if (stat == RPC_SUCCESS) {
417 * We have a reply from the remote RPCBIND - turn it
418 * into an appropriate address and make a new client
419 * that can talk to the remote NLM.
421 * XXX fixup IPv6 scope ID.
424 a = __rpc_uaddr2taddr_af(ss.ss_family, uaddr);
429 memcpy(&ss, a->buf, a->len);
432 xdr_free((xdrproc_t) xdr_wrapstring, &uaddr);
435 if (tryagain || stat == RPC_PROGVERSMISMATCH) {
436 if (rpcvers == RPCBVERS4)
438 else if (rpcvers == RPCBVERS)
440 CLNT_CONTROL(rpcb, CLSET_VERS, &rpcvers);
448 mapping.pm_prog = parms.r_prog;
449 mapping.pm_vers = parms.r_vers;
450 mapping.pm_prot = do_tcp ? IPPROTO_TCP : IPPROTO_UDP;
453 stat = CLNT_CALL(rpcb, (rpcprog_t) PMAPPROC_GETPORT,
454 (xdrproc_t) xdr_portmap, &mapping,
455 (xdrproc_t) xdr_u_short, &port, timo);
457 if (stat == RPC_SUCCESS) {
458 switch (ss.ss_family) {
460 ((struct sockaddr_in *)&ss)->sin_port =
466 ((struct sockaddr_in6 *)&ss)->sin6_port =
474 panic("invalid rpcvers %d", rpcvers);
477 * We may have a positive response from the portmapper, but the NLM
478 * service was not found. Make sure we received a valid port.
480 switch (ss.ss_family) {
482 port = ((struct sockaddr_in *)&ss)->sin_port;
486 port = ((struct sockaddr_in6 *)&ss)->sin6_port;
490 if (stat != RPC_SUCCESS || !port) {
492 * If we were able to talk to rpcbind or portmap, but the udp
493 * variant wasn't available, ask about tcp.
495 * XXX - We could also check for a TCP portmapper, but
496 * if the host is running a portmapper at all, we should be able
497 * to hail it over UDP.
499 if (stat == RPC_SUCCESS && !do_tcp) {
504 /* Otherwise, bad news. */
505 NLM_ERR("NLM: failed to contact remote rpcbind, "
506 "stat = %d, port = %d\n", (int) stat, port);
513 * Destroy the UDP client we used to speak to rpcbind and
514 * recreate as a TCP client.
516 struct netconfig *nconf = NULL;
520 switch (ss.ss_family) {
522 nconf = getnetconfigent("tcp");
526 nconf = getnetconfigent("tcp6");
531 rpcb = clnt_reconnect_create(nconf, (struct sockaddr *)&ss,
533 CLNT_CONTROL(rpcb, CLSET_WAITCHAN, wchan);
534 rpcb->cl_auth = nlm_auth;
538 * Re-use the client we used to speak to rpcbind.
540 CLNT_CONTROL(rpcb, CLSET_SVC_ADDR, &ss);
541 CLNT_CONTROL(rpcb, CLSET_PROG, &prog);
542 CLNT_CONTROL(rpcb, CLSET_VERS, &vers);
543 CLNT_CONTROL(rpcb, CLSET_WAITCHAN, wchan);
544 rpcb->cl_auth = nlm_auth;
551 * This async callback after when an async lock request has been
552 * granted. We notify the host which initiated the request.
555 nlm_lock_callback(void *arg, int pending)
557 struct nlm_async_lock *af = (struct nlm_async_lock *) arg;
558 struct rpc_callextra ext;
560 NLM_DEBUG(2, "NLM: async lock %p for %s (sysid %d) granted,"
561 " cookie %d:%d\n", af, af->af_host->nh_caller_name,
562 af->af_host->nh_sysid, ng_sysid(&af->af_granted.cookie),
563 ng_cookie(&af->af_granted.cookie));
566 * Send the results back to the host.
568 * Note: there is a possible race here with nlm_host_notify
569 * destroying the RPC client. To avoid problems, the first
570 * thing nlm_host_notify does is to cancel pending async lock
573 memset(&ext, 0, sizeof(ext));
574 ext.rc_auth = nlm_auth;
575 if (af->af_host->nh_vers == NLM_VERS4) {
576 nlm4_granted_msg_4(&af->af_granted,
577 NULL, af->af_rpc, &ext, nlm_zero_tv);
580 * Back-convert to legacy protocol
582 nlm_testargs granted;
583 granted.cookie = af->af_granted.cookie;
584 granted.exclusive = af->af_granted.exclusive;
585 granted.alock.caller_name =
586 af->af_granted.alock.caller_name;
587 granted.alock.fh = af->af_granted.alock.fh;
588 granted.alock.oh = af->af_granted.alock.oh;
589 granted.alock.svid = af->af_granted.alock.svid;
590 granted.alock.l_offset =
591 af->af_granted.alock.l_offset;
592 granted.alock.l_len =
593 af->af_granted.alock.l_len;
595 nlm_granted_msg_1(&granted,
596 NULL, af->af_rpc, &ext, nlm_zero_tv);
600 * Move this entry to the nh_granted list.
602 af->af_expiretime = time_uptime + NLM_EXPIRE_TIMEOUT;
603 mtx_lock(&af->af_host->nh_lock);
604 TAILQ_REMOVE(&af->af_host->nh_pending, af, af_link);
605 TAILQ_INSERT_TAIL(&af->af_host->nh_granted, af, af_link);
606 mtx_unlock(&af->af_host->nh_lock);
610 * Free an async lock request. The request must have been removed from
614 nlm_free_async_lock(struct nlm_async_lock *af)
617 * Free an async lock.
620 CLNT_RELEASE(af->af_rpc);
621 xdr_free((xdrproc_t) xdr_nlm4_testargs, &af->af_granted);
628 * Cancel our async request - this must be called with
629 * af->nh_host->nh_lock held. This is slightly complicated by a
630 * potential race with our own callback. If we fail to cancel the
631 * lock, it must already have been granted - we make sure our async
632 * task has completed by calling taskqueue_drain in this case.
635 nlm_cancel_async_lock(struct nlm_async_lock *af)
637 struct nlm_host *host = af->af_host;
640 mtx_assert(&host->nh_lock, MA_OWNED);
642 mtx_unlock(&host->nh_lock);
644 error = VOP_ADVLOCKASYNC(af->af_vp, NULL, F_CANCEL, &af->af_fl,
645 F_REMOTE, NULL, &af->af_cookie);
649 * We failed to cancel - make sure our callback has
650 * completed before we continue.
652 taskqueue_drain(taskqueue_thread, &af->af_task);
655 mtx_lock(&host->nh_lock);
658 NLM_DEBUG(2, "NLM: async lock %p for %s (sysid %d) "
659 "cancelled\n", af, host->nh_caller_name, host->nh_sysid);
662 * Remove from the nh_pending list and free now that
663 * we are safe from the callback.
665 TAILQ_REMOVE(&host->nh_pending, af, af_link);
666 mtx_unlock(&host->nh_lock);
667 nlm_free_async_lock(af);
668 mtx_lock(&host->nh_lock);
675 nlm_check_expired_locks(struct nlm_host *host)
677 struct nlm_async_lock *af;
678 time_t uptime = time_uptime;
680 mtx_lock(&host->nh_lock);
681 while ((af = TAILQ_FIRST(&host->nh_granted)) != NULL
682 && uptime >= af->af_expiretime) {
683 NLM_DEBUG(2, "NLM: async lock %p for %s (sysid %d) expired,"
684 " cookie %d:%d\n", af, af->af_host->nh_caller_name,
685 af->af_host->nh_sysid, ng_sysid(&af->af_granted.cookie),
686 ng_cookie(&af->af_granted.cookie));
687 TAILQ_REMOVE(&host->nh_granted, af, af_link);
688 mtx_unlock(&host->nh_lock);
689 nlm_free_async_lock(af);
690 mtx_lock(&host->nh_lock);
692 while ((af = TAILQ_FIRST(&host->nh_finished)) != NULL) {
693 TAILQ_REMOVE(&host->nh_finished, af, af_link);
694 mtx_unlock(&host->nh_lock);
695 nlm_free_async_lock(af);
696 mtx_lock(&host->nh_lock);
698 mtx_unlock(&host->nh_lock);
702 * Free resources used by a host. This is called after the reference
703 * count has reached zero so it doesn't need to worry about locks.
706 nlm_host_destroy(struct nlm_host *host)
709 mtx_lock(&nlm_global_lock);
710 TAILQ_REMOVE(&nlm_hosts, host, nh_link);
711 mtx_unlock(&nlm_global_lock);
713 if (host->nh_srvrpc.nr_client)
714 CLNT_RELEASE(host->nh_srvrpc.nr_client);
715 if (host->nh_clntrpc.nr_client)
716 CLNT_RELEASE(host->nh_clntrpc.nr_client);
717 mtx_destroy(&host->nh_lock);
718 sysctl_ctx_free(&host->nh_sysctl);
723 * Thread start callback for client lock recovery
726 nlm_client_recovery_start(void *arg)
728 struct nlm_host *host = (struct nlm_host *) arg;
730 NLM_DEBUG(1, "NLM: client lock recovery for %s started\n",
731 host->nh_caller_name);
733 nlm_client_recovery(host);
735 NLM_DEBUG(1, "NLM: client lock recovery for %s completed\n",
736 host->nh_caller_name);
738 host->nh_monstate = NLM_MONITORED;
739 nlm_host_release(host);
745 * This is called when we receive a host state change notification. We
746 * unlock any active locks owned by the host. When rpc.lockd is
747 * shutting down, this function is called with newstate set to zero
748 * which allows us to cancel any pending async locks and clear the
752 nlm_host_notify(struct nlm_host *host, int newstate)
754 struct nlm_async_lock *af;
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);
763 * Cancel any pending async locks for this host.
765 mtx_lock(&host->nh_lock);
766 while ((af = TAILQ_FIRST(&host->nh_pending)) != NULL) {
768 * nlm_cancel_async_lock will remove the entry from
769 * nh_pending and free it.
771 nlm_cancel_async_lock(af);
773 mtx_unlock(&host->nh_lock);
774 nlm_check_expired_locks(host);
777 * The host just rebooted - trash its locks.
779 lf_clearremotesys(host->nh_sysid);
780 host->nh_state = newstate;
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.
788 && host->nh_monstate != NLM_RECOVERING
789 && lf_countlocks(NLM_SYSID_CLIENT | host->nh_sysid) > 0) {
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);
799 * Sysctl handler to count the number of locks for a sysid.
802 nlm_host_lock_count_sysctl(SYSCTL_HANDLER_ARGS)
804 struct nlm_host *host;
807 host = oidp->oid_arg1;
808 count = lf_countlocks(host->nh_sysid);
809 return sysctl_handle_int(oidp, &count, 0, req);
813 * Sysctl handler to count the number of client locks for a sysid.
816 nlm_host_client_lock_count_sysctl(SYSCTL_HANDLER_ARGS)
818 struct nlm_host *host;
821 host = oidp->oid_arg1;
822 count = lf_countlocks(NLM_SYSID_CLIENT | host->nh_sysid);
823 return sysctl_handle_int(oidp, &count, 0, req);
827 * Create a new NLM host.
829 static struct nlm_host *
830 nlm_create_host(const char* caller_name)
832 struct nlm_host *host;
833 struct sysctl_oid *oid;
835 mtx_assert(&nlm_global_lock, MA_OWNED);
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);
842 mtx_init(&host->nh_lock, "nh_lock", NULL, MTX_DEF);
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);
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);
857 mtx_unlock(&nlm_global_lock);
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_UINT(&host->nh_sysctl, SYSCTL_CHILDREN(oid), OID_AUTO,
866 "version", CTLFLAG_RD, &host->nh_vers, 0, "");
867 SYSCTL_ADD_UINT(&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", "");
876 mtx_lock(&nlm_global_lock);
882 * Acquire the next sysid for remote locks not handled by the NLM.
885 nlm_acquire_next_sysid(void)
889 mtx_lock(&nlm_global_lock);
890 next_sysid = nlm_next_sysid++;
891 mtx_unlock(&nlm_global_lock);
896 * Return non-zero if the address parts of the two sockaddrs are the
900 nlm_compare_addr(const struct sockaddr *a, const struct sockaddr *b)
902 const struct sockaddr_in *a4, *b4;
904 const struct sockaddr_in6 *a6, *b6;
907 if (a->sa_family != b->sa_family)
910 switch (a->sa_family) {
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));
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));
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.
937 struct nlm_host *host;
939 mtx_assert(&nlm_global_lock, MA_OWNED);
941 if (time_uptime <= nlm_next_idle_check)
944 nlm_next_idle_check = time_uptime + NLM_IDLE_PERIOD;
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
953 host->nh_idle_timeout =
954 time_uptime + NLM_IDLE_TIMEOUT;
955 mtx_lock(&nlm_global_lock);
958 nlm_host_unmonitor(host);
959 mtx_lock(&nlm_global_lock);
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.
973 nlm_find_host_by_name(const char *name, const struct sockaddr *addr,
976 struct nlm_host *host;
978 mtx_lock(&nlm_global_lock);
981 * The remote host is determined by caller_name.
983 TAILQ_FOREACH(host, &nlm_hosts, nh_link) {
984 if (!strcmp(host->nh_caller_name, name))
989 host = nlm_create_host(name);
991 mtx_unlock(&nlm_global_lock);
995 refcount_acquire(&host->nh_refs);
997 host->nh_idle_timeout = time_uptime + NLM_IDLE_TIMEOUT;
1000 * If we have an address for the host, record it so that we
1001 * can send async replies etc.
1005 KASSERT(addr->sa_len < sizeof(struct sockaddr_storage),
1006 ("Strange remote transport address length"));
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.
1013 if (host->nh_addr.ss_len && host->nh_srvrpc.nr_client) {
1014 if (!nlm_compare_addr(
1015 (struct sockaddr *) &host->nh_addr,
1017 || host->nh_vers != vers) {
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);
1024 CLNT_RELEASE(client);
1028 memcpy(&host->nh_addr, addr, addr->sa_len);
1029 host->nh_vers = vers;
1034 mtx_unlock(&nlm_global_lock);
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
1046 nlm_find_host_by_addr(const struct sockaddr *addr, int vers)
1049 * Fake up a name using inet_ntop. This buffer is
1050 * large enough for an IPv6 address.
1052 char tmp[sizeof "ffff:ffff:ffff:ffff:ffff:ffff:255.255.255.255"];
1053 struct nlm_host *host;
1055 switch (addr->sa_family) {
1058 &((const struct sockaddr_in *) addr)->sin_addr,
1064 &((const struct sockaddr_in6 *) addr)->sin6_addr,
1069 strcmp(tmp, "<unknown>");
1073 mtx_lock(&nlm_global_lock);
1076 * The remote host is determined by caller_name.
1078 TAILQ_FOREACH(host, &nlm_hosts, nh_link) {
1079 if (nlm_compare_addr(addr,
1080 (const struct sockaddr *) &host->nh_addr))
1085 host = nlm_create_host(tmp);
1087 mtx_unlock(&nlm_global_lock);
1090 memcpy(&host->nh_addr, addr, addr->sa_len);
1091 host->nh_vers = vers;
1093 refcount_acquire(&host->nh_refs);
1095 host->nh_idle_timeout = time_uptime + NLM_IDLE_TIMEOUT;
1099 mtx_unlock(&nlm_global_lock);
1105 * Find the NLM host that matches the value of 'sysid'. If none
1106 * exists, return NULL.
1108 static struct nlm_host *
1109 nlm_find_host_by_sysid(int sysid)
1111 struct nlm_host *host;
1113 TAILQ_FOREACH(host, &nlm_hosts, nh_link) {
1114 if (host->nh_sysid == sysid) {
1115 refcount_acquire(&host->nh_refs);
1123 void nlm_host_release(struct nlm_host *host)
1125 if (refcount_release(&host->nh_refs)) {
1129 nlm_host_destroy(host);
1134 * Unregister this NLM host with the local NSM due to idleness.
1137 nlm_host_unmonitor(struct nlm_host *host)
1141 struct timeval timo;
1142 enum clnt_stat stat;
1144 NLM_DEBUG(1, "NLM: unmonitoring %s (sysid %d)\n",
1145 host->nh_caller_name, host->nh_sysid);
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
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;
1160 stat = CLNT_CALL(nlm_nsm, SM_UNMON,
1161 (xdrproc_t) xdr_mon, &smmonid,
1162 (xdrproc_t) xdr_sm_stat, &smstat, timo);
1164 if (stat != RPC_SUCCESS) {
1165 NLM_ERR("Failed to contact local NSM - rpc error %d\n", stat);
1168 if (smstat.res_stat == stat_fail) {
1169 NLM_ERR("Local NSM refuses to unmonitor %s\n",
1170 host->nh_caller_name);
1174 host->nh_monstate = NLM_UNMONITORED;
1178 * Register this NLM host with the local NSM so that we can be
1179 * notified if it reboots.
1182 nlm_host_monitor(struct nlm_host *host, int state)
1186 struct timeval timo;
1187 enum clnt_stat stat;
1189 if (state && !host->nh_state) {
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.
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);
1200 mtx_lock(&host->nh_lock);
1201 if (host->nh_monstate != NLM_UNMONITORED) {
1202 mtx_unlock(&host->nh_lock);
1205 host->nh_monstate = NLM_MONITORED;
1206 mtx_unlock(&host->nh_lock);
1208 NLM_DEBUG(1, "NLM: monitoring %s (sysid %d)\n",
1209 host->nh_caller_name, host->nh_sysid);
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
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));
1225 stat = CLNT_CALL(nlm_nsm, SM_MON,
1226 (xdrproc_t) xdr_mon, &smmon,
1227 (xdrproc_t) xdr_sm_stat, &smstat, timo);
1229 if (stat != RPC_SUCCESS) {
1230 NLM_ERR("Failed to contact local NSM - rpc error %d\n", stat);
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);
1242 host->nh_monstate = NLM_MONITORED;
1246 * Return an RPC client handle that can be used to talk to the NLM
1247 * running on the given host.
1250 nlm_host_get_rpc(struct nlm_host *host, bool_t isserver)
1252 struct nlm_rpc *rpc;
1255 mtx_lock(&host->nh_lock);
1258 rpc = &host->nh_srvrpc;
1260 rpc = &host->nh_clntrpc;
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.
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);
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);
1285 if (rpc->nr_client) {
1286 mtx_unlock(&host->nh_lock);
1287 CLNT_DESTROY(client);
1288 mtx_lock(&host->nh_lock);
1290 rpc->nr_client = client;
1291 rpc->nr_create_time = time_uptime;
1296 client = rpc->nr_client;
1298 CLNT_ACQUIRE(client);
1299 mtx_unlock(&host->nh_lock);
1305 int nlm_host_get_sysid(struct nlm_host *host)
1308 return (host->nh_sysid);
1312 nlm_host_get_state(struct nlm_host *host)
1315 return (host->nh_state);
1319 nlm_register_wait_lock(struct nlm4_lock *lock, struct vnode *vp)
1321 struct nlm_waiting_lock *nw;
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;
1330 mtx_lock(&nlm_global_lock);
1331 TAILQ_INSERT_TAIL(&nlm_waiting_locks, nw, nw_link);
1332 mtx_unlock(&nlm_global_lock);
1338 nlm_deregister_wait_lock(void *handle)
1340 struct nlm_waiting_lock *nw = handle;
1342 mtx_lock(&nlm_global_lock);
1343 TAILQ_REMOVE(&nlm_waiting_locks, nw, nw_link);
1344 mtx_unlock(&nlm_global_lock);
1350 nlm_wait_lock(void *handle, int timo)
1352 struct nlm_waiting_lock *nw = handle;
1356 * If the granted message arrived before we got here,
1357 * nw->nw_waiting will be FALSE - in that case, don't sleep.
1359 mtx_lock(&nlm_global_lock);
1362 error = msleep(nw, &nlm_global_lock, PCATCH, "nlmlock", timo);
1363 TAILQ_REMOVE(&nlm_waiting_locks, nw, nw_link);
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.
1370 if (!nw->nw_waiting)
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.
1381 mtx_unlock(&nlm_global_lock);
1389 nlm_cancel_wait(struct vnode *vp)
1391 struct nlm_waiting_lock *nw;
1393 mtx_lock(&nlm_global_lock);
1394 TAILQ_FOREACH(nw, &nlm_waiting_locks, nw_link) {
1395 if (nw->nw_vp == vp) {
1399 mtx_unlock(&nlm_global_lock);
1403 /**********************************************************************/
1406 * Syscall interface with userland.
1409 extern void nlm_prog_0(struct svc_req *rqstp, SVCXPRT *transp);
1410 extern void nlm_prog_1(struct svc_req *rqstp, SVCXPRT *transp);
1411 extern void nlm_prog_3(struct svc_req *rqstp, SVCXPRT *transp);
1412 extern void nlm_prog_4(struct svc_req *rqstp, SVCXPRT *transp);
1415 nlm_register_services(SVCPOOL *pool, int addr_count, char **addrs)
1417 static rpcvers_t versions[] = {
1418 NLM_SM, NLM_VERS, NLM_VERSX, NLM_VERS4
1420 static void (*dispatchers[])(struct svc_req *, SVCXPRT *) = {
1421 nlm_prog_0, nlm_prog_1, nlm_prog_3, nlm_prog_4
1423 static const int version_count = sizeof(versions) / sizeof(versions[0]);
1428 struct netconfig *nconf;
1432 NLM_ERR("NLM: no service addresses given - can't start server");
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++) {
1440 * Create transports for the first version and
1441 * then just register everything else to the
1447 error = copyin(&addrs[2*j], &up,
1451 error = copyinstr(up, netid, sizeof(netid),
1455 error = copyin(&addrs[2*j+1], &up,
1459 error = copyinstr(up, uaddr, sizeof(uaddr),
1463 nconf = getnetconfigent(netid);
1465 NLM_ERR("Can't lookup netid %s\n",
1470 xprts[j] = svc_tp_create(pool, dispatchers[i],
1471 NLM_PROG, versions[i], uaddr, nconf);
1473 NLM_ERR("NLM: unable to create "
1474 "(NLM_PROG, %d).\n", versions[i]);
1478 freenetconfigent(nconf);
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]);
1494 for (j = 0; j < addr_count; j++) {
1496 SVC_RELEASE(xprts[j]);
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
1509 nlm_server_main(int addr_count, char **addrs)
1511 struct thread *td = curthread;
1513 SVCPOOL *pool = NULL;
1517 struct sockaddr_in6 sin6;
1519 struct sockaddr_in sin;
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;
1534 NLM_ERR("NLM: can't start server - "
1535 "it appears to be running already\n");
1539 memset(&opt, 0, sizeof(opt));
1542 error = socreate(AF_INET, &nlm_socket, SOCK_DGRAM, 0,
1545 NLM_ERR("NLM: can't create IPv4 socket - error %d\n", error);
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);
1558 error = socreate(AF_INET6, &nlm_socket6, SOCK_DGRAM, 0,
1561 NLM_ERR("NLM: can't create IPv6 socket - error %d\n", error);
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);
1574 nlm_auth = authunix_create(curthread->td_ucred);
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);
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,
1595 NLM_ERR("Can't start NLM - unable to contact NSM\n");
1600 pool = svcpool_create("NLM", NULL);
1602 error = nlm_register_services(pool, addr_count, addrs);
1606 memset(&id, 0, sizeof(id));
1607 id.my_name = "NFS NLM";
1611 stat = CLNT_CALL(nlm_nsm, SM_UNMON_ALL,
1612 (xdrproc_t) xdr_my_id, &id,
1613 (xdrproc_t) xdr_sm_stat, &smstat, timo);
1615 if (stat != RPC_SUCCESS) {
1618 CLNT_GETERR(nlm_nsm, &err);
1619 NLM_ERR("NLM: unexpected error contacting NSM, "
1620 "stat=%d, errno=%d\n", stat, err.re_errno);
1625 NLM_DEBUG(1, "NLM: local NSM state is %d\n", smstat.state);
1626 nlm_nsm_state = smstat.state;
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;
1636 nfs_advlock_p = old_nfs_advlock;
1637 nfs_reclaim_p = old_nfs_reclaim;
1641 svcpool_destroy(pool);
1644 * We are finished communicating with the NSM.
1647 CLNT_RELEASE(nlm_nsm);
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.
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.
1670 mtx_lock(&nlm_global_lock);
1671 TAILQ_FOREACH(nw, &nlm_waiting_locks, nw_link) {
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);
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)
1687 if (host->nh_addr.ss_family == AF_INET6)
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.
1695 if (host->nh_srvrpc.nr_client)
1696 CLNT_CONTROL(host->nh_srvrpc.nr_client,
1698 if (host->nh_clntrpc.nr_client)
1699 CLNT_CONTROL(host->nh_clntrpc.nr_client,
1702 mtx_unlock(&host->nh_lock);
1704 mtx_unlock(&nlm_global_lock);
1706 AUTH_DESTROY(nlm_auth);
1709 soclose(nlm_socket);
1713 soclose(nlm_socket6);
1721 sys_nlm_syscall(struct thread *td, struct nlm_syscall_args *uap)
1725 #if __FreeBSD_version >= 700000
1726 error = priv_check(td, PRIV_NFS_LOCKD);
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;
1737 return nlm_server_main(uap->addr_count, uap->addrs);
1740 /**********************************************************************/
1743 * NLM implementation details, called from the RPC stubs.
1748 nlm_sm_notify(struct nlm_sm_status *argp)
1751 struct nlm_host *host;
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);
1757 nlm_host_notify(host, argp->state);
1758 nlm_host_release(host);
1763 nlm_convert_to_fhandle_t(fhandle_t *fhp, struct netobj *p)
1765 memcpy(fhp, p->n_bytes, sizeof(fhandle_t));
1769 struct mount *vs_mp;
1770 struct vnode *vs_vp;
1776 nlm_get_vfs_state(struct nlm_host *host, struct svc_req *rqstp,
1777 fhandle_t *fhp, struct vfs_state *vs)
1780 struct ucred *cred = NULL, *credanon;
1782 memset(vs, 0, sizeof(*vs));
1784 vs->vs_mp = vfs_getvfs(&fhp->fh_fsid);
1788 vs->vs_vfslocked = VFS_LOCK_GIANT(vs->vs_mp);
1790 error = VFS_CHECKEXP(vs->vs_mp, (struct sockaddr *)&host->nh_addr,
1791 &exflags, &credanon, NULL, NULL);
1795 if (exflags & MNT_EXRDONLY || (vs->vs_mp->mnt_flag & MNT_RDONLY)) {
1800 error = VFS_FHTOVP(vs->vs_mp, &fhp->fh_fid, LK_EXCLUSIVE, &vs->vs_vp);
1803 vs->vs_vnlocked = TRUE;
1805 if (!svc_getcred(rqstp, &cred, NULL)) {
1809 if (cred->cr_uid == 0 || (exflags & MNT_EXPORTANON)) {
1818 error = VOP_ACCESS(vs->vs_vp, VWRITE, cred, curthread);
1822 #if __FreeBSD_version < 800011
1823 VOP_UNLOCK(vs->vs_vp, 0, curthread);
1825 VOP_UNLOCK(vs->vs_vp, 0);
1827 vs->vs_vnlocked = FALSE;
1839 nlm_release_vfs_state(struct vfs_state *vs)
1843 if (vs->vs_vnlocked)
1850 VFS_UNLOCK_GIANT(vs->vs_vfslocked);
1854 nlm_convert_error(int error)
1857 if (error == ESTALE)
1858 return nlm4_stale_fh;
1859 else if (error == EROFS)
1866 nlm_do_test(nlm4_testargs *argp, nlm4_testres *result, struct svc_req *rqstp,
1870 struct vfs_state vs;
1871 struct nlm_host *host, *bhost;
1875 memset(result, 0, sizeof(*result));
1876 memset(&vs, 0, sizeof(vs));
1878 host = nlm_find_host_by_name(argp->alock.caller_name,
1879 svc_getrpccaller(rqstp), rqstp->rq_vers);
1881 result->stat.stat = nlm4_denied_nolocks;
1885 NLM_DEBUG(3, "nlm_do_test(): caller_name = %s (sysid = %d)\n",
1886 host->nh_caller_name, host->nh_sysid);
1888 nlm_check_expired_locks(host);
1889 sysid = host->nh_sysid;
1891 nlm_convert_to_fhandle_t(&fh, &argp->alock.fh);
1892 nlm_copy_netobj(&result->cookie, &argp->cookie, M_RPC);
1894 if (time_uptime < nlm_grace_threshold) {
1895 result->stat.stat = nlm4_denied_grace_period;
1899 error = nlm_get_vfs_state(host, rqstp, &fh, &vs);
1901 result->stat.stat = nlm_convert_error(error);
1905 fl.l_start = argp->alock.l_offset;
1906 fl.l_len = argp->alock.l_len;
1907 fl.l_pid = argp->alock.svid;
1909 fl.l_whence = SEEK_SET;
1910 if (argp->exclusive)
1911 fl.l_type = F_WRLCK;
1913 fl.l_type = F_RDLCK;
1914 error = VOP_ADVLOCK(vs.vs_vp, NULL, F_GETLK, &fl, F_REMOTE);
1916 result->stat.stat = nlm4_failed;
1920 if (fl.l_type == F_UNLCK) {
1921 result->stat.stat = nlm4_granted;
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);
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.
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.
1944 * For the moment, return nothing in oh
1945 * (already zero'ed above).
1947 nlm_host_release(bhost);
1949 result->stat.nlm4_testrply_u.holder.l_offset = fl.l_start;
1950 result->stat.nlm4_testrply_u.holder.l_len = fl.l_len;
1954 nlm_release_vfs_state(&vs);
1956 *rpcp = nlm_host_get_rpc(host, TRUE);
1957 nlm_host_release(host);
1962 nlm_do_lock(nlm4_lockargs *argp, nlm4_res *result, struct svc_req *rqstp,
1963 bool_t monitor, CLIENT **rpcp)
1966 struct vfs_state vs;
1967 struct nlm_host *host;
1971 memset(result, 0, sizeof(*result));
1972 memset(&vs, 0, sizeof(vs));
1974 host = nlm_find_host_by_name(argp->alock.caller_name,
1975 svc_getrpccaller(rqstp), rqstp->rq_vers);
1977 result->stat.stat = nlm4_denied_nolocks;
1981 NLM_DEBUG(3, "nlm_do_lock(): caller_name = %s (sysid = %d)\n",
1982 host->nh_caller_name, host->nh_sysid);
1984 if (monitor && host->nh_state && argp->state
1985 && host->nh_state != argp->state) {
1987 * The host rebooted without telling us. Trash its
1990 nlm_host_notify(host, argp->state);
1993 nlm_check_expired_locks(host);
1994 sysid = host->nh_sysid;
1996 nlm_convert_to_fhandle_t(&fh, &argp->alock.fh);
1997 nlm_copy_netobj(&result->cookie, &argp->cookie, M_RPC);
1999 if (time_uptime < nlm_grace_threshold && !argp->reclaim) {
2000 result->stat.stat = nlm4_denied_grace_period;
2004 error = nlm_get_vfs_state(host, rqstp, &fh, &vs);
2006 result->stat.stat = nlm_convert_error(error);
2010 fl.l_start = argp->alock.l_offset;
2011 fl.l_len = argp->alock.l_len;
2012 fl.l_pid = argp->alock.svid;
2014 fl.l_whence = SEEK_SET;
2015 if (argp->exclusive)
2016 fl.l_type = F_WRLCK;
2018 fl.l_type = F_RDLCK;
2020 struct nlm_async_lock *af;
2022 struct nlm_grantcookie cookie;
2025 * First, make sure we can contact the host's NLM.
2027 client = nlm_host_get_rpc(host, TRUE);
2029 result->stat.stat = nlm4_failed;
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.
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) {
2049 cookie.ng_sysid = host->nh_sysid;
2050 cookie.ng_cookie = host->nh_grantcookie++;
2052 mtx_unlock(&host->nh_lock);
2054 CLNT_RELEASE(client);
2055 result->stat.stat = nlm4_blocked;
2059 af = malloc(sizeof(struct nlm_async_lock), M_NLM,
2061 TASK_INIT(&af->af_task, 0, nlm_lock_callback, af);
2062 af->af_vp = vs.vs_vp;
2065 af->af_rpc = client;
2067 * We use M_RPC here so that we can xdr_free the thing
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;
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.
2091 mtx_lock(&host->nh_lock);
2092 TAILQ_INSERT_TAIL(&host->nh_pending, af, af_link);
2093 mtx_unlock(&host->nh_lock);
2095 error = VOP_ADVLOCKASYNC(vs.vs_vp, NULL, F_SETLK, &fl, F_REMOTE,
2096 &af->af_task, &af->af_cookie);
2099 * If the lock completed synchronously, just free the
2100 * tracking structure now.
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,
2111 NLM_DEBUG(2, "NLM: pending async lock %p for %s "
2112 "(sysid %d)\n", af, host->nh_caller_name, sysid);
2114 * Don't vrele the vnode just yet - this must
2115 * wait until either the async callback
2116 * happens or the lock is cancelled.
2121 error = VOP_ADVLOCK(vs.vs_vp, NULL, F_SETLK, &fl, F_REMOTE);
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;
2132 result->stat.stat = nlm4_failed;
2136 nlm_host_monitor(host, argp->state);
2137 result->stat.stat = nlm4_granted;
2141 nlm_release_vfs_state(&vs);
2143 *rpcp = nlm_host_get_rpc(host, TRUE);
2144 nlm_host_release(host);
2149 nlm_do_cancel(nlm4_cancargs *argp, nlm4_res *result, struct svc_req *rqstp,
2153 struct vfs_state vs;
2154 struct nlm_host *host;
2157 struct nlm_async_lock *af;
2159 memset(result, 0, sizeof(*result));
2160 memset(&vs, 0, sizeof(vs));
2162 host = nlm_find_host_by_name(argp->alock.caller_name,
2163 svc_getrpccaller(rqstp), rqstp->rq_vers);
2165 result->stat.stat = nlm4_denied_nolocks;
2169 NLM_DEBUG(3, "nlm_do_cancel(): caller_name = %s (sysid = %d)\n",
2170 host->nh_caller_name, host->nh_sysid);
2172 nlm_check_expired_locks(host);
2173 sysid = host->nh_sysid;
2175 nlm_convert_to_fhandle_t(&fh, &argp->alock.fh);
2176 nlm_copy_netobj(&result->cookie, &argp->cookie, M_RPC);
2178 if (time_uptime < nlm_grace_threshold) {
2179 result->stat.stat = nlm4_denied_grace_period;
2183 error = nlm_get_vfs_state(host, rqstp, &fh, &vs);
2185 result->stat.stat = nlm_convert_error(error);
2189 fl.l_start = argp->alock.l_offset;
2190 fl.l_len = argp->alock.l_len;
2191 fl.l_pid = argp->alock.svid;
2193 fl.l_whence = SEEK_SET;
2194 if (argp->exclusive)
2195 fl.l_type = F_WRLCK;
2197 fl.l_type = F_RDLCK;
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.
2203 mtx_lock(&host->nh_lock);
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) {
2215 mtx_unlock(&host->nh_lock);
2216 result->stat.stat = nlm4_denied;
2220 error = nlm_cancel_async_lock(af);
2223 result->stat.stat = nlm4_denied;
2225 result->stat.stat = nlm4_granted;
2228 mtx_unlock(&host->nh_lock);
2231 nlm_release_vfs_state(&vs);
2233 *rpcp = nlm_host_get_rpc(host, TRUE);
2234 nlm_host_release(host);
2239 nlm_do_unlock(nlm4_unlockargs *argp, nlm4_res *result, struct svc_req *rqstp,
2243 struct vfs_state vs;
2244 struct nlm_host *host;
2248 memset(result, 0, sizeof(*result));
2249 memset(&vs, 0, sizeof(vs));
2251 host = nlm_find_host_by_name(argp->alock.caller_name,
2252 svc_getrpccaller(rqstp), rqstp->rq_vers);
2254 result->stat.stat = nlm4_denied_nolocks;
2258 NLM_DEBUG(3, "nlm_do_unlock(): caller_name = %s (sysid = %d)\n",
2259 host->nh_caller_name, host->nh_sysid);
2261 nlm_check_expired_locks(host);
2262 sysid = host->nh_sysid;
2264 nlm_convert_to_fhandle_t(&fh, &argp->alock.fh);
2265 nlm_copy_netobj(&result->cookie, &argp->cookie, M_RPC);
2267 if (time_uptime < nlm_grace_threshold) {
2268 result->stat.stat = nlm4_denied_grace_period;
2272 error = nlm_get_vfs_state(host, rqstp, &fh, &vs);
2274 result->stat.stat = nlm_convert_error(error);
2278 fl.l_start = argp->alock.l_offset;
2279 fl.l_len = argp->alock.l_len;
2280 fl.l_pid = argp->alock.svid;
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);
2287 * Ignore the error - there is no result code for failure,
2288 * only for grace period.
2290 result->stat.stat = nlm4_granted;
2293 nlm_release_vfs_state(&vs);
2295 *rpcp = nlm_host_get_rpc(host, TRUE);
2296 nlm_host_release(host);
2301 nlm_do_granted(nlm4_testargs *argp, nlm4_res *result, struct svc_req *rqstp,
2305 struct nlm_host *host;
2306 struct nlm_waiting_lock *nw;
2308 memset(result, 0, sizeof(*result));
2310 host = nlm_find_host_by_addr(svc_getrpccaller(rqstp), rqstp->rq_vers);
2312 result->stat.stat = nlm4_denied_nolocks;
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);
2320 mtx_lock(&nlm_global_lock);
2321 TAILQ_FOREACH(nw, &nlm_waiting_locks, nw_link) {
2322 if (!nw->nw_waiting)
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;
2332 result->stat.stat = nlm4_granted;
2336 mtx_unlock(&nlm_global_lock);
2340 *rpcp = nlm_host_get_rpc(host, TRUE);
2341 nlm_host_release(host);
2346 nlm_do_granted_res(nlm4_res *argp, struct svc_req *rqstp)
2348 struct nlm_host *host = NULL;
2349 struct nlm_async_lock *af = NULL;
2352 if (argp->cookie.n_len != sizeof(struct nlm_grantcookie)) {
2353 NLM_DEBUG(1, "NLM: bogus grant cookie");
2357 host = nlm_find_host_by_sysid(ng_sysid(&argp->cookie));
2359 NLM_DEBUG(1, "NLM: Unknown host rejected our grant");
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))
2369 TAILQ_REMOVE(&host->nh_granted, af, af_link);
2370 mtx_unlock(&host->nh_lock);
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));
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);
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);
2390 NLM_DEBUG(5, "NLM: async lock %p rejected by host %s (sysid %d)",
2391 af, host->nh_caller_name, host->nh_sysid);
2393 NLM_DEBUG(5, "NLM: async lock %p accepted by host %s (sysid %d)",
2394 af, host->nh_caller_name, host->nh_sysid);
2399 nlm_free_async_lock(af);
2401 nlm_host_release(host);
2405 nlm_do_free_all(nlm4_notify *argp)
2407 struct nlm_host *host, *thost;
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);
2416 * Kernel module glue
2419 nfslockd_modevent(module_t mod, int type, void *data)
2424 static moduledata_t nfslockd_mod = {
2429 DECLARE_MODULE(nfslockd, nfslockd_mod, SI_SUB_VFS, SI_ORDER_ANY);
2431 /* So that loader and kldload(2) can find us, wherever we are.. */
2432 MODULE_DEPEND(nfslockd, krpc, 1, 1, 1);
2433 MODULE_DEPEND(nfslockd, nfslock, 1, 1, 1);
2434 MODULE_VERSION(nfslockd, 1);