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/sysproto.h>
50 #include <sys/systm.h>
51 #include <sys/taskqueue.h>
52 #include <sys/unistd.h>
53 #include <sys/vnode.h>
55 #include <nfs/nfsproto.h>
56 #include <nfsclient/nfs.h>
57 #include <nfsclient/nfsnode.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, "");
107 * Grace period handling. The value of nlm_grace_threshold is the
108 * value of time_uptime after which we are serving requests normally.
110 static time_t nlm_grace_threshold;
113 * We check for idle hosts if time_uptime is greater than
114 * nlm_next_idle_check,
116 static time_t nlm_next_idle_check;
119 * A socket to use for RPC - shared by all IPv4 RPC clients.
121 static struct socket *nlm_socket;
126 * A socket to use for RPC - shared by all IPv6 RPC clients.
128 static struct socket *nlm_socket6;
133 * An RPC client handle that can be used to communicate with the local
136 static CLIENT *nlm_nsm;
139 * An AUTH handle for the server's creds.
141 static AUTH *nlm_auth;
144 * A zero timeval for sending async RPC messages.
146 struct timeval nlm_zero_tv = { 0, 0 };
149 * The local NSM state number
155 * A lock to protect the host list and waiting lock list.
157 static struct mtx nlm_global_lock;
161 * (l) locked by nh_lock
162 * (s) only accessed via server RPC which is single threaded
163 * (g) locked by nlm_global_lock
164 * (c) const until freeing
165 * (a) modified using atomic ops
169 * A pending client-side lock request, stored on the nlm_waiting_locks
172 struct nlm_waiting_lock {
173 TAILQ_ENTRY(nlm_waiting_lock) nw_link; /* (g) */
174 bool_t nw_waiting; /* (g) */
175 nlm4_lock nw_lock; /* (c) */
176 union nfsfh nw_fh; /* (c) */
177 struct vnode *nw_vp; /* (c) */
179 TAILQ_HEAD(nlm_waiting_lock_list, nlm_waiting_lock);
181 struct nlm_waiting_lock_list nlm_waiting_locks; /* (g) */
184 * A pending server-side asynchronous lock request, stored on the
185 * nh_pending list of the NLM host.
187 struct nlm_async_lock {
188 TAILQ_ENTRY(nlm_async_lock) af_link; /* (l) host's list of locks */
189 struct task af_task; /* (c) async callback details */
190 void *af_cookie; /* (l) lock manager cancel token */
191 struct vnode *af_vp; /* (l) vnode to lock */
192 struct flock af_fl; /* (c) lock details */
193 struct nlm_host *af_host; /* (c) host which is locking */
194 CLIENT *af_rpc; /* (c) rpc client to send message */
195 nlm4_testargs af_granted; /* (c) notification details */
197 TAILQ_HEAD(nlm_async_lock_list, nlm_async_lock);
202 enum nlm_host_state {
210 volatile u_int nh_refs; /* (a) reference count */
211 TAILQ_ENTRY(nlm_host) nh_link; /* (g) global list of hosts */
212 char nh_caller_name[MAXNAMELEN]; /* (c) printable name of host */
213 uint32_t nh_sysid; /* (c) our allocaed system ID */
214 char nh_sysid_string[10]; /* (c) string rep. of sysid */
215 struct sockaddr_storage nh_addr; /* (s) remote address of host */
216 CLIENT *nh_rpc; /* (l) RPC handle to send to host */
217 rpcvers_t nh_vers; /* (s) NLM version of host */
218 int nh_state; /* (s) last seen NSM state of host */
219 enum nlm_host_state nh_monstate; /* (l) local NSM monitoring state */
220 time_t nh_idle_timeout; /* (s) Time at which host is idle */
221 time_t nh_rpc_create_time; /* (s) Time we create RPC client */
222 struct sysctl_ctx_list nh_sysctl; /* (c) vfs.nlm.sysid nodes */
223 struct nlm_async_lock_list nh_pending; /* (l) pending async locks */
224 struct nlm_async_lock_list nh_finished; /* (l) finished async locks */
226 TAILQ_HEAD(nlm_host_list, nlm_host);
228 static struct nlm_host_list nlm_hosts; /* (g) */
229 static uint32_t nlm_next_sysid = 1; /* (g) */
231 static void nlm_host_unmonitor(struct nlm_host *);
233 /**********************************************************************/
236 * Initialise NLM globals.
239 nlm_init(void *dummy)
243 mtx_init(&nlm_global_lock, "nlm_global_lock", NULL, MTX_DEF);
244 TAILQ_INIT(&nlm_waiting_locks);
245 TAILQ_INIT(&nlm_hosts);
247 error = syscall_register(&nlm_syscall_offset, &nlm_syscall_sysent,
248 &nlm_syscall_prev_sysent);
250 printf("Can't register NLM syscall\n");
252 nlm_syscall_registered = TRUE;
254 SYSINIT(nlm_init, SI_SUB_LOCK, SI_ORDER_FIRST, nlm_init, NULL);
257 nlm_uninit(void *dummy)
260 if (nlm_syscall_registered)
261 syscall_deregister(&nlm_syscall_offset,
262 &nlm_syscall_prev_sysent);
264 SYSUNINIT(nlm_uninit, SI_SUB_LOCK, SI_ORDER_FIRST, nlm_uninit, NULL);
267 * Copy a struct netobj.
270 nlm_copy_netobj(struct netobj *dst, struct netobj *src,
271 struct malloc_type *type)
274 dst->n_len = src->n_len;
275 dst->n_bytes = malloc(src->n_len, type, M_WAITOK);
276 memcpy(dst->n_bytes, src->n_bytes, src->n_len);
280 * Create an RPC client handle for the given (address,prog,vers)
284 nlm_get_rpc(struct sockaddr *sa, rpcprog_t prog, rpcvers_t vers)
286 const char *wchan = "nlmrcv";
287 const char* protofmly;
288 struct sockaddr_storage ss;
294 enum clnt_stat stat = RPC_SUCCESS;
295 int rpcvers = RPCBVERS4;
296 bool_t do_tcp = FALSE;
301 * First we need to contact the remote RPCBIND service to find
304 memcpy(&ss, sa, sa->sa_len);
305 switch (ss.ss_family) {
307 ((struct sockaddr_in *)&ss)->sin_port = htons(111);
314 ((struct sockaddr_in6 *)&ss)->sin6_port = htons(111);
322 * Unsupported address family - fail.
327 rpcb = clnt_dg_create(so, (struct sockaddr *)&ss,
328 RPCBPROG, rpcvers, 0, 0);
336 parms.r_netid = "tcp";
338 parms.r_netid = "udp";
343 * Use the default timeout.
352 * Try RPCBIND 4 then 3.
355 stat = CLNT_CALL(rpcb, (rpcprog_t) RPCBPROC_GETADDR,
356 (xdrproc_t) xdr_rpcb, &parms,
357 (xdrproc_t) xdr_wrapstring, &uaddr, timo);
358 if (stat == RPC_PROGVERSMISMATCH) {
359 if (rpcvers == RPCBVERS4)
361 else if (rpcvers == RPCBVERS)
363 CLNT_CONTROL(rpcb, CLSET_VERS, &rpcvers);
365 } else if (stat == RPC_SUCCESS) {
367 * We have a reply from the remote RPCBIND - turn it
368 * into an appropriate address and make a new client
369 * that can talk to the remote NLM.
371 * XXX fixup IPv6 scope ID.
374 a = __rpc_uaddr2taddr_af(ss.ss_family, uaddr);
379 memcpy(&ss, a->buf, a->len);
382 xdr_free((xdrproc_t) xdr_wrapstring, &uaddr);
389 mapping.pm_prog = parms.r_prog;
390 mapping.pm_vers = parms.r_vers;
391 mapping.pm_prot = do_tcp ? IPPROTO_TCP : IPPROTO_UDP;
394 stat = CLNT_CALL(rpcb, (rpcprog_t) PMAPPROC_GETPORT,
395 (xdrproc_t) xdr_pmap, &mapping,
396 (xdrproc_t) xdr_u_short, &port, timo);
398 if (stat == RPC_SUCCESS) {
399 switch (ss.ss_family) {
401 ((struct sockaddr_in *)&ss)->sin_port =
407 ((struct sockaddr_in6 *)&ss)->sin6_port =
415 panic("invalid rpcvers %d", rpcvers);
418 * We may have a positive response from the portmapper, but the NLM
419 * service was not found. Make sure we received a valid port.
421 switch (ss.ss_family) {
423 port = ((struct sockaddr_in *)&ss)->sin_port;
427 port = ((struct sockaddr_in6 *)&ss)->sin6_port;
431 if (stat != RPC_SUCCESS || !port) {
433 * If we were able to talk to rpcbind or portmap, but the udp
434 * variant wasn't available, ask about tcp.
436 * XXX - We could also check for a TCP portmapper, but
437 * if the host is running a portmapper at all, we should be able
438 * to hail it over UDP.
440 if (stat == RPC_SUCCESS && !do_tcp) {
445 /* Otherwise, bad news. */
446 printf("NLM: failed to contact remote rpcbind, "
447 "stat = %d, port = %d\n",
455 * Destroy the UDP client we used to speak to rpcbind and
456 * recreate as a TCP client.
458 struct netconfig *nconf = NULL;
462 switch (ss.ss_family) {
464 nconf = getnetconfigent("tcp");
468 nconf = getnetconfigent("tcp6");
473 rpcb = clnt_reconnect_create(nconf, (struct sockaddr *)&ss,
475 CLNT_CONTROL(rpcb, CLSET_WAITCHAN, &wchan);
476 rpcb->cl_auth = nlm_auth;
480 * Re-use the client we used to speak to rpcbind.
482 CLNT_CONTROL(rpcb, CLSET_SVC_ADDR, &ss);
483 CLNT_CONTROL(rpcb, CLSET_PROG, &prog);
484 CLNT_CONTROL(rpcb, CLSET_VERS, &vers);
485 CLNT_CONTROL(rpcb, CLSET_WAITCHAN, &wchan);
486 rpcb->cl_auth = nlm_auth;
493 * This async callback after when an async lock request has been
494 * granted. We notify the host which initiated the request.
497 nlm_lock_callback(void *arg, int pending)
499 struct nlm_async_lock *af = (struct nlm_async_lock *) arg;
500 struct rpc_callextra ext;
502 if (nlm_debug_level >= 2)
503 printf("NLM: async lock %p for %s (sysid %d) granted\n",
504 af, af->af_host->nh_caller_name,
505 af->af_host->nh_sysid);
508 * Send the results back to the host.
510 * Note: there is a possible race here with nlm_host_notify
511 * destroying the RPC client. To avoid problems, the first
512 * thing nlm_host_notify does is to cancel pending async lock
515 memset(&ext, 0, sizeof(ext));
516 ext.rc_auth = nlm_auth;
517 if (af->af_host->nh_vers == NLM_VERS4) {
518 nlm4_granted_msg_4(&af->af_granted,
519 NULL, af->af_rpc, &ext, nlm_zero_tv);
522 * Back-convert to legacy protocol
524 nlm_testargs granted;
525 granted.cookie = af->af_granted.cookie;
526 granted.exclusive = af->af_granted.exclusive;
527 granted.alock.caller_name =
528 af->af_granted.alock.caller_name;
529 granted.alock.fh = af->af_granted.alock.fh;
530 granted.alock.oh = af->af_granted.alock.oh;
531 granted.alock.svid = af->af_granted.alock.svid;
532 granted.alock.l_offset =
533 af->af_granted.alock.l_offset;
534 granted.alock.l_len =
535 af->af_granted.alock.l_len;
537 nlm_granted_msg_1(&granted,
538 NULL, af->af_rpc, &ext, nlm_zero_tv);
542 * Move this entry to the nh_finished list. Someone else will
543 * free it later - its too hard to do it here safely without
544 * racing with cancel.
546 * XXX possibly we should have a third "granted sent but not
547 * ack'ed" list so that we can re-send the granted message.
549 mtx_lock(&af->af_host->nh_lock);
550 TAILQ_REMOVE(&af->af_host->nh_pending, af, af_link);
551 TAILQ_INSERT_TAIL(&af->af_host->nh_finished, af, af_link);
552 mtx_unlock(&af->af_host->nh_lock);
556 * Free an async lock request. The request must have been removed from
560 nlm_free_async_lock(struct nlm_async_lock *af)
563 * Free an async lock.
566 CLNT_RELEASE(af->af_rpc);
567 xdr_free((xdrproc_t) xdr_nlm4_testargs, &af->af_granted);
574 * Cancel our async request - this must be called with
575 * af->nh_host->nh_lock held. This is slightly complicated by a
576 * potential race with our own callback. If we fail to cancel the
577 * lock, it must already have been granted - we make sure our async
578 * task has completed by calling taskqueue_drain in this case.
581 nlm_cancel_async_lock(struct nlm_async_lock *af)
583 struct nlm_host *host = af->af_host;
586 mtx_assert(&host->nh_lock, MA_OWNED);
588 mtx_unlock(&host->nh_lock);
590 error = VOP_ADVLOCKASYNC(af->af_vp, NULL, F_CANCEL, &af->af_fl,
591 F_REMOTE, NULL, &af->af_cookie);
595 * We failed to cancel - make sure our callback has
596 * completed before we continue.
598 taskqueue_drain(taskqueue_thread, &af->af_task);
601 mtx_lock(&host->nh_lock);
604 if (nlm_debug_level >= 2)
605 printf("NLM: async lock %p for %s (sysid %d) "
607 af, host->nh_caller_name, host->nh_sysid);
610 * Remove from the nh_pending list and free now that
611 * we are safe from the callback.
613 TAILQ_REMOVE(&host->nh_pending, af, af_link);
614 mtx_unlock(&host->nh_lock);
615 nlm_free_async_lock(af);
616 mtx_lock(&host->nh_lock);
623 nlm_free_finished_locks(struct nlm_host *host)
625 struct nlm_async_lock *af;
627 mtx_lock(&host->nh_lock);
628 while ((af = TAILQ_FIRST(&host->nh_finished)) != NULL) {
629 TAILQ_REMOVE(&host->nh_finished, af, af_link);
630 mtx_unlock(&host->nh_lock);
631 nlm_free_async_lock(af);
632 mtx_lock(&host->nh_lock);
634 mtx_unlock(&host->nh_lock);
638 * Free resources used by a host. This is called after the reference
639 * count has reached zero so it doesn't need to worry about locks.
642 nlm_host_destroy(struct nlm_host *host)
645 mtx_lock(&nlm_global_lock);
646 TAILQ_REMOVE(&nlm_hosts, host, nh_link);
647 mtx_unlock(&nlm_global_lock);
650 CLNT_RELEASE(host->nh_rpc);
651 mtx_destroy(&host->nh_lock);
652 sysctl_ctx_free(&host->nh_sysctl);
657 * Thread start callback for client lock recovery
660 nlm_client_recovery_start(void *arg)
662 struct nlm_host *host = (struct nlm_host *) arg;
664 if (nlm_debug_level >= 1)
665 printf("NLM: client lock recovery for %s started\n",
666 host->nh_caller_name);
668 nlm_client_recovery(host);
670 if (nlm_debug_level >= 1)
671 printf("NLM: client lock recovery for %s completed\n",
672 host->nh_caller_name);
674 host->nh_monstate = NLM_MONITORED;
675 nlm_host_release(host);
681 * This is called when we receive a host state change notification. We
682 * unlock any active locks owned by the host. When rpc.lockd is
683 * shutting down, this function is called with newstate set to zero
684 * which allows us to cancel any pending async locks and clear the
688 nlm_host_notify(struct nlm_host *host, int newstate)
690 struct nlm_async_lock *af;
693 if (nlm_debug_level >= 1)
694 printf("NLM: host %s (sysid %d) rebooted, new "
696 host->nh_caller_name, host->nh_sysid, newstate);
700 * Cancel any pending async locks for this host.
702 mtx_lock(&host->nh_lock);
703 while ((af = TAILQ_FIRST(&host->nh_pending)) != NULL) {
705 * nlm_cancel_async_lock will remove the entry from
706 * nh_pending and free it.
708 nlm_cancel_async_lock(af);
710 mtx_unlock(&host->nh_lock);
711 nlm_free_finished_locks(host);
714 * The host just rebooted - trash its locks.
716 lf_clearremotesys(host->nh_sysid);
717 host->nh_state = newstate;
720 * If we have any remote locks for this host (i.e. it
721 * represents a remote NFS server that our local NFS client
722 * has locks for), start a recovery thread.
725 && host->nh_monstate != NLM_RECOVERING
726 && lf_countlocks(NLM_SYSID_CLIENT | host->nh_sysid) > 0) {
727 host->nh_monstate = NLM_RECOVERING;
728 refcount_acquire(&host->nh_refs);
729 kthread_create(nlm_client_recovery_start, host, NULL, 0, 0,
730 "NFS lock recovery for %s", host->nh_caller_name);
735 * Sysctl handler to count the number of locks for a sysid.
738 nlm_host_lock_count_sysctl(SYSCTL_HANDLER_ARGS)
740 struct nlm_host *host;
743 host = oidp->oid_arg1;
744 count = lf_countlocks(host->nh_sysid);
745 return sysctl_handle_int(oidp, &count, 0, req);
749 * Sysctl handler to count the number of client locks for a sysid.
752 nlm_host_client_lock_count_sysctl(SYSCTL_HANDLER_ARGS)
754 struct nlm_host *host;
757 host = oidp->oid_arg1;
758 count = lf_countlocks(NLM_SYSID_CLIENT | host->nh_sysid);
759 return sysctl_handle_int(oidp, &count, 0, req);
763 * Create a new NLM host.
765 static struct nlm_host *
766 nlm_create_host(const char* caller_name)
768 struct nlm_host *host;
769 struct sysctl_oid *oid;
771 mtx_assert(&nlm_global_lock, MA_OWNED);
773 if (nlm_debug_level >= 1)
774 printf("NLM: new host %s (sysid %d)\n",
775 caller_name, nlm_next_sysid);
776 host = malloc(sizeof(struct nlm_host), M_NLM, M_NOWAIT|M_ZERO);
779 mtx_init(&host->nh_lock, "nh_lock", NULL, MTX_DEF);
781 strlcpy(host->nh_caller_name, caller_name, MAXNAMELEN);
782 host->nh_sysid = nlm_next_sysid++;
783 snprintf(host->nh_sysid_string, sizeof(host->nh_sysid_string),
784 "%d", host->nh_sysid);
788 host->nh_monstate = NLM_UNMONITORED;
789 TAILQ_INIT(&host->nh_pending);
790 TAILQ_INIT(&host->nh_finished);
791 TAILQ_INSERT_TAIL(&nlm_hosts, host, nh_link);
793 mtx_unlock(&nlm_global_lock);
795 sysctl_ctx_init(&host->nh_sysctl);
796 oid = SYSCTL_ADD_NODE(&host->nh_sysctl,
797 SYSCTL_STATIC_CHILDREN(_vfs_nlm_sysid),
798 OID_AUTO, host->nh_sysid_string, CTLFLAG_RD, NULL, "");
799 SYSCTL_ADD_STRING(&host->nh_sysctl, SYSCTL_CHILDREN(oid), OID_AUTO,
800 "hostname", CTLFLAG_RD, host->nh_caller_name, 0, "");
801 SYSCTL_ADD_INT(&host->nh_sysctl, SYSCTL_CHILDREN(oid), OID_AUTO,
802 "version", CTLFLAG_RD, &host->nh_vers, 0, "");
803 SYSCTL_ADD_INT(&host->nh_sysctl, SYSCTL_CHILDREN(oid), OID_AUTO,
804 "monitored", CTLFLAG_RD, &host->nh_monstate, 0, "");
805 SYSCTL_ADD_PROC(&host->nh_sysctl, SYSCTL_CHILDREN(oid), OID_AUTO,
806 "lock_count", CTLTYPE_INT | CTLFLAG_RD, host, 0,
807 nlm_host_lock_count_sysctl, "I", "");
808 SYSCTL_ADD_PROC(&host->nh_sysctl, SYSCTL_CHILDREN(oid), OID_AUTO,
809 "client_lock_count", CTLTYPE_INT | CTLFLAG_RD, host, 0,
810 nlm_host_client_lock_count_sysctl, "I", "");
812 mtx_lock(&nlm_global_lock);
818 * Return non-zero if the address parts of the two sockaddrs are the
822 nlm_compare_addr(const struct sockaddr *a, const struct sockaddr *b)
824 const struct sockaddr_in *a4, *b4;
826 const struct sockaddr_in6 *a6, *b6;
829 if (a->sa_family != b->sa_family)
832 switch (a->sa_family) {
834 a4 = (const struct sockaddr_in *) a;
835 b4 = (const struct sockaddr_in *) b;
836 return !memcmp(&a4->sin_addr, &b4->sin_addr,
837 sizeof(a4->sin_addr));
840 a6 = (const struct sockaddr_in6 *) a;
841 b6 = (const struct sockaddr_in6 *) b;
842 return !memcmp(&a6->sin6_addr, &b6->sin6_addr,
843 sizeof(a6->sin6_addr));
851 * Check for idle hosts and stop monitoring them. We could also free
852 * the host structure here, possibly after a larger timeout but that
853 * would require some care to avoid races with
854 * e.g. nlm_host_lock_count_sysctl.
859 struct nlm_host *host;
861 mtx_assert(&nlm_global_lock, MA_OWNED);
863 if (time_uptime <= nlm_next_idle_check)
866 nlm_next_idle_check = time_uptime + NLM_IDLE_PERIOD;
868 TAILQ_FOREACH(host, &nlm_hosts, nh_link) {
869 if (host->nh_monstate == NLM_MONITORED
870 && time_uptime > host->nh_idle_timeout) {
871 mtx_unlock(&nlm_global_lock);
872 if (lf_countlocks(host->nh_sysid) > 0
873 || lf_countlocks(NLM_SYSID_CLIENT
875 host->nh_idle_timeout =
876 time_uptime + NLM_IDLE_TIMEOUT;
877 mtx_lock(&nlm_global_lock);
880 nlm_host_unmonitor(host);
881 mtx_lock(&nlm_global_lock);
887 * Search for an existing NLM host that matches the given name
888 * (typically the caller_name element of an nlm4_lock). If none is
889 * found, create a new host. If 'addr' is non-NULL, record the remote
890 * address of the host so that we can call it back for async
891 * responses. If 'vers' is greater than zero then record the NLM
892 * program version to use to communicate with this client.
895 nlm_find_host_by_name(const char *name, const struct sockaddr *addr,
898 struct nlm_host *host;
900 mtx_lock(&nlm_global_lock);
903 * The remote host is determined by caller_name.
905 TAILQ_FOREACH(host, &nlm_hosts, nh_link) {
906 if (!strcmp(host->nh_caller_name, name))
911 host = nlm_create_host(name);
913 mtx_unlock(&nlm_global_lock);
917 refcount_acquire(&host->nh_refs);
919 host->nh_idle_timeout = time_uptime + NLM_IDLE_TIMEOUT;
922 * If we have an address for the host, record it so that we
923 * can send async replies etc.
927 KASSERT(addr->sa_len < sizeof(struct sockaddr_storage),
928 ("Strange remote transport address length"));
931 * If we have seen an address before and we currently
932 * have an RPC client handle, make sure the address is
933 * the same, otherwise discard the client handle.
935 if (host->nh_addr.ss_len && host->nh_rpc) {
936 if (!nlm_compare_addr(
937 (struct sockaddr *) &host->nh_addr,
939 || host->nh_vers != vers) {
941 mtx_lock(&host->nh_lock);
942 client = host->nh_rpc;
944 mtx_unlock(&host->nh_lock);
946 CLNT_RELEASE(client);
950 memcpy(&host->nh_addr, addr, addr->sa_len);
951 host->nh_vers = vers;
956 mtx_unlock(&nlm_global_lock);
962 * Search for an existing NLM host that matches the given remote
963 * address. If none is found, create a new host with the requested
964 * address and remember 'vers' as the NLM protocol version to use for
968 nlm_find_host_by_addr(const struct sockaddr *addr, int vers)
971 * Fake up a name using inet_ntop. This buffer is
972 * large enough for an IPv6 address.
974 char tmp[sizeof "ffff:ffff:ffff:ffff:ffff:ffff:255.255.255.255"];
975 struct nlm_host *host;
977 switch (addr->sa_family) {
979 __rpc_inet_ntop(AF_INET,
980 &((const struct sockaddr_in *) addr)->sin_addr,
985 __rpc_inet_ntop(AF_INET6,
986 &((const struct sockaddr_in6 *) addr)->sin6_addr,
991 strcmp(tmp, "<unknown>");
995 mtx_lock(&nlm_global_lock);
998 * The remote host is determined by caller_name.
1000 TAILQ_FOREACH(host, &nlm_hosts, nh_link) {
1001 if (nlm_compare_addr(addr,
1002 (const struct sockaddr *) &host->nh_addr))
1007 host = nlm_create_host(tmp);
1009 mtx_unlock(&nlm_global_lock);
1012 memcpy(&host->nh_addr, addr, addr->sa_len);
1013 host->nh_vers = vers;
1015 refcount_acquire(&host->nh_refs);
1017 host->nh_idle_timeout = time_uptime + NLM_IDLE_TIMEOUT;
1021 mtx_unlock(&nlm_global_lock);
1027 * Find the NLM host that matches the value of 'sysid'. If none
1028 * exists, return NULL.
1030 static struct nlm_host *
1031 nlm_find_host_by_sysid(int sysid)
1033 struct nlm_host *host;
1035 TAILQ_FOREACH(host, &nlm_hosts, nh_link) {
1036 if (host->nh_sysid == sysid) {
1037 refcount_acquire(&host->nh_refs);
1045 void nlm_host_release(struct nlm_host *host)
1047 if (refcount_release(&host->nh_refs)) {
1051 nlm_host_destroy(host);
1056 * Unregister this NLM host with the local NSM due to idleness.
1059 nlm_host_unmonitor(struct nlm_host *host)
1063 struct timeval timo;
1064 enum clnt_stat stat;
1066 if (nlm_debug_level >= 1)
1067 printf("NLM: unmonitoring %s (sysid %d)\n",
1068 host->nh_caller_name, host->nh_sysid);
1071 * We put our assigned system ID value in the priv field to
1072 * make it simpler to find the host if we are notified of a
1075 smmonid.mon_name = host->nh_caller_name;
1076 smmonid.my_id.my_name = "localhost";
1077 smmonid.my_id.my_prog = NLM_PROG;
1078 smmonid.my_id.my_vers = NLM_SM;
1079 smmonid.my_id.my_proc = NLM_SM_NOTIFY;
1083 stat = CLNT_CALL(nlm_nsm, SM_UNMON,
1084 (xdrproc_t) xdr_mon, &smmonid,
1085 (xdrproc_t) xdr_sm_stat, &smstat, timo);
1087 if (stat != RPC_SUCCESS) {
1088 printf("Failed to contact local NSM - rpc error %d\n", stat);
1091 if (smstat.res_stat == stat_fail) {
1092 printf("Local NSM refuses to unmonitor %s\n",
1093 host->nh_caller_name);
1097 host->nh_monstate = NLM_UNMONITORED;
1101 * Register this NLM host with the local NSM so that we can be
1102 * notified if it reboots.
1105 nlm_host_monitor(struct nlm_host *host, int state)
1109 struct timeval timo;
1110 enum clnt_stat stat;
1112 if (state && !host->nh_state) {
1114 * This is the first time we have seen an NSM state
1115 * value for this host. We record it here to help
1116 * detect host reboots.
1118 host->nh_state = state;
1119 if (nlm_debug_level >= 1)
1120 printf("NLM: host %s (sysid %d) has NSM state %d\n",
1121 host->nh_caller_name, host->nh_sysid, state);
1124 mtx_lock(&host->nh_lock);
1125 if (host->nh_monstate != NLM_UNMONITORED) {
1126 mtx_unlock(&host->nh_lock);
1129 host->nh_monstate = NLM_MONITORED;
1130 mtx_unlock(&host->nh_lock);
1132 if (nlm_debug_level >= 1)
1133 printf("NLM: monitoring %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 smmon.mon_id.mon_name = host->nh_caller_name;
1142 smmon.mon_id.my_id.my_name = "localhost";
1143 smmon.mon_id.my_id.my_prog = NLM_PROG;
1144 smmon.mon_id.my_id.my_vers = NLM_SM;
1145 smmon.mon_id.my_id.my_proc = NLM_SM_NOTIFY;
1146 memcpy(smmon.priv, &host->nh_sysid, sizeof(host->nh_sysid));
1150 stat = CLNT_CALL(nlm_nsm, SM_MON,
1151 (xdrproc_t) xdr_mon, &smmon,
1152 (xdrproc_t) xdr_sm_stat, &smstat, timo);
1154 if (stat != RPC_SUCCESS) {
1155 printf("Failed to contact local NSM - rpc error %d\n", stat);
1158 if (smstat.res_stat == stat_fail) {
1159 printf("Local NSM refuses to monitor %s\n",
1160 host->nh_caller_name);
1161 mtx_lock(&host->nh_lock);
1162 host->nh_monstate = NLM_MONITOR_FAILED;
1163 mtx_unlock(&host->nh_lock);
1167 host->nh_monstate = NLM_MONITORED;
1171 * Return an RPC client handle that can be used to talk to the NLM
1172 * running on the given host.
1175 nlm_host_get_rpc(struct nlm_host *host)
1179 mtx_lock(&host->nh_lock);
1182 * We can't hold onto RPC handles for too long - the async
1183 * call/reply protocol used by some NLM clients makes it hard
1184 * to tell when they change port numbers (e.g. after a
1185 * reboot). Note that if a client reboots while it isn't
1186 * holding any locks, it won't bother to notify us. We
1187 * expire the RPC handles after two minutes.
1189 if (host->nh_rpc && time_uptime > host->nh_rpc_create_time + 2*60) {
1190 client = host->nh_rpc;
1191 host->nh_rpc = NULL;
1192 mtx_unlock(&host->nh_lock);
1193 CLNT_RELEASE(client);
1194 mtx_lock(&host->nh_lock);
1197 if (!host->nh_rpc) {
1198 mtx_unlock(&host->nh_lock);
1199 client = nlm_get_rpc((struct sockaddr *)&host->nh_addr,
1200 NLM_PROG, host->nh_vers);
1201 mtx_lock(&host->nh_lock);
1205 mtx_unlock(&host->nh_lock);
1206 CLNT_DESTROY(client);
1207 mtx_lock(&host->nh_lock);
1209 host->nh_rpc = client;
1210 host->nh_rpc_create_time = time_uptime;
1215 client = host->nh_rpc;
1217 CLNT_ACQUIRE(client);
1218 mtx_unlock(&host->nh_lock);
1224 int nlm_host_get_sysid(struct nlm_host *host)
1227 return (host->nh_sysid);
1231 nlm_host_get_state(struct nlm_host *host)
1234 return (host->nh_state);
1238 nlm_register_wait_lock(struct nlm4_lock *lock, struct vnode *vp)
1240 struct nlm_waiting_lock *nw;
1242 nw = malloc(sizeof(struct nlm_waiting_lock), M_NLM, M_WAITOK);
1243 nw->nw_lock = *lock;
1244 memcpy(&nw->nw_fh.fh_bytes, nw->nw_lock.fh.n_bytes,
1245 nw->nw_lock.fh.n_len);
1246 nw->nw_lock.fh.n_bytes = nw->nw_fh.fh_bytes;
1247 nw->nw_waiting = TRUE;
1249 mtx_lock(&nlm_global_lock);
1250 TAILQ_INSERT_TAIL(&nlm_waiting_locks, nw, nw_link);
1251 mtx_unlock(&nlm_global_lock);
1257 nlm_deregister_wait_lock(void *handle)
1259 struct nlm_waiting_lock *nw = handle;
1261 mtx_lock(&nlm_global_lock);
1262 TAILQ_REMOVE(&nlm_waiting_locks, nw, nw_link);
1263 mtx_unlock(&nlm_global_lock);
1269 nlm_wait_lock(void *handle, int timo)
1271 struct nlm_waiting_lock *nw = handle;
1275 * If the granted message arrived before we got here,
1276 * nw->nw_waiting will be FALSE - in that case, don't sleep.
1278 mtx_lock(&nlm_global_lock);
1281 error = msleep(nw, &nlm_global_lock, PCATCH, "nlmlock", timo);
1282 TAILQ_REMOVE(&nlm_waiting_locks, nw, nw_link);
1285 * The granted message may arrive after the
1286 * interrupt/timeout but before we manage to lock the
1287 * mutex. Detect this by examining nw_lock.
1289 if (!nw->nw_waiting)
1293 * If nlm_cancel_wait is called, then error will be
1294 * zero but nw_waiting will still be TRUE. We
1295 * translate this into EINTR.
1300 mtx_unlock(&nlm_global_lock);
1308 nlm_cancel_wait(struct vnode *vp)
1310 struct nlm_waiting_lock *nw;
1312 mtx_lock(&nlm_global_lock);
1313 TAILQ_FOREACH(nw, &nlm_waiting_locks, nw_link) {
1314 if (nw->nw_vp == vp) {
1318 mtx_unlock(&nlm_global_lock);
1322 /**********************************************************************/
1325 * Syscall interface with userland.
1328 extern void nlm_prog_0(struct svc_req *rqstp, SVCXPRT *transp);
1329 extern void nlm_prog_1(struct svc_req *rqstp, SVCXPRT *transp);
1330 extern void nlm_prog_3(struct svc_req *rqstp, SVCXPRT *transp);
1331 extern void nlm_prog_4(struct svc_req *rqstp, SVCXPRT *transp);
1334 nlm_register_services(SVCPOOL *pool, int addr_count, char **addrs)
1336 static rpcvers_t versions[] = {
1337 NLM_SM, NLM_VERS, NLM_VERSX, NLM_VERS4
1339 static void (*dispatchers[])(struct svc_req *, SVCXPRT *) = {
1340 nlm_prog_0, nlm_prog_1, nlm_prog_3, nlm_prog_4
1342 static const int version_count = sizeof(versions) / sizeof(versions[0]);
1347 struct netconfig *nconf;
1351 printf("NLM: no service addresses given - can't start server");
1355 xprts = malloc(addr_count * sizeof(SVCXPRT *), M_NLM, M_WAITOK);
1356 for (i = 0; i < version_count; i++) {
1357 for (j = 0; j < addr_count; j++) {
1359 * Create transports for the first version and
1360 * then just register everything else to the
1366 error = copyin(&addrs[2*j], &up,
1370 error = copyinstr(up, netid, sizeof(netid),
1374 error = copyin(&addrs[2*j+1], &up,
1378 error = copyinstr(up, uaddr, sizeof(uaddr),
1382 nconf = getnetconfigent(netid);
1384 printf("Can't lookup netid %s\n",
1389 xprts[j] = svc_tp_create(pool, dispatchers[i],
1390 NLM_PROG, versions[i], uaddr, nconf);
1392 printf("NLM: unable to create "
1393 "(NLM_PROG, %d).\n", versions[i]);
1397 freenetconfigent(nconf);
1399 nconf = getnetconfigent(xprts[j]->xp_netid);
1400 rpcb_unset(NLM_PROG, versions[i], nconf);
1401 if (!svc_reg(xprts[j], NLM_PROG, versions[i],
1402 dispatchers[i], nconf)) {
1403 printf("NLM: can't register "
1404 "(NLM_PROG, %d)\n", versions[i]);
1418 * Main server entry point. Contacts the local NSM to get its current
1419 * state and send SM_UNMON_ALL. Registers the NLM services and then
1420 * services requests. Does not return until the server is interrupted
1424 nlm_server_main(int addr_count, char **addrs)
1426 struct thread *td = curthread;
1428 SVCPOOL *pool = NULL;
1432 struct sockaddr_in6 sin6;
1434 struct sockaddr_in sin;
1437 struct timeval timo;
1438 enum clnt_stat stat;
1439 struct nlm_host *host, *nhost;
1440 struct nlm_waiting_lock *nw;
1441 vop_advlock_t *old_nfs_advlock;
1442 vop_reclaim_t *old_nfs_reclaim;
1449 printf("NLM: can't start server - it appears to be running already\n");
1453 memset(&opt, 0, sizeof(opt));
1456 error = socreate(AF_INET, &nlm_socket, SOCK_DGRAM, 0,
1459 printf("NLM: can't create IPv4 socket - error %d\n", error);
1462 opt.sopt_dir = SOPT_SET;
1463 opt.sopt_level = IPPROTO_IP;
1464 opt.sopt_name = IP_PORTRANGE;
1465 portlow = IP_PORTRANGE_LOW;
1466 opt.sopt_val = &portlow;
1467 opt.sopt_valsize = sizeof(portlow);
1468 sosetopt(nlm_socket, &opt);
1472 error = socreate(AF_INET6, &nlm_socket6, SOCK_DGRAM, 0,
1475 printf("NLM: can't create IPv6 socket - error %d\n", error);
1479 opt.sopt_dir = SOPT_SET;
1480 opt.sopt_level = IPPROTO_IPV6;
1481 opt.sopt_name = IPV6_PORTRANGE;
1482 portlow = IPV6_PORTRANGE_LOW;
1483 opt.sopt_val = &portlow;
1484 opt.sopt_valsize = sizeof(portlow);
1485 sosetopt(nlm_socket6, &opt);
1488 nlm_auth = authunix_create(curthread->td_ucred);
1491 memset(&sin6, 0, sizeof(sin6));
1492 sin6.sin6_len = sizeof(sin6);
1493 sin6.sin6_family = AF_INET6;
1494 sin6.sin6_addr = in6addr_loopback;
1495 nlm_nsm = nlm_get_rpc((struct sockaddr *) &sin6, SM_PROG, SM_VERS);
1498 memset(&sin, 0, sizeof(sin));
1499 sin.sin_len = sizeof(sin);
1500 sin.sin_family = AF_INET;
1501 sin.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
1502 nlm_nsm = nlm_get_rpc((struct sockaddr *) &sin, SM_PROG,
1509 printf("Can't start NLM - unable to contact NSM\n");
1514 pool = svcpool_create();
1516 error = nlm_register_services(pool, addr_count, addrs);
1520 memset(&id, 0, sizeof(id));
1521 id.my_name = "NFS NLM";
1525 stat = CLNT_CALL(nlm_nsm, SM_UNMON_ALL,
1526 (xdrproc_t) xdr_my_id, &id,
1527 (xdrproc_t) xdr_sm_stat, &smstat, timo);
1529 if (stat != RPC_SUCCESS) {
1532 CLNT_GETERR(nlm_nsm, &err);
1533 printf("NLM: unexpected error contacting NSM, stat=%d, errno=%d\n",
1534 stat, err.re_errno);
1539 if (nlm_debug_level >= 1)
1540 printf("NLM: local NSM state is %d\n", smstat.state);
1541 nlm_nsm_state = smstat.state;
1543 old_nfs_advlock = nfs_advlock_p;
1544 nfs_advlock_p = nlm_advlock;
1545 old_nfs_reclaim = nfs_reclaim_p;
1546 nfs_reclaim_p = nlm_reclaim;
1551 nfs_advlock_p = old_nfs_advlock;
1552 nfs_reclaim_p = old_nfs_reclaim;
1556 svcpool_destroy(pool);
1559 * We are finished communicating with the NSM.
1562 CLNT_RELEASE(nlm_nsm);
1567 * Trash all the existing state so that if the server
1568 * restarts, it gets a clean slate. This is complicated by the
1569 * possibility that there may be other threads trying to make
1570 * client locking requests.
1572 * First we fake a client reboot notification which will
1573 * cancel any pending async locks and purge remote lock state
1574 * from the local lock manager. We release the reference from
1575 * nlm_hosts to the host (which may remove it from the list
1576 * and free it). After this phase, the only entries in the
1577 * nlm_host list should be from other threads performing
1578 * client lock requests. We arrange to defer closing the
1579 * sockets until the last RPC client handle is released.
1585 mtx_lock(&nlm_global_lock);
1586 TAILQ_FOREACH(nw, &nlm_waiting_locks, nw_link) {
1589 TAILQ_FOREACH_SAFE(host, &nlm_hosts, nh_link, nhost) {
1590 mtx_unlock(&nlm_global_lock);
1591 nlm_host_notify(host, 0);
1592 nlm_host_release(host);
1593 mtx_lock(&nlm_global_lock);
1595 TAILQ_FOREACH_SAFE(host, &nlm_hosts, nh_link, nhost) {
1596 mtx_lock(&host->nh_lock);
1598 if (host->nh_addr.ss_family == AF_INET)
1601 if (host->nh_addr.ss_family == AF_INET6)
1605 * Note that the rpc over udp code copes
1606 * correctly with the fact that a socket may
1607 * be used by many rpc handles.
1609 CLNT_CONTROL(host->nh_rpc, CLSET_FD_CLOSE, 0);
1611 mtx_unlock(&host->nh_lock);
1613 mtx_unlock(&nlm_global_lock);
1615 AUTH_DESTROY(nlm_auth);
1618 soclose(nlm_socket);
1622 soclose(nlm_socket6);
1630 nlm_syscall(struct thread *td, struct nlm_syscall_args *uap)
1634 #if __FreeBSD_version >= 700000
1635 error = priv_check(td, PRIV_NFS_LOCKD);
1642 nlm_debug_level = uap->debug_level;
1643 nlm_grace_threshold = time_uptime + uap->grace_period;
1644 nlm_next_idle_check = time_uptime + NLM_IDLE_PERIOD;
1646 return nlm_server_main(uap->addr_count, uap->addrs);
1649 /**********************************************************************/
1652 * NLM implementation details, called from the RPC stubs.
1657 nlm_sm_notify(struct nlm_sm_status *argp)
1660 struct nlm_host *host;
1662 if (nlm_debug_level >= 3)
1663 printf("nlm_sm_notify(): mon_name = %s\n", argp->mon_name);
1664 memcpy(&sysid, &argp->priv, sizeof(sysid));
1665 host = nlm_find_host_by_sysid(sysid);
1667 nlm_host_notify(host, argp->state);
1668 nlm_host_release(host);
1673 nlm_convert_to_fhandle_t(fhandle_t *fhp, struct netobj *p)
1675 memcpy(fhp, p->n_bytes, sizeof(fhandle_t));
1679 struct mount *vs_mp;
1680 struct vnode *vs_vp;
1686 nlm_get_vfs_state(struct nlm_host *host, struct svc_req *rqstp,
1687 fhandle_t *fhp, struct vfs_state *vs)
1689 int error, exflags, freecred;
1690 struct ucred *cred = NULL, *credanon;
1692 memset(vs, 0, sizeof(*vs));
1695 vs->vs_mp = vfs_getvfs(&fhp->fh_fsid);
1699 vs->vs_vfslocked = VFS_LOCK_GIANT(vs->vs_mp);
1701 error = VFS_CHECKEXP(vs->vs_mp, (struct sockaddr *)&host->nh_addr,
1702 &exflags, &credanon);
1706 if (exflags & MNT_EXRDONLY || (vs->vs_mp->mnt_flag & MNT_RDONLY)) {
1711 error = VFS_FHTOVP(vs->vs_mp, &fhp->fh_fid, &vs->vs_vp);
1714 vs->vs_vnlocked = TRUE;
1718 if (!svc_getcred(rqstp, cred, NULL)) {
1722 if (cred->cr_uid == 0 || (exflags & MNT_EXPORTANON)) {
1731 error = VOP_ACCESS(vs->vs_vp, VWRITE, cred, curthread);
1735 #if __FreeBSD_version < 800011
1736 VOP_UNLOCK(vs->vs_vp, 0, curthread);
1738 VOP_UNLOCK(vs->vs_vp, 0);
1740 vs->vs_vnlocked = FALSE;
1750 nlm_release_vfs_state(struct vfs_state *vs)
1754 if (vs->vs_vnlocked)
1761 VFS_UNLOCK_GIANT(vs->vs_vfslocked);
1765 nlm_convert_error(int error)
1768 if (error == ESTALE)
1769 return nlm4_stale_fh;
1770 else if (error == EROFS)
1777 nlm_do_test(nlm4_testargs *argp, nlm4_testres *result, struct svc_req *rqstp,
1781 struct vfs_state vs;
1782 struct nlm_host *host, *bhost;
1786 memset(result, 0, sizeof(*result));
1787 memset(&vs, 0, sizeof(vs));
1789 host = nlm_find_host_by_name(argp->alock.caller_name,
1790 (struct sockaddr *) rqstp->rq_xprt->xp_rtaddr.buf, rqstp->rq_vers);
1792 result->stat.stat = nlm4_denied_nolocks;
1796 if (nlm_debug_level >= 3)
1797 printf("nlm_do_test(): caller_name = %s (sysid = %d)\n",
1798 host->nh_caller_name, host->nh_sysid);
1800 nlm_free_finished_locks(host);
1801 sysid = host->nh_sysid;
1803 nlm_convert_to_fhandle_t(&fh, &argp->alock.fh);
1804 nlm_copy_netobj(&result->cookie, &argp->cookie, M_RPC);
1806 if (time_uptime < nlm_grace_threshold) {
1807 result->stat.stat = nlm4_denied_grace_period;
1811 error = nlm_get_vfs_state(host, rqstp, &fh, &vs);
1813 result->stat.stat = nlm_convert_error(error);
1817 fl.l_start = argp->alock.l_offset;
1818 fl.l_len = argp->alock.l_len;
1819 fl.l_pid = argp->alock.svid;
1821 fl.l_whence = SEEK_SET;
1822 if (argp->exclusive)
1823 fl.l_type = F_WRLCK;
1825 fl.l_type = F_RDLCK;
1826 error = VOP_ADVLOCK(vs.vs_vp, NULL, F_GETLK, &fl, F_REMOTE);
1828 result->stat.stat = nlm4_failed;
1832 if (fl.l_type == F_UNLCK) {
1833 result->stat.stat = nlm4_granted;
1835 result->stat.stat = nlm4_denied;
1836 result->stat.nlm4_testrply_u.holder.exclusive =
1837 (fl.l_type == F_WRLCK);
1838 result->stat.nlm4_testrply_u.holder.svid = fl.l_pid;
1839 bhost = nlm_find_host_by_sysid(fl.l_sysid);
1842 * We don't have any useful way of recording
1843 * the value of oh used in the original lock
1844 * request. Ideally, the test reply would have
1845 * a space for the owning host's name allowing
1846 * our caller's NLM to keep track.
1848 * As far as I can see, Solaris uses an eight
1849 * byte structure for oh which contains a four
1850 * byte pid encoded in local byte order and
1851 * the first four bytes of the host
1852 * name. Linux uses a variable length string
1853 * 'pid@hostname' in ascii but doesn't even
1854 * return that in test replies.
1856 * For the moment, return nothing in oh
1857 * (already zero'ed above).
1859 nlm_host_release(bhost);
1861 result->stat.nlm4_testrply_u.holder.l_offset = fl.l_start;
1862 result->stat.nlm4_testrply_u.holder.l_len = fl.l_len;
1866 nlm_release_vfs_state(&vs);
1868 *rpcp = nlm_host_get_rpc(host);
1869 nlm_host_release(host);
1874 nlm_do_lock(nlm4_lockargs *argp, nlm4_res *result, struct svc_req *rqstp,
1875 bool_t monitor, CLIENT **rpcp)
1878 struct vfs_state vs;
1879 struct nlm_host *host;
1883 memset(result, 0, sizeof(*result));
1884 memset(&vs, 0, sizeof(vs));
1886 host = nlm_find_host_by_name(argp->alock.caller_name,
1887 (struct sockaddr *) rqstp->rq_xprt->xp_rtaddr.buf, rqstp->rq_vers);
1889 result->stat.stat = nlm4_denied_nolocks;
1893 if (nlm_debug_level >= 3)
1894 printf("nlm_do_lock(): caller_name = %s (sysid = %d)\n",
1895 host->nh_caller_name, host->nh_sysid);
1897 if (monitor && host->nh_state && argp->state
1898 && host->nh_state != argp->state) {
1900 * The host rebooted without telling us. Trash its
1903 nlm_host_notify(host, argp->state);
1906 nlm_free_finished_locks(host);
1907 sysid = host->nh_sysid;
1909 nlm_convert_to_fhandle_t(&fh, &argp->alock.fh);
1910 nlm_copy_netobj(&result->cookie, &argp->cookie, M_RPC);
1912 if (time_uptime < nlm_grace_threshold && !argp->reclaim) {
1913 result->stat.stat = nlm4_denied_grace_period;
1917 error = nlm_get_vfs_state(host, rqstp, &fh, &vs);
1919 result->stat.stat = nlm_convert_error(error);
1923 fl.l_start = argp->alock.l_offset;
1924 fl.l_len = argp->alock.l_len;
1925 fl.l_pid = argp->alock.svid;
1927 fl.l_whence = SEEK_SET;
1928 if (argp->exclusive)
1929 fl.l_type = F_WRLCK;
1931 fl.l_type = F_RDLCK;
1933 struct nlm_async_lock *af;
1937 * First, make sure we can contact the host's NLM.
1939 client = nlm_host_get_rpc(host);
1941 result->stat.stat = nlm4_failed;
1946 * First we need to check and see if there is an
1947 * existing blocked lock that matches. This could be a
1948 * badly behaved client or an RPC re-send. If we find
1949 * one, just return nlm4_blocked.
1951 mtx_lock(&host->nh_lock);
1952 TAILQ_FOREACH(af, &host->nh_pending, af_link) {
1953 if (af->af_fl.l_start == fl.l_start
1954 && af->af_fl.l_len == fl.l_len
1955 && af->af_fl.l_pid == fl.l_pid
1956 && af->af_fl.l_type == fl.l_type) {
1960 mtx_unlock(&host->nh_lock);
1962 CLNT_RELEASE(client);
1963 result->stat.stat = nlm4_blocked;
1967 af = malloc(sizeof(struct nlm_async_lock), M_NLM,
1969 TASK_INIT(&af->af_task, 0, nlm_lock_callback, af);
1970 af->af_vp = vs.vs_vp;
1973 af->af_rpc = client;
1975 * We use M_RPC here so that we can xdr_free the thing
1978 af->af_granted.exclusive = argp->exclusive;
1979 af->af_granted.alock.caller_name =
1980 strdup(argp->alock.caller_name, M_RPC);
1981 nlm_copy_netobj(&af->af_granted.alock.fh,
1982 &argp->alock.fh, M_RPC);
1983 nlm_copy_netobj(&af->af_granted.alock.oh,
1984 &argp->alock.oh, M_RPC);
1985 af->af_granted.alock.svid = argp->alock.svid;
1986 af->af_granted.alock.l_offset = argp->alock.l_offset;
1987 af->af_granted.alock.l_len = argp->alock.l_len;
1990 * Put the entry on the pending list before calling
1991 * VOP_ADVLOCKASYNC. We do this in case the lock
1992 * request was blocked (returning EINPROGRESS) but
1993 * then granted before we manage to run again. The
1994 * client may receive the granted message before we
1995 * send our blocked reply but thats their problem.
1997 mtx_lock(&host->nh_lock);
1998 TAILQ_INSERT_TAIL(&host->nh_pending, af, af_link);
1999 mtx_unlock(&host->nh_lock);
2001 error = VOP_ADVLOCKASYNC(vs.vs_vp, NULL, F_SETLK, &fl, F_REMOTE,
2002 &af->af_task, &af->af_cookie);
2005 * If the lock completed synchronously, just free the
2006 * tracking structure now.
2008 if (error != EINPROGRESS) {
2009 CLNT_RELEASE(af->af_rpc);
2010 mtx_lock(&host->nh_lock);
2011 TAILQ_REMOVE(&host->nh_pending, af, af_link);
2012 mtx_unlock(&host->nh_lock);
2013 xdr_free((xdrproc_t) xdr_nlm4_testargs,
2017 if (nlm_debug_level >= 2)
2018 printf("NLM: pending async lock %p for %s "
2020 af, host->nh_caller_name, sysid);
2022 * Don't vrele the vnode just yet - this must
2023 * wait until either the async callback
2024 * happens or the lock is cancelled.
2029 error = VOP_ADVLOCK(vs.vs_vp, NULL, F_SETLK, &fl, F_REMOTE);
2033 if (error == EINPROGRESS) {
2034 result->stat.stat = nlm4_blocked;
2035 } else if (error == EDEADLK) {
2036 result->stat.stat = nlm4_deadlck;
2037 } else if (error == EAGAIN) {
2038 result->stat.stat = nlm4_denied;
2040 result->stat.stat = nlm4_failed;
2044 nlm_host_monitor(host, argp->state);
2045 result->stat.stat = nlm4_granted;
2049 nlm_release_vfs_state(&vs);
2051 *rpcp = nlm_host_get_rpc(host);
2052 nlm_host_release(host);
2057 nlm_do_cancel(nlm4_cancargs *argp, nlm4_res *result, struct svc_req *rqstp,
2061 struct vfs_state vs;
2062 struct nlm_host *host;
2065 struct nlm_async_lock *af;
2067 memset(result, 0, sizeof(*result));
2068 memset(&vs, 0, sizeof(vs));
2070 host = nlm_find_host_by_name(argp->alock.caller_name,
2071 (struct sockaddr *) rqstp->rq_xprt->xp_rtaddr.buf, rqstp->rq_vers);
2073 result->stat.stat = nlm4_denied_nolocks;
2077 if (nlm_debug_level >= 3)
2078 printf("nlm_do_cancel(): caller_name = %s (sysid = %d)\n",
2079 host->nh_caller_name, host->nh_sysid);
2081 nlm_free_finished_locks(host);
2082 sysid = host->nh_sysid;
2084 nlm_convert_to_fhandle_t(&fh, &argp->alock.fh);
2085 nlm_copy_netobj(&result->cookie, &argp->cookie, M_RPC);
2087 if (time_uptime < nlm_grace_threshold) {
2088 result->stat.stat = nlm4_denied_grace_period;
2092 error = nlm_get_vfs_state(host, rqstp, &fh, &vs);
2094 result->stat.stat = nlm_convert_error(error);
2098 fl.l_start = argp->alock.l_offset;
2099 fl.l_len = argp->alock.l_len;
2100 fl.l_pid = argp->alock.svid;
2102 fl.l_whence = SEEK_SET;
2103 if (argp->exclusive)
2104 fl.l_type = F_WRLCK;
2106 fl.l_type = F_RDLCK;
2109 * First we need to try and find the async lock request - if
2110 * there isn't one, we give up and return nlm4_denied.
2112 mtx_lock(&host->nh_lock);
2114 TAILQ_FOREACH(af, &host->nh_pending, af_link) {
2115 if (af->af_fl.l_start == fl.l_start
2116 && af->af_fl.l_len == fl.l_len
2117 && af->af_fl.l_pid == fl.l_pid
2118 && af->af_fl.l_type == fl.l_type) {
2124 mtx_unlock(&host->nh_lock);
2125 result->stat.stat = nlm4_denied;
2129 error = nlm_cancel_async_lock(af);
2132 result->stat.stat = nlm4_denied;
2134 result->stat.stat = nlm4_granted;
2137 mtx_unlock(&host->nh_lock);
2140 nlm_release_vfs_state(&vs);
2142 *rpcp = nlm_host_get_rpc(host);
2143 nlm_host_release(host);
2148 nlm_do_unlock(nlm4_unlockargs *argp, nlm4_res *result, struct svc_req *rqstp,
2152 struct vfs_state vs;
2153 struct nlm_host *host;
2157 memset(result, 0, sizeof(*result));
2158 memset(&vs, 0, sizeof(vs));
2160 host = nlm_find_host_by_name(argp->alock.caller_name,
2161 (struct sockaddr *) rqstp->rq_xprt->xp_rtaddr.buf, rqstp->rq_vers);
2163 result->stat.stat = nlm4_denied_nolocks;
2167 if (nlm_debug_level >= 3)
2168 printf("nlm_do_unlock(): caller_name = %s (sysid = %d)\n",
2169 host->nh_caller_name, host->nh_sysid);
2171 nlm_free_finished_locks(host);
2172 sysid = host->nh_sysid;
2174 nlm_convert_to_fhandle_t(&fh, &argp->alock.fh);
2175 nlm_copy_netobj(&result->cookie, &argp->cookie, M_RPC);
2177 if (time_uptime < nlm_grace_threshold) {
2178 result->stat.stat = nlm4_denied_grace_period;
2182 error = nlm_get_vfs_state(host, rqstp, &fh, &vs);
2184 result->stat.stat = nlm_convert_error(error);
2188 fl.l_start = argp->alock.l_offset;
2189 fl.l_len = argp->alock.l_len;
2190 fl.l_pid = argp->alock.svid;
2192 fl.l_whence = SEEK_SET;
2193 fl.l_type = F_UNLCK;
2194 error = VOP_ADVLOCK(vs.vs_vp, NULL, F_UNLCK, &fl, F_REMOTE);
2197 * Ignore the error - there is no result code for failure,
2198 * only for grace period.
2200 result->stat.stat = nlm4_granted;
2203 nlm_release_vfs_state(&vs);
2205 *rpcp = nlm_host_get_rpc(host);
2206 nlm_host_release(host);
2211 nlm_do_granted(nlm4_testargs *argp, nlm4_res *result, struct svc_req *rqstp,
2215 struct nlm_host *host;
2216 struct nlm_waiting_lock *nw;
2218 memset(result, 0, sizeof(*result));
2220 host = nlm_find_host_by_addr(
2221 (struct sockaddr *) rqstp->rq_xprt->xp_rtaddr.buf,
2224 result->stat.stat = nlm4_denied_nolocks;
2228 nlm_copy_netobj(&result->cookie, &argp->cookie, M_RPC);
2229 result->stat.stat = nlm4_denied;
2231 mtx_lock(&nlm_global_lock);
2232 TAILQ_FOREACH(nw, &nlm_waiting_locks, nw_link) {
2233 if (!nw->nw_waiting)
2235 if (argp->alock.svid == nw->nw_lock.svid
2236 && argp->alock.l_offset == nw->nw_lock.l_offset
2237 && argp->alock.l_len == nw->nw_lock.l_len
2238 && argp->alock.fh.n_len == nw->nw_lock.fh.n_len
2239 && !memcmp(argp->alock.fh.n_bytes, nw->nw_lock.fh.n_bytes,
2240 nw->nw_lock.fh.n_len)) {
2241 nw->nw_waiting = FALSE;
2243 result->stat.stat = nlm4_granted;
2247 mtx_unlock(&nlm_global_lock);
2249 *rpcp = nlm_host_get_rpc(host);
2250 nlm_host_release(host);
2255 nlm_do_free_all(nlm4_notify *argp)
2257 struct nlm_host *host, *thost;
2259 TAILQ_FOREACH_SAFE(host, &nlm_hosts, nh_link, thost) {
2260 if (!strcmp(host->nh_caller_name, argp->name))
2261 nlm_host_notify(host, argp->state);
2266 * Kernel module glue
2269 nfslockd_modevent(module_t mod, int type, void *data)
2274 static moduledata_t nfslockd_mod = {
2279 DECLARE_MODULE(nfslockd, nfslockd_mod, SI_SUB_VFS, SI_ORDER_ANY);
2281 /* So that loader and kldload(2) can find us, wherever we are.. */
2282 MODULE_DEPEND(nfslockd, krpc, 1, 1, 1);
2283 MODULE_DEPEND(nfslockd, nfs, 1, 1, 1);
2284 MODULE_VERSION(nfslockd, 1);