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