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