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