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"
31 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD$");
34 #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 <nfsclient/nfs.h>
59 #include <nfsclient/nfsnode.h>
61 #include <nlm/nlm_prot.h>
62 #include <nlm/sm_inter.h>
64 #include <rpc/rpc_com.h>
65 #include <rpc/rpcb_prot.h>
67 MALLOC_DEFINE(M_NLM, "NLM", "Network Lock Manager");
70 * If a host is inactive (and holds no locks) for this amount of
71 * seconds, we consider it idle and stop tracking it.
73 #define NLM_IDLE_TIMEOUT 30
76 * We check the host list for idle every few seconds.
78 #define NLM_IDLE_PERIOD 5
81 * Support for sysctl vfs.nlm.sysid
83 SYSCTL_NODE(_vfs, OID_AUTO, nlm, CTLFLAG_RW, NULL, "Network Lock Manager");
84 SYSCTL_NODE(_vfs_nlm, OID_AUTO, sysid, CTLFLAG_RW, NULL, "");
89 static int nlm_syscall_offset = SYS_nlm_syscall;
90 static struct sysent nlm_syscall_prev_sysent;
91 #if __FreeBSD_version < 700000
92 static struct sysent nlm_syscall_sysent = {
93 (sizeof(struct nlm_syscall_args) / sizeof(register_t)) | SYF_MPSAFE,
94 (sy_call_t *) nlm_syscall
97 MAKE_SYSENT(nlm_syscall);
99 static bool_t nlm_syscall_registered = FALSE;
102 * Debug level passed in from userland. We also support a sysctl hook
103 * so that it can be changed on a live system.
105 static int nlm_debug_level;
106 SYSCTL_INT(_debug, OID_AUTO, nlm_debug, CTLFLAG_RW, &nlm_debug_level, 0, "");
108 #define NLM_DEBUG(_level, args...) \
110 if (nlm_debug_level >= (_level)) \
111 log(LOG_DEBUG, args); \
113 #define NLM_ERR(args...) \
115 log(LOG_ERR, args); \
119 * Grace period handling. The value of nlm_grace_threshold is the
120 * value of time_uptime after which we are serving requests normally.
122 static time_t nlm_grace_threshold;
125 * We check for idle hosts if time_uptime is greater than
126 * nlm_next_idle_check,
128 static time_t nlm_next_idle_check;
131 * A socket to use for RPC - shared by all IPv4 RPC clients.
133 static struct socket *nlm_socket;
138 * A socket to use for RPC - shared by all IPv6 RPC clients.
140 static struct socket *nlm_socket6;
145 * An RPC client handle that can be used to communicate with the local
148 static CLIENT *nlm_nsm;
151 * An AUTH handle for the server's creds.
153 static AUTH *nlm_auth;
156 * A zero timeval for sending async RPC messages.
158 struct timeval nlm_zero_tv = { 0, 0 };
161 * The local NSM state number
167 * A lock to protect the host list and waiting lock list.
169 static struct mtx nlm_global_lock;
173 * (l) locked by nh_lock
174 * (s) only accessed via server RPC which is single threaded
175 * (g) locked by nlm_global_lock
176 * (c) const until freeing
177 * (a) modified using atomic ops
181 * A pending client-side lock request, stored on the nlm_waiting_locks
184 struct nlm_waiting_lock {
185 TAILQ_ENTRY(nlm_waiting_lock) nw_link; /* (g) */
186 bool_t nw_waiting; /* (g) */
187 nlm4_lock nw_lock; /* (c) */
188 union nfsfh nw_fh; /* (c) */
189 struct vnode *nw_vp; /* (c) */
191 TAILQ_HEAD(nlm_waiting_lock_list, nlm_waiting_lock);
193 struct nlm_waiting_lock_list nlm_waiting_locks; /* (g) */
196 * A pending server-side asynchronous lock request, stored on the
197 * nh_pending list of the NLM host.
199 struct nlm_async_lock {
200 TAILQ_ENTRY(nlm_async_lock) af_link; /* (l) host's list of locks */
201 struct task af_task; /* (c) async callback details */
202 void *af_cookie; /* (l) lock manager cancel token */
203 struct vnode *af_vp; /* (l) vnode to lock */
204 struct flock af_fl; /* (c) lock details */
205 struct nlm_host *af_host; /* (c) host which is locking */
206 CLIENT *af_rpc; /* (c) rpc client to send message */
207 nlm4_testargs af_granted; /* (c) notification details */
209 TAILQ_HEAD(nlm_async_lock_list, nlm_async_lock);
214 enum nlm_host_state {
222 CLIENT *nr_client; /* (l) RPC client handle */
223 time_t nr_create_time; /* (l) when client was created */
228 volatile u_int nh_refs; /* (a) reference count */
229 TAILQ_ENTRY(nlm_host) nh_link; /* (g) global list of hosts */
230 char nh_caller_name[MAXNAMELEN]; /* (c) printable name of host */
231 uint32_t nh_sysid; /* (c) our allocaed system ID */
232 char nh_sysid_string[10]; /* (c) string rep. of sysid */
233 struct sockaddr_storage nh_addr; /* (s) remote address of host */
234 struct nlm_rpc nh_srvrpc; /* (l) RPC for server replies */
235 struct nlm_rpc nh_clntrpc; /* (l) RPC for client requests */
236 rpcvers_t nh_vers; /* (s) NLM version of host */
237 int nh_state; /* (s) last seen NSM state of host */
238 enum nlm_host_state nh_monstate; /* (l) local NSM monitoring state */
239 time_t nh_idle_timeout; /* (s) Time at which host is idle */
240 struct sysctl_ctx_list nh_sysctl; /* (c) vfs.nlm.sysid nodes */
241 struct nlm_async_lock_list nh_pending; /* (l) pending async locks */
242 struct nlm_async_lock_list nh_finished; /* (l) finished async locks */
244 TAILQ_HEAD(nlm_host_list, nlm_host);
246 static struct nlm_host_list nlm_hosts; /* (g) */
247 static uint32_t nlm_next_sysid = 1; /* (g) */
249 static void nlm_host_unmonitor(struct nlm_host *);
251 /**********************************************************************/
254 * Initialise NLM globals.
257 nlm_init(void *dummy)
261 mtx_init(&nlm_global_lock, "nlm_global_lock", NULL, MTX_DEF);
262 TAILQ_INIT(&nlm_waiting_locks);
263 TAILQ_INIT(&nlm_hosts);
265 error = syscall_register(&nlm_syscall_offset, &nlm_syscall_sysent,
266 &nlm_syscall_prev_sysent);
268 NLM_ERR("Can't register NLM syscall\n");
270 nlm_syscall_registered = TRUE;
272 SYSINIT(nlm_init, SI_SUB_LOCK, SI_ORDER_FIRST, nlm_init, NULL);
275 nlm_uninit(void *dummy)
278 if (nlm_syscall_registered)
279 syscall_deregister(&nlm_syscall_offset,
280 &nlm_syscall_prev_sysent);
282 SYSUNINIT(nlm_uninit, SI_SUB_LOCK, SI_ORDER_FIRST, nlm_uninit, NULL);
285 * Copy a struct netobj.
288 nlm_copy_netobj(struct netobj *dst, struct netobj *src,
289 struct malloc_type *type)
292 dst->n_len = src->n_len;
293 dst->n_bytes = malloc(src->n_len, type, M_WAITOK);
294 memcpy(dst->n_bytes, src->n_bytes, src->n_len);
298 * Create an RPC client handle for the given (address,prog,vers)
302 nlm_get_rpc(struct sockaddr *sa, rpcprog_t prog, rpcvers_t vers)
304 char *wchan = "nlmrcv";
305 const char* protofmly;
306 struct sockaddr_storage ss;
312 enum clnt_stat stat = RPC_SUCCESS;
313 int rpcvers = RPCBVERS4;
314 bool_t do_tcp = FALSE;
315 bool_t tryagain = FALSE;
316 struct portmap mapping;
320 * First we need to contact the remote RPCBIND service to find
323 memcpy(&ss, sa, sa->sa_len);
324 switch (ss.ss_family) {
326 ((struct sockaddr_in *)&ss)->sin_port = htons(111);
333 ((struct sockaddr_in6 *)&ss)->sin6_port = htons(111);
341 * Unsupported address family - fail.
346 rpcb = clnt_dg_create(so, (struct sockaddr *)&ss,
347 RPCBPROG, rpcvers, 0, 0);
355 parms.r_netid = "tcp";
357 parms.r_netid = "udp";
362 * Use the default timeout.
371 * Try RPCBIND 4 then 3.
374 stat = CLNT_CALL(rpcb, (rpcprog_t) RPCBPROC_GETADDR,
375 (xdrproc_t) xdr_rpcb, &parms,
376 (xdrproc_t) xdr_wrapstring, &uaddr, timo);
377 if (stat == RPC_SUCCESS) {
379 * We have a reply from the remote RPCBIND - turn it
380 * into an appropriate address and make a new client
381 * that can talk to the remote NLM.
383 * XXX fixup IPv6 scope ID.
386 a = __rpc_uaddr2taddr_af(ss.ss_family, uaddr);
391 memcpy(&ss, a->buf, a->len);
394 xdr_free((xdrproc_t) xdr_wrapstring, &uaddr);
397 if (tryagain || stat == RPC_PROGVERSMISMATCH) {
398 if (rpcvers == RPCBVERS4)
400 else if (rpcvers == RPCBVERS)
402 CLNT_CONTROL(rpcb, CLSET_VERS, &rpcvers);
410 mapping.pm_prog = parms.r_prog;
411 mapping.pm_vers = parms.r_vers;
412 mapping.pm_prot = do_tcp ? IPPROTO_TCP : IPPROTO_UDP;
415 stat = CLNT_CALL(rpcb, (rpcprog_t) PMAPPROC_GETPORT,
416 (xdrproc_t) xdr_portmap, &mapping,
417 (xdrproc_t) xdr_u_short, &port, timo);
419 if (stat == RPC_SUCCESS) {
420 switch (ss.ss_family) {
422 ((struct sockaddr_in *)&ss)->sin_port =
428 ((struct sockaddr_in6 *)&ss)->sin6_port =
436 panic("invalid rpcvers %d", rpcvers);
439 * We may have a positive response from the portmapper, but the NLM
440 * service was not found. Make sure we received a valid port.
442 switch (ss.ss_family) {
444 port = ((struct sockaddr_in *)&ss)->sin_port;
448 port = ((struct sockaddr_in6 *)&ss)->sin6_port;
452 if (stat != RPC_SUCCESS || !port) {
454 * If we were able to talk to rpcbind or portmap, but the udp
455 * variant wasn't available, ask about tcp.
457 * XXX - We could also check for a TCP portmapper, but
458 * if the host is running a portmapper at all, we should be able
459 * to hail it over UDP.
461 if (stat == RPC_SUCCESS && !do_tcp) {
466 /* Otherwise, bad news. */
467 NLM_ERR("NLM: failed to contact remote rpcbind, "
468 "stat = %d, port = %d\n", (int) stat, port);
475 * Destroy the UDP client we used to speak to rpcbind and
476 * recreate as a TCP client.
478 struct netconfig *nconf = NULL;
482 switch (ss.ss_family) {
484 nconf = getnetconfigent("tcp");
488 nconf = getnetconfigent("tcp6");
493 rpcb = clnt_reconnect_create(nconf, (struct sockaddr *)&ss,
495 CLNT_CONTROL(rpcb, CLSET_WAITCHAN, wchan);
496 rpcb->cl_auth = nlm_auth;
500 * Re-use the client we used to speak to rpcbind.
502 CLNT_CONTROL(rpcb, CLSET_SVC_ADDR, &ss);
503 CLNT_CONTROL(rpcb, CLSET_PROG, &prog);
504 CLNT_CONTROL(rpcb, CLSET_VERS, &vers);
505 CLNT_CONTROL(rpcb, CLSET_WAITCHAN, wchan);
506 rpcb->cl_auth = nlm_auth;
513 * This async callback after when an async lock request has been
514 * granted. We notify the host which initiated the request.
517 nlm_lock_callback(void *arg, int pending)
519 struct nlm_async_lock *af = (struct nlm_async_lock *) arg;
520 struct rpc_callextra ext;
522 NLM_DEBUG(2, "NLM: async lock %p for %s (sysid %d) granted\n",
523 af, af->af_host->nh_caller_name, af->af_host->nh_sysid);
526 * Send the results back to the host.
528 * Note: there is a possible race here with nlm_host_notify
529 * destroying the RPC client. To avoid problems, the first
530 * thing nlm_host_notify does is to cancel pending async lock
533 memset(&ext, 0, sizeof(ext));
534 ext.rc_auth = nlm_auth;
535 if (af->af_host->nh_vers == NLM_VERS4) {
536 nlm4_granted_msg_4(&af->af_granted,
537 NULL, af->af_rpc, &ext, nlm_zero_tv);
540 * Back-convert to legacy protocol
542 nlm_testargs granted;
543 granted.cookie = af->af_granted.cookie;
544 granted.exclusive = af->af_granted.exclusive;
545 granted.alock.caller_name =
546 af->af_granted.alock.caller_name;
547 granted.alock.fh = af->af_granted.alock.fh;
548 granted.alock.oh = af->af_granted.alock.oh;
549 granted.alock.svid = af->af_granted.alock.svid;
550 granted.alock.l_offset =
551 af->af_granted.alock.l_offset;
552 granted.alock.l_len =
553 af->af_granted.alock.l_len;
555 nlm_granted_msg_1(&granted,
556 NULL, af->af_rpc, &ext, nlm_zero_tv);
560 * Move this entry to the nh_finished list. Someone else will
561 * free it later - its too hard to do it here safely without
562 * racing with cancel.
564 * XXX possibly we should have a third "granted sent but not
565 * ack'ed" list so that we can re-send the granted message.
567 mtx_lock(&af->af_host->nh_lock);
568 TAILQ_REMOVE(&af->af_host->nh_pending, af, af_link);
569 TAILQ_INSERT_TAIL(&af->af_host->nh_finished, af, af_link);
570 mtx_unlock(&af->af_host->nh_lock);
574 * Free an async lock request. The request must have been removed from
578 nlm_free_async_lock(struct nlm_async_lock *af)
581 * Free an async lock.
584 CLNT_RELEASE(af->af_rpc);
585 xdr_free((xdrproc_t) xdr_nlm4_testargs, &af->af_granted);
592 * Cancel our async request - this must be called with
593 * af->nh_host->nh_lock held. This is slightly complicated by a
594 * potential race with our own callback. If we fail to cancel the
595 * lock, it must already have been granted - we make sure our async
596 * task has completed by calling taskqueue_drain in this case.
599 nlm_cancel_async_lock(struct nlm_async_lock *af)
601 struct nlm_host *host = af->af_host;
604 mtx_assert(&host->nh_lock, MA_OWNED);
606 mtx_unlock(&host->nh_lock);
608 error = VOP_ADVLOCKASYNC(af->af_vp, NULL, F_CANCEL, &af->af_fl,
609 F_REMOTE, NULL, &af->af_cookie);
613 * We failed to cancel - make sure our callback has
614 * completed before we continue.
616 taskqueue_drain(taskqueue_thread, &af->af_task);
619 mtx_lock(&host->nh_lock);
622 NLM_DEBUG(2, "NLM: async lock %p for %s (sysid %d) "
623 "cancelled\n", af, host->nh_caller_name, host->nh_sysid);
626 * Remove from the nh_pending list and free now that
627 * we are safe from the callback.
629 TAILQ_REMOVE(&host->nh_pending, af, af_link);
630 mtx_unlock(&host->nh_lock);
631 nlm_free_async_lock(af);
632 mtx_lock(&host->nh_lock);
639 nlm_free_finished_locks(struct nlm_host *host)
641 struct nlm_async_lock *af;
643 mtx_lock(&host->nh_lock);
644 while ((af = TAILQ_FIRST(&host->nh_finished)) != NULL) {
645 TAILQ_REMOVE(&host->nh_finished, af, af_link);
646 mtx_unlock(&host->nh_lock);
647 nlm_free_async_lock(af);
648 mtx_lock(&host->nh_lock);
650 mtx_unlock(&host->nh_lock);
654 * Free resources used by a host. This is called after the reference
655 * count has reached zero so it doesn't need to worry about locks.
658 nlm_host_destroy(struct nlm_host *host)
661 mtx_lock(&nlm_global_lock);
662 TAILQ_REMOVE(&nlm_hosts, host, nh_link);
663 mtx_unlock(&nlm_global_lock);
665 if (host->nh_srvrpc.nr_client)
666 CLNT_RELEASE(host->nh_srvrpc.nr_client);
667 if (host->nh_clntrpc.nr_client)
668 CLNT_RELEASE(host->nh_clntrpc.nr_client);
669 mtx_destroy(&host->nh_lock);
670 sysctl_ctx_free(&host->nh_sysctl);
677 * Thread start callback for client lock recovery
680 nlm_client_recovery_start(void *arg)
682 struct nlm_host *host = (struct nlm_host *) arg;
684 NLM_DEBUG(1, "NLM: client lock recovery for %s started\n",
685 host->nh_caller_name);
687 nlm_client_recovery(host);
689 NLM_DEBUG(1, "NLM: client lock recovery for %s completed\n",
690 host->nh_caller_name);
692 host->nh_monstate = NLM_MONITORED;
693 nlm_host_release(host);
701 * This is called when we receive a host state change notification. We
702 * unlock any active locks owned by the host. When rpc.lockd is
703 * shutting down, this function is called with newstate set to zero
704 * which allows us to cancel any pending async locks and clear the
708 nlm_host_notify(struct nlm_host *host, int newstate)
710 struct nlm_async_lock *af;
713 NLM_DEBUG(1, "NLM: host %s (sysid %d) rebooted, new "
714 "state is %d\n", host->nh_caller_name,
715 host->nh_sysid, newstate);
719 * Cancel any pending async locks for this host.
721 mtx_lock(&host->nh_lock);
722 while ((af = TAILQ_FIRST(&host->nh_pending)) != NULL) {
724 * nlm_cancel_async_lock will remove the entry from
725 * nh_pending and free it.
727 nlm_cancel_async_lock(af);
729 mtx_unlock(&host->nh_lock);
730 nlm_free_finished_locks(host);
733 * The host just rebooted - trash its locks.
735 lf_clearremotesys(host->nh_sysid);
736 host->nh_state = newstate;
740 * If we have any remote locks for this host (i.e. it
741 * represents a remote NFS server that our local NFS client
742 * has locks for), start a recovery thread.
745 && host->nh_monstate != NLM_RECOVERING
746 && lf_countlocks(NLM_SYSID_CLIENT | host->nh_sysid) > 0) {
748 host->nh_monstate = NLM_RECOVERING;
749 refcount_acquire(&host->nh_refs);
750 kthread_add(nlm_client_recovery_start, host, curproc, &td, 0, 0,
751 "NFS lock recovery for %s", host->nh_caller_name);
757 * Sysctl handler to count the number of locks for a sysid.
760 nlm_host_lock_count_sysctl(SYSCTL_HANDLER_ARGS)
762 struct nlm_host *host;
765 host = oidp->oid_arg1;
766 count = lf_countlocks(host->nh_sysid);
767 return sysctl_handle_int(oidp, &count, 0, req);
771 * Sysctl handler to count the number of client locks for a sysid.
774 nlm_host_client_lock_count_sysctl(SYSCTL_HANDLER_ARGS)
776 struct nlm_host *host;
779 host = oidp->oid_arg1;
780 count = lf_countlocks(NLM_SYSID_CLIENT | host->nh_sysid);
781 return sysctl_handle_int(oidp, &count, 0, req);
785 * Create a new NLM host.
787 static struct nlm_host *
788 nlm_create_host(const char* caller_name)
790 struct nlm_host *host;
791 struct sysctl_oid *oid;
793 mtx_assert(&nlm_global_lock, MA_OWNED);
795 NLM_DEBUG(1, "NLM: new host %s (sysid %d)\n",
796 caller_name, nlm_next_sysid);
797 host = malloc(sizeof(struct nlm_host), M_NLM, M_NOWAIT|M_ZERO);
800 mtx_init(&host->nh_lock, "nh_lock", NULL, MTX_DEF);
802 strlcpy(host->nh_caller_name, caller_name, MAXNAMELEN);
803 host->nh_sysid = nlm_next_sysid++;
804 snprintf(host->nh_sysid_string, sizeof(host->nh_sysid_string),
805 "%d", host->nh_sysid);
808 host->nh_monstate = NLM_UNMONITORED;
809 TAILQ_INIT(&host->nh_pending);
810 TAILQ_INIT(&host->nh_finished);
811 TAILQ_INSERT_TAIL(&nlm_hosts, host, nh_link);
813 mtx_unlock(&nlm_global_lock);
815 sysctl_ctx_init(&host->nh_sysctl);
816 oid = SYSCTL_ADD_NODE(&host->nh_sysctl,
817 SYSCTL_STATIC_CHILDREN(_vfs_nlm_sysid),
818 OID_AUTO, host->nh_sysid_string, CTLFLAG_RD, NULL, "");
819 SYSCTL_ADD_STRING(&host->nh_sysctl, SYSCTL_CHILDREN(oid), OID_AUTO,
820 "hostname", CTLFLAG_RD, host->nh_caller_name, 0, "");
821 SYSCTL_ADD_INT(&host->nh_sysctl, SYSCTL_CHILDREN(oid), OID_AUTO,
822 "version", CTLFLAG_RD, &host->nh_vers, 0, "");
823 SYSCTL_ADD_INT(&host->nh_sysctl, SYSCTL_CHILDREN(oid), OID_AUTO,
824 "monitored", CTLFLAG_RD, &host->nh_monstate, 0, "");
825 SYSCTL_ADD_PROC(&host->nh_sysctl, SYSCTL_CHILDREN(oid), OID_AUTO,
826 "lock_count", CTLTYPE_INT | CTLFLAG_RD, host, 0,
827 nlm_host_lock_count_sysctl, "I", "");
828 SYSCTL_ADD_PROC(&host->nh_sysctl, SYSCTL_CHILDREN(oid), OID_AUTO,
829 "client_lock_count", CTLTYPE_INT | CTLFLAG_RD, host, 0,
830 nlm_host_client_lock_count_sysctl, "I", "");
832 mtx_lock(&nlm_global_lock);
838 * Return non-zero if the address parts of the two sockaddrs are the
842 nlm_compare_addr(const struct sockaddr *a, const struct sockaddr *b)
844 const struct sockaddr_in *a4, *b4;
846 const struct sockaddr_in6 *a6, *b6;
849 if (a->sa_family != b->sa_family)
852 switch (a->sa_family) {
854 a4 = (const struct sockaddr_in *) a;
855 b4 = (const struct sockaddr_in *) b;
856 return !memcmp(&a4->sin_addr, &b4->sin_addr,
857 sizeof(a4->sin_addr));
860 a6 = (const struct sockaddr_in6 *) a;
861 b6 = (const struct sockaddr_in6 *) b;
862 return !memcmp(&a6->sin6_addr, &b6->sin6_addr,
863 sizeof(a6->sin6_addr));
871 * Check for idle hosts and stop monitoring them. We could also free
872 * the host structure here, possibly after a larger timeout but that
873 * would require some care to avoid races with
874 * e.g. nlm_host_lock_count_sysctl.
879 struct nlm_host *host;
881 mtx_assert(&nlm_global_lock, MA_OWNED);
883 if (time_uptime <= nlm_next_idle_check)
886 nlm_next_idle_check = time_uptime + NLM_IDLE_PERIOD;
888 TAILQ_FOREACH(host, &nlm_hosts, nh_link) {
889 if (host->nh_monstate == NLM_MONITORED
890 && time_uptime > host->nh_idle_timeout) {
891 mtx_unlock(&nlm_global_lock);
892 if (lf_countlocks(host->nh_sysid) > 0
893 || lf_countlocks(NLM_SYSID_CLIENT
895 host->nh_idle_timeout =
896 time_uptime + NLM_IDLE_TIMEOUT;
897 mtx_lock(&nlm_global_lock);
900 nlm_host_unmonitor(host);
901 mtx_lock(&nlm_global_lock);
907 * Search for an existing NLM host that matches the given name
908 * (typically the caller_name element of an nlm4_lock). If none is
909 * found, create a new host. If 'addr' is non-NULL, record the remote
910 * address of the host so that we can call it back for async
911 * responses. If 'vers' is greater than zero then record the NLM
912 * program version to use to communicate with this client.
915 nlm_find_host_by_name(const char *name, const struct sockaddr *addr,
918 struct nlm_host *host;
920 mtx_lock(&nlm_global_lock);
923 * The remote host is determined by caller_name.
925 TAILQ_FOREACH(host, &nlm_hosts, nh_link) {
926 if (!strcmp(host->nh_caller_name, name))
931 host = nlm_create_host(name);
933 mtx_unlock(&nlm_global_lock);
937 refcount_acquire(&host->nh_refs);
939 host->nh_idle_timeout = time_uptime + NLM_IDLE_TIMEOUT;
942 * If we have an address for the host, record it so that we
943 * can send async replies etc.
947 KASSERT(addr->sa_len < sizeof(struct sockaddr_storage),
948 ("Strange remote transport address length"));
951 * If we have seen an address before and we currently
952 * have an RPC client handle, make sure the address is
953 * the same, otherwise discard the client handle.
955 if (host->nh_addr.ss_len && host->nh_srvrpc.nr_client) {
956 if (!nlm_compare_addr(
957 (struct sockaddr *) &host->nh_addr,
959 || host->nh_vers != vers) {
961 mtx_lock(&host->nh_lock);
962 client = host->nh_srvrpc.nr_client;
963 host->nh_srvrpc.nr_client = NULL;
964 mtx_unlock(&host->nh_lock);
966 CLNT_RELEASE(client);
970 memcpy(&host->nh_addr, addr, addr->sa_len);
971 host->nh_vers = vers;
976 mtx_unlock(&nlm_global_lock);
982 * Search for an existing NLM host that matches the given remote
983 * address. If none is found, create a new host with the requested
984 * address and remember 'vers' as the NLM protocol version to use for
988 nlm_find_host_by_addr(const struct sockaddr *addr, int vers)
991 * Fake up a name using inet_ntop. This buffer is
992 * large enough for an IPv6 address.
994 char tmp[sizeof "ffff:ffff:ffff:ffff:ffff:ffff:255.255.255.255"];
995 struct nlm_host *host;
997 switch (addr->sa_family) {
999 __rpc_inet_ntop(AF_INET,
1000 &((const struct sockaddr_in *) addr)->sin_addr,
1005 __rpc_inet_ntop(AF_INET6,
1006 &((const struct sockaddr_in6 *) addr)->sin6_addr,
1011 strcmp(tmp, "<unknown>");
1015 mtx_lock(&nlm_global_lock);
1018 * The remote host is determined by caller_name.
1020 TAILQ_FOREACH(host, &nlm_hosts, nh_link) {
1021 if (nlm_compare_addr(addr,
1022 (const struct sockaddr *) &host->nh_addr))
1027 host = nlm_create_host(tmp);
1029 mtx_unlock(&nlm_global_lock);
1032 memcpy(&host->nh_addr, addr, addr->sa_len);
1033 host->nh_vers = vers;
1035 refcount_acquire(&host->nh_refs);
1037 host->nh_idle_timeout = time_uptime + NLM_IDLE_TIMEOUT;
1041 mtx_unlock(&nlm_global_lock);
1047 * Find the NLM host that matches the value of 'sysid'. If none
1048 * exists, return NULL.
1050 static struct nlm_host *
1051 nlm_find_host_by_sysid(int sysid)
1053 struct nlm_host *host;
1055 TAILQ_FOREACH(host, &nlm_hosts, nh_link) {
1056 if (host->nh_sysid == sysid) {
1057 refcount_acquire(&host->nh_refs);
1065 void nlm_host_release(struct nlm_host *host)
1067 if (refcount_release(&host->nh_refs)) {
1071 nlm_host_destroy(host);
1076 * Unregister this NLM host with the local NSM due to idleness.
1079 nlm_host_unmonitor(struct nlm_host *host)
1083 struct timeval timo;
1084 enum clnt_stat stat;
1086 NLM_DEBUG(1, "NLM: unmonitoring %s (sysid %d)\n",
1087 host->nh_caller_name, host->nh_sysid);
1090 * We put our assigned system ID value in the priv field to
1091 * make it simpler to find the host if we are notified of a
1094 smmonid.mon_name = host->nh_caller_name;
1095 smmonid.my_id.my_name = "localhost";
1096 smmonid.my_id.my_prog = NLM_PROG;
1097 smmonid.my_id.my_vers = NLM_SM;
1098 smmonid.my_id.my_proc = NLM_SM_NOTIFY;
1102 stat = CLNT_CALL(nlm_nsm, SM_UNMON,
1103 (xdrproc_t) xdr_mon, &smmonid,
1104 (xdrproc_t) xdr_sm_stat, &smstat, timo);
1106 if (stat != RPC_SUCCESS) {
1107 NLM_ERR("Failed to contact local NSM - rpc error %d\n", stat);
1110 if (smstat.res_stat == stat_fail) {
1111 NLM_ERR("Local NSM refuses to unmonitor %s\n",
1112 host->nh_caller_name);
1116 host->nh_monstate = NLM_UNMONITORED;
1120 * Register this NLM host with the local NSM so that we can be
1121 * notified if it reboots.
1124 nlm_host_monitor(struct nlm_host *host, int state)
1128 struct timeval timo;
1129 enum clnt_stat stat;
1131 if (state && !host->nh_state) {
1133 * This is the first time we have seen an NSM state
1134 * value for this host. We record it here to help
1135 * detect host reboots.
1137 host->nh_state = state;
1138 NLM_DEBUG(1, "NLM: host %s (sysid %d) has NSM state %d\n",
1139 host->nh_caller_name, host->nh_sysid, state);
1142 mtx_lock(&host->nh_lock);
1143 if (host->nh_monstate != NLM_UNMONITORED) {
1144 mtx_unlock(&host->nh_lock);
1147 host->nh_monstate = NLM_MONITORED;
1148 mtx_unlock(&host->nh_lock);
1150 NLM_DEBUG(1, "NLM: monitoring %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 smmon.mon_id.mon_name = host->nh_caller_name;
1159 smmon.mon_id.my_id.my_name = "localhost";
1160 smmon.mon_id.my_id.my_prog = NLM_PROG;
1161 smmon.mon_id.my_id.my_vers = NLM_SM;
1162 smmon.mon_id.my_id.my_proc = NLM_SM_NOTIFY;
1163 memcpy(smmon.priv, &host->nh_sysid, sizeof(host->nh_sysid));
1167 stat = CLNT_CALL(nlm_nsm, SM_MON,
1168 (xdrproc_t) xdr_mon, &smmon,
1169 (xdrproc_t) xdr_sm_stat, &smstat, timo);
1171 if (stat != RPC_SUCCESS) {
1172 NLM_ERR("Failed to contact local NSM - rpc error %d\n", stat);
1175 if (smstat.res_stat == stat_fail) {
1176 NLM_ERR("Local NSM refuses to monitor %s\n",
1177 host->nh_caller_name);
1178 mtx_lock(&host->nh_lock);
1179 host->nh_monstate = NLM_MONITOR_FAILED;
1180 mtx_unlock(&host->nh_lock);
1184 host->nh_monstate = NLM_MONITORED;
1188 * Return an RPC client handle that can be used to talk to the NLM
1189 * running on the given host.
1192 nlm_host_get_rpc(struct nlm_host *host, bool_t isserver)
1194 struct nlm_rpc *rpc;
1197 mtx_lock(&host->nh_lock);
1200 rpc = &host->nh_srvrpc;
1202 rpc = &host->nh_clntrpc;
1205 * We can't hold onto RPC handles for too long - the async
1206 * call/reply protocol used by some NLM clients makes it hard
1207 * to tell when they change port numbers (e.g. after a
1208 * reboot). Note that if a client reboots while it isn't
1209 * holding any locks, it won't bother to notify us. We
1210 * expire the RPC handles after two minutes.
1212 if (rpc->nr_client && time_uptime > rpc->nr_create_time + 2*60) {
1213 client = rpc->nr_client;
1214 rpc->nr_client = NULL;
1215 mtx_unlock(&host->nh_lock);
1216 CLNT_RELEASE(client);
1217 mtx_lock(&host->nh_lock);
1220 if (!rpc->nr_client) {
1221 mtx_unlock(&host->nh_lock);
1222 client = nlm_get_rpc((struct sockaddr *)&host->nh_addr,
1223 NLM_PROG, host->nh_vers);
1224 mtx_lock(&host->nh_lock);
1227 if (rpc->nr_client) {
1228 mtx_unlock(&host->nh_lock);
1229 CLNT_DESTROY(client);
1230 mtx_lock(&host->nh_lock);
1232 rpc->nr_client = client;
1233 rpc->nr_create_time = time_uptime;
1238 client = rpc->nr_client;
1240 CLNT_ACQUIRE(client);
1241 mtx_unlock(&host->nh_lock);
1247 int nlm_host_get_sysid(struct nlm_host *host)
1250 return (host->nh_sysid);
1254 nlm_host_get_state(struct nlm_host *host)
1257 return (host->nh_state);
1261 nlm_register_wait_lock(struct nlm4_lock *lock, struct vnode *vp)
1263 struct nlm_waiting_lock *nw;
1265 nw = malloc(sizeof(struct nlm_waiting_lock), M_NLM, M_WAITOK);
1266 nw->nw_lock = *lock;
1267 memcpy(&nw->nw_fh.fh_bytes, nw->nw_lock.fh.n_bytes,
1268 nw->nw_lock.fh.n_len);
1269 nw->nw_lock.fh.n_bytes = nw->nw_fh.fh_bytes;
1270 nw->nw_waiting = TRUE;
1272 mtx_lock(&nlm_global_lock);
1273 TAILQ_INSERT_TAIL(&nlm_waiting_locks, nw, nw_link);
1274 mtx_unlock(&nlm_global_lock);
1280 nlm_deregister_wait_lock(void *handle)
1282 struct nlm_waiting_lock *nw = handle;
1284 mtx_lock(&nlm_global_lock);
1285 TAILQ_REMOVE(&nlm_waiting_locks, nw, nw_link);
1286 mtx_unlock(&nlm_global_lock);
1292 nlm_wait_lock(void *handle, int timo)
1294 struct nlm_waiting_lock *nw = handle;
1298 * If the granted message arrived before we got here,
1299 * nw->nw_waiting will be FALSE - in that case, don't sleep.
1301 mtx_lock(&nlm_global_lock);
1304 error = msleep(nw, &nlm_global_lock, PCATCH, "nlmlock", timo);
1305 TAILQ_REMOVE(&nlm_waiting_locks, nw, nw_link);
1308 * The granted message may arrive after the
1309 * interrupt/timeout but before we manage to lock the
1310 * mutex. Detect this by examining nw_lock.
1312 if (!nw->nw_waiting)
1316 * If nlm_cancel_wait is called, then error will be
1317 * zero but nw_waiting will still be TRUE. We
1318 * translate this into EINTR.
1323 mtx_unlock(&nlm_global_lock);
1331 nlm_cancel_wait(struct vnode *vp)
1333 struct nlm_waiting_lock *nw;
1335 mtx_lock(&nlm_global_lock);
1336 TAILQ_FOREACH(nw, &nlm_waiting_locks, nw_link) {
1337 if (nw->nw_vp == vp) {
1341 mtx_unlock(&nlm_global_lock);
1345 /**********************************************************************/
1348 * Syscall interface with userland.
1351 extern void nlm_prog_0(struct svc_req *rqstp, SVCXPRT *transp);
1352 extern void nlm_prog_1(struct svc_req *rqstp, SVCXPRT *transp);
1353 extern void nlm_prog_3(struct svc_req *rqstp, SVCXPRT *transp);
1354 extern void nlm_prog_4(struct svc_req *rqstp, SVCXPRT *transp);
1357 nlm_register_services(SVCPOOL *pool, int addr_count, char **addrs)
1359 static rpcvers_t versions[] = {
1360 NLM_SM, NLM_VERS, NLM_VERSX, NLM_VERS4
1362 static void (*dispatchers[])(struct svc_req *, SVCXPRT *) = {
1363 nlm_prog_0, nlm_prog_1, nlm_prog_3, nlm_prog_4
1365 static const int version_count = sizeof(versions) / sizeof(versions[0]);
1370 struct netconfig *nconf;
1374 NLM_ERR("NLM: no service addresses given - can't start server");
1378 xprts = malloc(addr_count * sizeof(SVCXPRT *), M_NLM, M_WAITOK);
1379 for (i = 0; i < version_count; i++) {
1380 for (j = 0; j < addr_count; j++) {
1382 * Create transports for the first version and
1383 * then just register everything else to the
1389 error = copyin(&addrs[2*j], &up,
1393 error = copyinstr(up, netid, sizeof(netid),
1397 error = copyin(&addrs[2*j+1], &up,
1401 error = copyinstr(up, uaddr, sizeof(uaddr),
1405 nconf = getnetconfigent(netid);
1407 NLM_ERR("Can't lookup netid %s\n",
1412 xprts[j] = svc_tp_create(pool, dispatchers[i],
1413 NLM_PROG, versions[i], uaddr, nconf);
1415 NLM_ERR("NLM: unable to create "
1416 "(NLM_PROG, %d).\n", versions[i]);
1420 freenetconfigent(nconf);
1422 nconf = getnetconfigent(xprts[j]->xp_netid);
1423 rpcb_unset(NLM_PROG, versions[i], nconf);
1424 if (!svc_reg(xprts[j], NLM_PROG, versions[i],
1425 dispatchers[i], nconf)) {
1426 NLM_ERR("NLM: can't register "
1427 "(NLM_PROG, %d)\n", versions[i]);
1441 * Main server entry point. Contacts the local NSM to get its current
1442 * state and send SM_UNMON_ALL. Registers the NLM services and then
1443 * services requests. Does not return until the server is interrupted
1447 nlm_server_main(int addr_count, char **addrs)
1449 struct thread *td = curthread;
1451 SVCPOOL *pool = NULL;
1455 struct sockaddr_in6 sin6;
1457 struct sockaddr_in sin;
1460 struct timeval timo;
1461 enum clnt_stat stat;
1462 struct nlm_host *host, *nhost;
1463 struct nlm_waiting_lock *nw;
1465 vop_advlock_t *old_nfs_advlock;
1466 vop_reclaim_t *old_nfs_reclaim;
1474 NLM_ERR("NLM: can't start server - "
1475 "it appears to be running already\n");
1479 memset(&opt, 0, sizeof(opt));
1482 error = socreate(AF_INET, &nlm_socket, SOCK_DGRAM, 0,
1485 NLM_ERR("NLM: can't create IPv4 socket - error %d\n", error);
1488 opt.sopt_dir = SOPT_SET;
1489 opt.sopt_level = IPPROTO_IP;
1490 opt.sopt_name = IP_PORTRANGE;
1491 portlow = IP_PORTRANGE_LOW;
1492 opt.sopt_val = &portlow;
1493 opt.sopt_valsize = sizeof(portlow);
1494 sosetopt(nlm_socket, &opt);
1498 error = socreate(AF_INET6, &nlm_socket6, SOCK_DGRAM, 0,
1501 NLM_ERR("NLM: can't create IPv6 socket - error %d\n", error);
1505 opt.sopt_dir = SOPT_SET;
1506 opt.sopt_level = IPPROTO_IPV6;
1507 opt.sopt_name = IPV6_PORTRANGE;
1508 portlow = IPV6_PORTRANGE_LOW;
1509 opt.sopt_val = &portlow;
1510 opt.sopt_valsize = sizeof(portlow);
1511 sosetopt(nlm_socket6, &opt);
1514 nlm_auth = authunix_create(curthread->td_ucred);
1517 memset(&sin6, 0, sizeof(sin6));
1518 sin6.sin6_len = sizeof(sin6);
1519 sin6.sin6_family = AF_INET6;
1520 sin6.sin6_addr = in6addr_loopback;
1521 nlm_nsm = nlm_get_rpc((struct sockaddr *) &sin6, SM_PROG, SM_VERS);
1524 memset(&sin, 0, sizeof(sin));
1525 sin.sin_len = sizeof(sin);
1526 sin.sin_family = AF_INET;
1527 sin.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
1528 nlm_nsm = nlm_get_rpc((struct sockaddr *) &sin, SM_PROG,
1535 NLM_ERR("Can't start NLM - unable to contact NSM\n");
1540 pool = svcpool_create("NLM", NULL);
1542 error = nlm_register_services(pool, addr_count, addrs);
1546 memset(&id, 0, sizeof(id));
1547 id.my_name = "NFS NLM";
1551 stat = CLNT_CALL(nlm_nsm, SM_UNMON_ALL,
1552 (xdrproc_t) xdr_my_id, &id,
1553 (xdrproc_t) xdr_sm_stat, &smstat, timo);
1555 if (stat != RPC_SUCCESS) {
1558 CLNT_GETERR(nlm_nsm, &err);
1559 NLM_ERR("NLM: unexpected error contacting NSM, "
1560 "stat=%d, errno=%d\n", stat, err.re_errno);
1565 NLM_DEBUG(1, "NLM: local NSM state is %d\n", smstat.state);
1566 nlm_nsm_state = smstat.state;
1569 old_nfs_advlock = nfs_advlock_p;
1570 nfs_advlock_p = nlm_advlock;
1571 old_nfs_reclaim = nfs_reclaim_p;
1572 nfs_reclaim_p = nlm_reclaim;
1579 nfs_advlock_p = old_nfs_advlock;
1580 nfs_reclaim_p = old_nfs_reclaim;
1585 svcpool_destroy(pool);
1588 * We are finished communicating with the NSM.
1591 CLNT_RELEASE(nlm_nsm);
1596 * Trash all the existing state so that if the server
1597 * restarts, it gets a clean slate. This is complicated by the
1598 * possibility that there may be other threads trying to make
1599 * client locking requests.
1601 * First we fake a client reboot notification which will
1602 * cancel any pending async locks and purge remote lock state
1603 * from the local lock manager. We release the reference from
1604 * nlm_hosts to the host (which may remove it from the list
1605 * and free it). After this phase, the only entries in the
1606 * nlm_host list should be from other threads performing
1607 * client lock requests. We arrange to defer closing the
1608 * sockets until the last RPC client handle is released.
1614 mtx_lock(&nlm_global_lock);
1615 TAILQ_FOREACH(nw, &nlm_waiting_locks, nw_link) {
1618 TAILQ_FOREACH_SAFE(host, &nlm_hosts, nh_link, nhost) {
1619 mtx_unlock(&nlm_global_lock);
1620 nlm_host_notify(host, 0);
1621 nlm_host_release(host);
1622 mtx_lock(&nlm_global_lock);
1624 TAILQ_FOREACH_SAFE(host, &nlm_hosts, nh_link, nhost) {
1625 mtx_lock(&host->nh_lock);
1626 if (host->nh_srvrpc.nr_client
1627 || host->nh_clntrpc.nr_client) {
1628 if (host->nh_addr.ss_family == AF_INET)
1631 if (host->nh_addr.ss_family == AF_INET6)
1635 * Note that the rpc over udp code copes
1636 * correctly with the fact that a socket may
1637 * be used by many rpc handles.
1639 if (host->nh_srvrpc.nr_client)
1640 CLNT_CONTROL(host->nh_srvrpc.nr_client,
1642 if (host->nh_clntrpc.nr_client)
1643 CLNT_CONTROL(host->nh_clntrpc.nr_client,
1646 mtx_unlock(&host->nh_lock);
1648 mtx_unlock(&nlm_global_lock);
1650 AUTH_DESTROY(nlm_auth);
1653 soclose(nlm_socket);
1657 soclose(nlm_socket6);
1665 nlm_syscall(struct thread *td, struct nlm_syscall_args *uap)
1669 #if __FreeBSD_version >= 700000
1670 error = priv_check(td, PRIV_NFS_LOCKD);
1677 nlm_debug_level = uap->debug_level;
1678 nlm_grace_threshold = time_uptime + uap->grace_period;
1679 nlm_next_idle_check = time_uptime + NLM_IDLE_PERIOD;
1681 return nlm_server_main(uap->addr_count, uap->addrs);
1684 /**********************************************************************/
1687 * NLM implementation details, called from the RPC stubs.
1692 nlm_sm_notify(struct nlm_sm_status *argp)
1695 struct nlm_host *host;
1697 NLM_DEBUG(3, "nlm_sm_notify(): mon_name = %s\n", argp->mon_name);
1698 memcpy(&sysid, &argp->priv, sizeof(sysid));
1699 host = nlm_find_host_by_sysid(sysid);
1701 nlm_host_notify(host, argp->state);
1702 nlm_host_release(host);
1707 nlm_convert_to_fhandle_t(fhandle_t *fhp, struct netobj *p)
1709 memcpy(fhp, p->n_bytes, sizeof(fhandle_t));
1713 struct mount *vs_mp;
1714 struct vnode *vs_vp;
1720 nlm_get_vfs_state(struct nlm_host *host, struct svc_req *rqstp,
1721 fhandle_t *fhp, struct vfs_state *vs)
1724 struct ucred *cred = NULL, *credanon;
1726 memset(vs, 0, sizeof(*vs));
1728 vs->vs_mp = vfs_getvfs(&fhp->fh_fsid);
1732 vs->vs_vfslocked = VFS_LOCK_GIANT(vs->vs_mp);
1734 error = VFS_CHECKEXP(vs->vs_mp, (struct sockaddr *)&host->nh_addr,
1735 &exflags, &credanon, NULL, NULL);
1739 if (exflags & MNT_EXRDONLY || (vs->vs_mp->mnt_flag & MNT_RDONLY)) {
1744 error = VFS_FHTOVP(vs->vs_mp, &fhp->fh_fid, &vs->vs_vp);
1747 vs->vs_vnlocked = TRUE;
1749 if (!svc_getcred(rqstp, &cred, NULL)) {
1753 if (cred->cr_uid == 0 || (exflags & MNT_EXPORTANON)) {
1755 cred = crhold(credanon);
1761 error = VOP_ACCESS(vs->vs_vp, VWRITE, cred, curthread);
1765 #if __FreeBSD_version < 800011
1766 VOP_UNLOCK(vs->vs_vp, 0, curthread);
1768 VOP_UNLOCK(vs->vs_vp, 0);
1770 vs->vs_vnlocked = FALSE;
1780 nlm_release_vfs_state(struct vfs_state *vs)
1784 if (vs->vs_vnlocked)
1791 VFS_UNLOCK_GIANT(vs->vs_vfslocked);
1795 nlm_convert_error(int error)
1798 if (error == ESTALE)
1799 return nlm4_stale_fh;
1800 else if (error == EROFS)
1807 nlm_do_test(nlm4_testargs *argp, nlm4_testres *result, struct svc_req *rqstp,
1811 struct vfs_state vs;
1812 struct nlm_host *host, *bhost;
1816 memset(result, 0, sizeof(*result));
1817 memset(&vs, 0, sizeof(vs));
1819 host = nlm_find_host_by_name(argp->alock.caller_name,
1820 svc_getrpccaller(rqstp), rqstp->rq_vers);
1822 result->stat.stat = nlm4_denied_nolocks;
1826 NLM_DEBUG(3, "nlm_do_test(): caller_name = %s (sysid = %d)\n",
1827 host->nh_caller_name, host->nh_sysid);
1829 nlm_free_finished_locks(host);
1830 sysid = host->nh_sysid;
1832 nlm_convert_to_fhandle_t(&fh, &argp->alock.fh);
1833 nlm_copy_netobj(&result->cookie, &argp->cookie, M_RPC);
1835 if (time_uptime < nlm_grace_threshold) {
1836 result->stat.stat = nlm4_denied_grace_period;
1840 error = nlm_get_vfs_state(host, rqstp, &fh, &vs);
1842 result->stat.stat = nlm_convert_error(error);
1846 fl.l_start = argp->alock.l_offset;
1847 fl.l_len = argp->alock.l_len;
1848 fl.l_pid = argp->alock.svid;
1850 fl.l_whence = SEEK_SET;
1851 if (argp->exclusive)
1852 fl.l_type = F_WRLCK;
1854 fl.l_type = F_RDLCK;
1855 error = VOP_ADVLOCK(vs.vs_vp, NULL, F_GETLK, &fl, F_REMOTE);
1857 result->stat.stat = nlm4_failed;
1861 if (fl.l_type == F_UNLCK) {
1862 result->stat.stat = nlm4_granted;
1864 result->stat.stat = nlm4_denied;
1865 result->stat.nlm4_testrply_u.holder.exclusive =
1866 (fl.l_type == F_WRLCK);
1867 result->stat.nlm4_testrply_u.holder.svid = fl.l_pid;
1868 bhost = nlm_find_host_by_sysid(fl.l_sysid);
1871 * We don't have any useful way of recording
1872 * the value of oh used in the original lock
1873 * request. Ideally, the test reply would have
1874 * a space for the owning host's name allowing
1875 * our caller's NLM to keep track.
1877 * As far as I can see, Solaris uses an eight
1878 * byte structure for oh which contains a four
1879 * byte pid encoded in local byte order and
1880 * the first four bytes of the host
1881 * name. Linux uses a variable length string
1882 * 'pid@hostname' in ascii but doesn't even
1883 * return that in test replies.
1885 * For the moment, return nothing in oh
1886 * (already zero'ed above).
1888 nlm_host_release(bhost);
1890 result->stat.nlm4_testrply_u.holder.l_offset = fl.l_start;
1891 result->stat.nlm4_testrply_u.holder.l_len = fl.l_len;
1895 nlm_release_vfs_state(&vs);
1897 *rpcp = nlm_host_get_rpc(host, TRUE);
1898 nlm_host_release(host);
1903 nlm_do_lock(nlm4_lockargs *argp, nlm4_res *result, struct svc_req *rqstp,
1904 bool_t monitor, CLIENT **rpcp)
1907 struct vfs_state vs;
1908 struct nlm_host *host;
1912 memset(result, 0, sizeof(*result));
1913 memset(&vs, 0, sizeof(vs));
1915 host = nlm_find_host_by_name(argp->alock.caller_name,
1916 svc_getrpccaller(rqstp), rqstp->rq_vers);
1918 result->stat.stat = nlm4_denied_nolocks;
1922 NLM_DEBUG(3, "nlm_do_lock(): caller_name = %s (sysid = %d)\n",
1923 host->nh_caller_name, host->nh_sysid);
1925 if (monitor && host->nh_state && argp->state
1926 && host->nh_state != argp->state) {
1928 * The host rebooted without telling us. Trash its
1931 nlm_host_notify(host, argp->state);
1934 nlm_free_finished_locks(host);
1935 sysid = host->nh_sysid;
1937 nlm_convert_to_fhandle_t(&fh, &argp->alock.fh);
1938 nlm_copy_netobj(&result->cookie, &argp->cookie, M_RPC);
1940 if (time_uptime < nlm_grace_threshold && !argp->reclaim) {
1941 result->stat.stat = nlm4_denied_grace_period;
1945 error = nlm_get_vfs_state(host, rqstp, &fh, &vs);
1947 result->stat.stat = nlm_convert_error(error);
1951 fl.l_start = argp->alock.l_offset;
1952 fl.l_len = argp->alock.l_len;
1953 fl.l_pid = argp->alock.svid;
1955 fl.l_whence = SEEK_SET;
1956 if (argp->exclusive)
1957 fl.l_type = F_WRLCK;
1959 fl.l_type = F_RDLCK;
1961 struct nlm_async_lock *af;
1965 * First, make sure we can contact the host's NLM.
1967 client = nlm_host_get_rpc(host, TRUE);
1969 result->stat.stat = nlm4_failed;
1974 * First we need to check and see if there is an
1975 * existing blocked lock that matches. This could be a
1976 * badly behaved client or an RPC re-send. If we find
1977 * one, just return nlm4_blocked.
1979 mtx_lock(&host->nh_lock);
1980 TAILQ_FOREACH(af, &host->nh_pending, af_link) {
1981 if (af->af_fl.l_start == fl.l_start
1982 && af->af_fl.l_len == fl.l_len
1983 && af->af_fl.l_pid == fl.l_pid
1984 && af->af_fl.l_type == fl.l_type) {
1988 mtx_unlock(&host->nh_lock);
1990 CLNT_RELEASE(client);
1991 result->stat.stat = nlm4_blocked;
1995 af = malloc(sizeof(struct nlm_async_lock), M_NLM,
1997 TASK_INIT(&af->af_task, 0, nlm_lock_callback, af);
1998 af->af_vp = vs.vs_vp;
2001 af->af_rpc = client;
2003 * We use M_RPC here so that we can xdr_free the thing
2006 af->af_granted.exclusive = argp->exclusive;
2007 af->af_granted.alock.caller_name =
2008 strdup(argp->alock.caller_name, M_RPC);
2009 nlm_copy_netobj(&af->af_granted.alock.fh,
2010 &argp->alock.fh, M_RPC);
2011 nlm_copy_netobj(&af->af_granted.alock.oh,
2012 &argp->alock.oh, M_RPC);
2013 af->af_granted.alock.svid = argp->alock.svid;
2014 af->af_granted.alock.l_offset = argp->alock.l_offset;
2015 af->af_granted.alock.l_len = argp->alock.l_len;
2018 * Put the entry on the pending list before calling
2019 * VOP_ADVLOCKASYNC. We do this in case the lock
2020 * request was blocked (returning EINPROGRESS) but
2021 * then granted before we manage to run again. The
2022 * client may receive the granted message before we
2023 * send our blocked reply but thats their problem.
2025 mtx_lock(&host->nh_lock);
2026 TAILQ_INSERT_TAIL(&host->nh_pending, af, af_link);
2027 mtx_unlock(&host->nh_lock);
2029 error = VOP_ADVLOCKASYNC(vs.vs_vp, NULL, F_SETLK, &fl, F_REMOTE,
2030 &af->af_task, &af->af_cookie);
2033 * If the lock completed synchronously, just free the
2034 * tracking structure now.
2036 if (error != EINPROGRESS) {
2037 CLNT_RELEASE(af->af_rpc);
2038 mtx_lock(&host->nh_lock);
2039 TAILQ_REMOVE(&host->nh_pending, af, af_link);
2040 mtx_unlock(&host->nh_lock);
2041 xdr_free((xdrproc_t) xdr_nlm4_testargs,
2045 NLM_DEBUG(2, "NLM: pending async lock %p for %s "
2046 "(sysid %d)\n", af, host->nh_caller_name, sysid);
2048 * Don't vrele the vnode just yet - this must
2049 * wait until either the async callback
2050 * happens or the lock is cancelled.
2055 error = VOP_ADVLOCK(vs.vs_vp, NULL, F_SETLK, &fl, F_REMOTE);
2059 if (error == EINPROGRESS) {
2060 result->stat.stat = nlm4_blocked;
2061 } else if (error == EDEADLK) {
2062 result->stat.stat = nlm4_deadlck;
2063 } else if (error == EAGAIN) {
2064 result->stat.stat = nlm4_denied;
2066 result->stat.stat = nlm4_failed;
2070 nlm_host_monitor(host, argp->state);
2071 result->stat.stat = nlm4_granted;
2075 nlm_release_vfs_state(&vs);
2077 *rpcp = nlm_host_get_rpc(host, TRUE);
2078 nlm_host_release(host);
2083 nlm_do_cancel(nlm4_cancargs *argp, nlm4_res *result, struct svc_req *rqstp,
2087 struct vfs_state vs;
2088 struct nlm_host *host;
2091 struct nlm_async_lock *af;
2093 memset(result, 0, sizeof(*result));
2094 memset(&vs, 0, sizeof(vs));
2096 host = nlm_find_host_by_name(argp->alock.caller_name,
2097 svc_getrpccaller(rqstp), rqstp->rq_vers);
2099 result->stat.stat = nlm4_denied_nolocks;
2103 NLM_DEBUG(3, "nlm_do_cancel(): caller_name = %s (sysid = %d)\n",
2104 host->nh_caller_name, host->nh_sysid);
2106 nlm_free_finished_locks(host);
2107 sysid = host->nh_sysid;
2109 nlm_convert_to_fhandle_t(&fh, &argp->alock.fh);
2110 nlm_copy_netobj(&result->cookie, &argp->cookie, M_RPC);
2112 if (time_uptime < nlm_grace_threshold) {
2113 result->stat.stat = nlm4_denied_grace_period;
2117 error = nlm_get_vfs_state(host, rqstp, &fh, &vs);
2119 result->stat.stat = nlm_convert_error(error);
2123 fl.l_start = argp->alock.l_offset;
2124 fl.l_len = argp->alock.l_len;
2125 fl.l_pid = argp->alock.svid;
2127 fl.l_whence = SEEK_SET;
2128 if (argp->exclusive)
2129 fl.l_type = F_WRLCK;
2131 fl.l_type = F_RDLCK;
2134 * First we need to try and find the async lock request - if
2135 * there isn't one, we give up and return nlm4_denied.
2137 mtx_lock(&host->nh_lock);
2139 TAILQ_FOREACH(af, &host->nh_pending, af_link) {
2140 if (af->af_fl.l_start == fl.l_start
2141 && af->af_fl.l_len == fl.l_len
2142 && af->af_fl.l_pid == fl.l_pid
2143 && af->af_fl.l_type == fl.l_type) {
2149 mtx_unlock(&host->nh_lock);
2150 result->stat.stat = nlm4_denied;
2154 error = nlm_cancel_async_lock(af);
2157 result->stat.stat = nlm4_denied;
2159 result->stat.stat = nlm4_granted;
2162 mtx_unlock(&host->nh_lock);
2165 nlm_release_vfs_state(&vs);
2167 *rpcp = nlm_host_get_rpc(host, TRUE);
2168 nlm_host_release(host);
2173 nlm_do_unlock(nlm4_unlockargs *argp, nlm4_res *result, struct svc_req *rqstp,
2177 struct vfs_state vs;
2178 struct nlm_host *host;
2182 memset(result, 0, sizeof(*result));
2183 memset(&vs, 0, sizeof(vs));
2185 host = nlm_find_host_by_name(argp->alock.caller_name,
2186 svc_getrpccaller(rqstp), rqstp->rq_vers);
2188 result->stat.stat = nlm4_denied_nolocks;
2192 NLM_DEBUG(3, "nlm_do_unlock(): caller_name = %s (sysid = %d)\n",
2193 host->nh_caller_name, host->nh_sysid);
2195 nlm_free_finished_locks(host);
2196 sysid = host->nh_sysid;
2198 nlm_convert_to_fhandle_t(&fh, &argp->alock.fh);
2199 nlm_copy_netobj(&result->cookie, &argp->cookie, M_RPC);
2201 if (time_uptime < nlm_grace_threshold) {
2202 result->stat.stat = nlm4_denied_grace_period;
2206 error = nlm_get_vfs_state(host, rqstp, &fh, &vs);
2208 result->stat.stat = nlm_convert_error(error);
2212 fl.l_start = argp->alock.l_offset;
2213 fl.l_len = argp->alock.l_len;
2214 fl.l_pid = argp->alock.svid;
2216 fl.l_whence = SEEK_SET;
2217 fl.l_type = F_UNLCK;
2218 error = VOP_ADVLOCK(vs.vs_vp, NULL, F_UNLCK, &fl, F_REMOTE);
2221 * Ignore the error - there is no result code for failure,
2222 * only for grace period.
2224 result->stat.stat = nlm4_granted;
2227 nlm_release_vfs_state(&vs);
2229 *rpcp = nlm_host_get_rpc(host, TRUE);
2230 nlm_host_release(host);
2235 nlm_do_granted(nlm4_testargs *argp, nlm4_res *result, struct svc_req *rqstp,
2239 struct nlm_host *host;
2240 struct nlm_waiting_lock *nw;
2242 memset(result, 0, sizeof(*result));
2244 host = nlm_find_host_by_addr(svc_getrpccaller(rqstp), rqstp->rq_vers);
2246 result->stat.stat = nlm4_denied_nolocks;
2250 nlm_copy_netobj(&result->cookie, &argp->cookie, M_RPC);
2251 result->stat.stat = nlm4_denied;
2253 mtx_lock(&nlm_global_lock);
2254 TAILQ_FOREACH(nw, &nlm_waiting_locks, nw_link) {
2255 if (!nw->nw_waiting)
2257 if (argp->alock.svid == nw->nw_lock.svid
2258 && argp->alock.l_offset == nw->nw_lock.l_offset
2259 && argp->alock.l_len == nw->nw_lock.l_len
2260 && argp->alock.fh.n_len == nw->nw_lock.fh.n_len
2261 && !memcmp(argp->alock.fh.n_bytes, nw->nw_lock.fh.n_bytes,
2262 nw->nw_lock.fh.n_len)) {
2263 nw->nw_waiting = FALSE;
2265 result->stat.stat = nlm4_granted;
2269 mtx_unlock(&nlm_global_lock);
2271 *rpcp = nlm_host_get_rpc(host, TRUE);
2272 nlm_host_release(host);
2277 nlm_do_free_all(nlm4_notify *argp)
2279 struct nlm_host *host, *thost;
2281 TAILQ_FOREACH_SAFE(host, &nlm_hosts, nh_link, thost) {
2282 if (!strcmp(host->nh_caller_name, argp->name))
2283 nlm_host_notify(host, argp->state);
2288 * Kernel module glue
2291 nfslockd_modevent(module_t mod, int type, void *data)
2296 static moduledata_t nfslockd_mod = {
2301 DECLARE_MODULE(nfslockd, nfslockd_mod, SI_SUB_VFS, SI_ORDER_ANY);
2303 /* So that loader and kldload(2) can find us, wherever we are.. */
2304 MODULE_DEPEND(nfslockd, krpc, 1, 1, 1);
2305 MODULE_DEPEND(nfslockd, nfs, 1, 1, 1);
2306 MODULE_VERSION(nfslockd, 1);