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>
35 #include <sys/fcntl.h>
36 #include <sys/kernel.h>
37 #include <sys/kthread.h>
38 #include <sys/lockf.h>
39 #include <sys/malloc.h>
40 #include <sys/mount.h>
41 #if __FreeBSD_version >= 700000
45 #include <sys/socket.h>
46 #include <sys/socketvar.h>
47 #include <sys/syscall.h>
48 #include <sys/sysctl.h>
49 #include <sys/sysent.h>
50 #include <sys/syslog.h>
51 #include <sys/sysproto.h>
52 #include <sys/systm.h>
53 #include <sys/taskqueue.h>
54 #include <sys/unistd.h>
55 #include <sys/vnode.h>
57 #include <nfs/nfsproto.h>
58 #include <nfs/nfs_lock.h>
60 #include <nlm/nlm_prot.h>
61 #include <nlm/sm_inter.h>
63 #include <rpc/rpc_com.h>
64 #include <rpc/rpcb_prot.h>
66 MALLOC_DEFINE(M_NLM, "NLM", "Network Lock Manager");
69 * If a host is inactive (and holds no locks) for this amount of
70 * seconds, we consider it idle and stop tracking it.
72 #define NLM_IDLE_TIMEOUT 30
75 * We check the host list for idle every few seconds.
77 #define NLM_IDLE_PERIOD 5
80 * We only look for GRANTED_RES messages for a little while.
82 #define NLM_EXPIRE_TIMEOUT 10
85 * Support for sysctl vfs.nlm.sysid
87 static SYSCTL_NODE(_vfs, OID_AUTO, nlm, CTLFLAG_RW, NULL,
88 "Network Lock Manager");
89 static SYSCTL_NODE(_vfs_nlm, OID_AUTO, sysid, CTLFLAG_RW, NULL, "");
94 static int nlm_syscall_offset = SYS_nlm_syscall;
95 static struct sysent nlm_syscall_prev_sysent;
96 #if __FreeBSD_version < 700000
97 static struct sysent nlm_syscall_sysent = {
98 (sizeof(struct nlm_syscall_args) / sizeof(register_t)) | SYF_MPSAFE,
99 (sy_call_t *) nlm_syscall
102 MAKE_SYSENT(nlm_syscall);
104 static bool_t nlm_syscall_registered = FALSE;
107 * Debug level passed in from userland. We also support a sysctl hook
108 * so that it can be changed on a live system.
110 static int nlm_debug_level;
111 SYSCTL_INT(_debug, OID_AUTO, nlm_debug, CTLFLAG_RW, &nlm_debug_level, 0, "");
113 #define NLM_DEBUG(_level, args...) \
115 if (nlm_debug_level >= (_level)) \
116 log(LOG_DEBUG, args); \
118 #define NLM_ERR(args...) \
120 log(LOG_ERR, args); \
124 * Grace period handling. The value of nlm_grace_threshold is the
125 * value of time_uptime after which we are serving requests normally.
127 static time_t nlm_grace_threshold;
130 * We check for idle hosts if time_uptime is greater than
131 * nlm_next_idle_check,
133 static time_t nlm_next_idle_check;
136 * A flag to indicate the server is already running.
138 static int nlm_is_running;
141 * A socket to use for RPC - shared by all IPv4 RPC clients.
143 static struct socket *nlm_socket;
148 * A socket to use for RPC - shared by all IPv6 RPC clients.
150 static struct socket *nlm_socket6;
155 * An RPC client handle that can be used to communicate with the local
158 static CLIENT *nlm_nsm;
161 * An AUTH handle for the server's creds.
163 static AUTH *nlm_auth;
166 * A zero timeval for sending async RPC messages.
168 struct timeval nlm_zero_tv = { 0, 0 };
171 * The local NSM state number
177 * A lock to protect the host list and waiting lock list.
179 static struct mtx nlm_global_lock;
183 * (l) locked by nh_lock
184 * (s) only accessed via server RPC which is single threaded
185 * (g) locked by nlm_global_lock
186 * (c) const until freeing
187 * (a) modified using atomic ops
191 * A pending client-side lock request, stored on the nlm_waiting_locks
194 struct nlm_waiting_lock {
195 TAILQ_ENTRY(nlm_waiting_lock) nw_link; /* (g) */
196 bool_t nw_waiting; /* (g) */
197 nlm4_lock nw_lock; /* (c) */
198 union nfsfh nw_fh; /* (c) */
199 struct vnode *nw_vp; /* (c) */
201 TAILQ_HEAD(nlm_waiting_lock_list, nlm_waiting_lock);
203 struct nlm_waiting_lock_list nlm_waiting_locks; /* (g) */
206 * A pending server-side asynchronous lock request, stored on the
207 * nh_pending list of the NLM host.
209 struct nlm_async_lock {
210 TAILQ_ENTRY(nlm_async_lock) af_link; /* (l) host's list of locks */
211 struct task af_task; /* (c) async callback details */
212 void *af_cookie; /* (l) lock manager cancel token */
213 struct vnode *af_vp; /* (l) vnode to lock */
214 struct flock af_fl; /* (c) lock details */
215 struct nlm_host *af_host; /* (c) host which is locking */
216 CLIENT *af_rpc; /* (c) rpc client to send message */
217 nlm4_testargs af_granted; /* (c) notification details */
218 time_t af_expiretime; /* (c) notification time */
220 TAILQ_HEAD(nlm_async_lock_list, nlm_async_lock);
225 enum nlm_host_state {
233 CLIENT *nr_client; /* (l) RPC client handle */
234 time_t nr_create_time; /* (l) when client was created */
239 volatile u_int nh_refs; /* (a) reference count */
240 TAILQ_ENTRY(nlm_host) nh_link; /* (g) global list of hosts */
241 char nh_caller_name[MAXNAMELEN]; /* (c) printable name of host */
242 uint32_t nh_sysid; /* (c) our allocaed system ID */
243 char nh_sysid_string[10]; /* (c) string rep. of sysid */
244 struct sockaddr_storage nh_addr; /* (s) remote address of host */
245 struct nlm_rpc nh_srvrpc; /* (l) RPC for server replies */
246 struct nlm_rpc nh_clntrpc; /* (l) RPC for client requests */
247 rpcvers_t nh_vers; /* (s) NLM version of host */
248 int nh_state; /* (s) last seen NSM state of host */
249 enum nlm_host_state nh_monstate; /* (l) local NSM monitoring state */
250 time_t nh_idle_timeout; /* (s) Time at which host is idle */
251 struct sysctl_ctx_list nh_sysctl; /* (c) vfs.nlm.sysid nodes */
252 uint32_t nh_grantcookie; /* (l) grant cookie counter */
253 struct nlm_async_lock_list nh_pending; /* (l) pending async locks */
254 struct nlm_async_lock_list nh_granted; /* (l) granted locks */
255 struct nlm_async_lock_list nh_finished; /* (l) finished async locks */
257 TAILQ_HEAD(nlm_host_list, nlm_host);
259 static struct nlm_host_list nlm_hosts; /* (g) */
260 static uint32_t nlm_next_sysid = 1; /* (g) */
262 static void nlm_host_unmonitor(struct nlm_host *);
264 struct nlm_grantcookie {
269 static inline uint32_t
270 ng_sysid(struct netobj *src)
273 return ((struct nlm_grantcookie *)src->n_bytes)->ng_sysid;
276 static inline uint32_t
277 ng_cookie(struct netobj *src)
280 return ((struct nlm_grantcookie *)src->n_bytes)->ng_cookie;
283 /**********************************************************************/
286 * Initialise NLM globals.
289 nlm_init(void *dummy)
293 mtx_init(&nlm_global_lock, "nlm_global_lock", NULL, MTX_DEF);
294 TAILQ_INIT(&nlm_waiting_locks);
295 TAILQ_INIT(&nlm_hosts);
297 error = syscall_register(&nlm_syscall_offset, &nlm_syscall_sysent,
298 &nlm_syscall_prev_sysent);
300 NLM_ERR("Can't register NLM syscall\n");
302 nlm_syscall_registered = TRUE;
304 SYSINIT(nlm_init, SI_SUB_LOCK, SI_ORDER_FIRST, nlm_init, NULL);
307 nlm_uninit(void *dummy)
310 if (nlm_syscall_registered)
311 syscall_deregister(&nlm_syscall_offset,
312 &nlm_syscall_prev_sysent);
314 SYSUNINIT(nlm_uninit, SI_SUB_LOCK, SI_ORDER_FIRST, nlm_uninit, NULL);
317 * Create a netobj from an arbitrary source.
320 nlm_make_netobj(struct netobj *dst, caddr_t src, size_t srcsize,
321 struct malloc_type *type)
324 dst->n_len = srcsize;
325 dst->n_bytes = malloc(srcsize, type, M_WAITOK);
326 memcpy(dst->n_bytes, src, srcsize);
330 * Copy a struct netobj.
333 nlm_copy_netobj(struct netobj *dst, struct netobj *src,
334 struct malloc_type *type)
337 nlm_make_netobj(dst, src->n_bytes, src->n_len, type);
342 * Create an RPC client handle for the given (address,prog,vers)
346 nlm_get_rpc(struct sockaddr *sa, rpcprog_t prog, rpcvers_t vers)
348 char *wchan = "nlmrcv";
349 const char* protofmly;
350 struct sockaddr_storage ss;
356 enum clnt_stat stat = RPC_SUCCESS;
357 int rpcvers = RPCBVERS4;
358 bool_t do_tcp = FALSE;
359 bool_t tryagain = FALSE;
360 struct portmap mapping;
364 * First we need to contact the remote RPCBIND service to find
367 memcpy(&ss, sa, sa->sa_len);
368 switch (ss.ss_family) {
370 ((struct sockaddr_in *)&ss)->sin_port = htons(111);
377 ((struct sockaddr_in6 *)&ss)->sin6_port = htons(111);
385 * Unsupported address family - fail.
390 rpcb = clnt_dg_create(so, (struct sockaddr *)&ss,
391 RPCBPROG, rpcvers, 0, 0);
399 parms.r_netid = "tcp";
401 parms.r_netid = "udp";
406 * Use the default timeout.
415 * Try RPCBIND 4 then 3.
418 stat = CLNT_CALL(rpcb, (rpcprog_t) RPCBPROC_GETADDR,
419 (xdrproc_t) xdr_rpcb, &parms,
420 (xdrproc_t) xdr_wrapstring, &uaddr, timo);
421 if (stat == RPC_SUCCESS) {
423 * We have a reply from the remote RPCBIND - turn it
424 * into an appropriate address and make a new client
425 * that can talk to the remote NLM.
427 * XXX fixup IPv6 scope ID.
430 a = __rpc_uaddr2taddr_af(ss.ss_family, uaddr);
435 memcpy(&ss, a->buf, a->len);
438 xdr_free((xdrproc_t) xdr_wrapstring, &uaddr);
441 if (tryagain || stat == RPC_PROGVERSMISMATCH) {
442 if (rpcvers == RPCBVERS4)
444 else if (rpcvers == RPCBVERS)
446 CLNT_CONTROL(rpcb, CLSET_VERS, &rpcvers);
454 mapping.pm_prog = parms.r_prog;
455 mapping.pm_vers = parms.r_vers;
456 mapping.pm_prot = do_tcp ? IPPROTO_TCP : IPPROTO_UDP;
459 stat = CLNT_CALL(rpcb, (rpcprog_t) PMAPPROC_GETPORT,
460 (xdrproc_t) xdr_portmap, &mapping,
461 (xdrproc_t) xdr_u_short, &port, timo);
463 if (stat == RPC_SUCCESS) {
464 switch (ss.ss_family) {
466 ((struct sockaddr_in *)&ss)->sin_port =
472 ((struct sockaddr_in6 *)&ss)->sin6_port =
480 panic("invalid rpcvers %d", rpcvers);
483 * We may have a positive response from the portmapper, but the NLM
484 * service was not found. Make sure we received a valid port.
486 switch (ss.ss_family) {
488 port = ((struct sockaddr_in *)&ss)->sin_port;
492 port = ((struct sockaddr_in6 *)&ss)->sin6_port;
496 if (stat != RPC_SUCCESS || !port) {
498 * If we were able to talk to rpcbind or portmap, but the udp
499 * variant wasn't available, ask about tcp.
501 * XXX - We could also check for a TCP portmapper, but
502 * if the host is running a portmapper at all, we should be able
503 * to hail it over UDP.
505 if (stat == RPC_SUCCESS && !do_tcp) {
510 /* Otherwise, bad news. */
511 NLM_ERR("NLM: failed to contact remote rpcbind, "
512 "stat = %d, port = %d\n", (int) stat, port);
519 * Destroy the UDP client we used to speak to rpcbind and
520 * recreate as a TCP client.
522 struct netconfig *nconf = NULL;
526 switch (ss.ss_family) {
528 nconf = getnetconfigent("tcp");
532 nconf = getnetconfigent("tcp6");
537 rpcb = clnt_reconnect_create(nconf, (struct sockaddr *)&ss,
539 CLNT_CONTROL(rpcb, CLSET_WAITCHAN, wchan);
540 rpcb->cl_auth = nlm_auth;
544 * Re-use the client we used to speak to rpcbind.
546 CLNT_CONTROL(rpcb, CLSET_SVC_ADDR, &ss);
547 CLNT_CONTROL(rpcb, CLSET_PROG, &prog);
548 CLNT_CONTROL(rpcb, CLSET_VERS, &vers);
549 CLNT_CONTROL(rpcb, CLSET_WAITCHAN, wchan);
550 rpcb->cl_auth = nlm_auth;
557 * This async callback after when an async lock request has been
558 * granted. We notify the host which initiated the request.
561 nlm_lock_callback(void *arg, int pending)
563 struct nlm_async_lock *af = (struct nlm_async_lock *) arg;
564 struct rpc_callextra ext;
566 NLM_DEBUG(2, "NLM: async lock %p for %s (sysid %d) granted,"
567 " cookie %d:%d\n", af, af->af_host->nh_caller_name,
568 af->af_host->nh_sysid, ng_sysid(&af->af_granted.cookie),
569 ng_cookie(&af->af_granted.cookie));
572 * Send the results back to the host.
574 * Note: there is a possible race here with nlm_host_notify
575 * destroying the RPC client. To avoid problems, the first
576 * thing nlm_host_notify does is to cancel pending async lock
579 memset(&ext, 0, sizeof(ext));
580 ext.rc_auth = nlm_auth;
581 if (af->af_host->nh_vers == NLM_VERS4) {
582 nlm4_granted_msg_4(&af->af_granted,
583 NULL, af->af_rpc, &ext, nlm_zero_tv);
586 * Back-convert to legacy protocol
588 nlm_testargs granted;
589 granted.cookie = af->af_granted.cookie;
590 granted.exclusive = af->af_granted.exclusive;
591 granted.alock.caller_name =
592 af->af_granted.alock.caller_name;
593 granted.alock.fh = af->af_granted.alock.fh;
594 granted.alock.oh = af->af_granted.alock.oh;
595 granted.alock.svid = af->af_granted.alock.svid;
596 granted.alock.l_offset =
597 af->af_granted.alock.l_offset;
598 granted.alock.l_len =
599 af->af_granted.alock.l_len;
601 nlm_granted_msg_1(&granted,
602 NULL, af->af_rpc, &ext, nlm_zero_tv);
606 * Move this entry to the nh_granted list.
608 af->af_expiretime = time_uptime + NLM_EXPIRE_TIMEOUT;
609 mtx_lock(&af->af_host->nh_lock);
610 TAILQ_REMOVE(&af->af_host->nh_pending, af, af_link);
611 TAILQ_INSERT_TAIL(&af->af_host->nh_granted, af, af_link);
612 mtx_unlock(&af->af_host->nh_lock);
616 * Free an async lock request. The request must have been removed from
620 nlm_free_async_lock(struct nlm_async_lock *af)
623 * Free an async lock.
626 CLNT_RELEASE(af->af_rpc);
627 xdr_free((xdrproc_t) xdr_nlm4_testargs, &af->af_granted);
634 * Cancel our async request - this must be called with
635 * af->nh_host->nh_lock held. This is slightly complicated by a
636 * potential race with our own callback. If we fail to cancel the
637 * lock, it must already have been granted - we make sure our async
638 * task has completed by calling taskqueue_drain in this case.
641 nlm_cancel_async_lock(struct nlm_async_lock *af)
643 struct nlm_host *host = af->af_host;
646 mtx_assert(&host->nh_lock, MA_OWNED);
648 mtx_unlock(&host->nh_lock);
650 error = VOP_ADVLOCKASYNC(af->af_vp, NULL, F_CANCEL, &af->af_fl,
651 F_REMOTE, NULL, &af->af_cookie);
655 * We failed to cancel - make sure our callback has
656 * completed before we continue.
658 taskqueue_drain(taskqueue_thread, &af->af_task);
661 mtx_lock(&host->nh_lock);
664 NLM_DEBUG(2, "NLM: async lock %p for %s (sysid %d) "
665 "cancelled\n", af, host->nh_caller_name, host->nh_sysid);
668 * Remove from the nh_pending list and free now that
669 * we are safe from the callback.
671 TAILQ_REMOVE(&host->nh_pending, af, af_link);
672 mtx_unlock(&host->nh_lock);
673 nlm_free_async_lock(af);
674 mtx_lock(&host->nh_lock);
681 nlm_check_expired_locks(struct nlm_host *host)
683 struct nlm_async_lock *af;
684 time_t uptime = time_uptime;
686 mtx_lock(&host->nh_lock);
687 while ((af = TAILQ_FIRST(&host->nh_granted)) != NULL
688 && uptime >= af->af_expiretime) {
689 NLM_DEBUG(2, "NLM: async lock %p for %s (sysid %d) expired,"
690 " cookie %d:%d\n", af, af->af_host->nh_caller_name,
691 af->af_host->nh_sysid, ng_sysid(&af->af_granted.cookie),
692 ng_cookie(&af->af_granted.cookie));
693 TAILQ_REMOVE(&host->nh_granted, af, af_link);
694 mtx_unlock(&host->nh_lock);
695 nlm_free_async_lock(af);
696 mtx_lock(&host->nh_lock);
698 while ((af = TAILQ_FIRST(&host->nh_finished)) != NULL) {
699 TAILQ_REMOVE(&host->nh_finished, af, af_link);
700 mtx_unlock(&host->nh_lock);
701 nlm_free_async_lock(af);
702 mtx_lock(&host->nh_lock);
704 mtx_unlock(&host->nh_lock);
708 * Free resources used by a host. This is called after the reference
709 * count has reached zero so it doesn't need to worry about locks.
712 nlm_host_destroy(struct nlm_host *host)
715 mtx_lock(&nlm_global_lock);
716 TAILQ_REMOVE(&nlm_hosts, host, nh_link);
717 mtx_unlock(&nlm_global_lock);
719 if (host->nh_srvrpc.nr_client)
720 CLNT_RELEASE(host->nh_srvrpc.nr_client);
721 if (host->nh_clntrpc.nr_client)
722 CLNT_RELEASE(host->nh_clntrpc.nr_client);
723 mtx_destroy(&host->nh_lock);
724 sysctl_ctx_free(&host->nh_sysctl);
729 * Thread start callback for client lock recovery
732 nlm_client_recovery_start(void *arg)
734 struct nlm_host *host = (struct nlm_host *) arg;
736 NLM_DEBUG(1, "NLM: client lock recovery for %s started\n",
737 host->nh_caller_name);
739 nlm_client_recovery(host);
741 NLM_DEBUG(1, "NLM: client lock recovery for %s completed\n",
742 host->nh_caller_name);
744 host->nh_monstate = NLM_MONITORED;
745 nlm_host_release(host);
751 * This is called when we receive a host state change notification. We
752 * unlock any active locks owned by the host. When rpc.lockd is
753 * shutting down, this function is called with newstate set to zero
754 * which allows us to cancel any pending async locks and clear the
758 nlm_host_notify(struct nlm_host *host, int newstate)
760 struct nlm_async_lock *af;
763 NLM_DEBUG(1, "NLM: host %s (sysid %d) rebooted, new "
764 "state is %d\n", host->nh_caller_name,
765 host->nh_sysid, newstate);
769 * Cancel any pending async locks for this host.
771 mtx_lock(&host->nh_lock);
772 while ((af = TAILQ_FIRST(&host->nh_pending)) != NULL) {
774 * nlm_cancel_async_lock will remove the entry from
775 * nh_pending and free it.
777 nlm_cancel_async_lock(af);
779 mtx_unlock(&host->nh_lock);
780 nlm_check_expired_locks(host);
783 * The host just rebooted - trash its locks.
785 lf_clearremotesys(host->nh_sysid);
786 host->nh_state = newstate;
789 * If we have any remote locks for this host (i.e. it
790 * represents a remote NFS server that our local NFS client
791 * has locks for), start a recovery thread.
794 && host->nh_monstate != NLM_RECOVERING
795 && lf_countlocks(NLM_SYSID_CLIENT | host->nh_sysid) > 0) {
797 host->nh_monstate = NLM_RECOVERING;
798 refcount_acquire(&host->nh_refs);
799 kthread_add(nlm_client_recovery_start, host, curproc, &td, 0, 0,
800 "NFS lock recovery for %s", host->nh_caller_name);
805 * Sysctl handler to count the number of locks for a sysid.
808 nlm_host_lock_count_sysctl(SYSCTL_HANDLER_ARGS)
810 struct nlm_host *host;
813 host = oidp->oid_arg1;
814 count = lf_countlocks(host->nh_sysid);
815 return sysctl_handle_int(oidp, &count, 0, req);
819 * Sysctl handler to count the number of client locks for a sysid.
822 nlm_host_client_lock_count_sysctl(SYSCTL_HANDLER_ARGS)
824 struct nlm_host *host;
827 host = oidp->oid_arg1;
828 count = lf_countlocks(NLM_SYSID_CLIENT | host->nh_sysid);
829 return sysctl_handle_int(oidp, &count, 0, req);
833 * Create a new NLM host.
835 static struct nlm_host *
836 nlm_create_host(const char* caller_name)
838 struct nlm_host *host;
839 struct sysctl_oid *oid;
841 mtx_assert(&nlm_global_lock, MA_OWNED);
843 NLM_DEBUG(1, "NLM: new host %s (sysid %d)\n",
844 caller_name, nlm_next_sysid);
845 host = malloc(sizeof(struct nlm_host), M_NLM, M_NOWAIT|M_ZERO);
848 mtx_init(&host->nh_lock, "nh_lock", NULL, MTX_DEF);
850 strlcpy(host->nh_caller_name, caller_name, MAXNAMELEN);
851 host->nh_sysid = nlm_next_sysid++;
852 snprintf(host->nh_sysid_string, sizeof(host->nh_sysid_string),
853 "%d", host->nh_sysid);
856 host->nh_monstate = NLM_UNMONITORED;
857 host->nh_grantcookie = 1;
858 TAILQ_INIT(&host->nh_pending);
859 TAILQ_INIT(&host->nh_granted);
860 TAILQ_INIT(&host->nh_finished);
861 TAILQ_INSERT_TAIL(&nlm_hosts, host, nh_link);
863 mtx_unlock(&nlm_global_lock);
865 sysctl_ctx_init(&host->nh_sysctl);
866 oid = SYSCTL_ADD_NODE(&host->nh_sysctl,
867 SYSCTL_STATIC_CHILDREN(_vfs_nlm_sysid),
868 OID_AUTO, host->nh_sysid_string, CTLFLAG_RD, NULL, "");
869 SYSCTL_ADD_STRING(&host->nh_sysctl, SYSCTL_CHILDREN(oid), OID_AUTO,
870 "hostname", CTLFLAG_RD, host->nh_caller_name, 0, "");
871 SYSCTL_ADD_UINT(&host->nh_sysctl, SYSCTL_CHILDREN(oid), OID_AUTO,
872 "version", CTLFLAG_RD, &host->nh_vers, 0, "");
873 SYSCTL_ADD_UINT(&host->nh_sysctl, SYSCTL_CHILDREN(oid), OID_AUTO,
874 "monitored", CTLFLAG_RD, &host->nh_monstate, 0, "");
875 SYSCTL_ADD_PROC(&host->nh_sysctl, SYSCTL_CHILDREN(oid), OID_AUTO,
876 "lock_count", CTLTYPE_INT | CTLFLAG_RD, host, 0,
877 nlm_host_lock_count_sysctl, "I", "");
878 SYSCTL_ADD_PROC(&host->nh_sysctl, SYSCTL_CHILDREN(oid), OID_AUTO,
879 "client_lock_count", CTLTYPE_INT | CTLFLAG_RD, host, 0,
880 nlm_host_client_lock_count_sysctl, "I", "");
882 mtx_lock(&nlm_global_lock);
888 * Acquire the next sysid for remote locks not handled by the NLM.
891 nlm_acquire_next_sysid(void)
895 mtx_lock(&nlm_global_lock);
896 next_sysid = nlm_next_sysid++;
897 mtx_unlock(&nlm_global_lock);
902 * Return non-zero if the address parts of the two sockaddrs are the
906 nlm_compare_addr(const struct sockaddr *a, const struct sockaddr *b)
908 const struct sockaddr_in *a4, *b4;
910 const struct sockaddr_in6 *a6, *b6;
913 if (a->sa_family != b->sa_family)
916 switch (a->sa_family) {
918 a4 = (const struct sockaddr_in *) a;
919 b4 = (const struct sockaddr_in *) b;
920 return !memcmp(&a4->sin_addr, &b4->sin_addr,
921 sizeof(a4->sin_addr));
924 a6 = (const struct sockaddr_in6 *) a;
925 b6 = (const struct sockaddr_in6 *) b;
926 return !memcmp(&a6->sin6_addr, &b6->sin6_addr,
927 sizeof(a6->sin6_addr));
935 * Check for idle hosts and stop monitoring them. We could also free
936 * the host structure here, possibly after a larger timeout but that
937 * would require some care to avoid races with
938 * e.g. nlm_host_lock_count_sysctl.
943 struct nlm_host *host;
945 mtx_assert(&nlm_global_lock, MA_OWNED);
947 if (time_uptime <= nlm_next_idle_check)
950 nlm_next_idle_check = time_uptime + NLM_IDLE_PERIOD;
952 TAILQ_FOREACH(host, &nlm_hosts, nh_link) {
953 if (host->nh_monstate == NLM_MONITORED
954 && time_uptime > host->nh_idle_timeout) {
955 mtx_unlock(&nlm_global_lock);
956 if (lf_countlocks(host->nh_sysid) > 0
957 || lf_countlocks(NLM_SYSID_CLIENT
959 host->nh_idle_timeout =
960 time_uptime + NLM_IDLE_TIMEOUT;
961 mtx_lock(&nlm_global_lock);
964 nlm_host_unmonitor(host);
965 mtx_lock(&nlm_global_lock);
971 * Search for an existing NLM host that matches the given name
972 * (typically the caller_name element of an nlm4_lock). If none is
973 * found, create a new host. If 'addr' is non-NULL, record the remote
974 * address of the host so that we can call it back for async
975 * responses. If 'vers' is greater than zero then record the NLM
976 * program version to use to communicate with this client.
979 nlm_find_host_by_name(const char *name, const struct sockaddr *addr,
982 struct nlm_host *host;
984 mtx_lock(&nlm_global_lock);
987 * The remote host is determined by caller_name.
989 TAILQ_FOREACH(host, &nlm_hosts, nh_link) {
990 if (!strcmp(host->nh_caller_name, name))
995 host = nlm_create_host(name);
997 mtx_unlock(&nlm_global_lock);
1001 refcount_acquire(&host->nh_refs);
1003 host->nh_idle_timeout = time_uptime + NLM_IDLE_TIMEOUT;
1006 * If we have an address for the host, record it so that we
1007 * can send async replies etc.
1011 KASSERT(addr->sa_len < sizeof(struct sockaddr_storage),
1012 ("Strange remote transport address length"));
1015 * If we have seen an address before and we currently
1016 * have an RPC client handle, make sure the address is
1017 * the same, otherwise discard the client handle.
1019 if (host->nh_addr.ss_len && host->nh_srvrpc.nr_client) {
1020 if (!nlm_compare_addr(
1021 (struct sockaddr *) &host->nh_addr,
1023 || host->nh_vers != vers) {
1025 mtx_lock(&host->nh_lock);
1026 client = host->nh_srvrpc.nr_client;
1027 host->nh_srvrpc.nr_client = NULL;
1028 mtx_unlock(&host->nh_lock);
1030 CLNT_RELEASE(client);
1034 memcpy(&host->nh_addr, addr, addr->sa_len);
1035 host->nh_vers = vers;
1040 mtx_unlock(&nlm_global_lock);
1046 * Search for an existing NLM host that matches the given remote
1047 * address. If none is found, create a new host with the requested
1048 * address and remember 'vers' as the NLM protocol version to use for
1052 nlm_find_host_by_addr(const struct sockaddr *addr, int vers)
1055 * Fake up a name using inet_ntop. This buffer is
1056 * large enough for an IPv6 address.
1058 char tmp[sizeof "ffff:ffff:ffff:ffff:ffff:ffff:255.255.255.255"];
1059 struct nlm_host *host;
1061 switch (addr->sa_family) {
1064 &((const struct sockaddr_in *) addr)->sin_addr,
1070 &((const struct sockaddr_in6 *) addr)->sin6_addr,
1075 strlcpy(tmp, "<unknown>", sizeof(tmp));
1079 mtx_lock(&nlm_global_lock);
1082 * The remote host is determined by caller_name.
1084 TAILQ_FOREACH(host, &nlm_hosts, nh_link) {
1085 if (nlm_compare_addr(addr,
1086 (const struct sockaddr *) &host->nh_addr))
1091 host = nlm_create_host(tmp);
1093 mtx_unlock(&nlm_global_lock);
1096 memcpy(&host->nh_addr, addr, addr->sa_len);
1097 host->nh_vers = vers;
1099 refcount_acquire(&host->nh_refs);
1101 host->nh_idle_timeout = time_uptime + NLM_IDLE_TIMEOUT;
1105 mtx_unlock(&nlm_global_lock);
1111 * Find the NLM host that matches the value of 'sysid'. If none
1112 * exists, return NULL.
1114 static struct nlm_host *
1115 nlm_find_host_by_sysid(int sysid)
1117 struct nlm_host *host;
1119 TAILQ_FOREACH(host, &nlm_hosts, nh_link) {
1120 if (host->nh_sysid == sysid) {
1121 refcount_acquire(&host->nh_refs);
1129 void nlm_host_release(struct nlm_host *host)
1131 if (refcount_release(&host->nh_refs)) {
1135 nlm_host_destroy(host);
1140 * Unregister this NLM host with the local NSM due to idleness.
1143 nlm_host_unmonitor(struct nlm_host *host)
1147 struct timeval timo;
1148 enum clnt_stat stat;
1150 NLM_DEBUG(1, "NLM: unmonitoring %s (sysid %d)\n",
1151 host->nh_caller_name, host->nh_sysid);
1154 * We put our assigned system ID value in the priv field to
1155 * make it simpler to find the host if we are notified of a
1158 smmonid.mon_name = host->nh_caller_name;
1159 smmonid.my_id.my_name = "localhost";
1160 smmonid.my_id.my_prog = NLM_PROG;
1161 smmonid.my_id.my_vers = NLM_SM;
1162 smmonid.my_id.my_proc = NLM_SM_NOTIFY;
1166 stat = CLNT_CALL(nlm_nsm, SM_UNMON,
1167 (xdrproc_t) xdr_mon, &smmonid,
1168 (xdrproc_t) xdr_sm_stat, &smstat, timo);
1170 if (stat != RPC_SUCCESS) {
1171 NLM_ERR("Failed to contact local NSM - rpc error %d\n", stat);
1174 if (smstat.res_stat == stat_fail) {
1175 NLM_ERR("Local NSM refuses to unmonitor %s\n",
1176 host->nh_caller_name);
1180 host->nh_monstate = NLM_UNMONITORED;
1184 * Register this NLM host with the local NSM so that we can be
1185 * notified if it reboots.
1188 nlm_host_monitor(struct nlm_host *host, int state)
1192 struct timeval timo;
1193 enum clnt_stat stat;
1195 if (state && !host->nh_state) {
1197 * This is the first time we have seen an NSM state
1198 * value for this host. We record it here to help
1199 * detect host reboots.
1201 host->nh_state = state;
1202 NLM_DEBUG(1, "NLM: host %s (sysid %d) has NSM state %d\n",
1203 host->nh_caller_name, host->nh_sysid, state);
1206 mtx_lock(&host->nh_lock);
1207 if (host->nh_monstate != NLM_UNMONITORED) {
1208 mtx_unlock(&host->nh_lock);
1211 host->nh_monstate = NLM_MONITORED;
1212 mtx_unlock(&host->nh_lock);
1214 NLM_DEBUG(1, "NLM: monitoring %s (sysid %d)\n",
1215 host->nh_caller_name, host->nh_sysid);
1218 * We put our assigned system ID value in the priv field to
1219 * make it simpler to find the host if we are notified of a
1222 smmon.mon_id.mon_name = host->nh_caller_name;
1223 smmon.mon_id.my_id.my_name = "localhost";
1224 smmon.mon_id.my_id.my_prog = NLM_PROG;
1225 smmon.mon_id.my_id.my_vers = NLM_SM;
1226 smmon.mon_id.my_id.my_proc = NLM_SM_NOTIFY;
1227 memcpy(smmon.priv, &host->nh_sysid, sizeof(host->nh_sysid));
1231 stat = CLNT_CALL(nlm_nsm, SM_MON,
1232 (xdrproc_t) xdr_mon, &smmon,
1233 (xdrproc_t) xdr_sm_stat, &smstat, timo);
1235 if (stat != RPC_SUCCESS) {
1236 NLM_ERR("Failed to contact local NSM - rpc error %d\n", stat);
1239 if (smstat.res_stat == stat_fail) {
1240 NLM_ERR("Local NSM refuses to monitor %s\n",
1241 host->nh_caller_name);
1242 mtx_lock(&host->nh_lock);
1243 host->nh_monstate = NLM_MONITOR_FAILED;
1244 mtx_unlock(&host->nh_lock);
1248 host->nh_monstate = NLM_MONITORED;
1252 * Return an RPC client handle that can be used to talk to the NLM
1253 * running on the given host.
1256 nlm_host_get_rpc(struct nlm_host *host, bool_t isserver)
1258 struct nlm_rpc *rpc;
1261 mtx_lock(&host->nh_lock);
1264 rpc = &host->nh_srvrpc;
1266 rpc = &host->nh_clntrpc;
1269 * We can't hold onto RPC handles for too long - the async
1270 * call/reply protocol used by some NLM clients makes it hard
1271 * to tell when they change port numbers (e.g. after a
1272 * reboot). Note that if a client reboots while it isn't
1273 * holding any locks, it won't bother to notify us. We
1274 * expire the RPC handles after two minutes.
1276 if (rpc->nr_client && time_uptime > rpc->nr_create_time + 2*60) {
1277 client = rpc->nr_client;
1278 rpc->nr_client = NULL;
1279 mtx_unlock(&host->nh_lock);
1280 CLNT_RELEASE(client);
1281 mtx_lock(&host->nh_lock);
1284 if (!rpc->nr_client) {
1285 mtx_unlock(&host->nh_lock);
1286 client = nlm_get_rpc((struct sockaddr *)&host->nh_addr,
1287 NLM_PROG, host->nh_vers);
1288 mtx_lock(&host->nh_lock);
1291 if (rpc->nr_client) {
1292 mtx_unlock(&host->nh_lock);
1293 CLNT_DESTROY(client);
1294 mtx_lock(&host->nh_lock);
1296 rpc->nr_client = client;
1297 rpc->nr_create_time = time_uptime;
1302 client = rpc->nr_client;
1304 CLNT_ACQUIRE(client);
1305 mtx_unlock(&host->nh_lock);
1311 int nlm_host_get_sysid(struct nlm_host *host)
1314 return (host->nh_sysid);
1318 nlm_host_get_state(struct nlm_host *host)
1321 return (host->nh_state);
1325 nlm_register_wait_lock(struct nlm4_lock *lock, struct vnode *vp)
1327 struct nlm_waiting_lock *nw;
1329 nw = malloc(sizeof(struct nlm_waiting_lock), M_NLM, M_WAITOK);
1330 nw->nw_lock = *lock;
1331 memcpy(&nw->nw_fh.fh_bytes, nw->nw_lock.fh.n_bytes,
1332 nw->nw_lock.fh.n_len);
1333 nw->nw_lock.fh.n_bytes = nw->nw_fh.fh_bytes;
1334 nw->nw_waiting = TRUE;
1336 mtx_lock(&nlm_global_lock);
1337 TAILQ_INSERT_TAIL(&nlm_waiting_locks, nw, nw_link);
1338 mtx_unlock(&nlm_global_lock);
1344 nlm_deregister_wait_lock(void *handle)
1346 struct nlm_waiting_lock *nw = handle;
1348 mtx_lock(&nlm_global_lock);
1349 TAILQ_REMOVE(&nlm_waiting_locks, nw, nw_link);
1350 mtx_unlock(&nlm_global_lock);
1356 nlm_wait_lock(void *handle, int timo)
1358 struct nlm_waiting_lock *nw = handle;
1362 * If the granted message arrived before we got here,
1363 * nw->nw_waiting will be FALSE - in that case, don't sleep.
1365 mtx_lock(&nlm_global_lock);
1368 error = msleep(nw, &nlm_global_lock, PCATCH, "nlmlock", timo);
1369 TAILQ_REMOVE(&nlm_waiting_locks, nw, nw_link);
1372 * The granted message may arrive after the
1373 * interrupt/timeout but before we manage to lock the
1374 * mutex. Detect this by examining nw_lock.
1376 if (!nw->nw_waiting)
1380 * If nlm_cancel_wait is called, then error will be
1381 * zero but nw_waiting will still be TRUE. We
1382 * translate this into EINTR.
1387 mtx_unlock(&nlm_global_lock);
1395 nlm_cancel_wait(struct vnode *vp)
1397 struct nlm_waiting_lock *nw;
1399 mtx_lock(&nlm_global_lock);
1400 TAILQ_FOREACH(nw, &nlm_waiting_locks, nw_link) {
1401 if (nw->nw_vp == vp) {
1405 mtx_unlock(&nlm_global_lock);
1409 /**********************************************************************/
1412 * Syscall interface with userland.
1415 extern void nlm_prog_0(struct svc_req *rqstp, SVCXPRT *transp);
1416 extern void nlm_prog_1(struct svc_req *rqstp, SVCXPRT *transp);
1417 extern void nlm_prog_3(struct svc_req *rqstp, SVCXPRT *transp);
1418 extern void nlm_prog_4(struct svc_req *rqstp, SVCXPRT *transp);
1421 nlm_register_services(SVCPOOL *pool, int addr_count, char **addrs)
1423 static rpcvers_t versions[] = {
1424 NLM_SM, NLM_VERS, NLM_VERSX, NLM_VERS4
1426 static void (*dispatchers[])(struct svc_req *, SVCXPRT *) = {
1427 nlm_prog_0, nlm_prog_1, nlm_prog_3, nlm_prog_4
1429 static const int version_count = sizeof(versions) / sizeof(versions[0]);
1434 struct netconfig *nconf;
1438 NLM_ERR("NLM: no service addresses given - can't start server");
1442 xprts = malloc(addr_count * sizeof(SVCXPRT *), M_NLM, M_WAITOK|M_ZERO);
1443 for (i = 0; i < version_count; i++) {
1444 for (j = 0; j < addr_count; j++) {
1446 * Create transports for the first version and
1447 * then just register everything else to the
1453 error = copyin(&addrs[2*j], &up,
1457 error = copyinstr(up, netid, sizeof(netid),
1461 error = copyin(&addrs[2*j+1], &up,
1465 error = copyinstr(up, uaddr, sizeof(uaddr),
1469 nconf = getnetconfigent(netid);
1471 NLM_ERR("Can't lookup netid %s\n",
1476 xprts[j] = svc_tp_create(pool, dispatchers[i],
1477 NLM_PROG, versions[i], uaddr, nconf);
1479 NLM_ERR("NLM: unable to create "
1480 "(NLM_PROG, %d).\n", versions[i]);
1484 freenetconfigent(nconf);
1486 nconf = getnetconfigent(xprts[j]->xp_netid);
1487 rpcb_unset(NLM_PROG, versions[i], nconf);
1488 if (!svc_reg(xprts[j], NLM_PROG, versions[i],
1489 dispatchers[i], nconf)) {
1490 NLM_ERR("NLM: can't register "
1491 "(NLM_PROG, %d)\n", versions[i]);
1500 for (j = 0; j < addr_count; j++) {
1502 SVC_RELEASE(xprts[j]);
1509 * Main server entry point. Contacts the local NSM to get its current
1510 * state and send SM_UNMON_ALL. Registers the NLM services and then
1511 * services requests. Does not return until the server is interrupted
1515 nlm_server_main(int addr_count, char **addrs)
1517 struct thread *td = curthread;
1519 SVCPOOL *pool = NULL;
1523 struct sockaddr_in6 sin6;
1525 struct sockaddr_in sin;
1528 struct timeval timo;
1529 enum clnt_stat stat;
1530 struct nlm_host *host, *nhost;
1531 struct nlm_waiting_lock *nw;
1532 vop_advlock_t *old_nfs_advlock;
1533 vop_reclaim_t *old_nfs_reclaim;
1535 if (nlm_is_running != 0) {
1536 NLM_ERR("NLM: can't start server - "
1537 "it appears to be running already\n");
1541 if (nlm_socket == NULL) {
1542 memset(&opt, 0, sizeof(opt));
1544 error = socreate(AF_INET, &nlm_socket, SOCK_DGRAM, 0,
1547 NLM_ERR("NLM: can't create IPv4 socket - error %d\n",
1551 opt.sopt_dir = SOPT_SET;
1552 opt.sopt_level = IPPROTO_IP;
1553 opt.sopt_name = IP_PORTRANGE;
1554 portlow = IP_PORTRANGE_LOW;
1555 opt.sopt_val = &portlow;
1556 opt.sopt_valsize = sizeof(portlow);
1557 sosetopt(nlm_socket, &opt);
1561 error = socreate(AF_INET6, &nlm_socket6, SOCK_DGRAM, 0,
1564 NLM_ERR("NLM: can't create IPv6 socket - error %d\n",
1566 soclose(nlm_socket);
1570 opt.sopt_dir = SOPT_SET;
1571 opt.sopt_level = IPPROTO_IPV6;
1572 opt.sopt_name = IPV6_PORTRANGE;
1573 portlow = IPV6_PORTRANGE_LOW;
1574 opt.sopt_val = &portlow;
1575 opt.sopt_valsize = sizeof(portlow);
1576 sosetopt(nlm_socket6, &opt);
1580 nlm_auth = authunix_create(curthread->td_ucred);
1583 memset(&sin6, 0, sizeof(sin6));
1584 sin6.sin6_len = sizeof(sin6);
1585 sin6.sin6_family = AF_INET6;
1586 sin6.sin6_addr = in6addr_loopback;
1587 nlm_nsm = nlm_get_rpc((struct sockaddr *) &sin6, SM_PROG, SM_VERS);
1590 memset(&sin, 0, sizeof(sin));
1591 sin.sin_len = sizeof(sin);
1592 sin.sin_family = AF_INET;
1593 sin.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
1594 nlm_nsm = nlm_get_rpc((struct sockaddr *) &sin, SM_PROG,
1601 NLM_ERR("Can't start NLM - unable to contact NSM\n");
1606 pool = svcpool_create("NLM", NULL);
1608 error = nlm_register_services(pool, addr_count, addrs);
1612 memset(&id, 0, sizeof(id));
1613 id.my_name = "NFS NLM";
1617 stat = CLNT_CALL(nlm_nsm, SM_UNMON_ALL,
1618 (xdrproc_t) xdr_my_id, &id,
1619 (xdrproc_t) xdr_sm_stat, &smstat, timo);
1621 if (stat != RPC_SUCCESS) {
1624 CLNT_GETERR(nlm_nsm, &err);
1625 NLM_ERR("NLM: unexpected error contacting NSM, "
1626 "stat=%d, errno=%d\n", stat, err.re_errno);
1632 NLM_DEBUG(1, "NLM: local NSM state is %d\n", smstat.state);
1633 nlm_nsm_state = smstat.state;
1635 old_nfs_advlock = nfs_advlock_p;
1636 nfs_advlock_p = nlm_advlock;
1637 old_nfs_reclaim = nfs_reclaim_p;
1638 nfs_reclaim_p = nlm_reclaim;
1643 nfs_advlock_p = old_nfs_advlock;
1644 nfs_reclaim_p = old_nfs_reclaim;
1649 svcpool_destroy(pool);
1652 * We are finished communicating with the NSM.
1655 CLNT_RELEASE(nlm_nsm);
1660 * Trash all the existing state so that if the server
1661 * restarts, it gets a clean slate. This is complicated by the
1662 * possibility that there may be other threads trying to make
1663 * client locking requests.
1665 * First we fake a client reboot notification which will
1666 * cancel any pending async locks and purge remote lock state
1667 * from the local lock manager. We release the reference from
1668 * nlm_hosts to the host (which may remove it from the list
1669 * and free it). After this phase, the only entries in the
1670 * nlm_host list should be from other threads performing
1671 * client lock requests.
1673 mtx_lock(&nlm_global_lock);
1674 TAILQ_FOREACH(nw, &nlm_waiting_locks, nw_link) {
1677 TAILQ_FOREACH_SAFE(host, &nlm_hosts, nh_link, nhost) {
1678 mtx_unlock(&nlm_global_lock);
1679 nlm_host_notify(host, 0);
1680 nlm_host_release(host);
1681 mtx_lock(&nlm_global_lock);
1683 mtx_unlock(&nlm_global_lock);
1685 AUTH_DESTROY(nlm_auth);
1691 sys_nlm_syscall(struct thread *td, struct nlm_syscall_args *uap)
1695 #if __FreeBSD_version >= 700000
1696 error = priv_check(td, PRIV_NFS_LOCKD);
1703 nlm_debug_level = uap->debug_level;
1704 nlm_grace_threshold = time_uptime + uap->grace_period;
1705 nlm_next_idle_check = time_uptime + NLM_IDLE_PERIOD;
1707 return nlm_server_main(uap->addr_count, uap->addrs);
1710 /**********************************************************************/
1713 * NLM implementation details, called from the RPC stubs.
1718 nlm_sm_notify(struct nlm_sm_status *argp)
1721 struct nlm_host *host;
1723 NLM_DEBUG(3, "nlm_sm_notify(): mon_name = %s\n", argp->mon_name);
1724 memcpy(&sysid, &argp->priv, sizeof(sysid));
1725 host = nlm_find_host_by_sysid(sysid);
1727 nlm_host_notify(host, argp->state);
1728 nlm_host_release(host);
1733 nlm_convert_to_fhandle_t(fhandle_t *fhp, struct netobj *p)
1735 memcpy(fhp, p->n_bytes, sizeof(fhandle_t));
1739 struct mount *vs_mp;
1740 struct vnode *vs_vp;
1746 nlm_get_vfs_state(struct nlm_host *host, struct svc_req *rqstp,
1747 fhandle_t *fhp, struct vfs_state *vs, accmode_t accmode)
1750 struct ucred *cred = NULL, *credanon = NULL;
1752 memset(vs, 0, sizeof(*vs));
1754 vs->vs_mp = vfs_getvfs(&fhp->fh_fsid);
1758 vs->vs_vfslocked = VFS_LOCK_GIANT(vs->vs_mp);
1760 /* accmode == 0 means don't check, since it is an unlock. */
1762 error = VFS_CHECKEXP(vs->vs_mp,
1763 (struct sockaddr *)&host->nh_addr, &exflags, &credanon,
1768 if (exflags & MNT_EXRDONLY ||
1769 (vs->vs_mp->mnt_flag & MNT_RDONLY)) {
1775 error = VFS_FHTOVP(vs->vs_mp, &fhp->fh_fid, LK_EXCLUSIVE, &vs->vs_vp);
1778 vs->vs_vnlocked = TRUE;
1781 if (!svc_getcred(rqstp, &cred, NULL)) {
1785 if (cred->cr_uid == 0 || (exflags & MNT_EXPORTANON)) {
1794 error = VOP_ACCESS(vs->vs_vp, accmode, cred, curthread);
1796 * If this failed and accmode != VWRITE, try again with
1797 * VWRITE to maintain backwards compatibility with the
1798 * old code that always used VWRITE.
1800 if (error != 0 && accmode != VWRITE)
1801 error = VOP_ACCESS(vs->vs_vp, VWRITE, cred, curthread);
1806 #if __FreeBSD_version < 800011
1807 VOP_UNLOCK(vs->vs_vp, 0, curthread);
1809 VOP_UNLOCK(vs->vs_vp, 0);
1811 vs->vs_vnlocked = FALSE;
1823 nlm_release_vfs_state(struct vfs_state *vs)
1827 if (vs->vs_vnlocked)
1834 VFS_UNLOCK_GIANT(vs->vs_vfslocked);
1838 nlm_convert_error(int error)
1841 if (error == ESTALE)
1842 return nlm4_stale_fh;
1843 else if (error == EROFS)
1850 nlm_do_test(nlm4_testargs *argp, nlm4_testres *result, struct svc_req *rqstp,
1854 struct vfs_state vs;
1855 struct nlm_host *host, *bhost;
1860 memset(result, 0, sizeof(*result));
1861 memset(&vs, 0, sizeof(vs));
1863 host = nlm_find_host_by_name(argp->alock.caller_name,
1864 svc_getrpccaller(rqstp), rqstp->rq_vers);
1866 result->stat.stat = nlm4_denied_nolocks;
1870 NLM_DEBUG(3, "nlm_do_test(): caller_name = %s (sysid = %d)\n",
1871 host->nh_caller_name, host->nh_sysid);
1873 nlm_check_expired_locks(host);
1874 sysid = host->nh_sysid;
1876 nlm_convert_to_fhandle_t(&fh, &argp->alock.fh);
1877 nlm_copy_netobj(&result->cookie, &argp->cookie, M_RPC);
1879 if (time_uptime < nlm_grace_threshold) {
1880 result->stat.stat = nlm4_denied_grace_period;
1884 accmode = argp->exclusive ? VWRITE : VREAD;
1885 error = nlm_get_vfs_state(host, rqstp, &fh, &vs, accmode);
1887 result->stat.stat = nlm_convert_error(error);
1891 fl.l_start = argp->alock.l_offset;
1892 fl.l_len = argp->alock.l_len;
1893 fl.l_pid = argp->alock.svid;
1895 fl.l_whence = SEEK_SET;
1896 if (argp->exclusive)
1897 fl.l_type = F_WRLCK;
1899 fl.l_type = F_RDLCK;
1900 error = VOP_ADVLOCK(vs.vs_vp, NULL, F_GETLK, &fl, F_REMOTE);
1902 result->stat.stat = nlm4_failed;
1906 if (fl.l_type == F_UNLCK) {
1907 result->stat.stat = nlm4_granted;
1909 result->stat.stat = nlm4_denied;
1910 result->stat.nlm4_testrply_u.holder.exclusive =
1911 (fl.l_type == F_WRLCK);
1912 result->stat.nlm4_testrply_u.holder.svid = fl.l_pid;
1913 bhost = nlm_find_host_by_sysid(fl.l_sysid);
1916 * We don't have any useful way of recording
1917 * the value of oh used in the original lock
1918 * request. Ideally, the test reply would have
1919 * a space for the owning host's name allowing
1920 * our caller's NLM to keep track.
1922 * As far as I can see, Solaris uses an eight
1923 * byte structure for oh which contains a four
1924 * byte pid encoded in local byte order and
1925 * the first four bytes of the host
1926 * name. Linux uses a variable length string
1927 * 'pid@hostname' in ascii but doesn't even
1928 * return that in test replies.
1930 * For the moment, return nothing in oh
1931 * (already zero'ed above).
1933 nlm_host_release(bhost);
1935 result->stat.nlm4_testrply_u.holder.l_offset = fl.l_start;
1936 result->stat.nlm4_testrply_u.holder.l_len = fl.l_len;
1940 nlm_release_vfs_state(&vs);
1942 *rpcp = nlm_host_get_rpc(host, TRUE);
1943 nlm_host_release(host);
1948 nlm_do_lock(nlm4_lockargs *argp, nlm4_res *result, struct svc_req *rqstp,
1949 bool_t monitor, CLIENT **rpcp)
1952 struct vfs_state vs;
1953 struct nlm_host *host;
1958 memset(result, 0, sizeof(*result));
1959 memset(&vs, 0, sizeof(vs));
1961 host = nlm_find_host_by_name(argp->alock.caller_name,
1962 svc_getrpccaller(rqstp), rqstp->rq_vers);
1964 result->stat.stat = nlm4_denied_nolocks;
1968 NLM_DEBUG(3, "nlm_do_lock(): caller_name = %s (sysid = %d)\n",
1969 host->nh_caller_name, host->nh_sysid);
1971 if (monitor && host->nh_state && argp->state
1972 && host->nh_state != argp->state) {
1974 * The host rebooted without telling us. Trash its
1977 nlm_host_notify(host, argp->state);
1980 nlm_check_expired_locks(host);
1981 sysid = host->nh_sysid;
1983 nlm_convert_to_fhandle_t(&fh, &argp->alock.fh);
1984 nlm_copy_netobj(&result->cookie, &argp->cookie, M_RPC);
1986 if (time_uptime < nlm_grace_threshold && !argp->reclaim) {
1987 result->stat.stat = nlm4_denied_grace_period;
1991 accmode = argp->exclusive ? VWRITE : VREAD;
1992 error = nlm_get_vfs_state(host, rqstp, &fh, &vs, accmode);
1994 result->stat.stat = nlm_convert_error(error);
1998 fl.l_start = argp->alock.l_offset;
1999 fl.l_len = argp->alock.l_len;
2000 fl.l_pid = argp->alock.svid;
2002 fl.l_whence = SEEK_SET;
2003 if (argp->exclusive)
2004 fl.l_type = F_WRLCK;
2006 fl.l_type = F_RDLCK;
2008 struct nlm_async_lock *af;
2010 struct nlm_grantcookie cookie;
2013 * First, make sure we can contact the host's NLM.
2015 client = nlm_host_get_rpc(host, TRUE);
2017 result->stat.stat = nlm4_failed;
2022 * First we need to check and see if there is an
2023 * existing blocked lock that matches. This could be a
2024 * badly behaved client or an RPC re-send. If we find
2025 * one, just return nlm4_blocked.
2027 mtx_lock(&host->nh_lock);
2028 TAILQ_FOREACH(af, &host->nh_pending, af_link) {
2029 if (af->af_fl.l_start == fl.l_start
2030 && af->af_fl.l_len == fl.l_len
2031 && af->af_fl.l_pid == fl.l_pid
2032 && af->af_fl.l_type == fl.l_type) {
2037 cookie.ng_sysid = host->nh_sysid;
2038 cookie.ng_cookie = host->nh_grantcookie++;
2040 mtx_unlock(&host->nh_lock);
2042 CLNT_RELEASE(client);
2043 result->stat.stat = nlm4_blocked;
2047 af = malloc(sizeof(struct nlm_async_lock), M_NLM,
2049 TASK_INIT(&af->af_task, 0, nlm_lock_callback, af);
2050 af->af_vp = vs.vs_vp;
2053 af->af_rpc = client;
2055 * We use M_RPC here so that we can xdr_free the thing
2058 nlm_make_netobj(&af->af_granted.cookie,
2059 (caddr_t)&cookie, sizeof(cookie), M_RPC);
2060 af->af_granted.exclusive = argp->exclusive;
2061 af->af_granted.alock.caller_name =
2062 strdup(argp->alock.caller_name, M_RPC);
2063 nlm_copy_netobj(&af->af_granted.alock.fh,
2064 &argp->alock.fh, M_RPC);
2065 nlm_copy_netobj(&af->af_granted.alock.oh,
2066 &argp->alock.oh, M_RPC);
2067 af->af_granted.alock.svid = argp->alock.svid;
2068 af->af_granted.alock.l_offset = argp->alock.l_offset;
2069 af->af_granted.alock.l_len = argp->alock.l_len;
2072 * Put the entry on the pending list before calling
2073 * VOP_ADVLOCKASYNC. We do this in case the lock
2074 * request was blocked (returning EINPROGRESS) but
2075 * then granted before we manage to run again. The
2076 * client may receive the granted message before we
2077 * send our blocked reply but thats their problem.
2079 mtx_lock(&host->nh_lock);
2080 TAILQ_INSERT_TAIL(&host->nh_pending, af, af_link);
2081 mtx_unlock(&host->nh_lock);
2083 error = VOP_ADVLOCKASYNC(vs.vs_vp, NULL, F_SETLK, &fl, F_REMOTE,
2084 &af->af_task, &af->af_cookie);
2087 * If the lock completed synchronously, just free the
2088 * tracking structure now.
2090 if (error != EINPROGRESS) {
2091 CLNT_RELEASE(af->af_rpc);
2092 mtx_lock(&host->nh_lock);
2093 TAILQ_REMOVE(&host->nh_pending, af, af_link);
2094 mtx_unlock(&host->nh_lock);
2095 xdr_free((xdrproc_t) xdr_nlm4_testargs,
2099 NLM_DEBUG(2, "NLM: pending async lock %p for %s "
2100 "(sysid %d)\n", af, host->nh_caller_name, sysid);
2102 * Don't vrele the vnode just yet - this must
2103 * wait until either the async callback
2104 * happens or the lock is cancelled.
2109 error = VOP_ADVLOCK(vs.vs_vp, NULL, F_SETLK, &fl, F_REMOTE);
2113 if (error == EINPROGRESS) {
2114 result->stat.stat = nlm4_blocked;
2115 } else if (error == EDEADLK) {
2116 result->stat.stat = nlm4_deadlck;
2117 } else if (error == EAGAIN) {
2118 result->stat.stat = nlm4_denied;
2120 result->stat.stat = nlm4_failed;
2124 nlm_host_monitor(host, argp->state);
2125 result->stat.stat = nlm4_granted;
2129 nlm_release_vfs_state(&vs);
2131 *rpcp = nlm_host_get_rpc(host, TRUE);
2132 nlm_host_release(host);
2137 nlm_do_cancel(nlm4_cancargs *argp, nlm4_res *result, struct svc_req *rqstp,
2141 struct vfs_state vs;
2142 struct nlm_host *host;
2145 struct nlm_async_lock *af;
2147 memset(result, 0, sizeof(*result));
2148 memset(&vs, 0, sizeof(vs));
2150 host = nlm_find_host_by_name(argp->alock.caller_name,
2151 svc_getrpccaller(rqstp), rqstp->rq_vers);
2153 result->stat.stat = nlm4_denied_nolocks;
2157 NLM_DEBUG(3, "nlm_do_cancel(): caller_name = %s (sysid = %d)\n",
2158 host->nh_caller_name, host->nh_sysid);
2160 nlm_check_expired_locks(host);
2161 sysid = host->nh_sysid;
2163 nlm_convert_to_fhandle_t(&fh, &argp->alock.fh);
2164 nlm_copy_netobj(&result->cookie, &argp->cookie, M_RPC);
2166 if (time_uptime < nlm_grace_threshold) {
2167 result->stat.stat = nlm4_denied_grace_period;
2171 error = nlm_get_vfs_state(host, rqstp, &fh, &vs, (accmode_t)0);
2173 result->stat.stat = nlm_convert_error(error);
2177 fl.l_start = argp->alock.l_offset;
2178 fl.l_len = argp->alock.l_len;
2179 fl.l_pid = argp->alock.svid;
2181 fl.l_whence = SEEK_SET;
2182 if (argp->exclusive)
2183 fl.l_type = F_WRLCK;
2185 fl.l_type = F_RDLCK;
2188 * First we need to try and find the async lock request - if
2189 * there isn't one, we give up and return nlm4_denied.
2191 mtx_lock(&host->nh_lock);
2193 TAILQ_FOREACH(af, &host->nh_pending, af_link) {
2194 if (af->af_fl.l_start == fl.l_start
2195 && af->af_fl.l_len == fl.l_len
2196 && af->af_fl.l_pid == fl.l_pid
2197 && af->af_fl.l_type == fl.l_type) {
2203 mtx_unlock(&host->nh_lock);
2204 result->stat.stat = nlm4_denied;
2208 error = nlm_cancel_async_lock(af);
2211 result->stat.stat = nlm4_denied;
2213 result->stat.stat = nlm4_granted;
2216 mtx_unlock(&host->nh_lock);
2219 nlm_release_vfs_state(&vs);
2221 *rpcp = nlm_host_get_rpc(host, TRUE);
2222 nlm_host_release(host);
2227 nlm_do_unlock(nlm4_unlockargs *argp, nlm4_res *result, struct svc_req *rqstp,
2231 struct vfs_state vs;
2232 struct nlm_host *host;
2236 memset(result, 0, sizeof(*result));
2237 memset(&vs, 0, sizeof(vs));
2239 host = nlm_find_host_by_name(argp->alock.caller_name,
2240 svc_getrpccaller(rqstp), rqstp->rq_vers);
2242 result->stat.stat = nlm4_denied_nolocks;
2246 NLM_DEBUG(3, "nlm_do_unlock(): caller_name = %s (sysid = %d)\n",
2247 host->nh_caller_name, host->nh_sysid);
2249 nlm_check_expired_locks(host);
2250 sysid = host->nh_sysid;
2252 nlm_convert_to_fhandle_t(&fh, &argp->alock.fh);
2253 nlm_copy_netobj(&result->cookie, &argp->cookie, M_RPC);
2255 if (time_uptime < nlm_grace_threshold) {
2256 result->stat.stat = nlm4_denied_grace_period;
2260 error = nlm_get_vfs_state(host, rqstp, &fh, &vs, (accmode_t)0);
2262 result->stat.stat = nlm_convert_error(error);
2266 fl.l_start = argp->alock.l_offset;
2267 fl.l_len = argp->alock.l_len;
2268 fl.l_pid = argp->alock.svid;
2270 fl.l_whence = SEEK_SET;
2271 fl.l_type = F_UNLCK;
2272 error = VOP_ADVLOCK(vs.vs_vp, NULL, F_UNLCK, &fl, F_REMOTE);
2275 * Ignore the error - there is no result code for failure,
2276 * only for grace period.
2278 result->stat.stat = nlm4_granted;
2281 nlm_release_vfs_state(&vs);
2283 *rpcp = nlm_host_get_rpc(host, TRUE);
2284 nlm_host_release(host);
2289 nlm_do_granted(nlm4_testargs *argp, nlm4_res *result, struct svc_req *rqstp,
2293 struct nlm_host *host;
2294 struct nlm_waiting_lock *nw;
2296 memset(result, 0, sizeof(*result));
2298 host = nlm_find_host_by_addr(svc_getrpccaller(rqstp), rqstp->rq_vers);
2300 result->stat.stat = nlm4_denied_nolocks;
2304 nlm_copy_netobj(&result->cookie, &argp->cookie, M_RPC);
2305 result->stat.stat = nlm4_denied;
2306 KFAIL_POINT_CODE(DEBUG_FP, nlm_deny_grant, goto out);
2308 mtx_lock(&nlm_global_lock);
2309 TAILQ_FOREACH(nw, &nlm_waiting_locks, nw_link) {
2310 if (!nw->nw_waiting)
2312 if (argp->alock.svid == nw->nw_lock.svid
2313 && argp->alock.l_offset == nw->nw_lock.l_offset
2314 && argp->alock.l_len == nw->nw_lock.l_len
2315 && argp->alock.fh.n_len == nw->nw_lock.fh.n_len
2316 && !memcmp(argp->alock.fh.n_bytes, nw->nw_lock.fh.n_bytes,
2317 nw->nw_lock.fh.n_len)) {
2318 nw->nw_waiting = FALSE;
2320 result->stat.stat = nlm4_granted;
2324 mtx_unlock(&nlm_global_lock);
2328 *rpcp = nlm_host_get_rpc(host, TRUE);
2329 nlm_host_release(host);
2334 nlm_do_granted_res(nlm4_res *argp, struct svc_req *rqstp)
2336 struct nlm_host *host = NULL;
2337 struct nlm_async_lock *af = NULL;
2340 if (argp->cookie.n_len != sizeof(struct nlm_grantcookie)) {
2341 NLM_DEBUG(1, "NLM: bogus grant cookie");
2345 host = nlm_find_host_by_sysid(ng_sysid(&argp->cookie));
2347 NLM_DEBUG(1, "NLM: Unknown host rejected our grant");
2351 mtx_lock(&host->nh_lock);
2352 TAILQ_FOREACH(af, &host->nh_granted, af_link)
2353 if (ng_cookie(&argp->cookie) ==
2354 ng_cookie(&af->af_granted.cookie))
2357 TAILQ_REMOVE(&host->nh_granted, af, af_link);
2358 mtx_unlock(&host->nh_lock);
2361 NLM_DEBUG(1, "NLM: host %s (sysid %d) replied to our grant "
2362 "with unrecognized cookie %d:%d", host->nh_caller_name,
2363 host->nh_sysid, ng_sysid(&argp->cookie),
2364 ng_cookie(&argp->cookie));
2368 if (argp->stat.stat != nlm4_granted) {
2369 af->af_fl.l_type = F_UNLCK;
2370 error = VOP_ADVLOCK(af->af_vp, NULL, F_UNLCK, &af->af_fl, F_REMOTE);
2372 NLM_DEBUG(1, "NLM: host %s (sysid %d) rejected our grant "
2373 "and we failed to unlock (%d)", host->nh_caller_name,
2374 host->nh_sysid, error);
2378 NLM_DEBUG(5, "NLM: async lock %p rejected by host %s (sysid %d)",
2379 af, host->nh_caller_name, host->nh_sysid);
2381 NLM_DEBUG(5, "NLM: async lock %p accepted by host %s (sysid %d)",
2382 af, host->nh_caller_name, host->nh_sysid);
2387 nlm_free_async_lock(af);
2389 nlm_host_release(host);
2393 nlm_do_free_all(nlm4_notify *argp)
2395 struct nlm_host *host, *thost;
2397 TAILQ_FOREACH_SAFE(host, &nlm_hosts, nh_link, thost) {
2398 if (!strcmp(host->nh_caller_name, argp->name))
2399 nlm_host_notify(host, argp->state);
2404 * Kernel module glue
2407 nfslockd_modevent(module_t mod, int type, void *data)
2414 /* The NLM module cannot be safely unloaded. */
2417 return (EOPNOTSUPP);
2420 static moduledata_t nfslockd_mod = {
2425 DECLARE_MODULE(nfslockd, nfslockd_mod, SI_SUB_VFS, SI_ORDER_ANY);
2427 /* So that loader and kldload(2) can find us, wherever we are.. */
2428 MODULE_DEPEND(nfslockd, krpc, 1, 1, 1);
2429 MODULE_DEPEND(nfslockd, nfslock, 1, 1, 1);
2430 MODULE_VERSION(nfslockd, 1);