xref: /freebsd/sys/nlm/nlm_advlock.c (revision bdd1243d)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 2008 Isilon Inc http://www.isilon.com/
5  * Authors: Doug Rabson <dfr@rabson.org>
6  * Developed with Red Inc: Alfred Perlstein <alfred@freebsd.org>
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  */
29 
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
32 
33 #include <sys/param.h>
34 #include <sys/fcntl.h>
35 #include <sys/jail.h>
36 #include <sys/kernel.h>
37 #include <sys/limits.h>
38 #include <sys/lock.h>
39 #include <sys/lockf.h>
40 #include <sys/malloc.h>
41 #include <sys/mbuf.h>
42 #include <sys/mount.h>
43 #include <sys/mutex.h>
44 #include <sys/proc.h>
45 #include <sys/socket.h>
46 #include <sys/syslog.h>
47 #include <sys/systm.h>
48 #include <sys/unistd.h>
49 #include <sys/vnode.h>
50 
51 #include <nfs/nfsproto.h>
52 #include <nfsclient/nfs.h>
53 #include <nfsclient/nfsmount.h>
54 
55 #include <nlm/nlm_prot.h>
56 #include <nlm/nlm.h>
57 
58 /*
59  * We need to keep track of the svid values used for F_FLOCK locks.
60  */
61 struct nlm_file_svid {
62 	int		ns_refs;	/* thread count + 1 if active */
63 	int		ns_svid;	/* on-the-wire SVID for this file */
64 	struct ucred	*ns_ucred;	/* creds to use for lock recovery */
65 	void		*ns_id;		/* local struct file pointer */
66 	bool_t		ns_active;	/* TRUE if we own a lock */
67 	LIST_ENTRY(nlm_file_svid) ns_link;
68 };
69 LIST_HEAD(nlm_file_svid_list, nlm_file_svid);
70 
71 #define NLM_SVID_HASH_SIZE	256
72 struct nlm_file_svid_list nlm_file_svids[NLM_SVID_HASH_SIZE];
73 
74 struct mtx nlm_svid_lock;
75 static struct unrhdr *nlm_svid_allocator;
76 static volatile u_int nlm_xid = 1;
77 
78 static int nlm_setlock(struct nlm_host *host, struct rpc_callextra *ext,
79     rpcvers_t vers, struct timeval *timo, int retries,
80     struct vnode *vp, int op, struct flock *fl, int flags,
81     int svid, size_t fhlen, void *fh, off_t size, bool_t reclaim);
82 static int nlm_clearlock(struct nlm_host *host,  struct rpc_callextra *ext,
83     rpcvers_t vers, struct timeval *timo, int retries,
84     struct vnode *vp, int op, struct flock *fl, int flags,
85     int svid, size_t fhlen, void *fh, off_t size);
86 static int nlm_getlock(struct nlm_host *host, struct rpc_callextra *ext,
87     rpcvers_t vers, struct timeval *timo, int retries,
88     struct vnode *vp, int op, struct flock *fl, int flags,
89     int svid, size_t fhlen, void *fh, off_t size);
90 static int nlm_map_status(nlm4_stats stat);
91 static struct nlm_file_svid *nlm_find_svid(void *id);
92 static void nlm_free_svid(struct nlm_file_svid *nf);
93 static int nlm_init_lock(struct flock *fl, int flags, int svid,
94     rpcvers_t vers, size_t fhlen, void *fh, off_t size,
95     struct nlm4_lock *lock, char oh_space[32]);
96 
97 static void
98 nlm_client_init(void *dummy)
99 {
100 	int i;
101 
102 	mtx_init(&nlm_svid_lock, "NLM svid lock", NULL, MTX_DEF);
103 	/* pid_max cannot be greater than PID_MAX */
104 	nlm_svid_allocator = new_unrhdr(PID_MAX + 2, INT_MAX, &nlm_svid_lock);
105 	for (i = 0; i < NLM_SVID_HASH_SIZE; i++)
106 		LIST_INIT(&nlm_file_svids[i]);
107 }
108 SYSINIT(nlm_client_init, SI_SUB_LOCK, SI_ORDER_FIRST, nlm_client_init, NULL);
109 
110 static int
111 nlm_msg(struct thread *td, const char *server, const char *msg, int error)
112 {
113 	struct proc *p;
114 
115 	p = td ? td->td_proc : NULL;
116 	if (error) {
117 		tprintf(p, LOG_INFO, "nfs server %s: %s, error %d\n", server,
118 		    msg, error);
119 	} else {
120 		tprintf(p, LOG_INFO, "nfs server %s: %s\n", server, msg);
121 	}
122 	return (0);
123 }
124 
125 struct nlm_feedback_arg {
126 	bool_t	nf_printed;
127 	struct nfsmount *nf_nmp;
128 };
129 
130 static void
131 nlm_down(struct nlm_feedback_arg *nf, struct thread *td,
132     const char *msg, int error)
133 {
134 	struct nfsmount *nmp = nf->nf_nmp;
135 
136 	if (nmp == NULL)
137 		return;
138 	mtx_lock(&nmp->nm_mtx);
139 	if (!(nmp->nm_state & NFSSTA_LOCKTIMEO)) {
140 		nmp->nm_state |= NFSSTA_LOCKTIMEO;
141 		mtx_unlock(&nmp->nm_mtx);
142 		vfs_event_signal(&nmp->nm_mountp->mnt_stat.f_fsid,
143 		    VQ_NOTRESPLOCK, 0);
144 	} else {
145 		mtx_unlock(&nmp->nm_mtx);
146 	}
147 
148 	nf->nf_printed = TRUE;
149 	nlm_msg(td, nmp->nm_mountp->mnt_stat.f_mntfromname, msg, error);
150 }
151 
152 static void
153 nlm_up(struct nlm_feedback_arg *nf, struct thread *td,
154     const char *msg)
155 {
156 	struct nfsmount *nmp = nf->nf_nmp;
157 
158 	if (!nf->nf_printed)
159 		return;
160 
161 	nlm_msg(td, nmp->nm_mountp->mnt_stat.f_mntfromname, msg, 0);
162 
163 	mtx_lock(&nmp->nm_mtx);
164 	if (nmp->nm_state & NFSSTA_LOCKTIMEO) {
165 		nmp->nm_state &= ~NFSSTA_LOCKTIMEO;
166 		mtx_unlock(&nmp->nm_mtx);
167 		vfs_event_signal(&nmp->nm_mountp->mnt_stat.f_fsid,
168 		    VQ_NOTRESPLOCK, 1);
169 	} else {
170 		mtx_unlock(&nmp->nm_mtx);
171 	}
172 }
173 
174 static void
175 nlm_feedback(int type, int proc, void *arg)
176 {
177 	struct thread *td = curthread;
178 	struct nlm_feedback_arg *nf = (struct nlm_feedback_arg *) arg;
179 
180 	switch (type) {
181 	case FEEDBACK_REXMIT2:
182 	case FEEDBACK_RECONNECT:
183 		nlm_down(nf, td, "lockd not responding", 0);
184 		break;
185 
186 	case FEEDBACK_OK:
187 		nlm_up(nf, td, "lockd is alive again");
188 		break;
189 	}
190 }
191 
192 /*
193  * nlm_advlock --
194  *      NFS advisory byte-level locks.
195  */
196 static int
197 nlm_advlock_internal(struct vnode *vp, void *id, int op, struct flock *fl,
198     int flags, bool_t reclaim, bool_t unlock_vp)
199 {
200 	struct thread *td = curthread;
201 	struct nfsmount *nmp;
202 	off_t size;
203 	size_t fhlen;
204 	union nfsfh fh;
205 	struct sockaddr *sa;
206 	struct sockaddr_storage ss;
207 	char *servername;
208 	struct timeval timo;
209 	int retries;
210 	rpcvers_t vers;
211 	struct nlm_host *host;
212 	struct rpc_callextra ext;
213 	struct nlm_feedback_arg nf;
214 	AUTH *auth;
215 	struct ucred *cred, *cred1;
216 	struct nlm_file_svid *ns;
217 	int svid;
218 	int error;
219 	int is_v3;
220 
221 	ASSERT_VOP_LOCKED(vp, "nlm_advlock_1");
222 
223 	servername = malloc(MNAMELEN, M_TEMP, M_WAITOK); /* XXXKIB vp locked */
224 	nmp = VFSTONFS(vp->v_mount);
225 	/*
226 	 * Push any pending writes to the server and flush our cache
227 	 * so that if we are contending with another machine for a
228 	 * file, we get whatever they wrote and vice-versa.
229 	 */
230 	if (op == F_SETLK || op == F_UNLCK)
231 		nmp->nm_vinvalbuf(vp, V_SAVE, td, 1);
232 
233 	strcpy(servername, nmp->nm_hostname);
234 	nmp->nm_getinfo(vp, fh.fh_bytes, &fhlen, &ss, &is_v3, &size, &timo);
235 	sa = (struct sockaddr *) &ss;
236 	if (is_v3 != 0)
237 		vers = NLM_VERS4;
238 	else
239 		vers = NLM_VERS;
240 
241 	if (nmp->nm_flag & NFSMNT_SOFT)
242 		retries = nmp->nm_retry;
243 	else
244 		retries = INT_MAX;
245 
246 	/*
247 	 * We need to switch to mount-point creds so that we can send
248 	 * packets from a privileged port.  Reference mnt_cred and
249 	 * switch to them before unlocking the vnode, since mount
250 	 * point could be unmounted right after unlock.
251 	 */
252 	cred = td->td_ucred;
253 	td->td_ucred = vp->v_mount->mnt_cred;
254 	crhold(td->td_ucred);
255 	if (unlock_vp)
256 		VOP_UNLOCK(vp);
257 
258 	host = nlm_find_host_by_name(servername, sa, vers);
259 	auth = authunix_create(cred);
260 	memset(&ext, 0, sizeof(ext));
261 
262 	nf.nf_printed = FALSE;
263 	nf.nf_nmp = nmp;
264 	ext.rc_auth = auth;
265 
266 	ext.rc_feedback = nlm_feedback;
267 	ext.rc_feedback_arg = &nf;
268 	ext.rc_timers = NULL;
269 
270 	ns = NULL;
271 	if (flags & F_FLOCK) {
272 		ns = nlm_find_svid(id);
273 		KASSERT(fl->l_start == 0 && fl->l_len == 0,
274 		    ("F_FLOCK lock requests must be whole-file locks"));
275 		if (!ns->ns_ucred) {
276 			/*
277 			 * Remember the creds used for locking in case
278 			 * we need to recover the lock later.
279 			 */
280 			ns->ns_ucred = crdup(cred);
281 		}
282 		svid = ns->ns_svid;
283 	} else if (flags & F_REMOTE) {
284 		/*
285 		 * If we are recovering after a server restart or
286 		 * trashing locks on a force unmount, use the same
287 		 * svid as last time.
288 		 */
289 		svid = fl->l_pid;
290 	} else {
291 		svid = ((struct proc *) id)->p_pid;
292 	}
293 
294 	switch(op) {
295 	case F_SETLK:
296 		if ((flags & (F_FLOCK|F_WAIT)) == (F_FLOCK|F_WAIT)
297 		    && fl->l_type == F_WRLCK) {
298 			/*
299 			 * The semantics for flock(2) require that any
300 			 * shared lock on the file must be released
301 			 * before an exclusive lock is granted. The
302 			 * local locking code interprets this by
303 			 * unlocking the file before sleeping on a
304 			 * blocked exclusive lock request. We
305 			 * approximate this by first attempting
306 			 * non-blocking and if that fails, we unlock
307 			 * the file and block.
308 			 */
309 			error = nlm_setlock(host, &ext, vers, &timo, retries,
310 			    vp, F_SETLK, fl, flags & ~F_WAIT,
311 			    svid, fhlen, &fh.fh_bytes, size, reclaim);
312 			if (error == EAGAIN) {
313 				fl->l_type = F_UNLCK;
314 				error = nlm_clearlock(host, &ext, vers, &timo,
315 				    retries, vp, F_UNLCK, fl, flags,
316 				    svid, fhlen, &fh.fh_bytes, size);
317 				fl->l_type = F_WRLCK;
318 				if (!error) {
319 					mtx_lock(&nlm_svid_lock);
320 					if (ns->ns_active) {
321 						ns->ns_refs--;
322 						ns->ns_active = FALSE;
323 					}
324 					mtx_unlock(&nlm_svid_lock);
325 					flags |= F_WAIT;
326 					error = nlm_setlock(host, &ext, vers,
327 					    &timo, retries, vp, F_SETLK, fl,
328 					    flags, svid, fhlen, &fh.fh_bytes,
329 					    size, reclaim);
330 				}
331 			}
332 		} else {
333 			error = nlm_setlock(host, &ext, vers, &timo, retries,
334 			    vp, op, fl, flags, svid, fhlen, &fh.fh_bytes,
335 			    size, reclaim);
336 		}
337 		if (!error && ns) {
338 			mtx_lock(&nlm_svid_lock);
339 			if (!ns->ns_active) {
340 				/*
341 				 * Add one to the reference count to
342 				 * hold onto the SVID for the lifetime
343 				 * of the lock. Note that since
344 				 * F_FLOCK only supports whole-file
345 				 * locks, there can only be one active
346 				 * lock for this SVID.
347 				 */
348 				ns->ns_refs++;
349 				ns->ns_active = TRUE;
350 			}
351 			mtx_unlock(&nlm_svid_lock);
352 		}
353 		break;
354 
355 	case F_UNLCK:
356 		error = nlm_clearlock(host, &ext, vers, &timo, retries,
357 		    vp, op, fl, flags, svid, fhlen, &fh.fh_bytes, size);
358 		if (!error && ns) {
359 			mtx_lock(&nlm_svid_lock);
360 			if (ns->ns_active) {
361 				ns->ns_refs--;
362 				ns->ns_active = FALSE;
363 			}
364 			mtx_unlock(&nlm_svid_lock);
365 		}
366 		break;
367 
368 	case F_GETLK:
369 		error = nlm_getlock(host, &ext, vers, &timo, retries,
370 		    vp, op, fl, flags, svid, fhlen, &fh.fh_bytes, size);
371 		break;
372 
373 	default:
374 		error = EINVAL;
375 		break;
376 	}
377 
378 	if (ns)
379 		nlm_free_svid(ns);
380 
381 	cred1 = td->td_ucred;
382 	td->td_ucred = cred;
383 	crfree(cred1);
384 	AUTH_DESTROY(auth);
385 
386 	nlm_host_release(host);
387 	free(servername, M_TEMP);
388 	return (error);
389 }
390 
391 int
392 nlm_advlock(struct vop_advlock_args *ap)
393 {
394 
395 	return (nlm_advlock_internal(ap->a_vp, ap->a_id, ap->a_op, ap->a_fl,
396 		ap->a_flags, FALSE, TRUE));
397 }
398 
399 /*
400  * Set the creds of td to the creds of the given lock's owner. The new
401  * creds reference count will be incremented via crhold. The caller is
402  * responsible for calling crfree and restoring td's original creds.
403  */
404 static void
405 nlm_set_creds_for_lock(struct thread *td, struct flock *fl)
406 {
407 	int i;
408 	struct nlm_file_svid *ns;
409 	struct proc *p;
410 	struct ucred *cred;
411 
412 	cred = NULL;
413 	if (fl->l_pid > PID_MAX) {
414 		/*
415 		 * If this was originally a F_FLOCK-style lock, we
416 		 * recorded the creds used when it was originally
417 		 * locked in the nlm_file_svid structure.
418 		 */
419 		mtx_lock(&nlm_svid_lock);
420 		for (i = 0; i < NLM_SVID_HASH_SIZE; i++) {
421 			for (ns = LIST_FIRST(&nlm_file_svids[i]); ns;
422 			     ns = LIST_NEXT(ns, ns_link)) {
423 				if (ns->ns_svid == fl->l_pid) {
424 					cred = crhold(ns->ns_ucred);
425 					break;
426 				}
427 			}
428 		}
429 		mtx_unlock(&nlm_svid_lock);
430 	} else {
431 		/*
432 		 * This lock is owned by a process. Get a reference to
433 		 * the process creds.
434 		 */
435 		p = pfind(fl->l_pid);
436 		if (p) {
437 			cred = crhold(p->p_ucred);
438 			PROC_UNLOCK(p);
439 		}
440 	}
441 
442 	/*
443 	 * If we can't find a cred, fall back on the recovery
444 	 * thread's cred.
445 	 */
446 	if (!cred) {
447 		cred = crhold(td->td_ucred);
448 	}
449 
450 	td->td_ucred = cred;
451 }
452 
453 static int
454 nlm_reclaim_free_lock(struct vnode *vp, struct flock *fl, void *arg)
455 {
456 	struct flock newfl;
457 	struct thread *td = curthread;
458 	struct ucred *oldcred;
459 	int error;
460 
461 	newfl = *fl;
462 	newfl.l_type = F_UNLCK;
463 
464 	oldcred = td->td_ucred;
465 	nlm_set_creds_for_lock(td, &newfl);
466 
467 	error = nlm_advlock_internal(vp, NULL, F_UNLCK, &newfl, F_REMOTE,
468 	    FALSE, FALSE);
469 
470 	crfree(td->td_ucred);
471 	td->td_ucred = oldcred;
472 
473 	return (error);
474 }
475 
476 int
477 nlm_reclaim(struct vop_reclaim_args *ap)
478 {
479 
480 	nlm_cancel_wait(ap->a_vp);
481 	lf_iteratelocks_vnode(ap->a_vp, nlm_reclaim_free_lock, NULL);
482 	return (0);
483 }
484 
485 struct nlm_recovery_context {
486 	struct nlm_host	*nr_host;	/* host we are recovering */
487 	int		nr_state;	/* remote NSM state for recovery */
488 };
489 
490 static int
491 nlm_client_recover_lock(struct vnode *vp, struct flock *fl, void *arg)
492 {
493 	struct nlm_recovery_context *nr = (struct nlm_recovery_context *) arg;
494 	struct thread *td = curthread;
495 	struct ucred *oldcred;
496 	int state, error;
497 
498 	/*
499 	 * If the remote NSM state changes during recovery, the host
500 	 * must have rebooted a second time. In that case, we must
501 	 * restart the recovery.
502 	 */
503 	state = nlm_host_get_state(nr->nr_host);
504 	if (nr->nr_state != state)
505 		return (ERESTART);
506 
507 	error = vn_lock(vp, LK_SHARED);
508 	if (error)
509 		return (error);
510 
511 	oldcred = td->td_ucred;
512 	nlm_set_creds_for_lock(td, fl);
513 
514 	error = nlm_advlock_internal(vp, NULL, F_SETLK, fl, F_REMOTE,
515 	    TRUE, TRUE);
516 
517 	crfree(td->td_ucred);
518 	td->td_ucred = oldcred;
519 
520 	return (error);
521 }
522 
523 void
524 nlm_client_recovery(struct nlm_host *host)
525 {
526 	struct nlm_recovery_context nr;
527 	int sysid, error;
528 
529 	sysid = NLM_SYSID_CLIENT | nlm_host_get_sysid(host);
530 	do {
531 		nr.nr_host = host;
532 		nr.nr_state = nlm_host_get_state(host);
533 		error = lf_iteratelocks_sysid(sysid,
534 		    nlm_client_recover_lock, &nr);
535 	} while (error == ERESTART);
536 }
537 
538 static void
539 nlm_convert_to_nlm_lock(struct nlm_lock *dst, struct nlm4_lock *src)
540 {
541 
542 	dst->caller_name = src->caller_name;
543 	dst->fh = src->fh;
544 	dst->oh = src->oh;
545 	dst->svid = src->svid;
546 	dst->l_offset = src->l_offset;
547 	dst->l_len = src->l_len;
548 }
549 
550 static void
551 nlm_convert_to_nlm4_holder(struct nlm4_holder *dst, struct nlm_holder *src)
552 {
553 
554 	dst->exclusive = src->exclusive;
555 	dst->svid = src->svid;
556 	dst->oh = src->oh;
557 	dst->l_offset = src->l_offset;
558 	dst->l_len = src->l_len;
559 }
560 
561 static void
562 nlm_convert_to_nlm4_res(struct nlm4_res *dst, struct nlm_res *src)
563 {
564 	dst->cookie = src->cookie;
565 	dst->stat.stat = (enum nlm4_stats) src->stat.stat;
566 }
567 
568 static enum clnt_stat
569 nlm_test_rpc(rpcvers_t vers, nlm4_testargs *args, nlm4_testres *res, CLIENT *client,
570     struct rpc_callextra *ext, struct timeval timo)
571 {
572 	if (vers == NLM_VERS4) {
573 		return nlm4_test_4(args, res, client, ext, timo);
574 	} else {
575 		nlm_testargs args1;
576 		nlm_testres res1;
577 		enum clnt_stat stat;
578 
579 		args1.cookie = args->cookie;
580 		args1.exclusive = args->exclusive;
581 		nlm_convert_to_nlm_lock(&args1.alock, &args->alock);
582 		memset(&res1, 0, sizeof(res1));
583 
584 		stat = nlm_test_1(&args1, &res1, client, ext, timo);
585 
586 		if (stat == RPC_SUCCESS) {
587 			res->cookie = res1.cookie;
588 			res->stat.stat = (enum nlm4_stats) res1.stat.stat;
589 			if (res1.stat.stat == nlm_denied)
590 				nlm_convert_to_nlm4_holder(
591 					&res->stat.nlm4_testrply_u.holder,
592 					&res1.stat.nlm_testrply_u.holder);
593 		}
594 
595 		return (stat);
596 	}
597 }
598 
599 static enum clnt_stat
600 nlm_lock_rpc(rpcvers_t vers, nlm4_lockargs *args, nlm4_res *res, CLIENT *client,
601     struct rpc_callextra *ext, struct timeval timo)
602 {
603 	if (vers == NLM_VERS4) {
604 		return nlm4_lock_4(args, res, client, ext, timo);
605 	} else {
606 		nlm_lockargs args1;
607 		nlm_res res1;
608 		enum clnt_stat stat;
609 
610 		args1.cookie = args->cookie;
611 		args1.block = args->block;
612 		args1.exclusive = args->exclusive;
613 		nlm_convert_to_nlm_lock(&args1.alock, &args->alock);
614 		args1.reclaim = args->reclaim;
615 		args1.state = args->state;
616 		memset(&res1, 0, sizeof(res1));
617 
618 		stat = nlm_lock_1(&args1, &res1, client, ext, timo);
619 
620 		if (stat == RPC_SUCCESS) {
621 			nlm_convert_to_nlm4_res(res, &res1);
622 		}
623 
624 		return (stat);
625 	}
626 }
627 
628 static enum clnt_stat
629 nlm_cancel_rpc(rpcvers_t vers, nlm4_cancargs *args, nlm4_res *res, CLIENT *client,
630     struct rpc_callextra *ext, struct timeval timo)
631 {
632 	if (vers == NLM_VERS4) {
633 		return nlm4_cancel_4(args, res, client, ext, timo);
634 	} else {
635 		nlm_cancargs args1;
636 		nlm_res res1;
637 		enum clnt_stat stat;
638 
639 		args1.cookie = args->cookie;
640 		args1.block = args->block;
641 		args1.exclusive = args->exclusive;
642 		nlm_convert_to_nlm_lock(&args1.alock, &args->alock);
643 		memset(&res1, 0, sizeof(res1));
644 
645 		stat = nlm_cancel_1(&args1, &res1, client, ext, timo);
646 
647 		if (stat == RPC_SUCCESS) {
648 			nlm_convert_to_nlm4_res(res, &res1);
649 		}
650 
651 		return (stat);
652 	}
653 }
654 
655 static enum clnt_stat
656 nlm_unlock_rpc(rpcvers_t vers, nlm4_unlockargs *args, nlm4_res *res, CLIENT *client,
657     struct rpc_callextra *ext, struct timeval timo)
658 {
659 	if (vers == NLM_VERS4) {
660 		return nlm4_unlock_4(args, res, client, ext, timo);
661 	} else {
662 		nlm_unlockargs args1;
663 		nlm_res res1;
664 		enum clnt_stat stat;
665 
666 		args1.cookie = args->cookie;
667 		nlm_convert_to_nlm_lock(&args1.alock, &args->alock);
668 		memset(&res1, 0, sizeof(res1));
669 
670 		stat = nlm_unlock_1(&args1, &res1, client, ext, timo);
671 
672 		if (stat == RPC_SUCCESS) {
673 			nlm_convert_to_nlm4_res(res, &res1);
674 		}
675 
676 		return (stat);
677 	}
678 }
679 
680 /*
681  * Called after a lock request (set or clear) succeeded. We record the
682  * details in the local lock manager. Note that since the remote
683  * server has granted the lock, we can be sure that it doesn't
684  * conflict with any other locks we have in the local lock manager.
685  *
686  * Since it is possible that host may also make NLM client requests to
687  * our NLM server, we use a different sysid value to record our own
688  * client locks.
689  *
690  * Note that since it is possible for us to receive replies from the
691  * server in a different order than the locks were granted (e.g. if
692  * many local threads are contending for the same lock), we must use a
693  * blocking operation when registering with the local lock manager.
694  * We expect that any actual wait will be rare and short hence we
695  * ignore signals for this.
696  */
697 static void
698 nlm_record_lock(struct vnode *vp, int op, struct flock *fl,
699     int svid, int sysid, off_t size)
700 {
701 	struct vop_advlockasync_args a;
702 	struct flock newfl;
703 	struct proc *p;
704 	int error, stops_deferred;
705 
706 	a.a_vp = vp;
707 	a.a_id = NULL;
708 	a.a_op = op;
709 	a.a_fl = &newfl;
710 	a.a_flags = F_REMOTE|F_WAIT|F_NOINTR;
711 	a.a_task = NULL;
712 	a.a_cookiep = NULL;
713 	newfl.l_start = fl->l_start;
714 	newfl.l_len = fl->l_len;
715 	newfl.l_type = fl->l_type;
716 	newfl.l_whence = fl->l_whence;
717 	newfl.l_pid = svid;
718 	newfl.l_sysid = NLM_SYSID_CLIENT | sysid;
719 
720 	for (;;) {
721 		error = lf_advlockasync(&a, &vp->v_lockf, size);
722 		if (error == EDEADLK) {
723 			/*
724 			 * Locks are associated with the processes and
725 			 * not with threads.  Suppose we have two
726 			 * threads A1 A2 in one process, A1 locked
727 			 * file f1, A2 is locking file f2, and A1 is
728 			 * unlocking f1. Then remote server may
729 			 * already unlocked f1, while local still not
730 			 * yet scheduled A1 to make the call to local
731 			 * advlock manager. The process B owns lock on
732 			 * f2 and issued the lock on f1.  Remote would
733 			 * grant B the request on f1, but local would
734 			 * return EDEADLK.
735 			*/
736 			pause("nlmdlk", 1);
737 			p = curproc;
738 			stops_deferred = sigdeferstop(SIGDEFERSTOP_OFF);
739 			PROC_LOCK(p);
740 			thread_suspend_check(0);
741 			PROC_UNLOCK(p);
742 			sigallowstop(stops_deferred);
743 		} else if (error == EINTR) {
744 			/*
745 			 * lf_purgelocks() might wake up the lock
746 			 * waiter and removed our lock graph edges.
747 			 * There is no sense in re-trying recording
748 			 * the lock to the local manager after
749 			 * reclaim.
750 			 */
751 			error = 0;
752 			break;
753 		} else
754 			break;
755 	}
756 	KASSERT(error == 0 || error == ENOENT,
757 	    ("Failed to register NFS lock locally - error=%d", error));
758 }
759 
760 static int
761 nlm_setlock(struct nlm_host *host, struct rpc_callextra *ext,
762     rpcvers_t vers, struct timeval *timo, int retries,
763     struct vnode *vp, int op, struct flock *fl, int flags,
764     int svid, size_t fhlen, void *fh, off_t size, bool_t reclaim)
765 {
766 	struct nlm4_lockargs args;
767 	char oh_space[32];
768 	struct nlm4_res res;
769 	u_int xid;
770 	CLIENT *client;
771 	enum clnt_stat stat;
772 	int retry, block, exclusive;
773 	void *wait_handle = NULL;
774 	int error;
775 
776 	memset(&args, 0, sizeof(args));
777 	memset(&res, 0, sizeof(res));
778 
779 	block = (flags & F_WAIT) ? TRUE : FALSE;
780 	exclusive = (fl->l_type == F_WRLCK);
781 
782 	error = nlm_init_lock(fl, flags, svid, vers, fhlen, fh, size,
783 	    &args.alock, oh_space);
784 	if (error)
785 		return (error);
786 	args.block = block;
787 	args.exclusive = exclusive;
788 	args.reclaim = reclaim;
789 	args.state = nlm_nsm_state;
790 
791 	retry = 5*hz;
792 	for (;;) {
793 		client = nlm_host_get_rpc(host, FALSE);
794 		if (!client)
795 			return (ENOLCK); /* XXX retry? */
796 
797 		if (block)
798 			wait_handle = nlm_register_wait_lock(&args.alock, vp);
799 
800 		xid = atomic_fetchadd_int(&nlm_xid, 1);
801 		args.cookie.n_len = sizeof(xid);
802 		args.cookie.n_bytes = (char*) &xid;
803 
804 		stat = nlm_lock_rpc(vers, &args, &res, client, ext, *timo);
805 
806 		CLNT_RELEASE(client);
807 
808 		if (stat != RPC_SUCCESS) {
809 			if (block)
810 				nlm_deregister_wait_lock(wait_handle);
811 			if (retries) {
812 				retries--;
813 				continue;
814 			}
815 			return (EINVAL);
816 		}
817 
818 		/*
819 		 * Free res.cookie.
820 		 */
821 		xdr_free((xdrproc_t) xdr_nlm4_res, &res);
822 
823 		if (block && res.stat.stat != nlm4_blocked)
824 			nlm_deregister_wait_lock(wait_handle);
825 
826 		if (res.stat.stat == nlm4_denied_grace_period) {
827 			/*
828 			 * The server has recently rebooted and is
829 			 * giving old clients a change to reclaim
830 			 * their locks. Wait for a few seconds and try
831 			 * again.
832 			 */
833 			error = tsleep(&args, PCATCH, "nlmgrace", retry);
834 			if (error && error != EWOULDBLOCK)
835 				return (error);
836 			retry = 2*retry;
837 			if (retry > 30*hz)
838 				retry = 30*hz;
839 			continue;
840 		}
841 
842 		if (block && res.stat.stat == nlm4_blocked) {
843 			/*
844 			 * The server should call us back with a
845 			 * granted message when the lock succeeds. In
846 			 * order to deal with broken servers, lost
847 			 * granted messages and server reboots, we
848 			 * will also re-try every few seconds.
849 			 */
850 			error = nlm_wait_lock(wait_handle, retry);
851 			if (error == EWOULDBLOCK) {
852 				retry = 2*retry;
853 				if (retry > 30*hz)
854 					retry = 30*hz;
855 				continue;
856 			}
857 			if (error) {
858 				/*
859 				 * We need to call the server to
860 				 * cancel our lock request.
861 				 */
862 				nlm4_cancargs cancel;
863 
864 				memset(&cancel, 0, sizeof(cancel));
865 
866 				xid = atomic_fetchadd_int(&nlm_xid, 1);
867 				cancel.cookie.n_len = sizeof(xid);
868 				cancel.cookie.n_bytes = (char*) &xid;
869 				cancel.block = block;
870 				cancel.exclusive = exclusive;
871 				cancel.alock = args.alock;
872 
873 				do {
874 					client = nlm_host_get_rpc(host, FALSE);
875 					if (!client)
876 						/* XXX retry? */
877 						return (ENOLCK);
878 
879 					stat = nlm_cancel_rpc(vers, &cancel,
880 					    &res, client, ext, *timo);
881 
882 					CLNT_RELEASE(client);
883 
884 					if (stat != RPC_SUCCESS) {
885 						/*
886 						 * We need to cope
887 						 * with temporary
888 						 * network partitions
889 						 * as well as server
890 						 * reboots. This means
891 						 * we have to keep
892 						 * trying to cancel
893 						 * until the server
894 						 * wakes up again.
895 						 */
896 						pause("nlmcancel", 10*hz);
897 					}
898 				} while (stat != RPC_SUCCESS);
899 
900 				/*
901 				 * Free res.cookie.
902 				 */
903 				xdr_free((xdrproc_t) xdr_nlm4_res, &res);
904 
905 				switch (res.stat.stat) {
906 				case nlm_denied:
907 					/*
908 					 * There was nothing
909 					 * to cancel. We are
910 					 * going to go ahead
911 					 * and assume we got
912 					 * the lock.
913 					 */
914 					error = 0;
915 					break;
916 
917 				case nlm4_denied_grace_period:
918 					/*
919 					 * The server has
920 					 * recently rebooted -
921 					 * treat this as a
922 					 * successful
923 					 * cancellation.
924 					 */
925 					break;
926 
927 				case nlm4_granted:
928 					/*
929 					 * We managed to
930 					 * cancel.
931 					 */
932 					break;
933 
934 				default:
935 					/*
936 					 * Broken server
937 					 * implementation -
938 					 * can't really do
939 					 * anything here.
940 					 */
941 					break;
942 				}
943 			}
944 		} else {
945 			error = nlm_map_status(res.stat.stat);
946 		}
947 
948 		if (!error && !reclaim) {
949 			nlm_record_lock(vp, op, fl, args.alock.svid,
950 			    nlm_host_get_sysid(host), size);
951 			nlm_host_monitor(host, 0);
952 		}
953 
954 		return (error);
955 	}
956 }
957 
958 static int
959 nlm_clearlock(struct nlm_host *host, struct rpc_callextra *ext,
960     rpcvers_t vers, struct timeval *timo, int retries,
961     struct vnode *vp, int op, struct flock *fl, int flags,
962     int svid, size_t fhlen, void *fh, off_t size)
963 {
964 	struct nlm4_unlockargs args;
965 	char oh_space[32];
966 	struct nlm4_res res;
967 	u_int xid;
968 	CLIENT *client;
969 	enum clnt_stat stat;
970 	int error;
971 
972 	memset(&args, 0, sizeof(args));
973 	memset(&res, 0, sizeof(res));
974 
975 	error = nlm_init_lock(fl, flags, svid, vers, fhlen, fh, size,
976 	    &args.alock, oh_space);
977 	if (error)
978 		return (error);
979 
980 	for (;;) {
981 		client = nlm_host_get_rpc(host, FALSE);
982 		if (!client)
983 			return (ENOLCK); /* XXX retry? */
984 
985 		xid = atomic_fetchadd_int(&nlm_xid, 1);
986 		args.cookie.n_len = sizeof(xid);
987 		args.cookie.n_bytes = (char*) &xid;
988 
989 		stat = nlm_unlock_rpc(vers, &args, &res, client, ext, *timo);
990 
991 		CLNT_RELEASE(client);
992 
993 		if (stat != RPC_SUCCESS) {
994 			if (retries) {
995 				retries--;
996 				continue;
997 			}
998 			return (EINVAL);
999 		}
1000 
1001 		/*
1002 		 * Free res.cookie.
1003 		 */
1004 		xdr_free((xdrproc_t) xdr_nlm4_res, &res);
1005 
1006 		if (res.stat.stat == nlm4_denied_grace_period) {
1007 			/*
1008 			 * The server has recently rebooted and is
1009 			 * giving old clients a change to reclaim
1010 			 * their locks. Wait for a few seconds and try
1011 			 * again.
1012 			 */
1013 			error = tsleep(&args, PCATCH, "nlmgrace", 5*hz);
1014 			if (error && error != EWOULDBLOCK)
1015 				return (error);
1016 			continue;
1017 		}
1018 
1019 		/*
1020 		 * If we are being called via nlm_reclaim (which will
1021 		 * use the F_REMOTE flag), don't record the lock
1022 		 * operation in the local lock manager since the vnode
1023 		 * is going away.
1024 		 */
1025 		if (!(flags & F_REMOTE))
1026 			nlm_record_lock(vp, op, fl, args.alock.svid,
1027 			    nlm_host_get_sysid(host), size);
1028 
1029 		return (0);
1030 	}
1031 }
1032 
1033 static int
1034 nlm_getlock(struct nlm_host *host, struct rpc_callextra *ext,
1035     rpcvers_t vers, struct timeval *timo, int retries,
1036     struct vnode *vp, int op, struct flock *fl, int flags,
1037     int svid, size_t fhlen, void *fh, off_t size)
1038 {
1039 	struct nlm4_testargs args;
1040 	char oh_space[32];
1041 	struct nlm4_testres res;
1042 	u_int xid;
1043 	CLIENT *client;
1044 	enum clnt_stat stat;
1045 	int exclusive;
1046 	int error;
1047 
1048 	KASSERT(!(flags & F_FLOCK), ("unexpected F_FLOCK for F_GETLK"));
1049 
1050 	memset(&args, 0, sizeof(args));
1051 	memset(&res, 0, sizeof(res));
1052 
1053 	exclusive = (fl->l_type == F_WRLCK);
1054 
1055 	error = nlm_init_lock(fl, flags, svid, vers, fhlen, fh, size,
1056 	    &args.alock, oh_space);
1057 	if (error)
1058 		return (error);
1059 	args.exclusive = exclusive;
1060 
1061 	for (;;) {
1062 		client = nlm_host_get_rpc(host, FALSE);
1063 		if (!client)
1064 			return (ENOLCK); /* XXX retry? */
1065 
1066 		xid = atomic_fetchadd_int(&nlm_xid, 1);
1067 		args.cookie.n_len = sizeof(xid);
1068 		args.cookie.n_bytes = (char*) &xid;
1069 
1070 		stat = nlm_test_rpc(vers, &args, &res, client, ext, *timo);
1071 
1072 		CLNT_RELEASE(client);
1073 
1074 		if (stat != RPC_SUCCESS) {
1075 			if (retries) {
1076 				retries--;
1077 				continue;
1078 			}
1079 			return (EINVAL);
1080 		}
1081 
1082 		if (res.stat.stat == nlm4_denied_grace_period) {
1083 			/*
1084 			 * The server has recently rebooted and is
1085 			 * giving old clients a change to reclaim
1086 			 * their locks. Wait for a few seconds and try
1087 			 * again.
1088 			 */
1089 			xdr_free((xdrproc_t) xdr_nlm4_testres, &res);
1090 			error = tsleep(&args, PCATCH, "nlmgrace", 5*hz);
1091 			if (error && error != EWOULDBLOCK)
1092 				return (error);
1093 			continue;
1094 		}
1095 
1096 		if (res.stat.stat == nlm4_denied) {
1097 			struct nlm4_holder *h =
1098 				&res.stat.nlm4_testrply_u.holder;
1099 			fl->l_start = h->l_offset;
1100 			fl->l_len = h->l_len;
1101 			fl->l_pid = h->svid;
1102 			if (h->exclusive)
1103 				fl->l_type = F_WRLCK;
1104 			else
1105 				fl->l_type = F_RDLCK;
1106 			fl->l_whence = SEEK_SET;
1107 			fl->l_sysid = 0;
1108 		} else {
1109 			fl->l_type = F_UNLCK;
1110 		}
1111 
1112 		xdr_free((xdrproc_t) xdr_nlm4_testres, &res);
1113 
1114 		return (0);
1115 	}
1116 }
1117 
1118 static int
1119 nlm_map_status(nlm4_stats stat)
1120 {
1121 	switch (stat) {
1122 	case nlm4_granted:
1123 		return (0);
1124 
1125 	case nlm4_denied:
1126 		return (EAGAIN);
1127 
1128 	case nlm4_denied_nolocks:
1129 		return (ENOLCK);
1130 
1131 	case nlm4_deadlck:
1132 		return (EDEADLK);
1133 
1134 	case nlm4_rofs:
1135 		return (EROFS);
1136 
1137 	case nlm4_stale_fh:
1138 		return (ESTALE);
1139 
1140 	case nlm4_fbig:
1141 		return (EFBIG);
1142 
1143 	case nlm4_failed:
1144 		return (EACCES);
1145 
1146 	default:
1147 		return (EINVAL);
1148 	}
1149 }
1150 
1151 static struct nlm_file_svid *
1152 nlm_find_svid(void *id)
1153 {
1154 	struct nlm_file_svid *ns, *newns;
1155 	int h;
1156 
1157 	h = (((uintptr_t) id) >> 7) % NLM_SVID_HASH_SIZE;
1158 
1159 	mtx_lock(&nlm_svid_lock);
1160 	LIST_FOREACH(ns, &nlm_file_svids[h], ns_link) {
1161 		if (ns->ns_id == id) {
1162 			ns->ns_refs++;
1163 			break;
1164 		}
1165 	}
1166 	mtx_unlock(&nlm_svid_lock);
1167 	if (!ns) {
1168 		int svid = alloc_unr(nlm_svid_allocator);
1169 		newns = malloc(sizeof(struct nlm_file_svid), M_NLM,
1170 		    M_WAITOK);
1171 		newns->ns_refs = 1;
1172 		newns->ns_id = id;
1173 		newns->ns_svid = svid;
1174 		newns->ns_ucred = NULL;
1175 		newns->ns_active = FALSE;
1176 
1177 		/*
1178 		 * We need to check for a race with some other
1179 		 * thread allocating a svid for this file.
1180 		 */
1181 		mtx_lock(&nlm_svid_lock);
1182 		LIST_FOREACH(ns, &nlm_file_svids[h], ns_link) {
1183 			if (ns->ns_id == id) {
1184 				ns->ns_refs++;
1185 				break;
1186 			}
1187 		}
1188 		if (ns) {
1189 			mtx_unlock(&nlm_svid_lock);
1190 			free_unr(nlm_svid_allocator, newns->ns_svid);
1191 			free(newns, M_NLM);
1192 		} else {
1193 			LIST_INSERT_HEAD(&nlm_file_svids[h], newns,
1194 			    ns_link);
1195 			ns = newns;
1196 			mtx_unlock(&nlm_svid_lock);
1197 		}
1198 	}
1199 
1200 	return (ns);
1201 }
1202 
1203 static void
1204 nlm_free_svid(struct nlm_file_svid *ns)
1205 {
1206 
1207 	mtx_lock(&nlm_svid_lock);
1208 	ns->ns_refs--;
1209 	if (!ns->ns_refs) {
1210 		KASSERT(!ns->ns_active, ("Freeing active SVID"));
1211 		LIST_REMOVE(ns, ns_link);
1212 		mtx_unlock(&nlm_svid_lock);
1213 		free_unr(nlm_svid_allocator, ns->ns_svid);
1214 		if (ns->ns_ucred)
1215 			crfree(ns->ns_ucred);
1216 		free(ns, M_NLM);
1217 	} else {
1218 		mtx_unlock(&nlm_svid_lock);
1219 	}
1220 }
1221 
1222 static int
1223 nlm_init_lock(struct flock *fl, int flags, int svid,
1224     rpcvers_t vers, size_t fhlen, void *fh, off_t size,
1225     struct nlm4_lock *lock, char oh_space[32])
1226 {
1227 	size_t oh_len;
1228 	off_t start, len;
1229 
1230 	if (fl->l_whence == SEEK_END) {
1231 		if (size > OFF_MAX
1232 		    || (fl->l_start > 0 && size > OFF_MAX - fl->l_start))
1233 			return (EOVERFLOW);
1234 		start = size + fl->l_start;
1235 	} else if (fl->l_whence == SEEK_SET || fl->l_whence == SEEK_CUR) {
1236 		start = fl->l_start;
1237 	} else {
1238 		return (EINVAL);
1239 	}
1240 	if (start < 0)
1241 		return (EINVAL);
1242 	if (fl->l_len < 0) {
1243 		len = -fl->l_len;
1244 		start -= len;
1245 		if (start < 0)
1246 			return (EINVAL);
1247 	} else {
1248 		len = fl->l_len;
1249 	}
1250 
1251 	if (vers == NLM_VERS) {
1252 		/*
1253 		 * Enforce range limits on V1 locks
1254 		 */
1255 		if (start > 0xffffffffLL || len > 0xffffffffLL)
1256 			return (EOVERFLOW);
1257 	}
1258 
1259 	snprintf(oh_space, 32, "%d@", svid);
1260 	oh_len = strlen(oh_space);
1261 	getcredhostname(NULL, oh_space + oh_len, 32 - oh_len);
1262 	oh_len = strlen(oh_space);
1263 
1264 	memset(lock, 0, sizeof(*lock));
1265 	lock->caller_name = prison0.pr_hostname;
1266 	lock->fh.n_len = fhlen;
1267 	lock->fh.n_bytes = fh;
1268 	lock->oh.n_len = oh_len;
1269 	lock->oh.n_bytes = oh_space;
1270 	lock->svid = svid;
1271 	lock->l_offset = start;
1272 	lock->l_len = len;
1273 
1274 	return (0);
1275 }
1276