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