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