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>
43 #if __FreeBSD_version >= 700000
47 #include <sys/socket.h>
48 #include <sys/socketvar.h>
49 #include <sys/syscall.h>
50 #include <sys/sysctl.h>
51 #include <sys/sysent.h>
52 #include <sys/syslog.h>
53 #include <sys/sysproto.h>
54 #include <sys/systm.h>
55 #include <sys/taskqueue.h>
56 #include <sys/unistd.h>
57 #include <sys/vnode.h>
59 #include <nfs/nfsproto.h>
60 #include <nfs/nfs_lock.h>
62 #include <nlm/nlm_prot.h>
63 #include <nlm/sm_inter.h>
65 #include <rpc/rpc_com.h>
66 #include <rpc/rpcb_prot.h>
68 MALLOC_DEFINE(M_NLM, "NLM", "Network Lock Manager");
71 * If a host is inactive (and holds no locks) for this amount of
72 * seconds, we consider it idle and stop tracking it.
74 #define NLM_IDLE_TIMEOUT 30
77 * We check the host list for idle every few seconds.
79 #define NLM_IDLE_PERIOD 5
82 * We only look for GRANTED_RES messages for a little while.
84 #define NLM_EXPIRE_TIMEOUT 10
87 * Support for sysctl vfs.nlm.sysid
89 static SYSCTL_NODE(_vfs, OID_AUTO, nlm, CTLFLAG_RW, NULL,
90 "Network Lock Manager");
91 static SYSCTL_NODE(_vfs_nlm, OID_AUTO, sysid, CTLFLAG_RW, NULL, "");
96 static int nlm_syscall_offset = SYS_nlm_syscall;
97 static struct sysent nlm_syscall_prev_sysent;
98 #if __FreeBSD_version < 700000
99 static struct sysent nlm_syscall_sysent = {
100 (sizeof(struct nlm_syscall_args) / sizeof(register_t)) | SYF_MPSAFE,
101 (sy_call_t *) nlm_syscall
104 MAKE_SYSENT(nlm_syscall);
106 static bool_t nlm_syscall_registered = FALSE;
109 * Debug level passed in from userland. We also support a sysctl hook
110 * so that it can be changed on a live system.
112 static int nlm_debug_level;
113 SYSCTL_INT(_debug, OID_AUTO, nlm_debug, CTLFLAG_RW, &nlm_debug_level, 0, "");
115 #define NLM_DEBUG(_level, args...) \
117 if (nlm_debug_level >= (_level)) \
118 log(LOG_DEBUG, args); \
120 #define NLM_ERR(args...) \
122 log(LOG_ERR, args); \
126 * Grace period handling. The value of nlm_grace_threshold is the
127 * value of time_uptime after which we are serving requests normally.
129 static time_t nlm_grace_threshold;
132 * We check for idle hosts if time_uptime is greater than
133 * nlm_next_idle_check,
135 static time_t nlm_next_idle_check;
138 * A flag to indicate the server is already running.
140 static int nlm_is_running;
143 * A socket to use for RPC - shared by all IPv4 RPC clients.
145 static struct socket *nlm_socket;
150 * A socket to use for RPC - shared by all IPv6 RPC clients.
152 static struct socket *nlm_socket6;
157 * An RPC client handle that can be used to communicate with the local
160 static CLIENT *nlm_nsm;
163 * An AUTH handle for the server's creds.
165 static AUTH *nlm_auth;
168 * A zero timeval for sending async RPC messages.
170 struct timeval nlm_zero_tv = { 0, 0 };
173 * The local NSM state number
179 * A lock to protect the host list and waiting lock list.
181 static struct mtx nlm_global_lock;
185 * (l) locked by nh_lock
186 * (s) only accessed via server RPC which is single threaded
187 * (g) locked by nlm_global_lock
188 * (c) const until freeing
189 * (a) modified using atomic ops
193 * A pending client-side lock request, stored on the nlm_waiting_locks
196 struct nlm_waiting_lock {
197 TAILQ_ENTRY(nlm_waiting_lock) nw_link; /* (g) */
198 bool_t nw_waiting; /* (g) */
199 nlm4_lock nw_lock; /* (c) */
200 union nfsfh nw_fh; /* (c) */
201 struct vnode *nw_vp; /* (c) */
203 TAILQ_HEAD(nlm_waiting_lock_list, nlm_waiting_lock);
205 struct nlm_waiting_lock_list nlm_waiting_locks; /* (g) */
208 * A pending server-side asynchronous lock request, stored on the
209 * nh_pending list of the NLM host.
211 struct nlm_async_lock {
212 TAILQ_ENTRY(nlm_async_lock) af_link; /* (l) host's list of locks */
213 struct task af_task; /* (c) async callback details */
214 void *af_cookie; /* (l) lock manager cancel token */
215 struct vnode *af_vp; /* (l) vnode to lock */
216 struct flock af_fl; /* (c) lock details */
217 struct nlm_host *af_host; /* (c) host which is locking */
218 CLIENT *af_rpc; /* (c) rpc client to send message */
219 nlm4_testargs af_granted; /* (c) notification details */
220 time_t af_expiretime; /* (c) notification time */
222 TAILQ_HEAD(nlm_async_lock_list, nlm_async_lock);
227 enum nlm_host_state {
235 CLIENT *nr_client; /* (l) RPC client handle */
236 time_t nr_create_time; /* (l) when client was created */
241 volatile u_int nh_refs; /* (a) reference count */
242 TAILQ_ENTRY(nlm_host) nh_link; /* (g) global list of hosts */
243 char nh_caller_name[MAXNAMELEN]; /* (c) printable name of host */
244 uint32_t nh_sysid; /* (c) our allocaed system ID */
245 char nh_sysid_string[10]; /* (c) string rep. of sysid */
246 struct sockaddr_storage nh_addr; /* (s) remote address of host */
247 struct nlm_rpc nh_srvrpc; /* (l) RPC for server replies */
248 struct nlm_rpc nh_clntrpc; /* (l) RPC for client requests */
249 rpcvers_t nh_vers; /* (s) NLM version of host */
250 int nh_state; /* (s) last seen NSM state of host */
251 enum nlm_host_state nh_monstate; /* (l) local NSM monitoring state */
252 time_t nh_idle_timeout; /* (s) Time at which host is idle */
253 struct sysctl_ctx_list nh_sysctl; /* (c) vfs.nlm.sysid nodes */
254 uint32_t nh_grantcookie; /* (l) grant cookie counter */
255 struct nlm_async_lock_list nh_pending; /* (l) pending async locks */
256 struct nlm_async_lock_list nh_granted; /* (l) granted locks */
257 struct nlm_async_lock_list nh_finished; /* (l) finished async locks */
259 TAILQ_HEAD(nlm_host_list, nlm_host);
261 static struct nlm_host_list nlm_hosts; /* (g) */
262 static uint32_t nlm_next_sysid = 1; /* (g) */
264 static void nlm_host_unmonitor(struct nlm_host *);
266 struct nlm_grantcookie {
271 static inline uint32_t
272 ng_sysid(struct netobj *src)
275 return ((struct nlm_grantcookie *)src->n_bytes)->ng_sysid;
278 static inline uint32_t
279 ng_cookie(struct netobj *src)
282 return ((struct nlm_grantcookie *)src->n_bytes)->ng_cookie;
285 /**********************************************************************/
288 * Initialise NLM globals.
291 nlm_init(void *dummy)
295 mtx_init(&nlm_global_lock, "nlm_global_lock", NULL, MTX_DEF);
296 TAILQ_INIT(&nlm_waiting_locks);
297 TAILQ_INIT(&nlm_hosts);
299 error = syscall_register(&nlm_syscall_offset, &nlm_syscall_sysent,
300 &nlm_syscall_prev_sysent, SY_THR_STATIC_KLD);
302 NLM_ERR("Can't register NLM syscall\n");
304 nlm_syscall_registered = TRUE;
306 SYSINIT(nlm_init, SI_SUB_LOCK, SI_ORDER_FIRST, nlm_init, NULL);
309 nlm_uninit(void *dummy)
312 if (nlm_syscall_registered)
313 syscall_deregister(&nlm_syscall_offset,
314 &nlm_syscall_prev_sysent);
316 SYSUNINIT(nlm_uninit, SI_SUB_LOCK, SI_ORDER_FIRST, nlm_uninit, NULL);
319 * Create a netobj from an arbitrary source.
322 nlm_make_netobj(struct netobj *dst, caddr_t src, size_t srcsize,
323 struct malloc_type *type)
326 dst->n_len = srcsize;
327 dst->n_bytes = malloc(srcsize, type, M_WAITOK);
328 memcpy(dst->n_bytes, src, srcsize);
332 * Copy a struct netobj.
335 nlm_copy_netobj(struct netobj *dst, struct netobj *src,
336 struct malloc_type *type)
339 nlm_make_netobj(dst, src->n_bytes, src->n_len, type);
344 * Create an RPC client handle for the given (address,prog,vers)
348 nlm_get_rpc(struct sockaddr *sa, rpcprog_t prog, rpcvers_t vers)
350 char *wchan = "nlmrcv";
351 const char* protofmly;
352 struct sockaddr_storage ss;
358 enum clnt_stat stat = RPC_SUCCESS;
359 int rpcvers = RPCBVERS4;
360 bool_t do_tcp = FALSE;
361 bool_t tryagain = FALSE;
362 struct portmap mapping;
366 * First we need to contact the remote RPCBIND service to find
369 memcpy(&ss, sa, sa->sa_len);
370 switch (ss.ss_family) {
372 ((struct sockaddr_in *)&ss)->sin_port = htons(111);
379 ((struct sockaddr_in6 *)&ss)->sin6_port = htons(111);
387 * Unsupported address family - fail.
392 rpcb = clnt_dg_create(so, (struct sockaddr *)&ss,
393 RPCBPROG, rpcvers, 0, 0);
401 parms.r_netid = "tcp";
403 parms.r_netid = "udp";
408 * Use the default timeout.
417 * Try RPCBIND 4 then 3.
420 stat = CLNT_CALL(rpcb, (rpcprog_t) RPCBPROC_GETADDR,
421 (xdrproc_t) xdr_rpcb, &parms,
422 (xdrproc_t) xdr_wrapstring, &uaddr, timo);
423 if (stat == RPC_SUCCESS) {
425 * We have a reply from the remote RPCBIND - turn it
426 * into an appropriate address and make a new client
427 * that can talk to the remote NLM.
429 * XXX fixup IPv6 scope ID.
432 a = __rpc_uaddr2taddr_af(ss.ss_family, uaddr);
437 memcpy(&ss, a->buf, a->len);
440 xdr_free((xdrproc_t) xdr_wrapstring, &uaddr);
443 if (tryagain || stat == RPC_PROGVERSMISMATCH) {
444 if (rpcvers == RPCBVERS4)
446 else if (rpcvers == RPCBVERS)
448 CLNT_CONTROL(rpcb, CLSET_VERS, &rpcvers);
456 mapping.pm_prog = parms.r_prog;
457 mapping.pm_vers = parms.r_vers;
458 mapping.pm_prot = do_tcp ? IPPROTO_TCP : IPPROTO_UDP;
461 stat = CLNT_CALL(rpcb, (rpcprog_t) PMAPPROC_GETPORT,
462 (xdrproc_t) xdr_portmap, &mapping,
463 (xdrproc_t) xdr_u_short, &port, timo);
465 if (stat == RPC_SUCCESS) {
466 switch (ss.ss_family) {
468 ((struct sockaddr_in *)&ss)->sin_port =
474 ((struct sockaddr_in6 *)&ss)->sin6_port =
482 panic("invalid rpcvers %d", rpcvers);
485 * We may have a positive response from the portmapper, but the NLM
486 * service was not found. Make sure we received a valid port.
488 switch (ss.ss_family) {
490 port = ((struct sockaddr_in *)&ss)->sin_port;
494 port = ((struct sockaddr_in6 *)&ss)->sin6_port;
498 if (stat != RPC_SUCCESS || !port) {
500 * If we were able to talk to rpcbind or portmap, but the udp
501 * variant wasn't available, ask about tcp.
503 * XXX - We could also check for a TCP portmapper, but
504 * if the host is running a portmapper at all, we should be able
505 * to hail it over UDP.
507 if (stat == RPC_SUCCESS && !do_tcp) {
512 /* Otherwise, bad news. */
513 NLM_ERR("NLM: failed to contact remote rpcbind, "
514 "stat = %d, port = %d\n", (int) stat, port);
521 * Destroy the UDP client we used to speak to rpcbind and
522 * recreate as a TCP client.
524 struct netconfig *nconf = NULL;
528 switch (ss.ss_family) {
530 nconf = getnetconfigent("tcp");
534 nconf = getnetconfigent("tcp6");
539 rpcb = clnt_reconnect_create(nconf, (struct sockaddr *)&ss,
541 CLNT_CONTROL(rpcb, CLSET_WAITCHAN, wchan);
542 rpcb->cl_auth = nlm_auth;
546 * Re-use the client we used to speak to rpcbind.
548 CLNT_CONTROL(rpcb, CLSET_SVC_ADDR, &ss);
549 CLNT_CONTROL(rpcb, CLSET_PROG, &prog);
550 CLNT_CONTROL(rpcb, CLSET_VERS, &vers);
551 CLNT_CONTROL(rpcb, CLSET_WAITCHAN, wchan);
552 rpcb->cl_auth = nlm_auth;
559 * This async callback after when an async lock request has been
560 * granted. We notify the host which initiated the request.
563 nlm_lock_callback(void *arg, int pending)
565 struct nlm_async_lock *af = (struct nlm_async_lock *) arg;
566 struct rpc_callextra ext;
568 NLM_DEBUG(2, "NLM: async lock %p for %s (sysid %d) granted,"
569 " cookie %d:%d\n", af, af->af_host->nh_caller_name,
570 af->af_host->nh_sysid, ng_sysid(&af->af_granted.cookie),
571 ng_cookie(&af->af_granted.cookie));
574 * Send the results back to the host.
576 * Note: there is a possible race here with nlm_host_notify
577 * destroying the RPC client. To avoid problems, the first
578 * thing nlm_host_notify does is to cancel pending async lock
581 memset(&ext, 0, sizeof(ext));
582 ext.rc_auth = nlm_auth;
583 if (af->af_host->nh_vers == NLM_VERS4) {
584 nlm4_granted_msg_4(&af->af_granted,
585 NULL, af->af_rpc, &ext, nlm_zero_tv);
588 * Back-convert to legacy protocol
590 nlm_testargs granted;
591 granted.cookie = af->af_granted.cookie;
592 granted.exclusive = af->af_granted.exclusive;
593 granted.alock.caller_name =
594 af->af_granted.alock.caller_name;
595 granted.alock.fh = af->af_granted.alock.fh;
596 granted.alock.oh = af->af_granted.alock.oh;
597 granted.alock.svid = af->af_granted.alock.svid;
598 granted.alock.l_offset =
599 af->af_granted.alock.l_offset;
600 granted.alock.l_len =
601 af->af_granted.alock.l_len;
603 nlm_granted_msg_1(&granted,
604 NULL, af->af_rpc, &ext, nlm_zero_tv);
608 * Move this entry to the nh_granted list.
610 af->af_expiretime = time_uptime + NLM_EXPIRE_TIMEOUT;
611 mtx_lock(&af->af_host->nh_lock);
612 TAILQ_REMOVE(&af->af_host->nh_pending, af, af_link);
613 TAILQ_INSERT_TAIL(&af->af_host->nh_granted, af, af_link);
614 mtx_unlock(&af->af_host->nh_lock);
618 * Free an async lock request. The request must have been removed from
622 nlm_free_async_lock(struct nlm_async_lock *af)
625 * Free an async lock.
628 CLNT_RELEASE(af->af_rpc);
629 xdr_free((xdrproc_t) xdr_nlm4_testargs, &af->af_granted);
636 * Cancel our async request - this must be called with
637 * af->nh_host->nh_lock held. This is slightly complicated by a
638 * potential race with our own callback. If we fail to cancel the
639 * lock, it must already have been granted - we make sure our async
640 * task has completed by calling taskqueue_drain in this case.
643 nlm_cancel_async_lock(struct nlm_async_lock *af)
645 struct nlm_host *host = af->af_host;
648 mtx_assert(&host->nh_lock, MA_OWNED);
650 mtx_unlock(&host->nh_lock);
652 error = VOP_ADVLOCKASYNC(af->af_vp, NULL, F_CANCEL, &af->af_fl,
653 F_REMOTE, NULL, &af->af_cookie);
657 * We failed to cancel - make sure our callback has
658 * completed before we continue.
660 taskqueue_drain(taskqueue_thread, &af->af_task);
663 mtx_lock(&host->nh_lock);
666 NLM_DEBUG(2, "NLM: async lock %p for %s (sysid %d) "
667 "cancelled\n", af, host->nh_caller_name, host->nh_sysid);
670 * Remove from the nh_pending list and free now that
671 * we are safe from the callback.
673 TAILQ_REMOVE(&host->nh_pending, af, af_link);
674 mtx_unlock(&host->nh_lock);
675 nlm_free_async_lock(af);
676 mtx_lock(&host->nh_lock);
683 nlm_check_expired_locks(struct nlm_host *host)
685 struct nlm_async_lock *af;
686 time_t uptime = time_uptime;
688 mtx_lock(&host->nh_lock);
689 while ((af = TAILQ_FIRST(&host->nh_granted)) != NULL
690 && uptime >= af->af_expiretime) {
691 NLM_DEBUG(2, "NLM: async lock %p for %s (sysid %d) expired,"
692 " cookie %d:%d\n", af, af->af_host->nh_caller_name,
693 af->af_host->nh_sysid, ng_sysid(&af->af_granted.cookie),
694 ng_cookie(&af->af_granted.cookie));
695 TAILQ_REMOVE(&host->nh_granted, af, af_link);
696 mtx_unlock(&host->nh_lock);
697 nlm_free_async_lock(af);
698 mtx_lock(&host->nh_lock);
700 while ((af = TAILQ_FIRST(&host->nh_finished)) != NULL) {
701 TAILQ_REMOVE(&host->nh_finished, af, af_link);
702 mtx_unlock(&host->nh_lock);
703 nlm_free_async_lock(af);
704 mtx_lock(&host->nh_lock);
706 mtx_unlock(&host->nh_lock);
710 * Free resources used by a host. This is called after the reference
711 * count has reached zero so it doesn't need to worry about locks.
714 nlm_host_destroy(struct nlm_host *host)
717 mtx_lock(&nlm_global_lock);
718 TAILQ_REMOVE(&nlm_hosts, host, nh_link);
719 mtx_unlock(&nlm_global_lock);
721 if (host->nh_srvrpc.nr_client)
722 CLNT_RELEASE(host->nh_srvrpc.nr_client);
723 if (host->nh_clntrpc.nr_client)
724 CLNT_RELEASE(host->nh_clntrpc.nr_client);
725 mtx_destroy(&host->nh_lock);
726 sysctl_ctx_free(&host->nh_sysctl);
731 * Thread start callback for client lock recovery
734 nlm_client_recovery_start(void *arg)
736 struct nlm_host *host = (struct nlm_host *) arg;
738 NLM_DEBUG(1, "NLM: client lock recovery for %s started\n",
739 host->nh_caller_name);
741 nlm_client_recovery(host);
743 NLM_DEBUG(1, "NLM: client lock recovery for %s completed\n",
744 host->nh_caller_name);
746 host->nh_monstate = NLM_MONITORED;
747 nlm_host_release(host);
753 * This is called when we receive a host state change notification. We
754 * unlock any active locks owned by the host. When rpc.lockd is
755 * shutting down, this function is called with newstate set to zero
756 * which allows us to cancel any pending async locks and clear the
760 nlm_host_notify(struct nlm_host *host, int newstate)
762 struct nlm_async_lock *af;
765 NLM_DEBUG(1, "NLM: host %s (sysid %d) rebooted, new "
766 "state is %d\n", host->nh_caller_name,
767 host->nh_sysid, newstate);
771 * Cancel any pending async locks for this host.
773 mtx_lock(&host->nh_lock);
774 while ((af = TAILQ_FIRST(&host->nh_pending)) != NULL) {
776 * nlm_cancel_async_lock will remove the entry from
777 * nh_pending and free it.
779 nlm_cancel_async_lock(af);
781 mtx_unlock(&host->nh_lock);
782 nlm_check_expired_locks(host);
785 * The host just rebooted - trash its locks.
787 lf_clearremotesys(host->nh_sysid);
788 host->nh_state = newstate;
791 * If we have any remote locks for this host (i.e. it
792 * represents a remote NFS server that our local NFS client
793 * has locks for), start a recovery thread.
796 && host->nh_monstate != NLM_RECOVERING
797 && lf_countlocks(NLM_SYSID_CLIENT | host->nh_sysid) > 0) {
799 host->nh_monstate = NLM_RECOVERING;
800 refcount_acquire(&host->nh_refs);
801 kthread_add(nlm_client_recovery_start, host, curproc, &td, 0, 0,
802 "NFS lock recovery for %s", host->nh_caller_name);
807 * Sysctl handler to count the number of locks for a sysid.
810 nlm_host_lock_count_sysctl(SYSCTL_HANDLER_ARGS)
812 struct nlm_host *host;
815 host = oidp->oid_arg1;
816 count = lf_countlocks(host->nh_sysid);
817 return sysctl_handle_int(oidp, &count, 0, req);
821 * Sysctl handler to count the number of client locks for a sysid.
824 nlm_host_client_lock_count_sysctl(SYSCTL_HANDLER_ARGS)
826 struct nlm_host *host;
829 host = oidp->oid_arg1;
830 count = lf_countlocks(NLM_SYSID_CLIENT | host->nh_sysid);
831 return sysctl_handle_int(oidp, &count, 0, req);
835 * Create a new NLM host.
837 static struct nlm_host *
838 nlm_create_host(const char* caller_name)
840 struct nlm_host *host;
841 struct sysctl_oid *oid;
843 mtx_assert(&nlm_global_lock, MA_OWNED);
845 NLM_DEBUG(1, "NLM: new host %s (sysid %d)\n",
846 caller_name, nlm_next_sysid);
847 host = malloc(sizeof(struct nlm_host), M_NLM, M_NOWAIT|M_ZERO);
850 mtx_init(&host->nh_lock, "nh_lock", NULL, MTX_DEF);
852 strlcpy(host->nh_caller_name, caller_name, MAXNAMELEN);
853 host->nh_sysid = nlm_next_sysid++;
854 snprintf(host->nh_sysid_string, sizeof(host->nh_sysid_string),
855 "%d", host->nh_sysid);
858 host->nh_monstate = NLM_UNMONITORED;
859 host->nh_grantcookie = 1;
860 TAILQ_INIT(&host->nh_pending);
861 TAILQ_INIT(&host->nh_granted);
862 TAILQ_INIT(&host->nh_finished);
863 TAILQ_INSERT_TAIL(&nlm_hosts, host, nh_link);
865 mtx_unlock(&nlm_global_lock);
867 sysctl_ctx_init(&host->nh_sysctl);
868 oid = SYSCTL_ADD_NODE(&host->nh_sysctl,
869 SYSCTL_STATIC_CHILDREN(_vfs_nlm_sysid),
870 OID_AUTO, host->nh_sysid_string, CTLFLAG_RD, NULL, "");
871 SYSCTL_ADD_STRING(&host->nh_sysctl, SYSCTL_CHILDREN(oid), OID_AUTO,
872 "hostname", CTLFLAG_RD, host->nh_caller_name, 0, "");
873 SYSCTL_ADD_UINT(&host->nh_sysctl, SYSCTL_CHILDREN(oid), OID_AUTO,
874 "version", CTLFLAG_RD, &host->nh_vers, 0, "");
875 SYSCTL_ADD_UINT(&host->nh_sysctl, SYSCTL_CHILDREN(oid), OID_AUTO,
876 "monitored", CTLFLAG_RD, &host->nh_monstate, 0, "");
877 SYSCTL_ADD_PROC(&host->nh_sysctl, SYSCTL_CHILDREN(oid), OID_AUTO,
878 "lock_count", CTLTYPE_INT | CTLFLAG_RD, host, 0,
879 nlm_host_lock_count_sysctl, "I", "");
880 SYSCTL_ADD_PROC(&host->nh_sysctl, SYSCTL_CHILDREN(oid), OID_AUTO,
881 "client_lock_count", CTLTYPE_INT | CTLFLAG_RD, host, 0,
882 nlm_host_client_lock_count_sysctl, "I", "");
884 mtx_lock(&nlm_global_lock);
890 * Acquire the next sysid for remote locks not handled by the NLM.
893 nlm_acquire_next_sysid(void)
897 mtx_lock(&nlm_global_lock);
898 next_sysid = nlm_next_sysid++;
899 mtx_unlock(&nlm_global_lock);
904 * Return non-zero if the address parts of the two sockaddrs are the
908 nlm_compare_addr(const struct sockaddr *a, const struct sockaddr *b)
910 const struct sockaddr_in *a4, *b4;
912 const struct sockaddr_in6 *a6, *b6;
915 if (a->sa_family != b->sa_family)
918 switch (a->sa_family) {
920 a4 = (const struct sockaddr_in *) a;
921 b4 = (const struct sockaddr_in *) b;
922 return !memcmp(&a4->sin_addr, &b4->sin_addr,
923 sizeof(a4->sin_addr));
926 a6 = (const struct sockaddr_in6 *) a;
927 b6 = (const struct sockaddr_in6 *) b;
928 return !memcmp(&a6->sin6_addr, &b6->sin6_addr,
929 sizeof(a6->sin6_addr));
937 * Check for idle hosts and stop monitoring them. We could also free
938 * the host structure here, possibly after a larger timeout but that
939 * would require some care to avoid races with
940 * e.g. nlm_host_lock_count_sysctl.
945 struct nlm_host *host;
947 mtx_assert(&nlm_global_lock, MA_OWNED);
949 if (time_uptime <= nlm_next_idle_check)
952 nlm_next_idle_check = time_uptime + NLM_IDLE_PERIOD;
954 TAILQ_FOREACH(host, &nlm_hosts, nh_link) {
955 if (host->nh_monstate == NLM_MONITORED
956 && time_uptime > host->nh_idle_timeout) {
957 mtx_unlock(&nlm_global_lock);
958 if (lf_countlocks(host->nh_sysid) > 0
959 || lf_countlocks(NLM_SYSID_CLIENT
961 host->nh_idle_timeout =
962 time_uptime + NLM_IDLE_TIMEOUT;
963 mtx_lock(&nlm_global_lock);
966 nlm_host_unmonitor(host);
967 mtx_lock(&nlm_global_lock);
973 * Search for an existing NLM host that matches the given name
974 * (typically the caller_name element of an nlm4_lock). If none is
975 * found, create a new host. If 'addr' is non-NULL, record the remote
976 * address of the host so that we can call it back for async
977 * responses. If 'vers' is greater than zero then record the NLM
978 * program version to use to communicate with this client.
981 nlm_find_host_by_name(const char *name, const struct sockaddr *addr,
984 struct nlm_host *host;
986 mtx_lock(&nlm_global_lock);
989 * The remote host is determined by caller_name.
991 TAILQ_FOREACH(host, &nlm_hosts, nh_link) {
992 if (!strcmp(host->nh_caller_name, name))
997 host = nlm_create_host(name);
999 mtx_unlock(&nlm_global_lock);
1003 refcount_acquire(&host->nh_refs);
1005 host->nh_idle_timeout = time_uptime + NLM_IDLE_TIMEOUT;
1008 * If we have an address for the host, record it so that we
1009 * can send async replies etc.
1013 KASSERT(addr->sa_len < sizeof(struct sockaddr_storage),
1014 ("Strange remote transport address length"));
1017 * If we have seen an address before and we currently
1018 * have an RPC client handle, make sure the address is
1019 * the same, otherwise discard the client handle.
1021 if (host->nh_addr.ss_len && host->nh_srvrpc.nr_client) {
1022 if (!nlm_compare_addr(
1023 (struct sockaddr *) &host->nh_addr,
1025 || host->nh_vers != vers) {
1027 mtx_lock(&host->nh_lock);
1028 client = host->nh_srvrpc.nr_client;
1029 host->nh_srvrpc.nr_client = NULL;
1030 mtx_unlock(&host->nh_lock);
1032 CLNT_RELEASE(client);
1036 memcpy(&host->nh_addr, addr, addr->sa_len);
1037 host->nh_vers = vers;
1042 mtx_unlock(&nlm_global_lock);
1048 * Search for an existing NLM host that matches the given remote
1049 * address. If none is found, create a new host with the requested
1050 * address and remember 'vers' as the NLM protocol version to use for
1054 nlm_find_host_by_addr(const struct sockaddr *addr, int vers)
1057 * Fake up a name using inet_ntop. This buffer is
1058 * large enough for an IPv6 address.
1060 char tmp[sizeof "ffff:ffff:ffff:ffff:ffff:ffff:255.255.255.255"];
1061 struct nlm_host *host;
1063 switch (addr->sa_family) {
1066 &((const struct sockaddr_in *) addr)->sin_addr,
1072 &((const struct sockaddr_in6 *) addr)->sin6_addr,
1077 strlcpy(tmp, "<unknown>", sizeof(tmp));
1081 mtx_lock(&nlm_global_lock);
1084 * The remote host is determined by caller_name.
1086 TAILQ_FOREACH(host, &nlm_hosts, nh_link) {
1087 if (nlm_compare_addr(addr,
1088 (const struct sockaddr *) &host->nh_addr))
1093 host = nlm_create_host(tmp);
1095 mtx_unlock(&nlm_global_lock);
1098 memcpy(&host->nh_addr, addr, addr->sa_len);
1099 host->nh_vers = vers;
1101 refcount_acquire(&host->nh_refs);
1103 host->nh_idle_timeout = time_uptime + NLM_IDLE_TIMEOUT;
1107 mtx_unlock(&nlm_global_lock);
1113 * Find the NLM host that matches the value of 'sysid'. If none
1114 * exists, return NULL.
1116 static struct nlm_host *
1117 nlm_find_host_by_sysid(int sysid)
1119 struct nlm_host *host;
1121 TAILQ_FOREACH(host, &nlm_hosts, nh_link) {
1122 if (host->nh_sysid == sysid) {
1123 refcount_acquire(&host->nh_refs);
1131 void nlm_host_release(struct nlm_host *host)
1133 if (refcount_release(&host->nh_refs)) {
1137 nlm_host_destroy(host);
1142 * Unregister this NLM host with the local NSM due to idleness.
1145 nlm_host_unmonitor(struct nlm_host *host)
1149 struct timeval timo;
1150 enum clnt_stat stat;
1152 NLM_DEBUG(1, "NLM: unmonitoring %s (sysid %d)\n",
1153 host->nh_caller_name, host->nh_sysid);
1156 * We put our assigned system ID value in the priv field to
1157 * make it simpler to find the host if we are notified of a
1160 smmonid.mon_name = host->nh_caller_name;
1161 smmonid.my_id.my_name = "localhost";
1162 smmonid.my_id.my_prog = NLM_PROG;
1163 smmonid.my_id.my_vers = NLM_SM;
1164 smmonid.my_id.my_proc = NLM_SM_NOTIFY;
1168 stat = CLNT_CALL(nlm_nsm, SM_UNMON,
1169 (xdrproc_t) xdr_mon, &smmonid,
1170 (xdrproc_t) xdr_sm_stat, &smstat, timo);
1172 if (stat != RPC_SUCCESS) {
1173 NLM_ERR("Failed to contact local NSM - rpc error %d\n", stat);
1176 if (smstat.res_stat == stat_fail) {
1177 NLM_ERR("Local NSM refuses to unmonitor %s\n",
1178 host->nh_caller_name);
1182 host->nh_monstate = NLM_UNMONITORED;
1186 * Register this NLM host with the local NSM so that we can be
1187 * notified if it reboots.
1190 nlm_host_monitor(struct nlm_host *host, int state)
1194 struct timeval timo;
1195 enum clnt_stat stat;
1197 if (state && !host->nh_state) {
1199 * This is the first time we have seen an NSM state
1200 * value for this host. We record it here to help
1201 * detect host reboots.
1203 host->nh_state = state;
1204 NLM_DEBUG(1, "NLM: host %s (sysid %d) has NSM state %d\n",
1205 host->nh_caller_name, host->nh_sysid, state);
1208 mtx_lock(&host->nh_lock);
1209 if (host->nh_monstate != NLM_UNMONITORED) {
1210 mtx_unlock(&host->nh_lock);
1213 host->nh_monstate = NLM_MONITORED;
1214 mtx_unlock(&host->nh_lock);
1216 NLM_DEBUG(1, "NLM: monitoring %s (sysid %d)\n",
1217 host->nh_caller_name, host->nh_sysid);
1220 * We put our assigned system ID value in the priv field to
1221 * make it simpler to find the host if we are notified of a
1224 smmon.mon_id.mon_name = host->nh_caller_name;
1225 smmon.mon_id.my_id.my_name = "localhost";
1226 smmon.mon_id.my_id.my_prog = NLM_PROG;
1227 smmon.mon_id.my_id.my_vers = NLM_SM;
1228 smmon.mon_id.my_id.my_proc = NLM_SM_NOTIFY;
1229 memcpy(smmon.priv, &host->nh_sysid, sizeof(host->nh_sysid));
1233 stat = CLNT_CALL(nlm_nsm, SM_MON,
1234 (xdrproc_t) xdr_mon, &smmon,
1235 (xdrproc_t) xdr_sm_stat, &smstat, timo);
1237 if (stat != RPC_SUCCESS) {
1238 NLM_ERR("Failed to contact local NSM - rpc error %d\n", stat);
1241 if (smstat.res_stat == stat_fail) {
1242 NLM_ERR("Local NSM refuses to monitor %s\n",
1243 host->nh_caller_name);
1244 mtx_lock(&host->nh_lock);
1245 host->nh_monstate = NLM_MONITOR_FAILED;
1246 mtx_unlock(&host->nh_lock);
1250 host->nh_monstate = NLM_MONITORED;
1254 * Return an RPC client handle that can be used to talk to the NLM
1255 * running on the given host.
1258 nlm_host_get_rpc(struct nlm_host *host, bool_t isserver)
1260 struct nlm_rpc *rpc;
1263 mtx_lock(&host->nh_lock);
1266 rpc = &host->nh_srvrpc;
1268 rpc = &host->nh_clntrpc;
1271 * We can't hold onto RPC handles for too long - the async
1272 * call/reply protocol used by some NLM clients makes it hard
1273 * to tell when they change port numbers (e.g. after a
1274 * reboot). Note that if a client reboots while it isn't
1275 * holding any locks, it won't bother to notify us. We
1276 * expire the RPC handles after two minutes.
1278 if (rpc->nr_client && time_uptime > rpc->nr_create_time + 2*60) {
1279 client = rpc->nr_client;
1280 rpc->nr_client = NULL;
1281 mtx_unlock(&host->nh_lock);
1282 CLNT_RELEASE(client);
1283 mtx_lock(&host->nh_lock);
1286 if (!rpc->nr_client) {
1287 mtx_unlock(&host->nh_lock);
1288 client = nlm_get_rpc((struct sockaddr *)&host->nh_addr,
1289 NLM_PROG, host->nh_vers);
1290 mtx_lock(&host->nh_lock);
1293 if (rpc->nr_client) {
1294 mtx_unlock(&host->nh_lock);
1295 CLNT_DESTROY(client);
1296 mtx_lock(&host->nh_lock);
1298 rpc->nr_client = client;
1299 rpc->nr_create_time = time_uptime;
1304 client = rpc->nr_client;
1306 CLNT_ACQUIRE(client);
1307 mtx_unlock(&host->nh_lock);
1313 int nlm_host_get_sysid(struct nlm_host *host)
1316 return (host->nh_sysid);
1320 nlm_host_get_state(struct nlm_host *host)
1323 return (host->nh_state);
1327 nlm_register_wait_lock(struct nlm4_lock *lock, struct vnode *vp)
1329 struct nlm_waiting_lock *nw;
1331 nw = malloc(sizeof(struct nlm_waiting_lock), M_NLM, M_WAITOK);
1332 nw->nw_lock = *lock;
1333 memcpy(&nw->nw_fh.fh_bytes, nw->nw_lock.fh.n_bytes,
1334 nw->nw_lock.fh.n_len);
1335 nw->nw_lock.fh.n_bytes = nw->nw_fh.fh_bytes;
1336 nw->nw_waiting = TRUE;
1338 mtx_lock(&nlm_global_lock);
1339 TAILQ_INSERT_TAIL(&nlm_waiting_locks, nw, nw_link);
1340 mtx_unlock(&nlm_global_lock);
1346 nlm_deregister_wait_lock(void *handle)
1348 struct nlm_waiting_lock *nw = handle;
1350 mtx_lock(&nlm_global_lock);
1351 TAILQ_REMOVE(&nlm_waiting_locks, nw, nw_link);
1352 mtx_unlock(&nlm_global_lock);
1358 nlm_wait_lock(void *handle, int timo)
1360 struct nlm_waiting_lock *nw = handle;
1361 int error, stops_deferred;
1364 * If the granted message arrived before we got here,
1365 * nw->nw_waiting will be FALSE - in that case, don't sleep.
1367 mtx_lock(&nlm_global_lock);
1369 if (nw->nw_waiting) {
1370 stops_deferred = sigdeferstop(SIGDEFERSTOP_ERESTART);
1371 error = msleep(nw, &nlm_global_lock, PCATCH, "nlmlock", timo);
1372 sigallowstop(stops_deferred);
1374 TAILQ_REMOVE(&nlm_waiting_locks, nw, nw_link);
1377 * The granted message may arrive after the
1378 * interrupt/timeout but before we manage to lock the
1379 * mutex. Detect this by examining nw_lock.
1381 if (!nw->nw_waiting)
1385 * If nlm_cancel_wait is called, then error will be
1386 * zero but nw_waiting will still be TRUE. We
1387 * translate this into EINTR.
1392 mtx_unlock(&nlm_global_lock);
1400 nlm_cancel_wait(struct vnode *vp)
1402 struct nlm_waiting_lock *nw;
1404 mtx_lock(&nlm_global_lock);
1405 TAILQ_FOREACH(nw, &nlm_waiting_locks, nw_link) {
1406 if (nw->nw_vp == vp) {
1410 mtx_unlock(&nlm_global_lock);
1414 /**********************************************************************/
1417 * Syscall interface with userland.
1420 extern void nlm_prog_0(struct svc_req *rqstp, SVCXPRT *transp);
1421 extern void nlm_prog_1(struct svc_req *rqstp, SVCXPRT *transp);
1422 extern void nlm_prog_3(struct svc_req *rqstp, SVCXPRT *transp);
1423 extern void nlm_prog_4(struct svc_req *rqstp, SVCXPRT *transp);
1426 nlm_register_services(SVCPOOL *pool, int addr_count, char **addrs)
1428 static rpcvers_t versions[] = {
1429 NLM_SM, NLM_VERS, NLM_VERSX, NLM_VERS4
1431 static void (*dispatchers[])(struct svc_req *, SVCXPRT *) = {
1432 nlm_prog_0, nlm_prog_1, nlm_prog_3, nlm_prog_4
1438 struct netconfig *nconf;
1442 NLM_ERR("NLM: no service addresses given - can't start server");
1446 if (addr_count < 0 || addr_count > 256 ) {
1447 NLM_ERR("NLM: too many service addresses (%d) given, "
1448 "max 256 - can't start server\n", addr_count);
1452 xprts = malloc(addr_count * sizeof(SVCXPRT *), M_NLM, M_WAITOK|M_ZERO);
1453 for (i = 0; i < nitems(versions); i++) {
1454 for (j = 0; j < addr_count; j++) {
1456 * Create transports for the first version and
1457 * then just register everything else to the
1463 error = copyin(&addrs[2*j], &up,
1467 error = copyinstr(up, netid, sizeof(netid),
1471 error = copyin(&addrs[2*j+1], &up,
1475 error = copyinstr(up, uaddr, sizeof(uaddr),
1479 nconf = getnetconfigent(netid);
1481 NLM_ERR("Can't lookup netid %s\n",
1486 xprts[j] = svc_tp_create(pool, dispatchers[i],
1487 NLM_PROG, versions[i], uaddr, nconf);
1489 NLM_ERR("NLM: unable to create "
1490 "(NLM_PROG, %d).\n", versions[i]);
1494 freenetconfigent(nconf);
1496 nconf = getnetconfigent(xprts[j]->xp_netid);
1497 rpcb_unset(NLM_PROG, versions[i], nconf);
1498 if (!svc_reg(xprts[j], NLM_PROG, versions[i],
1499 dispatchers[i], nconf)) {
1500 NLM_ERR("NLM: can't register "
1501 "(NLM_PROG, %d)\n", versions[i]);
1510 for (j = 0; j < addr_count; j++) {
1512 SVC_RELEASE(xprts[j]);
1519 * Main server entry point. Contacts the local NSM to get its current
1520 * state and send SM_UNMON_ALL. Registers the NLM services and then
1521 * services requests. Does not return until the server is interrupted
1525 nlm_server_main(int addr_count, char **addrs)
1527 struct thread *td = curthread;
1529 SVCPOOL *pool = NULL;
1533 struct sockaddr_in6 sin6;
1535 struct sockaddr_in sin;
1538 struct timeval timo;
1539 enum clnt_stat stat;
1540 struct nlm_host *host, *nhost;
1541 struct nlm_waiting_lock *nw;
1542 vop_advlock_t *old_nfs_advlock;
1543 vop_reclaim_t *old_nfs_reclaim;
1545 if (nlm_is_running != 0) {
1546 NLM_ERR("NLM: can't start server - "
1547 "it appears to be running already\n");
1551 if (nlm_socket == NULL) {
1552 memset(&opt, 0, sizeof(opt));
1554 error = socreate(AF_INET, &nlm_socket, SOCK_DGRAM, 0,
1557 NLM_ERR("NLM: can't create IPv4 socket - error %d\n",
1561 opt.sopt_dir = SOPT_SET;
1562 opt.sopt_level = IPPROTO_IP;
1563 opt.sopt_name = IP_PORTRANGE;
1564 portlow = IP_PORTRANGE_LOW;
1565 opt.sopt_val = &portlow;
1566 opt.sopt_valsize = sizeof(portlow);
1567 sosetopt(nlm_socket, &opt);
1571 error = socreate(AF_INET6, &nlm_socket6, SOCK_DGRAM, 0,
1574 NLM_ERR("NLM: can't create IPv6 socket - error %d\n",
1576 soclose(nlm_socket);
1580 opt.sopt_dir = SOPT_SET;
1581 opt.sopt_level = IPPROTO_IPV6;
1582 opt.sopt_name = IPV6_PORTRANGE;
1583 portlow = IPV6_PORTRANGE_LOW;
1584 opt.sopt_val = &portlow;
1585 opt.sopt_valsize = sizeof(portlow);
1586 sosetopt(nlm_socket6, &opt);
1590 nlm_auth = authunix_create(curthread->td_ucred);
1593 memset(&sin6, 0, sizeof(sin6));
1594 sin6.sin6_len = sizeof(sin6);
1595 sin6.sin6_family = AF_INET6;
1596 sin6.sin6_addr = in6addr_loopback;
1597 nlm_nsm = nlm_get_rpc((struct sockaddr *) &sin6, SM_PROG, SM_VERS);
1600 memset(&sin, 0, sizeof(sin));
1601 sin.sin_len = sizeof(sin);
1602 sin.sin_family = AF_INET;
1603 sin.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
1604 nlm_nsm = nlm_get_rpc((struct sockaddr *) &sin, SM_PROG,
1611 NLM_ERR("Can't start NLM - unable to contact NSM\n");
1616 pool = svcpool_create("NLM", NULL);
1618 error = nlm_register_services(pool, addr_count, addrs);
1622 memset(&id, 0, sizeof(id));
1623 id.my_name = "NFS NLM";
1627 stat = CLNT_CALL(nlm_nsm, SM_UNMON_ALL,
1628 (xdrproc_t) xdr_my_id, &id,
1629 (xdrproc_t) xdr_sm_stat, &smstat, timo);
1631 if (stat != RPC_SUCCESS) {
1634 CLNT_GETERR(nlm_nsm, &err);
1635 NLM_ERR("NLM: unexpected error contacting NSM, "
1636 "stat=%d, errno=%d\n", stat, err.re_errno);
1642 NLM_DEBUG(1, "NLM: local NSM state is %d\n", smstat.state);
1643 nlm_nsm_state = smstat.state;
1645 old_nfs_advlock = nfs_advlock_p;
1646 nfs_advlock_p = nlm_advlock;
1647 old_nfs_reclaim = nfs_reclaim_p;
1648 nfs_reclaim_p = nlm_reclaim;
1653 nfs_advlock_p = old_nfs_advlock;
1654 nfs_reclaim_p = old_nfs_reclaim;
1659 svcpool_destroy(pool);
1662 * We are finished communicating with the NSM.
1665 CLNT_RELEASE(nlm_nsm);
1670 * Trash all the existing state so that if the server
1671 * restarts, it gets a clean slate. This is complicated by the
1672 * possibility that there may be other threads trying to make
1673 * client locking requests.
1675 * First we fake a client reboot notification which will
1676 * cancel any pending async locks and purge remote lock state
1677 * from the local lock manager. We release the reference from
1678 * nlm_hosts to the host (which may remove it from the list
1679 * and free it). After this phase, the only entries in the
1680 * nlm_host list should be from other threads performing
1681 * client lock requests.
1683 mtx_lock(&nlm_global_lock);
1684 TAILQ_FOREACH(nw, &nlm_waiting_locks, nw_link) {
1687 TAILQ_FOREACH_SAFE(host, &nlm_hosts, nh_link, nhost) {
1688 mtx_unlock(&nlm_global_lock);
1689 nlm_host_notify(host, 0);
1690 nlm_host_release(host);
1691 mtx_lock(&nlm_global_lock);
1693 mtx_unlock(&nlm_global_lock);
1695 AUTH_DESTROY(nlm_auth);
1701 sys_nlm_syscall(struct thread *td, struct nlm_syscall_args *uap)
1705 #if __FreeBSD_version >= 700000
1706 error = priv_check(td, PRIV_NFS_LOCKD);
1713 nlm_debug_level = uap->debug_level;
1714 nlm_grace_threshold = time_uptime + uap->grace_period;
1715 nlm_next_idle_check = time_uptime + NLM_IDLE_PERIOD;
1717 return nlm_server_main(uap->addr_count, uap->addrs);
1720 /**********************************************************************/
1723 * NLM implementation details, called from the RPC stubs.
1728 nlm_sm_notify(struct nlm_sm_status *argp)
1731 struct nlm_host *host;
1733 NLM_DEBUG(3, "nlm_sm_notify(): mon_name = %s\n", argp->mon_name);
1734 memcpy(&sysid, &argp->priv, sizeof(sysid));
1735 host = nlm_find_host_by_sysid(sysid);
1737 nlm_host_notify(host, argp->state);
1738 nlm_host_release(host);
1743 nlm_convert_to_fhandle_t(fhandle_t *fhp, struct netobj *p)
1745 memcpy(fhp, p->n_bytes, sizeof(fhandle_t));
1749 struct mount *vs_mp;
1750 struct vnode *vs_vp;
1755 nlm_get_vfs_state(struct nlm_host *host, struct svc_req *rqstp,
1756 fhandle_t *fhp, struct vfs_state *vs, accmode_t accmode)
1759 struct ucred *cred = NULL, *credanon = NULL;
1761 memset(vs, 0, sizeof(*vs));
1763 vs->vs_mp = vfs_getvfs(&fhp->fh_fsid);
1768 /* accmode == 0 means don't check, since it is an unlock. */
1770 error = VFS_CHECKEXP(vs->vs_mp,
1771 (struct sockaddr *)&host->nh_addr, &exflags, &credanon,
1776 if (exflags & MNT_EXRDONLY ||
1777 (vs->vs_mp->mnt_flag & MNT_RDONLY)) {
1783 error = VFS_FHTOVP(vs->vs_mp, &fhp->fh_fid, LK_EXCLUSIVE, &vs->vs_vp);
1786 vs->vs_vnlocked = TRUE;
1789 if (!svc_getcred(rqstp, &cred, NULL)) {
1793 if (cred->cr_uid == 0 || (exflags & MNT_EXPORTANON)) {
1802 error = VOP_ACCESS(vs->vs_vp, accmode, cred, curthread);
1804 * If this failed and accmode != VWRITE, try again with
1805 * VWRITE to maintain backwards compatibility with the
1806 * old code that always used VWRITE.
1808 if (error != 0 && accmode != VWRITE)
1809 error = VOP_ACCESS(vs->vs_vp, VWRITE, cred, curthread);
1814 #if __FreeBSD_version < 800011
1815 VOP_UNLOCK(vs->vs_vp, 0, curthread);
1817 VOP_UNLOCK(vs->vs_vp, 0);
1819 vs->vs_vnlocked = FALSE;
1831 nlm_release_vfs_state(struct vfs_state *vs)
1835 if (vs->vs_vnlocked)
1845 nlm_convert_error(int error)
1848 if (error == ESTALE)
1849 return nlm4_stale_fh;
1850 else if (error == EROFS)
1857 nlm_do_test(nlm4_testargs *argp, nlm4_testres *result, struct svc_req *rqstp,
1861 struct vfs_state vs;
1862 struct nlm_host *host, *bhost;
1867 memset(result, 0, sizeof(*result));
1868 memset(&vs, 0, sizeof(vs));
1870 host = nlm_find_host_by_name(argp->alock.caller_name,
1871 svc_getrpccaller(rqstp), rqstp->rq_vers);
1873 result->stat.stat = nlm4_denied_nolocks;
1877 NLM_DEBUG(3, "nlm_do_test(): caller_name = %s (sysid = %d)\n",
1878 host->nh_caller_name, host->nh_sysid);
1880 nlm_check_expired_locks(host);
1881 sysid = host->nh_sysid;
1883 nlm_convert_to_fhandle_t(&fh, &argp->alock.fh);
1884 nlm_copy_netobj(&result->cookie, &argp->cookie, M_RPC);
1886 if (time_uptime < nlm_grace_threshold) {
1887 result->stat.stat = nlm4_denied_grace_period;
1891 accmode = argp->exclusive ? VWRITE : VREAD;
1892 error = nlm_get_vfs_state(host, rqstp, &fh, &vs, accmode);
1894 result->stat.stat = nlm_convert_error(error);
1898 fl.l_start = argp->alock.l_offset;
1899 fl.l_len = argp->alock.l_len;
1900 fl.l_pid = argp->alock.svid;
1902 fl.l_whence = SEEK_SET;
1903 if (argp->exclusive)
1904 fl.l_type = F_WRLCK;
1906 fl.l_type = F_RDLCK;
1907 error = VOP_ADVLOCK(vs.vs_vp, NULL, F_GETLK, &fl, F_REMOTE);
1909 result->stat.stat = nlm4_failed;
1913 if (fl.l_type == F_UNLCK) {
1914 result->stat.stat = nlm4_granted;
1916 result->stat.stat = nlm4_denied;
1917 result->stat.nlm4_testrply_u.holder.exclusive =
1918 (fl.l_type == F_WRLCK);
1919 result->stat.nlm4_testrply_u.holder.svid = fl.l_pid;
1920 bhost = nlm_find_host_by_sysid(fl.l_sysid);
1923 * We don't have any useful way of recording
1924 * the value of oh used in the original lock
1925 * request. Ideally, the test reply would have
1926 * a space for the owning host's name allowing
1927 * our caller's NLM to keep track.
1929 * As far as I can see, Solaris uses an eight
1930 * byte structure for oh which contains a four
1931 * byte pid encoded in local byte order and
1932 * the first four bytes of the host
1933 * name. Linux uses a variable length string
1934 * 'pid@hostname' in ascii but doesn't even
1935 * return that in test replies.
1937 * For the moment, return nothing in oh
1938 * (already zero'ed above).
1940 nlm_host_release(bhost);
1942 result->stat.nlm4_testrply_u.holder.l_offset = fl.l_start;
1943 result->stat.nlm4_testrply_u.holder.l_len = fl.l_len;
1947 nlm_release_vfs_state(&vs);
1949 *rpcp = nlm_host_get_rpc(host, TRUE);
1950 nlm_host_release(host);
1955 nlm_do_lock(nlm4_lockargs *argp, nlm4_res *result, struct svc_req *rqstp,
1956 bool_t monitor, CLIENT **rpcp)
1959 struct vfs_state vs;
1960 struct nlm_host *host;
1965 memset(result, 0, sizeof(*result));
1966 memset(&vs, 0, sizeof(vs));
1968 host = nlm_find_host_by_name(argp->alock.caller_name,
1969 svc_getrpccaller(rqstp), rqstp->rq_vers);
1971 result->stat.stat = nlm4_denied_nolocks;
1975 NLM_DEBUG(3, "nlm_do_lock(): caller_name = %s (sysid = %d)\n",
1976 host->nh_caller_name, host->nh_sysid);
1978 if (monitor && host->nh_state && argp->state
1979 && host->nh_state != argp->state) {
1981 * The host rebooted without telling us. Trash its
1984 nlm_host_notify(host, argp->state);
1987 nlm_check_expired_locks(host);
1988 sysid = host->nh_sysid;
1990 nlm_convert_to_fhandle_t(&fh, &argp->alock.fh);
1991 nlm_copy_netobj(&result->cookie, &argp->cookie, M_RPC);
1993 if (time_uptime < nlm_grace_threshold && !argp->reclaim) {
1994 result->stat.stat = nlm4_denied_grace_period;
1998 accmode = argp->exclusive ? VWRITE : VREAD;
1999 error = nlm_get_vfs_state(host, rqstp, &fh, &vs, accmode);
2001 result->stat.stat = nlm_convert_error(error);
2005 fl.l_start = argp->alock.l_offset;
2006 fl.l_len = argp->alock.l_len;
2007 fl.l_pid = argp->alock.svid;
2009 fl.l_whence = SEEK_SET;
2010 if (argp->exclusive)
2011 fl.l_type = F_WRLCK;
2013 fl.l_type = F_RDLCK;
2015 struct nlm_async_lock *af;
2017 struct nlm_grantcookie cookie;
2020 * First, make sure we can contact the host's NLM.
2022 client = nlm_host_get_rpc(host, TRUE);
2024 result->stat.stat = nlm4_failed;
2029 * First we need to check and see if there is an
2030 * existing blocked lock that matches. This could be a
2031 * badly behaved client or an RPC re-send. If we find
2032 * one, just return nlm4_blocked.
2034 mtx_lock(&host->nh_lock);
2035 TAILQ_FOREACH(af, &host->nh_pending, af_link) {
2036 if (af->af_fl.l_start == fl.l_start
2037 && af->af_fl.l_len == fl.l_len
2038 && af->af_fl.l_pid == fl.l_pid
2039 && af->af_fl.l_type == fl.l_type) {
2044 cookie.ng_sysid = host->nh_sysid;
2045 cookie.ng_cookie = host->nh_grantcookie++;
2047 mtx_unlock(&host->nh_lock);
2049 CLNT_RELEASE(client);
2050 result->stat.stat = nlm4_blocked;
2054 af = malloc(sizeof(struct nlm_async_lock), M_NLM,
2056 TASK_INIT(&af->af_task, 0, nlm_lock_callback, af);
2057 af->af_vp = vs.vs_vp;
2060 af->af_rpc = client;
2062 * We use M_RPC here so that we can xdr_free the thing
2065 nlm_make_netobj(&af->af_granted.cookie,
2066 (caddr_t)&cookie, sizeof(cookie), M_RPC);
2067 af->af_granted.exclusive = argp->exclusive;
2068 af->af_granted.alock.caller_name =
2069 strdup(argp->alock.caller_name, M_RPC);
2070 nlm_copy_netobj(&af->af_granted.alock.fh,
2071 &argp->alock.fh, M_RPC);
2072 nlm_copy_netobj(&af->af_granted.alock.oh,
2073 &argp->alock.oh, M_RPC);
2074 af->af_granted.alock.svid = argp->alock.svid;
2075 af->af_granted.alock.l_offset = argp->alock.l_offset;
2076 af->af_granted.alock.l_len = argp->alock.l_len;
2079 * Put the entry on the pending list before calling
2080 * VOP_ADVLOCKASYNC. We do this in case the lock
2081 * request was blocked (returning EINPROGRESS) but
2082 * then granted before we manage to run again. The
2083 * client may receive the granted message before we
2084 * send our blocked reply but thats their problem.
2086 mtx_lock(&host->nh_lock);
2087 TAILQ_INSERT_TAIL(&host->nh_pending, af, af_link);
2088 mtx_unlock(&host->nh_lock);
2090 error = VOP_ADVLOCKASYNC(vs.vs_vp, NULL, F_SETLK, &fl, F_REMOTE,
2091 &af->af_task, &af->af_cookie);
2094 * If the lock completed synchronously, just free the
2095 * tracking structure now.
2097 if (error != EINPROGRESS) {
2098 CLNT_RELEASE(af->af_rpc);
2099 mtx_lock(&host->nh_lock);
2100 TAILQ_REMOVE(&host->nh_pending, af, af_link);
2101 mtx_unlock(&host->nh_lock);
2102 xdr_free((xdrproc_t) xdr_nlm4_testargs,
2106 NLM_DEBUG(2, "NLM: pending async lock %p for %s "
2107 "(sysid %d)\n", af, host->nh_caller_name, sysid);
2109 * Don't vrele the vnode just yet - this must
2110 * wait until either the async callback
2111 * happens or the lock is cancelled.
2116 error = VOP_ADVLOCK(vs.vs_vp, NULL, F_SETLK, &fl, F_REMOTE);
2120 if (error == EINPROGRESS) {
2121 result->stat.stat = nlm4_blocked;
2122 } else if (error == EDEADLK) {
2123 result->stat.stat = nlm4_deadlck;
2124 } else if (error == EAGAIN) {
2125 result->stat.stat = nlm4_denied;
2127 result->stat.stat = nlm4_failed;
2131 nlm_host_monitor(host, argp->state);
2132 result->stat.stat = nlm4_granted;
2136 nlm_release_vfs_state(&vs);
2138 *rpcp = nlm_host_get_rpc(host, TRUE);
2139 nlm_host_release(host);
2144 nlm_do_cancel(nlm4_cancargs *argp, nlm4_res *result, struct svc_req *rqstp,
2148 struct vfs_state vs;
2149 struct nlm_host *host;
2152 struct nlm_async_lock *af;
2154 memset(result, 0, sizeof(*result));
2155 memset(&vs, 0, sizeof(vs));
2157 host = nlm_find_host_by_name(argp->alock.caller_name,
2158 svc_getrpccaller(rqstp), rqstp->rq_vers);
2160 result->stat.stat = nlm4_denied_nolocks;
2164 NLM_DEBUG(3, "nlm_do_cancel(): caller_name = %s (sysid = %d)\n",
2165 host->nh_caller_name, host->nh_sysid);
2167 nlm_check_expired_locks(host);
2168 sysid = host->nh_sysid;
2170 nlm_convert_to_fhandle_t(&fh, &argp->alock.fh);
2171 nlm_copy_netobj(&result->cookie, &argp->cookie, M_RPC);
2173 if (time_uptime < nlm_grace_threshold) {
2174 result->stat.stat = nlm4_denied_grace_period;
2178 error = nlm_get_vfs_state(host, rqstp, &fh, &vs, (accmode_t)0);
2180 result->stat.stat = nlm_convert_error(error);
2184 fl.l_start = argp->alock.l_offset;
2185 fl.l_len = argp->alock.l_len;
2186 fl.l_pid = argp->alock.svid;
2188 fl.l_whence = SEEK_SET;
2189 if (argp->exclusive)
2190 fl.l_type = F_WRLCK;
2192 fl.l_type = F_RDLCK;
2195 * First we need to try and find the async lock request - if
2196 * there isn't one, we give up and return nlm4_denied.
2198 mtx_lock(&host->nh_lock);
2200 TAILQ_FOREACH(af, &host->nh_pending, af_link) {
2201 if (af->af_fl.l_start == fl.l_start
2202 && af->af_fl.l_len == fl.l_len
2203 && af->af_fl.l_pid == fl.l_pid
2204 && af->af_fl.l_type == fl.l_type) {
2210 mtx_unlock(&host->nh_lock);
2211 result->stat.stat = nlm4_denied;
2215 error = nlm_cancel_async_lock(af);
2218 result->stat.stat = nlm4_denied;
2220 result->stat.stat = nlm4_granted;
2223 mtx_unlock(&host->nh_lock);
2226 nlm_release_vfs_state(&vs);
2228 *rpcp = nlm_host_get_rpc(host, TRUE);
2229 nlm_host_release(host);
2234 nlm_do_unlock(nlm4_unlockargs *argp, nlm4_res *result, struct svc_req *rqstp,
2238 struct vfs_state vs;
2239 struct nlm_host *host;
2243 memset(result, 0, sizeof(*result));
2244 memset(&vs, 0, sizeof(vs));
2246 host = nlm_find_host_by_name(argp->alock.caller_name,
2247 svc_getrpccaller(rqstp), rqstp->rq_vers);
2249 result->stat.stat = nlm4_denied_nolocks;
2253 NLM_DEBUG(3, "nlm_do_unlock(): caller_name = %s (sysid = %d)\n",
2254 host->nh_caller_name, host->nh_sysid);
2256 nlm_check_expired_locks(host);
2257 sysid = host->nh_sysid;
2259 nlm_convert_to_fhandle_t(&fh, &argp->alock.fh);
2260 nlm_copy_netobj(&result->cookie, &argp->cookie, M_RPC);
2262 if (time_uptime < nlm_grace_threshold) {
2263 result->stat.stat = nlm4_denied_grace_period;
2267 error = nlm_get_vfs_state(host, rqstp, &fh, &vs, (accmode_t)0);
2269 result->stat.stat = nlm_convert_error(error);
2273 fl.l_start = argp->alock.l_offset;
2274 fl.l_len = argp->alock.l_len;
2275 fl.l_pid = argp->alock.svid;
2277 fl.l_whence = SEEK_SET;
2278 fl.l_type = F_UNLCK;
2279 error = VOP_ADVLOCK(vs.vs_vp, NULL, F_UNLCK, &fl, F_REMOTE);
2282 * Ignore the error - there is no result code for failure,
2283 * only for grace period.
2285 result->stat.stat = nlm4_granted;
2288 nlm_release_vfs_state(&vs);
2290 *rpcp = nlm_host_get_rpc(host, TRUE);
2291 nlm_host_release(host);
2296 nlm_do_granted(nlm4_testargs *argp, nlm4_res *result, struct svc_req *rqstp,
2300 struct nlm_host *host;
2301 struct nlm_waiting_lock *nw;
2303 memset(result, 0, sizeof(*result));
2305 host = nlm_find_host_by_addr(svc_getrpccaller(rqstp), rqstp->rq_vers);
2307 result->stat.stat = nlm4_denied_nolocks;
2311 nlm_copy_netobj(&result->cookie, &argp->cookie, M_RPC);
2312 result->stat.stat = nlm4_denied;
2313 KFAIL_POINT_CODE(DEBUG_FP, nlm_deny_grant, goto out);
2315 mtx_lock(&nlm_global_lock);
2316 TAILQ_FOREACH(nw, &nlm_waiting_locks, nw_link) {
2317 if (!nw->nw_waiting)
2319 if (argp->alock.svid == nw->nw_lock.svid
2320 && argp->alock.l_offset == nw->nw_lock.l_offset
2321 && argp->alock.l_len == nw->nw_lock.l_len
2322 && argp->alock.fh.n_len == nw->nw_lock.fh.n_len
2323 && !memcmp(argp->alock.fh.n_bytes, nw->nw_lock.fh.n_bytes,
2324 nw->nw_lock.fh.n_len)) {
2325 nw->nw_waiting = FALSE;
2327 result->stat.stat = nlm4_granted;
2331 mtx_unlock(&nlm_global_lock);
2335 *rpcp = nlm_host_get_rpc(host, TRUE);
2336 nlm_host_release(host);
2341 nlm_do_granted_res(nlm4_res *argp, struct svc_req *rqstp)
2343 struct nlm_host *host = NULL;
2344 struct nlm_async_lock *af = NULL;
2347 if (argp->cookie.n_len != sizeof(struct nlm_grantcookie)) {
2348 NLM_DEBUG(1, "NLM: bogus grant cookie");
2352 host = nlm_find_host_by_sysid(ng_sysid(&argp->cookie));
2354 NLM_DEBUG(1, "NLM: Unknown host rejected our grant");
2358 mtx_lock(&host->nh_lock);
2359 TAILQ_FOREACH(af, &host->nh_granted, af_link)
2360 if (ng_cookie(&argp->cookie) ==
2361 ng_cookie(&af->af_granted.cookie))
2364 TAILQ_REMOVE(&host->nh_granted, af, af_link);
2365 mtx_unlock(&host->nh_lock);
2368 NLM_DEBUG(1, "NLM: host %s (sysid %d) replied to our grant "
2369 "with unrecognized cookie %d:%d", host->nh_caller_name,
2370 host->nh_sysid, ng_sysid(&argp->cookie),
2371 ng_cookie(&argp->cookie));
2375 if (argp->stat.stat != nlm4_granted) {
2376 af->af_fl.l_type = F_UNLCK;
2377 error = VOP_ADVLOCK(af->af_vp, NULL, F_UNLCK, &af->af_fl, F_REMOTE);
2379 NLM_DEBUG(1, "NLM: host %s (sysid %d) rejected our grant "
2380 "and we failed to unlock (%d)", host->nh_caller_name,
2381 host->nh_sysid, error);
2385 NLM_DEBUG(5, "NLM: async lock %p rejected by host %s (sysid %d)",
2386 af, host->nh_caller_name, host->nh_sysid);
2388 NLM_DEBUG(5, "NLM: async lock %p accepted by host %s (sysid %d)",
2389 af, host->nh_caller_name, host->nh_sysid);
2394 nlm_free_async_lock(af);
2396 nlm_host_release(host);
2400 nlm_do_free_all(nlm4_notify *argp)
2402 struct nlm_host *host, *thost;
2404 TAILQ_FOREACH_SAFE(host, &nlm_hosts, nh_link, thost) {
2405 if (!strcmp(host->nh_caller_name, argp->name))
2406 nlm_host_notify(host, argp->state);
2411 * Kernel module glue
2414 nfslockd_modevent(module_t mod, int type, void *data)
2421 /* The NLM module cannot be safely unloaded. */
2424 return (EOPNOTSUPP);
2427 static moduledata_t nfslockd_mod = {
2432 DECLARE_MODULE(nfslockd, nfslockd_mod, SI_SUB_VFS, SI_ORDER_ANY);
2434 /* So that loader and kldload(2) can find us, wherever we are.. */
2435 MODULE_DEPEND(nfslockd, krpc, 1, 1, 1);
2436 MODULE_DEPEND(nfslockd, nfslock, 1, 1, 1);
2437 MODULE_VERSION(nfslockd, 1);