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 * Acquire the next sysid for remote locks not handled by the NLM.
841 nlm_acquire_next_sysid(void)
845 mtx_lock(&nlm_global_lock);
846 next_sysid = nlm_next_sysid++;
847 mtx_unlock(&nlm_global_lock);
852 * Return non-zero if the address parts of the two sockaddrs are the
856 nlm_compare_addr(const struct sockaddr *a, const struct sockaddr *b)
858 const struct sockaddr_in *a4, *b4;
860 const struct sockaddr_in6 *a6, *b6;
863 if (a->sa_family != b->sa_family)
866 switch (a->sa_family) {
868 a4 = (const struct sockaddr_in *) a;
869 b4 = (const struct sockaddr_in *) b;
870 return !memcmp(&a4->sin_addr, &b4->sin_addr,
871 sizeof(a4->sin_addr));
874 a6 = (const struct sockaddr_in6 *) a;
875 b6 = (const struct sockaddr_in6 *) b;
876 return !memcmp(&a6->sin6_addr, &b6->sin6_addr,
877 sizeof(a6->sin6_addr));
885 * Check for idle hosts and stop monitoring them. We could also free
886 * the host structure here, possibly after a larger timeout but that
887 * would require some care to avoid races with
888 * e.g. nlm_host_lock_count_sysctl.
893 struct nlm_host *host;
895 mtx_assert(&nlm_global_lock, MA_OWNED);
897 if (time_uptime <= nlm_next_idle_check)
900 nlm_next_idle_check = time_uptime + NLM_IDLE_PERIOD;
902 TAILQ_FOREACH(host, &nlm_hosts, nh_link) {
903 if (host->nh_monstate == NLM_MONITORED
904 && time_uptime > host->nh_idle_timeout) {
905 mtx_unlock(&nlm_global_lock);
906 if (lf_countlocks(host->nh_sysid) > 0
907 || lf_countlocks(NLM_SYSID_CLIENT
909 host->nh_idle_timeout =
910 time_uptime + NLM_IDLE_TIMEOUT;
911 mtx_lock(&nlm_global_lock);
914 nlm_host_unmonitor(host);
915 mtx_lock(&nlm_global_lock);
921 * Search for an existing NLM host that matches the given name
922 * (typically the caller_name element of an nlm4_lock). If none is
923 * found, create a new host. If 'addr' is non-NULL, record the remote
924 * address of the host so that we can call it back for async
925 * responses. If 'vers' is greater than zero then record the NLM
926 * program version to use to communicate with this client.
929 nlm_find_host_by_name(const char *name, const struct sockaddr *addr,
932 struct nlm_host *host;
934 mtx_lock(&nlm_global_lock);
937 * The remote host is determined by caller_name.
939 TAILQ_FOREACH(host, &nlm_hosts, nh_link) {
940 if (!strcmp(host->nh_caller_name, name))
945 host = nlm_create_host(name);
947 mtx_unlock(&nlm_global_lock);
951 refcount_acquire(&host->nh_refs);
953 host->nh_idle_timeout = time_uptime + NLM_IDLE_TIMEOUT;
956 * If we have an address for the host, record it so that we
957 * can send async replies etc.
961 KASSERT(addr->sa_len < sizeof(struct sockaddr_storage),
962 ("Strange remote transport address length"));
965 * If we have seen an address before and we currently
966 * have an RPC client handle, make sure the address is
967 * the same, otherwise discard the client handle.
969 if (host->nh_addr.ss_len && host->nh_srvrpc.nr_client) {
970 if (!nlm_compare_addr(
971 (struct sockaddr *) &host->nh_addr,
973 || host->nh_vers != vers) {
975 mtx_lock(&host->nh_lock);
976 client = host->nh_srvrpc.nr_client;
977 host->nh_srvrpc.nr_client = NULL;
978 mtx_unlock(&host->nh_lock);
980 CLNT_RELEASE(client);
984 memcpy(&host->nh_addr, addr, addr->sa_len);
985 host->nh_vers = vers;
990 mtx_unlock(&nlm_global_lock);
996 * Search for an existing NLM host that matches the given remote
997 * address. If none is found, create a new host with the requested
998 * address and remember 'vers' as the NLM protocol version to use for
1002 nlm_find_host_by_addr(const struct sockaddr *addr, int vers)
1005 * Fake up a name using inet_ntop. This buffer is
1006 * large enough for an IPv6 address.
1008 char tmp[sizeof "ffff:ffff:ffff:ffff:ffff:ffff:255.255.255.255"];
1009 struct nlm_host *host;
1011 switch (addr->sa_family) {
1013 __rpc_inet_ntop(AF_INET,
1014 &((const struct sockaddr_in *) addr)->sin_addr,
1019 __rpc_inet_ntop(AF_INET6,
1020 &((const struct sockaddr_in6 *) addr)->sin6_addr,
1025 strcmp(tmp, "<unknown>");
1029 mtx_lock(&nlm_global_lock);
1032 * The remote host is determined by caller_name.
1034 TAILQ_FOREACH(host, &nlm_hosts, nh_link) {
1035 if (nlm_compare_addr(addr,
1036 (const struct sockaddr *) &host->nh_addr))
1041 host = nlm_create_host(tmp);
1043 mtx_unlock(&nlm_global_lock);
1046 memcpy(&host->nh_addr, addr, addr->sa_len);
1047 host->nh_vers = vers;
1049 refcount_acquire(&host->nh_refs);
1051 host->nh_idle_timeout = time_uptime + NLM_IDLE_TIMEOUT;
1055 mtx_unlock(&nlm_global_lock);
1061 * Find the NLM host that matches the value of 'sysid'. If none
1062 * exists, return NULL.
1064 static struct nlm_host *
1065 nlm_find_host_by_sysid(int sysid)
1067 struct nlm_host *host;
1069 TAILQ_FOREACH(host, &nlm_hosts, nh_link) {
1070 if (host->nh_sysid == sysid) {
1071 refcount_acquire(&host->nh_refs);
1079 void nlm_host_release(struct nlm_host *host)
1081 if (refcount_release(&host->nh_refs)) {
1085 nlm_host_destroy(host);
1090 * Unregister this NLM host with the local NSM due to idleness.
1093 nlm_host_unmonitor(struct nlm_host *host)
1097 struct timeval timo;
1098 enum clnt_stat stat;
1100 NLM_DEBUG(1, "NLM: unmonitoring %s (sysid %d)\n",
1101 host->nh_caller_name, host->nh_sysid);
1104 * We put our assigned system ID value in the priv field to
1105 * make it simpler to find the host if we are notified of a
1108 smmonid.mon_name = host->nh_caller_name;
1109 smmonid.my_id.my_name = "localhost";
1110 smmonid.my_id.my_prog = NLM_PROG;
1111 smmonid.my_id.my_vers = NLM_SM;
1112 smmonid.my_id.my_proc = NLM_SM_NOTIFY;
1116 stat = CLNT_CALL(nlm_nsm, SM_UNMON,
1117 (xdrproc_t) xdr_mon, &smmonid,
1118 (xdrproc_t) xdr_sm_stat, &smstat, timo);
1120 if (stat != RPC_SUCCESS) {
1121 NLM_ERR("Failed to contact local NSM - rpc error %d\n", stat);
1124 if (smstat.res_stat == stat_fail) {
1125 NLM_ERR("Local NSM refuses to unmonitor %s\n",
1126 host->nh_caller_name);
1130 host->nh_monstate = NLM_UNMONITORED;
1134 * Register this NLM host with the local NSM so that we can be
1135 * notified if it reboots.
1138 nlm_host_monitor(struct nlm_host *host, int state)
1142 struct timeval timo;
1143 enum clnt_stat stat;
1145 if (state && !host->nh_state) {
1147 * This is the first time we have seen an NSM state
1148 * value for this host. We record it here to help
1149 * detect host reboots.
1151 host->nh_state = state;
1152 NLM_DEBUG(1, "NLM: host %s (sysid %d) has NSM state %d\n",
1153 host->nh_caller_name, host->nh_sysid, state);
1156 mtx_lock(&host->nh_lock);
1157 if (host->nh_monstate != NLM_UNMONITORED) {
1158 mtx_unlock(&host->nh_lock);
1161 host->nh_monstate = NLM_MONITORED;
1162 mtx_unlock(&host->nh_lock);
1164 NLM_DEBUG(1, "NLM: monitoring %s (sysid %d)\n",
1165 host->nh_caller_name, host->nh_sysid);
1168 * We put our assigned system ID value in the priv field to
1169 * make it simpler to find the host if we are notified of a
1172 smmon.mon_id.mon_name = host->nh_caller_name;
1173 smmon.mon_id.my_id.my_name = "localhost";
1174 smmon.mon_id.my_id.my_prog = NLM_PROG;
1175 smmon.mon_id.my_id.my_vers = NLM_SM;
1176 smmon.mon_id.my_id.my_proc = NLM_SM_NOTIFY;
1177 memcpy(smmon.priv, &host->nh_sysid, sizeof(host->nh_sysid));
1181 stat = CLNT_CALL(nlm_nsm, SM_MON,
1182 (xdrproc_t) xdr_mon, &smmon,
1183 (xdrproc_t) xdr_sm_stat, &smstat, timo);
1185 if (stat != RPC_SUCCESS) {
1186 NLM_ERR("Failed to contact local NSM - rpc error %d\n", stat);
1189 if (smstat.res_stat == stat_fail) {
1190 NLM_ERR("Local NSM refuses to monitor %s\n",
1191 host->nh_caller_name);
1192 mtx_lock(&host->nh_lock);
1193 host->nh_monstate = NLM_MONITOR_FAILED;
1194 mtx_unlock(&host->nh_lock);
1198 host->nh_monstate = NLM_MONITORED;
1202 * Return an RPC client handle that can be used to talk to the NLM
1203 * running on the given host.
1206 nlm_host_get_rpc(struct nlm_host *host, bool_t isserver)
1208 struct nlm_rpc *rpc;
1211 mtx_lock(&host->nh_lock);
1214 rpc = &host->nh_srvrpc;
1216 rpc = &host->nh_clntrpc;
1219 * We can't hold onto RPC handles for too long - the async
1220 * call/reply protocol used by some NLM clients makes it hard
1221 * to tell when they change port numbers (e.g. after a
1222 * reboot). Note that if a client reboots while it isn't
1223 * holding any locks, it won't bother to notify us. We
1224 * expire the RPC handles after two minutes.
1226 if (rpc->nr_client && time_uptime > rpc->nr_create_time + 2*60) {
1227 client = rpc->nr_client;
1228 rpc->nr_client = NULL;
1229 mtx_unlock(&host->nh_lock);
1230 CLNT_RELEASE(client);
1231 mtx_lock(&host->nh_lock);
1234 if (!rpc->nr_client) {
1235 mtx_unlock(&host->nh_lock);
1236 client = nlm_get_rpc((struct sockaddr *)&host->nh_addr,
1237 NLM_PROG, host->nh_vers);
1238 mtx_lock(&host->nh_lock);
1241 if (rpc->nr_client) {
1242 mtx_unlock(&host->nh_lock);
1243 CLNT_DESTROY(client);
1244 mtx_lock(&host->nh_lock);
1246 rpc->nr_client = client;
1247 rpc->nr_create_time = time_uptime;
1252 client = rpc->nr_client;
1254 CLNT_ACQUIRE(client);
1255 mtx_unlock(&host->nh_lock);
1261 int nlm_host_get_sysid(struct nlm_host *host)
1264 return (host->nh_sysid);
1268 nlm_host_get_state(struct nlm_host *host)
1271 return (host->nh_state);
1275 nlm_register_wait_lock(struct nlm4_lock *lock, struct vnode *vp)
1277 struct nlm_waiting_lock *nw;
1279 nw = malloc(sizeof(struct nlm_waiting_lock), M_NLM, M_WAITOK);
1280 nw->nw_lock = *lock;
1281 memcpy(&nw->nw_fh.fh_bytes, nw->nw_lock.fh.n_bytes,
1282 nw->nw_lock.fh.n_len);
1283 nw->nw_lock.fh.n_bytes = nw->nw_fh.fh_bytes;
1284 nw->nw_waiting = TRUE;
1286 mtx_lock(&nlm_global_lock);
1287 TAILQ_INSERT_TAIL(&nlm_waiting_locks, nw, nw_link);
1288 mtx_unlock(&nlm_global_lock);
1294 nlm_deregister_wait_lock(void *handle)
1296 struct nlm_waiting_lock *nw = handle;
1298 mtx_lock(&nlm_global_lock);
1299 TAILQ_REMOVE(&nlm_waiting_locks, nw, nw_link);
1300 mtx_unlock(&nlm_global_lock);
1306 nlm_wait_lock(void *handle, int timo)
1308 struct nlm_waiting_lock *nw = handle;
1312 * If the granted message arrived before we got here,
1313 * nw->nw_waiting will be FALSE - in that case, don't sleep.
1315 mtx_lock(&nlm_global_lock);
1318 error = msleep(nw, &nlm_global_lock, PCATCH, "nlmlock", timo);
1319 TAILQ_REMOVE(&nlm_waiting_locks, nw, nw_link);
1322 * The granted message may arrive after the
1323 * interrupt/timeout but before we manage to lock the
1324 * mutex. Detect this by examining nw_lock.
1326 if (!nw->nw_waiting)
1330 * If nlm_cancel_wait is called, then error will be
1331 * zero but nw_waiting will still be TRUE. We
1332 * translate this into EINTR.
1337 mtx_unlock(&nlm_global_lock);
1345 nlm_cancel_wait(struct vnode *vp)
1347 struct nlm_waiting_lock *nw;
1349 mtx_lock(&nlm_global_lock);
1350 TAILQ_FOREACH(nw, &nlm_waiting_locks, nw_link) {
1351 if (nw->nw_vp == vp) {
1355 mtx_unlock(&nlm_global_lock);
1359 /**********************************************************************/
1362 * Syscall interface with userland.
1365 extern void nlm_prog_0(struct svc_req *rqstp, SVCXPRT *transp);
1366 extern void nlm_prog_1(struct svc_req *rqstp, SVCXPRT *transp);
1367 extern void nlm_prog_3(struct svc_req *rqstp, SVCXPRT *transp);
1368 extern void nlm_prog_4(struct svc_req *rqstp, SVCXPRT *transp);
1371 nlm_register_services(SVCPOOL *pool, int addr_count, char **addrs)
1373 static rpcvers_t versions[] = {
1374 NLM_SM, NLM_VERS, NLM_VERSX, NLM_VERS4
1376 static void (*dispatchers[])(struct svc_req *, SVCXPRT *) = {
1377 nlm_prog_0, nlm_prog_1, nlm_prog_3, nlm_prog_4
1379 static const int version_count = sizeof(versions) / sizeof(versions[0]);
1384 struct netconfig *nconf;
1388 NLM_ERR("NLM: no service addresses given - can't start server");
1392 xprts = malloc(addr_count * sizeof(SVCXPRT *), M_NLM, M_WAITOK|M_ZERO);
1393 for (i = 0; i < version_count; i++) {
1394 for (j = 0; j < addr_count; j++) {
1396 * Create transports for the first version and
1397 * then just register everything else to the
1403 error = copyin(&addrs[2*j], &up,
1407 error = copyinstr(up, netid, sizeof(netid),
1411 error = copyin(&addrs[2*j+1], &up,
1415 error = copyinstr(up, uaddr, sizeof(uaddr),
1419 nconf = getnetconfigent(netid);
1421 NLM_ERR("Can't lookup netid %s\n",
1426 xprts[j] = svc_tp_create(pool, dispatchers[i],
1427 NLM_PROG, versions[i], uaddr, nconf);
1429 NLM_ERR("NLM: unable to create "
1430 "(NLM_PROG, %d).\n", versions[i]);
1434 freenetconfigent(nconf);
1436 nconf = getnetconfigent(xprts[j]->xp_netid);
1437 rpcb_unset(NLM_PROG, versions[i], nconf);
1438 if (!svc_reg(xprts[j], NLM_PROG, versions[i],
1439 dispatchers[i], nconf)) {
1440 NLM_ERR("NLM: can't register "
1441 "(NLM_PROG, %d)\n", versions[i]);
1450 for (j = 0; j < addr_count; j++) {
1452 SVC_RELEASE(xprts[j]);
1459 * Main server entry point. Contacts the local NSM to get its current
1460 * state and send SM_UNMON_ALL. Registers the NLM services and then
1461 * services requests. Does not return until the server is interrupted
1465 nlm_server_main(int addr_count, char **addrs)
1467 struct thread *td = curthread;
1469 SVCPOOL *pool = NULL;
1473 struct sockaddr_in6 sin6;
1475 struct sockaddr_in sin;
1478 struct timeval timo;
1479 enum clnt_stat stat;
1480 struct nlm_host *host, *nhost;
1481 struct nlm_waiting_lock *nw;
1483 vop_advlock_t *old_nfs_advlock;
1484 vop_reclaim_t *old_nfs_reclaim;
1492 NLM_ERR("NLM: can't start server - "
1493 "it appears to be running already\n");
1497 memset(&opt, 0, sizeof(opt));
1500 error = socreate(AF_INET, &nlm_socket, SOCK_DGRAM, 0,
1503 NLM_ERR("NLM: can't create IPv4 socket - error %d\n", error);
1506 opt.sopt_dir = SOPT_SET;
1507 opt.sopt_level = IPPROTO_IP;
1508 opt.sopt_name = IP_PORTRANGE;
1509 portlow = IP_PORTRANGE_LOW;
1510 opt.sopt_val = &portlow;
1511 opt.sopt_valsize = sizeof(portlow);
1512 sosetopt(nlm_socket, &opt);
1516 error = socreate(AF_INET6, &nlm_socket6, SOCK_DGRAM, 0,
1519 NLM_ERR("NLM: can't create IPv6 socket - error %d\n", error);
1523 opt.sopt_dir = SOPT_SET;
1524 opt.sopt_level = IPPROTO_IPV6;
1525 opt.sopt_name = IPV6_PORTRANGE;
1526 portlow = IPV6_PORTRANGE_LOW;
1527 opt.sopt_val = &portlow;
1528 opt.sopt_valsize = sizeof(portlow);
1529 sosetopt(nlm_socket6, &opt);
1532 nlm_auth = authunix_create(curthread->td_ucred);
1535 memset(&sin6, 0, sizeof(sin6));
1536 sin6.sin6_len = sizeof(sin6);
1537 sin6.sin6_family = AF_INET6;
1538 sin6.sin6_addr = in6addr_loopback;
1539 nlm_nsm = nlm_get_rpc((struct sockaddr *) &sin6, SM_PROG, SM_VERS);
1542 memset(&sin, 0, sizeof(sin));
1543 sin.sin_len = sizeof(sin);
1544 sin.sin_family = AF_INET;
1545 sin.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
1546 nlm_nsm = nlm_get_rpc((struct sockaddr *) &sin, SM_PROG,
1553 NLM_ERR("Can't start NLM - unable to contact NSM\n");
1558 pool = svcpool_create("NLM", NULL);
1560 error = nlm_register_services(pool, addr_count, addrs);
1564 memset(&id, 0, sizeof(id));
1565 id.my_name = "NFS NLM";
1569 stat = CLNT_CALL(nlm_nsm, SM_UNMON_ALL,
1570 (xdrproc_t) xdr_my_id, &id,
1571 (xdrproc_t) xdr_sm_stat, &smstat, timo);
1573 if (stat != RPC_SUCCESS) {
1576 CLNT_GETERR(nlm_nsm, &err);
1577 NLM_ERR("NLM: unexpected error contacting NSM, "
1578 "stat=%d, errno=%d\n", stat, err.re_errno);
1583 NLM_DEBUG(1, "NLM: local NSM state is %d\n", smstat.state);
1584 nlm_nsm_state = smstat.state;
1587 old_nfs_advlock = nfs_advlock_p;
1588 nfs_advlock_p = nlm_advlock;
1589 old_nfs_reclaim = nfs_reclaim_p;
1590 nfs_reclaim_p = nlm_reclaim;
1597 nfs_advlock_p = old_nfs_advlock;
1598 nfs_reclaim_p = old_nfs_reclaim;
1603 svcpool_destroy(pool);
1606 * We are finished communicating with the NSM.
1609 CLNT_RELEASE(nlm_nsm);
1614 * Trash all the existing state so that if the server
1615 * restarts, it gets a clean slate. This is complicated by the
1616 * possibility that there may be other threads trying to make
1617 * client locking requests.
1619 * First we fake a client reboot notification which will
1620 * cancel any pending async locks and purge remote lock state
1621 * from the local lock manager. We release the reference from
1622 * nlm_hosts to the host (which may remove it from the list
1623 * and free it). After this phase, the only entries in the
1624 * nlm_host list should be from other threads performing
1625 * client lock requests. We arrange to defer closing the
1626 * sockets until the last RPC client handle is released.
1632 mtx_lock(&nlm_global_lock);
1633 TAILQ_FOREACH(nw, &nlm_waiting_locks, nw_link) {
1636 TAILQ_FOREACH_SAFE(host, &nlm_hosts, nh_link, nhost) {
1637 mtx_unlock(&nlm_global_lock);
1638 nlm_host_notify(host, 0);
1639 nlm_host_release(host);
1640 mtx_lock(&nlm_global_lock);
1642 TAILQ_FOREACH_SAFE(host, &nlm_hosts, nh_link, nhost) {
1643 mtx_lock(&host->nh_lock);
1644 if (host->nh_srvrpc.nr_client
1645 || host->nh_clntrpc.nr_client) {
1646 if (host->nh_addr.ss_family == AF_INET)
1649 if (host->nh_addr.ss_family == AF_INET6)
1653 * Note that the rpc over udp code copes
1654 * correctly with the fact that a socket may
1655 * be used by many rpc handles.
1657 if (host->nh_srvrpc.nr_client)
1658 CLNT_CONTROL(host->nh_srvrpc.nr_client,
1660 if (host->nh_clntrpc.nr_client)
1661 CLNT_CONTROL(host->nh_clntrpc.nr_client,
1664 mtx_unlock(&host->nh_lock);
1666 mtx_unlock(&nlm_global_lock);
1668 AUTH_DESTROY(nlm_auth);
1671 soclose(nlm_socket);
1675 soclose(nlm_socket6);
1683 nlm_syscall(struct thread *td, struct nlm_syscall_args *uap)
1687 #if __FreeBSD_version >= 700000
1688 error = priv_check(td, PRIV_NFS_LOCKD);
1695 nlm_debug_level = uap->debug_level;
1696 nlm_grace_threshold = time_uptime + uap->grace_period;
1697 nlm_next_idle_check = time_uptime + NLM_IDLE_PERIOD;
1699 return nlm_server_main(uap->addr_count, uap->addrs);
1702 /**********************************************************************/
1705 * NLM implementation details, called from the RPC stubs.
1710 nlm_sm_notify(struct nlm_sm_status *argp)
1713 struct nlm_host *host;
1715 NLM_DEBUG(3, "nlm_sm_notify(): mon_name = %s\n", argp->mon_name);
1716 memcpy(&sysid, &argp->priv, sizeof(sysid));
1717 host = nlm_find_host_by_sysid(sysid);
1719 nlm_host_notify(host, argp->state);
1720 nlm_host_release(host);
1725 nlm_convert_to_fhandle_t(fhandle_t *fhp, struct netobj *p)
1727 memcpy(fhp, p->n_bytes, sizeof(fhandle_t));
1731 struct mount *vs_mp;
1732 struct vnode *vs_vp;
1738 nlm_get_vfs_state(struct nlm_host *host, struct svc_req *rqstp,
1739 fhandle_t *fhp, struct vfs_state *vs)
1742 struct ucred *cred = NULL, *credanon;
1744 memset(vs, 0, sizeof(*vs));
1746 vs->vs_mp = vfs_getvfs(&fhp->fh_fsid);
1750 vs->vs_vfslocked = VFS_LOCK_GIANT(vs->vs_mp);
1752 error = VFS_CHECKEXP(vs->vs_mp, (struct sockaddr *)&host->nh_addr,
1753 &exflags, &credanon, NULL, NULL);
1757 if (exflags & MNT_EXRDONLY || (vs->vs_mp->mnt_flag & MNT_RDONLY)) {
1762 error = VFS_FHTOVP(vs->vs_mp, &fhp->fh_fid, &vs->vs_vp);
1765 vs->vs_vnlocked = TRUE;
1767 if (!svc_getcred(rqstp, &cred, NULL)) {
1771 if (cred->cr_uid == 0 || (exflags & MNT_EXPORTANON)) {
1780 error = VOP_ACCESS(vs->vs_vp, VWRITE, cred, curthread);
1784 #if __FreeBSD_version < 800011
1785 VOP_UNLOCK(vs->vs_vp, 0, curthread);
1787 VOP_UNLOCK(vs->vs_vp, 0);
1789 vs->vs_vnlocked = FALSE;
1801 nlm_release_vfs_state(struct vfs_state *vs)
1805 if (vs->vs_vnlocked)
1812 VFS_UNLOCK_GIANT(vs->vs_vfslocked);
1816 nlm_convert_error(int error)
1819 if (error == ESTALE)
1820 return nlm4_stale_fh;
1821 else if (error == EROFS)
1828 nlm_do_test(nlm4_testargs *argp, nlm4_testres *result, struct svc_req *rqstp,
1832 struct vfs_state vs;
1833 struct nlm_host *host, *bhost;
1837 memset(result, 0, sizeof(*result));
1838 memset(&vs, 0, sizeof(vs));
1840 host = nlm_find_host_by_name(argp->alock.caller_name,
1841 svc_getrpccaller(rqstp), rqstp->rq_vers);
1843 result->stat.stat = nlm4_denied_nolocks;
1847 NLM_DEBUG(3, "nlm_do_test(): caller_name = %s (sysid = %d)\n",
1848 host->nh_caller_name, host->nh_sysid);
1850 nlm_free_finished_locks(host);
1851 sysid = host->nh_sysid;
1853 nlm_convert_to_fhandle_t(&fh, &argp->alock.fh);
1854 nlm_copy_netobj(&result->cookie, &argp->cookie, M_RPC);
1856 if (time_uptime < nlm_grace_threshold) {
1857 result->stat.stat = nlm4_denied_grace_period;
1861 error = nlm_get_vfs_state(host, rqstp, &fh, &vs);
1863 result->stat.stat = nlm_convert_error(error);
1867 fl.l_start = argp->alock.l_offset;
1868 fl.l_len = argp->alock.l_len;
1869 fl.l_pid = argp->alock.svid;
1871 fl.l_whence = SEEK_SET;
1872 if (argp->exclusive)
1873 fl.l_type = F_WRLCK;
1875 fl.l_type = F_RDLCK;
1876 error = VOP_ADVLOCK(vs.vs_vp, NULL, F_GETLK, &fl, F_REMOTE);
1878 result->stat.stat = nlm4_failed;
1882 if (fl.l_type == F_UNLCK) {
1883 result->stat.stat = nlm4_granted;
1885 result->stat.stat = nlm4_denied;
1886 result->stat.nlm4_testrply_u.holder.exclusive =
1887 (fl.l_type == F_WRLCK);
1888 result->stat.nlm4_testrply_u.holder.svid = fl.l_pid;
1889 bhost = nlm_find_host_by_sysid(fl.l_sysid);
1892 * We don't have any useful way of recording
1893 * the value of oh used in the original lock
1894 * request. Ideally, the test reply would have
1895 * a space for the owning host's name allowing
1896 * our caller's NLM to keep track.
1898 * As far as I can see, Solaris uses an eight
1899 * byte structure for oh which contains a four
1900 * byte pid encoded in local byte order and
1901 * the first four bytes of the host
1902 * name. Linux uses a variable length string
1903 * 'pid@hostname' in ascii but doesn't even
1904 * return that in test replies.
1906 * For the moment, return nothing in oh
1907 * (already zero'ed above).
1909 nlm_host_release(bhost);
1911 result->stat.nlm4_testrply_u.holder.l_offset = fl.l_start;
1912 result->stat.nlm4_testrply_u.holder.l_len = fl.l_len;
1916 nlm_release_vfs_state(&vs);
1918 *rpcp = nlm_host_get_rpc(host, TRUE);
1919 nlm_host_release(host);
1924 nlm_do_lock(nlm4_lockargs *argp, nlm4_res *result, struct svc_req *rqstp,
1925 bool_t monitor, CLIENT **rpcp)
1928 struct vfs_state vs;
1929 struct nlm_host *host;
1933 memset(result, 0, sizeof(*result));
1934 memset(&vs, 0, sizeof(vs));
1936 host = nlm_find_host_by_name(argp->alock.caller_name,
1937 svc_getrpccaller(rqstp), rqstp->rq_vers);
1939 result->stat.stat = nlm4_denied_nolocks;
1943 NLM_DEBUG(3, "nlm_do_lock(): caller_name = %s (sysid = %d)\n",
1944 host->nh_caller_name, host->nh_sysid);
1946 if (monitor && host->nh_state && argp->state
1947 && host->nh_state != argp->state) {
1949 * The host rebooted without telling us. Trash its
1952 nlm_host_notify(host, argp->state);
1955 nlm_free_finished_locks(host);
1956 sysid = host->nh_sysid;
1958 nlm_convert_to_fhandle_t(&fh, &argp->alock.fh);
1959 nlm_copy_netobj(&result->cookie, &argp->cookie, M_RPC);
1961 if (time_uptime < nlm_grace_threshold && !argp->reclaim) {
1962 result->stat.stat = nlm4_denied_grace_period;
1966 error = nlm_get_vfs_state(host, rqstp, &fh, &vs);
1968 result->stat.stat = nlm_convert_error(error);
1972 fl.l_start = argp->alock.l_offset;
1973 fl.l_len = argp->alock.l_len;
1974 fl.l_pid = argp->alock.svid;
1976 fl.l_whence = SEEK_SET;
1977 if (argp->exclusive)
1978 fl.l_type = F_WRLCK;
1980 fl.l_type = F_RDLCK;
1982 struct nlm_async_lock *af;
1986 * First, make sure we can contact the host's NLM.
1988 client = nlm_host_get_rpc(host, TRUE);
1990 result->stat.stat = nlm4_failed;
1995 * First we need to check and see if there is an
1996 * existing blocked lock that matches. This could be a
1997 * badly behaved client or an RPC re-send. If we find
1998 * one, just return nlm4_blocked.
2000 mtx_lock(&host->nh_lock);
2001 TAILQ_FOREACH(af, &host->nh_pending, af_link) {
2002 if (af->af_fl.l_start == fl.l_start
2003 && af->af_fl.l_len == fl.l_len
2004 && af->af_fl.l_pid == fl.l_pid
2005 && af->af_fl.l_type == fl.l_type) {
2009 mtx_unlock(&host->nh_lock);
2011 CLNT_RELEASE(client);
2012 result->stat.stat = nlm4_blocked;
2016 af = malloc(sizeof(struct nlm_async_lock), M_NLM,
2018 TASK_INIT(&af->af_task, 0, nlm_lock_callback, af);
2019 af->af_vp = vs.vs_vp;
2022 af->af_rpc = client;
2024 * We use M_RPC here so that we can xdr_free the thing
2027 af->af_granted.exclusive = argp->exclusive;
2028 af->af_granted.alock.caller_name =
2029 strdup(argp->alock.caller_name, M_RPC);
2030 nlm_copy_netobj(&af->af_granted.alock.fh,
2031 &argp->alock.fh, M_RPC);
2032 nlm_copy_netobj(&af->af_granted.alock.oh,
2033 &argp->alock.oh, M_RPC);
2034 af->af_granted.alock.svid = argp->alock.svid;
2035 af->af_granted.alock.l_offset = argp->alock.l_offset;
2036 af->af_granted.alock.l_len = argp->alock.l_len;
2039 * Put the entry on the pending list before calling
2040 * VOP_ADVLOCKASYNC. We do this in case the lock
2041 * request was blocked (returning EINPROGRESS) but
2042 * then granted before we manage to run again. The
2043 * client may receive the granted message before we
2044 * send our blocked reply but thats their problem.
2046 mtx_lock(&host->nh_lock);
2047 TAILQ_INSERT_TAIL(&host->nh_pending, af, af_link);
2048 mtx_unlock(&host->nh_lock);
2050 error = VOP_ADVLOCKASYNC(vs.vs_vp, NULL, F_SETLK, &fl, F_REMOTE,
2051 &af->af_task, &af->af_cookie);
2054 * If the lock completed synchronously, just free the
2055 * tracking structure now.
2057 if (error != EINPROGRESS) {
2058 CLNT_RELEASE(af->af_rpc);
2059 mtx_lock(&host->nh_lock);
2060 TAILQ_REMOVE(&host->nh_pending, af, af_link);
2061 mtx_unlock(&host->nh_lock);
2062 xdr_free((xdrproc_t) xdr_nlm4_testargs,
2066 NLM_DEBUG(2, "NLM: pending async lock %p for %s "
2067 "(sysid %d)\n", af, host->nh_caller_name, sysid);
2069 * Don't vrele the vnode just yet - this must
2070 * wait until either the async callback
2071 * happens or the lock is cancelled.
2076 error = VOP_ADVLOCK(vs.vs_vp, NULL, F_SETLK, &fl, F_REMOTE);
2080 if (error == EINPROGRESS) {
2081 result->stat.stat = nlm4_blocked;
2082 } else if (error == EDEADLK) {
2083 result->stat.stat = nlm4_deadlck;
2084 } else if (error == EAGAIN) {
2085 result->stat.stat = nlm4_denied;
2087 result->stat.stat = nlm4_failed;
2091 nlm_host_monitor(host, argp->state);
2092 result->stat.stat = nlm4_granted;
2096 nlm_release_vfs_state(&vs);
2098 *rpcp = nlm_host_get_rpc(host, TRUE);
2099 nlm_host_release(host);
2104 nlm_do_cancel(nlm4_cancargs *argp, nlm4_res *result, struct svc_req *rqstp,
2108 struct vfs_state vs;
2109 struct nlm_host *host;
2112 struct nlm_async_lock *af;
2114 memset(result, 0, sizeof(*result));
2115 memset(&vs, 0, sizeof(vs));
2117 host = nlm_find_host_by_name(argp->alock.caller_name,
2118 svc_getrpccaller(rqstp), rqstp->rq_vers);
2120 result->stat.stat = nlm4_denied_nolocks;
2124 NLM_DEBUG(3, "nlm_do_cancel(): caller_name = %s (sysid = %d)\n",
2125 host->nh_caller_name, host->nh_sysid);
2127 nlm_free_finished_locks(host);
2128 sysid = host->nh_sysid;
2130 nlm_convert_to_fhandle_t(&fh, &argp->alock.fh);
2131 nlm_copy_netobj(&result->cookie, &argp->cookie, M_RPC);
2133 if (time_uptime < nlm_grace_threshold) {
2134 result->stat.stat = nlm4_denied_grace_period;
2138 error = nlm_get_vfs_state(host, rqstp, &fh, &vs);
2140 result->stat.stat = nlm_convert_error(error);
2144 fl.l_start = argp->alock.l_offset;
2145 fl.l_len = argp->alock.l_len;
2146 fl.l_pid = argp->alock.svid;
2148 fl.l_whence = SEEK_SET;
2149 if (argp->exclusive)
2150 fl.l_type = F_WRLCK;
2152 fl.l_type = F_RDLCK;
2155 * First we need to try and find the async lock request - if
2156 * there isn't one, we give up and return nlm4_denied.
2158 mtx_lock(&host->nh_lock);
2160 TAILQ_FOREACH(af, &host->nh_pending, af_link) {
2161 if (af->af_fl.l_start == fl.l_start
2162 && af->af_fl.l_len == fl.l_len
2163 && af->af_fl.l_pid == fl.l_pid
2164 && af->af_fl.l_type == fl.l_type) {
2170 mtx_unlock(&host->nh_lock);
2171 result->stat.stat = nlm4_denied;
2175 error = nlm_cancel_async_lock(af);
2178 result->stat.stat = nlm4_denied;
2180 result->stat.stat = nlm4_granted;
2183 mtx_unlock(&host->nh_lock);
2186 nlm_release_vfs_state(&vs);
2188 *rpcp = nlm_host_get_rpc(host, TRUE);
2189 nlm_host_release(host);
2194 nlm_do_unlock(nlm4_unlockargs *argp, nlm4_res *result, struct svc_req *rqstp,
2198 struct vfs_state vs;
2199 struct nlm_host *host;
2203 memset(result, 0, sizeof(*result));
2204 memset(&vs, 0, sizeof(vs));
2206 host = nlm_find_host_by_name(argp->alock.caller_name,
2207 svc_getrpccaller(rqstp), rqstp->rq_vers);
2209 result->stat.stat = nlm4_denied_nolocks;
2213 NLM_DEBUG(3, "nlm_do_unlock(): caller_name = %s (sysid = %d)\n",
2214 host->nh_caller_name, host->nh_sysid);
2216 nlm_free_finished_locks(host);
2217 sysid = host->nh_sysid;
2219 nlm_convert_to_fhandle_t(&fh, &argp->alock.fh);
2220 nlm_copy_netobj(&result->cookie, &argp->cookie, M_RPC);
2222 if (time_uptime < nlm_grace_threshold) {
2223 result->stat.stat = nlm4_denied_grace_period;
2227 error = nlm_get_vfs_state(host, rqstp, &fh, &vs);
2229 result->stat.stat = nlm_convert_error(error);
2233 fl.l_start = argp->alock.l_offset;
2234 fl.l_len = argp->alock.l_len;
2235 fl.l_pid = argp->alock.svid;
2237 fl.l_whence = SEEK_SET;
2238 fl.l_type = F_UNLCK;
2239 error = VOP_ADVLOCK(vs.vs_vp, NULL, F_UNLCK, &fl, F_REMOTE);
2242 * Ignore the error - there is no result code for failure,
2243 * only for grace period.
2245 result->stat.stat = nlm4_granted;
2248 nlm_release_vfs_state(&vs);
2250 *rpcp = nlm_host_get_rpc(host, TRUE);
2251 nlm_host_release(host);
2256 nlm_do_granted(nlm4_testargs *argp, nlm4_res *result, struct svc_req *rqstp,
2260 struct nlm_host *host;
2261 struct nlm_waiting_lock *nw;
2263 memset(result, 0, sizeof(*result));
2265 host = nlm_find_host_by_addr(svc_getrpccaller(rqstp), rqstp->rq_vers);
2267 result->stat.stat = nlm4_denied_nolocks;
2271 nlm_copy_netobj(&result->cookie, &argp->cookie, M_RPC);
2272 result->stat.stat = nlm4_denied;
2274 mtx_lock(&nlm_global_lock);
2275 TAILQ_FOREACH(nw, &nlm_waiting_locks, nw_link) {
2276 if (!nw->nw_waiting)
2278 if (argp->alock.svid == nw->nw_lock.svid
2279 && argp->alock.l_offset == nw->nw_lock.l_offset
2280 && argp->alock.l_len == nw->nw_lock.l_len
2281 && argp->alock.fh.n_len == nw->nw_lock.fh.n_len
2282 && !memcmp(argp->alock.fh.n_bytes, nw->nw_lock.fh.n_bytes,
2283 nw->nw_lock.fh.n_len)) {
2284 nw->nw_waiting = FALSE;
2286 result->stat.stat = nlm4_granted;
2290 mtx_unlock(&nlm_global_lock);
2292 *rpcp = nlm_host_get_rpc(host, TRUE);
2293 nlm_host_release(host);
2298 nlm_do_free_all(nlm4_notify *argp)
2300 struct nlm_host *host, *thost;
2302 TAILQ_FOREACH_SAFE(host, &nlm_hosts, nh_link, thost) {
2303 if (!strcmp(host->nh_caller_name, argp->name))
2304 nlm_host_notify(host, argp->state);
2309 * Kernel module glue
2312 nfslockd_modevent(module_t mod, int type, void *data)
2317 static moduledata_t nfslockd_mod = {
2322 DECLARE_MODULE(nfslockd, nfslockd_mod, SI_SUB_VFS, SI_ORDER_ANY);
2324 /* So that loader and kldload(2) can find us, wherever we are.. */
2325 MODULE_DEPEND(nfslockd, krpc, 1, 1, 1);
2326 MODULE_DEPEND(nfslockd, nfs, 1, 1, 1);
2327 MODULE_VERSION(nfslockd, 1);