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