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