2 * Copyright (c) 2008 Isilon Inc http://www.isilon.com/
3 * Authors: Doug Rabson <dfr@rabson.org>
4 * Developed with Red Inc: Alfred Perlstein <alfred@freebsd.org>
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 #include "opt_inet6.h"
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
33 #include <sys/param.h>
34 #include <sys/fcntl.h>
35 #include <sys/kernel.h>
36 #include <sys/kthread.h>
37 #include <sys/lockf.h>
38 #include <sys/malloc.h>
39 #include <sys/mount.h>
40 #if __FreeBSD_version >= 700000
44 #include <sys/socket.h>
45 #include <sys/socketvar.h>
46 #include <sys/syscall.h>
47 #include <sys/sysctl.h>
48 #include <sys/sysent.h>
49 #include <sys/syslog.h>
50 #include <sys/sysproto.h>
51 #include <sys/systm.h>
52 #include <sys/taskqueue.h>
53 #include <sys/unistd.h>
54 #include <sys/vnode.h>
56 #include <nfs/nfsproto.h>
57 #include <nfs/nfs_lock.h>
59 #include <nlm/nlm_prot.h>
60 #include <nlm/sm_inter.h>
62 #include <rpc/rpc_com.h>
63 #include <rpc/rpcb_prot.h>
65 MALLOC_DEFINE(M_NLM, "NLM", "Network Lock Manager");
68 * If a host is inactive (and holds no locks) for this amount of
69 * seconds, we consider it idle and stop tracking it.
71 #define NLM_IDLE_TIMEOUT 30
74 * We check the host list for idle every few seconds.
76 #define NLM_IDLE_PERIOD 5
79 * Support for sysctl vfs.nlm.sysid
81 SYSCTL_NODE(_vfs, OID_AUTO, nlm, CTLFLAG_RW, NULL, "Network Lock Manager");
82 SYSCTL_NODE(_vfs_nlm, OID_AUTO, sysid, CTLFLAG_RW, NULL, "");
87 static int nlm_syscall_offset = SYS_nlm_syscall;
88 static struct sysent nlm_syscall_prev_sysent;
89 #if __FreeBSD_version < 700000
90 static struct sysent nlm_syscall_sysent = {
91 (sizeof(struct nlm_syscall_args) / sizeof(register_t)) | SYF_MPSAFE,
92 (sy_call_t *) nlm_syscall
95 MAKE_SYSENT(nlm_syscall);
97 static bool_t nlm_syscall_registered = FALSE;
100 * Debug level passed in from userland. We also support a sysctl hook
101 * so that it can be changed on a live system.
103 static int nlm_debug_level;
104 SYSCTL_INT(_debug, OID_AUTO, nlm_debug, CTLFLAG_RW, &nlm_debug_level, 0, "");
106 #define NLM_DEBUG(_level, args...) \
108 if (nlm_debug_level >= (_level)) \
109 log(LOG_DEBUG, args); \
111 #define NLM_ERR(args...) \
113 log(LOG_ERR, args); \
117 * Grace period handling. The value of nlm_grace_threshold is the
118 * value of time_uptime after which we are serving requests normally.
120 static time_t nlm_grace_threshold;
123 * We check for idle hosts if time_uptime is greater than
124 * nlm_next_idle_check,
126 static time_t nlm_next_idle_check;
129 * A socket to use for RPC - shared by all IPv4 RPC clients.
131 static struct socket *nlm_socket;
136 * A socket to use for RPC - shared by all IPv6 RPC clients.
138 static struct socket *nlm_socket6;
143 * An RPC client handle that can be used to communicate with the local
146 static CLIENT *nlm_nsm;
149 * An AUTH handle for the server's creds.
151 static AUTH *nlm_auth;
154 * A zero timeval for sending async RPC messages.
156 struct timeval nlm_zero_tv = { 0, 0 };
159 * The local NSM state number
165 * A lock to protect the host list and waiting lock list.
167 static struct mtx nlm_global_lock;
171 * (l) locked by nh_lock
172 * (s) only accessed via server RPC which is single threaded
173 * (g) locked by nlm_global_lock
174 * (c) const until freeing
175 * (a) modified using atomic ops
179 * A pending client-side lock request, stored on the nlm_waiting_locks
182 struct nlm_waiting_lock {
183 TAILQ_ENTRY(nlm_waiting_lock) nw_link; /* (g) */
184 bool_t nw_waiting; /* (g) */
185 nlm4_lock nw_lock; /* (c) */
186 union nfsfh nw_fh; /* (c) */
187 struct vnode *nw_vp; /* (c) */
189 TAILQ_HEAD(nlm_waiting_lock_list, nlm_waiting_lock);
191 struct nlm_waiting_lock_list nlm_waiting_locks; /* (g) */
194 * A pending server-side asynchronous lock request, stored on the
195 * nh_pending list of the NLM host.
197 struct nlm_async_lock {
198 TAILQ_ENTRY(nlm_async_lock) af_link; /* (l) host's list of locks */
199 struct task af_task; /* (c) async callback details */
200 void *af_cookie; /* (l) lock manager cancel token */
201 struct vnode *af_vp; /* (l) vnode to lock */
202 struct flock af_fl; /* (c) lock details */
203 struct nlm_host *af_host; /* (c) host which is locking */
204 CLIENT *af_rpc; /* (c) rpc client to send message */
205 nlm4_testargs af_granted; /* (c) notification details */
207 TAILQ_HEAD(nlm_async_lock_list, nlm_async_lock);
212 enum nlm_host_state {
220 CLIENT *nr_client; /* (l) RPC client handle */
221 time_t nr_create_time; /* (l) when client was created */
226 volatile u_int nh_refs; /* (a) reference count */
227 TAILQ_ENTRY(nlm_host) nh_link; /* (g) global list of hosts */
228 char nh_caller_name[MAXNAMELEN]; /* (c) printable name of host */
229 uint32_t nh_sysid; /* (c) our allocaed system ID */
230 char nh_sysid_string[10]; /* (c) string rep. of sysid */
231 struct sockaddr_storage nh_addr; /* (s) remote address of host */
232 struct nlm_rpc nh_srvrpc; /* (l) RPC for server replies */
233 struct nlm_rpc nh_clntrpc; /* (l) RPC for client requests */
234 rpcvers_t nh_vers; /* (s) NLM version of host */
235 int nh_state; /* (s) last seen NSM state of host */
236 enum nlm_host_state nh_monstate; /* (l) local NSM monitoring state */
237 time_t nh_idle_timeout; /* (s) Time at which host is idle */
238 struct sysctl_ctx_list nh_sysctl; /* (c) vfs.nlm.sysid nodes */
239 struct nlm_async_lock_list nh_pending; /* (l) pending async locks */
240 struct nlm_async_lock_list nh_finished; /* (l) finished async locks */
242 TAILQ_HEAD(nlm_host_list, nlm_host);
244 static struct nlm_host_list nlm_hosts; /* (g) */
245 static uint32_t nlm_next_sysid = 1; /* (g) */
247 static void nlm_host_unmonitor(struct nlm_host *);
249 /**********************************************************************/
252 * Initialise NLM globals.
255 nlm_init(void *dummy)
259 mtx_init(&nlm_global_lock, "nlm_global_lock", NULL, MTX_DEF);
260 TAILQ_INIT(&nlm_waiting_locks);
261 TAILQ_INIT(&nlm_hosts);
263 error = syscall_register(&nlm_syscall_offset, &nlm_syscall_sysent,
264 &nlm_syscall_prev_sysent);
266 NLM_ERR("Can't register NLM syscall\n");
268 nlm_syscall_registered = TRUE;
270 SYSINIT(nlm_init, SI_SUB_LOCK, SI_ORDER_FIRST, nlm_init, NULL);
273 nlm_uninit(void *dummy)
276 if (nlm_syscall_registered)
277 syscall_deregister(&nlm_syscall_offset,
278 &nlm_syscall_prev_sysent);
280 SYSUNINIT(nlm_uninit, SI_SUB_LOCK, SI_ORDER_FIRST, nlm_uninit, NULL);
283 * Copy a struct netobj.
286 nlm_copy_netobj(struct netobj *dst, struct netobj *src,
287 struct malloc_type *type)
290 dst->n_len = src->n_len;
291 dst->n_bytes = malloc(src->n_len, type, M_WAITOK);
292 memcpy(dst->n_bytes, src->n_bytes, src->n_len);
296 * Create an RPC client handle for the given (address,prog,vers)
300 nlm_get_rpc(struct sockaddr *sa, rpcprog_t prog, rpcvers_t vers)
302 char *wchan = "nlmrcv";
303 const char* protofmly;
304 struct sockaddr_storage ss;
310 enum clnt_stat stat = RPC_SUCCESS;
311 int rpcvers = RPCBVERS4;
312 bool_t do_tcp = FALSE;
313 bool_t tryagain = FALSE;
314 struct portmap mapping;
318 * First we need to contact the remote RPCBIND service to find
321 memcpy(&ss, sa, sa->sa_len);
322 switch (ss.ss_family) {
324 ((struct sockaddr_in *)&ss)->sin_port = htons(111);
331 ((struct sockaddr_in6 *)&ss)->sin6_port = htons(111);
339 * Unsupported address family - fail.
344 rpcb = clnt_dg_create(so, (struct sockaddr *)&ss,
345 RPCBPROG, rpcvers, 0, 0);
353 parms.r_netid = "tcp";
355 parms.r_netid = "udp";
360 * Use the default timeout.
369 * Try RPCBIND 4 then 3.
372 stat = CLNT_CALL(rpcb, (rpcprog_t) RPCBPROC_GETADDR,
373 (xdrproc_t) xdr_rpcb, &parms,
374 (xdrproc_t) xdr_wrapstring, &uaddr, timo);
375 if (stat == RPC_SUCCESS) {
377 * We have a reply from the remote RPCBIND - turn it
378 * into an appropriate address and make a new client
379 * that can talk to the remote NLM.
381 * XXX fixup IPv6 scope ID.
384 a = __rpc_uaddr2taddr_af(ss.ss_family, uaddr);
389 memcpy(&ss, a->buf, a->len);
392 xdr_free((xdrproc_t) xdr_wrapstring, &uaddr);
395 if (tryagain || stat == RPC_PROGVERSMISMATCH) {
396 if (rpcvers == RPCBVERS4)
398 else if (rpcvers == RPCBVERS)
400 CLNT_CONTROL(rpcb, CLSET_VERS, &rpcvers);
408 mapping.pm_prog = parms.r_prog;
409 mapping.pm_vers = parms.r_vers;
410 mapping.pm_prot = do_tcp ? IPPROTO_TCP : IPPROTO_UDP;
413 stat = CLNT_CALL(rpcb, (rpcprog_t) PMAPPROC_GETPORT,
414 (xdrproc_t) xdr_portmap, &mapping,
415 (xdrproc_t) xdr_u_short, &port, timo);
417 if (stat == RPC_SUCCESS) {
418 switch (ss.ss_family) {
420 ((struct sockaddr_in *)&ss)->sin_port =
426 ((struct sockaddr_in6 *)&ss)->sin6_port =
434 panic("invalid rpcvers %d", rpcvers);
437 * We may have a positive response from the portmapper, but the NLM
438 * service was not found. Make sure we received a valid port.
440 switch (ss.ss_family) {
442 port = ((struct sockaddr_in *)&ss)->sin_port;
446 port = ((struct sockaddr_in6 *)&ss)->sin6_port;
450 if (stat != RPC_SUCCESS || !port) {
452 * If we were able to talk to rpcbind or portmap, but the udp
453 * variant wasn't available, ask about tcp.
455 * XXX - We could also check for a TCP portmapper, but
456 * if the host is running a portmapper at all, we should be able
457 * to hail it over UDP.
459 if (stat == RPC_SUCCESS && !do_tcp) {
464 /* Otherwise, bad news. */
465 NLM_ERR("NLM: failed to contact remote rpcbind, "
466 "stat = %d, port = %d\n", (int) stat, port);
473 * Destroy the UDP client we used to speak to rpcbind and
474 * recreate as a TCP client.
476 struct netconfig *nconf = NULL;
480 switch (ss.ss_family) {
482 nconf = getnetconfigent("tcp");
486 nconf = getnetconfigent("tcp6");
491 rpcb = clnt_reconnect_create(nconf, (struct sockaddr *)&ss,
493 CLNT_CONTROL(rpcb, CLSET_WAITCHAN, wchan);
494 rpcb->cl_auth = nlm_auth;
498 * Re-use the client we used to speak to rpcbind.
500 CLNT_CONTROL(rpcb, CLSET_SVC_ADDR, &ss);
501 CLNT_CONTROL(rpcb, CLSET_PROG, &prog);
502 CLNT_CONTROL(rpcb, CLSET_VERS, &vers);
503 CLNT_CONTROL(rpcb, CLSET_WAITCHAN, wchan);
504 rpcb->cl_auth = nlm_auth;
511 * This async callback after when an async lock request has been
512 * granted. We notify the host which initiated the request.
515 nlm_lock_callback(void *arg, int pending)
517 struct nlm_async_lock *af = (struct nlm_async_lock *) arg;
518 struct rpc_callextra ext;
520 NLM_DEBUG(2, "NLM: async lock %p for %s (sysid %d) granted\n",
521 af, af->af_host->nh_caller_name, af->af_host->nh_sysid);
524 * Send the results back to the host.
526 * Note: there is a possible race here with nlm_host_notify
527 * destroying the RPC client. To avoid problems, the first
528 * thing nlm_host_notify does is to cancel pending async lock
531 memset(&ext, 0, sizeof(ext));
532 ext.rc_auth = nlm_auth;
533 if (af->af_host->nh_vers == NLM_VERS4) {
534 nlm4_granted_msg_4(&af->af_granted,
535 NULL, af->af_rpc, &ext, nlm_zero_tv);
538 * Back-convert to legacy protocol
540 nlm_testargs granted;
541 granted.cookie = af->af_granted.cookie;
542 granted.exclusive = af->af_granted.exclusive;
543 granted.alock.caller_name =
544 af->af_granted.alock.caller_name;
545 granted.alock.fh = af->af_granted.alock.fh;
546 granted.alock.oh = af->af_granted.alock.oh;
547 granted.alock.svid = af->af_granted.alock.svid;
548 granted.alock.l_offset =
549 af->af_granted.alock.l_offset;
550 granted.alock.l_len =
551 af->af_granted.alock.l_len;
553 nlm_granted_msg_1(&granted,
554 NULL, af->af_rpc, &ext, nlm_zero_tv);
558 * Move this entry to the nh_finished list. Someone else will
559 * free it later - its too hard to do it here safely without
560 * racing with cancel.
562 * XXX possibly we should have a third "granted sent but not
563 * ack'ed" list so that we can re-send the granted message.
565 mtx_lock(&af->af_host->nh_lock);
566 TAILQ_REMOVE(&af->af_host->nh_pending, af, af_link);
567 TAILQ_INSERT_TAIL(&af->af_host->nh_finished, af, af_link);
568 mtx_unlock(&af->af_host->nh_lock);
572 * Free an async lock request. The request must have been removed from
576 nlm_free_async_lock(struct nlm_async_lock *af)
579 * Free an async lock.
582 CLNT_RELEASE(af->af_rpc);
583 xdr_free((xdrproc_t) xdr_nlm4_testargs, &af->af_granted);
590 * Cancel our async request - this must be called with
591 * af->nh_host->nh_lock held. This is slightly complicated by a
592 * potential race with our own callback. If we fail to cancel the
593 * lock, it must already have been granted - we make sure our async
594 * task has completed by calling taskqueue_drain in this case.
597 nlm_cancel_async_lock(struct nlm_async_lock *af)
599 struct nlm_host *host = af->af_host;
602 mtx_assert(&host->nh_lock, MA_OWNED);
604 mtx_unlock(&host->nh_lock);
606 error = VOP_ADVLOCKASYNC(af->af_vp, NULL, F_CANCEL, &af->af_fl,
607 F_REMOTE, NULL, &af->af_cookie);
611 * We failed to cancel - make sure our callback has
612 * completed before we continue.
614 taskqueue_drain(taskqueue_thread, &af->af_task);
617 mtx_lock(&host->nh_lock);
620 NLM_DEBUG(2, "NLM: async lock %p for %s (sysid %d) "
621 "cancelled\n", af, host->nh_caller_name, host->nh_sysid);
624 * Remove from the nh_pending list and free now that
625 * we are safe from the callback.
627 TAILQ_REMOVE(&host->nh_pending, af, af_link);
628 mtx_unlock(&host->nh_lock);
629 nlm_free_async_lock(af);
630 mtx_lock(&host->nh_lock);
637 nlm_free_finished_locks(struct nlm_host *host)
639 struct nlm_async_lock *af;
641 mtx_lock(&host->nh_lock);
642 while ((af = TAILQ_FIRST(&host->nh_finished)) != NULL) {
643 TAILQ_REMOVE(&host->nh_finished, af, af_link);
644 mtx_unlock(&host->nh_lock);
645 nlm_free_async_lock(af);
646 mtx_lock(&host->nh_lock);
648 mtx_unlock(&host->nh_lock);
652 * Free resources used by a host. This is called after the reference
653 * count has reached zero so it doesn't need to worry about locks.
656 nlm_host_destroy(struct nlm_host *host)
659 mtx_lock(&nlm_global_lock);
660 TAILQ_REMOVE(&nlm_hosts, host, nh_link);
661 mtx_unlock(&nlm_global_lock);
663 if (host->nh_srvrpc.nr_client)
664 CLNT_RELEASE(host->nh_srvrpc.nr_client);
665 if (host->nh_clntrpc.nr_client)
666 CLNT_RELEASE(host->nh_clntrpc.nr_client);
667 mtx_destroy(&host->nh_lock);
668 sysctl_ctx_free(&host->nh_sysctl);
673 * Thread start callback for client lock recovery
676 nlm_client_recovery_start(void *arg)
678 struct nlm_host *host = (struct nlm_host *) arg;
680 NLM_DEBUG(1, "NLM: client lock recovery for %s started\n",
681 host->nh_caller_name);
683 nlm_client_recovery(host);
685 NLM_DEBUG(1, "NLM: client lock recovery for %s completed\n",
686 host->nh_caller_name);
688 host->nh_monstate = NLM_MONITORED;
689 nlm_host_release(host);
695 * This is called when we receive a host state change notification. We
696 * unlock any active locks owned by the host. When rpc.lockd is
697 * shutting down, this function is called with newstate set to zero
698 * which allows us to cancel any pending async locks and clear the
702 nlm_host_notify(struct nlm_host *host, int newstate)
704 struct nlm_async_lock *af;
707 NLM_DEBUG(1, "NLM: host %s (sysid %d) rebooted, new "
708 "state is %d\n", host->nh_caller_name,
709 host->nh_sysid, newstate);
713 * Cancel any pending async locks for this host.
715 mtx_lock(&host->nh_lock);
716 while ((af = TAILQ_FIRST(&host->nh_pending)) != NULL) {
718 * nlm_cancel_async_lock will remove the entry from
719 * nh_pending and free it.
721 nlm_cancel_async_lock(af);
723 mtx_unlock(&host->nh_lock);
724 nlm_free_finished_locks(host);
727 * The host just rebooted - trash its locks.
729 lf_clearremotesys(host->nh_sysid);
730 host->nh_state = newstate;
733 * If we have any remote locks for this host (i.e. it
734 * represents a remote NFS server that our local NFS client
735 * has locks for), start a recovery thread.
738 && host->nh_monstate != NLM_RECOVERING
739 && lf_countlocks(NLM_SYSID_CLIENT | host->nh_sysid) > 0) {
741 host->nh_monstate = NLM_RECOVERING;
742 refcount_acquire(&host->nh_refs);
743 kthread_add(nlm_client_recovery_start, host, curproc, &td, 0, 0,
744 "NFS lock recovery for %s", host->nh_caller_name);
749 * Sysctl handler to count the number of locks for a sysid.
752 nlm_host_lock_count_sysctl(SYSCTL_HANDLER_ARGS)
754 struct nlm_host *host;
757 host = oidp->oid_arg1;
758 count = lf_countlocks(host->nh_sysid);
759 return sysctl_handle_int(oidp, &count, 0, req);
763 * Sysctl handler to count the number of client locks for a sysid.
766 nlm_host_client_lock_count_sysctl(SYSCTL_HANDLER_ARGS)
768 struct nlm_host *host;
771 host = oidp->oid_arg1;
772 count = lf_countlocks(NLM_SYSID_CLIENT | host->nh_sysid);
773 return sysctl_handle_int(oidp, &count, 0, req);
777 * Create a new NLM host.
779 static struct nlm_host *
780 nlm_create_host(const char* caller_name)
782 struct nlm_host *host;
783 struct sysctl_oid *oid;
785 mtx_assert(&nlm_global_lock, MA_OWNED);
787 NLM_DEBUG(1, "NLM: new host %s (sysid %d)\n",
788 caller_name, nlm_next_sysid);
789 host = malloc(sizeof(struct nlm_host), M_NLM, M_NOWAIT|M_ZERO);
792 mtx_init(&host->nh_lock, "nh_lock", NULL, MTX_DEF);
794 strlcpy(host->nh_caller_name, caller_name, MAXNAMELEN);
795 host->nh_sysid = nlm_next_sysid++;
796 snprintf(host->nh_sysid_string, sizeof(host->nh_sysid_string),
797 "%d", host->nh_sysid);
800 host->nh_monstate = NLM_UNMONITORED;
801 TAILQ_INIT(&host->nh_pending);
802 TAILQ_INIT(&host->nh_finished);
803 TAILQ_INSERT_TAIL(&nlm_hosts, host, nh_link);
805 mtx_unlock(&nlm_global_lock);
807 sysctl_ctx_init(&host->nh_sysctl);
808 oid = SYSCTL_ADD_NODE(&host->nh_sysctl,
809 SYSCTL_STATIC_CHILDREN(_vfs_nlm_sysid),
810 OID_AUTO, host->nh_sysid_string, CTLFLAG_RD, NULL, "");
811 SYSCTL_ADD_STRING(&host->nh_sysctl, SYSCTL_CHILDREN(oid), OID_AUTO,
812 "hostname", CTLFLAG_RD, host->nh_caller_name, 0, "");
813 SYSCTL_ADD_INT(&host->nh_sysctl, SYSCTL_CHILDREN(oid), OID_AUTO,
814 "version", CTLFLAG_RD, &host->nh_vers, 0, "");
815 SYSCTL_ADD_INT(&host->nh_sysctl, SYSCTL_CHILDREN(oid), OID_AUTO,
816 "monitored", CTLFLAG_RD, &host->nh_monstate, 0, "");
817 SYSCTL_ADD_PROC(&host->nh_sysctl, SYSCTL_CHILDREN(oid), OID_AUTO,
818 "lock_count", CTLTYPE_INT | CTLFLAG_RD, host, 0,
819 nlm_host_lock_count_sysctl, "I", "");
820 SYSCTL_ADD_PROC(&host->nh_sysctl, SYSCTL_CHILDREN(oid), OID_AUTO,
821 "client_lock_count", CTLTYPE_INT | CTLFLAG_RD, host, 0,
822 nlm_host_client_lock_count_sysctl, "I", "");
824 mtx_lock(&nlm_global_lock);
830 * Acquire the next sysid for remote locks not handled by the NLM.
833 nlm_acquire_next_sysid(void)
837 mtx_lock(&nlm_global_lock);
838 next_sysid = nlm_next_sysid++;
839 mtx_unlock(&nlm_global_lock);
844 * Return non-zero if the address parts of the two sockaddrs are the
848 nlm_compare_addr(const struct sockaddr *a, const struct sockaddr *b)
850 const struct sockaddr_in *a4, *b4;
852 const struct sockaddr_in6 *a6, *b6;
855 if (a->sa_family != b->sa_family)
858 switch (a->sa_family) {
860 a4 = (const struct sockaddr_in *) a;
861 b4 = (const struct sockaddr_in *) b;
862 return !memcmp(&a4->sin_addr, &b4->sin_addr,
863 sizeof(a4->sin_addr));
866 a6 = (const struct sockaddr_in6 *) a;
867 b6 = (const struct sockaddr_in6 *) b;
868 return !memcmp(&a6->sin6_addr, &b6->sin6_addr,
869 sizeof(a6->sin6_addr));
877 * Check for idle hosts and stop monitoring them. We could also free
878 * the host structure here, possibly after a larger timeout but that
879 * would require some care to avoid races with
880 * e.g. nlm_host_lock_count_sysctl.
885 struct nlm_host *host;
887 mtx_assert(&nlm_global_lock, MA_OWNED);
889 if (time_uptime <= nlm_next_idle_check)
892 nlm_next_idle_check = time_uptime + NLM_IDLE_PERIOD;
894 TAILQ_FOREACH(host, &nlm_hosts, nh_link) {
895 if (host->nh_monstate == NLM_MONITORED
896 && time_uptime > host->nh_idle_timeout) {
897 mtx_unlock(&nlm_global_lock);
898 if (lf_countlocks(host->nh_sysid) > 0
899 || lf_countlocks(NLM_SYSID_CLIENT
901 host->nh_idle_timeout =
902 time_uptime + NLM_IDLE_TIMEOUT;
903 mtx_lock(&nlm_global_lock);
906 nlm_host_unmonitor(host);
907 mtx_lock(&nlm_global_lock);
913 * Search for an existing NLM host that matches the given name
914 * (typically the caller_name element of an nlm4_lock). If none is
915 * found, create a new host. If 'addr' is non-NULL, record the remote
916 * address of the host so that we can call it back for async
917 * responses. If 'vers' is greater than zero then record the NLM
918 * program version to use to communicate with this client.
921 nlm_find_host_by_name(const char *name, const struct sockaddr *addr,
924 struct nlm_host *host;
926 mtx_lock(&nlm_global_lock);
929 * The remote host is determined by caller_name.
931 TAILQ_FOREACH(host, &nlm_hosts, nh_link) {
932 if (!strcmp(host->nh_caller_name, name))
937 host = nlm_create_host(name);
939 mtx_unlock(&nlm_global_lock);
943 refcount_acquire(&host->nh_refs);
945 host->nh_idle_timeout = time_uptime + NLM_IDLE_TIMEOUT;
948 * If we have an address for the host, record it so that we
949 * can send async replies etc.
953 KASSERT(addr->sa_len < sizeof(struct sockaddr_storage),
954 ("Strange remote transport address length"));
957 * If we have seen an address before and we currently
958 * have an RPC client handle, make sure the address is
959 * the same, otherwise discard the client handle.
961 if (host->nh_addr.ss_len && host->nh_srvrpc.nr_client) {
962 if (!nlm_compare_addr(
963 (struct sockaddr *) &host->nh_addr,
965 || host->nh_vers != vers) {
967 mtx_lock(&host->nh_lock);
968 client = host->nh_srvrpc.nr_client;
969 host->nh_srvrpc.nr_client = NULL;
970 mtx_unlock(&host->nh_lock);
972 CLNT_RELEASE(client);
976 memcpy(&host->nh_addr, addr, addr->sa_len);
977 host->nh_vers = vers;
982 mtx_unlock(&nlm_global_lock);
988 * Search for an existing NLM host that matches the given remote
989 * address. If none is found, create a new host with the requested
990 * address and remember 'vers' as the NLM protocol version to use for
994 nlm_find_host_by_addr(const struct sockaddr *addr, int vers)
997 * Fake up a name using inet_ntop. This buffer is
998 * large enough for an IPv6 address.
1000 char tmp[sizeof "ffff:ffff:ffff:ffff:ffff:ffff:255.255.255.255"];
1001 struct nlm_host *host;
1003 switch (addr->sa_family) {
1006 &((const struct sockaddr_in *) addr)->sin_addr,
1012 &((const struct sockaddr_in6 *) addr)->sin6_addr,
1017 strlcpy(tmp, "<unknown>", sizeof(tmp));
1021 mtx_lock(&nlm_global_lock);
1024 * The remote host is determined by caller_name.
1026 TAILQ_FOREACH(host, &nlm_hosts, nh_link) {
1027 if (nlm_compare_addr(addr,
1028 (const struct sockaddr *) &host->nh_addr))
1033 host = nlm_create_host(tmp);
1035 mtx_unlock(&nlm_global_lock);
1038 memcpy(&host->nh_addr, addr, addr->sa_len);
1039 host->nh_vers = vers;
1041 refcount_acquire(&host->nh_refs);
1043 host->nh_idle_timeout = time_uptime + NLM_IDLE_TIMEOUT;
1047 mtx_unlock(&nlm_global_lock);
1053 * Find the NLM host that matches the value of 'sysid'. If none
1054 * exists, return NULL.
1056 static struct nlm_host *
1057 nlm_find_host_by_sysid(int sysid)
1059 struct nlm_host *host;
1061 TAILQ_FOREACH(host, &nlm_hosts, nh_link) {
1062 if (host->nh_sysid == sysid) {
1063 refcount_acquire(&host->nh_refs);
1071 void nlm_host_release(struct nlm_host *host)
1073 if (refcount_release(&host->nh_refs)) {
1077 nlm_host_destroy(host);
1082 * Unregister this NLM host with the local NSM due to idleness.
1085 nlm_host_unmonitor(struct nlm_host *host)
1089 struct timeval timo;
1090 enum clnt_stat stat;
1092 NLM_DEBUG(1, "NLM: unmonitoring %s (sysid %d)\n",
1093 host->nh_caller_name, host->nh_sysid);
1096 * We put our assigned system ID value in the priv field to
1097 * make it simpler to find the host if we are notified of a
1100 smmonid.mon_name = host->nh_caller_name;
1101 smmonid.my_id.my_name = "localhost";
1102 smmonid.my_id.my_prog = NLM_PROG;
1103 smmonid.my_id.my_vers = NLM_SM;
1104 smmonid.my_id.my_proc = NLM_SM_NOTIFY;
1108 stat = CLNT_CALL(nlm_nsm, SM_UNMON,
1109 (xdrproc_t) xdr_mon, &smmonid,
1110 (xdrproc_t) xdr_sm_stat, &smstat, timo);
1112 if (stat != RPC_SUCCESS) {
1113 NLM_ERR("Failed to contact local NSM - rpc error %d\n", stat);
1116 if (smstat.res_stat == stat_fail) {
1117 NLM_ERR("Local NSM refuses to unmonitor %s\n",
1118 host->nh_caller_name);
1122 host->nh_monstate = NLM_UNMONITORED;
1126 * Register this NLM host with the local NSM so that we can be
1127 * notified if it reboots.
1130 nlm_host_monitor(struct nlm_host *host, int state)
1134 struct timeval timo;
1135 enum clnt_stat stat;
1137 if (state && !host->nh_state) {
1139 * This is the first time we have seen an NSM state
1140 * value for this host. We record it here to help
1141 * detect host reboots.
1143 host->nh_state = state;
1144 NLM_DEBUG(1, "NLM: host %s (sysid %d) has NSM state %d\n",
1145 host->nh_caller_name, host->nh_sysid, state);
1148 mtx_lock(&host->nh_lock);
1149 if (host->nh_monstate != NLM_UNMONITORED) {
1150 mtx_unlock(&host->nh_lock);
1153 host->nh_monstate = NLM_MONITORED;
1154 mtx_unlock(&host->nh_lock);
1156 NLM_DEBUG(1, "NLM: monitoring %s (sysid %d)\n",
1157 host->nh_caller_name, host->nh_sysid);
1160 * We put our assigned system ID value in the priv field to
1161 * make it simpler to find the host if we are notified of a
1164 smmon.mon_id.mon_name = host->nh_caller_name;
1165 smmon.mon_id.my_id.my_name = "localhost";
1166 smmon.mon_id.my_id.my_prog = NLM_PROG;
1167 smmon.mon_id.my_id.my_vers = NLM_SM;
1168 smmon.mon_id.my_id.my_proc = NLM_SM_NOTIFY;
1169 memcpy(smmon.priv, &host->nh_sysid, sizeof(host->nh_sysid));
1173 stat = CLNT_CALL(nlm_nsm, SM_MON,
1174 (xdrproc_t) xdr_mon, &smmon,
1175 (xdrproc_t) xdr_sm_stat, &smstat, timo);
1177 if (stat != RPC_SUCCESS) {
1178 NLM_ERR("Failed to contact local NSM - rpc error %d\n", stat);
1181 if (smstat.res_stat == stat_fail) {
1182 NLM_ERR("Local NSM refuses to monitor %s\n",
1183 host->nh_caller_name);
1184 mtx_lock(&host->nh_lock);
1185 host->nh_monstate = NLM_MONITOR_FAILED;
1186 mtx_unlock(&host->nh_lock);
1190 host->nh_monstate = NLM_MONITORED;
1194 * Return an RPC client handle that can be used to talk to the NLM
1195 * running on the given host.
1198 nlm_host_get_rpc(struct nlm_host *host, bool_t isserver)
1200 struct nlm_rpc *rpc;
1203 mtx_lock(&host->nh_lock);
1206 rpc = &host->nh_srvrpc;
1208 rpc = &host->nh_clntrpc;
1211 * We can't hold onto RPC handles for too long - the async
1212 * call/reply protocol used by some NLM clients makes it hard
1213 * to tell when they change port numbers (e.g. after a
1214 * reboot). Note that if a client reboots while it isn't
1215 * holding any locks, it won't bother to notify us. We
1216 * expire the RPC handles after two minutes.
1218 if (rpc->nr_client && time_uptime > rpc->nr_create_time + 2*60) {
1219 client = rpc->nr_client;
1220 rpc->nr_client = NULL;
1221 mtx_unlock(&host->nh_lock);
1222 CLNT_RELEASE(client);
1223 mtx_lock(&host->nh_lock);
1226 if (!rpc->nr_client) {
1227 mtx_unlock(&host->nh_lock);
1228 client = nlm_get_rpc((struct sockaddr *)&host->nh_addr,
1229 NLM_PROG, host->nh_vers);
1230 mtx_lock(&host->nh_lock);
1233 if (rpc->nr_client) {
1234 mtx_unlock(&host->nh_lock);
1235 CLNT_DESTROY(client);
1236 mtx_lock(&host->nh_lock);
1238 rpc->nr_client = client;
1239 rpc->nr_create_time = time_uptime;
1244 client = rpc->nr_client;
1246 CLNT_ACQUIRE(client);
1247 mtx_unlock(&host->nh_lock);
1253 int nlm_host_get_sysid(struct nlm_host *host)
1256 return (host->nh_sysid);
1260 nlm_host_get_state(struct nlm_host *host)
1263 return (host->nh_state);
1267 nlm_register_wait_lock(struct nlm4_lock *lock, struct vnode *vp)
1269 struct nlm_waiting_lock *nw;
1271 nw = malloc(sizeof(struct nlm_waiting_lock), M_NLM, M_WAITOK);
1272 nw->nw_lock = *lock;
1273 memcpy(&nw->nw_fh.fh_bytes, nw->nw_lock.fh.n_bytes,
1274 nw->nw_lock.fh.n_len);
1275 nw->nw_lock.fh.n_bytes = nw->nw_fh.fh_bytes;
1276 nw->nw_waiting = TRUE;
1278 mtx_lock(&nlm_global_lock);
1279 TAILQ_INSERT_TAIL(&nlm_waiting_locks, nw, nw_link);
1280 mtx_unlock(&nlm_global_lock);
1286 nlm_deregister_wait_lock(void *handle)
1288 struct nlm_waiting_lock *nw = handle;
1290 mtx_lock(&nlm_global_lock);
1291 TAILQ_REMOVE(&nlm_waiting_locks, nw, nw_link);
1292 mtx_unlock(&nlm_global_lock);
1298 nlm_wait_lock(void *handle, int timo)
1300 struct nlm_waiting_lock *nw = handle;
1304 * If the granted message arrived before we got here,
1305 * nw->nw_waiting will be FALSE - in that case, don't sleep.
1307 mtx_lock(&nlm_global_lock);
1310 error = msleep(nw, &nlm_global_lock, PCATCH, "nlmlock", timo);
1311 TAILQ_REMOVE(&nlm_waiting_locks, nw, nw_link);
1314 * The granted message may arrive after the
1315 * interrupt/timeout but before we manage to lock the
1316 * mutex. Detect this by examining nw_lock.
1318 if (!nw->nw_waiting)
1322 * If nlm_cancel_wait is called, then error will be
1323 * zero but nw_waiting will still be TRUE. We
1324 * translate this into EINTR.
1329 mtx_unlock(&nlm_global_lock);
1337 nlm_cancel_wait(struct vnode *vp)
1339 struct nlm_waiting_lock *nw;
1341 mtx_lock(&nlm_global_lock);
1342 TAILQ_FOREACH(nw, &nlm_waiting_locks, nw_link) {
1343 if (nw->nw_vp == vp) {
1347 mtx_unlock(&nlm_global_lock);
1351 /**********************************************************************/
1354 * Syscall interface with userland.
1357 extern void nlm_prog_0(struct svc_req *rqstp, SVCXPRT *transp);
1358 extern void nlm_prog_1(struct svc_req *rqstp, SVCXPRT *transp);
1359 extern void nlm_prog_3(struct svc_req *rqstp, SVCXPRT *transp);
1360 extern void nlm_prog_4(struct svc_req *rqstp, SVCXPRT *transp);
1363 nlm_register_services(SVCPOOL *pool, int addr_count, char **addrs)
1365 static rpcvers_t versions[] = {
1366 NLM_SM, NLM_VERS, NLM_VERSX, NLM_VERS4
1368 static void (*dispatchers[])(struct svc_req *, SVCXPRT *) = {
1369 nlm_prog_0, nlm_prog_1, nlm_prog_3, nlm_prog_4
1371 static const int version_count = sizeof(versions) / sizeof(versions[0]);
1376 struct netconfig *nconf;
1380 NLM_ERR("NLM: no service addresses given - can't start server");
1384 xprts = malloc(addr_count * sizeof(SVCXPRT *), M_NLM, M_WAITOK|M_ZERO);
1385 for (i = 0; i < version_count; i++) {
1386 for (j = 0; j < addr_count; j++) {
1388 * Create transports for the first version and
1389 * then just register everything else to the
1395 error = copyin(&addrs[2*j], &up,
1399 error = copyinstr(up, netid, sizeof(netid),
1403 error = copyin(&addrs[2*j+1], &up,
1407 error = copyinstr(up, uaddr, sizeof(uaddr),
1411 nconf = getnetconfigent(netid);
1413 NLM_ERR("Can't lookup netid %s\n",
1418 xprts[j] = svc_tp_create(pool, dispatchers[i],
1419 NLM_PROG, versions[i], uaddr, nconf);
1421 NLM_ERR("NLM: unable to create "
1422 "(NLM_PROG, %d).\n", versions[i]);
1426 freenetconfigent(nconf);
1428 nconf = getnetconfigent(xprts[j]->xp_netid);
1429 rpcb_unset(NLM_PROG, versions[i], nconf);
1430 if (!svc_reg(xprts[j], NLM_PROG, versions[i],
1431 dispatchers[i], nconf)) {
1432 NLM_ERR("NLM: can't register "
1433 "(NLM_PROG, %d)\n", versions[i]);
1442 for (j = 0; j < addr_count; j++) {
1444 SVC_RELEASE(xprts[j]);
1451 * Main server entry point. Contacts the local NSM to get its current
1452 * state and send SM_UNMON_ALL. Registers the NLM services and then
1453 * services requests. Does not return until the server is interrupted
1457 nlm_server_main(int addr_count, char **addrs)
1459 struct thread *td = curthread;
1461 SVCPOOL *pool = NULL;
1465 struct sockaddr_in6 sin6;
1467 struct sockaddr_in sin;
1470 struct timeval timo;
1471 enum clnt_stat stat;
1472 struct nlm_host *host, *nhost;
1473 struct nlm_waiting_lock *nw;
1474 vop_advlock_t *old_nfs_advlock;
1475 vop_reclaim_t *old_nfs_reclaim;
1482 NLM_ERR("NLM: can't start server - "
1483 "it appears to be running already\n");
1487 memset(&opt, 0, sizeof(opt));
1490 error = socreate(AF_INET, &nlm_socket, SOCK_DGRAM, 0,
1493 NLM_ERR("NLM: can't create IPv4 socket - error %d\n", error);
1496 opt.sopt_dir = SOPT_SET;
1497 opt.sopt_level = IPPROTO_IP;
1498 opt.sopt_name = IP_PORTRANGE;
1499 portlow = IP_PORTRANGE_LOW;
1500 opt.sopt_val = &portlow;
1501 opt.sopt_valsize = sizeof(portlow);
1502 sosetopt(nlm_socket, &opt);
1506 error = socreate(AF_INET6, &nlm_socket6, SOCK_DGRAM, 0,
1509 NLM_ERR("NLM: can't create IPv6 socket - error %d\n", error);
1513 opt.sopt_dir = SOPT_SET;
1514 opt.sopt_level = IPPROTO_IPV6;
1515 opt.sopt_name = IPV6_PORTRANGE;
1516 portlow = IPV6_PORTRANGE_LOW;
1517 opt.sopt_val = &portlow;
1518 opt.sopt_valsize = sizeof(portlow);
1519 sosetopt(nlm_socket6, &opt);
1522 nlm_auth = authunix_create(curthread->td_ucred);
1525 memset(&sin6, 0, sizeof(sin6));
1526 sin6.sin6_len = sizeof(sin6);
1527 sin6.sin6_family = AF_INET6;
1528 sin6.sin6_addr = in6addr_loopback;
1529 nlm_nsm = nlm_get_rpc((struct sockaddr *) &sin6, SM_PROG, SM_VERS);
1532 memset(&sin, 0, sizeof(sin));
1533 sin.sin_len = sizeof(sin);
1534 sin.sin_family = AF_INET;
1535 sin.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
1536 nlm_nsm = nlm_get_rpc((struct sockaddr *) &sin, SM_PROG,
1543 NLM_ERR("Can't start NLM - unable to contact NSM\n");
1548 pool = svcpool_create("NLM", NULL);
1550 error = nlm_register_services(pool, addr_count, addrs);
1554 memset(&id, 0, sizeof(id));
1555 id.my_name = "NFS NLM";
1559 stat = CLNT_CALL(nlm_nsm, SM_UNMON_ALL,
1560 (xdrproc_t) xdr_my_id, &id,
1561 (xdrproc_t) xdr_sm_stat, &smstat, timo);
1563 if (stat != RPC_SUCCESS) {
1566 CLNT_GETERR(nlm_nsm, &err);
1567 NLM_ERR("NLM: unexpected error contacting NSM, "
1568 "stat=%d, errno=%d\n", stat, err.re_errno);
1573 NLM_DEBUG(1, "NLM: local NSM state is %d\n", smstat.state);
1574 nlm_nsm_state = smstat.state;
1576 old_nfs_advlock = nfs_advlock_p;
1577 nfs_advlock_p = nlm_advlock;
1578 old_nfs_reclaim = nfs_reclaim_p;
1579 nfs_reclaim_p = nlm_reclaim;
1584 nfs_advlock_p = old_nfs_advlock;
1585 nfs_reclaim_p = old_nfs_reclaim;
1589 svcpool_destroy(pool);
1592 * We are finished communicating with the NSM.
1595 CLNT_RELEASE(nlm_nsm);
1600 * Trash all the existing state so that if the server
1601 * restarts, it gets a clean slate. This is complicated by the
1602 * possibility that there may be other threads trying to make
1603 * client locking requests.
1605 * First we fake a client reboot notification which will
1606 * cancel any pending async locks and purge remote lock state
1607 * from the local lock manager. We release the reference from
1608 * nlm_hosts to the host (which may remove it from the list
1609 * and free it). After this phase, the only entries in the
1610 * nlm_host list should be from other threads performing
1611 * client lock requests. We arrange to defer closing the
1612 * sockets until the last RPC client handle is released.
1618 mtx_lock(&nlm_global_lock);
1619 TAILQ_FOREACH(nw, &nlm_waiting_locks, nw_link) {
1622 TAILQ_FOREACH_SAFE(host, &nlm_hosts, nh_link, nhost) {
1623 mtx_unlock(&nlm_global_lock);
1624 nlm_host_notify(host, 0);
1625 nlm_host_release(host);
1626 mtx_lock(&nlm_global_lock);
1628 TAILQ_FOREACH_SAFE(host, &nlm_hosts, nh_link, nhost) {
1629 mtx_lock(&host->nh_lock);
1630 if (host->nh_srvrpc.nr_client
1631 || host->nh_clntrpc.nr_client) {
1632 if (host->nh_addr.ss_family == AF_INET)
1635 if (host->nh_addr.ss_family == AF_INET6)
1639 * Note that the rpc over udp code copes
1640 * correctly with the fact that a socket may
1641 * be used by many rpc handles.
1643 if (host->nh_srvrpc.nr_client)
1644 CLNT_CONTROL(host->nh_srvrpc.nr_client,
1646 if (host->nh_clntrpc.nr_client)
1647 CLNT_CONTROL(host->nh_clntrpc.nr_client,
1650 mtx_unlock(&host->nh_lock);
1652 mtx_unlock(&nlm_global_lock);
1654 AUTH_DESTROY(nlm_auth);
1657 soclose(nlm_socket);
1661 soclose(nlm_socket6);
1669 nlm_syscall(struct thread *td, struct nlm_syscall_args *uap)
1673 #if __FreeBSD_version >= 700000
1674 error = priv_check(td, PRIV_NFS_LOCKD);
1681 nlm_debug_level = uap->debug_level;
1682 nlm_grace_threshold = time_uptime + uap->grace_period;
1683 nlm_next_idle_check = time_uptime + NLM_IDLE_PERIOD;
1685 return nlm_server_main(uap->addr_count, uap->addrs);
1688 /**********************************************************************/
1691 * NLM implementation details, called from the RPC stubs.
1696 nlm_sm_notify(struct nlm_sm_status *argp)
1699 struct nlm_host *host;
1701 NLM_DEBUG(3, "nlm_sm_notify(): mon_name = %s\n", argp->mon_name);
1702 memcpy(&sysid, &argp->priv, sizeof(sysid));
1703 host = nlm_find_host_by_sysid(sysid);
1705 nlm_host_notify(host, argp->state);
1706 nlm_host_release(host);
1711 nlm_convert_to_fhandle_t(fhandle_t *fhp, struct netobj *p)
1713 memcpy(fhp, p->n_bytes, sizeof(fhandle_t));
1717 struct mount *vs_mp;
1718 struct vnode *vs_vp;
1724 nlm_get_vfs_state(struct nlm_host *host, struct svc_req *rqstp,
1725 fhandle_t *fhp, struct vfs_state *vs)
1728 struct ucred *cred = NULL, *credanon;
1730 memset(vs, 0, sizeof(*vs));
1732 vs->vs_mp = vfs_getvfs(&fhp->fh_fsid);
1736 vs->vs_vfslocked = VFS_LOCK_GIANT(vs->vs_mp);
1738 error = VFS_CHECKEXP(vs->vs_mp, (struct sockaddr *)&host->nh_addr,
1739 &exflags, &credanon, NULL, NULL);
1743 if (exflags & MNT_EXRDONLY || (vs->vs_mp->mnt_flag & MNT_RDONLY)) {
1748 error = VFS_FHTOVP(vs->vs_mp, &fhp->fh_fid, &vs->vs_vp);
1751 vs->vs_vnlocked = TRUE;
1753 if (!svc_getcred(rqstp, &cred, NULL)) {
1757 if (cred->cr_uid == 0 || (exflags & MNT_EXPORTANON)) {
1766 error = VOP_ACCESS(vs->vs_vp, VWRITE, cred, curthread);
1770 #if __FreeBSD_version < 800011
1771 VOP_UNLOCK(vs->vs_vp, 0, curthread);
1773 VOP_UNLOCK(vs->vs_vp, 0);
1775 vs->vs_vnlocked = FALSE;
1787 nlm_release_vfs_state(struct vfs_state *vs)
1791 if (vs->vs_vnlocked)
1798 VFS_UNLOCK_GIANT(vs->vs_vfslocked);
1802 nlm_convert_error(int error)
1805 if (error == ESTALE)
1806 return nlm4_stale_fh;
1807 else if (error == EROFS)
1814 nlm_do_test(nlm4_testargs *argp, nlm4_testres *result, struct svc_req *rqstp,
1818 struct vfs_state vs;
1819 struct nlm_host *host, *bhost;
1823 memset(result, 0, sizeof(*result));
1824 memset(&vs, 0, sizeof(vs));
1826 host = nlm_find_host_by_name(argp->alock.caller_name,
1827 svc_getrpccaller(rqstp), rqstp->rq_vers);
1829 result->stat.stat = nlm4_denied_nolocks;
1833 NLM_DEBUG(3, "nlm_do_test(): caller_name = %s (sysid = %d)\n",
1834 host->nh_caller_name, host->nh_sysid);
1836 nlm_free_finished_locks(host);
1837 sysid = host->nh_sysid;
1839 nlm_convert_to_fhandle_t(&fh, &argp->alock.fh);
1840 nlm_copy_netobj(&result->cookie, &argp->cookie, M_RPC);
1842 if (time_uptime < nlm_grace_threshold) {
1843 result->stat.stat = nlm4_denied_grace_period;
1847 error = nlm_get_vfs_state(host, rqstp, &fh, &vs);
1849 result->stat.stat = nlm_convert_error(error);
1853 fl.l_start = argp->alock.l_offset;
1854 fl.l_len = argp->alock.l_len;
1855 fl.l_pid = argp->alock.svid;
1857 fl.l_whence = SEEK_SET;
1858 if (argp->exclusive)
1859 fl.l_type = F_WRLCK;
1861 fl.l_type = F_RDLCK;
1862 error = VOP_ADVLOCK(vs.vs_vp, NULL, F_GETLK, &fl, F_REMOTE);
1864 result->stat.stat = nlm4_failed;
1868 if (fl.l_type == F_UNLCK) {
1869 result->stat.stat = nlm4_granted;
1871 result->stat.stat = nlm4_denied;
1872 result->stat.nlm4_testrply_u.holder.exclusive =
1873 (fl.l_type == F_WRLCK);
1874 result->stat.nlm4_testrply_u.holder.svid = fl.l_pid;
1875 bhost = nlm_find_host_by_sysid(fl.l_sysid);
1878 * We don't have any useful way of recording
1879 * the value of oh used in the original lock
1880 * request. Ideally, the test reply would have
1881 * a space for the owning host's name allowing
1882 * our caller's NLM to keep track.
1884 * As far as I can see, Solaris uses an eight
1885 * byte structure for oh which contains a four
1886 * byte pid encoded in local byte order and
1887 * the first four bytes of the host
1888 * name. Linux uses a variable length string
1889 * 'pid@hostname' in ascii but doesn't even
1890 * return that in test replies.
1892 * For the moment, return nothing in oh
1893 * (already zero'ed above).
1895 nlm_host_release(bhost);
1897 result->stat.nlm4_testrply_u.holder.l_offset = fl.l_start;
1898 result->stat.nlm4_testrply_u.holder.l_len = fl.l_len;
1902 nlm_release_vfs_state(&vs);
1904 *rpcp = nlm_host_get_rpc(host, TRUE);
1905 nlm_host_release(host);
1910 nlm_do_lock(nlm4_lockargs *argp, nlm4_res *result, struct svc_req *rqstp,
1911 bool_t monitor, CLIENT **rpcp)
1914 struct vfs_state vs;
1915 struct nlm_host *host;
1919 memset(result, 0, sizeof(*result));
1920 memset(&vs, 0, sizeof(vs));
1922 host = nlm_find_host_by_name(argp->alock.caller_name,
1923 svc_getrpccaller(rqstp), rqstp->rq_vers);
1925 result->stat.stat = nlm4_denied_nolocks;
1929 NLM_DEBUG(3, "nlm_do_lock(): caller_name = %s (sysid = %d)\n",
1930 host->nh_caller_name, host->nh_sysid);
1932 if (monitor && host->nh_state && argp->state
1933 && host->nh_state != argp->state) {
1935 * The host rebooted without telling us. Trash its
1938 nlm_host_notify(host, argp->state);
1941 nlm_free_finished_locks(host);
1942 sysid = host->nh_sysid;
1944 nlm_convert_to_fhandle_t(&fh, &argp->alock.fh);
1945 nlm_copy_netobj(&result->cookie, &argp->cookie, M_RPC);
1947 if (time_uptime < nlm_grace_threshold && !argp->reclaim) {
1948 result->stat.stat = nlm4_denied_grace_period;
1952 error = nlm_get_vfs_state(host, rqstp, &fh, &vs);
1954 result->stat.stat = nlm_convert_error(error);
1958 fl.l_start = argp->alock.l_offset;
1959 fl.l_len = argp->alock.l_len;
1960 fl.l_pid = argp->alock.svid;
1962 fl.l_whence = SEEK_SET;
1963 if (argp->exclusive)
1964 fl.l_type = F_WRLCK;
1966 fl.l_type = F_RDLCK;
1968 struct nlm_async_lock *af;
1972 * First, make sure we can contact the host's NLM.
1974 client = nlm_host_get_rpc(host, TRUE);
1976 result->stat.stat = nlm4_failed;
1981 * First we need to check and see if there is an
1982 * existing blocked lock that matches. This could be a
1983 * badly behaved client or an RPC re-send. If we find
1984 * one, just return nlm4_blocked.
1986 mtx_lock(&host->nh_lock);
1987 TAILQ_FOREACH(af, &host->nh_pending, af_link) {
1988 if (af->af_fl.l_start == fl.l_start
1989 && af->af_fl.l_len == fl.l_len
1990 && af->af_fl.l_pid == fl.l_pid
1991 && af->af_fl.l_type == fl.l_type) {
1995 mtx_unlock(&host->nh_lock);
1997 CLNT_RELEASE(client);
1998 result->stat.stat = nlm4_blocked;
2002 af = malloc(sizeof(struct nlm_async_lock), M_NLM,
2004 TASK_INIT(&af->af_task, 0, nlm_lock_callback, af);
2005 af->af_vp = vs.vs_vp;
2008 af->af_rpc = client;
2010 * We use M_RPC here so that we can xdr_free the thing
2013 af->af_granted.exclusive = argp->exclusive;
2014 af->af_granted.alock.caller_name =
2015 strdup(argp->alock.caller_name, M_RPC);
2016 nlm_copy_netobj(&af->af_granted.alock.fh,
2017 &argp->alock.fh, M_RPC);
2018 nlm_copy_netobj(&af->af_granted.alock.oh,
2019 &argp->alock.oh, M_RPC);
2020 af->af_granted.alock.svid = argp->alock.svid;
2021 af->af_granted.alock.l_offset = argp->alock.l_offset;
2022 af->af_granted.alock.l_len = argp->alock.l_len;
2025 * Put the entry on the pending list before calling
2026 * VOP_ADVLOCKASYNC. We do this in case the lock
2027 * request was blocked (returning EINPROGRESS) but
2028 * then granted before we manage to run again. The
2029 * client may receive the granted message before we
2030 * send our blocked reply but thats their problem.
2032 mtx_lock(&host->nh_lock);
2033 TAILQ_INSERT_TAIL(&host->nh_pending, af, af_link);
2034 mtx_unlock(&host->nh_lock);
2036 error = VOP_ADVLOCKASYNC(vs.vs_vp, NULL, F_SETLK, &fl, F_REMOTE,
2037 &af->af_task, &af->af_cookie);
2040 * If the lock completed synchronously, just free the
2041 * tracking structure now.
2043 if (error != EINPROGRESS) {
2044 CLNT_RELEASE(af->af_rpc);
2045 mtx_lock(&host->nh_lock);
2046 TAILQ_REMOVE(&host->nh_pending, af, af_link);
2047 mtx_unlock(&host->nh_lock);
2048 xdr_free((xdrproc_t) xdr_nlm4_testargs,
2052 NLM_DEBUG(2, "NLM: pending async lock %p for %s "
2053 "(sysid %d)\n", af, host->nh_caller_name, sysid);
2055 * Don't vrele the vnode just yet - this must
2056 * wait until either the async callback
2057 * happens or the lock is cancelled.
2062 error = VOP_ADVLOCK(vs.vs_vp, NULL, F_SETLK, &fl, F_REMOTE);
2066 if (error == EINPROGRESS) {
2067 result->stat.stat = nlm4_blocked;
2068 } else if (error == EDEADLK) {
2069 result->stat.stat = nlm4_deadlck;
2070 } else if (error == EAGAIN) {
2071 result->stat.stat = nlm4_denied;
2073 result->stat.stat = nlm4_failed;
2077 nlm_host_monitor(host, argp->state);
2078 result->stat.stat = nlm4_granted;
2082 nlm_release_vfs_state(&vs);
2084 *rpcp = nlm_host_get_rpc(host, TRUE);
2085 nlm_host_release(host);
2090 nlm_do_cancel(nlm4_cancargs *argp, nlm4_res *result, struct svc_req *rqstp,
2094 struct vfs_state vs;
2095 struct nlm_host *host;
2098 struct nlm_async_lock *af;
2100 memset(result, 0, sizeof(*result));
2101 memset(&vs, 0, sizeof(vs));
2103 host = nlm_find_host_by_name(argp->alock.caller_name,
2104 svc_getrpccaller(rqstp), rqstp->rq_vers);
2106 result->stat.stat = nlm4_denied_nolocks;
2110 NLM_DEBUG(3, "nlm_do_cancel(): caller_name = %s (sysid = %d)\n",
2111 host->nh_caller_name, host->nh_sysid);
2113 nlm_free_finished_locks(host);
2114 sysid = host->nh_sysid;
2116 nlm_convert_to_fhandle_t(&fh, &argp->alock.fh);
2117 nlm_copy_netobj(&result->cookie, &argp->cookie, M_RPC);
2119 if (time_uptime < nlm_grace_threshold) {
2120 result->stat.stat = nlm4_denied_grace_period;
2124 error = nlm_get_vfs_state(host, rqstp, &fh, &vs);
2126 result->stat.stat = nlm_convert_error(error);
2130 fl.l_start = argp->alock.l_offset;
2131 fl.l_len = argp->alock.l_len;
2132 fl.l_pid = argp->alock.svid;
2134 fl.l_whence = SEEK_SET;
2135 if (argp->exclusive)
2136 fl.l_type = F_WRLCK;
2138 fl.l_type = F_RDLCK;
2141 * First we need to try and find the async lock request - if
2142 * there isn't one, we give up and return nlm4_denied.
2144 mtx_lock(&host->nh_lock);
2146 TAILQ_FOREACH(af, &host->nh_pending, af_link) {
2147 if (af->af_fl.l_start == fl.l_start
2148 && af->af_fl.l_len == fl.l_len
2149 && af->af_fl.l_pid == fl.l_pid
2150 && af->af_fl.l_type == fl.l_type) {
2156 mtx_unlock(&host->nh_lock);
2157 result->stat.stat = nlm4_denied;
2161 error = nlm_cancel_async_lock(af);
2164 result->stat.stat = nlm4_denied;
2166 result->stat.stat = nlm4_granted;
2169 mtx_unlock(&host->nh_lock);
2172 nlm_release_vfs_state(&vs);
2174 *rpcp = nlm_host_get_rpc(host, TRUE);
2175 nlm_host_release(host);
2180 nlm_do_unlock(nlm4_unlockargs *argp, nlm4_res *result, struct svc_req *rqstp,
2184 struct vfs_state vs;
2185 struct nlm_host *host;
2189 memset(result, 0, sizeof(*result));
2190 memset(&vs, 0, sizeof(vs));
2192 host = nlm_find_host_by_name(argp->alock.caller_name,
2193 svc_getrpccaller(rqstp), rqstp->rq_vers);
2195 result->stat.stat = nlm4_denied_nolocks;
2199 NLM_DEBUG(3, "nlm_do_unlock(): caller_name = %s (sysid = %d)\n",
2200 host->nh_caller_name, host->nh_sysid);
2202 nlm_free_finished_locks(host);
2203 sysid = host->nh_sysid;
2205 nlm_convert_to_fhandle_t(&fh, &argp->alock.fh);
2206 nlm_copy_netobj(&result->cookie, &argp->cookie, M_RPC);
2208 if (time_uptime < nlm_grace_threshold) {
2209 result->stat.stat = nlm4_denied_grace_period;
2213 error = nlm_get_vfs_state(host, rqstp, &fh, &vs);
2215 result->stat.stat = nlm_convert_error(error);
2219 fl.l_start = argp->alock.l_offset;
2220 fl.l_len = argp->alock.l_len;
2221 fl.l_pid = argp->alock.svid;
2223 fl.l_whence = SEEK_SET;
2224 fl.l_type = F_UNLCK;
2225 error = VOP_ADVLOCK(vs.vs_vp, NULL, F_UNLCK, &fl, F_REMOTE);
2228 * Ignore the error - there is no result code for failure,
2229 * only for grace period.
2231 result->stat.stat = nlm4_granted;
2234 nlm_release_vfs_state(&vs);
2236 *rpcp = nlm_host_get_rpc(host, TRUE);
2237 nlm_host_release(host);
2242 nlm_do_granted(nlm4_testargs *argp, nlm4_res *result, struct svc_req *rqstp,
2246 struct nlm_host *host;
2247 struct nlm_waiting_lock *nw;
2249 memset(result, 0, sizeof(*result));
2251 host = nlm_find_host_by_addr(svc_getrpccaller(rqstp), rqstp->rq_vers);
2253 result->stat.stat = nlm4_denied_nolocks;
2257 nlm_copy_netobj(&result->cookie, &argp->cookie, M_RPC);
2258 result->stat.stat = nlm4_denied;
2260 mtx_lock(&nlm_global_lock);
2261 TAILQ_FOREACH(nw, &nlm_waiting_locks, nw_link) {
2262 if (!nw->nw_waiting)
2264 if (argp->alock.svid == nw->nw_lock.svid
2265 && argp->alock.l_offset == nw->nw_lock.l_offset
2266 && argp->alock.l_len == nw->nw_lock.l_len
2267 && argp->alock.fh.n_len == nw->nw_lock.fh.n_len
2268 && !memcmp(argp->alock.fh.n_bytes, nw->nw_lock.fh.n_bytes,
2269 nw->nw_lock.fh.n_len)) {
2270 nw->nw_waiting = FALSE;
2272 result->stat.stat = nlm4_granted;
2276 mtx_unlock(&nlm_global_lock);
2278 *rpcp = nlm_host_get_rpc(host, TRUE);
2279 nlm_host_release(host);
2284 nlm_do_free_all(nlm4_notify *argp)
2286 struct nlm_host *host, *thost;
2288 TAILQ_FOREACH_SAFE(host, &nlm_hosts, nh_link, thost) {
2289 if (!strcmp(host->nh_caller_name, argp->name))
2290 nlm_host_notify(host, argp->state);
2295 * Kernel module glue
2298 nfslockd_modevent(module_t mod, int type, void *data)
2303 static moduledata_t nfslockd_mod = {
2308 DECLARE_MODULE(nfslockd, nfslockd_mod, SI_SUB_VFS, SI_ORDER_ANY);
2310 /* So that loader and kldload(2) can find us, wherever we are.. */
2311 MODULE_DEPEND(nfslockd, krpc, 1, 1, 1);
2312 MODULE_DEPEND(nfslockd, nfslock, 1, 1, 1);
2313 MODULE_VERSION(nfslockd, 1);