1 /*-
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>
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
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.
14 *
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
25 * SUCH DAMAGE.
26 */
27
28 #include "opt_inet6.h"
29
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
32
33 #include <sys/param.h>
34 #include <sys/fail.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
42 #include <sys/priv.h>
43 #endif
44 #include <sys/proc.h>
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>
56
57 #include <nfs/nfsproto.h>
58 #include <nfs/nfs_lock.h>
59
60 #include <nlm/nlm_prot.h>
61 #include <nlm/sm_inter.h>
62 #include <nlm/nlm.h>
63 #include <rpc/rpc_com.h>
64 #include <rpc/rpcb_prot.h>
65
66 MALLOC_DEFINE(M_NLM, "NLM", "Network Lock Manager");
67
68 /*
69 * If a host is inactive (and holds no locks) for this amount of
70 * seconds, we consider it idle and stop tracking it.
71 */
72 #define NLM_IDLE_TIMEOUT 30
73
74 /*
75 * We check the host list for idle every few seconds.
76 */
77 #define NLM_IDLE_PERIOD 5
78
79 /*
80 * We only look for GRANTED_RES messages for a little while.
81 */
82 #define NLM_EXPIRE_TIMEOUT 10
83
84 /*
85 * Support for sysctl vfs.nlm.sysid
86 */
87 static SYSCTL_NODE(_vfs, OID_AUTO, nlm, CTLFLAG_RW, NULL,
88 "Network Lock Manager");
89 static SYSCTL_NODE(_vfs_nlm, OID_AUTO, sysid, CTLFLAG_RW, NULL, "");
90
91 /*
92 * Syscall hooks
93 */
94 static int nlm_syscall_offset = SYS_nlm_syscall;
95 static struct sysent nlm_syscall_prev_sysent;
96 #if __FreeBSD_version < 700000
97 static struct sysent nlm_syscall_sysent = {
98 (sizeof(struct nlm_syscall_args) / sizeof(register_t)) | SYF_MPSAFE,
99 (sy_call_t *) nlm_syscall
100 };
101 #else
102 MAKE_SYSENT(nlm_syscall);
103 #endif
104 static bool_t nlm_syscall_registered = FALSE;
105
106 /*
107 * Debug level passed in from userland. We also support a sysctl hook
108 * so that it can be changed on a live system.
109 */
110 static int nlm_debug_level;
111 SYSCTL_INT(_debug, OID_AUTO, nlm_debug, CTLFLAG_RW, &nlm_debug_level, 0, "");
112
113 #define NLM_DEBUG(_level, args...) \
114 do { \
115 if (nlm_debug_level >= (_level)) \
116 log(LOG_DEBUG, args); \
117 } while(0)
118 #define NLM_ERR(args...) \
119 do { \
120 log(LOG_ERR, args); \
121 } while(0)
122
123 /*
124 * Grace period handling. The value of nlm_grace_threshold is the
125 * value of time_uptime after which we are serving requests normally.
126 */
127 static time_t nlm_grace_threshold;
128
129 /*
130 * We check for idle hosts if time_uptime is greater than
131 * nlm_next_idle_check,
132 */
133 static time_t nlm_next_idle_check;
134
135 /*
136 * A flag to indicate the server is already running.
137 */
138 static int nlm_is_running;
139
140 /*
141 * A socket to use for RPC - shared by all IPv4 RPC clients.
142 */
143 static struct socket *nlm_socket;
144
145 #ifdef INET6
146
147 /*
148 * A socket to use for RPC - shared by all IPv6 RPC clients.
149 */
150 static struct socket *nlm_socket6;
151
152 #endif
153
154 /*
155 * An RPC client handle that can be used to communicate with the local
156 * NSM.
157 */
158 static CLIENT *nlm_nsm;
159
160 /*
161 * An AUTH handle for the server's creds.
162 */
163 static AUTH *nlm_auth;
164
165 /*
166 * A zero timeval for sending async RPC messages.
167 */
168 struct timeval nlm_zero_tv = { 0, 0 };
169
170 /*
171 * The local NSM state number
172 */
173 int nlm_nsm_state;
174
175
176 /*
177 * A lock to protect the host list and waiting lock list.
178 */
179 static struct mtx nlm_global_lock;
180
181 /*
182 * Locks:
183 * (l) locked by nh_lock
184 * (s) only accessed via server RPC which is single threaded
185 * (g) locked by nlm_global_lock
186 * (c) const until freeing
187 * (a) modified using atomic ops
188 */
189
190 /*
191 * A pending client-side lock request, stored on the nlm_waiting_locks
192 * list.
193 */
194 struct nlm_waiting_lock {
195 TAILQ_ENTRY(nlm_waiting_lock) nw_link; /* (g) */
196 bool_t nw_waiting; /* (g) */
197 nlm4_lock nw_lock; /* (c) */
198 union nfsfh nw_fh; /* (c) */
199 struct vnode *nw_vp; /* (c) */
200 };
201 TAILQ_HEAD(nlm_waiting_lock_list, nlm_waiting_lock);
202
203 struct nlm_waiting_lock_list nlm_waiting_locks; /* (g) */
204
205 /*
206 * A pending server-side asynchronous lock request, stored on the
207 * nh_pending list of the NLM host.
208 */
209 struct nlm_async_lock {
210 TAILQ_ENTRY(nlm_async_lock) af_link; /* (l) host's list of locks */
211 struct task af_task; /* (c) async callback details */
212 void *af_cookie; /* (l) lock manager cancel token */
213 struct vnode *af_vp; /* (l) vnode to lock */
214 struct flock af_fl; /* (c) lock details */
215 struct nlm_host *af_host; /* (c) host which is locking */
216 CLIENT *af_rpc; /* (c) rpc client to send message */
217 nlm4_testargs af_granted; /* (c) notification details */
218 time_t af_expiretime; /* (c) notification time */
219 };
220 TAILQ_HEAD(nlm_async_lock_list, nlm_async_lock);
221
222 /*
223 * NLM host.
224 */
225 enum nlm_host_state {
226 NLM_UNMONITORED,
227 NLM_MONITORED,
228 NLM_MONITOR_FAILED,
229 NLM_RECOVERING
230 };
231
232 struct nlm_rpc {
233 CLIENT *nr_client; /* (l) RPC client handle */
234 time_t nr_create_time; /* (l) when client was created */
235 };
236
237 struct nlm_host {
238 struct mtx nh_lock;
239 volatile u_int nh_refs; /* (a) reference count */
240 TAILQ_ENTRY(nlm_host) nh_link; /* (g) global list of hosts */
241 char nh_caller_name[MAXNAMELEN]; /* (c) printable name of host */
242 uint32_t nh_sysid; /* (c) our allocaed system ID */
243 char nh_sysid_string[10]; /* (c) string rep. of sysid */
244 struct sockaddr_storage nh_addr; /* (s) remote address of host */
245 struct nlm_rpc nh_srvrpc; /* (l) RPC for server replies */
246 struct nlm_rpc nh_clntrpc; /* (l) RPC for client requests */
247 rpcvers_t nh_vers; /* (s) NLM version of host */
248 int nh_state; /* (s) last seen NSM state of host */
249 enum nlm_host_state nh_monstate; /* (l) local NSM monitoring state */
250 time_t nh_idle_timeout; /* (s) Time at which host is idle */
251 struct sysctl_ctx_list nh_sysctl; /* (c) vfs.nlm.sysid nodes */
252 uint32_t nh_grantcookie; /* (l) grant cookie counter */
253 struct nlm_async_lock_list nh_pending; /* (l) pending async locks */
254 struct nlm_async_lock_list nh_granted; /* (l) granted locks */
255 struct nlm_async_lock_list nh_finished; /* (l) finished async locks */
256 };
257 TAILQ_HEAD(nlm_host_list, nlm_host);
258
259 static struct nlm_host_list nlm_hosts; /* (g) */
260 static uint32_t nlm_next_sysid = 1; /* (g) */
261
262 static void nlm_host_unmonitor(struct nlm_host *);
263
264 struct nlm_grantcookie {
265 uint32_t ng_sysid;
266 uint32_t ng_cookie;
267 };
268
269 static inline uint32_t
ng_sysid(struct netobj * src)270 ng_sysid(struct netobj *src)
271 {
272
273 return ((struct nlm_grantcookie *)src->n_bytes)->ng_sysid;
274 }
275
276 static inline uint32_t
ng_cookie(struct netobj * src)277 ng_cookie(struct netobj *src)
278 {
279
280 return ((struct nlm_grantcookie *)src->n_bytes)->ng_cookie;
281 }
282
283 /**********************************************************************/
284
285 /*
286 * Initialise NLM globals.
287 */
288 static void
nlm_init(void * dummy)289 nlm_init(void *dummy)
290 {
291 int error;
292
293 mtx_init(&nlm_global_lock, "nlm_global_lock", NULL, MTX_DEF);
294 TAILQ_INIT(&nlm_waiting_locks);
295 TAILQ_INIT(&nlm_hosts);
296
297 error = syscall_register(&nlm_syscall_offset, &nlm_syscall_sysent,
298 &nlm_syscall_prev_sysent);
299 if (error)
300 NLM_ERR("Can't register NLM syscall\n");
301 else
302 nlm_syscall_registered = TRUE;
303 }
304 SYSINIT(nlm_init, SI_SUB_LOCK, SI_ORDER_FIRST, nlm_init, NULL);
305
306 static void
nlm_uninit(void * dummy)307 nlm_uninit(void *dummy)
308 {
309
310 if (nlm_syscall_registered)
311 syscall_deregister(&nlm_syscall_offset,
312 &nlm_syscall_prev_sysent);
313 }
314 SYSUNINIT(nlm_uninit, SI_SUB_LOCK, SI_ORDER_FIRST, nlm_uninit, NULL);
315
316 /*
317 * Create a netobj from an arbitrary source.
318 */
319 void
nlm_make_netobj(struct netobj * dst,caddr_t src,size_t srcsize,struct malloc_type * type)320 nlm_make_netobj(struct netobj *dst, caddr_t src, size_t srcsize,
321 struct malloc_type *type)
322 {
323
324 dst->n_len = srcsize;
325 dst->n_bytes = malloc(srcsize, type, M_WAITOK);
326 memcpy(dst->n_bytes, src, srcsize);
327 }
328
329 /*
330 * Copy a struct netobj.
331 */
332 void
nlm_copy_netobj(struct netobj * dst,struct netobj * src,struct malloc_type * type)333 nlm_copy_netobj(struct netobj *dst, struct netobj *src,
334 struct malloc_type *type)
335 {
336
337 nlm_make_netobj(dst, src->n_bytes, src->n_len, type);
338 }
339
340
341 /*
342 * Create an RPC client handle for the given (address,prog,vers)
343 * triple using UDP.
344 */
345 static CLIENT *
nlm_get_rpc(struct sockaddr * sa,rpcprog_t prog,rpcvers_t vers)346 nlm_get_rpc(struct sockaddr *sa, rpcprog_t prog, rpcvers_t vers)
347 {
348 char *wchan = "nlmrcv";
349 const char* protofmly;
350 struct sockaddr_storage ss;
351 struct socket *so;
352 CLIENT *rpcb;
353 struct timeval timo;
354 RPCB parms;
355 char *uaddr;
356 enum clnt_stat stat = RPC_SUCCESS;
357 int rpcvers = RPCBVERS4;
358 bool_t do_tcp = FALSE;
359 bool_t tryagain = FALSE;
360 struct portmap mapping;
361 u_short port = 0;
362
363 char tmp[sizeof "ffff:ffff:ffff:ffff:ffff:ffff:255.255.255.255"];
364
365 /*
366 * First we need to contact the remote RPCBIND service to find
367 * the right port.
368 */
369 memcpy(&ss, sa, sa->sa_len);
370 switch (ss.ss_family) {
371 case AF_INET:
372 ((struct sockaddr_in *)&ss)->sin_port = htons(111);
373 protofmly = "inet";
374 so = nlm_socket;
375 break;
376
377 #ifdef INET6
378 case AF_INET6:
379 ((struct sockaddr_in6 *)&ss)->sin6_port = htons(111);
380 protofmly = "inet6";
381 so = nlm_socket6;
382 break;
383 #endif
384
385 default:
386 /*
387 * Unsupported address family - fail.
388 */
389 return (NULL);
390 }
391
392 rpcb = clnt_dg_create(so, (struct sockaddr *)&ss,
393 RPCBPROG, rpcvers, 0, 0);
394 if (!rpcb)
395 return (NULL);
396
397 try_tcp:
398 parms.r_prog = prog;
399 parms.r_vers = vers;
400 if (do_tcp)
401 parms.r_netid = "tcp";
402 else
403 parms.r_netid = "udp";
404 parms.r_addr = "";
405 parms.r_owner = "";
406
407 /*
408 * Use the default timeout.
409 */
410 timo.tv_sec = 25;
411 timo.tv_usec = 0;
412 again:
413 switch (rpcvers) {
414 case RPCBVERS4:
415 case RPCBVERS:
416 /*
417 * Try RPCBIND 4 then 3.
418 */
419 uaddr = NULL;
420 stat = CLNT_CALL(rpcb, (rpcprog_t) RPCBPROC_GETADDR,
421 (xdrproc_t) xdr_rpcb, &parms,
422 (xdrproc_t) xdr_wrapstring, &uaddr, timo);
423 if (stat == RPC_SUCCESS) {
424 /*
425 * We have a reply from the remote RPCBIND - turn it
426 * into an appropriate address and make a new client
427 * that can talk to the remote NLM.
428 *
429 * XXX fixup IPv6 scope ID.
430 */
431 struct netbuf *a;
432 a = __rpc_uaddr2taddr_af(ss.ss_family, uaddr);
433 if (!a) {
434 tryagain = TRUE;
435 } else {
436 tryagain = FALSE;
437 memcpy(&ss, a->buf, a->len);
438 free(a->buf, M_RPC);
439 free(a, M_RPC);
440 xdr_free((xdrproc_t) xdr_wrapstring, &uaddr);
441 }
442 }
443 if (tryagain || stat == RPC_PROGVERSMISMATCH) {
444 if (rpcvers == RPCBVERS4)
445 rpcvers = RPCBVERS;
446 else if (rpcvers == RPCBVERS)
447 rpcvers = PMAPVERS;
448 CLNT_CONTROL(rpcb, CLSET_VERS, &rpcvers);
449 goto again;
450 }
451 break;
452 case PMAPVERS:
453 /*
454 * Try portmap.
455 */
456 mapping.pm_prog = parms.r_prog;
457 mapping.pm_vers = parms.r_vers;
458 mapping.pm_prot = do_tcp ? IPPROTO_TCP : IPPROTO_UDP;
459 mapping.pm_port = 0;
460
461 stat = CLNT_CALL(rpcb, (rpcprog_t) PMAPPROC_GETPORT,
462 (xdrproc_t) xdr_portmap, &mapping,
463 (xdrproc_t) xdr_u_short, &port, timo);
464
465 if (stat == RPC_SUCCESS) {
466 switch (ss.ss_family) {
467 case AF_INET:
468 ((struct sockaddr_in *)&ss)->sin_port =
469 htons(port);
470 break;
471
472 #ifdef INET6
473 case AF_INET6:
474 ((struct sockaddr_in6 *)&ss)->sin6_port =
475 htons(port);
476 break;
477 #endif
478 }
479 }
480 break;
481 default:
482 panic("invalid rpcvers %d", rpcvers);
483 }
484 /*
485 * We may have a positive response from the portmapper, but the NLM
486 * service was not found. Make sure we received a valid port.
487 */
488 switch (ss.ss_family) {
489 case AF_INET:
490 port = ((struct sockaddr_in *)&ss)->sin_port;
491 break;
492 #ifdef INET6
493 case AF_INET6:
494 port = ((struct sockaddr_in6 *)&ss)->sin6_port;
495 break;
496 #endif
497 }
498 if (stat != RPC_SUCCESS || !port) {
499 /*
500 * If we were able to talk to rpcbind or portmap, but the udp
501 * variant wasn't available, ask about tcp.
502 *
503 * XXX - We could also check for a TCP portmapper, but
504 * if the host is running a portmapper at all, we should be able
505 * to hail it over UDP.
506 */
507 if (stat == RPC_SUCCESS && !do_tcp) {
508 do_tcp = TRUE;
509 goto try_tcp;
510 }
511
512 /* Otherwise, bad news. */
513 switch (sa->sa_family) {
514 case AF_INET:
515 inet_ntop(AF_INET,
516 &((const struct sockaddr_in *) sa)->sin_addr,
517 tmp, sizeof tmp);
518 break;
519 #ifdef INET6
520 case AF_INET6:
521 inet_ntop(AF_INET6,
522 &((const struct sockaddr_in6 *) sa)->sin6_addr,
523 tmp, sizeof tmp);
524 break;
525 #endif
526 default:
527 strlcpy(tmp, "<unknown>", sizeof(tmp));
528 }
529
530 NLM_ERR("NLM: failed to contact remote rpcbind, "
531 "stat = %d, addr = %s, port = %d\n", (int) stat, tmp, port);
532 CLNT_DESTROY(rpcb);
533 return (NULL);
534 }
535
536 if (do_tcp) {
537 /*
538 * Destroy the UDP client we used to speak to rpcbind and
539 * recreate as a TCP client.
540 */
541 struct netconfig *nconf = NULL;
542
543 CLNT_DESTROY(rpcb);
544
545 switch (ss.ss_family) {
546 case AF_INET:
547 nconf = getnetconfigent("tcp");
548 break;
549 #ifdef INET6
550 case AF_INET6:
551 nconf = getnetconfigent("tcp6");
552 break;
553 #endif
554 }
555
556 rpcb = clnt_reconnect_create(nconf, (struct sockaddr *)&ss,
557 prog, vers, 0, 0);
558 CLNT_CONTROL(rpcb, CLSET_WAITCHAN, wchan);
559 rpcb->cl_auth = nlm_auth;
560
561 } else {
562 /*
563 * Re-use the client we used to speak to rpcbind.
564 */
565 CLNT_CONTROL(rpcb, CLSET_SVC_ADDR, &ss);
566 CLNT_CONTROL(rpcb, CLSET_PROG, &prog);
567 CLNT_CONTROL(rpcb, CLSET_VERS, &vers);
568 CLNT_CONTROL(rpcb, CLSET_WAITCHAN, wchan);
569 rpcb->cl_auth = nlm_auth;
570 }
571
572 return (rpcb);
573 }
574
575 /*
576 * This async callback after when an async lock request has been
577 * granted. We notify the host which initiated the request.
578 */
579 static void
nlm_lock_callback(void * arg,int pending)580 nlm_lock_callback(void *arg, int pending)
581 {
582 struct nlm_async_lock *af = (struct nlm_async_lock *) arg;
583 struct rpc_callextra ext;
584
585 NLM_DEBUG(2, "NLM: async lock %p for %s (sysid %d) granted,"
586 " cookie %d:%d\n", af, af->af_host->nh_caller_name,
587 af->af_host->nh_sysid, ng_sysid(&af->af_granted.cookie),
588 ng_cookie(&af->af_granted.cookie));
589
590 /*
591 * Send the results back to the host.
592 *
593 * Note: there is a possible race here with nlm_host_notify
594 * destroying the RPC client. To avoid problems, the first
595 * thing nlm_host_notify does is to cancel pending async lock
596 * requests.
597 */
598 memset(&ext, 0, sizeof(ext));
599 ext.rc_auth = nlm_auth;
600 if (af->af_host->nh_vers == NLM_VERS4) {
601 nlm4_granted_msg_4(&af->af_granted,
602 NULL, af->af_rpc, &ext, nlm_zero_tv);
603 } else {
604 /*
605 * Back-convert to legacy protocol
606 */
607 nlm_testargs granted;
608 granted.cookie = af->af_granted.cookie;
609 granted.exclusive = af->af_granted.exclusive;
610 granted.alock.caller_name =
611 af->af_granted.alock.caller_name;
612 granted.alock.fh = af->af_granted.alock.fh;
613 granted.alock.oh = af->af_granted.alock.oh;
614 granted.alock.svid = af->af_granted.alock.svid;
615 granted.alock.l_offset =
616 af->af_granted.alock.l_offset;
617 granted.alock.l_len =
618 af->af_granted.alock.l_len;
619
620 nlm_granted_msg_1(&granted,
621 NULL, af->af_rpc, &ext, nlm_zero_tv);
622 }
623
624 /*
625 * Move this entry to the nh_granted list.
626 */
627 af->af_expiretime = time_uptime + NLM_EXPIRE_TIMEOUT;
628 mtx_lock(&af->af_host->nh_lock);
629 TAILQ_REMOVE(&af->af_host->nh_pending, af, af_link);
630 TAILQ_INSERT_TAIL(&af->af_host->nh_granted, af, af_link);
631 mtx_unlock(&af->af_host->nh_lock);
632 }
633
634 /*
635 * Free an async lock request. The request must have been removed from
636 * any list.
637 */
638 static void
nlm_free_async_lock(struct nlm_async_lock * af)639 nlm_free_async_lock(struct nlm_async_lock *af)
640 {
641 /*
642 * Free an async lock.
643 */
644 if (af->af_rpc)
645 CLNT_RELEASE(af->af_rpc);
646 xdr_free((xdrproc_t) xdr_nlm4_testargs, &af->af_granted);
647 if (af->af_vp)
648 vrele(af->af_vp);
649 free(af, M_NLM);
650 }
651
652 /*
653 * Cancel our async request - this must be called with
654 * af->nh_host->nh_lock held. This is slightly complicated by a
655 * potential race with our own callback. If we fail to cancel the
656 * lock, it must already have been granted - we make sure our async
657 * task has completed by calling taskqueue_drain in this case.
658 */
659 static int
nlm_cancel_async_lock(struct nlm_async_lock * af)660 nlm_cancel_async_lock(struct nlm_async_lock *af)
661 {
662 struct nlm_host *host = af->af_host;
663 int error;
664
665 mtx_assert(&host->nh_lock, MA_OWNED);
666
667 mtx_unlock(&host->nh_lock);
668
669 error = VOP_ADVLOCKASYNC(af->af_vp, NULL, F_CANCEL, &af->af_fl,
670 F_REMOTE, NULL, &af->af_cookie);
671
672 if (error) {
673 /*
674 * We failed to cancel - make sure our callback has
675 * completed before we continue.
676 */
677 taskqueue_drain(taskqueue_thread, &af->af_task);
678 }
679
680 mtx_lock(&host->nh_lock);
681
682 if (!error) {
683 NLM_DEBUG(2, "NLM: async lock %p for %s (sysid %d) "
684 "cancelled\n", af, host->nh_caller_name, host->nh_sysid);
685
686 /*
687 * Remove from the nh_pending list and free now that
688 * we are safe from the callback.
689 */
690 TAILQ_REMOVE(&host->nh_pending, af, af_link);
691 mtx_unlock(&host->nh_lock);
692 nlm_free_async_lock(af);
693 mtx_lock(&host->nh_lock);
694 }
695
696 return (error);
697 }
698
699 static void
nlm_check_expired_locks(struct nlm_host * host)700 nlm_check_expired_locks(struct nlm_host *host)
701 {
702 struct nlm_async_lock *af;
703 time_t uptime = time_uptime;
704
705 mtx_lock(&host->nh_lock);
706 while ((af = TAILQ_FIRST(&host->nh_granted)) != NULL
707 && uptime >= af->af_expiretime) {
708 NLM_DEBUG(2, "NLM: async lock %p for %s (sysid %d) expired,"
709 " cookie %d:%d\n", af, af->af_host->nh_caller_name,
710 af->af_host->nh_sysid, ng_sysid(&af->af_granted.cookie),
711 ng_cookie(&af->af_granted.cookie));
712 TAILQ_REMOVE(&host->nh_granted, af, af_link);
713 mtx_unlock(&host->nh_lock);
714 nlm_free_async_lock(af);
715 mtx_lock(&host->nh_lock);
716 }
717 while ((af = TAILQ_FIRST(&host->nh_finished)) != NULL) {
718 TAILQ_REMOVE(&host->nh_finished, af, af_link);
719 mtx_unlock(&host->nh_lock);
720 nlm_free_async_lock(af);
721 mtx_lock(&host->nh_lock);
722 }
723 mtx_unlock(&host->nh_lock);
724 }
725
726 /*
727 * Free resources used by a host. This is called after the reference
728 * count has reached zero so it doesn't need to worry about locks.
729 */
730 static void
nlm_host_destroy(struct nlm_host * host)731 nlm_host_destroy(struct nlm_host *host)
732 {
733
734 mtx_lock(&nlm_global_lock);
735 TAILQ_REMOVE(&nlm_hosts, host, nh_link);
736 mtx_unlock(&nlm_global_lock);
737
738 if (host->nh_srvrpc.nr_client)
739 CLNT_RELEASE(host->nh_srvrpc.nr_client);
740 if (host->nh_clntrpc.nr_client)
741 CLNT_RELEASE(host->nh_clntrpc.nr_client);
742 mtx_destroy(&host->nh_lock);
743 sysctl_ctx_free(&host->nh_sysctl);
744 free(host, M_NLM);
745 }
746
747 /*
748 * Thread start callback for client lock recovery
749 */
750 static void
nlm_client_recovery_start(void * arg)751 nlm_client_recovery_start(void *arg)
752 {
753 struct nlm_host *host = (struct nlm_host *) arg;
754
755 NLM_DEBUG(1, "NLM: client lock recovery for %s started\n",
756 host->nh_caller_name);
757
758 nlm_client_recovery(host);
759
760 NLM_DEBUG(1, "NLM: client lock recovery for %s completed\n",
761 host->nh_caller_name);
762
763 host->nh_monstate = NLM_MONITORED;
764 nlm_host_release(host);
765
766 kthread_exit();
767 }
768
769 /*
770 * This is called when we receive a host state change notification. We
771 * unlock any active locks owned by the host. When rpc.lockd is
772 * shutting down, this function is called with newstate set to zero
773 * which allows us to cancel any pending async locks and clear the
774 * locking state.
775 */
776 static void
nlm_host_notify(struct nlm_host * host,int newstate)777 nlm_host_notify(struct nlm_host *host, int newstate)
778 {
779 struct nlm_async_lock *af;
780
781 if (newstate) {
782 NLM_DEBUG(1, "NLM: host %s (sysid %d) rebooted, new "
783 "state is %d\n", host->nh_caller_name,
784 host->nh_sysid, newstate);
785 }
786
787 /*
788 * Cancel any pending async locks for this host.
789 */
790 mtx_lock(&host->nh_lock);
791 while ((af = TAILQ_FIRST(&host->nh_pending)) != NULL) {
792 /*
793 * nlm_cancel_async_lock will remove the entry from
794 * nh_pending and free it.
795 */
796 nlm_cancel_async_lock(af);
797 }
798 mtx_unlock(&host->nh_lock);
799 nlm_check_expired_locks(host);
800
801 /*
802 * The host just rebooted - trash its locks.
803 */
804 lf_clearremotesys(host->nh_sysid);
805 host->nh_state = newstate;
806
807 /*
808 * If we have any remote locks for this host (i.e. it
809 * represents a remote NFS server that our local NFS client
810 * has locks for), start a recovery thread.
811 */
812 if (newstate != 0
813 && host->nh_monstate != NLM_RECOVERING
814 && lf_countlocks(NLM_SYSID_CLIENT | host->nh_sysid) > 0) {
815 struct thread *td;
816 host->nh_monstate = NLM_RECOVERING;
817 refcount_acquire(&host->nh_refs);
818 kthread_add(nlm_client_recovery_start, host, curproc, &td, 0, 0,
819 "NFS lock recovery for %s", host->nh_caller_name);
820 }
821 }
822
823 /*
824 * Sysctl handler to count the number of locks for a sysid.
825 */
826 static int
nlm_host_lock_count_sysctl(SYSCTL_HANDLER_ARGS)827 nlm_host_lock_count_sysctl(SYSCTL_HANDLER_ARGS)
828 {
829 struct nlm_host *host;
830 int count;
831
832 host = oidp->oid_arg1;
833 count = lf_countlocks(host->nh_sysid);
834 return sysctl_handle_int(oidp, &count, 0, req);
835 }
836
837 /*
838 * Sysctl handler to count the number of client locks for a sysid.
839 */
840 static int
nlm_host_client_lock_count_sysctl(SYSCTL_HANDLER_ARGS)841 nlm_host_client_lock_count_sysctl(SYSCTL_HANDLER_ARGS)
842 {
843 struct nlm_host *host;
844 int count;
845
846 host = oidp->oid_arg1;
847 count = lf_countlocks(NLM_SYSID_CLIENT | host->nh_sysid);
848 return sysctl_handle_int(oidp, &count, 0, req);
849 }
850
851 /*
852 * Create a new NLM host.
853 */
854 static struct nlm_host *
nlm_create_host(const char * caller_name)855 nlm_create_host(const char* caller_name)
856 {
857 struct nlm_host *host;
858 struct sysctl_oid *oid;
859
860 mtx_assert(&nlm_global_lock, MA_OWNED);
861
862 NLM_DEBUG(1, "NLM: new host %s (sysid %d)\n",
863 caller_name, nlm_next_sysid);
864 host = malloc(sizeof(struct nlm_host), M_NLM, M_NOWAIT|M_ZERO);
865 if (!host)
866 return (NULL);
867 mtx_init(&host->nh_lock, "nh_lock", NULL, MTX_DEF);
868 host->nh_refs = 1;
869 strlcpy(host->nh_caller_name, caller_name, MAXNAMELEN);
870 host->nh_sysid = nlm_next_sysid++;
871 snprintf(host->nh_sysid_string, sizeof(host->nh_sysid_string),
872 "%d", host->nh_sysid);
873 host->nh_vers = 0;
874 host->nh_state = 0;
875 host->nh_monstate = NLM_UNMONITORED;
876 host->nh_grantcookie = 1;
877 TAILQ_INIT(&host->nh_pending);
878 TAILQ_INIT(&host->nh_granted);
879 TAILQ_INIT(&host->nh_finished);
880 TAILQ_INSERT_TAIL(&nlm_hosts, host, nh_link);
881
882 mtx_unlock(&nlm_global_lock);
883
884 sysctl_ctx_init(&host->nh_sysctl);
885 oid = SYSCTL_ADD_NODE(&host->nh_sysctl,
886 SYSCTL_STATIC_CHILDREN(_vfs_nlm_sysid),
887 OID_AUTO, host->nh_sysid_string, CTLFLAG_RD, NULL, "");
888 SYSCTL_ADD_STRING(&host->nh_sysctl, SYSCTL_CHILDREN(oid), OID_AUTO,
889 "hostname", CTLFLAG_RD, host->nh_caller_name, 0, "");
890 SYSCTL_ADD_UINT(&host->nh_sysctl, SYSCTL_CHILDREN(oid), OID_AUTO,
891 "version", CTLFLAG_RD, &host->nh_vers, 0, "");
892 SYSCTL_ADD_UINT(&host->nh_sysctl, SYSCTL_CHILDREN(oid), OID_AUTO,
893 "monitored", CTLFLAG_RD, &host->nh_monstate, 0, "");
894 SYSCTL_ADD_PROC(&host->nh_sysctl, SYSCTL_CHILDREN(oid), OID_AUTO,
895 "lock_count", CTLTYPE_INT | CTLFLAG_RD, host, 0,
896 nlm_host_lock_count_sysctl, "I", "");
897 SYSCTL_ADD_PROC(&host->nh_sysctl, SYSCTL_CHILDREN(oid), OID_AUTO,
898 "client_lock_count", CTLTYPE_INT | CTLFLAG_RD, host, 0,
899 nlm_host_client_lock_count_sysctl, "I", "");
900
901 mtx_lock(&nlm_global_lock);
902
903 return (host);
904 }
905
906 /*
907 * Acquire the next sysid for remote locks not handled by the NLM.
908 */
909 uint32_t
nlm_acquire_next_sysid(void)910 nlm_acquire_next_sysid(void)
911 {
912 uint32_t next_sysid;
913
914 mtx_lock(&nlm_global_lock);
915 next_sysid = nlm_next_sysid++;
916 mtx_unlock(&nlm_global_lock);
917 return (next_sysid);
918 }
919
920 /*
921 * Return non-zero if the address parts of the two sockaddrs are the
922 * same.
923 */
924 static int
nlm_compare_addr(const struct sockaddr * a,const struct sockaddr * b)925 nlm_compare_addr(const struct sockaddr *a, const struct sockaddr *b)
926 {
927 const struct sockaddr_in *a4, *b4;
928 #ifdef INET6
929 const struct sockaddr_in6 *a6, *b6;
930 #endif
931
932 if (a->sa_family != b->sa_family)
933 return (FALSE);
934
935 switch (a->sa_family) {
936 case AF_INET:
937 a4 = (const struct sockaddr_in *) a;
938 b4 = (const struct sockaddr_in *) b;
939 return !memcmp(&a4->sin_addr, &b4->sin_addr,
940 sizeof(a4->sin_addr));
941 #ifdef INET6
942 case AF_INET6:
943 a6 = (const struct sockaddr_in6 *) a;
944 b6 = (const struct sockaddr_in6 *) b;
945 return !memcmp(&a6->sin6_addr, &b6->sin6_addr,
946 sizeof(a6->sin6_addr));
947 #endif
948 }
949
950 return (0);
951 }
952
953 /*
954 * Check for idle hosts and stop monitoring them. We could also free
955 * the host structure here, possibly after a larger timeout but that
956 * would require some care to avoid races with
957 * e.g. nlm_host_lock_count_sysctl.
958 */
959 static void
nlm_check_idle(void)960 nlm_check_idle(void)
961 {
962 struct nlm_host *host;
963
964 mtx_assert(&nlm_global_lock, MA_OWNED);
965
966 if (time_uptime <= nlm_next_idle_check)
967 return;
968
969 nlm_next_idle_check = time_uptime + NLM_IDLE_PERIOD;
970
971 TAILQ_FOREACH(host, &nlm_hosts, nh_link) {
972 if (host->nh_monstate == NLM_MONITORED
973 && time_uptime > host->nh_idle_timeout) {
974 mtx_unlock(&nlm_global_lock);
975 if (lf_countlocks(host->nh_sysid) > 0
976 || lf_countlocks(NLM_SYSID_CLIENT
977 + host->nh_sysid)) {
978 host->nh_idle_timeout =
979 time_uptime + NLM_IDLE_TIMEOUT;
980 mtx_lock(&nlm_global_lock);
981 continue;
982 }
983 nlm_host_unmonitor(host);
984 mtx_lock(&nlm_global_lock);
985 }
986 }
987 }
988
989 /*
990 * Search for an existing NLM host that matches the given name
991 * (typically the caller_name element of an nlm4_lock). If none is
992 * found, create a new host. If 'addr' is non-NULL, record the remote
993 * address of the host so that we can call it back for async
994 * responses. If 'vers' is greater than zero then record the NLM
995 * program version to use to communicate with this client.
996 */
997 struct nlm_host *
nlm_find_host_by_name(const char * name,const struct sockaddr * addr,rpcvers_t vers)998 nlm_find_host_by_name(const char *name, const struct sockaddr *addr,
999 rpcvers_t vers)
1000 {
1001 struct nlm_host *host;
1002
1003 mtx_lock(&nlm_global_lock);
1004
1005 /*
1006 * The remote host is determined by caller_name.
1007 */
1008 TAILQ_FOREACH(host, &nlm_hosts, nh_link) {
1009 if (!strcmp(host->nh_caller_name, name))
1010 break;
1011 }
1012
1013 if (!host) {
1014 host = nlm_create_host(name);
1015 if (!host) {
1016 mtx_unlock(&nlm_global_lock);
1017 return (NULL);
1018 }
1019 }
1020 refcount_acquire(&host->nh_refs);
1021
1022 host->nh_idle_timeout = time_uptime + NLM_IDLE_TIMEOUT;
1023
1024 /*
1025 * If we have an address for the host, record it so that we
1026 * can send async replies etc.
1027 */
1028 if (addr) {
1029
1030 KASSERT(addr->sa_len < sizeof(struct sockaddr_storage),
1031 ("Strange remote transport address length"));
1032
1033 /*
1034 * If we have seen an address before and we currently
1035 * have an RPC client handle, make sure the address is
1036 * the same, otherwise discard the client handle.
1037 */
1038 if (host->nh_addr.ss_len && host->nh_srvrpc.nr_client) {
1039 if (!nlm_compare_addr(
1040 (struct sockaddr *) &host->nh_addr,
1041 addr)
1042 || host->nh_vers != vers) {
1043 CLIENT *client;
1044 mtx_lock(&host->nh_lock);
1045 client = host->nh_srvrpc.nr_client;
1046 host->nh_srvrpc.nr_client = NULL;
1047 mtx_unlock(&host->nh_lock);
1048 if (client) {
1049 CLNT_RELEASE(client);
1050 }
1051 }
1052 }
1053 memcpy(&host->nh_addr, addr, addr->sa_len);
1054 host->nh_vers = vers;
1055 }
1056
1057 nlm_check_idle();
1058
1059 mtx_unlock(&nlm_global_lock);
1060
1061 return (host);
1062 }
1063
1064 /*
1065 * Search for an existing NLM host that matches the given remote
1066 * address. If none is found, create a new host with the requested
1067 * address and remember 'vers' as the NLM protocol version to use for
1068 * that host.
1069 */
1070 struct nlm_host *
nlm_find_host_by_addr(const struct sockaddr * addr,int vers)1071 nlm_find_host_by_addr(const struct sockaddr *addr, int vers)
1072 {
1073 /*
1074 * Fake up a name using inet_ntop. This buffer is
1075 * large enough for an IPv6 address.
1076 */
1077 char tmp[sizeof "ffff:ffff:ffff:ffff:ffff:ffff:255.255.255.255"];
1078 struct nlm_host *host;
1079
1080 switch (addr->sa_family) {
1081 case AF_INET:
1082 inet_ntop(AF_INET,
1083 &((const struct sockaddr_in *) addr)->sin_addr,
1084 tmp, sizeof tmp);
1085 break;
1086 #ifdef INET6
1087 case AF_INET6:
1088 inet_ntop(AF_INET6,
1089 &((const struct sockaddr_in6 *) addr)->sin6_addr,
1090 tmp, sizeof tmp);
1091 break;
1092 #endif
1093 default:
1094 strlcpy(tmp, "<unknown>", sizeof(tmp));
1095 }
1096
1097
1098 mtx_lock(&nlm_global_lock);
1099
1100 /*
1101 * The remote host is determined by caller_name.
1102 */
1103 TAILQ_FOREACH(host, &nlm_hosts, nh_link) {
1104 if (nlm_compare_addr(addr,
1105 (const struct sockaddr *) &host->nh_addr))
1106 break;
1107 }
1108
1109 if (!host) {
1110 host = nlm_create_host(tmp);
1111 if (!host) {
1112 mtx_unlock(&nlm_global_lock);
1113 return (NULL);
1114 }
1115 memcpy(&host->nh_addr, addr, addr->sa_len);
1116 host->nh_vers = vers;
1117 }
1118 refcount_acquire(&host->nh_refs);
1119
1120 host->nh_idle_timeout = time_uptime + NLM_IDLE_TIMEOUT;
1121
1122 nlm_check_idle();
1123
1124 mtx_unlock(&nlm_global_lock);
1125
1126 return (host);
1127 }
1128
1129 /*
1130 * Find the NLM host that matches the value of 'sysid'. If none
1131 * exists, return NULL.
1132 */
1133 static struct nlm_host *
nlm_find_host_by_sysid(int sysid)1134 nlm_find_host_by_sysid(int sysid)
1135 {
1136 struct nlm_host *host;
1137
1138 TAILQ_FOREACH(host, &nlm_hosts, nh_link) {
1139 if (host->nh_sysid == sysid) {
1140 refcount_acquire(&host->nh_refs);
1141 return (host);
1142 }
1143 }
1144
1145 return (NULL);
1146 }
1147
nlm_host_release(struct nlm_host * host)1148 void nlm_host_release(struct nlm_host *host)
1149 {
1150 if (refcount_release(&host->nh_refs)) {
1151 /*
1152 * Free the host
1153 */
1154 nlm_host_destroy(host);
1155 }
1156 }
1157
1158 static void
nlm_host_monfail(const struct nlm_host * host,bool action)1159 nlm_host_monfail(const struct nlm_host *host, bool action)
1160 {
1161 const char *monitor = (action ? "monitor" : "unmonitor");
1162
1163 char tmp[sizeof "ffff:ffff:ffff:ffff:ffff:ffff:255.255.255.255"];
1164 const struct sockaddr *addr = (const struct sockaddr *)&(host->nh_addr);
1165
1166 switch (addr->sa_family) {
1167 case AF_INET:
1168 inet_ntop(AF_INET,
1169 &((const struct sockaddr_in *) addr)->sin_addr,
1170 tmp, sizeof tmp);
1171 break;
1172 #ifdef INET6
1173 case AF_INET6:
1174 inet_ntop(AF_INET6,
1175 &((const struct sockaddr_in6 *) addr)->sin6_addr,
1176 tmp, sizeof tmp);
1177 break;
1178 #endif
1179 default:
1180 strlcpy(tmp, "<unknown>", sizeof(tmp));
1181 }
1182
1183 NLM_ERR("Local NSM refuses to %s %s (%s)\n",
1184 monitor, host->nh_caller_name, tmp);
1185 }
1186
1187 /*
1188 * Unregister this NLM host with the local NSM due to idleness.
1189 */
1190 static void
nlm_host_unmonitor(struct nlm_host * host)1191 nlm_host_unmonitor(struct nlm_host *host)
1192 {
1193 mon_id smmonid;
1194 sm_stat_res smstat;
1195 struct timeval timo;
1196 enum clnt_stat stat;
1197
1198 NLM_DEBUG(1, "NLM: unmonitoring %s (sysid %d)\n",
1199 host->nh_caller_name, host->nh_sysid);
1200
1201 /*
1202 * We put our assigned system ID value in the priv field to
1203 * make it simpler to find the host if we are notified of a
1204 * host restart.
1205 */
1206 smmonid.mon_name = host->nh_caller_name;
1207 smmonid.my_id.my_name = "localhost";
1208 smmonid.my_id.my_prog = NLM_PROG;
1209 smmonid.my_id.my_vers = NLM_SM;
1210 smmonid.my_id.my_proc = NLM_SM_NOTIFY;
1211
1212 timo.tv_sec = 25;
1213 timo.tv_usec = 0;
1214 stat = CLNT_CALL(nlm_nsm, SM_UNMON,
1215 (xdrproc_t) xdr_mon, &smmonid,
1216 (xdrproc_t) xdr_sm_stat, &smstat, timo);
1217
1218 if (stat != RPC_SUCCESS) {
1219 NLM_ERR("Failed to contact local NSM - rpc error %d\n", stat);
1220 return;
1221 }
1222 if (smstat.res_stat == stat_fail) {
1223 nlm_host_monfail(host, false);
1224 return;
1225 }
1226
1227 host->nh_monstate = NLM_UNMONITORED;
1228 }
1229
1230 /*
1231 * Register this NLM host with the local NSM so that we can be
1232 * notified if it reboots.
1233 */
1234 void
nlm_host_monitor(struct nlm_host * host,int state)1235 nlm_host_monitor(struct nlm_host *host, int state)
1236 {
1237 mon smmon;
1238 sm_stat_res smstat;
1239 struct timeval timo;
1240 enum clnt_stat stat;
1241
1242 if (state && !host->nh_state) {
1243 /*
1244 * This is the first time we have seen an NSM state
1245 * value for this host. We record it here to help
1246 * detect host reboots.
1247 */
1248 host->nh_state = state;
1249 NLM_DEBUG(1, "NLM: host %s (sysid %d) has NSM state %d\n",
1250 host->nh_caller_name, host->nh_sysid, state);
1251 }
1252
1253 mtx_lock(&host->nh_lock);
1254 if (host->nh_monstate != NLM_UNMONITORED) {
1255 mtx_unlock(&host->nh_lock);
1256 return;
1257 }
1258 host->nh_monstate = NLM_MONITORED;
1259 mtx_unlock(&host->nh_lock);
1260
1261 NLM_DEBUG(1, "NLM: monitoring %s (sysid %d)\n",
1262 host->nh_caller_name, host->nh_sysid);
1263
1264 /*
1265 * We put our assigned system ID value in the priv field to
1266 * make it simpler to find the host if we are notified of a
1267 * host restart.
1268 */
1269 smmon.mon_id.mon_name = host->nh_caller_name;
1270 smmon.mon_id.my_id.my_name = "localhost";
1271 smmon.mon_id.my_id.my_prog = NLM_PROG;
1272 smmon.mon_id.my_id.my_vers = NLM_SM;
1273 smmon.mon_id.my_id.my_proc = NLM_SM_NOTIFY;
1274 memcpy(smmon.priv, &host->nh_sysid, sizeof(host->nh_sysid));
1275
1276 timo.tv_sec = 25;
1277 timo.tv_usec = 0;
1278 stat = CLNT_CALL(nlm_nsm, SM_MON,
1279 (xdrproc_t) xdr_mon, &smmon,
1280 (xdrproc_t) xdr_sm_stat, &smstat, timo);
1281
1282 if (stat != RPC_SUCCESS) {
1283 NLM_ERR("Failed to contact local NSM - rpc error %d\n", stat);
1284 return;
1285 }
1286 if (smstat.res_stat == stat_fail) {
1287 nlm_host_monfail(host, true);
1288 mtx_lock(&host->nh_lock);
1289 host->nh_monstate = NLM_MONITOR_FAILED;
1290 mtx_unlock(&host->nh_lock);
1291 return;
1292 }
1293
1294 host->nh_monstate = NLM_MONITORED;
1295 }
1296
1297 /*
1298 * Return an RPC client handle that can be used to talk to the NLM
1299 * running on the given host.
1300 */
1301 CLIENT *
nlm_host_get_rpc(struct nlm_host * host,bool_t isserver)1302 nlm_host_get_rpc(struct nlm_host *host, bool_t isserver)
1303 {
1304 struct nlm_rpc *rpc;
1305 CLIENT *client;
1306
1307 mtx_lock(&host->nh_lock);
1308
1309 if (isserver)
1310 rpc = &host->nh_srvrpc;
1311 else
1312 rpc = &host->nh_clntrpc;
1313
1314 /*
1315 * We can't hold onto RPC handles for too long - the async
1316 * call/reply protocol used by some NLM clients makes it hard
1317 * to tell when they change port numbers (e.g. after a
1318 * reboot). Note that if a client reboots while it isn't
1319 * holding any locks, it won't bother to notify us. We
1320 * expire the RPC handles after two minutes.
1321 */
1322 if (rpc->nr_client && time_uptime > rpc->nr_create_time + 2*60) {
1323 client = rpc->nr_client;
1324 rpc->nr_client = NULL;
1325 mtx_unlock(&host->nh_lock);
1326 CLNT_RELEASE(client);
1327 mtx_lock(&host->nh_lock);
1328 }
1329
1330 if (!rpc->nr_client) {
1331 mtx_unlock(&host->nh_lock);
1332 client = nlm_get_rpc((struct sockaddr *)&host->nh_addr,
1333 NLM_PROG, host->nh_vers);
1334 mtx_lock(&host->nh_lock);
1335
1336 if (client) {
1337 if (rpc->nr_client) {
1338 mtx_unlock(&host->nh_lock);
1339 CLNT_DESTROY(client);
1340 mtx_lock(&host->nh_lock);
1341 } else {
1342 rpc->nr_client = client;
1343 rpc->nr_create_time = time_uptime;
1344 }
1345 }
1346 }
1347
1348 client = rpc->nr_client;
1349 if (client)
1350 CLNT_ACQUIRE(client);
1351 mtx_unlock(&host->nh_lock);
1352
1353 return (client);
1354
1355 }
1356
nlm_host_get_sysid(struct nlm_host * host)1357 int nlm_host_get_sysid(struct nlm_host *host)
1358 {
1359
1360 return (host->nh_sysid);
1361 }
1362
1363 int
nlm_host_get_state(struct nlm_host * host)1364 nlm_host_get_state(struct nlm_host *host)
1365 {
1366
1367 return (host->nh_state);
1368 }
1369
1370 void *
nlm_register_wait_lock(struct nlm4_lock * lock,struct vnode * vp)1371 nlm_register_wait_lock(struct nlm4_lock *lock, struct vnode *vp)
1372 {
1373 struct nlm_waiting_lock *nw;
1374
1375 nw = malloc(sizeof(struct nlm_waiting_lock), M_NLM, M_WAITOK);
1376 nw->nw_lock = *lock;
1377 memcpy(&nw->nw_fh.fh_bytes, nw->nw_lock.fh.n_bytes,
1378 nw->nw_lock.fh.n_len);
1379 nw->nw_lock.fh.n_bytes = nw->nw_fh.fh_bytes;
1380 nw->nw_waiting = TRUE;
1381 nw->nw_vp = vp;
1382 mtx_lock(&nlm_global_lock);
1383 TAILQ_INSERT_TAIL(&nlm_waiting_locks, nw, nw_link);
1384 mtx_unlock(&nlm_global_lock);
1385
1386 return nw;
1387 }
1388
1389 void
nlm_deregister_wait_lock(void * handle)1390 nlm_deregister_wait_lock(void *handle)
1391 {
1392 struct nlm_waiting_lock *nw = handle;
1393
1394 mtx_lock(&nlm_global_lock);
1395 TAILQ_REMOVE(&nlm_waiting_locks, nw, nw_link);
1396 mtx_unlock(&nlm_global_lock);
1397
1398 free(nw, M_NLM);
1399 }
1400
1401 int
nlm_wait_lock(void * handle,int timo)1402 nlm_wait_lock(void *handle, int timo)
1403 {
1404 struct nlm_waiting_lock *nw = handle;
1405 int error;
1406
1407 /*
1408 * If the granted message arrived before we got here,
1409 * nw->nw_waiting will be FALSE - in that case, don't sleep.
1410 */
1411 mtx_lock(&nlm_global_lock);
1412 error = 0;
1413 if (nw->nw_waiting)
1414 error = msleep(nw, &nlm_global_lock, PCATCH, "nlmlock", timo);
1415 TAILQ_REMOVE(&nlm_waiting_locks, nw, nw_link);
1416 if (error) {
1417 /*
1418 * The granted message may arrive after the
1419 * interrupt/timeout but before we manage to lock the
1420 * mutex. Detect this by examining nw_lock.
1421 */
1422 if (!nw->nw_waiting)
1423 error = 0;
1424 } else {
1425 /*
1426 * If nlm_cancel_wait is called, then error will be
1427 * zero but nw_waiting will still be TRUE. We
1428 * translate this into EINTR.
1429 */
1430 if (nw->nw_waiting)
1431 error = EINTR;
1432 }
1433 mtx_unlock(&nlm_global_lock);
1434
1435 free(nw, M_NLM);
1436
1437 return (error);
1438 }
1439
1440 void
nlm_cancel_wait(struct vnode * vp)1441 nlm_cancel_wait(struct vnode *vp)
1442 {
1443 struct nlm_waiting_lock *nw;
1444
1445 mtx_lock(&nlm_global_lock);
1446 TAILQ_FOREACH(nw, &nlm_waiting_locks, nw_link) {
1447 if (nw->nw_vp == vp) {
1448 wakeup(nw);
1449 }
1450 }
1451 mtx_unlock(&nlm_global_lock);
1452 }
1453
1454
1455 /**********************************************************************/
1456
1457 /*
1458 * Syscall interface with userland.
1459 */
1460
1461 extern void nlm_prog_0(struct svc_req *rqstp, SVCXPRT *transp);
1462 extern void nlm_prog_1(struct svc_req *rqstp, SVCXPRT *transp);
1463 extern void nlm_prog_3(struct svc_req *rqstp, SVCXPRT *transp);
1464 extern void nlm_prog_4(struct svc_req *rqstp, SVCXPRT *transp);
1465
1466 static int
nlm_register_services(SVCPOOL * pool,int addr_count,char ** addrs)1467 nlm_register_services(SVCPOOL *pool, int addr_count, char **addrs)
1468 {
1469 static rpcvers_t versions[] = {
1470 NLM_SM, NLM_VERS, NLM_VERSX, NLM_VERS4
1471 };
1472 static void (*dispatchers[])(struct svc_req *, SVCXPRT *) = {
1473 nlm_prog_0, nlm_prog_1, nlm_prog_3, nlm_prog_4
1474 };
1475 static const int version_count = sizeof(versions) / sizeof(versions[0]);
1476
1477 SVCXPRT **xprts;
1478 char netid[16];
1479 char uaddr[128];
1480 struct netconfig *nconf;
1481 int i, j, error;
1482
1483 if (!addr_count) {
1484 NLM_ERR("NLM: no service addresses given - can't start server");
1485 return (EINVAL);
1486 }
1487
1488 xprts = malloc(addr_count * sizeof(SVCXPRT *), M_NLM, M_WAITOK|M_ZERO);
1489 for (i = 0; i < version_count; i++) {
1490 for (j = 0; j < addr_count; j++) {
1491 /*
1492 * Create transports for the first version and
1493 * then just register everything else to the
1494 * same transports.
1495 */
1496 if (i == 0) {
1497 char *up;
1498
1499 error = copyin(&addrs[2*j], &up,
1500 sizeof(char*));
1501 if (error)
1502 goto out;
1503 error = copyinstr(up, netid, sizeof(netid),
1504 NULL);
1505 if (error)
1506 goto out;
1507 error = copyin(&addrs[2*j+1], &up,
1508 sizeof(char*));
1509 if (error)
1510 goto out;
1511 error = copyinstr(up, uaddr, sizeof(uaddr),
1512 NULL);
1513 if (error)
1514 goto out;
1515 nconf = getnetconfigent(netid);
1516 if (!nconf) {
1517 NLM_ERR("Can't lookup netid %s\n",
1518 netid);
1519 error = EINVAL;
1520 goto out;
1521 }
1522 xprts[j] = svc_tp_create(pool, dispatchers[i],
1523 NLM_PROG, versions[i], uaddr, nconf);
1524 if (!xprts[j]) {
1525 NLM_ERR("NLM: unable to create "
1526 "(NLM_PROG, %d).\n", versions[i]);
1527 error = EINVAL;
1528 goto out;
1529 }
1530 freenetconfigent(nconf);
1531 } else {
1532 nconf = getnetconfigent(xprts[j]->xp_netid);
1533 rpcb_unset(NLM_PROG, versions[i], nconf);
1534 if (!svc_reg(xprts[j], NLM_PROG, versions[i],
1535 dispatchers[i], nconf)) {
1536 NLM_ERR("NLM: can't register "
1537 "(NLM_PROG, %d)\n", versions[i]);
1538 error = EINVAL;
1539 goto out;
1540 }
1541 }
1542 }
1543 }
1544 error = 0;
1545 out:
1546 for (j = 0; j < addr_count; j++) {
1547 if (xprts[j])
1548 SVC_RELEASE(xprts[j]);
1549 }
1550 free(xprts, M_NLM);
1551 return (error);
1552 }
1553
1554 /*
1555 * Main server entry point. Contacts the local NSM to get its current
1556 * state and send SM_UNMON_ALL. Registers the NLM services and then
1557 * services requests. Does not return until the server is interrupted
1558 * by a signal.
1559 */
1560 static int
nlm_server_main(int addr_count,char ** addrs)1561 nlm_server_main(int addr_count, char **addrs)
1562 {
1563 struct thread *td = curthread;
1564 int error;
1565 SVCPOOL *pool = NULL;
1566 struct sockopt opt;
1567 int portlow;
1568 #ifdef INET6
1569 struct sockaddr_in6 sin6;
1570 #endif
1571 struct sockaddr_in sin;
1572 my_id id;
1573 sm_stat smstat;
1574 struct timeval timo;
1575 enum clnt_stat stat;
1576 struct nlm_host *host, *nhost;
1577 struct nlm_waiting_lock *nw;
1578 vop_advlock_t *old_nfs_advlock;
1579 vop_reclaim_t *old_nfs_reclaim;
1580
1581 if (nlm_is_running != 0) {
1582 NLM_ERR("NLM: can't start server - "
1583 "it appears to be running already\n");
1584 return (EPERM);
1585 }
1586
1587 if (nlm_socket == NULL) {
1588 memset(&opt, 0, sizeof(opt));
1589
1590 error = socreate(AF_INET, &nlm_socket, SOCK_DGRAM, 0,
1591 td->td_ucred, td);
1592 if (error) {
1593 NLM_ERR("NLM: can't create IPv4 socket - error %d\n",
1594 error);
1595 return (error);
1596 }
1597 opt.sopt_dir = SOPT_SET;
1598 opt.sopt_level = IPPROTO_IP;
1599 opt.sopt_name = IP_PORTRANGE;
1600 portlow = IP_PORTRANGE_LOW;
1601 opt.sopt_val = &portlow;
1602 opt.sopt_valsize = sizeof(portlow);
1603 sosetopt(nlm_socket, &opt);
1604
1605 #ifdef INET6
1606 nlm_socket6 = NULL;
1607 error = socreate(AF_INET6, &nlm_socket6, SOCK_DGRAM, 0,
1608 td->td_ucred, td);
1609 if (error) {
1610 NLM_ERR("NLM: can't create IPv6 socket - error %d\n",
1611 error);
1612 soclose(nlm_socket);
1613 nlm_socket = NULL;
1614 return (error);
1615 }
1616 opt.sopt_dir = SOPT_SET;
1617 opt.sopt_level = IPPROTO_IPV6;
1618 opt.sopt_name = IPV6_PORTRANGE;
1619 portlow = IPV6_PORTRANGE_LOW;
1620 opt.sopt_val = &portlow;
1621 opt.sopt_valsize = sizeof(portlow);
1622 sosetopt(nlm_socket6, &opt);
1623 #endif
1624 }
1625
1626 nlm_auth = authunix_create(curthread->td_ucred);
1627
1628 #ifdef INET6
1629 memset(&sin6, 0, sizeof(sin6));
1630 sin6.sin6_len = sizeof(sin6);
1631 sin6.sin6_family = AF_INET6;
1632 sin6.sin6_addr = in6addr_loopback;
1633 nlm_nsm = nlm_get_rpc((struct sockaddr *) &sin6, SM_PROG, SM_VERS);
1634 if (!nlm_nsm) {
1635 #endif
1636 memset(&sin, 0, sizeof(sin));
1637 sin.sin_len = sizeof(sin);
1638 sin.sin_family = AF_INET;
1639 sin.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
1640 nlm_nsm = nlm_get_rpc((struct sockaddr *) &sin, SM_PROG,
1641 SM_VERS);
1642 #ifdef INET6
1643 }
1644 #endif
1645
1646 if (!nlm_nsm) {
1647 NLM_ERR("Can't start NLM - unable to contact NSM\n");
1648 error = EINVAL;
1649 goto out;
1650 }
1651
1652 pool = svcpool_create("NLM", NULL);
1653
1654 error = nlm_register_services(pool, addr_count, addrs);
1655 if (error)
1656 goto out;
1657
1658 memset(&id, 0, sizeof(id));
1659 id.my_name = "NFS NLM";
1660
1661 timo.tv_sec = 25;
1662 timo.tv_usec = 0;
1663 stat = CLNT_CALL(nlm_nsm, SM_UNMON_ALL,
1664 (xdrproc_t) xdr_my_id, &id,
1665 (xdrproc_t) xdr_sm_stat, &smstat, timo);
1666
1667 if (stat != RPC_SUCCESS) {
1668 struct rpc_err err;
1669
1670 CLNT_GETERR(nlm_nsm, &err);
1671 NLM_ERR("NLM: unexpected error contacting NSM, "
1672 "stat=%d, errno=%d\n", stat, err.re_errno);
1673 error = EINVAL;
1674 goto out;
1675 }
1676 nlm_is_running = 1;
1677
1678 NLM_DEBUG(1, "NLM: local NSM state is %d\n", smstat.state);
1679 nlm_nsm_state = smstat.state;
1680
1681 old_nfs_advlock = nfs_advlock_p;
1682 nfs_advlock_p = nlm_advlock;
1683 old_nfs_reclaim = nfs_reclaim_p;
1684 nfs_reclaim_p = nlm_reclaim;
1685
1686 svc_run(pool);
1687 error = 0;
1688
1689 nfs_advlock_p = old_nfs_advlock;
1690 nfs_reclaim_p = old_nfs_reclaim;
1691
1692 out:
1693 nlm_is_running = 0;
1694 if (pool)
1695 svcpool_destroy(pool);
1696
1697 /*
1698 * We are finished communicating with the NSM.
1699 */
1700 if (nlm_nsm) {
1701 CLNT_RELEASE(nlm_nsm);
1702 nlm_nsm = NULL;
1703 }
1704
1705 /*
1706 * Trash all the existing state so that if the server
1707 * restarts, it gets a clean slate. This is complicated by the
1708 * possibility that there may be other threads trying to make
1709 * client locking requests.
1710 *
1711 * First we fake a client reboot notification which will
1712 * cancel any pending async locks and purge remote lock state
1713 * from the local lock manager. We release the reference from
1714 * nlm_hosts to the host (which may remove it from the list
1715 * and free it). After this phase, the only entries in the
1716 * nlm_host list should be from other threads performing
1717 * client lock requests.
1718 */
1719 mtx_lock(&nlm_global_lock);
1720 TAILQ_FOREACH(nw, &nlm_waiting_locks, nw_link) {
1721 wakeup(nw);
1722 }
1723 TAILQ_FOREACH_SAFE(host, &nlm_hosts, nh_link, nhost) {
1724 mtx_unlock(&nlm_global_lock);
1725 nlm_host_notify(host, 0);
1726 nlm_host_release(host);
1727 mtx_lock(&nlm_global_lock);
1728 }
1729 mtx_unlock(&nlm_global_lock);
1730
1731 AUTH_DESTROY(nlm_auth);
1732
1733 return (error);
1734 }
1735
1736 int
sys_nlm_syscall(struct thread * td,struct nlm_syscall_args * uap)1737 sys_nlm_syscall(struct thread *td, struct nlm_syscall_args *uap)
1738 {
1739 int error;
1740
1741 #if __FreeBSD_version >= 700000
1742 error = priv_check(td, PRIV_NFS_LOCKD);
1743 #else
1744 error = suser(td);
1745 #endif
1746 if (error)
1747 return (error);
1748
1749 nlm_debug_level = uap->debug_level;
1750 nlm_grace_threshold = time_uptime + uap->grace_period;
1751 nlm_next_idle_check = time_uptime + NLM_IDLE_PERIOD;
1752
1753 return nlm_server_main(uap->addr_count, uap->addrs);
1754 }
1755
1756 /**********************************************************************/
1757
1758 /*
1759 * NLM implementation details, called from the RPC stubs.
1760 */
1761
1762
1763 void
nlm_sm_notify(struct nlm_sm_status * argp)1764 nlm_sm_notify(struct nlm_sm_status *argp)
1765 {
1766 uint32_t sysid;
1767 struct nlm_host *host;
1768
1769 NLM_DEBUG(3, "nlm_sm_notify(): mon_name = %s\n", argp->mon_name);
1770 memcpy(&sysid, &argp->priv, sizeof(sysid));
1771 host = nlm_find_host_by_sysid(sysid);
1772 if (host) {
1773 nlm_host_notify(host, argp->state);
1774 nlm_host_release(host);
1775 }
1776 }
1777
1778 static void
nlm_convert_to_fhandle_t(fhandle_t * fhp,struct netobj * p)1779 nlm_convert_to_fhandle_t(fhandle_t *fhp, struct netobj *p)
1780 {
1781 memcpy(fhp, p->n_bytes, sizeof(fhandle_t));
1782 }
1783
1784 struct vfs_state {
1785 struct mount *vs_mp;
1786 struct vnode *vs_vp;
1787 int vs_vnlocked;
1788 };
1789
1790 static int
nlm_get_vfs_state(struct nlm_host * host,struct svc_req * rqstp,fhandle_t * fhp,struct vfs_state * vs,accmode_t accmode)1791 nlm_get_vfs_state(struct nlm_host *host, struct svc_req *rqstp,
1792 fhandle_t *fhp, struct vfs_state *vs, accmode_t accmode)
1793 {
1794 int error, exflags;
1795 struct ucred *cred = NULL, *credanon = NULL;
1796
1797 memset(vs, 0, sizeof(*vs));
1798
1799 vs->vs_mp = vfs_getvfs(&fhp->fh_fsid);
1800 if (!vs->vs_mp) {
1801 return (ESTALE);
1802 }
1803
1804 /* accmode == 0 means don't check, since it is an unlock. */
1805 if (accmode != 0) {
1806 error = VFS_CHECKEXP(vs->vs_mp,
1807 (struct sockaddr *)&host->nh_addr, &exflags, &credanon,
1808 NULL, NULL);
1809 if (error)
1810 goto out;
1811
1812 if (exflags & MNT_EXRDONLY ||
1813 (vs->vs_mp->mnt_flag & MNT_RDONLY)) {
1814 error = EROFS;
1815 goto out;
1816 }
1817 }
1818
1819 error = VFS_FHTOVP(vs->vs_mp, &fhp->fh_fid, LK_EXCLUSIVE, &vs->vs_vp);
1820 if (error)
1821 goto out;
1822 vs->vs_vnlocked = TRUE;
1823
1824 if (accmode != 0) {
1825 if (!svc_getcred(rqstp, &cred, NULL)) {
1826 error = EINVAL;
1827 goto out;
1828 }
1829 if (cred->cr_uid == 0 || (exflags & MNT_EXPORTANON)) {
1830 crfree(cred);
1831 cred = credanon;
1832 credanon = NULL;
1833 }
1834
1835 /*
1836 * Check cred.
1837 */
1838 error = VOP_ACCESS(vs->vs_vp, accmode, cred, curthread);
1839 /*
1840 * If this failed and accmode != VWRITE, try again with
1841 * VWRITE to maintain backwards compatibility with the
1842 * old code that always used VWRITE.
1843 */
1844 if (error != 0 && accmode != VWRITE)
1845 error = VOP_ACCESS(vs->vs_vp, VWRITE, cred, curthread);
1846 if (error)
1847 goto out;
1848 }
1849
1850 #if __FreeBSD_version < 800011
1851 VOP_UNLOCK(vs->vs_vp, 0, curthread);
1852 #else
1853 VOP_UNLOCK(vs->vs_vp, 0);
1854 #endif
1855 vs->vs_vnlocked = FALSE;
1856
1857 out:
1858 if (cred)
1859 crfree(cred);
1860 if (credanon)
1861 crfree(credanon);
1862
1863 return (error);
1864 }
1865
1866 static void
nlm_release_vfs_state(struct vfs_state * vs)1867 nlm_release_vfs_state(struct vfs_state *vs)
1868 {
1869
1870 if (vs->vs_vp) {
1871 if (vs->vs_vnlocked)
1872 vput(vs->vs_vp);
1873 else
1874 vrele(vs->vs_vp);
1875 }
1876 if (vs->vs_mp)
1877 vfs_rel(vs->vs_mp);
1878 }
1879
1880 static nlm4_stats
nlm_convert_error(int error)1881 nlm_convert_error(int error)
1882 {
1883
1884 if (error == ESTALE)
1885 return nlm4_stale_fh;
1886 else if (error == EROFS)
1887 return nlm4_rofs;
1888 else
1889 return nlm4_failed;
1890 }
1891
1892 int
nlm_do_test(nlm4_testargs * argp,nlm4_testres * result,struct svc_req * rqstp,CLIENT ** rpcp)1893 nlm_do_test(nlm4_testargs *argp, nlm4_testres *result, struct svc_req *rqstp,
1894 CLIENT **rpcp)
1895 {
1896 fhandle_t fh;
1897 struct vfs_state vs;
1898 struct nlm_host *host, *bhost;
1899 int error, sysid;
1900 struct flock fl;
1901 accmode_t accmode;
1902
1903 memset(result, 0, sizeof(*result));
1904 memset(&vs, 0, sizeof(vs));
1905
1906 host = nlm_find_host_by_name(argp->alock.caller_name,
1907 svc_getrpccaller(rqstp), rqstp->rq_vers);
1908 if (!host) {
1909 result->stat.stat = nlm4_denied_nolocks;
1910 return (ENOMEM);
1911 }
1912
1913 NLM_DEBUG(3, "nlm_do_test(): caller_name = %s (sysid = %d)\n",
1914 host->nh_caller_name, host->nh_sysid);
1915
1916 nlm_check_expired_locks(host);
1917 sysid = host->nh_sysid;
1918
1919 nlm_convert_to_fhandle_t(&fh, &argp->alock.fh);
1920 nlm_copy_netobj(&result->cookie, &argp->cookie, M_RPC);
1921
1922 if (time_uptime < nlm_grace_threshold) {
1923 result->stat.stat = nlm4_denied_grace_period;
1924 goto out;
1925 }
1926
1927 accmode = argp->exclusive ? VWRITE : VREAD;
1928 error = nlm_get_vfs_state(host, rqstp, &fh, &vs, accmode);
1929 if (error) {
1930 result->stat.stat = nlm_convert_error(error);
1931 goto out;
1932 }
1933
1934 fl.l_start = argp->alock.l_offset;
1935 fl.l_len = argp->alock.l_len;
1936 fl.l_pid = argp->alock.svid;
1937 fl.l_sysid = sysid;
1938 fl.l_whence = SEEK_SET;
1939 if (argp->exclusive)
1940 fl.l_type = F_WRLCK;
1941 else
1942 fl.l_type = F_RDLCK;
1943 error = VOP_ADVLOCK(vs.vs_vp, NULL, F_GETLK, &fl, F_REMOTE);
1944 if (error) {
1945 result->stat.stat = nlm4_failed;
1946 goto out;
1947 }
1948
1949 if (fl.l_type == F_UNLCK) {
1950 result->stat.stat = nlm4_granted;
1951 } else {
1952 result->stat.stat = nlm4_denied;
1953 result->stat.nlm4_testrply_u.holder.exclusive =
1954 (fl.l_type == F_WRLCK);
1955 result->stat.nlm4_testrply_u.holder.svid = fl.l_pid;
1956 bhost = nlm_find_host_by_sysid(fl.l_sysid);
1957 if (bhost) {
1958 /*
1959 * We don't have any useful way of recording
1960 * the value of oh used in the original lock
1961 * request. Ideally, the test reply would have
1962 * a space for the owning host's name allowing
1963 * our caller's NLM to keep track.
1964 *
1965 * As far as I can see, Solaris uses an eight
1966 * byte structure for oh which contains a four
1967 * byte pid encoded in local byte order and
1968 * the first four bytes of the host
1969 * name. Linux uses a variable length string
1970 * 'pid@hostname' in ascii but doesn't even
1971 * return that in test replies.
1972 *
1973 * For the moment, return nothing in oh
1974 * (already zero'ed above).
1975 */
1976 nlm_host_release(bhost);
1977 }
1978 result->stat.nlm4_testrply_u.holder.l_offset = fl.l_start;
1979 result->stat.nlm4_testrply_u.holder.l_len = fl.l_len;
1980 }
1981
1982 out:
1983 nlm_release_vfs_state(&vs);
1984 if (rpcp)
1985 *rpcp = nlm_host_get_rpc(host, TRUE);
1986 nlm_host_release(host);
1987 return (0);
1988 }
1989
1990 int
nlm_do_lock(nlm4_lockargs * argp,nlm4_res * result,struct svc_req * rqstp,bool_t monitor,CLIENT ** rpcp)1991 nlm_do_lock(nlm4_lockargs *argp, nlm4_res *result, struct svc_req *rqstp,
1992 bool_t monitor, CLIENT **rpcp)
1993 {
1994 fhandle_t fh;
1995 struct vfs_state vs;
1996 struct nlm_host *host;
1997 int error, sysid;
1998 struct flock fl;
1999 accmode_t accmode;
2000
2001 memset(result, 0, sizeof(*result));
2002 memset(&vs, 0, sizeof(vs));
2003
2004 host = nlm_find_host_by_name(argp->alock.caller_name,
2005 svc_getrpccaller(rqstp), rqstp->rq_vers);
2006 if (!host) {
2007 result->stat.stat = nlm4_denied_nolocks;
2008 return (ENOMEM);
2009 }
2010
2011 NLM_DEBUG(3, "nlm_do_lock(): caller_name = %s (sysid = %d)\n",
2012 host->nh_caller_name, host->nh_sysid);
2013
2014 if (monitor && host->nh_state && argp->state
2015 && host->nh_state != argp->state) {
2016 /*
2017 * The host rebooted without telling us. Trash its
2018 * locks.
2019 */
2020 nlm_host_notify(host, argp->state);
2021 }
2022
2023 nlm_check_expired_locks(host);
2024 sysid = host->nh_sysid;
2025
2026 nlm_convert_to_fhandle_t(&fh, &argp->alock.fh);
2027 nlm_copy_netobj(&result->cookie, &argp->cookie, M_RPC);
2028
2029 if (time_uptime < nlm_grace_threshold && !argp->reclaim) {
2030 result->stat.stat = nlm4_denied_grace_period;
2031 goto out;
2032 }
2033
2034 accmode = argp->exclusive ? VWRITE : VREAD;
2035 error = nlm_get_vfs_state(host, rqstp, &fh, &vs, accmode);
2036 if (error) {
2037 result->stat.stat = nlm_convert_error(error);
2038 goto out;
2039 }
2040
2041 fl.l_start = argp->alock.l_offset;
2042 fl.l_len = argp->alock.l_len;
2043 fl.l_pid = argp->alock.svid;
2044 fl.l_sysid = sysid;
2045 fl.l_whence = SEEK_SET;
2046 if (argp->exclusive)
2047 fl.l_type = F_WRLCK;
2048 else
2049 fl.l_type = F_RDLCK;
2050 if (argp->block) {
2051 struct nlm_async_lock *af;
2052 CLIENT *client;
2053 struct nlm_grantcookie cookie;
2054
2055 /*
2056 * First, make sure we can contact the host's NLM.
2057 */
2058 client = nlm_host_get_rpc(host, TRUE);
2059 if (!client) {
2060 result->stat.stat = nlm4_failed;
2061 goto out;
2062 }
2063
2064 /*
2065 * First we need to check and see if there is an
2066 * existing blocked lock that matches. This could be a
2067 * badly behaved client or an RPC re-send. If we find
2068 * one, just return nlm4_blocked.
2069 */
2070 mtx_lock(&host->nh_lock);
2071 TAILQ_FOREACH(af, &host->nh_pending, af_link) {
2072 if (af->af_fl.l_start == fl.l_start
2073 && af->af_fl.l_len == fl.l_len
2074 && af->af_fl.l_pid == fl.l_pid
2075 && af->af_fl.l_type == fl.l_type) {
2076 break;
2077 }
2078 }
2079 if (!af) {
2080 cookie.ng_sysid = host->nh_sysid;
2081 cookie.ng_cookie = host->nh_grantcookie++;
2082 }
2083 mtx_unlock(&host->nh_lock);
2084 if (af) {
2085 CLNT_RELEASE(client);
2086 result->stat.stat = nlm4_blocked;
2087 goto out;
2088 }
2089
2090 af = malloc(sizeof(struct nlm_async_lock), M_NLM,
2091 M_WAITOK|M_ZERO);
2092 TASK_INIT(&af->af_task, 0, nlm_lock_callback, af);
2093 af->af_vp = vs.vs_vp;
2094 af->af_fl = fl;
2095 af->af_host = host;
2096 af->af_rpc = client;
2097 /*
2098 * We use M_RPC here so that we can xdr_free the thing
2099 * later.
2100 */
2101 nlm_make_netobj(&af->af_granted.cookie,
2102 (caddr_t)&cookie, sizeof(cookie), M_RPC);
2103 af->af_granted.exclusive = argp->exclusive;
2104 af->af_granted.alock.caller_name =
2105 strdup(argp->alock.caller_name, M_RPC);
2106 nlm_copy_netobj(&af->af_granted.alock.fh,
2107 &argp->alock.fh, M_RPC);
2108 nlm_copy_netobj(&af->af_granted.alock.oh,
2109 &argp->alock.oh, M_RPC);
2110 af->af_granted.alock.svid = argp->alock.svid;
2111 af->af_granted.alock.l_offset = argp->alock.l_offset;
2112 af->af_granted.alock.l_len = argp->alock.l_len;
2113
2114 /*
2115 * Put the entry on the pending list before calling
2116 * VOP_ADVLOCKASYNC. We do this in case the lock
2117 * request was blocked (returning EINPROGRESS) but
2118 * then granted before we manage to run again. The
2119 * client may receive the granted message before we
2120 * send our blocked reply but thats their problem.
2121 */
2122 mtx_lock(&host->nh_lock);
2123 TAILQ_INSERT_TAIL(&host->nh_pending, af, af_link);
2124 mtx_unlock(&host->nh_lock);
2125
2126 error = VOP_ADVLOCKASYNC(vs.vs_vp, NULL, F_SETLK, &fl, F_REMOTE,
2127 &af->af_task, &af->af_cookie);
2128
2129 /*
2130 * If the lock completed synchronously, just free the
2131 * tracking structure now.
2132 */
2133 if (error != EINPROGRESS) {
2134 CLNT_RELEASE(af->af_rpc);
2135 mtx_lock(&host->nh_lock);
2136 TAILQ_REMOVE(&host->nh_pending, af, af_link);
2137 mtx_unlock(&host->nh_lock);
2138 xdr_free((xdrproc_t) xdr_nlm4_testargs,
2139 &af->af_granted);
2140 free(af, M_NLM);
2141 } else {
2142 NLM_DEBUG(2, "NLM: pending async lock %p for %s "
2143 "(sysid %d)\n", af, host->nh_caller_name, sysid);
2144 /*
2145 * Don't vrele the vnode just yet - this must
2146 * wait until either the async callback
2147 * happens or the lock is cancelled.
2148 */
2149 vs.vs_vp = NULL;
2150 }
2151 } else {
2152 error = VOP_ADVLOCK(vs.vs_vp, NULL, F_SETLK, &fl, F_REMOTE);
2153 }
2154
2155 if (error) {
2156 if (error == EINPROGRESS) {
2157 result->stat.stat = nlm4_blocked;
2158 } else if (error == EDEADLK) {
2159 result->stat.stat = nlm4_deadlck;
2160 } else if (error == EAGAIN) {
2161 result->stat.stat = nlm4_denied;
2162 } else {
2163 result->stat.stat = nlm4_failed;
2164 }
2165 } else {
2166 if (monitor)
2167 nlm_host_monitor(host, argp->state);
2168 result->stat.stat = nlm4_granted;
2169 }
2170
2171 out:
2172 nlm_release_vfs_state(&vs);
2173 if (rpcp)
2174 *rpcp = nlm_host_get_rpc(host, TRUE);
2175 nlm_host_release(host);
2176 return (0);
2177 }
2178
2179 int
nlm_do_cancel(nlm4_cancargs * argp,nlm4_res * result,struct svc_req * rqstp,CLIENT ** rpcp)2180 nlm_do_cancel(nlm4_cancargs *argp, nlm4_res *result, struct svc_req *rqstp,
2181 CLIENT **rpcp)
2182 {
2183 fhandle_t fh;
2184 struct vfs_state vs;
2185 struct nlm_host *host;
2186 int error, sysid;
2187 struct flock fl;
2188 struct nlm_async_lock *af;
2189
2190 memset(result, 0, sizeof(*result));
2191 memset(&vs, 0, sizeof(vs));
2192
2193 host = nlm_find_host_by_name(argp->alock.caller_name,
2194 svc_getrpccaller(rqstp), rqstp->rq_vers);
2195 if (!host) {
2196 result->stat.stat = nlm4_denied_nolocks;
2197 return (ENOMEM);
2198 }
2199
2200 NLM_DEBUG(3, "nlm_do_cancel(): caller_name = %s (sysid = %d)\n",
2201 host->nh_caller_name, host->nh_sysid);
2202
2203 nlm_check_expired_locks(host);
2204 sysid = host->nh_sysid;
2205
2206 nlm_convert_to_fhandle_t(&fh, &argp->alock.fh);
2207 nlm_copy_netobj(&result->cookie, &argp->cookie, M_RPC);
2208
2209 if (time_uptime < nlm_grace_threshold) {
2210 result->stat.stat = nlm4_denied_grace_period;
2211 goto out;
2212 }
2213
2214 error = nlm_get_vfs_state(host, rqstp, &fh, &vs, (accmode_t)0);
2215 if (error) {
2216 result->stat.stat = nlm_convert_error(error);
2217 goto out;
2218 }
2219
2220 fl.l_start = argp->alock.l_offset;
2221 fl.l_len = argp->alock.l_len;
2222 fl.l_pid = argp->alock.svid;
2223 fl.l_sysid = sysid;
2224 fl.l_whence = SEEK_SET;
2225 if (argp->exclusive)
2226 fl.l_type = F_WRLCK;
2227 else
2228 fl.l_type = F_RDLCK;
2229
2230 /*
2231 * First we need to try and find the async lock request - if
2232 * there isn't one, we give up and return nlm4_denied.
2233 */
2234 mtx_lock(&host->nh_lock);
2235
2236 TAILQ_FOREACH(af, &host->nh_pending, af_link) {
2237 if (af->af_fl.l_start == fl.l_start
2238 && af->af_fl.l_len == fl.l_len
2239 && af->af_fl.l_pid == fl.l_pid
2240 && af->af_fl.l_type == fl.l_type) {
2241 break;
2242 }
2243 }
2244
2245 if (!af) {
2246 mtx_unlock(&host->nh_lock);
2247 result->stat.stat = nlm4_denied;
2248 goto out;
2249 }
2250
2251 error = nlm_cancel_async_lock(af);
2252
2253 if (error) {
2254 result->stat.stat = nlm4_denied;
2255 } else {
2256 result->stat.stat = nlm4_granted;
2257 }
2258
2259 mtx_unlock(&host->nh_lock);
2260
2261 out:
2262 nlm_release_vfs_state(&vs);
2263 if (rpcp)
2264 *rpcp = nlm_host_get_rpc(host, TRUE);
2265 nlm_host_release(host);
2266 return (0);
2267 }
2268
2269 int
nlm_do_unlock(nlm4_unlockargs * argp,nlm4_res * result,struct svc_req * rqstp,CLIENT ** rpcp)2270 nlm_do_unlock(nlm4_unlockargs *argp, nlm4_res *result, struct svc_req *rqstp,
2271 CLIENT **rpcp)
2272 {
2273 fhandle_t fh;
2274 struct vfs_state vs;
2275 struct nlm_host *host;
2276 int error, sysid;
2277 struct flock fl;
2278
2279 memset(result, 0, sizeof(*result));
2280 memset(&vs, 0, sizeof(vs));
2281
2282 host = nlm_find_host_by_name(argp->alock.caller_name,
2283 svc_getrpccaller(rqstp), rqstp->rq_vers);
2284 if (!host) {
2285 result->stat.stat = nlm4_denied_nolocks;
2286 return (ENOMEM);
2287 }
2288
2289 NLM_DEBUG(3, "nlm_do_unlock(): caller_name = %s (sysid = %d)\n",
2290 host->nh_caller_name, host->nh_sysid);
2291
2292 nlm_check_expired_locks(host);
2293 sysid = host->nh_sysid;
2294
2295 nlm_convert_to_fhandle_t(&fh, &argp->alock.fh);
2296 nlm_copy_netobj(&result->cookie, &argp->cookie, M_RPC);
2297
2298 if (time_uptime < nlm_grace_threshold) {
2299 result->stat.stat = nlm4_denied_grace_period;
2300 goto out;
2301 }
2302
2303 error = nlm_get_vfs_state(host, rqstp, &fh, &vs, (accmode_t)0);
2304 if (error) {
2305 result->stat.stat = nlm_convert_error(error);
2306 goto out;
2307 }
2308
2309 fl.l_start = argp->alock.l_offset;
2310 fl.l_len = argp->alock.l_len;
2311 fl.l_pid = argp->alock.svid;
2312 fl.l_sysid = sysid;
2313 fl.l_whence = SEEK_SET;
2314 fl.l_type = F_UNLCK;
2315 error = VOP_ADVLOCK(vs.vs_vp, NULL, F_UNLCK, &fl, F_REMOTE);
2316
2317 /*
2318 * Ignore the error - there is no result code for failure,
2319 * only for grace period.
2320 */
2321 result->stat.stat = nlm4_granted;
2322
2323 out:
2324 nlm_release_vfs_state(&vs);
2325 if (rpcp)
2326 *rpcp = nlm_host_get_rpc(host, TRUE);
2327 nlm_host_release(host);
2328 return (0);
2329 }
2330
2331 int
nlm_do_granted(nlm4_testargs * argp,nlm4_res * result,struct svc_req * rqstp,CLIENT ** rpcp)2332 nlm_do_granted(nlm4_testargs *argp, nlm4_res *result, struct svc_req *rqstp,
2333
2334 CLIENT **rpcp)
2335 {
2336 struct nlm_host *host;
2337 struct nlm_waiting_lock *nw;
2338
2339 memset(result, 0, sizeof(*result));
2340
2341 host = nlm_find_host_by_addr(svc_getrpccaller(rqstp), rqstp->rq_vers);
2342 if (!host) {
2343 result->stat.stat = nlm4_denied_nolocks;
2344 return (ENOMEM);
2345 }
2346
2347 nlm_copy_netobj(&result->cookie, &argp->cookie, M_RPC);
2348 result->stat.stat = nlm4_denied;
2349 KFAIL_POINT_CODE(DEBUG_FP, nlm_deny_grant, goto out);
2350
2351 mtx_lock(&nlm_global_lock);
2352 TAILQ_FOREACH(nw, &nlm_waiting_locks, nw_link) {
2353 if (!nw->nw_waiting)
2354 continue;
2355 if (argp->alock.svid == nw->nw_lock.svid
2356 && argp->alock.l_offset == nw->nw_lock.l_offset
2357 && argp->alock.l_len == nw->nw_lock.l_len
2358 && argp->alock.fh.n_len == nw->nw_lock.fh.n_len
2359 && !memcmp(argp->alock.fh.n_bytes, nw->nw_lock.fh.n_bytes,
2360 nw->nw_lock.fh.n_len)) {
2361 nw->nw_waiting = FALSE;
2362 wakeup(nw);
2363 result->stat.stat = nlm4_granted;
2364 break;
2365 }
2366 }
2367 mtx_unlock(&nlm_global_lock);
2368
2369 out:
2370 if (rpcp)
2371 *rpcp = nlm_host_get_rpc(host, TRUE);
2372 nlm_host_release(host);
2373 return (0);
2374 }
2375
2376 void
nlm_do_granted_res(nlm4_res * argp,struct svc_req * rqstp)2377 nlm_do_granted_res(nlm4_res *argp, struct svc_req *rqstp)
2378 {
2379 struct nlm_host *host = NULL;
2380 struct nlm_async_lock *af = NULL;
2381 int error;
2382
2383 if (argp->cookie.n_len != sizeof(struct nlm_grantcookie)) {
2384 NLM_DEBUG(1, "NLM: bogus grant cookie");
2385 goto out;
2386 }
2387
2388 host = nlm_find_host_by_sysid(ng_sysid(&argp->cookie));
2389 if (!host) {
2390 NLM_DEBUG(1, "NLM: Unknown host rejected our grant");
2391 goto out;
2392 }
2393
2394 mtx_lock(&host->nh_lock);
2395 TAILQ_FOREACH(af, &host->nh_granted, af_link)
2396 if (ng_cookie(&argp->cookie) ==
2397 ng_cookie(&af->af_granted.cookie))
2398 break;
2399 if (af)
2400 TAILQ_REMOVE(&host->nh_granted, af, af_link);
2401 mtx_unlock(&host->nh_lock);
2402
2403 if (!af) {
2404 NLM_DEBUG(1, "NLM: host %s (sysid %d) replied to our grant "
2405 "with unrecognized cookie %d:%d", host->nh_caller_name,
2406 host->nh_sysid, ng_sysid(&argp->cookie),
2407 ng_cookie(&argp->cookie));
2408 goto out;
2409 }
2410
2411 if (argp->stat.stat != nlm4_granted) {
2412 af->af_fl.l_type = F_UNLCK;
2413 error = VOP_ADVLOCK(af->af_vp, NULL, F_UNLCK, &af->af_fl, F_REMOTE);
2414 if (error) {
2415 NLM_DEBUG(1, "NLM: host %s (sysid %d) rejected our grant "
2416 "and we failed to unlock (%d)", host->nh_caller_name,
2417 host->nh_sysid, error);
2418 goto out;
2419 }
2420
2421 NLM_DEBUG(5, "NLM: async lock %p rejected by host %s (sysid %d)",
2422 af, host->nh_caller_name, host->nh_sysid);
2423 } else {
2424 NLM_DEBUG(5, "NLM: async lock %p accepted by host %s (sysid %d)",
2425 af, host->nh_caller_name, host->nh_sysid);
2426 }
2427
2428 out:
2429 if (af)
2430 nlm_free_async_lock(af);
2431 if (host)
2432 nlm_host_release(host);
2433 }
2434
2435 void
nlm_do_free_all(nlm4_notify * argp)2436 nlm_do_free_all(nlm4_notify *argp)
2437 {
2438 struct nlm_host *host, *thost;
2439
2440 TAILQ_FOREACH_SAFE(host, &nlm_hosts, nh_link, thost) {
2441 if (!strcmp(host->nh_caller_name, argp->name))
2442 nlm_host_notify(host, argp->state);
2443 }
2444 }
2445
2446 /*
2447 * Kernel module glue
2448 */
2449 static int
nfslockd_modevent(module_t mod,int type,void * data)2450 nfslockd_modevent(module_t mod, int type, void *data)
2451 {
2452
2453 switch (type) {
2454 case MOD_LOAD:
2455 return (0);
2456 case MOD_UNLOAD:
2457 /* The NLM module cannot be safely unloaded. */
2458 /* FALLTHROUGH */
2459 default:
2460 return (EOPNOTSUPP);
2461 }
2462 }
2463 static moduledata_t nfslockd_mod = {
2464 "nfslockd",
2465 nfslockd_modevent,
2466 NULL,
2467 };
2468 DECLARE_MODULE(nfslockd, nfslockd_mod, SI_SUB_VFS, SI_ORDER_ANY);
2469
2470 /* So that loader and kldload(2) can find us, wherever we are.. */
2471 MODULE_DEPEND(nfslockd, krpc, 1, 1, 1);
2472 MODULE_DEPEND(nfslockd, nfslock, 1, 1, 1);
2473 MODULE_VERSION(nfslockd, 1);
2474