xref: /netbsd-src/sys/fs/nfs/nlm/nlm_advlock.c (revision d687e96a027d668c69e289ca0ac86d6257cf26c3)
1*d687e96aSpgoyette /*	$NetBSD: nlm_advlock.c,v 1.2 2016/12/13 21:58:17 pgoyette Exp $	*/
26ca35587Sdholland /*-
36ca35587Sdholland  * Copyright (c) 2008 Isilon Inc http://www.isilon.com/
46ca35587Sdholland  * Authors: Doug Rabson <dfr@rabson.org>
56ca35587Sdholland  * Developed with Red Inc: Alfred Perlstein <alfred@freebsd.org>
66ca35587Sdholland  *
76ca35587Sdholland  * Redistribution and use in source and binary forms, with or without
86ca35587Sdholland  * modification, are permitted provided that the following conditions
96ca35587Sdholland  * are met:
106ca35587Sdholland  * 1. Redistributions of source code must retain the above copyright
116ca35587Sdholland  *    notice, this list of conditions and the following disclaimer.
126ca35587Sdholland  * 2. Redistributions in binary form must reproduce the above copyright
136ca35587Sdholland  *    notice, this list of conditions and the following disclaimer in the
146ca35587Sdholland  *    documentation and/or other materials provided with the distribution.
156ca35587Sdholland  *
166ca35587Sdholland  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
176ca35587Sdholland  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
186ca35587Sdholland  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
196ca35587Sdholland  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
206ca35587Sdholland  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
216ca35587Sdholland  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
226ca35587Sdholland  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
236ca35587Sdholland  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
246ca35587Sdholland  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
256ca35587Sdholland  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
266ca35587Sdholland  * SUCH DAMAGE.
276ca35587Sdholland  */
286ca35587Sdholland 
296ca35587Sdholland #include <sys/cdefs.h>
30e81f0ea2Spgoyette /* __FBSDID("FreeBSD: head/sys/nlm/nlm_advlock.c 302216 2016-06-26 20:08:42Z kib "); */
31*d687e96aSpgoyette __RCSID("$NetBSD: nlm_advlock.c,v 1.2 2016/12/13 21:58:17 pgoyette Exp $");
326ca35587Sdholland 
336ca35587Sdholland #include <sys/param.h>
346ca35587Sdholland #include <sys/fcntl.h>
356ca35587Sdholland #include <sys/jail.h>
366ca35587Sdholland #include <sys/kernel.h>
376ca35587Sdholland #include <sys/limits.h>
386ca35587Sdholland #include <sys/lock.h>
396ca35587Sdholland #include <sys/lockf.h>
406ca35587Sdholland #include <sys/malloc.h>
416ca35587Sdholland #include <sys/mbuf.h>
426ca35587Sdholland #include <sys/mount.h>
436ca35587Sdholland #include <sys/mutex.h>
446ca35587Sdholland #include <sys/proc.h>
456ca35587Sdholland #include <sys/socket.h>
466ca35587Sdholland #include <sys/syslog.h>
476ca35587Sdholland #include <sys/systm.h>
486ca35587Sdholland #include <sys/unistd.h>
496ca35587Sdholland #include <sys/vnode.h>
506ca35587Sdholland 
51*d687e96aSpgoyette #include <fs/nfs/common/nfsproto.h>
52*d687e96aSpgoyette #include <fs/nfs/client/nfs.h>
53*d687e96aSpgoyette #include <fs/nfs/client/nfsmount.h>
546ca35587Sdholland 
55*d687e96aSpgoyette #include <fs/nfs/nlm/nlm_prot.h>
56*d687e96aSpgoyette #include <fs/nfs/nlm/nlm.h>
576ca35587Sdholland 
586ca35587Sdholland /*
596ca35587Sdholland  * We need to keep track of the svid values used for F_FLOCK locks.
606ca35587Sdholland  */
616ca35587Sdholland struct nlm_file_svid {
626ca35587Sdholland 	int		ns_refs;	/* thread count + 1 if active */
636ca35587Sdholland 	int		ns_svid;	/* on-the-wire SVID for this file */
646ca35587Sdholland 	struct ucred	*ns_ucred;	/* creds to use for lock recovery */
656ca35587Sdholland 	void		*ns_id;		/* local struct file pointer */
666ca35587Sdholland 	bool_t		ns_active;	/* TRUE if we own a lock */
676ca35587Sdholland 	LIST_ENTRY(nlm_file_svid) ns_link;
686ca35587Sdholland };
696ca35587Sdholland LIST_HEAD(nlm_file_svid_list, nlm_file_svid);
706ca35587Sdholland 
716ca35587Sdholland #define NLM_SVID_HASH_SIZE	256
726ca35587Sdholland struct nlm_file_svid_list nlm_file_svids[NLM_SVID_HASH_SIZE];
736ca35587Sdholland 
746ca35587Sdholland struct mtx nlm_svid_lock;
756ca35587Sdholland static struct unrhdr *nlm_svid_allocator;
766ca35587Sdholland static volatile u_int nlm_xid = 1;
776ca35587Sdholland 
786ca35587Sdholland static int nlm_setlock(struct nlm_host *host, struct rpc_callextra *ext,
796ca35587Sdholland     rpcvers_t vers, struct timeval *timo, int retries,
806ca35587Sdholland     struct vnode *vp, int op, struct flock *fl, int flags,
816ca35587Sdholland     int svid, size_t fhlen, void *fh, off_t size, bool_t reclaim);
826ca35587Sdholland static int nlm_clearlock(struct nlm_host *host,  struct rpc_callextra *ext,
836ca35587Sdholland     rpcvers_t vers, struct timeval *timo, int retries,
846ca35587Sdholland     struct vnode *vp, int op, struct flock *fl, int flags,
856ca35587Sdholland     int svid, size_t fhlen, void *fh, off_t size);
866ca35587Sdholland static int nlm_getlock(struct nlm_host *host, struct rpc_callextra *ext,
876ca35587Sdholland     rpcvers_t vers, struct timeval *timo, int retries,
886ca35587Sdholland     struct vnode *vp, int op, struct flock *fl, int flags,
896ca35587Sdholland     int svid, size_t fhlen, void *fh, off_t size);
906ca35587Sdholland static int nlm_map_status(nlm4_stats stat);
916ca35587Sdholland static struct nlm_file_svid *nlm_find_svid(void *id);
926ca35587Sdholland static void nlm_free_svid(struct nlm_file_svid *nf);
936ca35587Sdholland static int nlm_init_lock(struct flock *fl, int flags, int svid,
946ca35587Sdholland     rpcvers_t vers, size_t fhlen, void *fh, off_t size,
956ca35587Sdholland     struct nlm4_lock *lock, char oh_space[32]);
966ca35587Sdholland 
976ca35587Sdholland static void
nlm_client_init(void * dummy)986ca35587Sdholland nlm_client_init(void *dummy)
996ca35587Sdholland {
1006ca35587Sdholland 	int i;
1016ca35587Sdholland 
1026ca35587Sdholland 	mtx_init(&nlm_svid_lock, "NLM svid lock", NULL, MTX_DEF);
1036ca35587Sdholland 	/* pid_max cannot be greater than PID_MAX */
1046ca35587Sdholland 	nlm_svid_allocator = new_unrhdr(PID_MAX + 2, INT_MAX, &nlm_svid_lock);
1056ca35587Sdholland 	for (i = 0; i < NLM_SVID_HASH_SIZE; i++)
1066ca35587Sdholland 		LIST_INIT(&nlm_file_svids[i]);
1076ca35587Sdholland }
1086ca35587Sdholland SYSINIT(nlm_client_init, SI_SUB_LOCK, SI_ORDER_FIRST, nlm_client_init, NULL);
1096ca35587Sdholland 
1106ca35587Sdholland static int
nlm_msg(struct thread * td,const char * server,const char * msg,int error)1116ca35587Sdholland nlm_msg(struct thread *td, const char *server, const char *msg, int error)
1126ca35587Sdholland {
1136ca35587Sdholland 	struct proc *p;
1146ca35587Sdholland 
1156ca35587Sdholland 	p = td ? td->td_proc : NULL;
1166ca35587Sdholland 	if (error) {
1176ca35587Sdholland 		tprintf(p, LOG_INFO, "nfs server %s: %s, error %d\n", server,
1186ca35587Sdholland 		    msg, error);
1196ca35587Sdholland 	} else {
1206ca35587Sdholland 		tprintf(p, LOG_INFO, "nfs server %s: %s\n", server, msg);
1216ca35587Sdholland 	}
1226ca35587Sdholland 	return (0);
1236ca35587Sdholland }
1246ca35587Sdholland 
1256ca35587Sdholland struct nlm_feedback_arg {
1266ca35587Sdholland 	bool_t	nf_printed;
1276ca35587Sdholland 	struct nfsmount *nf_nmp;
1286ca35587Sdholland };
1296ca35587Sdholland 
1306ca35587Sdholland static void
nlm_down(struct nlm_feedback_arg * nf,struct thread * td,const char * msg,int error)1316ca35587Sdholland nlm_down(struct nlm_feedback_arg *nf, struct thread *td,
1326ca35587Sdholland     const char *msg, int error)
1336ca35587Sdholland {
1346ca35587Sdholland 	struct nfsmount *nmp = nf->nf_nmp;
1356ca35587Sdholland 
1366ca35587Sdholland 	if (nmp == NULL)
1376ca35587Sdholland 		return;
1386ca35587Sdholland 	mtx_lock(&nmp->nm_mtx);
1396ca35587Sdholland 	if (!(nmp->nm_state & NFSSTA_LOCKTIMEO)) {
1406ca35587Sdholland 		nmp->nm_state |= NFSSTA_LOCKTIMEO;
1416ca35587Sdholland 		mtx_unlock(&nmp->nm_mtx);
1426ca35587Sdholland 		vfs_event_signal(&nmp->nm_mountp->mnt_stat.f_fsid,
1436ca35587Sdholland 		    VQ_NOTRESPLOCK, 0);
1446ca35587Sdholland 	} else {
1456ca35587Sdholland 		mtx_unlock(&nmp->nm_mtx);
1466ca35587Sdholland 	}
1476ca35587Sdholland 
1486ca35587Sdholland 	nf->nf_printed = TRUE;
1496ca35587Sdholland 	nlm_msg(td, nmp->nm_mountp->mnt_stat.f_mntfromname, msg, error);
1506ca35587Sdholland }
1516ca35587Sdholland 
1526ca35587Sdholland static void
nlm_up(struct nlm_feedback_arg * nf,struct thread * td,const char * msg)1536ca35587Sdholland nlm_up(struct nlm_feedback_arg *nf, struct thread *td,
1546ca35587Sdholland     const char *msg)
1556ca35587Sdholland {
1566ca35587Sdholland 	struct nfsmount *nmp = nf->nf_nmp;
1576ca35587Sdholland 
1586ca35587Sdholland 	if (!nf->nf_printed)
1596ca35587Sdholland 		return;
1606ca35587Sdholland 
1616ca35587Sdholland 	nlm_msg(td, nmp->nm_mountp->mnt_stat.f_mntfromname, msg, 0);
1626ca35587Sdholland 
1636ca35587Sdholland 	mtx_lock(&nmp->nm_mtx);
1646ca35587Sdholland 	if (nmp->nm_state & NFSSTA_LOCKTIMEO) {
1656ca35587Sdholland 		nmp->nm_state &= ~NFSSTA_LOCKTIMEO;
1666ca35587Sdholland 		mtx_unlock(&nmp->nm_mtx);
1676ca35587Sdholland 		vfs_event_signal(&nmp->nm_mountp->mnt_stat.f_fsid,
1686ca35587Sdholland 		    VQ_NOTRESPLOCK, 1);
1696ca35587Sdholland 	} else {
1706ca35587Sdholland 		mtx_unlock(&nmp->nm_mtx);
1716ca35587Sdholland 	}
1726ca35587Sdholland }
1736ca35587Sdholland 
1746ca35587Sdholland static void
nlm_feedback(int type,int proc,void * arg)1756ca35587Sdholland nlm_feedback(int type, int proc, void *arg)
1766ca35587Sdholland {
1776ca35587Sdholland 	struct thread *td = curthread;
1786ca35587Sdholland 	struct nlm_feedback_arg *nf = (struct nlm_feedback_arg *) arg;
1796ca35587Sdholland 
1806ca35587Sdholland 	switch (type) {
1816ca35587Sdholland 	case FEEDBACK_REXMIT2:
1826ca35587Sdholland 	case FEEDBACK_RECONNECT:
1836ca35587Sdholland 		nlm_down(nf, td, "lockd not responding", 0);
1846ca35587Sdholland 		break;
1856ca35587Sdholland 
1866ca35587Sdholland 	case FEEDBACK_OK:
1876ca35587Sdholland 		nlm_up(nf, td, "lockd is alive again");
1886ca35587Sdholland 		break;
1896ca35587Sdholland 	}
1906ca35587Sdholland }
1916ca35587Sdholland 
1926ca35587Sdholland /*
1936ca35587Sdholland  * nlm_advlock --
1946ca35587Sdholland  *      NFS advisory byte-level locks.
1956ca35587Sdholland  */
1966ca35587Sdholland static int
nlm_advlock_internal(struct vnode * vp,void * id,int op,struct flock * fl,int flags,bool_t reclaim,bool_t unlock_vp)1976ca35587Sdholland nlm_advlock_internal(struct vnode *vp, void *id, int op, struct flock *fl,
1986ca35587Sdholland     int flags, bool_t reclaim, bool_t unlock_vp)
1996ca35587Sdholland {
2006ca35587Sdholland 	struct thread *td = curthread;
2016ca35587Sdholland 	struct nfsmount *nmp;
2026ca35587Sdholland 	off_t size;
2036ca35587Sdholland 	size_t fhlen;
2046ca35587Sdholland 	union nfsfh fh;
2056ca35587Sdholland 	struct sockaddr *sa;
2066ca35587Sdholland 	struct sockaddr_storage ss;
2076ca35587Sdholland 	char servername[MNAMELEN];
2086ca35587Sdholland 	struct timeval timo;
2096ca35587Sdholland 	int retries;
2106ca35587Sdholland 	rpcvers_t vers;
2116ca35587Sdholland 	struct nlm_host *host;
2126ca35587Sdholland 	struct rpc_callextra ext;
2136ca35587Sdholland 	struct nlm_feedback_arg nf;
2146ca35587Sdholland 	AUTH *auth;
215e81f0ea2Spgoyette 	struct ucred *cred, *cred1;
2166ca35587Sdholland 	struct nlm_file_svid *ns;
2176ca35587Sdholland 	int svid;
2186ca35587Sdholland 	int error;
2196ca35587Sdholland 	int is_v3;
2206ca35587Sdholland 
2216ca35587Sdholland 	ASSERT_VOP_LOCKED(vp, "nlm_advlock_1");
2226ca35587Sdholland 
2236ca35587Sdholland 	nmp = VFSTONFS(vp->v_mount);
2246ca35587Sdholland 	/*
2256ca35587Sdholland 	 * Push any pending writes to the server and flush our cache
2266ca35587Sdholland 	 * so that if we are contending with another machine for a
2276ca35587Sdholland 	 * file, we get whatever they wrote and vice-versa.
2286ca35587Sdholland 	 */
2296ca35587Sdholland 	if (op == F_SETLK || op == F_UNLCK)
2306ca35587Sdholland 		nmp->nm_vinvalbuf(vp, V_SAVE, td, 1);
2316ca35587Sdholland 
2326ca35587Sdholland 	strcpy(servername, nmp->nm_hostname);
2336ca35587Sdholland 	nmp->nm_getinfo(vp, fh.fh_bytes, &fhlen, &ss, &is_v3, &size, &timo);
2346ca35587Sdholland 	sa = (struct sockaddr *) &ss;
2356ca35587Sdholland 	if (is_v3 != 0)
2366ca35587Sdholland 		vers = NLM_VERS4;
2376ca35587Sdholland 	else
2386ca35587Sdholland 		vers = NLM_VERS;
2396ca35587Sdholland 
2406ca35587Sdholland 	if (nmp->nm_flag & NFSMNT_SOFT)
2416ca35587Sdholland 		retries = nmp->nm_retry;
2426ca35587Sdholland 	else
2436ca35587Sdholland 		retries = INT_MAX;
2446ca35587Sdholland 
2456ca35587Sdholland 	/*
2466ca35587Sdholland 	 * We need to switch to mount-point creds so that we can send
247e81f0ea2Spgoyette 	 * packets from a privileged port.  Reference mnt_cred and
248e81f0ea2Spgoyette 	 * switch to them before unlocking the vnode, since mount
249e81f0ea2Spgoyette 	 * point could be unmounted right after unlock.
2506ca35587Sdholland 	 */
2516ca35587Sdholland 	cred = td->td_ucred;
2526ca35587Sdholland 	td->td_ucred = vp->v_mount->mnt_cred;
253e81f0ea2Spgoyette 	crhold(td->td_ucred);
254e81f0ea2Spgoyette 	if (unlock_vp)
255e81f0ea2Spgoyette 		VOP_UNLOCK(vp, 0);
2566ca35587Sdholland 
2576ca35587Sdholland 	host = nlm_find_host_by_name(servername, sa, vers);
2586ca35587Sdholland 	auth = authunix_create(cred);
2596ca35587Sdholland 	memset(&ext, 0, sizeof(ext));
2606ca35587Sdholland 
2616ca35587Sdholland 	nf.nf_printed = FALSE;
2626ca35587Sdholland 	nf.nf_nmp = nmp;
2636ca35587Sdholland 	ext.rc_auth = auth;
2646ca35587Sdholland 
2656ca35587Sdholland 	ext.rc_feedback = nlm_feedback;
2666ca35587Sdholland 	ext.rc_feedback_arg = &nf;
2676ca35587Sdholland 	ext.rc_timers = NULL;
2686ca35587Sdholland 
2696ca35587Sdholland 	ns = NULL;
2706ca35587Sdholland 	if (flags & F_FLOCK) {
2716ca35587Sdholland 		ns = nlm_find_svid(id);
2726ca35587Sdholland 		KASSERT(fl->l_start == 0 && fl->l_len == 0,
2736ca35587Sdholland 		    ("F_FLOCK lock requests must be whole-file locks"));
2746ca35587Sdholland 		if (!ns->ns_ucred) {
2756ca35587Sdholland 			/*
2766ca35587Sdholland 			 * Remember the creds used for locking in case
2776ca35587Sdholland 			 * we need to recover the lock later.
2786ca35587Sdholland 			 */
2796ca35587Sdholland 			ns->ns_ucred = crdup(cred);
2806ca35587Sdholland 		}
2816ca35587Sdholland 		svid = ns->ns_svid;
2826ca35587Sdholland 	} else if (flags & F_REMOTE) {
2836ca35587Sdholland 		/*
2846ca35587Sdholland 		 * If we are recovering after a server restart or
2856ca35587Sdholland 		 * trashing locks on a force unmount, use the same
2866ca35587Sdholland 		 * svid as last time.
2876ca35587Sdholland 		 */
2886ca35587Sdholland 		svid = fl->l_pid;
2896ca35587Sdholland 	} else {
2906ca35587Sdholland 		svid = ((struct proc *) id)->p_pid;
2916ca35587Sdholland 	}
2926ca35587Sdholland 
2936ca35587Sdholland 	switch(op) {
2946ca35587Sdholland 	case F_SETLK:
2956ca35587Sdholland 		if ((flags & (F_FLOCK|F_WAIT)) == (F_FLOCK|F_WAIT)
2966ca35587Sdholland 		    && fl->l_type == F_WRLCK) {
2976ca35587Sdholland 			/*
2986ca35587Sdholland 			 * The semantics for flock(2) require that any
2996ca35587Sdholland 			 * shared lock on the file must be released
3006ca35587Sdholland 			 * before an exclusive lock is granted. The
3016ca35587Sdholland 			 * local locking code interprets this by
3026ca35587Sdholland 			 * unlocking the file before sleeping on a
3036ca35587Sdholland 			 * blocked exclusive lock request. We
3046ca35587Sdholland 			 * approximate this by first attempting
3056ca35587Sdholland 			 * non-blocking and if that fails, we unlock
3066ca35587Sdholland 			 * the file and block.
3076ca35587Sdholland 			 */
3086ca35587Sdholland 			error = nlm_setlock(host, &ext, vers, &timo, retries,
3096ca35587Sdholland 			    vp, F_SETLK, fl, flags & ~F_WAIT,
3106ca35587Sdholland 			    svid, fhlen, &fh.fh_bytes, size, reclaim);
3116ca35587Sdholland 			if (error == EAGAIN) {
3126ca35587Sdholland 				fl->l_type = F_UNLCK;
3136ca35587Sdholland 				error = nlm_clearlock(host, &ext, vers, &timo,
3146ca35587Sdholland 				    retries, vp, F_UNLCK, fl, flags,
3156ca35587Sdholland 				    svid, fhlen, &fh.fh_bytes, size);
3166ca35587Sdholland 				fl->l_type = F_WRLCK;
3176ca35587Sdholland 				if (!error) {
3186ca35587Sdholland 					mtx_lock(&nlm_svid_lock);
3196ca35587Sdholland 					if (ns->ns_active) {
3206ca35587Sdholland 						ns->ns_refs--;
3216ca35587Sdholland 						ns->ns_active = FALSE;
3226ca35587Sdholland 					}
3236ca35587Sdholland 					mtx_unlock(&nlm_svid_lock);
3246ca35587Sdholland 					flags |= F_WAIT;
3256ca35587Sdholland 					error = nlm_setlock(host, &ext, vers,
3266ca35587Sdholland 					    &timo, retries, vp, F_SETLK, fl,
3276ca35587Sdholland 					    flags, svid, fhlen, &fh.fh_bytes,
3286ca35587Sdholland 					    size, reclaim);
3296ca35587Sdholland 				}
3306ca35587Sdholland 			}
3316ca35587Sdholland 		} else {
3326ca35587Sdholland 			error = nlm_setlock(host, &ext, vers, &timo, retries,
3336ca35587Sdholland 			    vp, op, fl, flags, svid, fhlen, &fh.fh_bytes,
3346ca35587Sdholland 			    size, reclaim);
3356ca35587Sdholland 		}
3366ca35587Sdholland 		if (!error && ns) {
3376ca35587Sdholland 			mtx_lock(&nlm_svid_lock);
3386ca35587Sdholland 			if (!ns->ns_active) {
3396ca35587Sdholland 				/*
3406ca35587Sdholland 				 * Add one to the reference count to
3416ca35587Sdholland 				 * hold onto the SVID for the lifetime
3426ca35587Sdholland 				 * of the lock. Note that since
3436ca35587Sdholland 				 * F_FLOCK only supports whole-file
3446ca35587Sdholland 				 * locks, there can only be one active
3456ca35587Sdholland 				 * lock for this SVID.
3466ca35587Sdholland 				 */
3476ca35587Sdholland 				ns->ns_refs++;
3486ca35587Sdholland 				ns->ns_active = TRUE;
3496ca35587Sdholland 			}
3506ca35587Sdholland 			mtx_unlock(&nlm_svid_lock);
3516ca35587Sdholland 		}
3526ca35587Sdholland 		break;
3536ca35587Sdholland 
3546ca35587Sdholland 	case F_UNLCK:
3556ca35587Sdholland 		error = nlm_clearlock(host, &ext, vers, &timo, retries,
3566ca35587Sdholland 		    vp, op, fl, flags, svid, fhlen, &fh.fh_bytes, size);
3576ca35587Sdholland 		if (!error && ns) {
3586ca35587Sdholland 			mtx_lock(&nlm_svid_lock);
3596ca35587Sdholland 			if (ns->ns_active) {
3606ca35587Sdholland 				ns->ns_refs--;
3616ca35587Sdholland 				ns->ns_active = FALSE;
3626ca35587Sdholland 			}
3636ca35587Sdholland 			mtx_unlock(&nlm_svid_lock);
3646ca35587Sdholland 		}
3656ca35587Sdholland 		break;
3666ca35587Sdholland 
3676ca35587Sdholland 	case F_GETLK:
3686ca35587Sdholland 		error = nlm_getlock(host, &ext, vers, &timo, retries,
3696ca35587Sdholland 		    vp, op, fl, flags, svid, fhlen, &fh.fh_bytes, size);
3706ca35587Sdholland 		break;
3716ca35587Sdholland 
3726ca35587Sdholland 	default:
3736ca35587Sdholland 		error = EINVAL;
3746ca35587Sdholland 		break;
3756ca35587Sdholland 	}
3766ca35587Sdholland 
3776ca35587Sdholland 	if (ns)
3786ca35587Sdholland 		nlm_free_svid(ns);
3796ca35587Sdholland 
380e81f0ea2Spgoyette 	cred1 = td->td_ucred;
3816ca35587Sdholland 	td->td_ucred = cred;
382e81f0ea2Spgoyette 	crfree(cred1);
3836ca35587Sdholland 	AUTH_DESTROY(auth);
3846ca35587Sdholland 
3856ca35587Sdholland 	nlm_host_release(host);
3866ca35587Sdholland 
3876ca35587Sdholland 	return (error);
3886ca35587Sdholland }
3896ca35587Sdholland 
3906ca35587Sdholland int
nlm_advlock(struct vop_advlock_args * ap)3916ca35587Sdholland nlm_advlock(struct vop_advlock_args *ap)
3926ca35587Sdholland {
3936ca35587Sdholland 
3946ca35587Sdholland 	return (nlm_advlock_internal(ap->a_vp, ap->a_id, ap->a_op, ap->a_fl,
3956ca35587Sdholland 		ap->a_flags, FALSE, TRUE));
3966ca35587Sdholland }
3976ca35587Sdholland 
3986ca35587Sdholland /*
3996ca35587Sdholland  * Set the creds of td to the creds of the given lock's owner. The new
4006ca35587Sdholland  * creds reference count will be incremented via crhold. The caller is
4016ca35587Sdholland  * responsible for calling crfree and restoring td's original creds.
4026ca35587Sdholland  */
4036ca35587Sdholland static void
nlm_set_creds_for_lock(struct thread * td,struct flock * fl)4046ca35587Sdholland nlm_set_creds_for_lock(struct thread *td, struct flock *fl)
4056ca35587Sdholland {
4066ca35587Sdholland 	int i;
4076ca35587Sdholland 	struct nlm_file_svid *ns;
4086ca35587Sdholland 	struct proc *p;
4096ca35587Sdholland 	struct ucred *cred;
4106ca35587Sdholland 
4116ca35587Sdholland 	cred = NULL;
4126ca35587Sdholland 	if (fl->l_pid > PID_MAX) {
4136ca35587Sdholland 		/*
4146ca35587Sdholland 		 * If this was originally a F_FLOCK-style lock, we
4156ca35587Sdholland 		 * recorded the creds used when it was originally
4166ca35587Sdholland 		 * locked in the nlm_file_svid structure.
4176ca35587Sdholland 		 */
4186ca35587Sdholland 		mtx_lock(&nlm_svid_lock);
4196ca35587Sdholland 		for (i = 0; i < NLM_SVID_HASH_SIZE; i++) {
4206ca35587Sdholland 			for (ns = LIST_FIRST(&nlm_file_svids[i]); ns;
4216ca35587Sdholland 			     ns = LIST_NEXT(ns, ns_link)) {
4226ca35587Sdholland 				if (ns->ns_svid == fl->l_pid) {
4236ca35587Sdholland 					cred = crhold(ns->ns_ucred);
4246ca35587Sdholland 					break;
4256ca35587Sdholland 				}
4266ca35587Sdholland 			}
4276ca35587Sdholland 		}
4286ca35587Sdholland 		mtx_unlock(&nlm_svid_lock);
4296ca35587Sdholland 	} else {
4306ca35587Sdholland 		/*
4316ca35587Sdholland 		 * This lock is owned by a process. Get a reference to
4326ca35587Sdholland 		 * the process creds.
4336ca35587Sdholland 		 */
4346ca35587Sdholland 		p = pfind(fl->l_pid);
4356ca35587Sdholland 		if (p) {
4366ca35587Sdholland 			cred = crhold(p->p_ucred);
4376ca35587Sdholland 			PROC_UNLOCK(p);
4386ca35587Sdholland 		}
4396ca35587Sdholland 	}
4406ca35587Sdholland 
4416ca35587Sdholland 	/*
4426ca35587Sdholland 	 * If we can't find a cred, fall back on the recovery
4436ca35587Sdholland 	 * thread's cred.
4446ca35587Sdholland 	 */
4456ca35587Sdholland 	if (!cred) {
4466ca35587Sdholland 		cred = crhold(td->td_ucred);
4476ca35587Sdholland 	}
4486ca35587Sdholland 
4496ca35587Sdholland 	td->td_ucred = cred;
4506ca35587Sdholland }
4516ca35587Sdholland 
4526ca35587Sdholland static int
nlm_reclaim_free_lock(struct vnode * vp,struct flock * fl,void * arg)4536ca35587Sdholland nlm_reclaim_free_lock(struct vnode *vp, struct flock *fl, void *arg)
4546ca35587Sdholland {
4556ca35587Sdholland 	struct flock newfl;
4566ca35587Sdholland 	struct thread *td = curthread;
4576ca35587Sdholland 	struct ucred *oldcred;
4586ca35587Sdholland 	int error;
4596ca35587Sdholland 
4606ca35587Sdholland 	newfl = *fl;
4616ca35587Sdholland 	newfl.l_type = F_UNLCK;
4626ca35587Sdholland 
4636ca35587Sdholland 	oldcred = td->td_ucred;
4646ca35587Sdholland 	nlm_set_creds_for_lock(td, &newfl);
4656ca35587Sdholland 
4666ca35587Sdholland 	error = nlm_advlock_internal(vp, NULL, F_UNLCK, &newfl, F_REMOTE,
4676ca35587Sdholland 	    FALSE, FALSE);
4686ca35587Sdholland 
4696ca35587Sdholland 	crfree(td->td_ucred);
4706ca35587Sdholland 	td->td_ucred = oldcred;
4716ca35587Sdholland 
4726ca35587Sdholland 	return (error);
4736ca35587Sdholland }
4746ca35587Sdholland 
4756ca35587Sdholland int
nlm_reclaim(struct vop_reclaim_args * ap)4766ca35587Sdholland nlm_reclaim(struct vop_reclaim_args *ap)
4776ca35587Sdholland {
4786ca35587Sdholland 
4796ca35587Sdholland 	nlm_cancel_wait(ap->a_vp);
4806ca35587Sdholland 	lf_iteratelocks_vnode(ap->a_vp, nlm_reclaim_free_lock, NULL);
4816ca35587Sdholland 	return (0);
4826ca35587Sdholland }
4836ca35587Sdholland 
4846ca35587Sdholland struct nlm_recovery_context {
4856ca35587Sdholland 	struct nlm_host	*nr_host;	/* host we are recovering */
4866ca35587Sdholland 	int		nr_state;	/* remote NSM state for recovery */
4876ca35587Sdholland };
4886ca35587Sdholland 
4896ca35587Sdholland static int
nlm_client_recover_lock(struct vnode * vp,struct flock * fl,void * arg)4906ca35587Sdholland nlm_client_recover_lock(struct vnode *vp, struct flock *fl, void *arg)
4916ca35587Sdholland {
4926ca35587Sdholland 	struct nlm_recovery_context *nr = (struct nlm_recovery_context *) arg;
4936ca35587Sdholland 	struct thread *td = curthread;
4946ca35587Sdholland 	struct ucred *oldcred;
4956ca35587Sdholland 	int state, error;
4966ca35587Sdholland 
4976ca35587Sdholland 	/*
4986ca35587Sdholland 	 * If the remote NSM state changes during recovery, the host
4996ca35587Sdholland 	 * must have rebooted a second time. In that case, we must
5006ca35587Sdholland 	 * restart the recovery.
5016ca35587Sdholland 	 */
5026ca35587Sdholland 	state = nlm_host_get_state(nr->nr_host);
5036ca35587Sdholland 	if (nr->nr_state != state)
5046ca35587Sdholland 		return (ERESTART);
5056ca35587Sdholland 
5066ca35587Sdholland 	error = vn_lock(vp, LK_SHARED);
5076ca35587Sdholland 	if (error)
5086ca35587Sdholland 		return (error);
5096ca35587Sdholland 
5106ca35587Sdholland 	oldcred = td->td_ucred;
5116ca35587Sdholland 	nlm_set_creds_for_lock(td, fl);
5126ca35587Sdholland 
5136ca35587Sdholland 	error = nlm_advlock_internal(vp, NULL, F_SETLK, fl, F_REMOTE,
5146ca35587Sdholland 	    TRUE, TRUE);
5156ca35587Sdholland 
5166ca35587Sdholland 	crfree(td->td_ucred);
5176ca35587Sdholland 	td->td_ucred = oldcred;
5186ca35587Sdholland 
5196ca35587Sdholland 	return (error);
5206ca35587Sdholland }
5216ca35587Sdholland 
5226ca35587Sdholland void
nlm_client_recovery(struct nlm_host * host)5236ca35587Sdholland nlm_client_recovery(struct nlm_host *host)
5246ca35587Sdholland {
5256ca35587Sdholland 	struct nlm_recovery_context nr;
5266ca35587Sdholland 	int sysid, error;
5276ca35587Sdholland 
5286ca35587Sdholland 	sysid = NLM_SYSID_CLIENT | nlm_host_get_sysid(host);
5296ca35587Sdholland 	do {
5306ca35587Sdholland 		nr.nr_host = host;
5316ca35587Sdholland 		nr.nr_state = nlm_host_get_state(host);
5326ca35587Sdholland 		error = lf_iteratelocks_sysid(sysid,
5336ca35587Sdholland 		    nlm_client_recover_lock, &nr);
5346ca35587Sdholland 	} while (error == ERESTART);
5356ca35587Sdholland }
5366ca35587Sdholland 
5376ca35587Sdholland static void
nlm_convert_to_nlm_lock(struct nlm_lock * dst,struct nlm4_lock * src)5386ca35587Sdholland nlm_convert_to_nlm_lock(struct nlm_lock *dst, struct nlm4_lock *src)
5396ca35587Sdholland {
5406ca35587Sdholland 
5416ca35587Sdholland 	dst->caller_name = src->caller_name;
5426ca35587Sdholland 	dst->fh = src->fh;
5436ca35587Sdholland 	dst->oh = src->oh;
5446ca35587Sdholland 	dst->svid = src->svid;
5456ca35587Sdholland 	dst->l_offset = src->l_offset;
5466ca35587Sdholland 	dst->l_len = src->l_len;
5476ca35587Sdholland }
5486ca35587Sdholland 
5496ca35587Sdholland static void
nlm_convert_to_nlm4_holder(struct nlm4_holder * dst,struct nlm_holder * src)5506ca35587Sdholland nlm_convert_to_nlm4_holder(struct nlm4_holder *dst, struct nlm_holder *src)
5516ca35587Sdholland {
5526ca35587Sdholland 
5536ca35587Sdholland 	dst->exclusive = src->exclusive;
5546ca35587Sdholland 	dst->svid = src->svid;
5556ca35587Sdholland 	dst->oh = src->oh;
5566ca35587Sdholland 	dst->l_offset = src->l_offset;
5576ca35587Sdholland 	dst->l_len = src->l_len;
5586ca35587Sdholland }
5596ca35587Sdholland 
5606ca35587Sdholland static void
nlm_convert_to_nlm4_res(struct nlm4_res * dst,struct nlm_res * src)5616ca35587Sdholland nlm_convert_to_nlm4_res(struct nlm4_res *dst, struct nlm_res *src)
5626ca35587Sdholland {
5636ca35587Sdholland 	dst->cookie = src->cookie;
5646ca35587Sdholland 	dst->stat.stat = (enum nlm4_stats) src->stat.stat;
5656ca35587Sdholland }
5666ca35587Sdholland 
5676ca35587Sdholland static enum clnt_stat
nlm_test_rpc(rpcvers_t vers,nlm4_testargs * args,nlm4_testres * res,CLIENT * client,struct rpc_callextra * ext,struct timeval timo)5686ca35587Sdholland nlm_test_rpc(rpcvers_t vers, nlm4_testargs *args, nlm4_testres *res, CLIENT *client,
5696ca35587Sdholland     struct rpc_callextra *ext, struct timeval timo)
5706ca35587Sdholland {
5716ca35587Sdholland 	if (vers == NLM_VERS4) {
5726ca35587Sdholland 		return nlm4_test_4(args, res, client, ext, timo);
5736ca35587Sdholland 	} else {
5746ca35587Sdholland 		nlm_testargs args1;
5756ca35587Sdholland 		nlm_testres res1;
5766ca35587Sdholland 		enum clnt_stat stat;
5776ca35587Sdholland 
5786ca35587Sdholland 		args1.cookie = args->cookie;
5796ca35587Sdholland 		args1.exclusive = args->exclusive;
5806ca35587Sdholland 		nlm_convert_to_nlm_lock(&args1.alock, &args->alock);
5816ca35587Sdholland 		memset(&res1, 0, sizeof(res1));
5826ca35587Sdholland 
5836ca35587Sdholland 		stat = nlm_test_1(&args1, &res1, client, ext, timo);
5846ca35587Sdholland 
5856ca35587Sdholland 		if (stat == RPC_SUCCESS) {
5866ca35587Sdholland 			res->cookie = res1.cookie;
5876ca35587Sdholland 			res->stat.stat = (enum nlm4_stats) res1.stat.stat;
5886ca35587Sdholland 			if (res1.stat.stat == nlm_denied)
5896ca35587Sdholland 				nlm_convert_to_nlm4_holder(
5906ca35587Sdholland 					&res->stat.nlm4_testrply_u.holder,
5916ca35587Sdholland 					&res1.stat.nlm_testrply_u.holder);
5926ca35587Sdholland 		}
5936ca35587Sdholland 
5946ca35587Sdholland 		return (stat);
5956ca35587Sdholland 	}
5966ca35587Sdholland }
5976ca35587Sdholland 
5986ca35587Sdholland static enum clnt_stat
nlm_lock_rpc(rpcvers_t vers,nlm4_lockargs * args,nlm4_res * res,CLIENT * client,struct rpc_callextra * ext,struct timeval timo)5996ca35587Sdholland nlm_lock_rpc(rpcvers_t vers, nlm4_lockargs *args, nlm4_res *res, CLIENT *client,
6006ca35587Sdholland     struct rpc_callextra *ext, struct timeval timo)
6016ca35587Sdholland {
6026ca35587Sdholland 	if (vers == NLM_VERS4) {
6036ca35587Sdholland 		return nlm4_lock_4(args, res, client, ext, timo);
6046ca35587Sdholland 	} else {
6056ca35587Sdholland 		nlm_lockargs args1;
6066ca35587Sdholland 		nlm_res res1;
6076ca35587Sdholland 		enum clnt_stat stat;
6086ca35587Sdholland 
6096ca35587Sdholland 		args1.cookie = args->cookie;
6106ca35587Sdholland 		args1.block = args->block;
6116ca35587Sdholland 		args1.exclusive = args->exclusive;
6126ca35587Sdholland 		nlm_convert_to_nlm_lock(&args1.alock, &args->alock);
6136ca35587Sdholland 		args1.reclaim = args->reclaim;
6146ca35587Sdholland 		args1.state = args->state;
6156ca35587Sdholland 		memset(&res1, 0, sizeof(res1));
6166ca35587Sdholland 
6176ca35587Sdholland 		stat = nlm_lock_1(&args1, &res1, client, ext, timo);
6186ca35587Sdholland 
6196ca35587Sdholland 		if (stat == RPC_SUCCESS) {
6206ca35587Sdholland 			nlm_convert_to_nlm4_res(res, &res1);
6216ca35587Sdholland 		}
6226ca35587Sdholland 
6236ca35587Sdholland 		return (stat);
6246ca35587Sdholland 	}
6256ca35587Sdholland }
6266ca35587Sdholland 
6276ca35587Sdholland static enum clnt_stat
nlm_cancel_rpc(rpcvers_t vers,nlm4_cancargs * args,nlm4_res * res,CLIENT * client,struct rpc_callextra * ext,struct timeval timo)6286ca35587Sdholland nlm_cancel_rpc(rpcvers_t vers, nlm4_cancargs *args, nlm4_res *res, CLIENT *client,
6296ca35587Sdholland     struct rpc_callextra *ext, struct timeval timo)
6306ca35587Sdholland {
6316ca35587Sdholland 	if (vers == NLM_VERS4) {
6326ca35587Sdholland 		return nlm4_cancel_4(args, res, client, ext, timo);
6336ca35587Sdholland 	} else {
6346ca35587Sdholland 		nlm_cancargs args1;
6356ca35587Sdholland 		nlm_res res1;
6366ca35587Sdholland 		enum clnt_stat stat;
6376ca35587Sdholland 
6386ca35587Sdholland 		args1.cookie = args->cookie;
6396ca35587Sdholland 		args1.block = args->block;
6406ca35587Sdholland 		args1.exclusive = args->exclusive;
6416ca35587Sdholland 		nlm_convert_to_nlm_lock(&args1.alock, &args->alock);
6426ca35587Sdholland 		memset(&res1, 0, sizeof(res1));
6436ca35587Sdholland 
6446ca35587Sdholland 		stat = nlm_cancel_1(&args1, &res1, client, ext, timo);
6456ca35587Sdholland 
6466ca35587Sdholland 		if (stat == RPC_SUCCESS) {
6476ca35587Sdholland 			nlm_convert_to_nlm4_res(res, &res1);
6486ca35587Sdholland 		}
6496ca35587Sdholland 
6506ca35587Sdholland 		return (stat);
6516ca35587Sdholland 	}
6526ca35587Sdholland }
6536ca35587Sdholland 
6546ca35587Sdholland static enum clnt_stat
nlm_unlock_rpc(rpcvers_t vers,nlm4_unlockargs * args,nlm4_res * res,CLIENT * client,struct rpc_callextra * ext,struct timeval timo)6556ca35587Sdholland nlm_unlock_rpc(rpcvers_t vers, nlm4_unlockargs *args, nlm4_res *res, CLIENT *client,
6566ca35587Sdholland     struct rpc_callextra *ext, struct timeval timo)
6576ca35587Sdholland {
6586ca35587Sdholland 	if (vers == NLM_VERS4) {
6596ca35587Sdholland 		return nlm4_unlock_4(args, res, client, ext, timo);
6606ca35587Sdholland 	} else {
6616ca35587Sdholland 		nlm_unlockargs args1;
6626ca35587Sdholland 		nlm_res res1;
6636ca35587Sdholland 		enum clnt_stat stat;
6646ca35587Sdholland 
6656ca35587Sdholland 		args1.cookie = args->cookie;
6666ca35587Sdholland 		nlm_convert_to_nlm_lock(&args1.alock, &args->alock);
6676ca35587Sdholland 		memset(&res1, 0, sizeof(res1));
6686ca35587Sdholland 
6696ca35587Sdholland 		stat = nlm_unlock_1(&args1, &res1, client, ext, timo);
6706ca35587Sdholland 
6716ca35587Sdholland 		if (stat == RPC_SUCCESS) {
6726ca35587Sdholland 			nlm_convert_to_nlm4_res(res, &res1);
6736ca35587Sdholland 		}
6746ca35587Sdholland 
6756ca35587Sdholland 		return (stat);
6766ca35587Sdholland 	}
6776ca35587Sdholland }
6786ca35587Sdholland 
6796ca35587Sdholland /*
6806ca35587Sdholland  * Called after a lock request (set or clear) succeeded. We record the
6816ca35587Sdholland  * details in the local lock manager. Note that since the remote
6826ca35587Sdholland  * server has granted the lock, we can be sure that it doesn't
6836ca35587Sdholland  * conflict with any other locks we have in the local lock manager.
6846ca35587Sdholland  *
6856ca35587Sdholland  * Since it is possible that host may also make NLM client requests to
6866ca35587Sdholland  * our NLM server, we use a different sysid value to record our own
6876ca35587Sdholland  * client locks.
6886ca35587Sdholland  *
6896ca35587Sdholland  * Note that since it is possible for us to receive replies from the
6906ca35587Sdholland  * server in a different order than the locks were granted (e.g. if
6916ca35587Sdholland  * many local threads are contending for the same lock), we must use a
6926ca35587Sdholland  * blocking operation when registering with the local lock manager.
6936ca35587Sdholland  * We expect that any actual wait will be rare and short hence we
6946ca35587Sdholland  * ignore signals for this.
6956ca35587Sdholland  */
6966ca35587Sdholland static void
nlm_record_lock(struct vnode * vp,int op,struct flock * fl,int svid,int sysid,off_t size)6976ca35587Sdholland nlm_record_lock(struct vnode *vp, int op, struct flock *fl,
6986ca35587Sdholland     int svid, int sysid, off_t size)
6996ca35587Sdholland {
7006ca35587Sdholland 	struct vop_advlockasync_args a;
7016ca35587Sdholland 	struct flock newfl;
702e81f0ea2Spgoyette 	struct proc *p;
703e81f0ea2Spgoyette 	int error, stops_deferred;
7046ca35587Sdholland 
7056ca35587Sdholland 	a.a_vp = vp;
7066ca35587Sdholland 	a.a_id = NULL;
7076ca35587Sdholland 	a.a_op = op;
7086ca35587Sdholland 	a.a_fl = &newfl;
7096ca35587Sdholland 	a.a_flags = F_REMOTE|F_WAIT|F_NOINTR;
7106ca35587Sdholland 	a.a_task = NULL;
7116ca35587Sdholland 	a.a_cookiep = NULL;
7126ca35587Sdholland 	newfl.l_start = fl->l_start;
7136ca35587Sdholland 	newfl.l_len = fl->l_len;
7146ca35587Sdholland 	newfl.l_type = fl->l_type;
7156ca35587Sdholland 	newfl.l_whence = fl->l_whence;
7166ca35587Sdholland 	newfl.l_pid = svid;
7176ca35587Sdholland 	newfl.l_sysid = NLM_SYSID_CLIENT | sysid;
7186ca35587Sdholland 
719e81f0ea2Spgoyette 	for (;;) {
7206ca35587Sdholland 		error = lf_advlockasync(&a, &vp->v_lockf, size);
721e81f0ea2Spgoyette 		if (error == EDEADLK) {
722e81f0ea2Spgoyette 			/*
723e81f0ea2Spgoyette 			 * Locks are associated with the processes and
724e81f0ea2Spgoyette 			 * not with threads.  Suppose we have two
725e81f0ea2Spgoyette 			 * threads A1 A2 in one process, A1 locked
726e81f0ea2Spgoyette 			 * file f1, A2 is locking file f2, and A1 is
727e81f0ea2Spgoyette 			 * unlocking f1. Then remote server may
728e81f0ea2Spgoyette 			 * already unlocked f1, while local still not
729e81f0ea2Spgoyette 			 * yet scheduled A1 to make the call to local
730e81f0ea2Spgoyette 			 * advlock manager. The process B owns lock on
731e81f0ea2Spgoyette 			 * f2 and issued the lock on f1.  Remote would
732e81f0ea2Spgoyette 			 * grant B the request on f1, but local would
733e81f0ea2Spgoyette 			 * return EDEADLK.
734e81f0ea2Spgoyette 			*/
735e81f0ea2Spgoyette 			pause("nlmdlk", 1);
736e81f0ea2Spgoyette 			p = curproc;
737e81f0ea2Spgoyette 			stops_deferred = sigdeferstop(SIGDEFERSTOP_OFF);
738e81f0ea2Spgoyette 			PROC_LOCK(p);
739e81f0ea2Spgoyette 			thread_suspend_check(0);
740e81f0ea2Spgoyette 			PROC_UNLOCK(p);
741e81f0ea2Spgoyette 			sigallowstop(stops_deferred);
742e81f0ea2Spgoyette 		} else if (error == EINTR) {
743e81f0ea2Spgoyette 			/*
744e81f0ea2Spgoyette 			 * lf_purgelocks() might wake up the lock
745e81f0ea2Spgoyette 			 * waiter and removed our lock graph edges.
746e81f0ea2Spgoyette 			 * There is no sense in re-trying recording
747e81f0ea2Spgoyette 			 * the lock to the local manager after
748e81f0ea2Spgoyette 			 * reclaim.
749e81f0ea2Spgoyette 			 */
750e81f0ea2Spgoyette 			error = 0;
751e81f0ea2Spgoyette 			break;
752e81f0ea2Spgoyette 		} else
753e81f0ea2Spgoyette 			break;
754e81f0ea2Spgoyette 	}
7556ca35587Sdholland 	KASSERT(error == 0 || error == ENOENT,
7566ca35587Sdholland 	    ("Failed to register NFS lock locally - error=%d", error));
7576ca35587Sdholland }
7586ca35587Sdholland 
7596ca35587Sdholland static int
nlm_setlock(struct nlm_host * host,struct rpc_callextra * ext,rpcvers_t vers,struct timeval * timo,int retries,struct vnode * vp,int op,struct flock * fl,int flags,int svid,size_t fhlen,void * fh,off_t size,bool_t reclaim)7606ca35587Sdholland nlm_setlock(struct nlm_host *host, struct rpc_callextra *ext,
7616ca35587Sdholland     rpcvers_t vers, struct timeval *timo, int retries,
7626ca35587Sdholland     struct vnode *vp, int op, struct flock *fl, int flags,
7636ca35587Sdholland     int svid, size_t fhlen, void *fh, off_t size, bool_t reclaim)
7646ca35587Sdholland {
7656ca35587Sdholland 	struct nlm4_lockargs args;
7666ca35587Sdholland 	char oh_space[32];
7676ca35587Sdholland 	struct nlm4_res res;
7686ca35587Sdholland 	u_int xid;
7696ca35587Sdholland 	CLIENT *client;
7706ca35587Sdholland 	enum clnt_stat stat;
7716ca35587Sdholland 	int retry, block, exclusive;
7726ca35587Sdholland 	void *wait_handle = NULL;
7736ca35587Sdholland 	int error;
7746ca35587Sdholland 
7756ca35587Sdholland 	memset(&args, 0, sizeof(args));
7766ca35587Sdholland 	memset(&res, 0, sizeof(res));
7776ca35587Sdholland 
7786ca35587Sdholland 	block = (flags & F_WAIT) ? TRUE : FALSE;
7796ca35587Sdholland 	exclusive = (fl->l_type == F_WRLCK);
7806ca35587Sdholland 
7816ca35587Sdholland 	error = nlm_init_lock(fl, flags, svid, vers, fhlen, fh, size,
7826ca35587Sdholland 	    &args.alock, oh_space);
7836ca35587Sdholland 	if (error)
7846ca35587Sdholland 		return (error);
7856ca35587Sdholland 	args.block = block;
7866ca35587Sdholland 	args.exclusive = exclusive;
7876ca35587Sdholland 	args.reclaim = reclaim;
7886ca35587Sdholland 	args.state = nlm_nsm_state;
7896ca35587Sdholland 
7906ca35587Sdholland 	retry = 5*hz;
7916ca35587Sdholland 	for (;;) {
7926ca35587Sdholland 		client = nlm_host_get_rpc(host, FALSE);
7936ca35587Sdholland 		if (!client)
7946ca35587Sdholland 			return (ENOLCK); /* XXX retry? */
7956ca35587Sdholland 
7966ca35587Sdholland 		if (block)
7976ca35587Sdholland 			wait_handle = nlm_register_wait_lock(&args.alock, vp);
7986ca35587Sdholland 
7996ca35587Sdholland 		xid = atomic_fetchadd_int(&nlm_xid, 1);
8006ca35587Sdholland 		args.cookie.n_len = sizeof(xid);
8016ca35587Sdholland 		args.cookie.n_bytes = (char*) &xid;
8026ca35587Sdholland 
8036ca35587Sdholland 		stat = nlm_lock_rpc(vers, &args, &res, client, ext, *timo);
8046ca35587Sdholland 
8056ca35587Sdholland 		CLNT_RELEASE(client);
8066ca35587Sdholland 
8076ca35587Sdholland 		if (stat != RPC_SUCCESS) {
8086ca35587Sdholland 			if (block)
8096ca35587Sdholland 				nlm_deregister_wait_lock(wait_handle);
8106ca35587Sdholland 			if (retries) {
8116ca35587Sdholland 				retries--;
8126ca35587Sdholland 				continue;
8136ca35587Sdholland 			}
8146ca35587Sdholland 			return (EINVAL);
8156ca35587Sdholland 		}
8166ca35587Sdholland 
8176ca35587Sdholland 		/*
8186ca35587Sdholland 		 * Free res.cookie.
8196ca35587Sdholland 		 */
8206ca35587Sdholland 		xdr_free((xdrproc_t) xdr_nlm4_res, &res);
8216ca35587Sdholland 
8226ca35587Sdholland 		if (block && res.stat.stat != nlm4_blocked)
8236ca35587Sdholland 			nlm_deregister_wait_lock(wait_handle);
8246ca35587Sdholland 
8256ca35587Sdholland 		if (res.stat.stat == nlm4_denied_grace_period) {
8266ca35587Sdholland 			/*
8276ca35587Sdholland 			 * The server has recently rebooted and is
8286ca35587Sdholland 			 * giving old clients a change to reclaim
8296ca35587Sdholland 			 * their locks. Wait for a few seconds and try
8306ca35587Sdholland 			 * again.
8316ca35587Sdholland 			 */
8326ca35587Sdholland 			error = tsleep(&args, PCATCH, "nlmgrace", retry);
8336ca35587Sdholland 			if (error && error != EWOULDBLOCK)
8346ca35587Sdholland 				return (error);
8356ca35587Sdholland 			retry = 2*retry;
8366ca35587Sdholland 			if (retry > 30*hz)
8376ca35587Sdholland 				retry = 30*hz;
8386ca35587Sdholland 			continue;
8396ca35587Sdholland 		}
8406ca35587Sdholland 
8416ca35587Sdholland 		if (block && res.stat.stat == nlm4_blocked) {
8426ca35587Sdholland 			/*
8436ca35587Sdholland 			 * The server should call us back with a
8446ca35587Sdholland 			 * granted message when the lock succeeds. In
8456ca35587Sdholland 			 * order to deal with broken servers, lost
8466ca35587Sdholland 			 * granted messages and server reboots, we
8476ca35587Sdholland 			 * will also re-try every few seconds.
8486ca35587Sdholland 			 */
8496ca35587Sdholland 			error = nlm_wait_lock(wait_handle, retry);
8506ca35587Sdholland 			if (error == EWOULDBLOCK) {
8516ca35587Sdholland 				retry = 2*retry;
8526ca35587Sdholland 				if (retry > 30*hz)
8536ca35587Sdholland 					retry = 30*hz;
8546ca35587Sdholland 				continue;
8556ca35587Sdholland 			}
8566ca35587Sdholland 			if (error) {
8576ca35587Sdholland 				/*
8586ca35587Sdholland 				 * We need to call the server to
8596ca35587Sdholland 				 * cancel our lock request.
8606ca35587Sdholland 				 */
8616ca35587Sdholland 				nlm4_cancargs cancel;
8626ca35587Sdholland 
8636ca35587Sdholland 				memset(&cancel, 0, sizeof(cancel));
8646ca35587Sdholland 
8656ca35587Sdholland 				xid = atomic_fetchadd_int(&nlm_xid, 1);
8666ca35587Sdholland 				cancel.cookie.n_len = sizeof(xid);
8676ca35587Sdholland 				cancel.cookie.n_bytes = (char*) &xid;
8686ca35587Sdholland 				cancel.block = block;
8696ca35587Sdholland 				cancel.exclusive = exclusive;
8706ca35587Sdholland 				cancel.alock = args.alock;
8716ca35587Sdholland 
8726ca35587Sdholland 				do {
8736ca35587Sdholland 					client = nlm_host_get_rpc(host, FALSE);
8746ca35587Sdholland 					if (!client)
8756ca35587Sdholland 						/* XXX retry? */
8766ca35587Sdholland 						return (ENOLCK);
8776ca35587Sdholland 
8786ca35587Sdholland 					stat = nlm_cancel_rpc(vers, &cancel,
8796ca35587Sdholland 					    &res, client, ext, *timo);
8806ca35587Sdholland 
8816ca35587Sdholland 					CLNT_RELEASE(client);
8826ca35587Sdholland 
8836ca35587Sdholland 					if (stat != RPC_SUCCESS) {
8846ca35587Sdholland 						/*
8856ca35587Sdholland 						 * We need to cope
8866ca35587Sdholland 						 * with temporary
8876ca35587Sdholland 						 * network partitions
8886ca35587Sdholland 						 * as well as server
8896ca35587Sdholland 						 * reboots. This means
8906ca35587Sdholland 						 * we have to keep
8916ca35587Sdholland 						 * trying to cancel
8926ca35587Sdholland 						 * until the server
8936ca35587Sdholland 						 * wakes up again.
8946ca35587Sdholland 						 */
8956ca35587Sdholland 						pause("nlmcancel", 10*hz);
8966ca35587Sdholland 					}
8976ca35587Sdholland 				} while (stat != RPC_SUCCESS);
8986ca35587Sdholland 
8996ca35587Sdholland 				/*
9006ca35587Sdholland 				 * Free res.cookie.
9016ca35587Sdholland 				 */
9026ca35587Sdholland 				xdr_free((xdrproc_t) xdr_nlm4_res, &res);
9036ca35587Sdholland 
9046ca35587Sdholland 				switch (res.stat.stat) {
9056ca35587Sdholland 				case nlm_denied:
9066ca35587Sdholland 					/*
9076ca35587Sdholland 					 * There was nothing
9086ca35587Sdholland 					 * to cancel. We are
9096ca35587Sdholland 					 * going to go ahead
9106ca35587Sdholland 					 * and assume we got
9116ca35587Sdholland 					 * the lock.
9126ca35587Sdholland 					 */
9136ca35587Sdholland 					error = 0;
9146ca35587Sdholland 					break;
9156ca35587Sdholland 
9166ca35587Sdholland 				case nlm4_denied_grace_period:
9176ca35587Sdholland 					/*
9186ca35587Sdholland 					 * The server has
9196ca35587Sdholland 					 * recently rebooted -
9206ca35587Sdholland 					 * treat this as a
9216ca35587Sdholland 					 * successful
9226ca35587Sdholland 					 * cancellation.
9236ca35587Sdholland 					 */
9246ca35587Sdholland 					break;
9256ca35587Sdholland 
9266ca35587Sdholland 				case nlm4_granted:
9276ca35587Sdholland 					/*
9286ca35587Sdholland 					 * We managed to
9296ca35587Sdholland 					 * cancel.
9306ca35587Sdholland 					 */
9316ca35587Sdholland 					break;
9326ca35587Sdholland 
9336ca35587Sdholland 				default:
9346ca35587Sdholland 					/*
9356ca35587Sdholland 					 * Broken server
9366ca35587Sdholland 					 * implementation -
9376ca35587Sdholland 					 * can't really do
9386ca35587Sdholland 					 * anything here.
9396ca35587Sdholland 					 */
9406ca35587Sdholland 					break;
9416ca35587Sdholland 				}
9426ca35587Sdholland 
9436ca35587Sdholland 			}
9446ca35587Sdholland 		} else {
9456ca35587Sdholland 			error = nlm_map_status(res.stat.stat);
9466ca35587Sdholland 		}
9476ca35587Sdholland 
9486ca35587Sdholland 		if (!error && !reclaim) {
9496ca35587Sdholland 			nlm_record_lock(vp, op, fl, args.alock.svid,
9506ca35587Sdholland 			    nlm_host_get_sysid(host), size);
9516ca35587Sdholland 			nlm_host_monitor(host, 0);
9526ca35587Sdholland 		}
9536ca35587Sdholland 
9546ca35587Sdholland 		return (error);
9556ca35587Sdholland 	}
9566ca35587Sdholland }
9576ca35587Sdholland 
9586ca35587Sdholland static int
nlm_clearlock(struct nlm_host * host,struct rpc_callextra * ext,rpcvers_t vers,struct timeval * timo,int retries,struct vnode * vp,int op,struct flock * fl,int flags,int svid,size_t fhlen,void * fh,off_t size)9596ca35587Sdholland nlm_clearlock(struct nlm_host *host, struct rpc_callextra *ext,
9606ca35587Sdholland     rpcvers_t vers, struct timeval *timo, int retries,
9616ca35587Sdholland     struct vnode *vp, int op, struct flock *fl, int flags,
9626ca35587Sdholland     int svid, size_t fhlen, void *fh, off_t size)
9636ca35587Sdholland {
9646ca35587Sdholland 	struct nlm4_unlockargs args;
9656ca35587Sdholland 	char oh_space[32];
9666ca35587Sdholland 	struct nlm4_res res;
9676ca35587Sdholland 	u_int xid;
9686ca35587Sdholland 	CLIENT *client;
9696ca35587Sdholland 	enum clnt_stat stat;
9706ca35587Sdholland 	int error;
9716ca35587Sdholland 
9726ca35587Sdholland 	memset(&args, 0, sizeof(args));
9736ca35587Sdholland 	memset(&res, 0, sizeof(res));
9746ca35587Sdholland 
9756ca35587Sdholland 	error = nlm_init_lock(fl, flags, svid, vers, fhlen, fh, size,
9766ca35587Sdholland 	    &args.alock, oh_space);
9776ca35587Sdholland 	if (error)
9786ca35587Sdholland 		return (error);
9796ca35587Sdholland 
9806ca35587Sdholland 	for (;;) {
9816ca35587Sdholland 		client = nlm_host_get_rpc(host, FALSE);
9826ca35587Sdholland 		if (!client)
9836ca35587Sdholland 			return (ENOLCK); /* XXX retry? */
9846ca35587Sdholland 
9856ca35587Sdholland 		xid = atomic_fetchadd_int(&nlm_xid, 1);
9866ca35587Sdholland 		args.cookie.n_len = sizeof(xid);
9876ca35587Sdholland 		args.cookie.n_bytes = (char*) &xid;
9886ca35587Sdholland 
9896ca35587Sdholland 		stat = nlm_unlock_rpc(vers, &args, &res, client, ext, *timo);
9906ca35587Sdholland 
9916ca35587Sdholland 		CLNT_RELEASE(client);
9926ca35587Sdholland 
9936ca35587Sdholland 		if (stat != RPC_SUCCESS) {
9946ca35587Sdholland 			if (retries) {
9956ca35587Sdholland 				retries--;
9966ca35587Sdholland 				continue;
9976ca35587Sdholland 			}
9986ca35587Sdholland 			return (EINVAL);
9996ca35587Sdholland 		}
10006ca35587Sdholland 
10016ca35587Sdholland 		/*
10026ca35587Sdholland 		 * Free res.cookie.
10036ca35587Sdholland 		 */
10046ca35587Sdholland 		xdr_free((xdrproc_t) xdr_nlm4_res, &res);
10056ca35587Sdholland 
10066ca35587Sdholland 		if (res.stat.stat == nlm4_denied_grace_period) {
10076ca35587Sdholland 			/*
10086ca35587Sdholland 			 * The server has recently rebooted and is
10096ca35587Sdholland 			 * giving old clients a change to reclaim
10106ca35587Sdholland 			 * their locks. Wait for a few seconds and try
10116ca35587Sdholland 			 * again.
10126ca35587Sdholland 			 */
10136ca35587Sdholland 			error = tsleep(&args, PCATCH, "nlmgrace", 5*hz);
10146ca35587Sdholland 			if (error && error != EWOULDBLOCK)
10156ca35587Sdholland 				return (error);
10166ca35587Sdholland 			continue;
10176ca35587Sdholland 		}
10186ca35587Sdholland 
10196ca35587Sdholland 		/*
10206ca35587Sdholland 		 * If we are being called via nlm_reclaim (which will
10216ca35587Sdholland 		 * use the F_REMOTE flag), don't record the lock
10226ca35587Sdholland 		 * operation in the local lock manager since the vnode
10236ca35587Sdholland 		 * is going away.
10246ca35587Sdholland 		 */
10256ca35587Sdholland 		if (!(flags & F_REMOTE))
10266ca35587Sdholland 			nlm_record_lock(vp, op, fl, args.alock.svid,
10276ca35587Sdholland 			    nlm_host_get_sysid(host), size);
10286ca35587Sdholland 
10296ca35587Sdholland 		return (0);
10306ca35587Sdholland 	}
10316ca35587Sdholland }
10326ca35587Sdholland 
10336ca35587Sdholland static int
nlm_getlock(struct nlm_host * host,struct rpc_callextra * ext,rpcvers_t vers,struct timeval * timo,int retries,struct vnode * vp,int op,struct flock * fl,int flags,int svid,size_t fhlen,void * fh,off_t size)10346ca35587Sdholland nlm_getlock(struct nlm_host *host, struct rpc_callextra *ext,
10356ca35587Sdholland     rpcvers_t vers, struct timeval *timo, int retries,
10366ca35587Sdholland     struct vnode *vp, int op, struct flock *fl, int flags,
10376ca35587Sdholland     int svid, size_t fhlen, void *fh, off_t size)
10386ca35587Sdholland {
10396ca35587Sdholland 	struct nlm4_testargs args;
10406ca35587Sdholland 	char oh_space[32];
10416ca35587Sdholland 	struct nlm4_testres res;
10426ca35587Sdholland 	u_int xid;
10436ca35587Sdholland 	CLIENT *client;
10446ca35587Sdholland 	enum clnt_stat stat;
10456ca35587Sdholland 	int exclusive;
10466ca35587Sdholland 	int error;
10476ca35587Sdholland 
10486ca35587Sdholland 	KASSERT(!(flags & F_FLOCK), ("unexpected F_FLOCK for F_GETLK"));
10496ca35587Sdholland 
10506ca35587Sdholland 	memset(&args, 0, sizeof(args));
10516ca35587Sdholland 	memset(&res, 0, sizeof(res));
10526ca35587Sdholland 
10536ca35587Sdholland 	exclusive = (fl->l_type == F_WRLCK);
10546ca35587Sdholland 
10556ca35587Sdholland 	error = nlm_init_lock(fl, flags, svid, vers, fhlen, fh, size,
10566ca35587Sdholland 	    &args.alock, oh_space);
10576ca35587Sdholland 	if (error)
10586ca35587Sdholland 		return (error);
10596ca35587Sdholland 	args.exclusive = exclusive;
10606ca35587Sdholland 
10616ca35587Sdholland 	for (;;) {
10626ca35587Sdholland 		client = nlm_host_get_rpc(host, FALSE);
10636ca35587Sdholland 		if (!client)
10646ca35587Sdholland 			return (ENOLCK); /* XXX retry? */
10656ca35587Sdholland 
10666ca35587Sdholland 		xid = atomic_fetchadd_int(&nlm_xid, 1);
10676ca35587Sdholland 		args.cookie.n_len = sizeof(xid);
10686ca35587Sdholland 		args.cookie.n_bytes = (char*) &xid;
10696ca35587Sdholland 
10706ca35587Sdholland 		stat = nlm_test_rpc(vers, &args, &res, client, ext, *timo);
10716ca35587Sdholland 
10726ca35587Sdholland 		CLNT_RELEASE(client);
10736ca35587Sdholland 
10746ca35587Sdholland 		if (stat != RPC_SUCCESS) {
10756ca35587Sdholland 			if (retries) {
10766ca35587Sdholland 				retries--;
10776ca35587Sdholland 				continue;
10786ca35587Sdholland 			}
10796ca35587Sdholland 			return (EINVAL);
10806ca35587Sdholland 		}
10816ca35587Sdholland 
10826ca35587Sdholland 		if (res.stat.stat == nlm4_denied_grace_period) {
10836ca35587Sdholland 			/*
10846ca35587Sdholland 			 * The server has recently rebooted and is
10856ca35587Sdholland 			 * giving old clients a change to reclaim
10866ca35587Sdholland 			 * their locks. Wait for a few seconds and try
10876ca35587Sdholland 			 * again.
10886ca35587Sdholland 			 */
10896ca35587Sdholland 			xdr_free((xdrproc_t) xdr_nlm4_testres, &res);
10906ca35587Sdholland 			error = tsleep(&args, PCATCH, "nlmgrace", 5*hz);
10916ca35587Sdholland 			if (error && error != EWOULDBLOCK)
10926ca35587Sdholland 				return (error);
10936ca35587Sdholland 			continue;
10946ca35587Sdholland 		}
10956ca35587Sdholland 
10966ca35587Sdholland 		if (res.stat.stat == nlm4_denied) {
10976ca35587Sdholland 			struct nlm4_holder *h =
10986ca35587Sdholland 				&res.stat.nlm4_testrply_u.holder;
10996ca35587Sdholland 			fl->l_start = h->l_offset;
11006ca35587Sdholland 			fl->l_len = h->l_len;
11016ca35587Sdholland 			fl->l_pid = h->svid;
11026ca35587Sdholland 			if (h->exclusive)
11036ca35587Sdholland 				fl->l_type = F_WRLCK;
11046ca35587Sdholland 			else
11056ca35587Sdholland 				fl->l_type = F_RDLCK;
11066ca35587Sdholland 			fl->l_whence = SEEK_SET;
11076ca35587Sdholland 			fl->l_sysid = 0;
11086ca35587Sdholland 		} else {
11096ca35587Sdholland 			fl->l_type = F_UNLCK;
11106ca35587Sdholland 		}
11116ca35587Sdholland 
11126ca35587Sdholland 		xdr_free((xdrproc_t) xdr_nlm4_testres, &res);
11136ca35587Sdholland 
11146ca35587Sdholland 		return (0);
11156ca35587Sdholland 	}
11166ca35587Sdholland }
11176ca35587Sdholland 
11186ca35587Sdholland static int
nlm_map_status(nlm4_stats stat)11196ca35587Sdholland nlm_map_status(nlm4_stats stat)
11206ca35587Sdholland {
11216ca35587Sdholland 	switch (stat) {
11226ca35587Sdholland 	case nlm4_granted:
11236ca35587Sdholland 		return (0);
11246ca35587Sdholland 
11256ca35587Sdholland 	case nlm4_denied:
11266ca35587Sdholland 		return (EAGAIN);
11276ca35587Sdholland 
11286ca35587Sdholland 	case nlm4_denied_nolocks:
11296ca35587Sdholland 		return (ENOLCK);
11306ca35587Sdholland 
11316ca35587Sdholland 	case nlm4_deadlck:
11326ca35587Sdholland 		return (EDEADLK);
11336ca35587Sdholland 
11346ca35587Sdholland 	case nlm4_rofs:
11356ca35587Sdholland 		return (EROFS);
11366ca35587Sdholland 
11376ca35587Sdholland 	case nlm4_stale_fh:
11386ca35587Sdholland 		return (ESTALE);
11396ca35587Sdholland 
11406ca35587Sdholland 	case nlm4_fbig:
11416ca35587Sdholland 		return (EFBIG);
11426ca35587Sdholland 
11436ca35587Sdholland 	case nlm4_failed:
11446ca35587Sdholland 		return (EACCES);
11456ca35587Sdholland 
11466ca35587Sdholland 	default:
11476ca35587Sdholland 		return (EINVAL);
11486ca35587Sdholland 	}
11496ca35587Sdholland }
11506ca35587Sdholland 
11516ca35587Sdholland static struct nlm_file_svid *
nlm_find_svid(void * id)11526ca35587Sdholland nlm_find_svid(void *id)
11536ca35587Sdholland {
11546ca35587Sdholland 	struct nlm_file_svid *ns, *newns;
11556ca35587Sdholland 	int h;
11566ca35587Sdholland 
11576ca35587Sdholland 	h = (((uintptr_t) id) >> 7) % NLM_SVID_HASH_SIZE;
11586ca35587Sdholland 
11596ca35587Sdholland 	mtx_lock(&nlm_svid_lock);
11606ca35587Sdholland 	LIST_FOREACH(ns, &nlm_file_svids[h], ns_link) {
11616ca35587Sdholland 		if (ns->ns_id == id) {
11626ca35587Sdholland 			ns->ns_refs++;
11636ca35587Sdholland 			break;
11646ca35587Sdholland 		}
11656ca35587Sdholland 	}
11666ca35587Sdholland 	mtx_unlock(&nlm_svid_lock);
11676ca35587Sdholland 	if (!ns) {
11686ca35587Sdholland 		int svid = alloc_unr(nlm_svid_allocator);
11696ca35587Sdholland 		newns = malloc(sizeof(struct nlm_file_svid), M_NLM,
11706ca35587Sdholland 		    M_WAITOK);
11716ca35587Sdholland 		newns->ns_refs = 1;
11726ca35587Sdholland 		newns->ns_id = id;
11736ca35587Sdholland 		newns->ns_svid = svid;
11746ca35587Sdholland 		newns->ns_ucred = NULL;
11756ca35587Sdholland 		newns->ns_active = FALSE;
11766ca35587Sdholland 
11776ca35587Sdholland 		/*
11786ca35587Sdholland 		 * We need to check for a race with some other
11796ca35587Sdholland 		 * thread allocating a svid for this file.
11806ca35587Sdholland 		 */
11816ca35587Sdholland 		mtx_lock(&nlm_svid_lock);
11826ca35587Sdholland 		LIST_FOREACH(ns, &nlm_file_svids[h], ns_link) {
11836ca35587Sdholland 			if (ns->ns_id == id) {
11846ca35587Sdholland 				ns->ns_refs++;
11856ca35587Sdholland 				break;
11866ca35587Sdholland 			}
11876ca35587Sdholland 		}
11886ca35587Sdholland 		if (ns) {
11896ca35587Sdholland 			mtx_unlock(&nlm_svid_lock);
11906ca35587Sdholland 			free_unr(nlm_svid_allocator, newns->ns_svid);
11916ca35587Sdholland 			free(newns, M_NLM);
11926ca35587Sdholland 		} else {
11936ca35587Sdholland 			LIST_INSERT_HEAD(&nlm_file_svids[h], newns,
11946ca35587Sdholland 			    ns_link);
11956ca35587Sdholland 			ns = newns;
11966ca35587Sdholland 			mtx_unlock(&nlm_svid_lock);
11976ca35587Sdholland 		}
11986ca35587Sdholland 	}
11996ca35587Sdholland 
12006ca35587Sdholland 	return (ns);
12016ca35587Sdholland }
12026ca35587Sdholland 
12036ca35587Sdholland static void
nlm_free_svid(struct nlm_file_svid * ns)12046ca35587Sdholland nlm_free_svid(struct nlm_file_svid *ns)
12056ca35587Sdholland {
12066ca35587Sdholland 
12076ca35587Sdholland 	mtx_lock(&nlm_svid_lock);
12086ca35587Sdholland 	ns->ns_refs--;
12096ca35587Sdholland 	if (!ns->ns_refs) {
12106ca35587Sdholland 		KASSERT(!ns->ns_active, ("Freeing active SVID"));
12116ca35587Sdholland 		LIST_REMOVE(ns, ns_link);
12126ca35587Sdholland 		mtx_unlock(&nlm_svid_lock);
12136ca35587Sdholland 		free_unr(nlm_svid_allocator, ns->ns_svid);
12146ca35587Sdholland 		if (ns->ns_ucred)
12156ca35587Sdholland 			crfree(ns->ns_ucred);
12166ca35587Sdholland 		free(ns, M_NLM);
12176ca35587Sdholland 	} else {
12186ca35587Sdholland 		mtx_unlock(&nlm_svid_lock);
12196ca35587Sdholland 	}
12206ca35587Sdholland }
12216ca35587Sdholland 
12226ca35587Sdholland static int
nlm_init_lock(struct flock * fl,int flags,int svid,rpcvers_t vers,size_t fhlen,void * fh,off_t size,struct nlm4_lock * lock,char oh_space[32])12236ca35587Sdholland nlm_init_lock(struct flock *fl, int flags, int svid,
12246ca35587Sdholland     rpcvers_t vers, size_t fhlen, void *fh, off_t size,
12256ca35587Sdholland     struct nlm4_lock *lock, char oh_space[32])
12266ca35587Sdholland {
12276ca35587Sdholland 	size_t oh_len;
12286ca35587Sdholland 	off_t start, len;
12296ca35587Sdholland 
12306ca35587Sdholland 	if (fl->l_whence == SEEK_END) {
12316ca35587Sdholland 		if (size > OFF_MAX
12326ca35587Sdholland 		    || (fl->l_start > 0 && size > OFF_MAX - fl->l_start))
12336ca35587Sdholland 			return (EOVERFLOW);
12346ca35587Sdholland 		start = size + fl->l_start;
12356ca35587Sdholland 	} else if (fl->l_whence == SEEK_SET || fl->l_whence == SEEK_CUR) {
12366ca35587Sdholland 		start = fl->l_start;
12376ca35587Sdholland 	} else {
12386ca35587Sdholland 		return (EINVAL);
12396ca35587Sdholland 	}
12406ca35587Sdholland 	if (start < 0)
12416ca35587Sdholland 		return (EINVAL);
12426ca35587Sdholland 	if (fl->l_len < 0) {
12436ca35587Sdholland 		len = -fl->l_len;
12446ca35587Sdholland 		start -= len;
12456ca35587Sdholland 		if (start < 0)
12466ca35587Sdholland 			return (EINVAL);
12476ca35587Sdholland 	} else {
12486ca35587Sdholland 		len = fl->l_len;
12496ca35587Sdholland 	}
12506ca35587Sdholland 
12516ca35587Sdholland 	if (vers == NLM_VERS) {
12526ca35587Sdholland 		/*
12536ca35587Sdholland 		 * Enforce range limits on V1 locks
12546ca35587Sdholland 		 */
12556ca35587Sdholland 		if (start > 0xffffffffLL || len > 0xffffffffLL)
12566ca35587Sdholland 			return (EOVERFLOW);
12576ca35587Sdholland 	}
12586ca35587Sdholland 
12596ca35587Sdholland 	snprintf(oh_space, 32, "%d@", svid);
12606ca35587Sdholland 	oh_len = strlen(oh_space);
12616ca35587Sdholland 	getcredhostname(NULL, oh_space + oh_len, 32 - oh_len);
12626ca35587Sdholland 	oh_len = strlen(oh_space);
12636ca35587Sdholland 
12646ca35587Sdholland 	memset(lock, 0, sizeof(*lock));
12656ca35587Sdholland 	lock->caller_name = prison0.pr_hostname;
12666ca35587Sdholland 	lock->fh.n_len = fhlen;
12676ca35587Sdholland 	lock->fh.n_bytes = fh;
12686ca35587Sdholland 	lock->oh.n_len = oh_len;
12696ca35587Sdholland 	lock->oh.n_bytes = oh_space;
12706ca35587Sdholland 	lock->svid = svid;
12716ca35587Sdholland 	lock->l_offset = start;
12726ca35587Sdholland 	lock->l_len = len;
12736ca35587Sdholland 
12746ca35587Sdholland 	return (0);
12756ca35587Sdholland }
1276