2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2008 Isilon Inc http://www.isilon.com/
5 * Authors: Doug Rabson <dfr@rabson.org>
6 * Developed with Red Inc: Alfred Perlstein <alfred@freebsd.org>
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 #include "opt_inet6.h"
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
35 #include <sys/param.h>
37 #include <sys/fcntl.h>
38 #include <sys/kernel.h>
39 #include <sys/kthread.h>
40 #include <sys/lockf.h>
41 #include <sys/malloc.h>
42 #include <sys/mount.h>
45 #include <sys/socket.h>
46 #include <sys/socketvar.h>
47 #include <sys/syscall.h>
48 #include <sys/sysctl.h>
49 #include <sys/sysent.h>
50 #include <sys/syslog.h>
51 #include <sys/sysproto.h>
52 #include <sys/systm.h>
53 #include <sys/taskqueue.h>
54 #include <sys/unistd.h>
55 #include <sys/vnode.h>
57 #include <nfs/nfsproto.h>
58 #include <nfs/nfs_lock.h>
60 #include <nlm/nlm_prot.h>
61 #include <nlm/sm_inter.h>
63 #include <rpc/rpc_com.h>
64 #include <rpc/rpcb_prot.h>
66 MALLOC_DEFINE(M_NLM, "NLM", "Network Lock Manager");
69 * If a host is inactive (and holds no locks) for this amount of
70 * seconds, we consider it idle and stop tracking it.
72 #define NLM_IDLE_TIMEOUT 30
75 * We check the host list for idle every few seconds.
77 #define NLM_IDLE_PERIOD 5
80 * We only look for GRANTED_RES messages for a little while.
82 #define NLM_EXPIRE_TIMEOUT 10
85 * Support for sysctl vfs.nlm.sysid
87 static SYSCTL_NODE(_vfs, OID_AUTO, nlm, CTLFLAG_RW | CTLFLAG_MPSAFE, NULL,
88 "Network Lock Manager");
89 static SYSCTL_NODE(_vfs_nlm, OID_AUTO, sysid,
90 CTLFLAG_RW | CTLFLAG_MPSAFE, NULL,
96 static struct syscall_helper_data nlm_syscalls[] = {
97 SYSCALL_INIT_HELPER(nlm_syscall),
102 * Debug level passed in from userland. We also support a sysctl hook
103 * so that it can be changed on a live system.
105 static int nlm_debug_level;
106 SYSCTL_INT(_debug, OID_AUTO, nlm_debug, CTLFLAG_RW, &nlm_debug_level, 0, "");
108 #define NLM_DEBUG(_level, args...) \
110 if (nlm_debug_level >= (_level)) \
111 log(LOG_DEBUG, args); \
113 #define NLM_ERR(args...) \
115 log(LOG_ERR, args); \
119 * Grace period handling. The value of nlm_grace_threshold is the
120 * value of time_uptime after which we are serving requests normally.
122 static time_t nlm_grace_threshold;
125 * We check for idle hosts if time_uptime is greater than
126 * nlm_next_idle_check,
128 static time_t nlm_next_idle_check;
131 * A flag to indicate the server is already running.
133 static int nlm_is_running;
136 * A socket to use for RPC - shared by all IPv4 RPC clients.
138 static struct socket *nlm_socket;
143 * A socket to use for RPC - shared by all IPv6 RPC clients.
145 static struct socket *nlm_socket6;
150 * An RPC client handle that can be used to communicate with the local
153 static CLIENT *nlm_nsm;
156 * An AUTH handle for the server's creds.
158 static AUTH *nlm_auth;
161 * A zero timeval for sending async RPC messages.
163 struct timeval nlm_zero_tv = { 0, 0 };
166 * The local NSM state number
171 * A lock to protect the host list and waiting lock list.
173 static struct mtx nlm_global_lock;
177 * (l) locked by nh_lock
178 * (s) only accessed via server RPC which is single threaded
179 * (g) locked by nlm_global_lock
180 * (c) const until freeing
181 * (a) modified using atomic ops
185 * A pending client-side lock request, stored on the nlm_waiting_locks
188 struct nlm_waiting_lock {
189 TAILQ_ENTRY(nlm_waiting_lock) nw_link; /* (g) */
190 bool_t nw_waiting; /* (g) */
191 nlm4_lock nw_lock; /* (c) */
192 union nfsfh nw_fh; /* (c) */
193 struct vnode *nw_vp; /* (c) */
195 TAILQ_HEAD(nlm_waiting_lock_list, nlm_waiting_lock);
197 struct nlm_waiting_lock_list nlm_waiting_locks; /* (g) */
200 * A pending server-side asynchronous lock request, stored on the
201 * nh_pending list of the NLM host.
203 struct nlm_async_lock {
204 TAILQ_ENTRY(nlm_async_lock) af_link; /* (l) host's list of locks */
205 struct task af_task; /* (c) async callback details */
206 void *af_cookie; /* (l) lock manager cancel token */
207 struct vnode *af_vp; /* (l) vnode to lock */
208 struct flock af_fl; /* (c) lock details */
209 struct nlm_host *af_host; /* (c) host which is locking */
210 CLIENT *af_rpc; /* (c) rpc client to send message */
211 nlm4_testargs af_granted; /* (c) notification details */
212 time_t af_expiretime; /* (c) notification time */
214 TAILQ_HEAD(nlm_async_lock_list, nlm_async_lock);
219 enum nlm_host_state {
227 CLIENT *nr_client; /* (l) RPC client handle */
228 time_t nr_create_time; /* (l) when client was created */
233 volatile u_int nh_refs; /* (a) reference count */
234 TAILQ_ENTRY(nlm_host) nh_link; /* (g) global list of hosts */
235 char nh_caller_name[MAXNAMELEN]; /* (c) printable name of host */
236 uint32_t nh_sysid; /* (c) our allocaed system ID */
237 char nh_sysid_string[10]; /* (c) string rep. of sysid */
238 struct sockaddr_storage nh_addr; /* (s) remote address of host */
239 struct nlm_rpc nh_srvrpc; /* (l) RPC for server replies */
240 struct nlm_rpc nh_clntrpc; /* (l) RPC for client requests */
241 rpcvers_t nh_vers; /* (s) NLM version of host */
242 int nh_state; /* (s) last seen NSM state of host */
243 enum nlm_host_state nh_monstate; /* (l) local NSM monitoring state */
244 time_t nh_idle_timeout; /* (s) Time at which host is idle */
245 struct sysctl_ctx_list nh_sysctl; /* (c) vfs.nlm.sysid nodes */
246 uint32_t nh_grantcookie; /* (l) grant cookie counter */
247 struct nlm_async_lock_list nh_pending; /* (l) pending async locks */
248 struct nlm_async_lock_list nh_granted; /* (l) granted locks */
249 struct nlm_async_lock_list nh_finished; /* (l) finished async locks */
251 TAILQ_HEAD(nlm_host_list, nlm_host);
253 static struct nlm_host_list nlm_hosts; /* (g) */
254 static uint32_t nlm_next_sysid = 1; /* (g) */
256 static void nlm_host_unmonitor(struct nlm_host *);
258 struct nlm_grantcookie {
263 static inline uint32_t
264 ng_sysid(struct netobj *src)
267 return ((struct nlm_grantcookie *)src->n_bytes)->ng_sysid;
270 static inline uint32_t
271 ng_cookie(struct netobj *src)
274 return ((struct nlm_grantcookie *)src->n_bytes)->ng_cookie;
277 /**********************************************************************/
280 * Initialise NLM globals.
287 mtx_init(&nlm_global_lock, "nlm_global_lock", NULL, MTX_DEF);
288 TAILQ_INIT(&nlm_waiting_locks);
289 TAILQ_INIT(&nlm_hosts);
291 error = syscall_helper_register(nlm_syscalls, SY_THR_STATIC_KLD);
293 NLM_ERR("Can't register NLM syscall\n");
301 syscall_helper_unregister(nlm_syscalls);
305 * Create a netobj from an arbitrary source.
308 nlm_make_netobj(struct netobj *dst, caddr_t src, size_t srcsize,
309 struct malloc_type *type)
312 dst->n_len = srcsize;
313 dst->n_bytes = malloc(srcsize, type, M_WAITOK);
314 memcpy(dst->n_bytes, src, srcsize);
318 * Copy a struct netobj.
321 nlm_copy_netobj(struct netobj *dst, struct netobj *src,
322 struct malloc_type *type)
325 nlm_make_netobj(dst, src->n_bytes, src->n_len, type);
329 * Create an RPC client handle for the given (address,prog,vers)
333 nlm_get_rpc(struct sockaddr *sa, rpcprog_t prog, rpcvers_t vers)
335 char *wchan = "nlmrcv";
336 struct sockaddr_storage ss;
342 enum clnt_stat stat = RPC_SUCCESS;
343 int rpcvers = RPCBVERS4;
344 bool_t do_tcp = FALSE;
345 bool_t tryagain = FALSE;
346 struct portmap mapping;
350 * First we need to contact the remote RPCBIND service to find
353 memcpy(&ss, sa, sa->sa_len);
354 switch (ss.ss_family) {
356 ((struct sockaddr_in *)&ss)->sin_port = htons(111);
361 ((struct sockaddr_in6 *)&ss)->sin6_port = htons(111);
368 * Unsupported address family - fail.
373 rpcb = clnt_dg_create(so, (struct sockaddr *)&ss,
374 RPCBPROG, rpcvers, 0, 0);
382 parms.r_netid = "tcp";
384 parms.r_netid = "udp";
389 * Use the default timeout.
398 * Try RPCBIND 4 then 3.
401 stat = CLNT_CALL(rpcb, (rpcprog_t) RPCBPROC_GETADDR,
402 (xdrproc_t) xdr_rpcb, &parms,
403 (xdrproc_t) xdr_wrapstring, &uaddr, timo);
404 if (stat == RPC_SUCCESS) {
406 * We have a reply from the remote RPCBIND - turn it
407 * into an appropriate address and make a new client
408 * that can talk to the remote NLM.
410 * XXX fixup IPv6 scope ID.
413 a = __rpc_uaddr2taddr_af(ss.ss_family, uaddr);
418 memcpy(&ss, a->buf, a->len);
421 xdr_free((xdrproc_t) xdr_wrapstring, &uaddr);
424 if (tryagain || stat == RPC_PROGVERSMISMATCH) {
425 if (rpcvers == RPCBVERS4)
427 else if (rpcvers == RPCBVERS)
429 CLNT_CONTROL(rpcb, CLSET_VERS, &rpcvers);
437 mapping.pm_prog = parms.r_prog;
438 mapping.pm_vers = parms.r_vers;
439 mapping.pm_prot = do_tcp ? IPPROTO_TCP : IPPROTO_UDP;
442 stat = CLNT_CALL(rpcb, (rpcprog_t) PMAPPROC_GETPORT,
443 (xdrproc_t) xdr_portmap, &mapping,
444 (xdrproc_t) xdr_u_short, &port, timo);
446 if (stat == RPC_SUCCESS) {
447 switch (ss.ss_family) {
449 ((struct sockaddr_in *)&ss)->sin_port =
455 ((struct sockaddr_in6 *)&ss)->sin6_port =
463 panic("invalid rpcvers %d", rpcvers);
466 * We may have a positive response from the portmapper, but the NLM
467 * service was not found. Make sure we received a valid port.
469 switch (ss.ss_family) {
471 port = ((struct sockaddr_in *)&ss)->sin_port;
475 port = ((struct sockaddr_in6 *)&ss)->sin6_port;
479 if (stat != RPC_SUCCESS || !port) {
481 * If we were able to talk to rpcbind or portmap, but the udp
482 * variant wasn't available, ask about tcp.
484 * XXX - We could also check for a TCP portmapper, but
485 * if the host is running a portmapper at all, we should be able
486 * to hail it over UDP.
488 if (stat == RPC_SUCCESS && !do_tcp) {
493 /* Otherwise, bad news. */
494 NLM_ERR("NLM: failed to contact remote rpcbind, "
495 "stat = %d, port = %d\n", (int) stat, port);
502 * Destroy the UDP client we used to speak to rpcbind and
503 * recreate as a TCP client.
505 struct netconfig *nconf = NULL;
509 switch (ss.ss_family) {
511 nconf = getnetconfigent("tcp");
515 nconf = getnetconfigent("tcp6");
520 rpcb = clnt_reconnect_create(nconf, (struct sockaddr *)&ss,
522 CLNT_CONTROL(rpcb, CLSET_WAITCHAN, wchan);
523 rpcb->cl_auth = nlm_auth;
527 * Re-use the client we used to speak to rpcbind.
529 CLNT_CONTROL(rpcb, CLSET_SVC_ADDR, &ss);
530 CLNT_CONTROL(rpcb, CLSET_PROG, &prog);
531 CLNT_CONTROL(rpcb, CLSET_VERS, &vers);
532 CLNT_CONTROL(rpcb, CLSET_WAITCHAN, wchan);
533 rpcb->cl_auth = nlm_auth;
540 * This async callback after when an async lock request has been
541 * granted. We notify the host which initiated the request.
544 nlm_lock_callback(void *arg, int pending)
546 struct nlm_async_lock *af = (struct nlm_async_lock *) arg;
547 struct rpc_callextra ext;
549 NLM_DEBUG(2, "NLM: async lock %p for %s (sysid %d) granted,"
550 " cookie %d:%d\n", af, af->af_host->nh_caller_name,
551 af->af_host->nh_sysid, ng_sysid(&af->af_granted.cookie),
552 ng_cookie(&af->af_granted.cookie));
555 * Send the results back to the host.
557 * Note: there is a possible race here with nlm_host_notify
558 * destroying the RPC client. To avoid problems, the first
559 * thing nlm_host_notify does is to cancel pending async lock
562 memset(&ext, 0, sizeof(ext));
563 ext.rc_auth = nlm_auth;
564 if (af->af_host->nh_vers == NLM_VERS4) {
565 nlm4_granted_msg_4(&af->af_granted,
566 NULL, af->af_rpc, &ext, nlm_zero_tv);
569 * Back-convert to legacy protocol
571 nlm_testargs granted;
572 granted.cookie = af->af_granted.cookie;
573 granted.exclusive = af->af_granted.exclusive;
574 granted.alock.caller_name =
575 af->af_granted.alock.caller_name;
576 granted.alock.fh = af->af_granted.alock.fh;
577 granted.alock.oh = af->af_granted.alock.oh;
578 granted.alock.svid = af->af_granted.alock.svid;
579 granted.alock.l_offset =
580 af->af_granted.alock.l_offset;
581 granted.alock.l_len =
582 af->af_granted.alock.l_len;
584 nlm_granted_msg_1(&granted,
585 NULL, af->af_rpc, &ext, nlm_zero_tv);
589 * Move this entry to the nh_granted list.
591 af->af_expiretime = time_uptime + NLM_EXPIRE_TIMEOUT;
592 mtx_lock(&af->af_host->nh_lock);
593 TAILQ_REMOVE(&af->af_host->nh_pending, af, af_link);
594 TAILQ_INSERT_TAIL(&af->af_host->nh_granted, af, af_link);
595 mtx_unlock(&af->af_host->nh_lock);
599 * Free an async lock request. The request must have been removed from
603 nlm_free_async_lock(struct nlm_async_lock *af)
606 * Free an async lock.
609 CLNT_RELEASE(af->af_rpc);
610 xdr_free((xdrproc_t) xdr_nlm4_testargs, &af->af_granted);
617 * Cancel our async request - this must be called with
618 * af->nh_host->nh_lock held. This is slightly complicated by a
619 * potential race with our own callback. If we fail to cancel the
620 * lock, it must already have been granted - we make sure our async
621 * task has completed by calling taskqueue_drain in this case.
624 nlm_cancel_async_lock(struct nlm_async_lock *af)
626 struct nlm_host *host = af->af_host;
629 mtx_assert(&host->nh_lock, MA_OWNED);
631 mtx_unlock(&host->nh_lock);
633 error = VOP_ADVLOCKASYNC(af->af_vp, NULL, F_CANCEL, &af->af_fl,
634 F_REMOTE, NULL, &af->af_cookie);
638 * We failed to cancel - make sure our callback has
639 * completed before we continue.
641 taskqueue_drain(taskqueue_thread, &af->af_task);
644 mtx_lock(&host->nh_lock);
647 NLM_DEBUG(2, "NLM: async lock %p for %s (sysid %d) "
648 "cancelled\n", af, host->nh_caller_name, host->nh_sysid);
651 * Remove from the nh_pending list and free now that
652 * we are safe from the callback.
654 TAILQ_REMOVE(&host->nh_pending, af, af_link);
655 mtx_unlock(&host->nh_lock);
656 nlm_free_async_lock(af);
657 mtx_lock(&host->nh_lock);
664 nlm_check_expired_locks(struct nlm_host *host)
666 struct nlm_async_lock *af;
667 time_t uptime = time_uptime;
669 mtx_lock(&host->nh_lock);
670 while ((af = TAILQ_FIRST(&host->nh_granted)) != NULL
671 && uptime >= af->af_expiretime) {
672 NLM_DEBUG(2, "NLM: async lock %p for %s (sysid %d) expired,"
673 " cookie %d:%d\n", af, af->af_host->nh_caller_name,
674 af->af_host->nh_sysid, ng_sysid(&af->af_granted.cookie),
675 ng_cookie(&af->af_granted.cookie));
676 TAILQ_REMOVE(&host->nh_granted, af, af_link);
677 mtx_unlock(&host->nh_lock);
678 nlm_free_async_lock(af);
679 mtx_lock(&host->nh_lock);
681 while ((af = TAILQ_FIRST(&host->nh_finished)) != NULL) {
682 TAILQ_REMOVE(&host->nh_finished, af, af_link);
683 mtx_unlock(&host->nh_lock);
684 nlm_free_async_lock(af);
685 mtx_lock(&host->nh_lock);
687 mtx_unlock(&host->nh_lock);
691 * Free resources used by a host. This is called after the reference
692 * count has reached zero so it doesn't need to worry about locks.
695 nlm_host_destroy(struct nlm_host *host)
698 mtx_lock(&nlm_global_lock);
699 TAILQ_REMOVE(&nlm_hosts, host, nh_link);
700 mtx_unlock(&nlm_global_lock);
702 if (host->nh_srvrpc.nr_client)
703 CLNT_RELEASE(host->nh_srvrpc.nr_client);
704 if (host->nh_clntrpc.nr_client)
705 CLNT_RELEASE(host->nh_clntrpc.nr_client);
706 mtx_destroy(&host->nh_lock);
707 sysctl_ctx_free(&host->nh_sysctl);
712 * Thread start callback for client lock recovery
715 nlm_client_recovery_start(void *arg)
717 struct nlm_host *host = (struct nlm_host *) arg;
719 NLM_DEBUG(1, "NLM: client lock recovery for %s started\n",
720 host->nh_caller_name);
722 nlm_client_recovery(host);
724 NLM_DEBUG(1, "NLM: client lock recovery for %s completed\n",
725 host->nh_caller_name);
727 host->nh_monstate = NLM_MONITORED;
728 nlm_host_release(host);
734 * This is called when we receive a host state change notification. We
735 * unlock any active locks owned by the host. When rpc.lockd is
736 * shutting down, this function is called with newstate set to zero
737 * which allows us to cancel any pending async locks and clear the
741 nlm_host_notify(struct nlm_host *host, int newstate)
743 struct nlm_async_lock *af;
746 NLM_DEBUG(1, "NLM: host %s (sysid %d) rebooted, new "
747 "state is %d\n", host->nh_caller_name,
748 host->nh_sysid, newstate);
752 * Cancel any pending async locks for this host.
754 mtx_lock(&host->nh_lock);
755 while ((af = TAILQ_FIRST(&host->nh_pending)) != NULL) {
757 * nlm_cancel_async_lock will remove the entry from
758 * nh_pending and free it.
760 nlm_cancel_async_lock(af);
762 mtx_unlock(&host->nh_lock);
763 nlm_check_expired_locks(host);
766 * The host just rebooted - trash its locks.
768 lf_clearremotesys(host->nh_sysid);
769 host->nh_state = newstate;
772 * If we have any remote locks for this host (i.e. it
773 * represents a remote NFS server that our local NFS client
774 * has locks for), start a recovery thread.
777 && host->nh_monstate != NLM_RECOVERING
778 && lf_countlocks(NLM_SYSID_CLIENT | host->nh_sysid) > 0) {
780 host->nh_monstate = NLM_RECOVERING;
781 refcount_acquire(&host->nh_refs);
782 kthread_add(nlm_client_recovery_start, host, curproc, &td, 0, 0,
783 "NFS lock recovery for %s", host->nh_caller_name);
788 * Sysctl handler to count the number of locks for a sysid.
791 nlm_host_lock_count_sysctl(SYSCTL_HANDLER_ARGS)
793 struct nlm_host *host;
796 host = oidp->oid_arg1;
797 count = lf_countlocks(host->nh_sysid);
798 return sysctl_handle_int(oidp, &count, 0, req);
802 * Sysctl handler to count the number of client locks for a sysid.
805 nlm_host_client_lock_count_sysctl(SYSCTL_HANDLER_ARGS)
807 struct nlm_host *host;
810 host = oidp->oid_arg1;
811 count = lf_countlocks(NLM_SYSID_CLIENT | host->nh_sysid);
812 return sysctl_handle_int(oidp, &count, 0, req);
816 * Create a new NLM host.
818 static struct nlm_host *
819 nlm_create_host(const char* caller_name)
821 struct nlm_host *host;
822 struct sysctl_oid *oid;
824 mtx_assert(&nlm_global_lock, MA_OWNED);
826 NLM_DEBUG(1, "NLM: new host %s (sysid %d)\n",
827 caller_name, nlm_next_sysid);
828 host = malloc(sizeof(struct nlm_host), M_NLM, M_NOWAIT|M_ZERO);
831 mtx_init(&host->nh_lock, "nh_lock", NULL, MTX_DEF);
833 strlcpy(host->nh_caller_name, caller_name, MAXNAMELEN);
834 host->nh_sysid = nlm_next_sysid++;
835 snprintf(host->nh_sysid_string, sizeof(host->nh_sysid_string),
836 "%d", host->nh_sysid);
839 host->nh_monstate = NLM_UNMONITORED;
840 host->nh_grantcookie = 1;
841 TAILQ_INIT(&host->nh_pending);
842 TAILQ_INIT(&host->nh_granted);
843 TAILQ_INIT(&host->nh_finished);
844 TAILQ_INSERT_TAIL(&nlm_hosts, host, nh_link);
846 mtx_unlock(&nlm_global_lock);
848 sysctl_ctx_init(&host->nh_sysctl);
849 oid = SYSCTL_ADD_NODE(&host->nh_sysctl,
850 SYSCTL_STATIC_CHILDREN(_vfs_nlm_sysid),
851 OID_AUTO, host->nh_sysid_string, CTLFLAG_RD | CTLFLAG_MPSAFE,
853 SYSCTL_ADD_STRING(&host->nh_sysctl, SYSCTL_CHILDREN(oid), OID_AUTO,
854 "hostname", CTLFLAG_RD, host->nh_caller_name, 0, "");
855 SYSCTL_ADD_UINT(&host->nh_sysctl, SYSCTL_CHILDREN(oid), OID_AUTO,
856 "version", CTLFLAG_RD, &host->nh_vers, 0, "");
857 SYSCTL_ADD_UINT(&host->nh_sysctl, SYSCTL_CHILDREN(oid), OID_AUTO,
858 "monitored", CTLFLAG_RD, &host->nh_monstate, 0, "");
859 SYSCTL_ADD_PROC(&host->nh_sysctl, SYSCTL_CHILDREN(oid), OID_AUTO,
860 "lock_count", CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_MPSAFE, host,
861 0, nlm_host_lock_count_sysctl, "I", "");
862 SYSCTL_ADD_PROC(&host->nh_sysctl, SYSCTL_CHILDREN(oid), OID_AUTO,
863 "client_lock_count", CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_MPSAFE,
864 host, 0, nlm_host_client_lock_count_sysctl, "I", "");
866 mtx_lock(&nlm_global_lock);
872 * Acquire the next sysid for remote locks not handled by the NLM.
875 nlm_acquire_next_sysid(void)
879 mtx_lock(&nlm_global_lock);
880 next_sysid = nlm_next_sysid++;
881 mtx_unlock(&nlm_global_lock);
886 * Return non-zero if the address parts of the two sockaddrs are the
890 nlm_compare_addr(const struct sockaddr *a, const struct sockaddr *b)
892 const struct sockaddr_in *a4, *b4;
894 const struct sockaddr_in6 *a6, *b6;
897 if (a->sa_family != b->sa_family)
900 switch (a->sa_family) {
902 a4 = (const struct sockaddr_in *) a;
903 b4 = (const struct sockaddr_in *) b;
904 return !memcmp(&a4->sin_addr, &b4->sin_addr,
905 sizeof(a4->sin_addr));
908 a6 = (const struct sockaddr_in6 *) a;
909 b6 = (const struct sockaddr_in6 *) b;
910 return !memcmp(&a6->sin6_addr, &b6->sin6_addr,
911 sizeof(a6->sin6_addr));
919 * Check for idle hosts and stop monitoring them. We could also free
920 * the host structure here, possibly after a larger timeout but that
921 * would require some care to avoid races with
922 * e.g. nlm_host_lock_count_sysctl.
927 struct nlm_host *host;
929 mtx_assert(&nlm_global_lock, MA_OWNED);
931 if (time_uptime <= nlm_next_idle_check)
934 nlm_next_idle_check = time_uptime + NLM_IDLE_PERIOD;
936 TAILQ_FOREACH(host, &nlm_hosts, nh_link) {
937 if (host->nh_monstate == NLM_MONITORED
938 && time_uptime > host->nh_idle_timeout) {
939 mtx_unlock(&nlm_global_lock);
940 if (lf_countlocks(host->nh_sysid) > 0
941 || lf_countlocks(NLM_SYSID_CLIENT
943 host->nh_idle_timeout =
944 time_uptime + NLM_IDLE_TIMEOUT;
945 mtx_lock(&nlm_global_lock);
948 nlm_host_unmonitor(host);
949 mtx_lock(&nlm_global_lock);
955 * Search for an existing NLM host that matches the given name
956 * (typically the caller_name element of an nlm4_lock). If none is
957 * found, create a new host. If 'addr' is non-NULL, record the remote
958 * address of the host so that we can call it back for async
959 * responses. If 'vers' is greater than zero then record the NLM
960 * program version to use to communicate with this client.
963 nlm_find_host_by_name(const char *name, const struct sockaddr *addr,
966 struct nlm_host *host;
968 mtx_lock(&nlm_global_lock);
971 * The remote host is determined by caller_name.
973 TAILQ_FOREACH(host, &nlm_hosts, nh_link) {
974 if (!strcmp(host->nh_caller_name, name))
979 host = nlm_create_host(name);
981 mtx_unlock(&nlm_global_lock);
985 refcount_acquire(&host->nh_refs);
987 host->nh_idle_timeout = time_uptime + NLM_IDLE_TIMEOUT;
990 * If we have an address for the host, record it so that we
991 * can send async replies etc.
995 KASSERT(addr->sa_len < sizeof(struct sockaddr_storage),
996 ("Strange remote transport address length"));
999 * If we have seen an address before and we currently
1000 * have an RPC client handle, make sure the address is
1001 * the same, otherwise discard the client handle.
1003 if (host->nh_addr.ss_len && host->nh_srvrpc.nr_client) {
1004 if (!nlm_compare_addr(
1005 (struct sockaddr *) &host->nh_addr,
1007 || host->nh_vers != vers) {
1009 mtx_lock(&host->nh_lock);
1010 client = host->nh_srvrpc.nr_client;
1011 host->nh_srvrpc.nr_client = NULL;
1012 mtx_unlock(&host->nh_lock);
1014 CLNT_RELEASE(client);
1018 memcpy(&host->nh_addr, addr, addr->sa_len);
1019 host->nh_vers = vers;
1024 mtx_unlock(&nlm_global_lock);
1030 * Search for an existing NLM host that matches the given remote
1031 * address. If none is found, create a new host with the requested
1032 * address and remember 'vers' as the NLM protocol version to use for
1036 nlm_find_host_by_addr(const struct sockaddr *addr, int vers)
1039 * Fake up a name using inet_ntop. This buffer is
1040 * large enough for an IPv6 address.
1042 char tmp[sizeof "ffff:ffff:ffff:ffff:ffff:ffff:255.255.255.255"];
1043 struct nlm_host *host;
1045 switch (addr->sa_family) {
1048 &((const struct sockaddr_in *) addr)->sin_addr,
1054 &((const struct sockaddr_in6 *) addr)->sin6_addr,
1059 strlcpy(tmp, "<unknown>", sizeof(tmp));
1062 mtx_lock(&nlm_global_lock);
1065 * The remote host is determined by caller_name.
1067 TAILQ_FOREACH(host, &nlm_hosts, nh_link) {
1068 if (nlm_compare_addr(addr,
1069 (const struct sockaddr *) &host->nh_addr))
1074 host = nlm_create_host(tmp);
1076 mtx_unlock(&nlm_global_lock);
1079 memcpy(&host->nh_addr, addr, addr->sa_len);
1080 host->nh_vers = vers;
1082 refcount_acquire(&host->nh_refs);
1084 host->nh_idle_timeout = time_uptime + NLM_IDLE_TIMEOUT;
1088 mtx_unlock(&nlm_global_lock);
1094 * Find the NLM host that matches the value of 'sysid'. If none
1095 * exists, return NULL.
1097 static struct nlm_host *
1098 nlm_find_host_by_sysid(int sysid)
1100 struct nlm_host *host;
1102 TAILQ_FOREACH(host, &nlm_hosts, nh_link) {
1103 if (host->nh_sysid == sysid) {
1104 refcount_acquire(&host->nh_refs);
1112 void nlm_host_release(struct nlm_host *host)
1114 if (refcount_release(&host->nh_refs)) {
1118 nlm_host_destroy(host);
1123 * Unregister this NLM host with the local NSM due to idleness.
1126 nlm_host_unmonitor(struct nlm_host *host)
1130 struct timeval timo;
1131 enum clnt_stat stat;
1133 NLM_DEBUG(1, "NLM: unmonitoring %s (sysid %d)\n",
1134 host->nh_caller_name, host->nh_sysid);
1137 * We put our assigned system ID value in the priv field to
1138 * make it simpler to find the host if we are notified of a
1141 smmonid.mon_name = host->nh_caller_name;
1142 smmonid.my_id.my_name = "localhost";
1143 smmonid.my_id.my_prog = NLM_PROG;
1144 smmonid.my_id.my_vers = NLM_SM;
1145 smmonid.my_id.my_proc = NLM_SM_NOTIFY;
1149 stat = CLNT_CALL(nlm_nsm, SM_UNMON,
1150 (xdrproc_t) xdr_mon, &smmonid,
1151 (xdrproc_t) xdr_sm_stat, &smstat, timo);
1153 if (stat != RPC_SUCCESS) {
1154 NLM_ERR("Failed to contact local NSM - rpc error %d\n", stat);
1157 if (smstat.res_stat == stat_fail) {
1158 NLM_ERR("Local NSM refuses to unmonitor %s\n",
1159 host->nh_caller_name);
1163 host->nh_monstate = NLM_UNMONITORED;
1167 * Register this NLM host with the local NSM so that we can be
1168 * notified if it reboots.
1171 nlm_host_monitor(struct nlm_host *host, int state)
1175 struct timeval timo;
1176 enum clnt_stat stat;
1178 if (state && !host->nh_state) {
1180 * This is the first time we have seen an NSM state
1181 * value for this host. We record it here to help
1182 * detect host reboots.
1184 host->nh_state = state;
1185 NLM_DEBUG(1, "NLM: host %s (sysid %d) has NSM state %d\n",
1186 host->nh_caller_name, host->nh_sysid, state);
1189 mtx_lock(&host->nh_lock);
1190 if (host->nh_monstate != NLM_UNMONITORED) {
1191 mtx_unlock(&host->nh_lock);
1194 host->nh_monstate = NLM_MONITORED;
1195 mtx_unlock(&host->nh_lock);
1197 NLM_DEBUG(1, "NLM: monitoring %s (sysid %d)\n",
1198 host->nh_caller_name, host->nh_sysid);
1201 * We put our assigned system ID value in the priv field to
1202 * make it simpler to find the host if we are notified of a
1205 smmon.mon_id.mon_name = host->nh_caller_name;
1206 smmon.mon_id.my_id.my_name = "localhost";
1207 smmon.mon_id.my_id.my_prog = NLM_PROG;
1208 smmon.mon_id.my_id.my_vers = NLM_SM;
1209 smmon.mon_id.my_id.my_proc = NLM_SM_NOTIFY;
1210 memcpy(smmon.priv, &host->nh_sysid, sizeof(host->nh_sysid));
1214 stat = CLNT_CALL(nlm_nsm, SM_MON,
1215 (xdrproc_t) xdr_mon, &smmon,
1216 (xdrproc_t) xdr_sm_stat, &smstat, timo);
1218 if (stat != RPC_SUCCESS) {
1219 NLM_ERR("Failed to contact local NSM - rpc error %d\n", stat);
1222 if (smstat.res_stat == stat_fail) {
1223 NLM_ERR("Local NSM refuses to monitor %s\n",
1224 host->nh_caller_name);
1225 mtx_lock(&host->nh_lock);
1226 host->nh_monstate = NLM_MONITOR_FAILED;
1227 mtx_unlock(&host->nh_lock);
1231 host->nh_monstate = NLM_MONITORED;
1235 * Return an RPC client handle that can be used to talk to the NLM
1236 * running on the given host.
1239 nlm_host_get_rpc(struct nlm_host *host, bool_t isserver)
1241 struct nlm_rpc *rpc;
1244 mtx_lock(&host->nh_lock);
1247 rpc = &host->nh_srvrpc;
1249 rpc = &host->nh_clntrpc;
1252 * We can't hold onto RPC handles for too long - the async
1253 * call/reply protocol used by some NLM clients makes it hard
1254 * to tell when they change port numbers (e.g. after a
1255 * reboot). Note that if a client reboots while it isn't
1256 * holding any locks, it won't bother to notify us. We
1257 * expire the RPC handles after two minutes.
1259 if (rpc->nr_client && time_uptime > rpc->nr_create_time + 2*60) {
1260 client = rpc->nr_client;
1261 rpc->nr_client = NULL;
1262 mtx_unlock(&host->nh_lock);
1263 CLNT_RELEASE(client);
1264 mtx_lock(&host->nh_lock);
1267 if (!rpc->nr_client) {
1268 mtx_unlock(&host->nh_lock);
1269 client = nlm_get_rpc((struct sockaddr *)&host->nh_addr,
1270 NLM_PROG, host->nh_vers);
1271 mtx_lock(&host->nh_lock);
1274 if (rpc->nr_client) {
1275 mtx_unlock(&host->nh_lock);
1276 CLNT_DESTROY(client);
1277 mtx_lock(&host->nh_lock);
1279 rpc->nr_client = client;
1280 rpc->nr_create_time = time_uptime;
1285 client = rpc->nr_client;
1287 CLNT_ACQUIRE(client);
1288 mtx_unlock(&host->nh_lock);
1294 int nlm_host_get_sysid(struct nlm_host *host)
1297 return (host->nh_sysid);
1301 nlm_host_get_state(struct nlm_host *host)
1304 return (host->nh_state);
1308 nlm_register_wait_lock(struct nlm4_lock *lock, struct vnode *vp)
1310 struct nlm_waiting_lock *nw;
1312 nw = malloc(sizeof(struct nlm_waiting_lock), M_NLM, M_WAITOK);
1313 nw->nw_lock = *lock;
1314 memcpy(&nw->nw_fh.fh_bytes, nw->nw_lock.fh.n_bytes,
1315 nw->nw_lock.fh.n_len);
1316 nw->nw_lock.fh.n_bytes = nw->nw_fh.fh_bytes;
1317 nw->nw_waiting = TRUE;
1319 mtx_lock(&nlm_global_lock);
1320 TAILQ_INSERT_TAIL(&nlm_waiting_locks, nw, nw_link);
1321 mtx_unlock(&nlm_global_lock);
1327 nlm_deregister_wait_lock(void *handle)
1329 struct nlm_waiting_lock *nw = handle;
1331 mtx_lock(&nlm_global_lock);
1332 TAILQ_REMOVE(&nlm_waiting_locks, nw, nw_link);
1333 mtx_unlock(&nlm_global_lock);
1339 nlm_wait_lock(void *handle, int timo)
1341 struct nlm_waiting_lock *nw = handle;
1342 int error, stops_deferred;
1345 * If the granted message arrived before we got here,
1346 * nw->nw_waiting will be FALSE - in that case, don't sleep.
1348 mtx_lock(&nlm_global_lock);
1350 if (nw->nw_waiting) {
1351 stops_deferred = sigdeferstop(SIGDEFERSTOP_ERESTART);
1352 error = msleep(nw, &nlm_global_lock, PCATCH, "nlmlock", timo);
1353 sigallowstop(stops_deferred);
1355 TAILQ_REMOVE(&nlm_waiting_locks, nw, nw_link);
1358 * The granted message may arrive after the
1359 * interrupt/timeout but before we manage to lock the
1360 * mutex. Detect this by examining nw_lock.
1362 if (!nw->nw_waiting)
1366 * If nlm_cancel_wait is called, then error will be
1367 * zero but nw_waiting will still be TRUE. We
1368 * translate this into EINTR.
1373 mtx_unlock(&nlm_global_lock);
1381 nlm_cancel_wait(struct vnode *vp)
1383 struct nlm_waiting_lock *nw;
1385 mtx_lock(&nlm_global_lock);
1386 TAILQ_FOREACH(nw, &nlm_waiting_locks, nw_link) {
1387 if (nw->nw_vp == vp) {
1391 mtx_unlock(&nlm_global_lock);
1394 /**********************************************************************/
1397 * Syscall interface with userland.
1400 extern void nlm_prog_0(struct svc_req *rqstp, SVCXPRT *transp);
1401 extern void nlm_prog_1(struct svc_req *rqstp, SVCXPRT *transp);
1402 extern void nlm_prog_3(struct svc_req *rqstp, SVCXPRT *transp);
1403 extern void nlm_prog_4(struct svc_req *rqstp, SVCXPRT *transp);
1406 nlm_register_services(SVCPOOL *pool, int addr_count, char **addrs)
1408 static rpcvers_t versions[] = {
1409 NLM_SM, NLM_VERS, NLM_VERSX, NLM_VERS4
1411 static void (*dispatchers[])(struct svc_req *, SVCXPRT *) = {
1412 nlm_prog_0, nlm_prog_1, nlm_prog_3, nlm_prog_4
1418 struct netconfig *nconf;
1422 NLM_ERR("NLM: no service addresses given - can't start server");
1426 if (addr_count < 0 || addr_count > 256 ) {
1427 NLM_ERR("NLM: too many service addresses (%d) given, "
1428 "max 256 - can't start server\n", addr_count);
1432 xprts = malloc(addr_count * sizeof(SVCXPRT *), M_NLM, M_WAITOK|M_ZERO);
1433 for (i = 0; i < nitems(versions); i++) {
1434 for (j = 0; j < addr_count; j++) {
1436 * Create transports for the first version and
1437 * then just register everything else to the
1443 error = copyin(&addrs[2*j], &up,
1447 error = copyinstr(up, netid, sizeof(netid),
1451 error = copyin(&addrs[2*j+1], &up,
1455 error = copyinstr(up, uaddr, sizeof(uaddr),
1459 nconf = getnetconfigent(netid);
1461 NLM_ERR("Can't lookup netid %s\n",
1466 xprts[j] = svc_tp_create(pool, dispatchers[i],
1467 NLM_PROG, versions[i], uaddr, nconf);
1469 NLM_ERR("NLM: unable to create "
1470 "(NLM_PROG, %d).\n", versions[i]);
1474 freenetconfigent(nconf);
1476 nconf = getnetconfigent(xprts[j]->xp_netid);
1477 rpcb_unset(NLM_PROG, versions[i], nconf);
1478 if (!svc_reg(xprts[j], NLM_PROG, versions[i],
1479 dispatchers[i], nconf)) {
1480 NLM_ERR("NLM: can't register "
1481 "(NLM_PROG, %d)\n", versions[i]);
1490 for (j = 0; j < addr_count; j++) {
1492 SVC_RELEASE(xprts[j]);
1499 * Main server entry point. Contacts the local NSM to get its current
1500 * state and send SM_UNMON_ALL. Registers the NLM services and then
1501 * services requests. Does not return until the server is interrupted
1505 nlm_server_main(int addr_count, char **addrs)
1507 struct thread *td = curthread;
1509 SVCPOOL *pool = NULL;
1513 struct sockaddr_in6 sin6;
1515 struct sockaddr_in sin;
1518 struct timeval timo;
1519 enum clnt_stat stat;
1520 struct nlm_host *host, *nhost;
1521 struct nlm_waiting_lock *nw;
1522 vop_advlock_t *old_nfs_advlock;
1523 vop_reclaim_t *old_nfs_reclaim;
1525 if (nlm_is_running != 0) {
1526 NLM_ERR("NLM: can't start server - "
1527 "it appears to be running already\n");
1531 if (nlm_socket == NULL) {
1532 memset(&opt, 0, sizeof(opt));
1534 error = socreate(AF_INET, &nlm_socket, SOCK_DGRAM, 0,
1537 NLM_ERR("NLM: can't create IPv4 socket - error %d\n",
1541 opt.sopt_dir = SOPT_SET;
1542 opt.sopt_level = IPPROTO_IP;
1543 opt.sopt_name = IP_PORTRANGE;
1544 portlow = IP_PORTRANGE_LOW;
1545 opt.sopt_val = &portlow;
1546 opt.sopt_valsize = sizeof(portlow);
1547 sosetopt(nlm_socket, &opt);
1551 error = socreate(AF_INET6, &nlm_socket6, SOCK_DGRAM, 0,
1554 NLM_ERR("NLM: can't create IPv6 socket - error %d\n",
1556 soclose(nlm_socket);
1560 opt.sopt_dir = SOPT_SET;
1561 opt.sopt_level = IPPROTO_IPV6;
1562 opt.sopt_name = IPV6_PORTRANGE;
1563 portlow = IPV6_PORTRANGE_LOW;
1564 opt.sopt_val = &portlow;
1565 opt.sopt_valsize = sizeof(portlow);
1566 sosetopt(nlm_socket6, &opt);
1570 nlm_auth = authunix_create(curthread->td_ucred);
1573 memset(&sin6, 0, sizeof(sin6));
1574 sin6.sin6_len = sizeof(sin6);
1575 sin6.sin6_family = AF_INET6;
1576 sin6.sin6_addr = in6addr_loopback;
1577 nlm_nsm = nlm_get_rpc((struct sockaddr *) &sin6, SM_PROG, SM_VERS);
1580 memset(&sin, 0, sizeof(sin));
1581 sin.sin_len = sizeof(sin);
1582 sin.sin_family = AF_INET;
1583 sin.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
1584 nlm_nsm = nlm_get_rpc((struct sockaddr *) &sin, SM_PROG,
1591 NLM_ERR("Can't start NLM - unable to contact NSM\n");
1596 pool = svcpool_create("NLM", NULL);
1598 error = nlm_register_services(pool, addr_count, addrs);
1602 memset(&id, 0, sizeof(id));
1603 id.my_name = "NFS NLM";
1607 stat = CLNT_CALL(nlm_nsm, SM_UNMON_ALL,
1608 (xdrproc_t) xdr_my_id, &id,
1609 (xdrproc_t) xdr_sm_stat, &smstat, timo);
1611 if (stat != RPC_SUCCESS) {
1614 CLNT_GETERR(nlm_nsm, &err);
1615 NLM_ERR("NLM: unexpected error contacting NSM, "
1616 "stat=%d, errno=%d\n", stat, err.re_errno);
1622 NLM_DEBUG(1, "NLM: local NSM state is %d\n", smstat.state);
1623 nlm_nsm_state = smstat.state;
1625 old_nfs_advlock = nfs_advlock_p;
1626 nfs_advlock_p = nlm_advlock;
1627 old_nfs_reclaim = nfs_reclaim_p;
1628 nfs_reclaim_p = nlm_reclaim;
1633 nfs_advlock_p = old_nfs_advlock;
1634 nfs_reclaim_p = old_nfs_reclaim;
1639 svcpool_destroy(pool);
1642 * We are finished communicating with the NSM.
1645 CLNT_RELEASE(nlm_nsm);
1650 * Trash all the existing state so that if the server
1651 * restarts, it gets a clean slate. This is complicated by the
1652 * possibility that there may be other threads trying to make
1653 * client locking requests.
1655 * First we fake a client reboot notification which will
1656 * cancel any pending async locks and purge remote lock state
1657 * from the local lock manager. We release the reference from
1658 * nlm_hosts to the host (which may remove it from the list
1659 * and free it). After this phase, the only entries in the
1660 * nlm_host list should be from other threads performing
1661 * client lock requests.
1663 mtx_lock(&nlm_global_lock);
1664 TAILQ_FOREACH(nw, &nlm_waiting_locks, nw_link) {
1667 TAILQ_FOREACH_SAFE(host, &nlm_hosts, nh_link, nhost) {
1668 mtx_unlock(&nlm_global_lock);
1669 nlm_host_notify(host, 0);
1670 nlm_host_release(host);
1671 mtx_lock(&nlm_global_lock);
1673 mtx_unlock(&nlm_global_lock);
1675 AUTH_DESTROY(nlm_auth);
1681 sys_nlm_syscall(struct thread *td, struct nlm_syscall_args *uap)
1685 error = priv_check(td, PRIV_NFS_LOCKD);
1689 nlm_debug_level = uap->debug_level;
1690 nlm_grace_threshold = time_uptime + uap->grace_period;
1691 nlm_next_idle_check = time_uptime + NLM_IDLE_PERIOD;
1693 return nlm_server_main(uap->addr_count, uap->addrs);
1696 /**********************************************************************/
1699 * NLM implementation details, called from the RPC stubs.
1703 nlm_sm_notify(struct nlm_sm_status *argp)
1706 struct nlm_host *host;
1708 NLM_DEBUG(3, "nlm_sm_notify(): mon_name = %s\n", argp->mon_name);
1709 memcpy(&sysid, &argp->priv, sizeof(sysid));
1710 host = nlm_find_host_by_sysid(sysid);
1712 nlm_host_notify(host, argp->state);
1713 nlm_host_release(host);
1718 nlm_convert_to_fhandle_t(fhandle_t *fhp, struct netobj *p)
1720 memcpy(fhp, p->n_bytes, sizeof(fhandle_t));
1724 struct mount *vs_mp;
1725 struct vnode *vs_vp;
1730 nlm_get_vfs_state(struct nlm_host *host, struct svc_req *rqstp,
1731 fhandle_t *fhp, struct vfs_state *vs, accmode_t accmode)
1735 struct ucred *cred = NULL, *credanon = NULL;
1737 memset(vs, 0, sizeof(*vs));
1739 vs->vs_mp = vfs_getvfs(&fhp->fh_fsid);
1744 /* accmode == 0 means don't check, since it is an unlock. */
1746 error = VFS_CHECKEXP(vs->vs_mp,
1747 (struct sockaddr *)&host->nh_addr, &exflags, &credanon,
1752 if (exflags & MNT_EXRDONLY ||
1753 (vs->vs_mp->mnt_flag & MNT_RDONLY)) {
1759 error = VFS_FHTOVP(vs->vs_mp, &fhp->fh_fid, LK_EXCLUSIVE, &vs->vs_vp);
1762 vs->vs_vnlocked = TRUE;
1765 if (!svc_getcred(rqstp, &cred, NULL)) {
1769 if (cred->cr_uid == 0 || (exflags & MNT_EXPORTANON)) {
1778 error = VOP_ACCESS(vs->vs_vp, accmode, cred, curthread);
1780 * If this failed and accmode != VWRITE, try again with
1781 * VWRITE to maintain backwards compatibility with the
1782 * old code that always used VWRITE.
1784 if (error != 0 && accmode != VWRITE)
1785 error = VOP_ACCESS(vs->vs_vp, VWRITE, cred, curthread);
1790 VOP_UNLOCK(vs->vs_vp);
1791 vs->vs_vnlocked = FALSE;
1803 nlm_release_vfs_state(struct vfs_state *vs)
1807 if (vs->vs_vnlocked)
1817 nlm_convert_error(int error)
1820 if (error == ESTALE)
1821 return nlm4_stale_fh;
1822 else if (error == EROFS)
1829 nlm_do_test(nlm4_testargs *argp, nlm4_testres *result, struct svc_req *rqstp,
1833 struct vfs_state vs;
1834 struct nlm_host *host, *bhost;
1839 memset(result, 0, sizeof(*result));
1840 memset(&vs, 0, sizeof(vs));
1842 host = nlm_find_host_by_name(argp->alock.caller_name,
1843 svc_getrpccaller(rqstp), rqstp->rq_vers);
1845 result->stat.stat = nlm4_denied_nolocks;
1849 NLM_DEBUG(3, "nlm_do_test(): caller_name = %s (sysid = %d)\n",
1850 host->nh_caller_name, host->nh_sysid);
1852 nlm_check_expired_locks(host);
1853 sysid = host->nh_sysid;
1855 nlm_convert_to_fhandle_t(&fh, &argp->alock.fh);
1856 nlm_copy_netobj(&result->cookie, &argp->cookie, M_RPC);
1858 if (time_uptime < nlm_grace_threshold) {
1859 result->stat.stat = nlm4_denied_grace_period;
1863 accmode = argp->exclusive ? VWRITE : VREAD;
1864 error = nlm_get_vfs_state(host, rqstp, &fh, &vs, accmode);
1866 result->stat.stat = nlm_convert_error(error);
1870 fl.l_start = argp->alock.l_offset;
1871 fl.l_len = argp->alock.l_len;
1872 fl.l_pid = argp->alock.svid;
1874 fl.l_whence = SEEK_SET;
1875 if (argp->exclusive)
1876 fl.l_type = F_WRLCK;
1878 fl.l_type = F_RDLCK;
1879 error = VOP_ADVLOCK(vs.vs_vp, NULL, F_GETLK, &fl, F_REMOTE);
1881 result->stat.stat = nlm4_failed;
1885 if (fl.l_type == F_UNLCK) {
1886 result->stat.stat = nlm4_granted;
1888 result->stat.stat = nlm4_denied;
1889 result->stat.nlm4_testrply_u.holder.exclusive =
1890 (fl.l_type == F_WRLCK);
1891 result->stat.nlm4_testrply_u.holder.svid = fl.l_pid;
1892 bhost = nlm_find_host_by_sysid(fl.l_sysid);
1895 * We don't have any useful way of recording
1896 * the value of oh used in the original lock
1897 * request. Ideally, the test reply would have
1898 * a space for the owning host's name allowing
1899 * our caller's NLM to keep track.
1901 * As far as I can see, Solaris uses an eight
1902 * byte structure for oh which contains a four
1903 * byte pid encoded in local byte order and
1904 * the first four bytes of the host
1905 * name. Linux uses a variable length string
1906 * 'pid@hostname' in ascii but doesn't even
1907 * return that in test replies.
1909 * For the moment, return nothing in oh
1910 * (already zero'ed above).
1912 nlm_host_release(bhost);
1914 result->stat.nlm4_testrply_u.holder.l_offset = fl.l_start;
1915 result->stat.nlm4_testrply_u.holder.l_len = fl.l_len;
1919 nlm_release_vfs_state(&vs);
1921 *rpcp = nlm_host_get_rpc(host, TRUE);
1922 nlm_host_release(host);
1927 nlm_do_lock(nlm4_lockargs *argp, nlm4_res *result, struct svc_req *rqstp,
1928 bool_t monitor, CLIENT **rpcp)
1931 struct vfs_state vs;
1932 struct nlm_host *host;
1937 memset(result, 0, sizeof(*result));
1938 memset(&vs, 0, sizeof(vs));
1940 host = nlm_find_host_by_name(argp->alock.caller_name,
1941 svc_getrpccaller(rqstp), rqstp->rq_vers);
1943 result->stat.stat = nlm4_denied_nolocks;
1947 NLM_DEBUG(3, "nlm_do_lock(): caller_name = %s (sysid = %d)\n",
1948 host->nh_caller_name, host->nh_sysid);
1950 if (monitor && host->nh_state && argp->state
1951 && host->nh_state != argp->state) {
1953 * The host rebooted without telling us. Trash its
1956 nlm_host_notify(host, argp->state);
1959 nlm_check_expired_locks(host);
1960 sysid = host->nh_sysid;
1962 nlm_convert_to_fhandle_t(&fh, &argp->alock.fh);
1963 nlm_copy_netobj(&result->cookie, &argp->cookie, M_RPC);
1965 if (time_uptime < nlm_grace_threshold && !argp->reclaim) {
1966 result->stat.stat = nlm4_denied_grace_period;
1970 accmode = argp->exclusive ? VWRITE : VREAD;
1971 error = nlm_get_vfs_state(host, rqstp, &fh, &vs, accmode);
1973 result->stat.stat = nlm_convert_error(error);
1977 fl.l_start = argp->alock.l_offset;
1978 fl.l_len = argp->alock.l_len;
1979 fl.l_pid = argp->alock.svid;
1981 fl.l_whence = SEEK_SET;
1982 if (argp->exclusive)
1983 fl.l_type = F_WRLCK;
1985 fl.l_type = F_RDLCK;
1987 struct nlm_async_lock *af;
1989 struct nlm_grantcookie cookie;
1992 * First, make sure we can contact the host's NLM.
1994 client = nlm_host_get_rpc(host, TRUE);
1996 result->stat.stat = nlm4_failed;
2001 * First we need to check and see if there is an
2002 * existing blocked lock that matches. This could be a
2003 * badly behaved client or an RPC re-send. If we find
2004 * one, just return nlm4_blocked.
2006 mtx_lock(&host->nh_lock);
2007 TAILQ_FOREACH(af, &host->nh_pending, af_link) {
2008 if (af->af_fl.l_start == fl.l_start
2009 && af->af_fl.l_len == fl.l_len
2010 && af->af_fl.l_pid == fl.l_pid
2011 && af->af_fl.l_type == fl.l_type) {
2016 cookie.ng_sysid = host->nh_sysid;
2017 cookie.ng_cookie = host->nh_grantcookie++;
2019 mtx_unlock(&host->nh_lock);
2021 CLNT_RELEASE(client);
2022 result->stat.stat = nlm4_blocked;
2026 af = malloc(sizeof(struct nlm_async_lock), M_NLM,
2028 TASK_INIT(&af->af_task, 0, nlm_lock_callback, af);
2029 af->af_vp = vs.vs_vp;
2032 af->af_rpc = client;
2034 * We use M_RPC here so that we can xdr_free the thing
2037 nlm_make_netobj(&af->af_granted.cookie,
2038 (caddr_t)&cookie, sizeof(cookie), M_RPC);
2039 af->af_granted.exclusive = argp->exclusive;
2040 af->af_granted.alock.caller_name =
2041 strdup(argp->alock.caller_name, M_RPC);
2042 nlm_copy_netobj(&af->af_granted.alock.fh,
2043 &argp->alock.fh, M_RPC);
2044 nlm_copy_netobj(&af->af_granted.alock.oh,
2045 &argp->alock.oh, M_RPC);
2046 af->af_granted.alock.svid = argp->alock.svid;
2047 af->af_granted.alock.l_offset = argp->alock.l_offset;
2048 af->af_granted.alock.l_len = argp->alock.l_len;
2051 * Put the entry on the pending list before calling
2052 * VOP_ADVLOCKASYNC. We do this in case the lock
2053 * request was blocked (returning EINPROGRESS) but
2054 * then granted before we manage to run again. The
2055 * client may receive the granted message before we
2056 * send our blocked reply but thats their problem.
2058 mtx_lock(&host->nh_lock);
2059 TAILQ_INSERT_TAIL(&host->nh_pending, af, af_link);
2060 mtx_unlock(&host->nh_lock);
2062 error = VOP_ADVLOCKASYNC(vs.vs_vp, NULL, F_SETLK, &fl, F_REMOTE,
2063 &af->af_task, &af->af_cookie);
2066 * If the lock completed synchronously, just free the
2067 * tracking structure now.
2069 if (error != EINPROGRESS) {
2070 CLNT_RELEASE(af->af_rpc);
2071 mtx_lock(&host->nh_lock);
2072 TAILQ_REMOVE(&host->nh_pending, af, af_link);
2073 mtx_unlock(&host->nh_lock);
2074 xdr_free((xdrproc_t) xdr_nlm4_testargs,
2078 NLM_DEBUG(2, "NLM: pending async lock %p for %s "
2079 "(sysid %d)\n", af, host->nh_caller_name, sysid);
2081 * Don't vrele the vnode just yet - this must
2082 * wait until either the async callback
2083 * happens or the lock is cancelled.
2088 error = VOP_ADVLOCK(vs.vs_vp, NULL, F_SETLK, &fl, F_REMOTE);
2092 if (error == EINPROGRESS) {
2093 result->stat.stat = nlm4_blocked;
2094 } else if (error == EDEADLK) {
2095 result->stat.stat = nlm4_deadlck;
2096 } else if (error == EAGAIN) {
2097 result->stat.stat = nlm4_denied;
2099 result->stat.stat = nlm4_failed;
2103 nlm_host_monitor(host, argp->state);
2104 result->stat.stat = nlm4_granted;
2108 nlm_release_vfs_state(&vs);
2110 *rpcp = nlm_host_get_rpc(host, TRUE);
2111 nlm_host_release(host);
2116 nlm_do_cancel(nlm4_cancargs *argp, nlm4_res *result, struct svc_req *rqstp,
2120 struct vfs_state vs;
2121 struct nlm_host *host;
2124 struct nlm_async_lock *af;
2126 memset(result, 0, sizeof(*result));
2127 memset(&vs, 0, sizeof(vs));
2129 host = nlm_find_host_by_name(argp->alock.caller_name,
2130 svc_getrpccaller(rqstp), rqstp->rq_vers);
2132 result->stat.stat = nlm4_denied_nolocks;
2136 NLM_DEBUG(3, "nlm_do_cancel(): caller_name = %s (sysid = %d)\n",
2137 host->nh_caller_name, host->nh_sysid);
2139 nlm_check_expired_locks(host);
2140 sysid = host->nh_sysid;
2142 nlm_convert_to_fhandle_t(&fh, &argp->alock.fh);
2143 nlm_copy_netobj(&result->cookie, &argp->cookie, M_RPC);
2145 if (time_uptime < nlm_grace_threshold) {
2146 result->stat.stat = nlm4_denied_grace_period;
2150 error = nlm_get_vfs_state(host, rqstp, &fh, &vs, (accmode_t)0);
2152 result->stat.stat = nlm_convert_error(error);
2156 fl.l_start = argp->alock.l_offset;
2157 fl.l_len = argp->alock.l_len;
2158 fl.l_pid = argp->alock.svid;
2160 fl.l_whence = SEEK_SET;
2161 if (argp->exclusive)
2162 fl.l_type = F_WRLCK;
2164 fl.l_type = F_RDLCK;
2167 * First we need to try and find the async lock request - if
2168 * there isn't one, we give up and return nlm4_denied.
2170 mtx_lock(&host->nh_lock);
2172 TAILQ_FOREACH(af, &host->nh_pending, af_link) {
2173 if (af->af_fl.l_start == fl.l_start
2174 && af->af_fl.l_len == fl.l_len
2175 && af->af_fl.l_pid == fl.l_pid
2176 && af->af_fl.l_type == fl.l_type) {
2182 mtx_unlock(&host->nh_lock);
2183 result->stat.stat = nlm4_denied;
2187 error = nlm_cancel_async_lock(af);
2190 result->stat.stat = nlm4_denied;
2192 result->stat.stat = nlm4_granted;
2195 mtx_unlock(&host->nh_lock);
2198 nlm_release_vfs_state(&vs);
2200 *rpcp = nlm_host_get_rpc(host, TRUE);
2201 nlm_host_release(host);
2206 nlm_do_unlock(nlm4_unlockargs *argp, nlm4_res *result, struct svc_req *rqstp,
2210 struct vfs_state vs;
2211 struct nlm_host *host;
2215 memset(result, 0, sizeof(*result));
2216 memset(&vs, 0, sizeof(vs));
2218 host = nlm_find_host_by_name(argp->alock.caller_name,
2219 svc_getrpccaller(rqstp), rqstp->rq_vers);
2221 result->stat.stat = nlm4_denied_nolocks;
2225 NLM_DEBUG(3, "nlm_do_unlock(): caller_name = %s (sysid = %d)\n",
2226 host->nh_caller_name, host->nh_sysid);
2228 nlm_check_expired_locks(host);
2229 sysid = host->nh_sysid;
2231 nlm_convert_to_fhandle_t(&fh, &argp->alock.fh);
2232 nlm_copy_netobj(&result->cookie, &argp->cookie, M_RPC);
2234 if (time_uptime < nlm_grace_threshold) {
2235 result->stat.stat = nlm4_denied_grace_period;
2239 error = nlm_get_vfs_state(host, rqstp, &fh, &vs, (accmode_t)0);
2241 result->stat.stat = nlm_convert_error(error);
2245 fl.l_start = argp->alock.l_offset;
2246 fl.l_len = argp->alock.l_len;
2247 fl.l_pid = argp->alock.svid;
2249 fl.l_whence = SEEK_SET;
2250 fl.l_type = F_UNLCK;
2251 error = VOP_ADVLOCK(vs.vs_vp, NULL, F_UNLCK, &fl, F_REMOTE);
2254 * Ignore the error - there is no result code for failure,
2255 * only for grace period.
2257 result->stat.stat = nlm4_granted;
2260 nlm_release_vfs_state(&vs);
2262 *rpcp = nlm_host_get_rpc(host, TRUE);
2263 nlm_host_release(host);
2268 nlm_do_granted(nlm4_testargs *argp, nlm4_res *result, struct svc_req *rqstp,
2272 struct nlm_host *host;
2273 struct nlm_waiting_lock *nw;
2275 memset(result, 0, sizeof(*result));
2277 host = nlm_find_host_by_addr(svc_getrpccaller(rqstp), rqstp->rq_vers);
2279 result->stat.stat = nlm4_denied_nolocks;
2283 nlm_copy_netobj(&result->cookie, &argp->cookie, M_RPC);
2284 result->stat.stat = nlm4_denied;
2285 KFAIL_POINT_CODE(DEBUG_FP, nlm_deny_grant, goto out);
2287 mtx_lock(&nlm_global_lock);
2288 TAILQ_FOREACH(nw, &nlm_waiting_locks, nw_link) {
2289 if (!nw->nw_waiting)
2291 if (argp->alock.svid == nw->nw_lock.svid
2292 && argp->alock.l_offset == nw->nw_lock.l_offset
2293 && argp->alock.l_len == nw->nw_lock.l_len
2294 && argp->alock.fh.n_len == nw->nw_lock.fh.n_len
2295 && !memcmp(argp->alock.fh.n_bytes, nw->nw_lock.fh.n_bytes,
2296 nw->nw_lock.fh.n_len)) {
2297 nw->nw_waiting = FALSE;
2299 result->stat.stat = nlm4_granted;
2303 mtx_unlock(&nlm_global_lock);
2307 *rpcp = nlm_host_get_rpc(host, TRUE);
2308 nlm_host_release(host);
2313 nlm_do_granted_res(nlm4_res *argp, struct svc_req *rqstp)
2315 struct nlm_host *host = NULL;
2316 struct nlm_async_lock *af = NULL;
2319 if (argp->cookie.n_len != sizeof(struct nlm_grantcookie)) {
2320 NLM_DEBUG(1, "NLM: bogus grant cookie");
2324 host = nlm_find_host_by_sysid(ng_sysid(&argp->cookie));
2326 NLM_DEBUG(1, "NLM: Unknown host rejected our grant");
2330 mtx_lock(&host->nh_lock);
2331 TAILQ_FOREACH(af, &host->nh_granted, af_link)
2332 if (ng_cookie(&argp->cookie) ==
2333 ng_cookie(&af->af_granted.cookie))
2336 TAILQ_REMOVE(&host->nh_granted, af, af_link);
2337 mtx_unlock(&host->nh_lock);
2340 NLM_DEBUG(1, "NLM: host %s (sysid %d) replied to our grant "
2341 "with unrecognized cookie %d:%d", host->nh_caller_name,
2342 host->nh_sysid, ng_sysid(&argp->cookie),
2343 ng_cookie(&argp->cookie));
2347 if (argp->stat.stat != nlm4_granted) {
2348 af->af_fl.l_type = F_UNLCK;
2349 error = VOP_ADVLOCK(af->af_vp, NULL, F_UNLCK, &af->af_fl, F_REMOTE);
2351 NLM_DEBUG(1, "NLM: host %s (sysid %d) rejected our grant "
2352 "and we failed to unlock (%d)", host->nh_caller_name,
2353 host->nh_sysid, error);
2357 NLM_DEBUG(5, "NLM: async lock %p rejected by host %s (sysid %d)",
2358 af, host->nh_caller_name, host->nh_sysid);
2360 NLM_DEBUG(5, "NLM: async lock %p accepted by host %s (sysid %d)",
2361 af, host->nh_caller_name, host->nh_sysid);
2366 nlm_free_async_lock(af);
2368 nlm_host_release(host);
2372 nlm_do_free_all(nlm4_notify *argp)
2374 struct nlm_host *host, *thost;
2376 TAILQ_FOREACH_SAFE(host, &nlm_hosts, nh_link, thost) {
2377 if (!strcmp(host->nh_caller_name, argp->name))
2378 nlm_host_notify(host, argp->state);
2383 * Kernel module glue
2386 nfslockd_modevent(module_t mod, int type, void *data)
2391 return (nlm_init());
2395 /* The NLM module cannot be safely unloaded. */
2398 return (EOPNOTSUPP);
2401 static moduledata_t nfslockd_mod = {
2406 DECLARE_MODULE(nfslockd, nfslockd_mod, SI_SUB_VFS, SI_ORDER_ANY);
2408 /* So that loader and kldload(2) can find us, wherever we are.. */
2409 MODULE_DEPEND(nfslockd, xdr, 1, 1, 1);
2410 MODULE_DEPEND(nfslockd, krpc, 1, 1, 1);
2411 MODULE_DEPEND(nfslockd, nfscommon, 1, 1, 1);
2412 MODULE_VERSION(nfslockd, 1);