2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2008 Isilon Inc http://www.isilon.com/
5 * Authors: Doug Rabson <dfr@rabson.org>
6 * Developed with Red Inc: Alfred Perlstein <alfred@freebsd.org>
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 #include "opt_inet6.h"
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
35 #include <sys/param.h>
37 #include <sys/fcntl.h>
38 #include <sys/kernel.h>
39 #include <sys/kthread.h>
40 #include <sys/lockf.h>
41 #include <sys/malloc.h>
42 #include <sys/mount.h>
45 #include <sys/socket.h>
46 #include <sys/socketvar.h>
47 #include <sys/syscall.h>
48 #include <sys/sysctl.h>
49 #include <sys/sysent.h>
50 #include <sys/syslog.h>
51 #include <sys/sysproto.h>
52 #include <sys/systm.h>
53 #include <sys/taskqueue.h>
54 #include <sys/unistd.h>
55 #include <sys/vnode.h>
57 #include <nfs/nfsproto.h>
58 #include <nfs/nfs_lock.h>
60 #include <nlm/nlm_prot.h>
61 #include <nlm/sm_inter.h>
63 #include <rpc/rpc_com.h>
64 #include <rpc/rpcb_prot.h>
66 MALLOC_DEFINE(M_NLM, "NLM", "Network Lock Manager");
69 * If a host is inactive (and holds no locks) for this amount of
70 * seconds, we consider it idle and stop tracking it.
72 #define NLM_IDLE_TIMEOUT 30
75 * We check the host list for idle every few seconds.
77 #define NLM_IDLE_PERIOD 5
80 * We only look for GRANTED_RES messages for a little while.
82 #define NLM_EXPIRE_TIMEOUT 10
85 * Support for sysctl vfs.nlm.sysid
87 static SYSCTL_NODE(_vfs, OID_AUTO, nlm, CTLFLAG_RW | CTLFLAG_MPSAFE, NULL,
88 "Network Lock Manager");
89 static SYSCTL_NODE(_vfs_nlm, OID_AUTO, sysid,
90 CTLFLAG_RW | CTLFLAG_MPSAFE, NULL,
96 static struct syscall_helper_data nlm_syscalls[] = {
97 SYSCALL_INIT_HELPER(nlm_syscall),
102 * Debug level passed in from userland. We also support a sysctl hook
103 * so that it can be changed on a live system.
105 static int nlm_debug_level;
106 SYSCTL_INT(_debug, OID_AUTO, nlm_debug, CTLFLAG_RW, &nlm_debug_level, 0, "");
108 #define NLM_DEBUG(_level, args...) \
110 if (nlm_debug_level >= (_level)) \
111 log(LOG_DEBUG, args); \
113 #define NLM_ERR(args...) \
115 log(LOG_ERR, args); \
119 * Grace period handling. The value of nlm_grace_threshold is the
120 * value of time_uptime after which we are serving requests normally.
122 static time_t nlm_grace_threshold;
125 * We check for idle hosts if time_uptime is greater than
126 * nlm_next_idle_check,
128 static time_t nlm_next_idle_check;
131 * A flag to indicate the server is already running.
133 static int nlm_is_running;
136 * A socket to use for RPC - shared by all IPv4 RPC clients.
138 static struct socket *nlm_socket;
143 * A socket to use for RPC - shared by all IPv6 RPC clients.
145 static struct socket *nlm_socket6;
150 * An RPC client handle that can be used to communicate with the local
153 static CLIENT *nlm_nsm;
156 * An AUTH handle for the server's creds.
158 static AUTH *nlm_auth;
161 * A zero timeval for sending async RPC messages.
163 struct timeval nlm_zero_tv = { 0, 0 };
166 * The local NSM state number
172 * A lock to protect the host list and waiting lock list.
174 static struct mtx nlm_global_lock;
178 * (l) locked by nh_lock
179 * (s) only accessed via server RPC which is single threaded
180 * (g) locked by nlm_global_lock
181 * (c) const until freeing
182 * (a) modified using atomic ops
186 * A pending client-side lock request, stored on the nlm_waiting_locks
189 struct nlm_waiting_lock {
190 TAILQ_ENTRY(nlm_waiting_lock) nw_link; /* (g) */
191 bool_t nw_waiting; /* (g) */
192 nlm4_lock nw_lock; /* (c) */
193 union nfsfh nw_fh; /* (c) */
194 struct vnode *nw_vp; /* (c) */
196 TAILQ_HEAD(nlm_waiting_lock_list, nlm_waiting_lock);
198 struct nlm_waiting_lock_list nlm_waiting_locks; /* (g) */
201 * A pending server-side asynchronous lock request, stored on the
202 * nh_pending list of the NLM host.
204 struct nlm_async_lock {
205 TAILQ_ENTRY(nlm_async_lock) af_link; /* (l) host's list of locks */
206 struct task af_task; /* (c) async callback details */
207 void *af_cookie; /* (l) lock manager cancel token */
208 struct vnode *af_vp; /* (l) vnode to lock */
209 struct flock af_fl; /* (c) lock details */
210 struct nlm_host *af_host; /* (c) host which is locking */
211 CLIENT *af_rpc; /* (c) rpc client to send message */
212 nlm4_testargs af_granted; /* (c) notification details */
213 time_t af_expiretime; /* (c) notification time */
215 TAILQ_HEAD(nlm_async_lock_list, nlm_async_lock);
220 enum nlm_host_state {
228 CLIENT *nr_client; /* (l) RPC client handle */
229 time_t nr_create_time; /* (l) when client was created */
234 volatile u_int nh_refs; /* (a) reference count */
235 TAILQ_ENTRY(nlm_host) nh_link; /* (g) global list of hosts */
236 char nh_caller_name[MAXNAMELEN]; /* (c) printable name of host */
237 uint32_t nh_sysid; /* (c) our allocaed system ID */
238 char nh_sysid_string[10]; /* (c) string rep. of sysid */
239 struct sockaddr_storage nh_addr; /* (s) remote address of host */
240 struct nlm_rpc nh_srvrpc; /* (l) RPC for server replies */
241 struct nlm_rpc nh_clntrpc; /* (l) RPC for client requests */
242 rpcvers_t nh_vers; /* (s) NLM version of host */
243 int nh_state; /* (s) last seen NSM state of host */
244 enum nlm_host_state nh_monstate; /* (l) local NSM monitoring state */
245 time_t nh_idle_timeout; /* (s) Time at which host is idle */
246 struct sysctl_ctx_list nh_sysctl; /* (c) vfs.nlm.sysid nodes */
247 uint32_t nh_grantcookie; /* (l) grant cookie counter */
248 struct nlm_async_lock_list nh_pending; /* (l) pending async locks */
249 struct nlm_async_lock_list nh_granted; /* (l) granted locks */
250 struct nlm_async_lock_list nh_finished; /* (l) finished async locks */
252 TAILQ_HEAD(nlm_host_list, nlm_host);
254 static struct nlm_host_list nlm_hosts; /* (g) */
255 static uint32_t nlm_next_sysid = 1; /* (g) */
257 static void nlm_host_unmonitor(struct nlm_host *);
259 struct nlm_grantcookie {
264 static inline uint32_t
265 ng_sysid(struct netobj *src)
268 return ((struct nlm_grantcookie *)src->n_bytes)->ng_sysid;
271 static inline uint32_t
272 ng_cookie(struct netobj *src)
275 return ((struct nlm_grantcookie *)src->n_bytes)->ng_cookie;
278 /**********************************************************************/
281 * Initialise NLM globals.
288 mtx_init(&nlm_global_lock, "nlm_global_lock", NULL, MTX_DEF);
289 TAILQ_INIT(&nlm_waiting_locks);
290 TAILQ_INIT(&nlm_hosts);
292 error = syscall_helper_register(nlm_syscalls, SY_THR_STATIC_KLD);
294 NLM_ERR("Can't register NLM syscall\n");
302 syscall_helper_unregister(nlm_syscalls);
306 * Create a netobj from an arbitrary source.
309 nlm_make_netobj(struct netobj *dst, caddr_t src, size_t srcsize,
310 struct malloc_type *type)
313 dst->n_len = srcsize;
314 dst->n_bytes = malloc(srcsize, type, M_WAITOK);
315 memcpy(dst->n_bytes, src, srcsize);
319 * Copy a struct netobj.
322 nlm_copy_netobj(struct netobj *dst, struct netobj *src,
323 struct malloc_type *type)
326 nlm_make_netobj(dst, src->n_bytes, src->n_len, type);
331 * Create an RPC client handle for the given (address,prog,vers)
335 nlm_get_rpc(struct sockaddr *sa, rpcprog_t prog, rpcvers_t vers)
337 char *wchan = "nlmrcv";
338 struct sockaddr_storage ss;
344 enum clnt_stat stat = RPC_SUCCESS;
345 int rpcvers = RPCBVERS4;
346 bool_t do_tcp = FALSE;
347 bool_t tryagain = FALSE;
348 struct portmap mapping;
352 * First we need to contact the remote RPCBIND service to find
355 memcpy(&ss, sa, sa->sa_len);
356 switch (ss.ss_family) {
358 ((struct sockaddr_in *)&ss)->sin_port = htons(111);
363 ((struct sockaddr_in6 *)&ss)->sin6_port = htons(111);
370 * Unsupported address family - fail.
375 rpcb = clnt_dg_create(so, (struct sockaddr *)&ss,
376 RPCBPROG, rpcvers, 0, 0);
384 parms.r_netid = "tcp";
386 parms.r_netid = "udp";
391 * Use the default timeout.
400 * Try RPCBIND 4 then 3.
403 stat = CLNT_CALL(rpcb, (rpcprog_t) RPCBPROC_GETADDR,
404 (xdrproc_t) xdr_rpcb, &parms,
405 (xdrproc_t) xdr_wrapstring, &uaddr, timo);
406 if (stat == RPC_SUCCESS) {
408 * We have a reply from the remote RPCBIND - turn it
409 * into an appropriate address and make a new client
410 * that can talk to the remote NLM.
412 * XXX fixup IPv6 scope ID.
415 a = __rpc_uaddr2taddr_af(ss.ss_family, uaddr);
420 memcpy(&ss, a->buf, a->len);
423 xdr_free((xdrproc_t) xdr_wrapstring, &uaddr);
426 if (tryagain || stat == RPC_PROGVERSMISMATCH) {
427 if (rpcvers == RPCBVERS4)
429 else if (rpcvers == RPCBVERS)
431 CLNT_CONTROL(rpcb, CLSET_VERS, &rpcvers);
439 mapping.pm_prog = parms.r_prog;
440 mapping.pm_vers = parms.r_vers;
441 mapping.pm_prot = do_tcp ? IPPROTO_TCP : IPPROTO_UDP;
444 stat = CLNT_CALL(rpcb, (rpcprog_t) PMAPPROC_GETPORT,
445 (xdrproc_t) xdr_portmap, &mapping,
446 (xdrproc_t) xdr_u_short, &port, timo);
448 if (stat == RPC_SUCCESS) {
449 switch (ss.ss_family) {
451 ((struct sockaddr_in *)&ss)->sin_port =
457 ((struct sockaddr_in6 *)&ss)->sin6_port =
465 panic("invalid rpcvers %d", rpcvers);
468 * We may have a positive response from the portmapper, but the NLM
469 * service was not found. Make sure we received a valid port.
471 switch (ss.ss_family) {
473 port = ((struct sockaddr_in *)&ss)->sin_port;
477 port = ((struct sockaddr_in6 *)&ss)->sin6_port;
481 if (stat != RPC_SUCCESS || !port) {
483 * If we were able to talk to rpcbind or portmap, but the udp
484 * variant wasn't available, ask about tcp.
486 * XXX - We could also check for a TCP portmapper, but
487 * if the host is running a portmapper at all, we should be able
488 * to hail it over UDP.
490 if (stat == RPC_SUCCESS && !do_tcp) {
495 /* Otherwise, bad news. */
496 NLM_ERR("NLM: failed to contact remote rpcbind, "
497 "stat = %d, port = %d\n", (int) stat, port);
504 * Destroy the UDP client we used to speak to rpcbind and
505 * recreate as a TCP client.
507 struct netconfig *nconf = NULL;
511 switch (ss.ss_family) {
513 nconf = getnetconfigent("tcp");
517 nconf = getnetconfigent("tcp6");
522 rpcb = clnt_reconnect_create(nconf, (struct sockaddr *)&ss,
524 CLNT_CONTROL(rpcb, CLSET_WAITCHAN, wchan);
525 rpcb->cl_auth = nlm_auth;
529 * Re-use the client we used to speak to rpcbind.
531 CLNT_CONTROL(rpcb, CLSET_SVC_ADDR, &ss);
532 CLNT_CONTROL(rpcb, CLSET_PROG, &prog);
533 CLNT_CONTROL(rpcb, CLSET_VERS, &vers);
534 CLNT_CONTROL(rpcb, CLSET_WAITCHAN, wchan);
535 rpcb->cl_auth = nlm_auth;
542 * This async callback after when an async lock request has been
543 * granted. We notify the host which initiated the request.
546 nlm_lock_callback(void *arg, int pending)
548 struct nlm_async_lock *af = (struct nlm_async_lock *) arg;
549 struct rpc_callextra ext;
551 NLM_DEBUG(2, "NLM: async lock %p for %s (sysid %d) granted,"
552 " cookie %d:%d\n", af, af->af_host->nh_caller_name,
553 af->af_host->nh_sysid, ng_sysid(&af->af_granted.cookie),
554 ng_cookie(&af->af_granted.cookie));
557 * Send the results back to the host.
559 * Note: there is a possible race here with nlm_host_notify
560 * destroying the RPC client. To avoid problems, the first
561 * thing nlm_host_notify does is to cancel pending async lock
564 memset(&ext, 0, sizeof(ext));
565 ext.rc_auth = nlm_auth;
566 if (af->af_host->nh_vers == NLM_VERS4) {
567 nlm4_granted_msg_4(&af->af_granted,
568 NULL, af->af_rpc, &ext, nlm_zero_tv);
571 * Back-convert to legacy protocol
573 nlm_testargs granted;
574 granted.cookie = af->af_granted.cookie;
575 granted.exclusive = af->af_granted.exclusive;
576 granted.alock.caller_name =
577 af->af_granted.alock.caller_name;
578 granted.alock.fh = af->af_granted.alock.fh;
579 granted.alock.oh = af->af_granted.alock.oh;
580 granted.alock.svid = af->af_granted.alock.svid;
581 granted.alock.l_offset =
582 af->af_granted.alock.l_offset;
583 granted.alock.l_len =
584 af->af_granted.alock.l_len;
586 nlm_granted_msg_1(&granted,
587 NULL, af->af_rpc, &ext, nlm_zero_tv);
591 * Move this entry to the nh_granted list.
593 af->af_expiretime = time_uptime + NLM_EXPIRE_TIMEOUT;
594 mtx_lock(&af->af_host->nh_lock);
595 TAILQ_REMOVE(&af->af_host->nh_pending, af, af_link);
596 TAILQ_INSERT_TAIL(&af->af_host->nh_granted, af, af_link);
597 mtx_unlock(&af->af_host->nh_lock);
601 * Free an async lock request. The request must have been removed from
605 nlm_free_async_lock(struct nlm_async_lock *af)
608 * Free an async lock.
611 CLNT_RELEASE(af->af_rpc);
612 xdr_free((xdrproc_t) xdr_nlm4_testargs, &af->af_granted);
619 * Cancel our async request - this must be called with
620 * af->nh_host->nh_lock held. This is slightly complicated by a
621 * potential race with our own callback. If we fail to cancel the
622 * lock, it must already have been granted - we make sure our async
623 * task has completed by calling taskqueue_drain in this case.
626 nlm_cancel_async_lock(struct nlm_async_lock *af)
628 struct nlm_host *host = af->af_host;
631 mtx_assert(&host->nh_lock, MA_OWNED);
633 mtx_unlock(&host->nh_lock);
635 error = VOP_ADVLOCKASYNC(af->af_vp, NULL, F_CANCEL, &af->af_fl,
636 F_REMOTE, NULL, &af->af_cookie);
640 * We failed to cancel - make sure our callback has
641 * completed before we continue.
643 taskqueue_drain(taskqueue_thread, &af->af_task);
646 mtx_lock(&host->nh_lock);
649 NLM_DEBUG(2, "NLM: async lock %p for %s (sysid %d) "
650 "cancelled\n", af, host->nh_caller_name, host->nh_sysid);
653 * Remove from the nh_pending list and free now that
654 * we are safe from the callback.
656 TAILQ_REMOVE(&host->nh_pending, af, af_link);
657 mtx_unlock(&host->nh_lock);
658 nlm_free_async_lock(af);
659 mtx_lock(&host->nh_lock);
666 nlm_check_expired_locks(struct nlm_host *host)
668 struct nlm_async_lock *af;
669 time_t uptime = time_uptime;
671 mtx_lock(&host->nh_lock);
672 while ((af = TAILQ_FIRST(&host->nh_granted)) != NULL
673 && uptime >= af->af_expiretime) {
674 NLM_DEBUG(2, "NLM: async lock %p for %s (sysid %d) expired,"
675 " cookie %d:%d\n", af, af->af_host->nh_caller_name,
676 af->af_host->nh_sysid, ng_sysid(&af->af_granted.cookie),
677 ng_cookie(&af->af_granted.cookie));
678 TAILQ_REMOVE(&host->nh_granted, af, af_link);
679 mtx_unlock(&host->nh_lock);
680 nlm_free_async_lock(af);
681 mtx_lock(&host->nh_lock);
683 while ((af = TAILQ_FIRST(&host->nh_finished)) != NULL) {
684 TAILQ_REMOVE(&host->nh_finished, af, af_link);
685 mtx_unlock(&host->nh_lock);
686 nlm_free_async_lock(af);
687 mtx_lock(&host->nh_lock);
689 mtx_unlock(&host->nh_lock);
693 * Free resources used by a host. This is called after the reference
694 * count has reached zero so it doesn't need to worry about locks.
697 nlm_host_destroy(struct nlm_host *host)
700 mtx_lock(&nlm_global_lock);
701 TAILQ_REMOVE(&nlm_hosts, host, nh_link);
702 mtx_unlock(&nlm_global_lock);
704 if (host->nh_srvrpc.nr_client)
705 CLNT_RELEASE(host->nh_srvrpc.nr_client);
706 if (host->nh_clntrpc.nr_client)
707 CLNT_RELEASE(host->nh_clntrpc.nr_client);
708 mtx_destroy(&host->nh_lock);
709 sysctl_ctx_free(&host->nh_sysctl);
714 * Thread start callback for client lock recovery
717 nlm_client_recovery_start(void *arg)
719 struct nlm_host *host = (struct nlm_host *) arg;
721 NLM_DEBUG(1, "NLM: client lock recovery for %s started\n",
722 host->nh_caller_name);
724 nlm_client_recovery(host);
726 NLM_DEBUG(1, "NLM: client lock recovery for %s completed\n",
727 host->nh_caller_name);
729 host->nh_monstate = NLM_MONITORED;
730 nlm_host_release(host);
736 * This is called when we receive a host state change notification. We
737 * unlock any active locks owned by the host. When rpc.lockd is
738 * shutting down, this function is called with newstate set to zero
739 * which allows us to cancel any pending async locks and clear the
743 nlm_host_notify(struct nlm_host *host, int newstate)
745 struct nlm_async_lock *af;
748 NLM_DEBUG(1, "NLM: host %s (sysid %d) rebooted, new "
749 "state is %d\n", host->nh_caller_name,
750 host->nh_sysid, newstate);
754 * Cancel any pending async locks for this host.
756 mtx_lock(&host->nh_lock);
757 while ((af = TAILQ_FIRST(&host->nh_pending)) != NULL) {
759 * nlm_cancel_async_lock will remove the entry from
760 * nh_pending and free it.
762 nlm_cancel_async_lock(af);
764 mtx_unlock(&host->nh_lock);
765 nlm_check_expired_locks(host);
768 * The host just rebooted - trash its locks.
770 lf_clearremotesys(host->nh_sysid);
771 host->nh_state = newstate;
774 * If we have any remote locks for this host (i.e. it
775 * represents a remote NFS server that our local NFS client
776 * has locks for), start a recovery thread.
779 && host->nh_monstate != NLM_RECOVERING
780 && lf_countlocks(NLM_SYSID_CLIENT | host->nh_sysid) > 0) {
782 host->nh_monstate = NLM_RECOVERING;
783 refcount_acquire(&host->nh_refs);
784 kthread_add(nlm_client_recovery_start, host, curproc, &td, 0, 0,
785 "NFS lock recovery for %s", host->nh_caller_name);
790 * Sysctl handler to count the number of locks for a sysid.
793 nlm_host_lock_count_sysctl(SYSCTL_HANDLER_ARGS)
795 struct nlm_host *host;
798 host = oidp->oid_arg1;
799 count = lf_countlocks(host->nh_sysid);
800 return sysctl_handle_int(oidp, &count, 0, req);
804 * Sysctl handler to count the number of client locks for a sysid.
807 nlm_host_client_lock_count_sysctl(SYSCTL_HANDLER_ARGS)
809 struct nlm_host *host;
812 host = oidp->oid_arg1;
813 count = lf_countlocks(NLM_SYSID_CLIENT | host->nh_sysid);
814 return sysctl_handle_int(oidp, &count, 0, req);
818 * Create a new NLM host.
820 static struct nlm_host *
821 nlm_create_host(const char* caller_name)
823 struct nlm_host *host;
824 struct sysctl_oid *oid;
826 mtx_assert(&nlm_global_lock, MA_OWNED);
828 NLM_DEBUG(1, "NLM: new host %s (sysid %d)\n",
829 caller_name, nlm_next_sysid);
830 host = malloc(sizeof(struct nlm_host), M_NLM, M_NOWAIT|M_ZERO);
833 mtx_init(&host->nh_lock, "nh_lock", NULL, MTX_DEF);
835 strlcpy(host->nh_caller_name, caller_name, MAXNAMELEN);
836 host->nh_sysid = nlm_next_sysid++;
837 snprintf(host->nh_sysid_string, sizeof(host->nh_sysid_string),
838 "%d", host->nh_sysid);
841 host->nh_monstate = NLM_UNMONITORED;
842 host->nh_grantcookie = 1;
843 TAILQ_INIT(&host->nh_pending);
844 TAILQ_INIT(&host->nh_granted);
845 TAILQ_INIT(&host->nh_finished);
846 TAILQ_INSERT_TAIL(&nlm_hosts, host, nh_link);
848 mtx_unlock(&nlm_global_lock);
850 sysctl_ctx_init(&host->nh_sysctl);
851 oid = SYSCTL_ADD_NODE(&host->nh_sysctl,
852 SYSCTL_STATIC_CHILDREN(_vfs_nlm_sysid),
853 OID_AUTO, host->nh_sysid_string, CTLFLAG_RD | CTLFLAG_MPSAFE,
855 SYSCTL_ADD_STRING(&host->nh_sysctl, SYSCTL_CHILDREN(oid), OID_AUTO,
856 "hostname", CTLFLAG_RD, host->nh_caller_name, 0, "");
857 SYSCTL_ADD_UINT(&host->nh_sysctl, SYSCTL_CHILDREN(oid), OID_AUTO,
858 "version", CTLFLAG_RD, &host->nh_vers, 0, "");
859 SYSCTL_ADD_UINT(&host->nh_sysctl, SYSCTL_CHILDREN(oid), OID_AUTO,
860 "monitored", CTLFLAG_RD, &host->nh_monstate, 0, "");
861 SYSCTL_ADD_PROC(&host->nh_sysctl, SYSCTL_CHILDREN(oid), OID_AUTO,
862 "lock_count", CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_MPSAFE, host,
863 0, nlm_host_lock_count_sysctl, "I", "");
864 SYSCTL_ADD_PROC(&host->nh_sysctl, SYSCTL_CHILDREN(oid), OID_AUTO,
865 "client_lock_count", CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_MPSAFE,
866 host, 0, nlm_host_client_lock_count_sysctl, "I", "");
868 mtx_lock(&nlm_global_lock);
874 * Acquire the next sysid for remote locks not handled by the NLM.
877 nlm_acquire_next_sysid(void)
881 mtx_lock(&nlm_global_lock);
882 next_sysid = nlm_next_sysid++;
883 mtx_unlock(&nlm_global_lock);
888 * Return non-zero if the address parts of the two sockaddrs are the
892 nlm_compare_addr(const struct sockaddr *a, const struct sockaddr *b)
894 const struct sockaddr_in *a4, *b4;
896 const struct sockaddr_in6 *a6, *b6;
899 if (a->sa_family != b->sa_family)
902 switch (a->sa_family) {
904 a4 = (const struct sockaddr_in *) a;
905 b4 = (const struct sockaddr_in *) b;
906 return !memcmp(&a4->sin_addr, &b4->sin_addr,
907 sizeof(a4->sin_addr));
910 a6 = (const struct sockaddr_in6 *) a;
911 b6 = (const struct sockaddr_in6 *) b;
912 return !memcmp(&a6->sin6_addr, &b6->sin6_addr,
913 sizeof(a6->sin6_addr));
921 * Check for idle hosts and stop monitoring them. We could also free
922 * the host structure here, possibly after a larger timeout but that
923 * would require some care to avoid races with
924 * e.g. nlm_host_lock_count_sysctl.
929 struct nlm_host *host;
931 mtx_assert(&nlm_global_lock, MA_OWNED);
933 if (time_uptime <= nlm_next_idle_check)
936 nlm_next_idle_check = time_uptime + NLM_IDLE_PERIOD;
938 TAILQ_FOREACH(host, &nlm_hosts, nh_link) {
939 if (host->nh_monstate == NLM_MONITORED
940 && time_uptime > host->nh_idle_timeout) {
941 mtx_unlock(&nlm_global_lock);
942 if (lf_countlocks(host->nh_sysid) > 0
943 || lf_countlocks(NLM_SYSID_CLIENT
945 host->nh_idle_timeout =
946 time_uptime + NLM_IDLE_TIMEOUT;
947 mtx_lock(&nlm_global_lock);
950 nlm_host_unmonitor(host);
951 mtx_lock(&nlm_global_lock);
957 * Search for an existing NLM host that matches the given name
958 * (typically the caller_name element of an nlm4_lock). If none is
959 * found, create a new host. If 'addr' is non-NULL, record the remote
960 * address of the host so that we can call it back for async
961 * responses. If 'vers' is greater than zero then record the NLM
962 * program version to use to communicate with this client.
965 nlm_find_host_by_name(const char *name, const struct sockaddr *addr,
968 struct nlm_host *host;
970 mtx_lock(&nlm_global_lock);
973 * The remote host is determined by caller_name.
975 TAILQ_FOREACH(host, &nlm_hosts, nh_link) {
976 if (!strcmp(host->nh_caller_name, name))
981 host = nlm_create_host(name);
983 mtx_unlock(&nlm_global_lock);
987 refcount_acquire(&host->nh_refs);
989 host->nh_idle_timeout = time_uptime + NLM_IDLE_TIMEOUT;
992 * If we have an address for the host, record it so that we
993 * can send async replies etc.
997 KASSERT(addr->sa_len < sizeof(struct sockaddr_storage),
998 ("Strange remote transport address length"));
1001 * If we have seen an address before and we currently
1002 * have an RPC client handle, make sure the address is
1003 * the same, otherwise discard the client handle.
1005 if (host->nh_addr.ss_len && host->nh_srvrpc.nr_client) {
1006 if (!nlm_compare_addr(
1007 (struct sockaddr *) &host->nh_addr,
1009 || host->nh_vers != vers) {
1011 mtx_lock(&host->nh_lock);
1012 client = host->nh_srvrpc.nr_client;
1013 host->nh_srvrpc.nr_client = NULL;
1014 mtx_unlock(&host->nh_lock);
1016 CLNT_RELEASE(client);
1020 memcpy(&host->nh_addr, addr, addr->sa_len);
1021 host->nh_vers = vers;
1026 mtx_unlock(&nlm_global_lock);
1032 * Search for an existing NLM host that matches the given remote
1033 * address. If none is found, create a new host with the requested
1034 * address and remember 'vers' as the NLM protocol version to use for
1038 nlm_find_host_by_addr(const struct sockaddr *addr, int vers)
1041 * Fake up a name using inet_ntop. This buffer is
1042 * large enough for an IPv6 address.
1044 char tmp[sizeof "ffff:ffff:ffff:ffff:ffff:ffff:255.255.255.255"];
1045 struct nlm_host *host;
1047 switch (addr->sa_family) {
1050 &((const struct sockaddr_in *) addr)->sin_addr,
1056 &((const struct sockaddr_in6 *) addr)->sin6_addr,
1061 strlcpy(tmp, "<unknown>", sizeof(tmp));
1065 mtx_lock(&nlm_global_lock);
1068 * The remote host is determined by caller_name.
1070 TAILQ_FOREACH(host, &nlm_hosts, nh_link) {
1071 if (nlm_compare_addr(addr,
1072 (const struct sockaddr *) &host->nh_addr))
1077 host = nlm_create_host(tmp);
1079 mtx_unlock(&nlm_global_lock);
1082 memcpy(&host->nh_addr, addr, addr->sa_len);
1083 host->nh_vers = vers;
1085 refcount_acquire(&host->nh_refs);
1087 host->nh_idle_timeout = time_uptime + NLM_IDLE_TIMEOUT;
1091 mtx_unlock(&nlm_global_lock);
1097 * Find the NLM host that matches the value of 'sysid'. If none
1098 * exists, return NULL.
1100 static struct nlm_host *
1101 nlm_find_host_by_sysid(int sysid)
1103 struct nlm_host *host;
1105 TAILQ_FOREACH(host, &nlm_hosts, nh_link) {
1106 if (host->nh_sysid == sysid) {
1107 refcount_acquire(&host->nh_refs);
1115 void nlm_host_release(struct nlm_host *host)
1117 if (refcount_release(&host->nh_refs)) {
1121 nlm_host_destroy(host);
1126 * Unregister this NLM host with the local NSM due to idleness.
1129 nlm_host_unmonitor(struct nlm_host *host)
1133 struct timeval timo;
1134 enum clnt_stat stat;
1136 NLM_DEBUG(1, "NLM: unmonitoring %s (sysid %d)\n",
1137 host->nh_caller_name, host->nh_sysid);
1140 * We put our assigned system ID value in the priv field to
1141 * make it simpler to find the host if we are notified of a
1144 smmonid.mon_name = host->nh_caller_name;
1145 smmonid.my_id.my_name = "localhost";
1146 smmonid.my_id.my_prog = NLM_PROG;
1147 smmonid.my_id.my_vers = NLM_SM;
1148 smmonid.my_id.my_proc = NLM_SM_NOTIFY;
1152 stat = CLNT_CALL(nlm_nsm, SM_UNMON,
1153 (xdrproc_t) xdr_mon, &smmonid,
1154 (xdrproc_t) xdr_sm_stat, &smstat, timo);
1156 if (stat != RPC_SUCCESS) {
1157 NLM_ERR("Failed to contact local NSM - rpc error %d\n", stat);
1160 if (smstat.res_stat == stat_fail) {
1161 NLM_ERR("Local NSM refuses to unmonitor %s\n",
1162 host->nh_caller_name);
1166 host->nh_monstate = NLM_UNMONITORED;
1170 * Register this NLM host with the local NSM so that we can be
1171 * notified if it reboots.
1174 nlm_host_monitor(struct nlm_host *host, int state)
1178 struct timeval timo;
1179 enum clnt_stat stat;
1181 if (state && !host->nh_state) {
1183 * This is the first time we have seen an NSM state
1184 * value for this host. We record it here to help
1185 * detect host reboots.
1187 host->nh_state = state;
1188 NLM_DEBUG(1, "NLM: host %s (sysid %d) has NSM state %d\n",
1189 host->nh_caller_name, host->nh_sysid, state);
1192 mtx_lock(&host->nh_lock);
1193 if (host->nh_monstate != NLM_UNMONITORED) {
1194 mtx_unlock(&host->nh_lock);
1197 host->nh_monstate = NLM_MONITORED;
1198 mtx_unlock(&host->nh_lock);
1200 NLM_DEBUG(1, "NLM: monitoring %s (sysid %d)\n",
1201 host->nh_caller_name, host->nh_sysid);
1204 * We put our assigned system ID value in the priv field to
1205 * make it simpler to find the host if we are notified of a
1208 smmon.mon_id.mon_name = host->nh_caller_name;
1209 smmon.mon_id.my_id.my_name = "localhost";
1210 smmon.mon_id.my_id.my_prog = NLM_PROG;
1211 smmon.mon_id.my_id.my_vers = NLM_SM;
1212 smmon.mon_id.my_id.my_proc = NLM_SM_NOTIFY;
1213 memcpy(smmon.priv, &host->nh_sysid, sizeof(host->nh_sysid));
1217 stat = CLNT_CALL(nlm_nsm, SM_MON,
1218 (xdrproc_t) xdr_mon, &smmon,
1219 (xdrproc_t) xdr_sm_stat, &smstat, timo);
1221 if (stat != RPC_SUCCESS) {
1222 NLM_ERR("Failed to contact local NSM - rpc error %d\n", stat);
1225 if (smstat.res_stat == stat_fail) {
1226 NLM_ERR("Local NSM refuses to monitor %s\n",
1227 host->nh_caller_name);
1228 mtx_lock(&host->nh_lock);
1229 host->nh_monstate = NLM_MONITOR_FAILED;
1230 mtx_unlock(&host->nh_lock);
1234 host->nh_monstate = NLM_MONITORED;
1238 * Return an RPC client handle that can be used to talk to the NLM
1239 * running on the given host.
1242 nlm_host_get_rpc(struct nlm_host *host, bool_t isserver)
1244 struct nlm_rpc *rpc;
1247 mtx_lock(&host->nh_lock);
1250 rpc = &host->nh_srvrpc;
1252 rpc = &host->nh_clntrpc;
1255 * We can't hold onto RPC handles for too long - the async
1256 * call/reply protocol used by some NLM clients makes it hard
1257 * to tell when they change port numbers (e.g. after a
1258 * reboot). Note that if a client reboots while it isn't
1259 * holding any locks, it won't bother to notify us. We
1260 * expire the RPC handles after two minutes.
1262 if (rpc->nr_client && time_uptime > rpc->nr_create_time + 2*60) {
1263 client = rpc->nr_client;
1264 rpc->nr_client = NULL;
1265 mtx_unlock(&host->nh_lock);
1266 CLNT_RELEASE(client);
1267 mtx_lock(&host->nh_lock);
1270 if (!rpc->nr_client) {
1271 mtx_unlock(&host->nh_lock);
1272 client = nlm_get_rpc((struct sockaddr *)&host->nh_addr,
1273 NLM_PROG, host->nh_vers);
1274 mtx_lock(&host->nh_lock);
1277 if (rpc->nr_client) {
1278 mtx_unlock(&host->nh_lock);
1279 CLNT_DESTROY(client);
1280 mtx_lock(&host->nh_lock);
1282 rpc->nr_client = client;
1283 rpc->nr_create_time = time_uptime;
1288 client = rpc->nr_client;
1290 CLNT_ACQUIRE(client);
1291 mtx_unlock(&host->nh_lock);
1297 int nlm_host_get_sysid(struct nlm_host *host)
1300 return (host->nh_sysid);
1304 nlm_host_get_state(struct nlm_host *host)
1307 return (host->nh_state);
1311 nlm_register_wait_lock(struct nlm4_lock *lock, struct vnode *vp)
1313 struct nlm_waiting_lock *nw;
1315 nw = malloc(sizeof(struct nlm_waiting_lock), M_NLM, M_WAITOK);
1316 nw->nw_lock = *lock;
1317 memcpy(&nw->nw_fh.fh_bytes, nw->nw_lock.fh.n_bytes,
1318 nw->nw_lock.fh.n_len);
1319 nw->nw_lock.fh.n_bytes = nw->nw_fh.fh_bytes;
1320 nw->nw_waiting = TRUE;
1322 mtx_lock(&nlm_global_lock);
1323 TAILQ_INSERT_TAIL(&nlm_waiting_locks, nw, nw_link);
1324 mtx_unlock(&nlm_global_lock);
1330 nlm_deregister_wait_lock(void *handle)
1332 struct nlm_waiting_lock *nw = handle;
1334 mtx_lock(&nlm_global_lock);
1335 TAILQ_REMOVE(&nlm_waiting_locks, nw, nw_link);
1336 mtx_unlock(&nlm_global_lock);
1342 nlm_wait_lock(void *handle, int timo)
1344 struct nlm_waiting_lock *nw = handle;
1345 int error, stops_deferred;
1348 * If the granted message arrived before we got here,
1349 * nw->nw_waiting will be FALSE - in that case, don't sleep.
1351 mtx_lock(&nlm_global_lock);
1353 if (nw->nw_waiting) {
1354 stops_deferred = sigdeferstop(SIGDEFERSTOP_ERESTART);
1355 error = msleep(nw, &nlm_global_lock, PCATCH, "nlmlock", timo);
1356 sigallowstop(stops_deferred);
1358 TAILQ_REMOVE(&nlm_waiting_locks, nw, nw_link);
1361 * The granted message may arrive after the
1362 * interrupt/timeout but before we manage to lock the
1363 * mutex. Detect this by examining nw_lock.
1365 if (!nw->nw_waiting)
1369 * If nlm_cancel_wait is called, then error will be
1370 * zero but nw_waiting will still be TRUE. We
1371 * translate this into EINTR.
1376 mtx_unlock(&nlm_global_lock);
1384 nlm_cancel_wait(struct vnode *vp)
1386 struct nlm_waiting_lock *nw;
1388 mtx_lock(&nlm_global_lock);
1389 TAILQ_FOREACH(nw, &nlm_waiting_locks, nw_link) {
1390 if (nw->nw_vp == vp) {
1394 mtx_unlock(&nlm_global_lock);
1398 /**********************************************************************/
1401 * Syscall interface with userland.
1404 extern void nlm_prog_0(struct svc_req *rqstp, SVCXPRT *transp);
1405 extern void nlm_prog_1(struct svc_req *rqstp, SVCXPRT *transp);
1406 extern void nlm_prog_3(struct svc_req *rqstp, SVCXPRT *transp);
1407 extern void nlm_prog_4(struct svc_req *rqstp, SVCXPRT *transp);
1410 nlm_register_services(SVCPOOL *pool, int addr_count, char **addrs)
1412 static rpcvers_t versions[] = {
1413 NLM_SM, NLM_VERS, NLM_VERSX, NLM_VERS4
1415 static void (*dispatchers[])(struct svc_req *, SVCXPRT *) = {
1416 nlm_prog_0, nlm_prog_1, nlm_prog_3, nlm_prog_4
1422 struct netconfig *nconf;
1426 NLM_ERR("NLM: no service addresses given - can't start server");
1430 if (addr_count < 0 || addr_count > 256 ) {
1431 NLM_ERR("NLM: too many service addresses (%d) given, "
1432 "max 256 - can't start server\n", addr_count);
1436 xprts = malloc(addr_count * sizeof(SVCXPRT *), M_NLM, M_WAITOK|M_ZERO);
1437 for (i = 0; i < nitems(versions); 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;
1529 if (nlm_is_running != 0) {
1530 NLM_ERR("NLM: can't start server - "
1531 "it appears to be running already\n");
1535 if (nlm_socket == NULL) {
1536 memset(&opt, 0, sizeof(opt));
1538 error = socreate(AF_INET, &nlm_socket, SOCK_DGRAM, 0,
1541 NLM_ERR("NLM: can't create IPv4 socket - error %d\n",
1545 opt.sopt_dir = SOPT_SET;
1546 opt.sopt_level = IPPROTO_IP;
1547 opt.sopt_name = IP_PORTRANGE;
1548 portlow = IP_PORTRANGE_LOW;
1549 opt.sopt_val = &portlow;
1550 opt.sopt_valsize = sizeof(portlow);
1551 sosetopt(nlm_socket, &opt);
1555 error = socreate(AF_INET6, &nlm_socket6, SOCK_DGRAM, 0,
1558 NLM_ERR("NLM: can't create IPv6 socket - error %d\n",
1560 soclose(nlm_socket);
1564 opt.sopt_dir = SOPT_SET;
1565 opt.sopt_level = IPPROTO_IPV6;
1566 opt.sopt_name = IPV6_PORTRANGE;
1567 portlow = IPV6_PORTRANGE_LOW;
1568 opt.sopt_val = &portlow;
1569 opt.sopt_valsize = sizeof(portlow);
1570 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);
1626 NLM_DEBUG(1, "NLM: local NSM state is %d\n", smstat.state);
1627 nlm_nsm_state = smstat.state;
1629 old_nfs_advlock = nfs_advlock_p;
1630 nfs_advlock_p = nlm_advlock;
1631 old_nfs_reclaim = nfs_reclaim_p;
1632 nfs_reclaim_p = nlm_reclaim;
1637 nfs_advlock_p = old_nfs_advlock;
1638 nfs_reclaim_p = old_nfs_reclaim;
1643 svcpool_destroy(pool);
1646 * We are finished communicating with the NSM.
1649 CLNT_RELEASE(nlm_nsm);
1654 * Trash all the existing state so that if the server
1655 * restarts, it gets a clean slate. This is complicated by the
1656 * possibility that there may be other threads trying to make
1657 * client locking requests.
1659 * First we fake a client reboot notification which will
1660 * cancel any pending async locks and purge remote lock state
1661 * from the local lock manager. We release the reference from
1662 * nlm_hosts to the host (which may remove it from the list
1663 * and free it). After this phase, the only entries in the
1664 * nlm_host list should be from other threads performing
1665 * client lock requests.
1667 mtx_lock(&nlm_global_lock);
1668 TAILQ_FOREACH(nw, &nlm_waiting_locks, nw_link) {
1671 TAILQ_FOREACH_SAFE(host, &nlm_hosts, nh_link, nhost) {
1672 mtx_unlock(&nlm_global_lock);
1673 nlm_host_notify(host, 0);
1674 nlm_host_release(host);
1675 mtx_lock(&nlm_global_lock);
1677 mtx_unlock(&nlm_global_lock);
1679 AUTH_DESTROY(nlm_auth);
1685 sys_nlm_syscall(struct thread *td, struct nlm_syscall_args *uap)
1689 error = priv_check(td, PRIV_NFS_LOCKD);
1693 nlm_debug_level = uap->debug_level;
1694 nlm_grace_threshold = time_uptime + uap->grace_period;
1695 nlm_next_idle_check = time_uptime + NLM_IDLE_PERIOD;
1697 return nlm_server_main(uap->addr_count, uap->addrs);
1700 /**********************************************************************/
1703 * NLM implementation details, called from the RPC stubs.
1708 nlm_sm_notify(struct nlm_sm_status *argp)
1711 struct nlm_host *host;
1713 NLM_DEBUG(3, "nlm_sm_notify(): mon_name = %s\n", argp->mon_name);
1714 memcpy(&sysid, &argp->priv, sizeof(sysid));
1715 host = nlm_find_host_by_sysid(sysid);
1717 nlm_host_notify(host, argp->state);
1718 nlm_host_release(host);
1723 nlm_convert_to_fhandle_t(fhandle_t *fhp, struct netobj *p)
1725 memcpy(fhp, p->n_bytes, sizeof(fhandle_t));
1729 struct mount *vs_mp;
1730 struct vnode *vs_vp;
1735 nlm_get_vfs_state(struct nlm_host *host, struct svc_req *rqstp,
1736 fhandle_t *fhp, struct vfs_state *vs, accmode_t accmode)
1739 struct ucred *cred = NULL, *credanon = NULL;
1741 memset(vs, 0, sizeof(*vs));
1743 vs->vs_mp = vfs_getvfs(&fhp->fh_fsid);
1748 /* accmode == 0 means don't check, since it is an unlock. */
1750 error = VFS_CHECKEXP(vs->vs_mp,
1751 (struct sockaddr *)&host->nh_addr, &exflags, &credanon,
1756 if (exflags & MNT_EXRDONLY ||
1757 (vs->vs_mp->mnt_flag & MNT_RDONLY)) {
1763 error = VFS_FHTOVP(vs->vs_mp, &fhp->fh_fid, LK_EXCLUSIVE, &vs->vs_vp);
1766 vs->vs_vnlocked = TRUE;
1769 if (!svc_getcred(rqstp, &cred, NULL)) {
1773 if (cred->cr_uid == 0 || (exflags & MNT_EXPORTANON)) {
1782 error = VOP_ACCESS(vs->vs_vp, accmode, cred, curthread);
1784 * If this failed and accmode != VWRITE, try again with
1785 * VWRITE to maintain backwards compatibility with the
1786 * old code that always used VWRITE.
1788 if (error != 0 && accmode != VWRITE)
1789 error = VOP_ACCESS(vs->vs_vp, VWRITE, cred, curthread);
1794 VOP_UNLOCK(vs->vs_vp);
1795 vs->vs_vnlocked = FALSE;
1807 nlm_release_vfs_state(struct vfs_state *vs)
1811 if (vs->vs_vnlocked)
1821 nlm_convert_error(int error)
1824 if (error == ESTALE)
1825 return nlm4_stale_fh;
1826 else if (error == EROFS)
1833 nlm_do_test(nlm4_testargs *argp, nlm4_testres *result, struct svc_req *rqstp,
1837 struct vfs_state vs;
1838 struct nlm_host *host, *bhost;
1843 memset(result, 0, sizeof(*result));
1844 memset(&vs, 0, sizeof(vs));
1846 host = nlm_find_host_by_name(argp->alock.caller_name,
1847 svc_getrpccaller(rqstp), rqstp->rq_vers);
1849 result->stat.stat = nlm4_denied_nolocks;
1853 NLM_DEBUG(3, "nlm_do_test(): caller_name = %s (sysid = %d)\n",
1854 host->nh_caller_name, host->nh_sysid);
1856 nlm_check_expired_locks(host);
1857 sysid = host->nh_sysid;
1859 nlm_convert_to_fhandle_t(&fh, &argp->alock.fh);
1860 nlm_copy_netobj(&result->cookie, &argp->cookie, M_RPC);
1862 if (time_uptime < nlm_grace_threshold) {
1863 result->stat.stat = nlm4_denied_grace_period;
1867 accmode = argp->exclusive ? VWRITE : VREAD;
1868 error = nlm_get_vfs_state(host, rqstp, &fh, &vs, accmode);
1870 result->stat.stat = nlm_convert_error(error);
1874 fl.l_start = argp->alock.l_offset;
1875 fl.l_len = argp->alock.l_len;
1876 fl.l_pid = argp->alock.svid;
1878 fl.l_whence = SEEK_SET;
1879 if (argp->exclusive)
1880 fl.l_type = F_WRLCK;
1882 fl.l_type = F_RDLCK;
1883 error = VOP_ADVLOCK(vs.vs_vp, NULL, F_GETLK, &fl, F_REMOTE);
1885 result->stat.stat = nlm4_failed;
1889 if (fl.l_type == F_UNLCK) {
1890 result->stat.stat = nlm4_granted;
1892 result->stat.stat = nlm4_denied;
1893 result->stat.nlm4_testrply_u.holder.exclusive =
1894 (fl.l_type == F_WRLCK);
1895 result->stat.nlm4_testrply_u.holder.svid = fl.l_pid;
1896 bhost = nlm_find_host_by_sysid(fl.l_sysid);
1899 * We don't have any useful way of recording
1900 * the value of oh used in the original lock
1901 * request. Ideally, the test reply would have
1902 * a space for the owning host's name allowing
1903 * our caller's NLM to keep track.
1905 * As far as I can see, Solaris uses an eight
1906 * byte structure for oh which contains a four
1907 * byte pid encoded in local byte order and
1908 * the first four bytes of the host
1909 * name. Linux uses a variable length string
1910 * 'pid@hostname' in ascii but doesn't even
1911 * return that in test replies.
1913 * For the moment, return nothing in oh
1914 * (already zero'ed above).
1916 nlm_host_release(bhost);
1918 result->stat.nlm4_testrply_u.holder.l_offset = fl.l_start;
1919 result->stat.nlm4_testrply_u.holder.l_len = fl.l_len;
1923 nlm_release_vfs_state(&vs);
1925 *rpcp = nlm_host_get_rpc(host, TRUE);
1926 nlm_host_release(host);
1931 nlm_do_lock(nlm4_lockargs *argp, nlm4_res *result, struct svc_req *rqstp,
1932 bool_t monitor, CLIENT **rpcp)
1935 struct vfs_state vs;
1936 struct nlm_host *host;
1941 memset(result, 0, sizeof(*result));
1942 memset(&vs, 0, sizeof(vs));
1944 host = nlm_find_host_by_name(argp->alock.caller_name,
1945 svc_getrpccaller(rqstp), rqstp->rq_vers);
1947 result->stat.stat = nlm4_denied_nolocks;
1951 NLM_DEBUG(3, "nlm_do_lock(): caller_name = %s (sysid = %d)\n",
1952 host->nh_caller_name, host->nh_sysid);
1954 if (monitor && host->nh_state && argp->state
1955 && host->nh_state != argp->state) {
1957 * The host rebooted without telling us. Trash its
1960 nlm_host_notify(host, argp->state);
1963 nlm_check_expired_locks(host);
1964 sysid = host->nh_sysid;
1966 nlm_convert_to_fhandle_t(&fh, &argp->alock.fh);
1967 nlm_copy_netobj(&result->cookie, &argp->cookie, M_RPC);
1969 if (time_uptime < nlm_grace_threshold && !argp->reclaim) {
1970 result->stat.stat = nlm4_denied_grace_period;
1974 accmode = argp->exclusive ? VWRITE : VREAD;
1975 error = nlm_get_vfs_state(host, rqstp, &fh, &vs, accmode);
1977 result->stat.stat = nlm_convert_error(error);
1981 fl.l_start = argp->alock.l_offset;
1982 fl.l_len = argp->alock.l_len;
1983 fl.l_pid = argp->alock.svid;
1985 fl.l_whence = SEEK_SET;
1986 if (argp->exclusive)
1987 fl.l_type = F_WRLCK;
1989 fl.l_type = F_RDLCK;
1991 struct nlm_async_lock *af;
1993 struct nlm_grantcookie cookie;
1996 * First, make sure we can contact the host's NLM.
1998 client = nlm_host_get_rpc(host, TRUE);
2000 result->stat.stat = nlm4_failed;
2005 * First we need to check and see if there is an
2006 * existing blocked lock that matches. This could be a
2007 * badly behaved client or an RPC re-send. If we find
2008 * one, just return nlm4_blocked.
2010 mtx_lock(&host->nh_lock);
2011 TAILQ_FOREACH(af, &host->nh_pending, af_link) {
2012 if (af->af_fl.l_start == fl.l_start
2013 && af->af_fl.l_len == fl.l_len
2014 && af->af_fl.l_pid == fl.l_pid
2015 && af->af_fl.l_type == fl.l_type) {
2020 cookie.ng_sysid = host->nh_sysid;
2021 cookie.ng_cookie = host->nh_grantcookie++;
2023 mtx_unlock(&host->nh_lock);
2025 CLNT_RELEASE(client);
2026 result->stat.stat = nlm4_blocked;
2030 af = malloc(sizeof(struct nlm_async_lock), M_NLM,
2032 TASK_INIT(&af->af_task, 0, nlm_lock_callback, af);
2033 af->af_vp = vs.vs_vp;
2036 af->af_rpc = client;
2038 * We use M_RPC here so that we can xdr_free the thing
2041 nlm_make_netobj(&af->af_granted.cookie,
2042 (caddr_t)&cookie, sizeof(cookie), M_RPC);
2043 af->af_granted.exclusive = argp->exclusive;
2044 af->af_granted.alock.caller_name =
2045 strdup(argp->alock.caller_name, M_RPC);
2046 nlm_copy_netobj(&af->af_granted.alock.fh,
2047 &argp->alock.fh, M_RPC);
2048 nlm_copy_netobj(&af->af_granted.alock.oh,
2049 &argp->alock.oh, M_RPC);
2050 af->af_granted.alock.svid = argp->alock.svid;
2051 af->af_granted.alock.l_offset = argp->alock.l_offset;
2052 af->af_granted.alock.l_len = argp->alock.l_len;
2055 * Put the entry on the pending list before calling
2056 * VOP_ADVLOCKASYNC. We do this in case the lock
2057 * request was blocked (returning EINPROGRESS) but
2058 * then granted before we manage to run again. The
2059 * client may receive the granted message before we
2060 * send our blocked reply but thats their problem.
2062 mtx_lock(&host->nh_lock);
2063 TAILQ_INSERT_TAIL(&host->nh_pending, af, af_link);
2064 mtx_unlock(&host->nh_lock);
2066 error = VOP_ADVLOCKASYNC(vs.vs_vp, NULL, F_SETLK, &fl, F_REMOTE,
2067 &af->af_task, &af->af_cookie);
2070 * If the lock completed synchronously, just free the
2071 * tracking structure now.
2073 if (error != EINPROGRESS) {
2074 CLNT_RELEASE(af->af_rpc);
2075 mtx_lock(&host->nh_lock);
2076 TAILQ_REMOVE(&host->nh_pending, af, af_link);
2077 mtx_unlock(&host->nh_lock);
2078 xdr_free((xdrproc_t) xdr_nlm4_testargs,
2082 NLM_DEBUG(2, "NLM: pending async lock %p for %s "
2083 "(sysid %d)\n", af, host->nh_caller_name, sysid);
2085 * Don't vrele the vnode just yet - this must
2086 * wait until either the async callback
2087 * happens or the lock is cancelled.
2092 error = VOP_ADVLOCK(vs.vs_vp, NULL, F_SETLK, &fl, F_REMOTE);
2096 if (error == EINPROGRESS) {
2097 result->stat.stat = nlm4_blocked;
2098 } else if (error == EDEADLK) {
2099 result->stat.stat = nlm4_deadlck;
2100 } else if (error == EAGAIN) {
2101 result->stat.stat = nlm4_denied;
2103 result->stat.stat = nlm4_failed;
2107 nlm_host_monitor(host, argp->state);
2108 result->stat.stat = nlm4_granted;
2112 nlm_release_vfs_state(&vs);
2114 *rpcp = nlm_host_get_rpc(host, TRUE);
2115 nlm_host_release(host);
2120 nlm_do_cancel(nlm4_cancargs *argp, nlm4_res *result, struct svc_req *rqstp,
2124 struct vfs_state vs;
2125 struct nlm_host *host;
2128 struct nlm_async_lock *af;
2130 memset(result, 0, sizeof(*result));
2131 memset(&vs, 0, sizeof(vs));
2133 host = nlm_find_host_by_name(argp->alock.caller_name,
2134 svc_getrpccaller(rqstp), rqstp->rq_vers);
2136 result->stat.stat = nlm4_denied_nolocks;
2140 NLM_DEBUG(3, "nlm_do_cancel(): caller_name = %s (sysid = %d)\n",
2141 host->nh_caller_name, host->nh_sysid);
2143 nlm_check_expired_locks(host);
2144 sysid = host->nh_sysid;
2146 nlm_convert_to_fhandle_t(&fh, &argp->alock.fh);
2147 nlm_copy_netobj(&result->cookie, &argp->cookie, M_RPC);
2149 if (time_uptime < nlm_grace_threshold) {
2150 result->stat.stat = nlm4_denied_grace_period;
2154 error = nlm_get_vfs_state(host, rqstp, &fh, &vs, (accmode_t)0);
2156 result->stat.stat = nlm_convert_error(error);
2160 fl.l_start = argp->alock.l_offset;
2161 fl.l_len = argp->alock.l_len;
2162 fl.l_pid = argp->alock.svid;
2164 fl.l_whence = SEEK_SET;
2165 if (argp->exclusive)
2166 fl.l_type = F_WRLCK;
2168 fl.l_type = F_RDLCK;
2171 * First we need to try and find the async lock request - if
2172 * there isn't one, we give up and return nlm4_denied.
2174 mtx_lock(&host->nh_lock);
2176 TAILQ_FOREACH(af, &host->nh_pending, af_link) {
2177 if (af->af_fl.l_start == fl.l_start
2178 && af->af_fl.l_len == fl.l_len
2179 && af->af_fl.l_pid == fl.l_pid
2180 && af->af_fl.l_type == fl.l_type) {
2186 mtx_unlock(&host->nh_lock);
2187 result->stat.stat = nlm4_denied;
2191 error = nlm_cancel_async_lock(af);
2194 result->stat.stat = nlm4_denied;
2196 result->stat.stat = nlm4_granted;
2199 mtx_unlock(&host->nh_lock);
2202 nlm_release_vfs_state(&vs);
2204 *rpcp = nlm_host_get_rpc(host, TRUE);
2205 nlm_host_release(host);
2210 nlm_do_unlock(nlm4_unlockargs *argp, nlm4_res *result, struct svc_req *rqstp,
2214 struct vfs_state vs;
2215 struct nlm_host *host;
2219 memset(result, 0, sizeof(*result));
2220 memset(&vs, 0, sizeof(vs));
2222 host = nlm_find_host_by_name(argp->alock.caller_name,
2223 svc_getrpccaller(rqstp), rqstp->rq_vers);
2225 result->stat.stat = nlm4_denied_nolocks;
2229 NLM_DEBUG(3, "nlm_do_unlock(): caller_name = %s (sysid = %d)\n",
2230 host->nh_caller_name, host->nh_sysid);
2232 nlm_check_expired_locks(host);
2233 sysid = host->nh_sysid;
2235 nlm_convert_to_fhandle_t(&fh, &argp->alock.fh);
2236 nlm_copy_netobj(&result->cookie, &argp->cookie, M_RPC);
2238 if (time_uptime < nlm_grace_threshold) {
2239 result->stat.stat = nlm4_denied_grace_period;
2243 error = nlm_get_vfs_state(host, rqstp, &fh, &vs, (accmode_t)0);
2245 result->stat.stat = nlm_convert_error(error);
2249 fl.l_start = argp->alock.l_offset;
2250 fl.l_len = argp->alock.l_len;
2251 fl.l_pid = argp->alock.svid;
2253 fl.l_whence = SEEK_SET;
2254 fl.l_type = F_UNLCK;
2255 error = VOP_ADVLOCK(vs.vs_vp, NULL, F_UNLCK, &fl, F_REMOTE);
2258 * Ignore the error - there is no result code for failure,
2259 * only for grace period.
2261 result->stat.stat = nlm4_granted;
2264 nlm_release_vfs_state(&vs);
2266 *rpcp = nlm_host_get_rpc(host, TRUE);
2267 nlm_host_release(host);
2272 nlm_do_granted(nlm4_testargs *argp, nlm4_res *result, struct svc_req *rqstp,
2276 struct nlm_host *host;
2277 struct nlm_waiting_lock *nw;
2279 memset(result, 0, sizeof(*result));
2281 host = nlm_find_host_by_addr(svc_getrpccaller(rqstp), rqstp->rq_vers);
2283 result->stat.stat = nlm4_denied_nolocks;
2287 nlm_copy_netobj(&result->cookie, &argp->cookie, M_RPC);
2288 result->stat.stat = nlm4_denied;
2289 KFAIL_POINT_CODE(DEBUG_FP, nlm_deny_grant, goto out);
2291 mtx_lock(&nlm_global_lock);
2292 TAILQ_FOREACH(nw, &nlm_waiting_locks, nw_link) {
2293 if (!nw->nw_waiting)
2295 if (argp->alock.svid == nw->nw_lock.svid
2296 && argp->alock.l_offset == nw->nw_lock.l_offset
2297 && argp->alock.l_len == nw->nw_lock.l_len
2298 && argp->alock.fh.n_len == nw->nw_lock.fh.n_len
2299 && !memcmp(argp->alock.fh.n_bytes, nw->nw_lock.fh.n_bytes,
2300 nw->nw_lock.fh.n_len)) {
2301 nw->nw_waiting = FALSE;
2303 result->stat.stat = nlm4_granted;
2307 mtx_unlock(&nlm_global_lock);
2311 *rpcp = nlm_host_get_rpc(host, TRUE);
2312 nlm_host_release(host);
2317 nlm_do_granted_res(nlm4_res *argp, struct svc_req *rqstp)
2319 struct nlm_host *host = NULL;
2320 struct nlm_async_lock *af = NULL;
2323 if (argp->cookie.n_len != sizeof(struct nlm_grantcookie)) {
2324 NLM_DEBUG(1, "NLM: bogus grant cookie");
2328 host = nlm_find_host_by_sysid(ng_sysid(&argp->cookie));
2330 NLM_DEBUG(1, "NLM: Unknown host rejected our grant");
2334 mtx_lock(&host->nh_lock);
2335 TAILQ_FOREACH(af, &host->nh_granted, af_link)
2336 if (ng_cookie(&argp->cookie) ==
2337 ng_cookie(&af->af_granted.cookie))
2340 TAILQ_REMOVE(&host->nh_granted, af, af_link);
2341 mtx_unlock(&host->nh_lock);
2344 NLM_DEBUG(1, "NLM: host %s (sysid %d) replied to our grant "
2345 "with unrecognized cookie %d:%d", host->nh_caller_name,
2346 host->nh_sysid, ng_sysid(&argp->cookie),
2347 ng_cookie(&argp->cookie));
2351 if (argp->stat.stat != nlm4_granted) {
2352 af->af_fl.l_type = F_UNLCK;
2353 error = VOP_ADVLOCK(af->af_vp, NULL, F_UNLCK, &af->af_fl, F_REMOTE);
2355 NLM_DEBUG(1, "NLM: host %s (sysid %d) rejected our grant "
2356 "and we failed to unlock (%d)", host->nh_caller_name,
2357 host->nh_sysid, error);
2361 NLM_DEBUG(5, "NLM: async lock %p rejected by host %s (sysid %d)",
2362 af, host->nh_caller_name, host->nh_sysid);
2364 NLM_DEBUG(5, "NLM: async lock %p accepted by host %s (sysid %d)",
2365 af, host->nh_caller_name, host->nh_sysid);
2370 nlm_free_async_lock(af);
2372 nlm_host_release(host);
2376 nlm_do_free_all(nlm4_notify *argp)
2378 struct nlm_host *host, *thost;
2380 TAILQ_FOREACH_SAFE(host, &nlm_hosts, nh_link, thost) {
2381 if (!strcmp(host->nh_caller_name, argp->name))
2382 nlm_host_notify(host, argp->state);
2387 * Kernel module glue
2390 nfslockd_modevent(module_t mod, int type, void *data)
2395 return (nlm_init());
2399 /* The NLM module cannot be safely unloaded. */
2402 return (EOPNOTSUPP);
2405 static moduledata_t nfslockd_mod = {
2410 DECLARE_MODULE(nfslockd, nfslockd_mod, SI_SUB_VFS, SI_ORDER_ANY);
2412 /* So that loader and kldload(2) can find us, wherever we are.. */
2413 MODULE_DEPEND(nfslockd, xdr, 1, 1, 1);
2414 MODULE_DEPEND(nfslockd, krpc, 1, 1, 1);
2415 MODULE_DEPEND(nfslockd, nfscommon, 1, 1, 1);
2416 MODULE_VERSION(nfslockd, 1);