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