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"
31 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD$");
34 #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/sysproto.h>
51 #include <sys/systm.h>
52 #include <sys/taskqueue.h>
53 #include <sys/unistd.h>
54 #include <sys/vnode.h>
56 #include <nfs/nfsproto.h>
57 #include <nfsclient/nfs.h>
58 #include <nfsclient/nfsnode.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 * Support for sysctl vfs.nlm.sysid
82 SYSCTL_NODE(_vfs, OID_AUTO, nlm, CTLFLAG_RW, NULL, "Network Lock Manager");
83 SYSCTL_NODE(_vfs_nlm, OID_AUTO, sysid, CTLFLAG_RW, NULL, "");
88 static int nlm_syscall_offset = SYS_nlm_syscall;
89 static struct sysent nlm_syscall_prev_sysent;
90 #if __FreeBSD_version < 700000
91 static struct sysent nlm_syscall_sysent = {
92 (sizeof(struct nlm_syscall_args) / sizeof(register_t)) | SYF_MPSAFE,
93 (sy_call_t *) nlm_syscall
96 MAKE_SYSENT(nlm_syscall);
98 static bool_t nlm_syscall_registered = FALSE;
101 * Debug level passed in from userland. We also support a sysctl hook
102 * so that it can be changed on a live system.
104 static int nlm_debug_level;
105 SYSCTL_INT(_debug, OID_AUTO, nlm_debug, CTLFLAG_RW, &nlm_debug_level, 0, "");
108 * Grace period handling. The value of nlm_grace_threshold is the
109 * value of time_uptime after which we are serving requests normally.
111 static time_t nlm_grace_threshold;
114 * We check for idle hosts if time_uptime is greater than
115 * nlm_next_idle_check,
117 static time_t nlm_next_idle_check;
120 * A socket to use for RPC - shared by all IPv4 RPC clients.
122 static struct socket *nlm_socket;
127 * A socket to use for RPC - shared by all IPv6 RPC clients.
129 static struct socket *nlm_socket6;
134 * An RPC client handle that can be used to communicate with the local
137 static CLIENT *nlm_nsm;
140 * An AUTH handle for the server's creds.
142 static AUTH *nlm_auth;
145 * A zero timeval for sending async RPC messages.
147 struct timeval nlm_zero_tv = { 0, 0 };
150 * The local NSM state number
156 * A lock to protect the host list and waiting lock list.
158 static struct mtx nlm_global_lock;
162 * (l) locked by nh_lock
163 * (s) only accessed via server RPC which is single threaded
164 * (g) locked by nlm_global_lock
165 * (c) const until freeing
166 * (a) modified using atomic ops
170 * A pending client-side lock request, stored on the nlm_waiting_locks
173 struct nlm_waiting_lock {
174 TAILQ_ENTRY(nlm_waiting_lock) nw_link; /* (g) */
175 bool_t nw_waiting; /* (g) */
176 nlm4_lock nw_lock; /* (c) */
177 union nfsfh nw_fh; /* (c) */
178 struct vnode *nw_vp; /* (c) */
180 TAILQ_HEAD(nlm_waiting_lock_list, nlm_waiting_lock);
182 struct nlm_waiting_lock_list nlm_waiting_locks; /* (g) */
185 * A pending server-side asynchronous lock request, stored on the
186 * nh_pending list of the NLM host.
188 struct nlm_async_lock {
189 TAILQ_ENTRY(nlm_async_lock) af_link; /* (l) host's list of locks */
190 struct task af_task; /* (c) async callback details */
191 void *af_cookie; /* (l) lock manager cancel token */
192 struct vnode *af_vp; /* (l) vnode to lock */
193 struct flock af_fl; /* (c) lock details */
194 struct nlm_host *af_host; /* (c) host which is locking */
195 CLIENT *af_rpc; /* (c) rpc client to send message */
196 nlm4_testargs af_granted; /* (c) notification details */
198 TAILQ_HEAD(nlm_async_lock_list, nlm_async_lock);
203 enum nlm_host_state {
211 CLIENT *nr_client; /* (l) RPC client handle */
212 time_t nr_create_time; /* (l) when client was created */
217 volatile u_int nh_refs; /* (a) reference count */
218 TAILQ_ENTRY(nlm_host) nh_link; /* (g) global list of hosts */
219 char nh_caller_name[MAXNAMELEN]; /* (c) printable name of host */
220 uint32_t nh_sysid; /* (c) our allocaed system ID */
221 char nh_sysid_string[10]; /* (c) string rep. of sysid */
222 struct sockaddr_storage nh_addr; /* (s) remote address of host */
223 struct nlm_rpc nh_srvrpc; /* (l) RPC for server replies */
224 struct nlm_rpc nh_clntrpc; /* (l) RPC for client requests */
225 rpcvers_t nh_vers; /* (s) NLM version of host */
226 int nh_state; /* (s) last seen NSM state of host */
227 enum nlm_host_state nh_monstate; /* (l) local NSM monitoring state */
228 time_t nh_idle_timeout; /* (s) Time at which host is idle */
229 struct sysctl_ctx_list nh_sysctl; /* (c) vfs.nlm.sysid nodes */
230 struct nlm_async_lock_list nh_pending; /* (l) pending async locks */
231 struct nlm_async_lock_list nh_finished; /* (l) finished async locks */
233 TAILQ_HEAD(nlm_host_list, nlm_host);
235 static struct nlm_host_list nlm_hosts; /* (g) */
236 static uint32_t nlm_next_sysid = 1; /* (g) */
238 static void nlm_host_unmonitor(struct nlm_host *);
240 /**********************************************************************/
243 * Initialise NLM globals.
246 nlm_init(void *dummy)
250 mtx_init(&nlm_global_lock, "nlm_global_lock", NULL, MTX_DEF);
251 TAILQ_INIT(&nlm_waiting_locks);
252 TAILQ_INIT(&nlm_hosts);
254 error = syscall_register(&nlm_syscall_offset, &nlm_syscall_sysent,
255 &nlm_syscall_prev_sysent);
257 printf("Can't register NLM syscall\n");
259 nlm_syscall_registered = TRUE;
261 SYSINIT(nlm_init, SI_SUB_LOCK, SI_ORDER_FIRST, nlm_init, NULL);
264 nlm_uninit(void *dummy)
267 if (nlm_syscall_registered)
268 syscall_deregister(&nlm_syscall_offset,
269 &nlm_syscall_prev_sysent);
271 SYSUNINIT(nlm_uninit, SI_SUB_LOCK, SI_ORDER_FIRST, nlm_uninit, NULL);
274 * Copy a struct netobj.
277 nlm_copy_netobj(struct netobj *dst, struct netobj *src,
278 struct malloc_type *type)
281 dst->n_len = src->n_len;
282 dst->n_bytes = malloc(src->n_len, type, M_WAITOK);
283 memcpy(dst->n_bytes, src->n_bytes, src->n_len);
287 * Create an RPC client handle for the given (address,prog,vers)
291 nlm_get_rpc(struct sockaddr *sa, rpcprog_t prog, rpcvers_t vers)
293 char *wchan = "nlmrcv";
294 const char* protofmly;
295 struct sockaddr_storage ss;
301 enum clnt_stat stat = RPC_SUCCESS;
302 int rpcvers = RPCBVERS4;
303 bool_t do_tcp = FALSE;
304 struct portmap mapping;
308 * First we need to contact the remote RPCBIND service to find
311 memcpy(&ss, sa, sa->sa_len);
312 switch (ss.ss_family) {
314 ((struct sockaddr_in *)&ss)->sin_port = htons(111);
321 ((struct sockaddr_in6 *)&ss)->sin6_port = htons(111);
329 * Unsupported address family - fail.
334 rpcb = clnt_dg_create(so, (struct sockaddr *)&ss,
335 RPCBPROG, rpcvers, 0, 0);
343 parms.r_netid = "tcp";
345 parms.r_netid = "udp";
350 * Use the default timeout.
359 * Try RPCBIND 4 then 3.
362 stat = CLNT_CALL(rpcb, (rpcprog_t) RPCBPROC_GETADDR,
363 (xdrproc_t) xdr_rpcb, &parms,
364 (xdrproc_t) xdr_wrapstring, &uaddr, timo);
365 if (stat == RPC_PROGVERSMISMATCH) {
366 if (rpcvers == RPCBVERS4)
368 else if (rpcvers == RPCBVERS)
370 CLNT_CONTROL(rpcb, CLSET_VERS, &rpcvers);
372 } else if (stat == RPC_SUCCESS) {
374 * We have a reply from the remote RPCBIND - turn it
375 * into an appropriate address and make a new client
376 * that can talk to the remote NLM.
378 * XXX fixup IPv6 scope ID.
381 a = __rpc_uaddr2taddr_af(ss.ss_family, uaddr);
386 memcpy(&ss, a->buf, a->len);
389 xdr_free((xdrproc_t) xdr_wrapstring, &uaddr);
396 mapping.pm_prog = parms.r_prog;
397 mapping.pm_vers = parms.r_vers;
398 mapping.pm_prot = do_tcp ? IPPROTO_TCP : IPPROTO_UDP;
401 stat = CLNT_CALL(rpcb, (rpcprog_t) PMAPPROC_GETPORT,
402 (xdrproc_t) xdr_portmap, &mapping,
403 (xdrproc_t) xdr_u_short, &port, timo);
405 if (stat == RPC_SUCCESS) {
406 switch (ss.ss_family) {
408 ((struct sockaddr_in *)&ss)->sin_port =
414 ((struct sockaddr_in6 *)&ss)->sin6_port =
422 panic("invalid rpcvers %d", rpcvers);
425 * We may have a positive response from the portmapper, but the NLM
426 * service was not found. Make sure we received a valid port.
428 switch (ss.ss_family) {
430 port = ((struct sockaddr_in *)&ss)->sin_port;
434 port = ((struct sockaddr_in6 *)&ss)->sin6_port;
438 if (stat != RPC_SUCCESS || !port) {
440 * If we were able to talk to rpcbind or portmap, but the udp
441 * variant wasn't available, ask about tcp.
443 * XXX - We could also check for a TCP portmapper, but
444 * if the host is running a portmapper at all, we should be able
445 * to hail it over UDP.
447 if (stat == RPC_SUCCESS && !do_tcp) {
452 /* Otherwise, bad news. */
453 printf("NLM: failed to contact remote rpcbind, "
454 "stat = %d, port = %d\n",
462 * Destroy the UDP client we used to speak to rpcbind and
463 * recreate as a TCP client.
465 struct netconfig *nconf = NULL;
469 switch (ss.ss_family) {
471 nconf = getnetconfigent("tcp");
475 nconf = getnetconfigent("tcp6");
480 rpcb = clnt_reconnect_create(nconf, (struct sockaddr *)&ss,
482 CLNT_CONTROL(rpcb, CLSET_WAITCHAN, wchan);
483 rpcb->cl_auth = nlm_auth;
487 * Re-use the client we used to speak to rpcbind.
489 CLNT_CONTROL(rpcb, CLSET_SVC_ADDR, &ss);
490 CLNT_CONTROL(rpcb, CLSET_PROG, &prog);
491 CLNT_CONTROL(rpcb, CLSET_VERS, &vers);
492 CLNT_CONTROL(rpcb, CLSET_WAITCHAN, wchan);
493 rpcb->cl_auth = nlm_auth;
500 * This async callback after when an async lock request has been
501 * granted. We notify the host which initiated the request.
504 nlm_lock_callback(void *arg, int pending)
506 struct nlm_async_lock *af = (struct nlm_async_lock *) arg;
507 struct rpc_callextra ext;
509 if (nlm_debug_level >= 2)
510 printf("NLM: async lock %p for %s (sysid %d) granted\n",
511 af, af->af_host->nh_caller_name,
512 af->af_host->nh_sysid);
515 * Send the results back to the host.
517 * Note: there is a possible race here with nlm_host_notify
518 * destroying the RPC client. To avoid problems, the first
519 * thing nlm_host_notify does is to cancel pending async lock
522 memset(&ext, 0, sizeof(ext));
523 ext.rc_auth = nlm_auth;
524 if (af->af_host->nh_vers == NLM_VERS4) {
525 nlm4_granted_msg_4(&af->af_granted,
526 NULL, af->af_rpc, &ext, nlm_zero_tv);
529 * Back-convert to legacy protocol
531 nlm_testargs granted;
532 granted.cookie = af->af_granted.cookie;
533 granted.exclusive = af->af_granted.exclusive;
534 granted.alock.caller_name =
535 af->af_granted.alock.caller_name;
536 granted.alock.fh = af->af_granted.alock.fh;
537 granted.alock.oh = af->af_granted.alock.oh;
538 granted.alock.svid = af->af_granted.alock.svid;
539 granted.alock.l_offset =
540 af->af_granted.alock.l_offset;
541 granted.alock.l_len =
542 af->af_granted.alock.l_len;
544 nlm_granted_msg_1(&granted,
545 NULL, af->af_rpc, &ext, nlm_zero_tv);
549 * Move this entry to the nh_finished list. Someone else will
550 * free it later - its too hard to do it here safely without
551 * racing with cancel.
553 * XXX possibly we should have a third "granted sent but not
554 * ack'ed" list so that we can re-send the granted message.
556 mtx_lock(&af->af_host->nh_lock);
557 TAILQ_REMOVE(&af->af_host->nh_pending, af, af_link);
558 TAILQ_INSERT_TAIL(&af->af_host->nh_finished, af, af_link);
559 mtx_unlock(&af->af_host->nh_lock);
563 * Free an async lock request. The request must have been removed from
567 nlm_free_async_lock(struct nlm_async_lock *af)
570 * Free an async lock.
573 CLNT_RELEASE(af->af_rpc);
574 xdr_free((xdrproc_t) xdr_nlm4_testargs, &af->af_granted);
581 * Cancel our async request - this must be called with
582 * af->nh_host->nh_lock held. This is slightly complicated by a
583 * potential race with our own callback. If we fail to cancel the
584 * lock, it must already have been granted - we make sure our async
585 * task has completed by calling taskqueue_drain in this case.
588 nlm_cancel_async_lock(struct nlm_async_lock *af)
590 struct nlm_host *host = af->af_host;
593 mtx_assert(&host->nh_lock, MA_OWNED);
595 mtx_unlock(&host->nh_lock);
597 error = VOP_ADVLOCKASYNC(af->af_vp, NULL, F_CANCEL, &af->af_fl,
598 F_REMOTE, NULL, &af->af_cookie);
602 * We failed to cancel - make sure our callback has
603 * completed before we continue.
605 taskqueue_drain(taskqueue_thread, &af->af_task);
608 mtx_lock(&host->nh_lock);
611 if (nlm_debug_level >= 2)
612 printf("NLM: async lock %p for %s (sysid %d) "
614 af, host->nh_caller_name, host->nh_sysid);
617 * Remove from the nh_pending list and free now that
618 * we are safe from the callback.
620 TAILQ_REMOVE(&host->nh_pending, af, af_link);
621 mtx_unlock(&host->nh_lock);
622 nlm_free_async_lock(af);
623 mtx_lock(&host->nh_lock);
630 nlm_free_finished_locks(struct nlm_host *host)
632 struct nlm_async_lock *af;
634 mtx_lock(&host->nh_lock);
635 while ((af = TAILQ_FIRST(&host->nh_finished)) != NULL) {
636 TAILQ_REMOVE(&host->nh_finished, af, af_link);
637 mtx_unlock(&host->nh_lock);
638 nlm_free_async_lock(af);
639 mtx_lock(&host->nh_lock);
641 mtx_unlock(&host->nh_lock);
645 * Free resources used by a host. This is called after the reference
646 * count has reached zero so it doesn't need to worry about locks.
649 nlm_host_destroy(struct nlm_host *host)
652 mtx_lock(&nlm_global_lock);
653 TAILQ_REMOVE(&nlm_hosts, host, nh_link);
654 mtx_unlock(&nlm_global_lock);
656 if (host->nh_srvrpc.nr_client)
657 CLNT_RELEASE(host->nh_srvrpc.nr_client);
658 if (host->nh_clntrpc.nr_client)
659 CLNT_RELEASE(host->nh_clntrpc.nr_client);
660 mtx_destroy(&host->nh_lock);
661 sysctl_ctx_free(&host->nh_sysctl);
668 * Thread start callback for client lock recovery
671 nlm_client_recovery_start(void *arg)
673 struct nlm_host *host = (struct nlm_host *) arg;
675 if (nlm_debug_level >= 1)
676 printf("NLM: client lock recovery for %s started\n",
677 host->nh_caller_name);
679 nlm_client_recovery(host);
681 if (nlm_debug_level >= 1)
682 printf("NLM: client lock recovery for %s completed\n",
683 host->nh_caller_name);
685 host->nh_monstate = NLM_MONITORED;
686 nlm_host_release(host);
694 * This is called when we receive a host state change notification. We
695 * unlock any active locks owned by the host. When rpc.lockd is
696 * shutting down, this function is called with newstate set to zero
697 * which allows us to cancel any pending async locks and clear the
701 nlm_host_notify(struct nlm_host *host, int newstate)
703 struct nlm_async_lock *af;
706 if (nlm_debug_level >= 1)
707 printf("NLM: host %s (sysid %d) rebooted, new "
709 host->nh_caller_name, host->nh_sysid, newstate);
713 * Cancel any pending async locks for this host.
715 mtx_lock(&host->nh_lock);
716 while ((af = TAILQ_FIRST(&host->nh_pending)) != NULL) {
718 * nlm_cancel_async_lock will remove the entry from
719 * nh_pending and free it.
721 nlm_cancel_async_lock(af);
723 mtx_unlock(&host->nh_lock);
724 nlm_free_finished_locks(host);
727 * The host just rebooted - trash its locks.
729 lf_clearremotesys(host->nh_sysid);
730 host->nh_state = newstate;
734 * If we have any remote locks for this host (i.e. it
735 * represents a remote NFS server that our local NFS client
736 * has locks for), start a recovery thread.
739 && host->nh_monstate != NLM_RECOVERING
740 && lf_countlocks(NLM_SYSID_CLIENT | host->nh_sysid) > 0) {
742 host->nh_monstate = NLM_RECOVERING;
743 refcount_acquire(&host->nh_refs);
744 kthread_add(nlm_client_recovery_start, host, curproc, &td, 0, 0,
745 "NFS lock recovery for %s", host->nh_caller_name);
751 * Sysctl handler to count the number of locks for a sysid.
754 nlm_host_lock_count_sysctl(SYSCTL_HANDLER_ARGS)
756 struct nlm_host *host;
759 host = oidp->oid_arg1;
760 count = lf_countlocks(host->nh_sysid);
761 return sysctl_handle_int(oidp, &count, 0, req);
765 * Sysctl handler to count the number of client locks for a sysid.
768 nlm_host_client_lock_count_sysctl(SYSCTL_HANDLER_ARGS)
770 struct nlm_host *host;
773 host = oidp->oid_arg1;
774 count = lf_countlocks(NLM_SYSID_CLIENT | host->nh_sysid);
775 return sysctl_handle_int(oidp, &count, 0, req);
779 * Create a new NLM host.
781 static struct nlm_host *
782 nlm_create_host(const char* caller_name)
784 struct nlm_host *host;
785 struct sysctl_oid *oid;
787 mtx_assert(&nlm_global_lock, MA_OWNED);
789 if (nlm_debug_level >= 1)
790 printf("NLM: new host %s (sysid %d)\n",
791 caller_name, nlm_next_sysid);
792 host = malloc(sizeof(struct nlm_host), M_NLM, M_NOWAIT|M_ZERO);
795 mtx_init(&host->nh_lock, "nh_lock", NULL, MTX_DEF);
797 strlcpy(host->nh_caller_name, caller_name, MAXNAMELEN);
798 host->nh_sysid = nlm_next_sysid++;
799 snprintf(host->nh_sysid_string, sizeof(host->nh_sysid_string),
800 "%d", host->nh_sysid);
803 host->nh_monstate = NLM_UNMONITORED;
804 TAILQ_INIT(&host->nh_pending);
805 TAILQ_INIT(&host->nh_finished);
806 TAILQ_INSERT_TAIL(&nlm_hosts, host, nh_link);
808 mtx_unlock(&nlm_global_lock);
810 sysctl_ctx_init(&host->nh_sysctl);
811 oid = SYSCTL_ADD_NODE(&host->nh_sysctl,
812 SYSCTL_STATIC_CHILDREN(_vfs_nlm_sysid),
813 OID_AUTO, host->nh_sysid_string, CTLFLAG_RD, NULL, "");
814 SYSCTL_ADD_STRING(&host->nh_sysctl, SYSCTL_CHILDREN(oid), OID_AUTO,
815 "hostname", CTLFLAG_RD, host->nh_caller_name, 0, "");
816 SYSCTL_ADD_INT(&host->nh_sysctl, SYSCTL_CHILDREN(oid), OID_AUTO,
817 "version", CTLFLAG_RD, &host->nh_vers, 0, "");
818 SYSCTL_ADD_INT(&host->nh_sysctl, SYSCTL_CHILDREN(oid), OID_AUTO,
819 "monitored", CTLFLAG_RD, &host->nh_monstate, 0, "");
820 SYSCTL_ADD_PROC(&host->nh_sysctl, SYSCTL_CHILDREN(oid), OID_AUTO,
821 "lock_count", CTLTYPE_INT | CTLFLAG_RD, host, 0,
822 nlm_host_lock_count_sysctl, "I", "");
823 SYSCTL_ADD_PROC(&host->nh_sysctl, SYSCTL_CHILDREN(oid), OID_AUTO,
824 "client_lock_count", CTLTYPE_INT | CTLFLAG_RD, host, 0,
825 nlm_host_client_lock_count_sysctl, "I", "");
827 mtx_lock(&nlm_global_lock);
833 * Return non-zero if the address parts of the two sockaddrs are the
837 nlm_compare_addr(const struct sockaddr *a, const struct sockaddr *b)
839 const struct sockaddr_in *a4, *b4;
841 const struct sockaddr_in6 *a6, *b6;
844 if (a->sa_family != b->sa_family)
847 switch (a->sa_family) {
849 a4 = (const struct sockaddr_in *) a;
850 b4 = (const struct sockaddr_in *) b;
851 return !memcmp(&a4->sin_addr, &b4->sin_addr,
852 sizeof(a4->sin_addr));
855 a6 = (const struct sockaddr_in6 *) a;
856 b6 = (const struct sockaddr_in6 *) b;
857 return !memcmp(&a6->sin6_addr, &b6->sin6_addr,
858 sizeof(a6->sin6_addr));
866 * Check for idle hosts and stop monitoring them. We could also free
867 * the host structure here, possibly after a larger timeout but that
868 * would require some care to avoid races with
869 * e.g. nlm_host_lock_count_sysctl.
874 struct nlm_host *host;
876 mtx_assert(&nlm_global_lock, MA_OWNED);
878 if (time_uptime <= nlm_next_idle_check)
881 nlm_next_idle_check = time_uptime + NLM_IDLE_PERIOD;
883 TAILQ_FOREACH(host, &nlm_hosts, nh_link) {
884 if (host->nh_monstate == NLM_MONITORED
885 && time_uptime > host->nh_idle_timeout) {
886 mtx_unlock(&nlm_global_lock);
887 if (lf_countlocks(host->nh_sysid) > 0
888 || lf_countlocks(NLM_SYSID_CLIENT
890 host->nh_idle_timeout =
891 time_uptime + NLM_IDLE_TIMEOUT;
892 mtx_lock(&nlm_global_lock);
895 nlm_host_unmonitor(host);
896 mtx_lock(&nlm_global_lock);
902 * Search for an existing NLM host that matches the given name
903 * (typically the caller_name element of an nlm4_lock). If none is
904 * found, create a new host. If 'addr' is non-NULL, record the remote
905 * address of the host so that we can call it back for async
906 * responses. If 'vers' is greater than zero then record the NLM
907 * program version to use to communicate with this client.
910 nlm_find_host_by_name(const char *name, const struct sockaddr *addr,
913 struct nlm_host *host;
915 mtx_lock(&nlm_global_lock);
918 * The remote host is determined by caller_name.
920 TAILQ_FOREACH(host, &nlm_hosts, nh_link) {
921 if (!strcmp(host->nh_caller_name, name))
926 host = nlm_create_host(name);
928 mtx_unlock(&nlm_global_lock);
932 refcount_acquire(&host->nh_refs);
934 host->nh_idle_timeout = time_uptime + NLM_IDLE_TIMEOUT;
937 * If we have an address for the host, record it so that we
938 * can send async replies etc.
942 KASSERT(addr->sa_len < sizeof(struct sockaddr_storage),
943 ("Strange remote transport address length"));
946 * If we have seen an address before and we currently
947 * have an RPC client handle, make sure the address is
948 * the same, otherwise discard the client handle.
950 if (host->nh_addr.ss_len && host->nh_srvrpc.nr_client) {
951 if (!nlm_compare_addr(
952 (struct sockaddr *) &host->nh_addr,
954 || host->nh_vers != vers) {
956 mtx_lock(&host->nh_lock);
957 client = host->nh_srvrpc.nr_client;
958 host->nh_srvrpc.nr_client = NULL;
959 mtx_unlock(&host->nh_lock);
961 CLNT_RELEASE(client);
965 memcpy(&host->nh_addr, addr, addr->sa_len);
966 host->nh_vers = vers;
971 mtx_unlock(&nlm_global_lock);
977 * Search for an existing NLM host that matches the given remote
978 * address. If none is found, create a new host with the requested
979 * address and remember 'vers' as the NLM protocol version to use for
983 nlm_find_host_by_addr(const struct sockaddr *addr, int vers)
986 * Fake up a name using inet_ntop. This buffer is
987 * large enough for an IPv6 address.
989 char tmp[sizeof "ffff:ffff:ffff:ffff:ffff:ffff:255.255.255.255"];
990 struct nlm_host *host;
992 switch (addr->sa_family) {
994 __rpc_inet_ntop(AF_INET,
995 &((const struct sockaddr_in *) addr)->sin_addr,
1000 __rpc_inet_ntop(AF_INET6,
1001 &((const struct sockaddr_in6 *) addr)->sin6_addr,
1006 strcmp(tmp, "<unknown>");
1010 mtx_lock(&nlm_global_lock);
1013 * The remote host is determined by caller_name.
1015 TAILQ_FOREACH(host, &nlm_hosts, nh_link) {
1016 if (nlm_compare_addr(addr,
1017 (const struct sockaddr *) &host->nh_addr))
1022 host = nlm_create_host(tmp);
1024 mtx_unlock(&nlm_global_lock);
1027 memcpy(&host->nh_addr, addr, addr->sa_len);
1028 host->nh_vers = vers;
1030 refcount_acquire(&host->nh_refs);
1032 host->nh_idle_timeout = time_uptime + NLM_IDLE_TIMEOUT;
1036 mtx_unlock(&nlm_global_lock);
1042 * Find the NLM host that matches the value of 'sysid'. If none
1043 * exists, return NULL.
1045 static struct nlm_host *
1046 nlm_find_host_by_sysid(int sysid)
1048 struct nlm_host *host;
1050 TAILQ_FOREACH(host, &nlm_hosts, nh_link) {
1051 if (host->nh_sysid == sysid) {
1052 refcount_acquire(&host->nh_refs);
1060 void nlm_host_release(struct nlm_host *host)
1062 if (refcount_release(&host->nh_refs)) {
1066 nlm_host_destroy(host);
1071 * Unregister this NLM host with the local NSM due to idleness.
1074 nlm_host_unmonitor(struct nlm_host *host)
1078 struct timeval timo;
1079 enum clnt_stat stat;
1081 if (nlm_debug_level >= 1)
1082 printf("NLM: unmonitoring %s (sysid %d)\n",
1083 host->nh_caller_name, host->nh_sysid);
1086 * We put our assigned system ID value in the priv field to
1087 * make it simpler to find the host if we are notified of a
1090 smmonid.mon_name = host->nh_caller_name;
1091 smmonid.my_id.my_name = "localhost";
1092 smmonid.my_id.my_prog = NLM_PROG;
1093 smmonid.my_id.my_vers = NLM_SM;
1094 smmonid.my_id.my_proc = NLM_SM_NOTIFY;
1098 stat = CLNT_CALL(nlm_nsm, SM_UNMON,
1099 (xdrproc_t) xdr_mon, &smmonid,
1100 (xdrproc_t) xdr_sm_stat, &smstat, timo);
1102 if (stat != RPC_SUCCESS) {
1103 printf("Failed to contact local NSM - rpc error %d\n", stat);
1106 if (smstat.res_stat == stat_fail) {
1107 printf("Local NSM refuses to unmonitor %s\n",
1108 host->nh_caller_name);
1112 host->nh_monstate = NLM_UNMONITORED;
1116 * Register this NLM host with the local NSM so that we can be
1117 * notified if it reboots.
1120 nlm_host_monitor(struct nlm_host *host, int state)
1124 struct timeval timo;
1125 enum clnt_stat stat;
1127 if (state && !host->nh_state) {
1129 * This is the first time we have seen an NSM state
1130 * value for this host. We record it here to help
1131 * detect host reboots.
1133 host->nh_state = state;
1134 if (nlm_debug_level >= 1)
1135 printf("NLM: host %s (sysid %d) has NSM state %d\n",
1136 host->nh_caller_name, host->nh_sysid, state);
1139 mtx_lock(&host->nh_lock);
1140 if (host->nh_monstate != NLM_UNMONITORED) {
1141 mtx_unlock(&host->nh_lock);
1144 host->nh_monstate = NLM_MONITORED;
1145 mtx_unlock(&host->nh_lock);
1147 if (nlm_debug_level >= 1)
1148 printf("NLM: monitoring %s (sysid %d)\n",
1149 host->nh_caller_name, host->nh_sysid);
1152 * We put our assigned system ID value in the priv field to
1153 * make it simpler to find the host if we are notified of a
1156 smmon.mon_id.mon_name = host->nh_caller_name;
1157 smmon.mon_id.my_id.my_name = "localhost";
1158 smmon.mon_id.my_id.my_prog = NLM_PROG;
1159 smmon.mon_id.my_id.my_vers = NLM_SM;
1160 smmon.mon_id.my_id.my_proc = NLM_SM_NOTIFY;
1161 memcpy(smmon.priv, &host->nh_sysid, sizeof(host->nh_sysid));
1165 stat = CLNT_CALL(nlm_nsm, SM_MON,
1166 (xdrproc_t) xdr_mon, &smmon,
1167 (xdrproc_t) xdr_sm_stat, &smstat, timo);
1169 if (stat != RPC_SUCCESS) {
1170 printf("Failed to contact local NSM - rpc error %d\n", stat);
1173 if (smstat.res_stat == stat_fail) {
1174 printf("Local NSM refuses to monitor %s\n",
1175 host->nh_caller_name);
1176 mtx_lock(&host->nh_lock);
1177 host->nh_monstate = NLM_MONITOR_FAILED;
1178 mtx_unlock(&host->nh_lock);
1182 host->nh_monstate = NLM_MONITORED;
1186 * Return an RPC client handle that can be used to talk to the NLM
1187 * running on the given host.
1190 nlm_host_get_rpc(struct nlm_host *host, bool_t isserver)
1192 struct nlm_rpc *rpc;
1195 mtx_lock(&host->nh_lock);
1198 rpc = &host->nh_srvrpc;
1200 rpc = &host->nh_clntrpc;
1203 * We can't hold onto RPC handles for too long - the async
1204 * call/reply protocol used by some NLM clients makes it hard
1205 * to tell when they change port numbers (e.g. after a
1206 * reboot). Note that if a client reboots while it isn't
1207 * holding any locks, it won't bother to notify us. We
1208 * expire the RPC handles after two minutes.
1210 if (rpc->nr_client && time_uptime > rpc->nr_create_time + 2*60) {
1211 client = rpc->nr_client;
1212 rpc->nr_client = NULL;
1213 mtx_unlock(&host->nh_lock);
1214 CLNT_RELEASE(client);
1215 mtx_lock(&host->nh_lock);
1218 if (!rpc->nr_client) {
1219 mtx_unlock(&host->nh_lock);
1220 client = nlm_get_rpc((struct sockaddr *)&host->nh_addr,
1221 NLM_PROG, host->nh_vers);
1222 mtx_lock(&host->nh_lock);
1225 if (rpc->nr_client) {
1226 mtx_unlock(&host->nh_lock);
1227 CLNT_DESTROY(client);
1228 mtx_lock(&host->nh_lock);
1230 rpc->nr_client = client;
1231 rpc->nr_create_time = time_uptime;
1236 client = rpc->nr_client;
1238 CLNT_ACQUIRE(client);
1239 mtx_unlock(&host->nh_lock);
1245 int nlm_host_get_sysid(struct nlm_host *host)
1248 return (host->nh_sysid);
1252 nlm_host_get_state(struct nlm_host *host)
1255 return (host->nh_state);
1259 nlm_register_wait_lock(struct nlm4_lock *lock, struct vnode *vp)
1261 struct nlm_waiting_lock *nw;
1263 nw = malloc(sizeof(struct nlm_waiting_lock), M_NLM, M_WAITOK);
1264 nw->nw_lock = *lock;
1265 memcpy(&nw->nw_fh.fh_bytes, nw->nw_lock.fh.n_bytes,
1266 nw->nw_lock.fh.n_len);
1267 nw->nw_lock.fh.n_bytes = nw->nw_fh.fh_bytes;
1268 nw->nw_waiting = TRUE;
1270 mtx_lock(&nlm_global_lock);
1271 TAILQ_INSERT_TAIL(&nlm_waiting_locks, nw, nw_link);
1272 mtx_unlock(&nlm_global_lock);
1278 nlm_deregister_wait_lock(void *handle)
1280 struct nlm_waiting_lock *nw = handle;
1282 mtx_lock(&nlm_global_lock);
1283 TAILQ_REMOVE(&nlm_waiting_locks, nw, nw_link);
1284 mtx_unlock(&nlm_global_lock);
1290 nlm_wait_lock(void *handle, int timo)
1292 struct nlm_waiting_lock *nw = handle;
1296 * If the granted message arrived before we got here,
1297 * nw->nw_waiting will be FALSE - in that case, don't sleep.
1299 mtx_lock(&nlm_global_lock);
1302 error = msleep(nw, &nlm_global_lock, PCATCH, "nlmlock", timo);
1303 TAILQ_REMOVE(&nlm_waiting_locks, nw, nw_link);
1306 * The granted message may arrive after the
1307 * interrupt/timeout but before we manage to lock the
1308 * mutex. Detect this by examining nw_lock.
1310 if (!nw->nw_waiting)
1314 * If nlm_cancel_wait is called, then error will be
1315 * zero but nw_waiting will still be TRUE. We
1316 * translate this into EINTR.
1321 mtx_unlock(&nlm_global_lock);
1329 nlm_cancel_wait(struct vnode *vp)
1331 struct nlm_waiting_lock *nw;
1333 mtx_lock(&nlm_global_lock);
1334 TAILQ_FOREACH(nw, &nlm_waiting_locks, nw_link) {
1335 if (nw->nw_vp == vp) {
1339 mtx_unlock(&nlm_global_lock);
1343 /**********************************************************************/
1346 * Syscall interface with userland.
1349 extern void nlm_prog_0(struct svc_req *rqstp, SVCXPRT *transp);
1350 extern void nlm_prog_1(struct svc_req *rqstp, SVCXPRT *transp);
1351 extern void nlm_prog_3(struct svc_req *rqstp, SVCXPRT *transp);
1352 extern void nlm_prog_4(struct svc_req *rqstp, SVCXPRT *transp);
1355 nlm_register_services(SVCPOOL *pool, int addr_count, char **addrs)
1357 static rpcvers_t versions[] = {
1358 NLM_SM, NLM_VERS, NLM_VERSX, NLM_VERS4
1360 static void (*dispatchers[])(struct svc_req *, SVCXPRT *) = {
1361 nlm_prog_0, nlm_prog_1, nlm_prog_3, nlm_prog_4
1363 static const int version_count = sizeof(versions) / sizeof(versions[0]);
1368 struct netconfig *nconf;
1372 printf("NLM: no service addresses given - can't start server");
1376 xprts = malloc(addr_count * sizeof(SVCXPRT *), M_NLM, M_WAITOK);
1377 for (i = 0; i < version_count; i++) {
1378 for (j = 0; j < addr_count; j++) {
1380 * Create transports for the first version and
1381 * then just register everything else to the
1387 error = copyin(&addrs[2*j], &up,
1391 error = copyinstr(up, netid, sizeof(netid),
1395 error = copyin(&addrs[2*j+1], &up,
1399 error = copyinstr(up, uaddr, sizeof(uaddr),
1403 nconf = getnetconfigent(netid);
1405 printf("Can't lookup netid %s\n",
1410 xprts[j] = svc_tp_create(pool, dispatchers[i],
1411 NLM_PROG, versions[i], uaddr, nconf);
1413 printf("NLM: unable to create "
1414 "(NLM_PROG, %d).\n", versions[i]);
1418 freenetconfigent(nconf);
1420 nconf = getnetconfigent(xprts[j]->xp_netid);
1421 rpcb_unset(NLM_PROG, versions[i], nconf);
1422 if (!svc_reg(xprts[j], NLM_PROG, versions[i],
1423 dispatchers[i], nconf)) {
1424 printf("NLM: can't register "
1425 "(NLM_PROG, %d)\n", versions[i]);
1439 * Main server entry point. Contacts the local NSM to get its current
1440 * state and send SM_UNMON_ALL. Registers the NLM services and then
1441 * services requests. Does not return until the server is interrupted
1445 nlm_server_main(int addr_count, char **addrs)
1447 struct thread *td = curthread;
1449 SVCPOOL *pool = NULL;
1453 struct sockaddr_in6 sin6;
1455 struct sockaddr_in sin;
1458 struct timeval timo;
1459 enum clnt_stat stat;
1460 struct nlm_host *host, *nhost;
1461 struct nlm_waiting_lock *nw;
1463 vop_advlock_t *old_nfs_advlock;
1464 vop_reclaim_t *old_nfs_reclaim;
1472 printf("NLM: can't start server - it appears to be running already\n");
1476 memset(&opt, 0, sizeof(opt));
1479 error = socreate(AF_INET, &nlm_socket, SOCK_DGRAM, 0,
1482 printf("NLM: can't create IPv4 socket - error %d\n", error);
1485 opt.sopt_dir = SOPT_SET;
1486 opt.sopt_level = IPPROTO_IP;
1487 opt.sopt_name = IP_PORTRANGE;
1488 portlow = IP_PORTRANGE_LOW;
1489 opt.sopt_val = &portlow;
1490 opt.sopt_valsize = sizeof(portlow);
1491 sosetopt(nlm_socket, &opt);
1495 error = socreate(AF_INET6, &nlm_socket6, SOCK_DGRAM, 0,
1498 printf("NLM: can't create IPv6 socket - error %d\n", error);
1502 opt.sopt_dir = SOPT_SET;
1503 opt.sopt_level = IPPROTO_IPV6;
1504 opt.sopt_name = IPV6_PORTRANGE;
1505 portlow = IPV6_PORTRANGE_LOW;
1506 opt.sopt_val = &portlow;
1507 opt.sopt_valsize = sizeof(portlow);
1508 sosetopt(nlm_socket6, &opt);
1511 nlm_auth = authunix_create(curthread->td_ucred);
1514 memset(&sin6, 0, sizeof(sin6));
1515 sin6.sin6_len = sizeof(sin6);
1516 sin6.sin6_family = AF_INET6;
1517 sin6.sin6_addr = in6addr_loopback;
1518 nlm_nsm = nlm_get_rpc((struct sockaddr *) &sin6, SM_PROG, SM_VERS);
1521 memset(&sin, 0, sizeof(sin));
1522 sin.sin_len = sizeof(sin);
1523 sin.sin_family = AF_INET;
1524 sin.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
1525 nlm_nsm = nlm_get_rpc((struct sockaddr *) &sin, SM_PROG,
1532 printf("Can't start NLM - unable to contact NSM\n");
1537 pool = svcpool_create("NLM", NULL);
1539 error = nlm_register_services(pool, addr_count, addrs);
1543 memset(&id, 0, sizeof(id));
1544 id.my_name = "NFS NLM";
1548 stat = CLNT_CALL(nlm_nsm, SM_UNMON_ALL,
1549 (xdrproc_t) xdr_my_id, &id,
1550 (xdrproc_t) xdr_sm_stat, &smstat, timo);
1552 if (stat != RPC_SUCCESS) {
1555 CLNT_GETERR(nlm_nsm, &err);
1556 printf("NLM: unexpected error contacting NSM, stat=%d, errno=%d\n",
1557 stat, err.re_errno);
1562 if (nlm_debug_level >= 1)
1563 printf("NLM: local NSM state is %d\n", smstat.state);
1564 nlm_nsm_state = smstat.state;
1567 old_nfs_advlock = nfs_advlock_p;
1568 nfs_advlock_p = nlm_advlock;
1569 old_nfs_reclaim = nfs_reclaim_p;
1570 nfs_reclaim_p = nlm_reclaim;
1577 nfs_advlock_p = old_nfs_advlock;
1578 nfs_reclaim_p = old_nfs_reclaim;
1583 svcpool_destroy(pool);
1586 * We are finished communicating with the NSM.
1589 CLNT_RELEASE(nlm_nsm);
1594 * Trash all the existing state so that if the server
1595 * restarts, it gets a clean slate. This is complicated by the
1596 * possibility that there may be other threads trying to make
1597 * client locking requests.
1599 * First we fake a client reboot notification which will
1600 * cancel any pending async locks and purge remote lock state
1601 * from the local lock manager. We release the reference from
1602 * nlm_hosts to the host (which may remove it from the list
1603 * and free it). After this phase, the only entries in the
1604 * nlm_host list should be from other threads performing
1605 * client lock requests. We arrange to defer closing the
1606 * sockets until the last RPC client handle is released.
1612 mtx_lock(&nlm_global_lock);
1613 TAILQ_FOREACH(nw, &nlm_waiting_locks, nw_link) {
1616 TAILQ_FOREACH_SAFE(host, &nlm_hosts, nh_link, nhost) {
1617 mtx_unlock(&nlm_global_lock);
1618 nlm_host_notify(host, 0);
1619 nlm_host_release(host);
1620 mtx_lock(&nlm_global_lock);
1622 TAILQ_FOREACH_SAFE(host, &nlm_hosts, nh_link, nhost) {
1623 mtx_lock(&host->nh_lock);
1624 if (host->nh_srvrpc.nr_client
1625 || host->nh_clntrpc.nr_client) {
1626 if (host->nh_addr.ss_family == AF_INET)
1629 if (host->nh_addr.ss_family == AF_INET6)
1633 * Note that the rpc over udp code copes
1634 * correctly with the fact that a socket may
1635 * be used by many rpc handles.
1637 if (host->nh_srvrpc.nr_client)
1638 CLNT_CONTROL(host->nh_srvrpc.nr_client,
1640 if (host->nh_clntrpc.nr_client)
1641 CLNT_CONTROL(host->nh_clntrpc.nr_client,
1644 mtx_unlock(&host->nh_lock);
1646 mtx_unlock(&nlm_global_lock);
1648 AUTH_DESTROY(nlm_auth);
1651 soclose(nlm_socket);
1655 soclose(nlm_socket6);
1663 nlm_syscall(struct thread *td, struct nlm_syscall_args *uap)
1667 #if __FreeBSD_version >= 700000
1668 error = priv_check(td, PRIV_NFS_LOCKD);
1675 nlm_debug_level = uap->debug_level;
1676 nlm_grace_threshold = time_uptime + uap->grace_period;
1677 nlm_next_idle_check = time_uptime + NLM_IDLE_PERIOD;
1679 return nlm_server_main(uap->addr_count, uap->addrs);
1682 /**********************************************************************/
1685 * NLM implementation details, called from the RPC stubs.
1690 nlm_sm_notify(struct nlm_sm_status *argp)
1693 struct nlm_host *host;
1695 if (nlm_debug_level >= 3)
1696 printf("nlm_sm_notify(): mon_name = %s\n", argp->mon_name);
1697 memcpy(&sysid, &argp->priv, sizeof(sysid));
1698 host = nlm_find_host_by_sysid(sysid);
1700 nlm_host_notify(host, argp->state);
1701 nlm_host_release(host);
1706 nlm_convert_to_fhandle_t(fhandle_t *fhp, struct netobj *p)
1708 memcpy(fhp, p->n_bytes, sizeof(fhandle_t));
1712 struct mount *vs_mp;
1713 struct vnode *vs_vp;
1719 nlm_get_vfs_state(struct nlm_host *host, struct svc_req *rqstp,
1720 fhandle_t *fhp, struct vfs_state *vs)
1723 struct ucred *cred = NULL, *credanon;
1725 memset(vs, 0, sizeof(*vs));
1727 vs->vs_mp = vfs_getvfs(&fhp->fh_fsid);
1731 vs->vs_vfslocked = VFS_LOCK_GIANT(vs->vs_mp);
1733 error = VFS_CHECKEXP(vs->vs_mp, (struct sockaddr *)&host->nh_addr,
1734 &exflags, &credanon, NULL, NULL);
1738 if (exflags & MNT_EXRDONLY || (vs->vs_mp->mnt_flag & MNT_RDONLY)) {
1743 error = VFS_FHTOVP(vs->vs_mp, &fhp->fh_fid, &vs->vs_vp);
1746 vs->vs_vnlocked = TRUE;
1748 if (!svc_getcred(rqstp, &cred, NULL)) {
1752 if (cred->cr_uid == 0 || (exflags & MNT_EXPORTANON)) {
1754 cred = crhold(credanon);
1760 error = VOP_ACCESS(vs->vs_vp, VWRITE, cred, curthread);
1764 #if __FreeBSD_version < 800011
1765 VOP_UNLOCK(vs->vs_vp, 0, curthread);
1767 VOP_UNLOCK(vs->vs_vp, 0);
1769 vs->vs_vnlocked = FALSE;
1779 nlm_release_vfs_state(struct vfs_state *vs)
1783 if (vs->vs_vnlocked)
1790 VFS_UNLOCK_GIANT(vs->vs_vfslocked);
1794 nlm_convert_error(int error)
1797 if (error == ESTALE)
1798 return nlm4_stale_fh;
1799 else if (error == EROFS)
1806 nlm_do_test(nlm4_testargs *argp, nlm4_testres *result, struct svc_req *rqstp,
1810 struct vfs_state vs;
1811 struct nlm_host *host, *bhost;
1815 memset(result, 0, sizeof(*result));
1816 memset(&vs, 0, sizeof(vs));
1818 host = nlm_find_host_by_name(argp->alock.caller_name,
1819 svc_getrpccaller(rqstp), rqstp->rq_vers);
1821 result->stat.stat = nlm4_denied_nolocks;
1825 if (nlm_debug_level >= 3)
1826 printf("nlm_do_test(): caller_name = %s (sysid = %d)\n",
1827 host->nh_caller_name, host->nh_sysid);
1829 nlm_free_finished_locks(host);
1830 sysid = host->nh_sysid;
1832 nlm_convert_to_fhandle_t(&fh, &argp->alock.fh);
1833 nlm_copy_netobj(&result->cookie, &argp->cookie, M_RPC);
1835 if (time_uptime < nlm_grace_threshold) {
1836 result->stat.stat = nlm4_denied_grace_period;
1840 error = nlm_get_vfs_state(host, rqstp, &fh, &vs);
1842 result->stat.stat = nlm_convert_error(error);
1846 fl.l_start = argp->alock.l_offset;
1847 fl.l_len = argp->alock.l_len;
1848 fl.l_pid = argp->alock.svid;
1850 fl.l_whence = SEEK_SET;
1851 if (argp->exclusive)
1852 fl.l_type = F_WRLCK;
1854 fl.l_type = F_RDLCK;
1855 error = VOP_ADVLOCK(vs.vs_vp, NULL, F_GETLK, &fl, F_REMOTE);
1857 result->stat.stat = nlm4_failed;
1861 if (fl.l_type == F_UNLCK) {
1862 result->stat.stat = nlm4_granted;
1864 result->stat.stat = nlm4_denied;
1865 result->stat.nlm4_testrply_u.holder.exclusive =
1866 (fl.l_type == F_WRLCK);
1867 result->stat.nlm4_testrply_u.holder.svid = fl.l_pid;
1868 bhost = nlm_find_host_by_sysid(fl.l_sysid);
1871 * We don't have any useful way of recording
1872 * the value of oh used in the original lock
1873 * request. Ideally, the test reply would have
1874 * a space for the owning host's name allowing
1875 * our caller's NLM to keep track.
1877 * As far as I can see, Solaris uses an eight
1878 * byte structure for oh which contains a four
1879 * byte pid encoded in local byte order and
1880 * the first four bytes of the host
1881 * name. Linux uses a variable length string
1882 * 'pid@hostname' in ascii but doesn't even
1883 * return that in test replies.
1885 * For the moment, return nothing in oh
1886 * (already zero'ed above).
1888 nlm_host_release(bhost);
1890 result->stat.nlm4_testrply_u.holder.l_offset = fl.l_start;
1891 result->stat.nlm4_testrply_u.holder.l_len = fl.l_len;
1895 nlm_release_vfs_state(&vs);
1897 *rpcp = nlm_host_get_rpc(host, TRUE);
1898 nlm_host_release(host);
1903 nlm_do_lock(nlm4_lockargs *argp, nlm4_res *result, struct svc_req *rqstp,
1904 bool_t monitor, CLIENT **rpcp)
1907 struct vfs_state vs;
1908 struct nlm_host *host;
1912 memset(result, 0, sizeof(*result));
1913 memset(&vs, 0, sizeof(vs));
1915 host = nlm_find_host_by_name(argp->alock.caller_name,
1916 svc_getrpccaller(rqstp), rqstp->rq_vers);
1918 result->stat.stat = nlm4_denied_nolocks;
1922 if (nlm_debug_level >= 3)
1923 printf("nlm_do_lock(): caller_name = %s (sysid = %d)\n",
1924 host->nh_caller_name, host->nh_sysid);
1926 if (monitor && host->nh_state && argp->state
1927 && host->nh_state != argp->state) {
1929 * The host rebooted without telling us. Trash its
1932 nlm_host_notify(host, argp->state);
1935 nlm_free_finished_locks(host);
1936 sysid = host->nh_sysid;
1938 nlm_convert_to_fhandle_t(&fh, &argp->alock.fh);
1939 nlm_copy_netobj(&result->cookie, &argp->cookie, M_RPC);
1941 if (time_uptime < nlm_grace_threshold && !argp->reclaim) {
1942 result->stat.stat = nlm4_denied_grace_period;
1946 error = nlm_get_vfs_state(host, rqstp, &fh, &vs);
1948 result->stat.stat = nlm_convert_error(error);
1952 fl.l_start = argp->alock.l_offset;
1953 fl.l_len = argp->alock.l_len;
1954 fl.l_pid = argp->alock.svid;
1956 fl.l_whence = SEEK_SET;
1957 if (argp->exclusive)
1958 fl.l_type = F_WRLCK;
1960 fl.l_type = F_RDLCK;
1962 struct nlm_async_lock *af;
1966 * First, make sure we can contact the host's NLM.
1968 client = nlm_host_get_rpc(host, TRUE);
1970 result->stat.stat = nlm4_failed;
1975 * First we need to check and see if there is an
1976 * existing blocked lock that matches. This could be a
1977 * badly behaved client or an RPC re-send. If we find
1978 * one, just return nlm4_blocked.
1980 mtx_lock(&host->nh_lock);
1981 TAILQ_FOREACH(af, &host->nh_pending, af_link) {
1982 if (af->af_fl.l_start == fl.l_start
1983 && af->af_fl.l_len == fl.l_len
1984 && af->af_fl.l_pid == fl.l_pid
1985 && af->af_fl.l_type == fl.l_type) {
1989 mtx_unlock(&host->nh_lock);
1991 CLNT_RELEASE(client);
1992 result->stat.stat = nlm4_blocked;
1996 af = malloc(sizeof(struct nlm_async_lock), M_NLM,
1998 TASK_INIT(&af->af_task, 0, nlm_lock_callback, af);
1999 af->af_vp = vs.vs_vp;
2002 af->af_rpc = client;
2004 * We use M_RPC here so that we can xdr_free the thing
2007 af->af_granted.exclusive = argp->exclusive;
2008 af->af_granted.alock.caller_name =
2009 strdup(argp->alock.caller_name, M_RPC);
2010 nlm_copy_netobj(&af->af_granted.alock.fh,
2011 &argp->alock.fh, M_RPC);
2012 nlm_copy_netobj(&af->af_granted.alock.oh,
2013 &argp->alock.oh, M_RPC);
2014 af->af_granted.alock.svid = argp->alock.svid;
2015 af->af_granted.alock.l_offset = argp->alock.l_offset;
2016 af->af_granted.alock.l_len = argp->alock.l_len;
2019 * Put the entry on the pending list before calling
2020 * VOP_ADVLOCKASYNC. We do this in case the lock
2021 * request was blocked (returning EINPROGRESS) but
2022 * then granted before we manage to run again. The
2023 * client may receive the granted message before we
2024 * send our blocked reply but thats their problem.
2026 mtx_lock(&host->nh_lock);
2027 TAILQ_INSERT_TAIL(&host->nh_pending, af, af_link);
2028 mtx_unlock(&host->nh_lock);
2030 error = VOP_ADVLOCKASYNC(vs.vs_vp, NULL, F_SETLK, &fl, F_REMOTE,
2031 &af->af_task, &af->af_cookie);
2034 * If the lock completed synchronously, just free the
2035 * tracking structure now.
2037 if (error != EINPROGRESS) {
2038 CLNT_RELEASE(af->af_rpc);
2039 mtx_lock(&host->nh_lock);
2040 TAILQ_REMOVE(&host->nh_pending, af, af_link);
2041 mtx_unlock(&host->nh_lock);
2042 xdr_free((xdrproc_t) xdr_nlm4_testargs,
2046 if (nlm_debug_level >= 2)
2047 printf("NLM: pending async lock %p for %s "
2049 af, host->nh_caller_name, sysid);
2051 * Don't vrele the vnode just yet - this must
2052 * wait until either the async callback
2053 * happens or the lock is cancelled.
2058 error = VOP_ADVLOCK(vs.vs_vp, NULL, F_SETLK, &fl, F_REMOTE);
2062 if (error == EINPROGRESS) {
2063 result->stat.stat = nlm4_blocked;
2064 } else if (error == EDEADLK) {
2065 result->stat.stat = nlm4_deadlck;
2066 } else if (error == EAGAIN) {
2067 result->stat.stat = nlm4_denied;
2069 result->stat.stat = nlm4_failed;
2073 nlm_host_monitor(host, argp->state);
2074 result->stat.stat = nlm4_granted;
2078 nlm_release_vfs_state(&vs);
2080 *rpcp = nlm_host_get_rpc(host, TRUE);
2081 nlm_host_release(host);
2086 nlm_do_cancel(nlm4_cancargs *argp, nlm4_res *result, struct svc_req *rqstp,
2090 struct vfs_state vs;
2091 struct nlm_host *host;
2094 struct nlm_async_lock *af;
2096 memset(result, 0, sizeof(*result));
2097 memset(&vs, 0, sizeof(vs));
2099 host = nlm_find_host_by_name(argp->alock.caller_name,
2100 svc_getrpccaller(rqstp), rqstp->rq_vers);
2102 result->stat.stat = nlm4_denied_nolocks;
2106 if (nlm_debug_level >= 3)
2107 printf("nlm_do_cancel(): caller_name = %s (sysid = %d)\n",
2108 host->nh_caller_name, host->nh_sysid);
2110 nlm_free_finished_locks(host);
2111 sysid = host->nh_sysid;
2113 nlm_convert_to_fhandle_t(&fh, &argp->alock.fh);
2114 nlm_copy_netobj(&result->cookie, &argp->cookie, M_RPC);
2116 if (time_uptime < nlm_grace_threshold) {
2117 result->stat.stat = nlm4_denied_grace_period;
2121 error = nlm_get_vfs_state(host, rqstp, &fh, &vs);
2123 result->stat.stat = nlm_convert_error(error);
2127 fl.l_start = argp->alock.l_offset;
2128 fl.l_len = argp->alock.l_len;
2129 fl.l_pid = argp->alock.svid;
2131 fl.l_whence = SEEK_SET;
2132 if (argp->exclusive)
2133 fl.l_type = F_WRLCK;
2135 fl.l_type = F_RDLCK;
2138 * First we need to try and find the async lock request - if
2139 * there isn't one, we give up and return nlm4_denied.
2141 mtx_lock(&host->nh_lock);
2143 TAILQ_FOREACH(af, &host->nh_pending, af_link) {
2144 if (af->af_fl.l_start == fl.l_start
2145 && af->af_fl.l_len == fl.l_len
2146 && af->af_fl.l_pid == fl.l_pid
2147 && af->af_fl.l_type == fl.l_type) {
2153 mtx_unlock(&host->nh_lock);
2154 result->stat.stat = nlm4_denied;
2158 error = nlm_cancel_async_lock(af);
2161 result->stat.stat = nlm4_denied;
2163 result->stat.stat = nlm4_granted;
2166 mtx_unlock(&host->nh_lock);
2169 nlm_release_vfs_state(&vs);
2171 *rpcp = nlm_host_get_rpc(host, TRUE);
2172 nlm_host_release(host);
2177 nlm_do_unlock(nlm4_unlockargs *argp, nlm4_res *result, struct svc_req *rqstp,
2181 struct vfs_state vs;
2182 struct nlm_host *host;
2186 memset(result, 0, sizeof(*result));
2187 memset(&vs, 0, sizeof(vs));
2189 host = nlm_find_host_by_name(argp->alock.caller_name,
2190 svc_getrpccaller(rqstp), rqstp->rq_vers);
2192 result->stat.stat = nlm4_denied_nolocks;
2196 if (nlm_debug_level >= 3)
2197 printf("nlm_do_unlock(): caller_name = %s (sysid = %d)\n",
2198 host->nh_caller_name, host->nh_sysid);
2200 nlm_free_finished_locks(host);
2201 sysid = host->nh_sysid;
2203 nlm_convert_to_fhandle_t(&fh, &argp->alock.fh);
2204 nlm_copy_netobj(&result->cookie, &argp->cookie, M_RPC);
2206 if (time_uptime < nlm_grace_threshold) {
2207 result->stat.stat = nlm4_denied_grace_period;
2211 error = nlm_get_vfs_state(host, rqstp, &fh, &vs);
2213 result->stat.stat = nlm_convert_error(error);
2217 fl.l_start = argp->alock.l_offset;
2218 fl.l_len = argp->alock.l_len;
2219 fl.l_pid = argp->alock.svid;
2221 fl.l_whence = SEEK_SET;
2222 fl.l_type = F_UNLCK;
2223 error = VOP_ADVLOCK(vs.vs_vp, NULL, F_UNLCK, &fl, F_REMOTE);
2226 * Ignore the error - there is no result code for failure,
2227 * only for grace period.
2229 result->stat.stat = nlm4_granted;
2232 nlm_release_vfs_state(&vs);
2234 *rpcp = nlm_host_get_rpc(host, TRUE);
2235 nlm_host_release(host);
2240 nlm_do_granted(nlm4_testargs *argp, nlm4_res *result, struct svc_req *rqstp,
2244 struct nlm_host *host;
2245 struct nlm_waiting_lock *nw;
2247 memset(result, 0, sizeof(*result));
2249 host = nlm_find_host_by_addr(svc_getrpccaller(rqstp), rqstp->rq_vers);
2251 result->stat.stat = nlm4_denied_nolocks;
2255 nlm_copy_netobj(&result->cookie, &argp->cookie, M_RPC);
2256 result->stat.stat = nlm4_denied;
2258 mtx_lock(&nlm_global_lock);
2259 TAILQ_FOREACH(nw, &nlm_waiting_locks, nw_link) {
2260 if (!nw->nw_waiting)
2262 if (argp->alock.svid == nw->nw_lock.svid
2263 && argp->alock.l_offset == nw->nw_lock.l_offset
2264 && argp->alock.l_len == nw->nw_lock.l_len
2265 && argp->alock.fh.n_len == nw->nw_lock.fh.n_len
2266 && !memcmp(argp->alock.fh.n_bytes, nw->nw_lock.fh.n_bytes,
2267 nw->nw_lock.fh.n_len)) {
2268 nw->nw_waiting = FALSE;
2270 result->stat.stat = nlm4_granted;
2274 mtx_unlock(&nlm_global_lock);
2276 *rpcp = nlm_host_get_rpc(host, TRUE);
2277 nlm_host_release(host);
2282 nlm_do_free_all(nlm4_notify *argp)
2284 struct nlm_host *host, *thost;
2286 TAILQ_FOREACH_SAFE(host, &nlm_hosts, nh_link, thost) {
2287 if (!strcmp(host->nh_caller_name, argp->name))
2288 nlm_host_notify(host, argp->state);
2293 * Kernel module glue
2296 nfslockd_modevent(module_t mod, int type, void *data)
2301 static moduledata_t nfslockd_mod = {
2306 DECLARE_MODULE(nfslockd, nfslockd_mod, SI_SUB_VFS, SI_ORDER_ANY);
2308 /* So that loader and kldload(2) can find us, wherever we are.. */
2309 MODULE_DEPEND(nfslockd, krpc, 1, 1, 1);
2310 MODULE_DEPEND(nfslockd, nfs, 1, 1, 1);
2311 MODULE_VERSION(nfslockd, 1);