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 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, NULL, "");
854 SYSCTL_ADD_STRING(&host->nh_sysctl, SYSCTL_CHILDREN(oid), OID_AUTO,
855 "hostname", CTLFLAG_RD, host->nh_caller_name, 0, "");
856 SYSCTL_ADD_UINT(&host->nh_sysctl, SYSCTL_CHILDREN(oid), OID_AUTO,
857 "version", CTLFLAG_RD, &host->nh_vers, 0, "");
858 SYSCTL_ADD_UINT(&host->nh_sysctl, SYSCTL_CHILDREN(oid), OID_AUTO,
859 "monitored", CTLFLAG_RD, &host->nh_monstate, 0, "");
860 SYSCTL_ADD_PROC(&host->nh_sysctl, SYSCTL_CHILDREN(oid), OID_AUTO,
861 "lock_count", CTLTYPE_INT | CTLFLAG_RD, host, 0,
862 nlm_host_lock_count_sysctl, "I", "");
863 SYSCTL_ADD_PROC(&host->nh_sysctl, SYSCTL_CHILDREN(oid), OID_AUTO,
864 "client_lock_count", CTLTYPE_INT | CTLFLAG_RD, host, 0,
865 nlm_host_client_lock_count_sysctl, "I", "");
867 mtx_lock(&nlm_global_lock);
873 * Acquire the next sysid for remote locks not handled by the NLM.
876 nlm_acquire_next_sysid(void)
880 mtx_lock(&nlm_global_lock);
881 next_sysid = nlm_next_sysid++;
882 mtx_unlock(&nlm_global_lock);
887 * Return non-zero if the address parts of the two sockaddrs are the
891 nlm_compare_addr(const struct sockaddr *a, const struct sockaddr *b)
893 const struct sockaddr_in *a4, *b4;
895 const struct sockaddr_in6 *a6, *b6;
898 if (a->sa_family != b->sa_family)
901 switch (a->sa_family) {
903 a4 = (const struct sockaddr_in *) a;
904 b4 = (const struct sockaddr_in *) b;
905 return !memcmp(&a4->sin_addr, &b4->sin_addr,
906 sizeof(a4->sin_addr));
909 a6 = (const struct sockaddr_in6 *) a;
910 b6 = (const struct sockaddr_in6 *) b;
911 return !memcmp(&a6->sin6_addr, &b6->sin6_addr,
912 sizeof(a6->sin6_addr));
920 * Check for idle hosts and stop monitoring them. We could also free
921 * the host structure here, possibly after a larger timeout but that
922 * would require some care to avoid races with
923 * e.g. nlm_host_lock_count_sysctl.
928 struct nlm_host *host;
930 mtx_assert(&nlm_global_lock, MA_OWNED);
932 if (time_uptime <= nlm_next_idle_check)
935 nlm_next_idle_check = time_uptime + NLM_IDLE_PERIOD;
937 TAILQ_FOREACH(host, &nlm_hosts, nh_link) {
938 if (host->nh_monstate == NLM_MONITORED
939 && time_uptime > host->nh_idle_timeout) {
940 mtx_unlock(&nlm_global_lock);
941 if (lf_countlocks(host->nh_sysid) > 0
942 || lf_countlocks(NLM_SYSID_CLIENT
944 host->nh_idle_timeout =
945 time_uptime + NLM_IDLE_TIMEOUT;
946 mtx_lock(&nlm_global_lock);
949 nlm_host_unmonitor(host);
950 mtx_lock(&nlm_global_lock);
956 * Search for an existing NLM host that matches the given name
957 * (typically the caller_name element of an nlm4_lock). If none is
958 * found, create a new host. If 'addr' is non-NULL, record the remote
959 * address of the host so that we can call it back for async
960 * responses. If 'vers' is greater than zero then record the NLM
961 * program version to use to communicate with this client.
964 nlm_find_host_by_name(const char *name, const struct sockaddr *addr,
967 struct nlm_host *host;
969 mtx_lock(&nlm_global_lock);
972 * The remote host is determined by caller_name.
974 TAILQ_FOREACH(host, &nlm_hosts, nh_link) {
975 if (!strcmp(host->nh_caller_name, name))
980 host = nlm_create_host(name);
982 mtx_unlock(&nlm_global_lock);
986 refcount_acquire(&host->nh_refs);
988 host->nh_idle_timeout = time_uptime + NLM_IDLE_TIMEOUT;
991 * If we have an address for the host, record it so that we
992 * can send async replies etc.
996 KASSERT(addr->sa_len < sizeof(struct sockaddr_storage),
997 ("Strange remote transport address length"));
1000 * If we have seen an address before and we currently
1001 * have an RPC client handle, make sure the address is
1002 * the same, otherwise discard the client handle.
1004 if (host->nh_addr.ss_len && host->nh_srvrpc.nr_client) {
1005 if (!nlm_compare_addr(
1006 (struct sockaddr *) &host->nh_addr,
1008 || host->nh_vers != vers) {
1010 mtx_lock(&host->nh_lock);
1011 client = host->nh_srvrpc.nr_client;
1012 host->nh_srvrpc.nr_client = NULL;
1013 mtx_unlock(&host->nh_lock);
1015 CLNT_RELEASE(client);
1019 memcpy(&host->nh_addr, addr, addr->sa_len);
1020 host->nh_vers = vers;
1025 mtx_unlock(&nlm_global_lock);
1031 * Search for an existing NLM host that matches the given remote
1032 * address. If none is found, create a new host with the requested
1033 * address and remember 'vers' as the NLM protocol version to use for
1037 nlm_find_host_by_addr(const struct sockaddr *addr, int vers)
1040 * Fake up a name using inet_ntop. This buffer is
1041 * large enough for an IPv6 address.
1043 char tmp[sizeof "ffff:ffff:ffff:ffff:ffff:ffff:255.255.255.255"];
1044 struct nlm_host *host;
1046 switch (addr->sa_family) {
1049 &((const struct sockaddr_in *) addr)->sin_addr,
1055 &((const struct sockaddr_in6 *) addr)->sin6_addr,
1060 strlcpy(tmp, "<unknown>", sizeof(tmp));
1064 mtx_lock(&nlm_global_lock);
1067 * The remote host is determined by caller_name.
1069 TAILQ_FOREACH(host, &nlm_hosts, nh_link) {
1070 if (nlm_compare_addr(addr,
1071 (const struct sockaddr *) &host->nh_addr))
1076 host = nlm_create_host(tmp);
1078 mtx_unlock(&nlm_global_lock);
1081 memcpy(&host->nh_addr, addr, addr->sa_len);
1082 host->nh_vers = vers;
1084 refcount_acquire(&host->nh_refs);
1086 host->nh_idle_timeout = time_uptime + NLM_IDLE_TIMEOUT;
1090 mtx_unlock(&nlm_global_lock);
1096 * Find the NLM host that matches the value of 'sysid'. If none
1097 * exists, return NULL.
1099 static struct nlm_host *
1100 nlm_find_host_by_sysid(int sysid)
1102 struct nlm_host *host;
1104 TAILQ_FOREACH(host, &nlm_hosts, nh_link) {
1105 if (host->nh_sysid == sysid) {
1106 refcount_acquire(&host->nh_refs);
1114 void nlm_host_release(struct nlm_host *host)
1116 if (refcount_release(&host->nh_refs)) {
1120 nlm_host_destroy(host);
1125 * Unregister this NLM host with the local NSM due to idleness.
1128 nlm_host_unmonitor(struct nlm_host *host)
1132 struct timeval timo;
1133 enum clnt_stat stat;
1135 NLM_DEBUG(1, "NLM: unmonitoring %s (sysid %d)\n",
1136 host->nh_caller_name, host->nh_sysid);
1139 * We put our assigned system ID value in the priv field to
1140 * make it simpler to find the host if we are notified of a
1143 smmonid.mon_name = host->nh_caller_name;
1144 smmonid.my_id.my_name = "localhost";
1145 smmonid.my_id.my_prog = NLM_PROG;
1146 smmonid.my_id.my_vers = NLM_SM;
1147 smmonid.my_id.my_proc = NLM_SM_NOTIFY;
1151 stat = CLNT_CALL(nlm_nsm, SM_UNMON,
1152 (xdrproc_t) xdr_mon, &smmonid,
1153 (xdrproc_t) xdr_sm_stat, &smstat, timo);
1155 if (stat != RPC_SUCCESS) {
1156 NLM_ERR("Failed to contact local NSM - rpc error %d\n", stat);
1159 if (smstat.res_stat == stat_fail) {
1160 NLM_ERR("Local NSM refuses to unmonitor %s\n",
1161 host->nh_caller_name);
1165 host->nh_monstate = NLM_UNMONITORED;
1169 * Register this NLM host with the local NSM so that we can be
1170 * notified if it reboots.
1173 nlm_host_monitor(struct nlm_host *host, int state)
1177 struct timeval timo;
1178 enum clnt_stat stat;
1180 if (state && !host->nh_state) {
1182 * This is the first time we have seen an NSM state
1183 * value for this host. We record it here to help
1184 * detect host reboots.
1186 host->nh_state = state;
1187 NLM_DEBUG(1, "NLM: host %s (sysid %d) has NSM state %d\n",
1188 host->nh_caller_name, host->nh_sysid, state);
1191 mtx_lock(&host->nh_lock);
1192 if (host->nh_monstate != NLM_UNMONITORED) {
1193 mtx_unlock(&host->nh_lock);
1196 host->nh_monstate = NLM_MONITORED;
1197 mtx_unlock(&host->nh_lock);
1199 NLM_DEBUG(1, "NLM: monitoring %s (sysid %d)\n",
1200 host->nh_caller_name, host->nh_sysid);
1203 * We put our assigned system ID value in the priv field to
1204 * make it simpler to find the host if we are notified of a
1207 smmon.mon_id.mon_name = host->nh_caller_name;
1208 smmon.mon_id.my_id.my_name = "localhost";
1209 smmon.mon_id.my_id.my_prog = NLM_PROG;
1210 smmon.mon_id.my_id.my_vers = NLM_SM;
1211 smmon.mon_id.my_id.my_proc = NLM_SM_NOTIFY;
1212 memcpy(smmon.priv, &host->nh_sysid, sizeof(host->nh_sysid));
1216 stat = CLNT_CALL(nlm_nsm, SM_MON,
1217 (xdrproc_t) xdr_mon, &smmon,
1218 (xdrproc_t) xdr_sm_stat, &smstat, timo);
1220 if (stat != RPC_SUCCESS) {
1221 NLM_ERR("Failed to contact local NSM - rpc error %d\n", stat);
1224 if (smstat.res_stat == stat_fail) {
1225 NLM_ERR("Local NSM refuses to monitor %s\n",
1226 host->nh_caller_name);
1227 mtx_lock(&host->nh_lock);
1228 host->nh_monstate = NLM_MONITOR_FAILED;
1229 mtx_unlock(&host->nh_lock);
1233 host->nh_monstate = NLM_MONITORED;
1237 * Return an RPC client handle that can be used to talk to the NLM
1238 * running on the given host.
1241 nlm_host_get_rpc(struct nlm_host *host, bool_t isserver)
1243 struct nlm_rpc *rpc;
1246 mtx_lock(&host->nh_lock);
1249 rpc = &host->nh_srvrpc;
1251 rpc = &host->nh_clntrpc;
1254 * We can't hold onto RPC handles for too long - the async
1255 * call/reply protocol used by some NLM clients makes it hard
1256 * to tell when they change port numbers (e.g. after a
1257 * reboot). Note that if a client reboots while it isn't
1258 * holding any locks, it won't bother to notify us. We
1259 * expire the RPC handles after two minutes.
1261 if (rpc->nr_client && time_uptime > rpc->nr_create_time + 2*60) {
1262 client = rpc->nr_client;
1263 rpc->nr_client = NULL;
1264 mtx_unlock(&host->nh_lock);
1265 CLNT_RELEASE(client);
1266 mtx_lock(&host->nh_lock);
1269 if (!rpc->nr_client) {
1270 mtx_unlock(&host->nh_lock);
1271 client = nlm_get_rpc((struct sockaddr *)&host->nh_addr,
1272 NLM_PROG, host->nh_vers);
1273 mtx_lock(&host->nh_lock);
1276 if (rpc->nr_client) {
1277 mtx_unlock(&host->nh_lock);
1278 CLNT_DESTROY(client);
1279 mtx_lock(&host->nh_lock);
1281 rpc->nr_client = client;
1282 rpc->nr_create_time = time_uptime;
1287 client = rpc->nr_client;
1289 CLNT_ACQUIRE(client);
1290 mtx_unlock(&host->nh_lock);
1296 int nlm_host_get_sysid(struct nlm_host *host)
1299 return (host->nh_sysid);
1303 nlm_host_get_state(struct nlm_host *host)
1306 return (host->nh_state);
1310 nlm_register_wait_lock(struct nlm4_lock *lock, struct vnode *vp)
1312 struct nlm_waiting_lock *nw;
1314 nw = malloc(sizeof(struct nlm_waiting_lock), M_NLM, M_WAITOK);
1315 nw->nw_lock = *lock;
1316 memcpy(&nw->nw_fh.fh_bytes, nw->nw_lock.fh.n_bytes,
1317 nw->nw_lock.fh.n_len);
1318 nw->nw_lock.fh.n_bytes = nw->nw_fh.fh_bytes;
1319 nw->nw_waiting = TRUE;
1321 mtx_lock(&nlm_global_lock);
1322 TAILQ_INSERT_TAIL(&nlm_waiting_locks, nw, nw_link);
1323 mtx_unlock(&nlm_global_lock);
1329 nlm_deregister_wait_lock(void *handle)
1331 struct nlm_waiting_lock *nw = handle;
1333 mtx_lock(&nlm_global_lock);
1334 TAILQ_REMOVE(&nlm_waiting_locks, nw, nw_link);
1335 mtx_unlock(&nlm_global_lock);
1341 nlm_wait_lock(void *handle, int timo)
1343 struct nlm_waiting_lock *nw = handle;
1344 int error, stops_deferred;
1347 * If the granted message arrived before we got here,
1348 * nw->nw_waiting will be FALSE - in that case, don't sleep.
1350 mtx_lock(&nlm_global_lock);
1352 if (nw->nw_waiting) {
1353 stops_deferred = sigdeferstop(SIGDEFERSTOP_ERESTART);
1354 error = msleep(nw, &nlm_global_lock, PCATCH, "nlmlock", timo);
1355 sigallowstop(stops_deferred);
1357 TAILQ_REMOVE(&nlm_waiting_locks, nw, nw_link);
1360 * The granted message may arrive after the
1361 * interrupt/timeout but before we manage to lock the
1362 * mutex. Detect this by examining nw_lock.
1364 if (!nw->nw_waiting)
1368 * If nlm_cancel_wait is called, then error will be
1369 * zero but nw_waiting will still be TRUE. We
1370 * translate this into EINTR.
1375 mtx_unlock(&nlm_global_lock);
1383 nlm_cancel_wait(struct vnode *vp)
1385 struct nlm_waiting_lock *nw;
1387 mtx_lock(&nlm_global_lock);
1388 TAILQ_FOREACH(nw, &nlm_waiting_locks, nw_link) {
1389 if (nw->nw_vp == vp) {
1393 mtx_unlock(&nlm_global_lock);
1397 /**********************************************************************/
1400 * Syscall interface with userland.
1403 extern void nlm_prog_0(struct svc_req *rqstp, SVCXPRT *transp);
1404 extern void nlm_prog_1(struct svc_req *rqstp, SVCXPRT *transp);
1405 extern void nlm_prog_3(struct svc_req *rqstp, SVCXPRT *transp);
1406 extern void nlm_prog_4(struct svc_req *rqstp, SVCXPRT *transp);
1409 nlm_register_services(SVCPOOL *pool, int addr_count, char **addrs)
1411 static rpcvers_t versions[] = {
1412 NLM_SM, NLM_VERS, NLM_VERSX, NLM_VERS4
1414 static void (*dispatchers[])(struct svc_req *, SVCXPRT *) = {
1415 nlm_prog_0, nlm_prog_1, nlm_prog_3, nlm_prog_4
1421 struct netconfig *nconf;
1425 NLM_ERR("NLM: no service addresses given - can't start server");
1429 if (addr_count < 0 || addr_count > 256 ) {
1430 NLM_ERR("NLM: too many service addresses (%d) given, "
1431 "max 256 - can't start server\n", addr_count);
1435 xprts = malloc(addr_count * sizeof(SVCXPRT *), M_NLM, M_WAITOK|M_ZERO);
1436 for (i = 0; i < nitems(versions); i++) {
1437 for (j = 0; j < addr_count; j++) {
1439 * Create transports for the first version and
1440 * then just register everything else to the
1446 error = copyin(&addrs[2*j], &up,
1450 error = copyinstr(up, netid, sizeof(netid),
1454 error = copyin(&addrs[2*j+1], &up,
1458 error = copyinstr(up, uaddr, sizeof(uaddr),
1462 nconf = getnetconfigent(netid);
1464 NLM_ERR("Can't lookup netid %s\n",
1469 xprts[j] = svc_tp_create(pool, dispatchers[i],
1470 NLM_PROG, versions[i], uaddr, nconf);
1472 NLM_ERR("NLM: unable to create "
1473 "(NLM_PROG, %d).\n", versions[i]);
1477 freenetconfigent(nconf);
1479 nconf = getnetconfigent(xprts[j]->xp_netid);
1480 rpcb_unset(NLM_PROG, versions[i], nconf);
1481 if (!svc_reg(xprts[j], NLM_PROG, versions[i],
1482 dispatchers[i], nconf)) {
1483 NLM_ERR("NLM: can't register "
1484 "(NLM_PROG, %d)\n", versions[i]);
1493 for (j = 0; j < addr_count; j++) {
1495 SVC_RELEASE(xprts[j]);
1502 * Main server entry point. Contacts the local NSM to get its current
1503 * state and send SM_UNMON_ALL. Registers the NLM services and then
1504 * services requests. Does not return until the server is interrupted
1508 nlm_server_main(int addr_count, char **addrs)
1510 struct thread *td = curthread;
1512 SVCPOOL *pool = NULL;
1516 struct sockaddr_in6 sin6;
1518 struct sockaddr_in sin;
1521 struct timeval timo;
1522 enum clnt_stat stat;
1523 struct nlm_host *host, *nhost;
1524 struct nlm_waiting_lock *nw;
1525 vop_advlock_t *old_nfs_advlock;
1526 vop_reclaim_t *old_nfs_reclaim;
1528 if (nlm_is_running != 0) {
1529 NLM_ERR("NLM: can't start server - "
1530 "it appears to be running already\n");
1534 if (nlm_socket == NULL) {
1535 memset(&opt, 0, sizeof(opt));
1537 error = socreate(AF_INET, &nlm_socket, SOCK_DGRAM, 0,
1540 NLM_ERR("NLM: can't create IPv4 socket - error %d\n",
1544 opt.sopt_dir = SOPT_SET;
1545 opt.sopt_level = IPPROTO_IP;
1546 opt.sopt_name = IP_PORTRANGE;
1547 portlow = IP_PORTRANGE_LOW;
1548 opt.sopt_val = &portlow;
1549 opt.sopt_valsize = sizeof(portlow);
1550 sosetopt(nlm_socket, &opt);
1554 error = socreate(AF_INET6, &nlm_socket6, SOCK_DGRAM, 0,
1557 NLM_ERR("NLM: can't create IPv6 socket - error %d\n",
1559 soclose(nlm_socket);
1563 opt.sopt_dir = SOPT_SET;
1564 opt.sopt_level = IPPROTO_IPV6;
1565 opt.sopt_name = IPV6_PORTRANGE;
1566 portlow = IPV6_PORTRANGE_LOW;
1567 opt.sopt_val = &portlow;
1568 opt.sopt_valsize = sizeof(portlow);
1569 sosetopt(nlm_socket6, &opt);
1573 nlm_auth = authunix_create(curthread->td_ucred);
1576 memset(&sin6, 0, sizeof(sin6));
1577 sin6.sin6_len = sizeof(sin6);
1578 sin6.sin6_family = AF_INET6;
1579 sin6.sin6_addr = in6addr_loopback;
1580 nlm_nsm = nlm_get_rpc((struct sockaddr *) &sin6, SM_PROG, SM_VERS);
1583 memset(&sin, 0, sizeof(sin));
1584 sin.sin_len = sizeof(sin);
1585 sin.sin_family = AF_INET;
1586 sin.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
1587 nlm_nsm = nlm_get_rpc((struct sockaddr *) &sin, SM_PROG,
1594 NLM_ERR("Can't start NLM - unable to contact NSM\n");
1599 pool = svcpool_create("NLM", NULL);
1601 error = nlm_register_services(pool, addr_count, addrs);
1605 memset(&id, 0, sizeof(id));
1606 id.my_name = "NFS NLM";
1610 stat = CLNT_CALL(nlm_nsm, SM_UNMON_ALL,
1611 (xdrproc_t) xdr_my_id, &id,
1612 (xdrproc_t) xdr_sm_stat, &smstat, timo);
1614 if (stat != RPC_SUCCESS) {
1617 CLNT_GETERR(nlm_nsm, &err);
1618 NLM_ERR("NLM: unexpected error contacting NSM, "
1619 "stat=%d, errno=%d\n", stat, err.re_errno);
1625 NLM_DEBUG(1, "NLM: local NSM state is %d\n", smstat.state);
1626 nlm_nsm_state = smstat.state;
1628 old_nfs_advlock = nfs_advlock_p;
1629 nfs_advlock_p = nlm_advlock;
1630 old_nfs_reclaim = nfs_reclaim_p;
1631 nfs_reclaim_p = nlm_reclaim;
1636 nfs_advlock_p = old_nfs_advlock;
1637 nfs_reclaim_p = old_nfs_reclaim;
1642 svcpool_destroy(pool);
1645 * We are finished communicating with the NSM.
1648 CLNT_RELEASE(nlm_nsm);
1653 * Trash all the existing state so that if the server
1654 * restarts, it gets a clean slate. This is complicated by the
1655 * possibility that there may be other threads trying to make
1656 * client locking requests.
1658 * First we fake a client reboot notification which will
1659 * cancel any pending async locks and purge remote lock state
1660 * from the local lock manager. We release the reference from
1661 * nlm_hosts to the host (which may remove it from the list
1662 * and free it). After this phase, the only entries in the
1663 * nlm_host list should be from other threads performing
1664 * client lock requests.
1666 mtx_lock(&nlm_global_lock);
1667 TAILQ_FOREACH(nw, &nlm_waiting_locks, nw_link) {
1670 TAILQ_FOREACH_SAFE(host, &nlm_hosts, nh_link, nhost) {
1671 mtx_unlock(&nlm_global_lock);
1672 nlm_host_notify(host, 0);
1673 nlm_host_release(host);
1674 mtx_lock(&nlm_global_lock);
1676 mtx_unlock(&nlm_global_lock);
1678 AUTH_DESTROY(nlm_auth);
1684 sys_nlm_syscall(struct thread *td, struct nlm_syscall_args *uap)
1688 #if __FreeBSD_version >= 700000
1689 error = priv_check(td, PRIV_NFS_LOCKD);
1696 nlm_debug_level = uap->debug_level;
1697 nlm_grace_threshold = time_uptime + uap->grace_period;
1698 nlm_next_idle_check = time_uptime + NLM_IDLE_PERIOD;
1700 return nlm_server_main(uap->addr_count, uap->addrs);
1703 /**********************************************************************/
1706 * NLM implementation details, called from the RPC stubs.
1711 nlm_sm_notify(struct nlm_sm_status *argp)
1714 struct nlm_host *host;
1716 NLM_DEBUG(3, "nlm_sm_notify(): mon_name = %s\n", argp->mon_name);
1717 memcpy(&sysid, &argp->priv, sizeof(sysid));
1718 host = nlm_find_host_by_sysid(sysid);
1720 nlm_host_notify(host, argp->state);
1721 nlm_host_release(host);
1726 nlm_convert_to_fhandle_t(fhandle_t *fhp, struct netobj *p)
1728 memcpy(fhp, p->n_bytes, sizeof(fhandle_t));
1732 struct mount *vs_mp;
1733 struct vnode *vs_vp;
1738 nlm_get_vfs_state(struct nlm_host *host, struct svc_req *rqstp,
1739 fhandle_t *fhp, struct vfs_state *vs, accmode_t accmode)
1742 struct ucred *cred = NULL, *credanon = NULL;
1744 memset(vs, 0, sizeof(*vs));
1746 vs->vs_mp = vfs_getvfs(&fhp->fh_fsid);
1751 /* accmode == 0 means don't check, since it is an unlock. */
1753 error = VFS_CHECKEXP(vs->vs_mp,
1754 (struct sockaddr *)&host->nh_addr, &exflags, &credanon,
1759 if (exflags & MNT_EXRDONLY ||
1760 (vs->vs_mp->mnt_flag & MNT_RDONLY)) {
1766 error = VFS_FHTOVP(vs->vs_mp, &fhp->fh_fid, LK_EXCLUSIVE, &vs->vs_vp);
1769 vs->vs_vnlocked = TRUE;
1772 if (!svc_getcred(rqstp, &cred, NULL)) {
1776 if (cred->cr_uid == 0 || (exflags & MNT_EXPORTANON)) {
1785 error = VOP_ACCESS(vs->vs_vp, accmode, cred, curthread);
1787 * If this failed and accmode != VWRITE, try again with
1788 * VWRITE to maintain backwards compatibility with the
1789 * old code that always used VWRITE.
1791 if (error != 0 && accmode != VWRITE)
1792 error = VOP_ACCESS(vs->vs_vp, VWRITE, cred, curthread);
1797 #if __FreeBSD_version < 800011
1798 VOP_UNLOCK(vs->vs_vp, 0, curthread);
1800 VOP_UNLOCK(vs->vs_vp, 0);
1802 vs->vs_vnlocked = FALSE;
1814 nlm_release_vfs_state(struct vfs_state *vs)
1818 if (vs->vs_vnlocked)
1828 nlm_convert_error(int error)
1831 if (error == ESTALE)
1832 return nlm4_stale_fh;
1833 else if (error == EROFS)
1840 nlm_do_test(nlm4_testargs *argp, nlm4_testres *result, struct svc_req *rqstp,
1844 struct vfs_state vs;
1845 struct nlm_host *host, *bhost;
1850 memset(result, 0, sizeof(*result));
1851 memset(&vs, 0, sizeof(vs));
1853 host = nlm_find_host_by_name(argp->alock.caller_name,
1854 svc_getrpccaller(rqstp), rqstp->rq_vers);
1856 result->stat.stat = nlm4_denied_nolocks;
1860 NLM_DEBUG(3, "nlm_do_test(): caller_name = %s (sysid = %d)\n",
1861 host->nh_caller_name, host->nh_sysid);
1863 nlm_check_expired_locks(host);
1864 sysid = host->nh_sysid;
1866 nlm_convert_to_fhandle_t(&fh, &argp->alock.fh);
1867 nlm_copy_netobj(&result->cookie, &argp->cookie, M_RPC);
1869 if (time_uptime < nlm_grace_threshold) {
1870 result->stat.stat = nlm4_denied_grace_period;
1874 accmode = argp->exclusive ? VWRITE : VREAD;
1875 error = nlm_get_vfs_state(host, rqstp, &fh, &vs, accmode);
1877 result->stat.stat = nlm_convert_error(error);
1881 fl.l_start = argp->alock.l_offset;
1882 fl.l_len = argp->alock.l_len;
1883 fl.l_pid = argp->alock.svid;
1885 fl.l_whence = SEEK_SET;
1886 if (argp->exclusive)
1887 fl.l_type = F_WRLCK;
1889 fl.l_type = F_RDLCK;
1890 error = VOP_ADVLOCK(vs.vs_vp, NULL, F_GETLK, &fl, F_REMOTE);
1892 result->stat.stat = nlm4_failed;
1896 if (fl.l_type == F_UNLCK) {
1897 result->stat.stat = nlm4_granted;
1899 result->stat.stat = nlm4_denied;
1900 result->stat.nlm4_testrply_u.holder.exclusive =
1901 (fl.l_type == F_WRLCK);
1902 result->stat.nlm4_testrply_u.holder.svid = fl.l_pid;
1903 bhost = nlm_find_host_by_sysid(fl.l_sysid);
1906 * We don't have any useful way of recording
1907 * the value of oh used in the original lock
1908 * request. Ideally, the test reply would have
1909 * a space for the owning host's name allowing
1910 * our caller's NLM to keep track.
1912 * As far as I can see, Solaris uses an eight
1913 * byte structure for oh which contains a four
1914 * byte pid encoded in local byte order and
1915 * the first four bytes of the host
1916 * name. Linux uses a variable length string
1917 * 'pid@hostname' in ascii but doesn't even
1918 * return that in test replies.
1920 * For the moment, return nothing in oh
1921 * (already zero'ed above).
1923 nlm_host_release(bhost);
1925 result->stat.nlm4_testrply_u.holder.l_offset = fl.l_start;
1926 result->stat.nlm4_testrply_u.holder.l_len = fl.l_len;
1930 nlm_release_vfs_state(&vs);
1932 *rpcp = nlm_host_get_rpc(host, TRUE);
1933 nlm_host_release(host);
1938 nlm_do_lock(nlm4_lockargs *argp, nlm4_res *result, struct svc_req *rqstp,
1939 bool_t monitor, CLIENT **rpcp)
1942 struct vfs_state vs;
1943 struct nlm_host *host;
1948 memset(result, 0, sizeof(*result));
1949 memset(&vs, 0, sizeof(vs));
1951 host = nlm_find_host_by_name(argp->alock.caller_name,
1952 svc_getrpccaller(rqstp), rqstp->rq_vers);
1954 result->stat.stat = nlm4_denied_nolocks;
1958 NLM_DEBUG(3, "nlm_do_lock(): caller_name = %s (sysid = %d)\n",
1959 host->nh_caller_name, host->nh_sysid);
1961 if (monitor && host->nh_state && argp->state
1962 && host->nh_state != argp->state) {
1964 * The host rebooted without telling us. Trash its
1967 nlm_host_notify(host, argp->state);
1970 nlm_check_expired_locks(host);
1971 sysid = host->nh_sysid;
1973 nlm_convert_to_fhandle_t(&fh, &argp->alock.fh);
1974 nlm_copy_netobj(&result->cookie, &argp->cookie, M_RPC);
1976 if (time_uptime < nlm_grace_threshold && !argp->reclaim) {
1977 result->stat.stat = nlm4_denied_grace_period;
1981 accmode = argp->exclusive ? VWRITE : VREAD;
1982 error = nlm_get_vfs_state(host, rqstp, &fh, &vs, accmode);
1984 result->stat.stat = nlm_convert_error(error);
1988 fl.l_start = argp->alock.l_offset;
1989 fl.l_len = argp->alock.l_len;
1990 fl.l_pid = argp->alock.svid;
1992 fl.l_whence = SEEK_SET;
1993 if (argp->exclusive)
1994 fl.l_type = F_WRLCK;
1996 fl.l_type = F_RDLCK;
1998 struct nlm_async_lock *af;
2000 struct nlm_grantcookie cookie;
2003 * First, make sure we can contact the host's NLM.
2005 client = nlm_host_get_rpc(host, TRUE);
2007 result->stat.stat = nlm4_failed;
2012 * First we need to check and see if there is an
2013 * existing blocked lock that matches. This could be a
2014 * badly behaved client or an RPC re-send. If we find
2015 * one, just return nlm4_blocked.
2017 mtx_lock(&host->nh_lock);
2018 TAILQ_FOREACH(af, &host->nh_pending, af_link) {
2019 if (af->af_fl.l_start == fl.l_start
2020 && af->af_fl.l_len == fl.l_len
2021 && af->af_fl.l_pid == fl.l_pid
2022 && af->af_fl.l_type == fl.l_type) {
2027 cookie.ng_sysid = host->nh_sysid;
2028 cookie.ng_cookie = host->nh_grantcookie++;
2030 mtx_unlock(&host->nh_lock);
2032 CLNT_RELEASE(client);
2033 result->stat.stat = nlm4_blocked;
2037 af = malloc(sizeof(struct nlm_async_lock), M_NLM,
2039 TASK_INIT(&af->af_task, 0, nlm_lock_callback, af);
2040 af->af_vp = vs.vs_vp;
2043 af->af_rpc = client;
2045 * We use M_RPC here so that we can xdr_free the thing
2048 nlm_make_netobj(&af->af_granted.cookie,
2049 (caddr_t)&cookie, sizeof(cookie), M_RPC);
2050 af->af_granted.exclusive = argp->exclusive;
2051 af->af_granted.alock.caller_name =
2052 strdup(argp->alock.caller_name, M_RPC);
2053 nlm_copy_netobj(&af->af_granted.alock.fh,
2054 &argp->alock.fh, M_RPC);
2055 nlm_copy_netobj(&af->af_granted.alock.oh,
2056 &argp->alock.oh, M_RPC);
2057 af->af_granted.alock.svid = argp->alock.svid;
2058 af->af_granted.alock.l_offset = argp->alock.l_offset;
2059 af->af_granted.alock.l_len = argp->alock.l_len;
2062 * Put the entry on the pending list before calling
2063 * VOP_ADVLOCKASYNC. We do this in case the lock
2064 * request was blocked (returning EINPROGRESS) but
2065 * then granted before we manage to run again. The
2066 * client may receive the granted message before we
2067 * send our blocked reply but thats their problem.
2069 mtx_lock(&host->nh_lock);
2070 TAILQ_INSERT_TAIL(&host->nh_pending, af, af_link);
2071 mtx_unlock(&host->nh_lock);
2073 error = VOP_ADVLOCKASYNC(vs.vs_vp, NULL, F_SETLK, &fl, F_REMOTE,
2074 &af->af_task, &af->af_cookie);
2077 * If the lock completed synchronously, just free the
2078 * tracking structure now.
2080 if (error != EINPROGRESS) {
2081 CLNT_RELEASE(af->af_rpc);
2082 mtx_lock(&host->nh_lock);
2083 TAILQ_REMOVE(&host->nh_pending, af, af_link);
2084 mtx_unlock(&host->nh_lock);
2085 xdr_free((xdrproc_t) xdr_nlm4_testargs,
2089 NLM_DEBUG(2, "NLM: pending async lock %p for %s "
2090 "(sysid %d)\n", af, host->nh_caller_name, sysid);
2092 * Don't vrele the vnode just yet - this must
2093 * wait until either the async callback
2094 * happens or the lock is cancelled.
2099 error = VOP_ADVLOCK(vs.vs_vp, NULL, F_SETLK, &fl, F_REMOTE);
2103 if (error == EINPROGRESS) {
2104 result->stat.stat = nlm4_blocked;
2105 } else if (error == EDEADLK) {
2106 result->stat.stat = nlm4_deadlck;
2107 } else if (error == EAGAIN) {
2108 result->stat.stat = nlm4_denied;
2110 result->stat.stat = nlm4_failed;
2114 nlm_host_monitor(host, argp->state);
2115 result->stat.stat = nlm4_granted;
2119 nlm_release_vfs_state(&vs);
2121 *rpcp = nlm_host_get_rpc(host, TRUE);
2122 nlm_host_release(host);
2127 nlm_do_cancel(nlm4_cancargs *argp, nlm4_res *result, struct svc_req *rqstp,
2131 struct vfs_state vs;
2132 struct nlm_host *host;
2135 struct nlm_async_lock *af;
2137 memset(result, 0, sizeof(*result));
2138 memset(&vs, 0, sizeof(vs));
2140 host = nlm_find_host_by_name(argp->alock.caller_name,
2141 svc_getrpccaller(rqstp), rqstp->rq_vers);
2143 result->stat.stat = nlm4_denied_nolocks;
2147 NLM_DEBUG(3, "nlm_do_cancel(): caller_name = %s (sysid = %d)\n",
2148 host->nh_caller_name, host->nh_sysid);
2150 nlm_check_expired_locks(host);
2151 sysid = host->nh_sysid;
2153 nlm_convert_to_fhandle_t(&fh, &argp->alock.fh);
2154 nlm_copy_netobj(&result->cookie, &argp->cookie, M_RPC);
2156 if (time_uptime < nlm_grace_threshold) {
2157 result->stat.stat = nlm4_denied_grace_period;
2161 error = nlm_get_vfs_state(host, rqstp, &fh, &vs, (accmode_t)0);
2163 result->stat.stat = nlm_convert_error(error);
2167 fl.l_start = argp->alock.l_offset;
2168 fl.l_len = argp->alock.l_len;
2169 fl.l_pid = argp->alock.svid;
2171 fl.l_whence = SEEK_SET;
2172 if (argp->exclusive)
2173 fl.l_type = F_WRLCK;
2175 fl.l_type = F_RDLCK;
2178 * First we need to try and find the async lock request - if
2179 * there isn't one, we give up and return nlm4_denied.
2181 mtx_lock(&host->nh_lock);
2183 TAILQ_FOREACH(af, &host->nh_pending, af_link) {
2184 if (af->af_fl.l_start == fl.l_start
2185 && af->af_fl.l_len == fl.l_len
2186 && af->af_fl.l_pid == fl.l_pid
2187 && af->af_fl.l_type == fl.l_type) {
2193 mtx_unlock(&host->nh_lock);
2194 result->stat.stat = nlm4_denied;
2198 error = nlm_cancel_async_lock(af);
2201 result->stat.stat = nlm4_denied;
2203 result->stat.stat = nlm4_granted;
2206 mtx_unlock(&host->nh_lock);
2209 nlm_release_vfs_state(&vs);
2211 *rpcp = nlm_host_get_rpc(host, TRUE);
2212 nlm_host_release(host);
2217 nlm_do_unlock(nlm4_unlockargs *argp, nlm4_res *result, struct svc_req *rqstp,
2221 struct vfs_state vs;
2222 struct nlm_host *host;
2226 memset(result, 0, sizeof(*result));
2227 memset(&vs, 0, sizeof(vs));
2229 host = nlm_find_host_by_name(argp->alock.caller_name,
2230 svc_getrpccaller(rqstp), rqstp->rq_vers);
2232 result->stat.stat = nlm4_denied_nolocks;
2236 NLM_DEBUG(3, "nlm_do_unlock(): caller_name = %s (sysid = %d)\n",
2237 host->nh_caller_name, host->nh_sysid);
2239 nlm_check_expired_locks(host);
2240 sysid = host->nh_sysid;
2242 nlm_convert_to_fhandle_t(&fh, &argp->alock.fh);
2243 nlm_copy_netobj(&result->cookie, &argp->cookie, M_RPC);
2245 if (time_uptime < nlm_grace_threshold) {
2246 result->stat.stat = nlm4_denied_grace_period;
2250 error = nlm_get_vfs_state(host, rqstp, &fh, &vs, (accmode_t)0);
2252 result->stat.stat = nlm_convert_error(error);
2256 fl.l_start = argp->alock.l_offset;
2257 fl.l_len = argp->alock.l_len;
2258 fl.l_pid = argp->alock.svid;
2260 fl.l_whence = SEEK_SET;
2261 fl.l_type = F_UNLCK;
2262 error = VOP_ADVLOCK(vs.vs_vp, NULL, F_UNLCK, &fl, F_REMOTE);
2265 * Ignore the error - there is no result code for failure,
2266 * only for grace period.
2268 result->stat.stat = nlm4_granted;
2271 nlm_release_vfs_state(&vs);
2273 *rpcp = nlm_host_get_rpc(host, TRUE);
2274 nlm_host_release(host);
2279 nlm_do_granted(nlm4_testargs *argp, nlm4_res *result, struct svc_req *rqstp,
2283 struct nlm_host *host;
2284 struct nlm_waiting_lock *nw;
2286 memset(result, 0, sizeof(*result));
2288 host = nlm_find_host_by_addr(svc_getrpccaller(rqstp), rqstp->rq_vers);
2290 result->stat.stat = nlm4_denied_nolocks;
2294 nlm_copy_netobj(&result->cookie, &argp->cookie, M_RPC);
2295 result->stat.stat = nlm4_denied;
2296 KFAIL_POINT_CODE(DEBUG_FP, nlm_deny_grant, goto out);
2298 mtx_lock(&nlm_global_lock);
2299 TAILQ_FOREACH(nw, &nlm_waiting_locks, nw_link) {
2300 if (!nw->nw_waiting)
2302 if (argp->alock.svid == nw->nw_lock.svid
2303 && argp->alock.l_offset == nw->nw_lock.l_offset
2304 && argp->alock.l_len == nw->nw_lock.l_len
2305 && argp->alock.fh.n_len == nw->nw_lock.fh.n_len
2306 && !memcmp(argp->alock.fh.n_bytes, nw->nw_lock.fh.n_bytes,
2307 nw->nw_lock.fh.n_len)) {
2308 nw->nw_waiting = FALSE;
2310 result->stat.stat = nlm4_granted;
2314 mtx_unlock(&nlm_global_lock);
2318 *rpcp = nlm_host_get_rpc(host, TRUE);
2319 nlm_host_release(host);
2324 nlm_do_granted_res(nlm4_res *argp, struct svc_req *rqstp)
2326 struct nlm_host *host = NULL;
2327 struct nlm_async_lock *af = NULL;
2330 if (argp->cookie.n_len != sizeof(struct nlm_grantcookie)) {
2331 NLM_DEBUG(1, "NLM: bogus grant cookie");
2335 host = nlm_find_host_by_sysid(ng_sysid(&argp->cookie));
2337 NLM_DEBUG(1, "NLM: Unknown host rejected our grant");
2341 mtx_lock(&host->nh_lock);
2342 TAILQ_FOREACH(af, &host->nh_granted, af_link)
2343 if (ng_cookie(&argp->cookie) ==
2344 ng_cookie(&af->af_granted.cookie))
2347 TAILQ_REMOVE(&host->nh_granted, af, af_link);
2348 mtx_unlock(&host->nh_lock);
2351 NLM_DEBUG(1, "NLM: host %s (sysid %d) replied to our grant "
2352 "with unrecognized cookie %d:%d", host->nh_caller_name,
2353 host->nh_sysid, ng_sysid(&argp->cookie),
2354 ng_cookie(&argp->cookie));
2358 if (argp->stat.stat != nlm4_granted) {
2359 af->af_fl.l_type = F_UNLCK;
2360 error = VOP_ADVLOCK(af->af_vp, NULL, F_UNLCK, &af->af_fl, F_REMOTE);
2362 NLM_DEBUG(1, "NLM: host %s (sysid %d) rejected our grant "
2363 "and we failed to unlock (%d)", host->nh_caller_name,
2364 host->nh_sysid, error);
2368 NLM_DEBUG(5, "NLM: async lock %p rejected by host %s (sysid %d)",
2369 af, host->nh_caller_name, host->nh_sysid);
2371 NLM_DEBUG(5, "NLM: async lock %p accepted by host %s (sysid %d)",
2372 af, host->nh_caller_name, host->nh_sysid);
2377 nlm_free_async_lock(af);
2379 nlm_host_release(host);
2383 nlm_do_free_all(nlm4_notify *argp)
2385 struct nlm_host *host, *thost;
2387 TAILQ_FOREACH_SAFE(host, &nlm_hosts, nh_link, thost) {
2388 if (!strcmp(host->nh_caller_name, argp->name))
2389 nlm_host_notify(host, argp->state);
2394 * Kernel module glue
2397 nfslockd_modevent(module_t mod, int type, void *data)
2402 return (nlm_init());
2406 /* The NLM module cannot be safely unloaded. */
2409 return (EOPNOTSUPP);
2412 static moduledata_t nfslockd_mod = {
2417 DECLARE_MODULE(nfslockd, nfslockd_mod, SI_SUB_VFS, SI_ORDER_ANY);
2419 /* So that loader and kldload(2) can find us, wherever we are.. */
2420 MODULE_DEPEND(nfslockd, krpc, 1, 1, 1);
2421 MODULE_DEPEND(nfslockd, nfslock, 1, 1, 1);
2422 MODULE_VERSION(nfslockd, 1);